|
| 1 | +// RUN: %empty-directory(%t) |
| 2 | +// RUN: %target-build-swift %s -Onone -g -o %t/CrashWithThreads |
| 3 | +// RUN: %target-codesign %t/CrashWithThreads |
| 4 | +// RUN: (env SWIFT_BACKTRACE=enable=yes,cache=no,swift-backtrace=%backtracer %target-run %t/CrashWithThreads 2>&1 || true) | %FileCheck -vv %s -dump-input-filter=all |
| 5 | + |
| 6 | +// UNSUPPORTED: use_os_stdlib |
| 7 | +// UNSUPPORTED: back_deployment_runtime |
| 8 | +// UNSUPPORTED: asan |
| 9 | +// REQUIRES: executable_test |
| 10 | +// REQUIRES: backtracing |
| 11 | +// REQUIRES: OS=macosx || OS=linux-gnu |
| 12 | + |
| 13 | +#if canImport(Darwin) |
| 14 | + import Darwin |
| 15 | +#elseif canImport(Glibc) |
| 16 | + import Glibc |
| 17 | +#elseif canImport(Android) |
| 18 | + import Android |
| 19 | +#elseif os(Windows) |
| 20 | + import CRT |
| 21 | +#else |
| 22 | +#error("Unsupported platform") |
| 23 | +#endif |
| 24 | + |
| 25 | +func reallyCrashMe() { |
| 26 | + print("I'm going to crash now") |
| 27 | + let ptr = UnsafeMutablePointer<Int>(bitPattern: 4)! |
| 28 | + ptr.pointee = 42 |
| 29 | +} |
| 30 | + |
| 31 | +func crashMe() { |
| 32 | + reallyCrashMe() |
| 33 | +} |
| 34 | + |
| 35 | +func spawnThread(_ shouldCrash: Bool) { |
| 36 | + #if os(Linux) |
| 37 | + var thread: pthread_t = 0 |
| 38 | + #elseif os(macOS) |
| 39 | + var thread = pthread_t(nil) |
| 40 | + #endif |
| 41 | + if shouldCrash { |
| 42 | + pthread_create(&thread, nil, { _ in |
| 43 | + crashMe() |
| 44 | + // this should not be run |
| 45 | + while (true) { |
| 46 | + sleep(10) |
| 47 | + } |
| 48 | + }, nil) |
| 49 | + } else { |
| 50 | + pthread_create(&thread, nil, { _ in |
| 51 | + while (true) { |
| 52 | + sleep(10) |
| 53 | + } |
| 54 | + }, nil) |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +let crashingThreadIndex = (1..<10).randomElement() |
| 59 | + |
| 60 | +for threadIndex in 0..<10 { |
| 61 | + spawnThread(threadIndex == crashingThreadIndex) |
| 62 | +} |
| 63 | + |
| 64 | +while (true) { |
| 65 | + sleep(10) |
| 66 | +} |
| 67 | + |
| 68 | +// CHECK: *** Program crashed: Bad pointer dereference at 0x{{0+}}4 *** |
| 69 | + |
| 70 | +// make sure there are no threads before the crashing thread (rdar://164566321) |
| 71 | + |
| 72 | +// CHECK-NOT: Thread {{[0-9]+}}: |
| 73 | + |
| 74 | +// CHECK: Thread {{[1-9]+}} {{(".*" )?}}crashed: |
| 75 | + |
| 76 | +// CHECK: 0 0x{{[0-9a-f]+}} reallyCrashMe() + {{[0-9]+}} in CrashWithThreads at {{.*}}/CrashWithThreads |
| 77 | +// CHECK-NEXT: 1 [ra] 0x{{[0-9a-f]+}} crashMe() + {{[0-9]+}} in CrashWithThreads at {{.*}}/CrashWithThreads |
| 78 | +// CHECK-NEXT: 2 [ra] 0x{{[0-9a-f]+}} closure #{{[0-9]}} in spawnThread(_:) + {{[0-9]+}} in CrashWithThreads at {{.*}}/CrashWithThreads |
| 79 | +// CHECK-NEXT: 3 [ra] [thunk] 0x{{[0-9a-f]+}} @objc closure #{{[0-9]}} in spawnThread(_:) + {{[0-9]+}} in CrashWithThreads at {{.*}}<compiler-generated> |
| 80 | + |
| 81 | +// CHECK: Registers: |
| 82 | + |
| 83 | +// CHECK: Images ({{[0-9]+}} omitted): |
| 84 | + |
| 85 | +// CHECK: {{0x[0-9a-f]+}}–{{0x[0-9a-f]+}}{{ +}}{{([0-9a-f]+|<no build ID>)}}{{ +}}CrashWithThreads{{ +}}{{.*}}/CrashWithThreads |
0 commit comments