5 // Copyright (c) 2014 Apple Inc. All Rights Reserved.
10 #include <dispatch/dispatch.h>
12 #include <mach/mach.h>
14 /// Type to represent a boolean value.
15 #if TARGET_OS_IPHONE && __LP64__
18 typedef signed char BOOL
;
19 // BOOL is explicitly signed so @encode(BOOL) == "c" rather than "C"
20 // even if -funsigned-char is used.
23 static void __security_simulatecrash_link(CFStringRef reason
, uint32_t code
)
25 #if !TARGET_IPHONE_SIMULATOR
26 // Prototype defined in <CrashReporterSupport/CrashReporterSupport.h>, but objC only.
27 // Soft linking here so we don't link unless we hit this.
28 static BOOL (*__SimulateCrash
)(pid_t pid
, mach_exception_data_type_t exceptionCode
, CFStringRef description
);
30 static dispatch_once_t once
= 0;
31 dispatch_once(&once
, ^{
32 void *image
= dlopen("/System/Library/PrivateFrameworks/CrashReporterSupport.framework/CrashReporterSupport", RTLD_NOW
);
34 __SimulateCrash
= dlsym(image
, "SimulateCrash");
36 __SimulateCrash
= NULL
;
40 __SimulateCrash(getpid(), code
, reason
);
42 secerror("SimulateCrash not available");
44 secerror("SimulateCrash not available in iOS simulator");
48 static int __simulate_crash_counter
= -1;
50 void __security_simulatecrash(CFStringRef reason
, uint32_t code
)
52 secerror("Simulating crash, reason: %@, code=%08x", reason
, code
);
53 if (__simulate_crash_counter
< 0)
54 __security_simulatecrash_link(reason
, code
);
56 __simulate_crash_counter
++;
59 int __security_simulatecrash_enable(bool enable
)
61 int count
= __simulate_crash_counter
;
62 __simulate_crash_counter
= enable
? -1 : 0;