3535#ifndef HDR_KSJailbreak_h
3636#define HDR_KSJailbreak_h
3737
38+ #include <TargetConditionals.h>
3839#include <dirent.h>
3940#include <fcntl.h>
4041#include <stdbool.h>
4142#include <stdio.h>
4243#include <sys/types.h>
4344#include <unistd.h>
44- #include <TargetConditionals.h>
4545
4646// The global environ variable must be imported this way.
4747// See: https://opensource.apple.com/source/Libc/Libc-1439.40.11/man/FreeBSD/environ.7
4848extern char * * environ ;
4949
50- static inline bool ksj_local_is_insert_libraries_env_var (const char * str ) {
50+ static inline bool ksj_local_is_insert_libraries_env_var (const char * str )
51+ {
5152 if (str == NULL ) {
5253 return false;
5354 }
@@ -84,24 +85,26 @@ static inline bool ksj_local_is_insert_libraries_env_var(const char* str) {
8485// - Syscall# is in x16, params in x0, x1, x2, and return in x0.
8586// - Carry bit is cleared on success, set on failure (we copy the carry bit to x3).
8687// - We must also inform the compiler that memory and condition codes may get clobbered.
87- #define ksj_syscall3 (call_num , param0 , param1 , param2 , pResult ) do { \
88- register uintptr_t call asm("x16") = (uintptr_t)(call_num); \
89- register uintptr_t p0 asm("x0") = (uintptr_t)(param0); \
90- register uintptr_t p1 asm("x1") = (uintptr_t)(param1); \
91- register uintptr_t p2 asm("x2") = (uintptr_t)(param2); \
92- register uintptr_t carry_bit asm("x3") = 0; \
93- asm volatile("svc #0x80\n" \
94- "mov x3, #0\n" \
95- "adc x3, x3, x3\n" \
96- : "=r"(carry_bit), "=r"(p0),"=r"(p1) \
97- : "r"(p0), "r"(p1), "r"(p2), "r"(call), "r"(carry_bit) \
98- : "memory", "cc"); \
99- if(carry_bit == 1) { \
100- *(pResult) = -1; \
101- } else {\
102- *(pResult) = (int)p0; \
103- } \
104- } while(0)
88+ #define ksj_syscall3 (call_num , param0 , param1 , param2 , pResult ) \
89+ do { \
90+ register uintptr_t call asm("x16") = (uintptr_t)(call_num); \
91+ register uintptr_t p0 asm("x0") = (uintptr_t)(param0); \
92+ register uintptr_t p1 asm("x1") = (uintptr_t)(param1); \
93+ register uintptr_t p2 asm("x2") = (uintptr_t)(param2); \
94+ register uintptr_t carry_bit asm("x3") = 0; \
95+ asm volatile( \
96+ "svc #0x80\n" \
97+ "mov x3, #0\n" \
98+ "adc x3, x3, x3\n" \
99+ : "=r"(carry_bit), "=r"(p0), "=r"(p1) \
100+ : "r"(p0), "r"(p1), "r"(p2), "r"(call), "r"(carry_bit) \
101+ : "memory", "cc"); \
102+ if (carry_bit == 1) { \
103+ *(pResult) = -1; \
104+ } else { \
105+ *(pResult) = (int)p0; \
106+ } \
107+ } while (0)
105108
106109#elif TARGET_CPU_X86_64 && defined(__GCC_ASM_FLAG_OUTPUTS__ ) && !TARGET_OS_OSX
107110#define KSCRASH_HAS_CUSTOM_SYSCALL 1
@@ -115,23 +118,20 @@ static inline bool ksj_local_is_insert_libraries_env_var(const char* str) {
115118// - We must also inform the compiler that memory, rcx, r11 may get clobbered.
116119// The "=@ccc" constraint requires __GCC_ASM_FLAG_OUTPUTS__, not available in Xcode 10
117120// https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html#index-asm-flag-output-operands
118- #define ksj_syscall3 (call_num , param0 , param1 , param2 , pResult ) do { \
119- register uintptr_t rax = (uintptr_t)(call_num) | (2<<24); \
120- register uintptr_t p0 = (uintptr_t)(param0); \
121- register uintptr_t p1 = (uintptr_t)(param1); \
122- register uintptr_t p2 = (uintptr_t)(param2); \
123- register uintptr_t carry_bit = 0; \
124- asm volatile( \
125- "syscall" \
126- : "=@ccc"(carry_bit), "+a"(rax) \
127- : "D" (p0), "S" (p1), "d" (p2) \
128- : "memory", "rcx", "r11"); \
129- if(carry_bit == 1) { \
130- *(pResult) = -1; \
131- } else { \
132- *(pResult) = (int)rax; \
133- } \
134- } while(0)
121+ #define ksj_syscall3 (call_num , param0 , param1 , param2 , pResult ) \
122+ do { \
123+ register uintptr_t rax = (uintptr_t)(call_num) | (2 << 24); \
124+ register uintptr_t p0 = (uintptr_t)(param0); \
125+ register uintptr_t p1 = (uintptr_t)(param1); \
126+ register uintptr_t p2 = (uintptr_t)(param2); \
127+ register uintptr_t carry_bit = 0; \
128+ asm volatile("syscall" : "=@ccc"(carry_bit), "+a"(rax) : "D"(p0), "S"(p1), "d"(p2) : "memory", "rcx", "r11"); \
129+ if (carry_bit == 1) { \
130+ *(pResult) = -1; \
131+ } else { \
132+ *(pResult) = (int)rax; \
133+ } \
134+ } while (0)
135135
136136#else
137137#define KSCRASH_HAS_CUSTOM_SYSCALL 0
@@ -142,83 +142,86 @@ static inline bool ksj_local_is_insert_libraries_env_var(const char* str) {
142142
143143#endif /* TARGET_CPU_XYZ */
144144
145-
146145#if KSCRASH_HAS_CUSTOM_SYSCALL
147146
148147// See: https://opensource.apple.com/source/xnu/xnu-7195.81.3/bsd/kern/syscalls.master
149148#define KSCRASH_SYSCALL_OPEN 5
150- #define ksj_syscall_open (path , flags , mode , pResult ) ksj_syscall3(KSCRASH_SYSCALL_OPEN, (uintptr_t)path, flags, mode, pResult)
149+ #define ksj_syscall_open (path , flags , mode , pResult ) \
150+ ksj_syscall3(KSCRASH_SYSCALL_OPEN, (uintptr_t)path, flags, mode, pResult)
151151
152152#else
153153
154- #define ksj_syscall_open (path , flags , mode , pResult ) do {*(pResult) = open(path, flags, mode);} while(0)
154+ #define ksj_syscall_open (path , flags , mode , pResult ) \
155+ do { \
156+ *(pResult) = open(path, flags, mode); \
157+ } while (0)
155158
156159#endif /* KSCRASH_HAS_CUSTOM_SYSCALL */
157160
158-
159161/**
160162 * Get this device's jailbreak status.
161163 * Stores nonzero in *(pIsJailbroken) if the device is jailbroken, 0 otherwise.
162164 * Note: Implemented as a macro to force it inline always.
163165 */
164166#if !TARGET_OS_SIMULATOR && !TARGET_OS_OSX && KSCRASH_HAS_SYSCALL
165- #define get_jailbreak_status (pIsJailbroken ) do { \
166- int fd = 0; \
167- \
168- bool tmp_file_is_accessible = false; \
169- bool mobile_substrate_exists = false; \
170- bool etc_apt_exists = false; \
171- bool has_insert_libraries = false; \
172- \
173- const char* test_write_file = "/tmp/bugsnag-check.txt"; \
174- remove(test_write_file); \
175- ksj_syscall_open(test_write_file, O_CREAT, 0644, &fd); \
176- if(fd > 0) { \
177- close(fd); \
178- tmp_file_is_accessible = true; \
179- } else { \
180- ksj_syscall_open(test_write_file, O_RDONLY, 0, &fd); \
181- if(fd > 0) { \
182- close(fd); \
183- tmp_file_is_accessible = true; \
184- } \
185- } \
186- remove(test_write_file); \
187- \
188- const char* mobile_substrate_path = "/Library/MobileSubstrate/MobileSubstrate.dylib"; \
189- ksj_syscall_open(mobile_substrate_path, O_RDONLY, 0, &fd); \
190- if(fd > 0) { \
191- close(fd); \
192- mobile_substrate_exists = true; \
193- } \
194- \
195- const char* etc_apt_path = "/etc/apt"; \
196- DIR *dirp = opendir(etc_apt_path); \
197- if(dirp) { \
198- etc_apt_exists = true; \
199- closedir(dirp); \
200- } \
201- \
202- for(int i = 0; environ[i] != NULL; i++) { \
203- if(ksj_local_is_insert_libraries_env_var(environ[i])) { \
204- has_insert_libraries = true; \
205- break; \
206- } \
207- } \
208- \
209- *(pIsJailbroken) = tmp_file_is_accessible || \
210- mobile_substrate_exists || \
211- etc_apt_exists || \
212- has_insert_libraries; \
213- } while(0)
167+ #define get_jailbreak_status (pIsJailbroken ) \
168+ do { \
169+ int fd = 0; \
170+ \
171+ bool tmp_file_is_accessible = false; \
172+ bool mobile_substrate_exists = false; \
173+ bool etc_apt_exists = false; \
174+ bool has_insert_libraries = false; \
175+ \
176+ const char *test_write_file = "/tmp/bugsnag-check.txt"; \
177+ remove(test_write_file); \
178+ ksj_syscall_open(test_write_file, O_CREAT, 0644, &fd); \
179+ if (fd > 0) { \
180+ close(fd); \
181+ tmp_file_is_accessible = true; \
182+ } else { \
183+ ksj_syscall_open(test_write_file, O_RDONLY, 0, &fd); \
184+ if (fd > 0) { \
185+ close(fd); \
186+ tmp_file_is_accessible = true; \
187+ } \
188+ } \
189+ remove(test_write_file); \
190+ \
191+ const char *mobile_substrate_path = "/Library/MobileSubstrate/MobileSubstrate.dylib"; \
192+ ksj_syscall_open(mobile_substrate_path, O_RDONLY, 0, &fd); \
193+ if (fd > 0) { \
194+ close(fd); \
195+ mobile_substrate_exists = true; \
196+ } \
197+ \
198+ const char *etc_apt_path = "/etc/apt"; \
199+ DIR *dirp = opendir(etc_apt_path); \
200+ if (dirp) { \
201+ etc_apt_exists = true; \
202+ closedir(dirp); \
203+ } \
204+ \
205+ for (int i = 0; environ[i] != NULL; i++) { \
206+ if (ksj_local_is_insert_libraries_env_var(environ[i])) { \
207+ has_insert_libraries = true; \
208+ break; \
209+ } \
210+ } \
211+ \
212+ *(pIsJailbroken) = \
213+ tmp_file_is_accessible || mobile_substrate_exists || etc_apt_exists || has_insert_libraries; \
214+ } while (0)
214215
215216#else
216217
217218// "/tmp" is accessible on the simulator, which makes the JB test come back positive, so
218219// report false on the simulator.
219- #define get_jailbreak_status (pIsJailbroken ) do { *(pIsJailbroken) = 0; } while(0)
220+ #define get_jailbreak_status (pIsJailbroken ) \
221+ do { \
222+ *(pIsJailbroken) = 0; \
223+ } while (0)
220224
221225#endif /* !TARGET_OS_SIMULATOR */
222226
223-
224227#endif /* HDR_KSJailbreak_h */
0 commit comments