|
| 1 | +// RUN: %empty-directory(%t) |
| 2 | +// RUN: %target-build-swift %S/Inputs/CrashSubprocess.swift -parse-as-library -Onone -g -o %t/crash |
| 3 | +// RUN: %target-codesign %t/crash |
| 4 | + |
| 5 | +// RUN: %target-build-swift %s -o %t/crash-host |
| 6 | + |
| 7 | +// RUN: (env SWIFT_BACKTRACE=enable=yes,cache=no,interactive=no %target-run %t/crash-host %t/crash)| %FileCheck %s |
| 8 | + |
| 9 | +// UNSUPPORTED: use_os_stdlib |
| 10 | +// UNSUPPORTED: back_deployment_runtime |
| 11 | +// UNSUPPORTED: asan |
| 12 | + |
| 13 | +// REQUIRES: executable_test |
| 14 | +// REQUIRES: backtracing |
| 15 | +// REQUIRES: OS=linux-gnu || OS=macosx |
| 16 | + |
| 17 | +// COM: this test would need significant rework on Windows/WinSDK, but is not critical function so probably not worth porting the test to that platform |
| 18 | + |
| 19 | +#if os(macOS) |
| 20 | +internal import Darwin |
| 21 | +#elseif canImport(Glibc) |
| 22 | +internal import Glibc |
| 23 | +#elseif canImport(Musl) |
| 24 | +internal import Musl |
| 25 | +#endif |
| 26 | + |
| 27 | +guard CommandLine.arguments.count > 1 else { |
| 28 | + fputs("Usage: \(CommandLine.arguments[0]) <path-to-crash>\n", stderr) |
| 29 | + exit(1) |
| 30 | +} |
| 31 | + |
| 32 | +let crashPath = CommandLine.arguments[1] |
| 33 | + |
| 34 | +// CHECK: host stdout is a tty: 0 |
| 35 | +print("host stdout is a tty: \(isatty(1))") |
| 36 | +// (piped output on linux is fully buffered by default) |
| 37 | +fflush(stdout) |
| 38 | + |
| 39 | +// CHECK: host stderr is a tty: 0 |
| 40 | +print("host stderr is a tty: \(isatty(2))") |
| 41 | +fflush(stdout) |
| 42 | + |
| 43 | +var masterFD: Int32 = 0 |
| 44 | +var slaveFD: Int32 = 0 |
| 45 | + |
| 46 | +let slaveName = UnsafeMutablePointer<CChar>.allocate(capacity: 1024) |
| 47 | + |
| 48 | +guard openpty(&masterFD, &slaveFD, slaveName, nil, nil) == 0 else { |
| 49 | + perror("openpty") |
| 50 | + exit(1) |
| 51 | +} |
| 52 | + |
| 53 | +// CHECK: slave is: /dev/{{.+}} |
| 54 | +print("slave is: \(String(cString: slaveName))") |
| 55 | +slaveName.deallocate() |
| 56 | +fflush(stdout) |
| 57 | + |
| 58 | +// try to avoid using Foundation, use Posix primitives instead |
| 59 | +var childPid: pid_t = 0 |
| 60 | + |
| 61 | +#if os(macOS) |
| 62 | +var childFileActions: posix_spawn_file_actions_t? |
| 63 | +posix_spawn_file_actions_init(&childFileActions) |
| 64 | +posix_spawn_file_actions_adddup2(&childFileActions, slaveFD, STDOUT_FILENO) |
| 65 | +posix_spawn_file_actions_adddup2(&childFileActions, slaveFD, STDERR_FILENO) |
| 66 | + |
| 67 | +guard posix_spawn(&childPid, Array<CChar>(crashPath.utf8CString), &childFileActions, nil, nil, environ) == 0 else { |
| 68 | + perror("unable to spawn child") |
| 69 | + exit(1) |
| 70 | +} |
| 71 | + |
| 72 | +posix_spawn_file_actions_destroy(&childFileActions) |
| 73 | + |
| 74 | +#elseif os(Linux) |
| 75 | +var childFileActions = posix_spawn_file_actions_t() |
| 76 | +posix_spawn_file_actions_init(&childFileActions) |
| 77 | +posix_spawn_file_actions_adddup2(&childFileActions, slaveFD, STDOUT_FILENO) |
| 78 | +posix_spawn_file_actions_adddup2(&childFileActions, slaveFD, STDERR_FILENO) |
| 79 | + |
| 80 | +var argv: [UnsafeMutablePointer<CChar>?] = [nil] |
| 81 | + |
| 82 | +guard posix_spawn(&childPid, Array<CChar>(crashPath.utf8CString), &childFileActions, nil, argv, environ) == 0 else { |
| 83 | + perror("unable to spawn child") |
| 84 | + exit(1) |
| 85 | +} |
| 86 | + |
| 87 | +posix_spawn_file_actions_destroy(&childFileActions) |
| 88 | + |
| 89 | +#endif |
| 90 | + |
| 91 | +// CHECK: child pid is {{[0-9]+}} |
| 92 | +print("child pid is \(childPid)") |
| 93 | +fflush(stdout) |
| 94 | + |
| 95 | +// the parent process should now close this FD as it is used by the child for stdout/stderr |
| 96 | +close(slaveFD) |
| 97 | + |
| 98 | +// CHECK: subprocess stdout is a tty: 1 |
| 99 | +// CHECK: subprocess stderr is a tty: 1 |
| 100 | + |
| 101 | +// CHECK: About to crash |
| 102 | + |
| 103 | +// CHECK: 💣{{.*}}Program crashed: Bad pointer dereference at 0x{{0+}}4 |
| 104 | +// CHECK: Thread 0 {{(".*" )?}}crashed: |
| 105 | +// CHECK: Backtrace took {{[0-9.]+}}s |
| 106 | + |
| 107 | +// allow the child process time to start, and crash... |
| 108 | +sleep(1) |
| 109 | + |
| 110 | +// now read the standard output and error from our master pty |
| 111 | +// and write it to our own standard output for scanning by FileCheck |
| 112 | +let bufferSize = 4096 |
| 113 | +let buffer = UnsafeMutablePointer<UInt8>.allocate(capacity: bufferSize) |
| 114 | +var bytesRead = read(masterFD, buffer, bufferSize) |
| 115 | + |
| 116 | +while bytesRead > 0 { |
| 117 | + write(1, buffer, bytesRead) |
| 118 | + bytesRead = read(masterFD, buffer, bufferSize) |
| 119 | +} |
| 120 | + |
| 121 | +close(masterFD) |
| 122 | +buffer.deallocate() |
| 123 | + |
| 124 | +// CHECK-NOT: child exited with status: 0 |
| 125 | +var childExitStatus: Int32 = 0 |
| 126 | +waitpid(childPid, &childExitStatus, 0) |
| 127 | +print("child exited with status: \(childExitStatus)") |
0 commit comments