@@ -2,72 +2,71 @@ import Foundation
2
2
import LinuxSystemHeaders
3
3
4
4
#if arch(arm64)
5
- public typealias RegisterSet = user_pt_regs
5
+ public typealias RegisterSet = user_pt_regs
6
6
7
- extension RegisterSet {
8
- public static var trapInstructionSize : UInt { return 4 } // brk #0x0
7
+ extension RegisterSet {
8
+ public static var trapInstructionSize : UInt { return 4 } // brk #0x0
9
9
10
- public func setupCall(
11
- _ ptrace: PTrace , to funcAddr: UInt64 , with args: [ UInt64 ] , returnTo returnAddr: UInt64
12
- ) throws -> RegisterSet {
13
- precondition ( args. count <= 6 )
14
- var registers = self
15
- registers. regs. 0 = args. count > 0 ? args [ 0 ] : 0
16
- registers. regs. 1 = args. count > 1 ? args [ 1 ] : 0
17
- registers. regs. 2 = args. count > 2 ? args [ 2 ] : 0
18
- registers. regs. 3 = args. count > 3 ? args [ 3 ] : 0
19
- registers. regs. 4 = args. count > 4 ? args [ 4 ] : 0
20
- registers. regs. 5 = args. count > 5 ? args [ 5 ] : 0
21
- registers. pc = funcAddr
22
- registers. regs. 30 = returnAddr // link register (x30)
23
- return registers
24
- }
10
+ public func setupCall(
11
+ _ ptrace: PTrace , to funcAddr: UInt64 , with args: [ UInt64 ] , returnTo returnAddr: UInt64
12
+ ) throws -> RegisterSet {
13
+ precondition ( args. count <= 6 )
14
+ var registers = self
15
+ registers. regs. 0 = args. count > 0 ? args [ 0 ] : 0
16
+ registers. regs. 1 = args. count > 1 ? args [ 1 ] : 0
17
+ registers. regs. 2 = args. count > 2 ? args [ 2 ] : 0
18
+ registers. regs. 3 = args. count > 3 ? args [ 3 ] : 0
19
+ registers. regs. 4 = args. count > 4 ? args [ 4 ] : 0
20
+ registers. regs. 5 = args. count > 5 ? args [ 5 ] : 0
21
+ registers. pc = funcAddr
22
+ registers. regs. 30 = returnAddr // link register (x30)
23
+ return registers
24
+ }
25
25
26
- public func returnValue( ) -> UInt64 {
27
- return self . regs. 0
28
- }
26
+ public func returnValue( ) -> UInt64 {
27
+ return self . regs. 0
28
+ }
29
29
30
- public mutating func step( _ bytes: UInt ) {
31
- self . pc += UInt64 ( bytes)
32
- }
30
+ public mutating func step( _ bytes: UInt ) {
31
+ self . pc += UInt64 ( bytes)
33
32
}
33
+ }
34
34
35
35
#elseif arch(x86_64)
36
- public typealias RegisterSet = pt_regs
37
-
38
- extension RegisterSet {
39
- public static var trapInstructionSize : UInt { return 1 } // int3
36
+ public typealias RegisterSet = pt_regs
40
37
41
- public func setupCall(
42
- _ ptrace: PTrace , to funcAddr: UInt64 , with args: [ UInt64 ] , returnTo returnAddr: UInt64
43
- ) throws -> RegisterSet {
44
- precondition ( args. count <= 6 )
45
- var registers = self
46
- registers. rdi = UInt ( args. count > 0 ? args [ 0 ] : 0 )
47
- registers. rsi = UInt ( args. count > 1 ? args [ 1 ] : 0 )
48
- registers. rdx = UInt ( args. count > 2 ? args [ 2 ] : 0 )
49
- registers. rcx = UInt ( args. count > 3 ? args [ 3 ] : 0 )
50
- registers. r8 = UInt ( args. count > 4 ? args [ 4 ] : 0 )
51
- registers. r9 = UInt ( args. count > 5 ? args [ 5 ] : 0 )
52
- registers. rip = UInt ( funcAddr)
53
- registers. rax = 0 // rax is the number of args in a va_args function
38
+ extension RegisterSet {
39
+ public static var trapInstructionSize : UInt { return 1 } // int3
54
40
55
- // push the return address onto the stack
56
- registers. rsp -= UInt ( MemoryLayout< UInt64> . size)
57
- try ptrace. pokeData ( addr: UInt64 ( registers. rsp) , value: returnAddr)
41
+ public func setupCall(
42
+ _ ptrace: PTrace , to funcAddr: UInt64 , with args: [ UInt64 ] , returnTo returnAddr: UInt64
43
+ ) throws -> RegisterSet {
44
+ precondition ( args. count <= 6 )
45
+ var registers = self
46
+ registers. rdi = UInt ( args. count > 0 ? args [ 0 ] : 0 )
47
+ registers. rsi = UInt ( args. count > 1 ? args [ 1 ] : 0 )
48
+ registers. rdx = UInt ( args. count > 2 ? args [ 2 ] : 0 )
49
+ registers. rcx = UInt ( args. count > 3 ? args [ 3 ] : 0 )
50
+ registers. r8 = UInt ( args. count > 4 ? args [ 4 ] : 0 )
51
+ registers. r9 = UInt ( args. count > 5 ? args [ 5 ] : 0 )
52
+ registers. rip = UInt ( funcAddr)
53
+ registers. rax = 0 // rax is the number of args in a va_args function
58
54
59
- return registers
60
- }
55
+ // push the return address onto the stack
56
+ registers. rsp -= UInt ( MemoryLayout< UInt64> . size)
57
+ try ptrace. pokeData ( addr: UInt64 ( registers. rsp) , value: returnAddr)
61
58
62
- public func returnValue( ) -> UInt64 {
63
- return UInt64 ( self . rax)
64
- }
59
+ return registers
60
+ }
65
61
66
- public mutating func step( _ bytes: UInt ) {
67
- self . rip += UInt ( bytes)
68
- }
62
+ public func returnValue( ) -> UInt64 {
63
+ return UInt64 ( self . rax)
69
64
}
70
65
66
+ public mutating func step( _ bytes: UInt ) {
67
+ self . rip += UInt ( bytes)
68
+ }
69
+ }
71
70
#else
72
- #error("Only arm64 and x86_64 architectures are supported")
71
+ #error("Only arm64 and x86_64 architectures are supported")
73
72
#endif
0 commit comments