@@ -15,64 +15,64 @@ internal class DebuggerChecker {
1515 var mib : [ Int32 ] = [ CTL_KERN, KERN_PROC, KERN_PROC_PID, getpid ( ) ]
1616 var size = MemoryLayout< kinfo_proc> . stride
1717 let sysctlRet = sysctl ( & mib, UInt32 ( mib. count) , & kinfo, & size, nil , 0 )
18-
18+
1919 if sysctlRet != 0 {
2020 print ( " Error occurred when calling sysctl(). The debugger check may not be reliable " )
2121 }
22-
22+
2323 return ( kinfo. kp_proc. p_flag & P_TRACED) != 0
2424 }
25-
25+
2626 static func denyDebugger( ) {
2727 // bind ptrace()
2828 let pointerToPtrace = UnsafeMutableRawPointer ( bitPattern: - 2 )
2929 let ptracePtr = dlsym ( pointerToPtrace, " ptrace " )
3030 typealias PtraceType = @convention ( c) ( CInt , pid_t , CInt , CInt ) -> CInt
3131 let ptrace = unsafeBitCast ( ptracePtr, to: PtraceType . self)
32-
32+
3333 // PT_DENY_ATTACH == 31
3434 let ptraceRet = ptrace ( 31 , 0 , 0 , 0 )
35-
35+
3636 if ptraceRet != 0 {
3737 print ( " Error occured when calling ptrace(). Denying debugger may not be reliable " )
3838 }
3939 }
40-
40+
4141#if arch(arm64)
4242 static func hasBreakpointAt(
4343 _ functionAddr: UnsafeRawPointer ,
4444 functionSize: vm_size_t ?
4545 ) -> Bool {
4646 let funcAddr = vm_address_t ( UInt ( bitPattern: functionAddr) )
47-
47+
4848 var vmStart : vm_address_t = funcAddr
4949 var vmSize : vm_size_t = 0
5050 let vmRegionInfo = UnsafeMutablePointer< Int32> . allocate(
5151 capacity: MemoryLayout < vm_region_basic_info_64 > . size/ 4
5252 )
53-
53+
5454 defer {
5555 vmRegionInfo. deallocate ( )
5656 }
57-
57+
5858 var vmRegionInfoCount : mach_msg_type_number_t = mach_msg_type_number_t ( VM_REGION_BASIC_INFO_64)
5959 var objectName : mach_port_t = 0
60-
60+
6161 let ret = vm_region_64 (
6262 mach_task_self_, & vmStart,
6363 & vmSize, VM_REGION_BASIC_INFO_64,
6464 vmRegionInfo, & vmRegionInfoCount,
6565 & objectName
6666 )
67-
67+
6868 if ret != KERN_SUCCESS {
6969 return false
7070 }
71-
71+
7272 let vmRegion = vmRegionInfo. withMemoryRebound (
7373 to: vm_region_basic_info_64. self, capacity: 1 , { $0 }
7474 )
75-
75+
7676 if vmRegion. pointee. protection == ( VM_PROT_READ | VM_PROT_EXECUTE) {
7777 let armBreakpointOpcode = 0xe7ffdefe
7878 let arm64BreakpointOpcode = 0xd4200000
@@ -81,7 +81,7 @@ internal class DebuggerChecker {
8181 if let size = functionSize, size < judgeSize {
8282 judgeSize = size
8383 }
84-
84+
8585 for valueToOffset in 0 ..< ( judgeSize / 4 ) {
8686 if ( instructionBegin. advanced (
8787 by: Int ( valueToOffset)
@@ -92,31 +92,31 @@ internal class DebuggerChecker {
9292 }
9393 }
9494 }
95-
95+
9696 return false
9797 }
98-
98+
9999 static func hasWatchpoint( ) -> Bool {
100100 var threads : thread_act_array_t ?
101101 var threadCount : mach_msg_type_number_t = 0
102102 var hasWatchpoint = false
103-
103+
104104 if task_threads ( mach_task_self_, & threads, & threadCount) == KERN_SUCCESS {
105105 var threadStat = arm_debug_state64_t ( )
106106 let capacity = MemoryLayout < arm_debug_state64_t > . size/ MemoryLayout < natural_t > . size
107-
107+
108108 let threadStatPointer = withUnsafeMutablePointer ( to: & threadStat, {
109109 $0. withMemoryRebound ( to: natural_t. self, capacity: capacity, { $0 } )
110110 } )
111-
111+
112112 var count = mach_msg_type_number_t (
113113 MemoryLayout < arm_debug_state64_t > . size/ MemoryLayout < UInt32 > . size
114114 )
115-
115+
116116 guard let threads = threads else {
117117 return false
118118 }
119-
119+
120120 for threadIndex in 0 ..< threadCount where thread_get_state (
121121 threads [ Int ( threadIndex) ] ,
122122 ARM_DEBUG_STATE64,
@@ -128,21 +128,21 @@ internal class DebuggerChecker {
128128 ) . pointee. __wvr. 0 != 0
129129 if hasWatchpoint { break }
130130 }
131-
131+
132132 vm_deallocate (
133133 mach_task_self_,
134134 UInt ( bitPattern: threads) ,
135135 vm_size_t ( threadCount * UInt32( MemoryLayout< thread_act_t> . size) )
136136 )
137137 }
138-
138+
139139 return hasWatchpoint
140140 }
141141#endif
142-
142+
143143 static func isParentPidUnexpected( ) -> Bool {
144144 let parentPid : pid_t = getppid ( )
145-
145+
146146 return parentPid != 1 // LaunchD is pid 1
147147 }
148148}
0 commit comments