Skip to content

Instantly share code, notes, and snippets.

@AliSoftware
Last active February 9, 2016 13:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AliSoftware/1ddfa7bc5fa668e44193 to your computer and use it in GitHub Desktop.
Save AliSoftware/1ddfa7bc5fa668e44193 to your computer and use it in GitHub Desktop.
Crash with EXC_BAD_ACCESS on release
enum Crash: ErrorType { case Key(String) }
func kaboom(str: String) throws -> String {
throw Crash.Key(str)
}
// Crash with EXC_BAD_ACCESS, not every time but quite often
func test1() throws -> [String:[String]] {
return [
"foo": try [kaboom("test1a"), kaboom("test1b")]
]
}
// Crash with EXC_BAD_ACCESS, not every time but very often
func test2() throws -> [String:Any] {
return [
"foo": try ["test2a","test2b"].map { try kaboom($0) }
]
}
// Crash with EXC_BAD_ACCESS sometimes, but way less often
func test3() throws -> [String:Any] {
let t: [String:Any] = [
"foo": try ["test3a","test3b"].map { try kaboom($0) }
]
return t
}
// Crash with EXC_BAD_ACCESS, not every time but quite often
func test4() throws -> [String:String] {
return [
"foo": try kaboom("test4")
]
}
// Crash with EXC_BAD_ACCESS, not every time but quite often
func test5() -> [String:Any] {
do {
return [
"foo": try [kaboom("test5a"), kaboom("test5b")]
]
} catch {
print(error)
return [:]
}
}
// Seems ok
func test6() throws -> [Any] {
return [try kaboom("test6a"), try kaboom("test6b")]
}
// Obviously only test one case at a time to isolate the issue
do {
// randomly EXC_BAD_ACCESS
try test1()
} catch { print(error) }
do {
// randomly EXC_BAD_ACCESS
try test2()
} catch { print(error) }
do {
// randomly EXC_BAD_ACCESS
try test3()
} catch { print(error) }
do {
// randomly EXC_BAD_ACCESS
try test4()
} catch { print(error) }
// randomly EXC_BAD_ACCESS
test5()
do {
// Seems ok
try test6()
} catch { print(error) }
// - When trying throwing functions test1(), test2(), test3(), test4() and catching function test5(), I got EXC_BAD_ACCESS quite often
// - When trying test6() — which returns an Array and not a Dict — I didn't manage to generate a crash
// When crashing in REPL, Stacktrace looks like this:
/*
Execution interrupted. Enter Swift code to recover and continue.
Enter LLDB commands to investigate (type :help for assistance.)
29> :bt
* thread #1: tid = 0x8d0ec, 0x000000010024268a libswiftCore.dylib`_swift_release_(swift::HeapObject*) + 10, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=EXC_I386_GPFLT)
* frame #0: 0x000000010024268a libswiftCore.dylib`_swift_release_(swift::HeapObject*) + 10
frame #1: 0x000000010054ca8e $__lldb_expr2`__lldb_expr_1.crash2 ($error=ErrorType @ 0x00007fff5fbffd40) throws -> Swift.Dictionary<Swift.String, protocol<>> + 1070 at repl.swift:16
frame #2: 0x000000010054da76 $__lldb_expr4`main + 134 at repl.swift:28
frame #3: 0x0000000100000df0 repl_swift`_mh_execute_header + 3568
frame #4: 0x00007fff8d0ad5ad libdyld.dylib`start + 1
frame #5: 0x00007fff8d0ad5ad libdyld.dylib`start + 1
*/
// When interpreted from file, both with `swift main.swift`, `swift -Onone main.swift` and `swift -O main.swift`:
// (does not fail every time, might need to compile multiple times until crash)
/*
$ swift main.swift
0 swift 0x0000000110f28fbb llvm::sys::PrintStackTrace(__sFILE*) + 43
1 swift 0x0000000110f296fb SignalHandler(int) + 379
2 libsystem_platform.dylib 0x00007fff936f4eaa _sigtramp + 26
3 libswiftCore.dylib 0x0000000113266008 GCC_except_table23 + 46632
4 libswiftCore.dylib 0x0000000112f7a11a GCC_except_table23 + 4291950394
5 swift 0x000000010f4979bf llvm::MCJIT::runFunction(llvm::Function*, std::__1::vector<llvm::GenericValue, std::__1::allocator<llvm::GenericValue> > const&) + 271
6 swift 0x000000010f49a0f6 llvm::ExecutionEngine::runFunctionAsMain(llvm::Function*, std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > const&, char const* const*) + 1190
7 swift 0x000000010f32bd8c swift::RunImmediately(swift::CompilerInstance&, std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > const&, swift::IRGenOptions&, swift::SILOptions const&) + 2188
8 swift 0x000000010f01a151 performCompile(swift::CompilerInstance&, swift::CompilerInvocation&, llvm::ArrayRef<char const*>, int&) + 13425
9 swift 0x000000010f016ad3 frontend_main(llvm::ArrayRef<char const*>, char const*, void*) + 2691
10 swift 0x000000010f013154 main + 2324
11 libdyld.dylib 0x00007fff8d0ad5ad start + 1
Stack dump:
0. Program arguments: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift -frontend -interpret main.swift -target x86_64-apple-darwin15.3.0 -enable-objc-interop -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk -color-diagnostics -module-name main
[1] 40380 segmentation fault swift main.swift
*/
// When compiled using swiftc (-O & -Onone), segfaults at runtime
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment