Repository: apple/swift-nio
File: IntegrationTests/tests_02_syscall_wrappers/test_02_unacceptable_errnos.sh (Shell)

Code before:
let makeEFAULTHappen = CommandLine.arguments.dropFirst().first == .some("EFAULT")
var whatevs: UInt8 = 123
_ = try? withUnsafePointer(to: &whatevs) { ptr in
    print("makeEBADFHappen? \(makeEBADFHappen ? "YES" : "NO")")
    print("makeEFAULTHappen ? \(makeEFAULTHappen ? "YES" : "NO")")
    _ = try Posix.write(descriptor: makeEBADFHappen ? -1 : fds[0],
                        pointer: makeEFAULTHappen ? UnsafePointer<UInt8>(bitPattern: 0xdeadbee)! : ptr,
                     size: 1)
}
exit(42)
EOF

make_package

for mode in debug release; do
    for error in EFAULT EBADF; do
        temp_file="$tmp/stderr"
        if "$swift_binary" run -c "$mode" -Xswiftc -DRUNNING_INTEGRATION_TESTS \
            syscallwrapper "$error" 2> "$temp_file"; then

            fail "exited successfully but was supposed to fail"
        else
            exit_code=$?
            # expecting irrecoverable error as process should be terminated through fatalError/precondition/assert
            assert_greater_than_or_equal $exit_code $(( 128 + 4 ))  # 4 == SIGILL aka illegal instruction, expected on x86
            assert_less_than_or_equal $exit_code $(( 128 + 5 ))  # 5 == SIGTRAP aka trace trap, expected on arm64
            if [[ "$mode" == "debug" ]]; then
                grep -q unacceptable\ errno "$temp_file"
            fi
        fi
    done
done

rm -rf "$tmpdir"


Review comment (bug):
Should we conditionlize this on the platform rather? Otherwise the wrong exit code might be accepted on the wrong platform

Code after:
let makeEFAULTHappen = CommandLine.arguments.dropFirst().first == .some("EFAULT")
var whatevs: UInt8 = 123
_ = try? withUnsafePointer(to: &whatevs) { ptr in
    print("makeEBADFHappen? \(makeEBADFHappen ? "YES" : "NO")")
    print("makeEFAULTHappen ? \(makeEFAULTHappen ? "YES" : "NO")")
    _ = try Posix.write(descriptor: makeEBADFHappen ? -1 : fds[0],
                        pointer: makeEFAULTHappen ? UnsafePointer<UInt8>(bitPattern: 0xdeadbee)! : ptr,
                     size: 1)
}
exit(42)
EOF

make_package

for mode in debug release; do
    for error in EFAULT EBADF; do
        temp_file="$tmp/stderr"
        if "$swift_binary" run -c "$mode" -Xswiftc -DRUNNING_INTEGRATION_TESTS \
            syscallwrapper "$error" 2> "$temp_file"; then

            fail "exited successfully but was supposed to fail"
        else
            exit_code=$?
            
            # expecting irrecoverable error as process should be terminated through fatalError/precondition/assert
            architecture=$(uname -m)
            if [[ $architecture =~ ^(arm|aarch) ]]; then
                assert_equal $exit_code $(( 128 + 5 )) # 5 == SIGTRAP aka trace trap, expected on ARM
            elif [[ $architecture =~ ^(x86|i386) ]]; then
                assert_equal $exit_code $(( 128 + 4 ))  # 4 == SIGILL aka illegal instruction, expected on x86
            else
                fail "unknown CPU architecture for which we don't know the expected signal for a crash"
            fi
            
            if [[ "$mode" == "debug" ]]; then
                grep -q unacceptable\ errno "$temp_file"
            fi
        fi
    done
done

rm -rf "$tmpdir"