@@ -30,6 +30,10 @@ open class AndroidInjection {
30
30
31
31
static var injectionNumber = 0
32
32
33
+ open class func connectAndRun( ) {
34
+ connectAndRun ( forMainThread: { $0 ( ) } )
35
+ }
36
+
33
37
open class func connectAndRun( forMainThread: @escaping ( @escaping ( ) -> ( ) ) -> ( ) ) {
34
38
if androidInjectionHost == " NNN.NNN.NNN.NNN " {
35
39
NSLog ( " Injection: AndroidInjectionHost.swift has not been updated, please build again. " )
@@ -88,17 +92,17 @@ open class AndroidInjection {
88
92
var value : Int32 = Int32 ( INJECTION_PORT)
89
93
let valueLength = MemoryLayout . size ( ofValue: value)
90
94
if serverWrite == nil || fwrite ( & value, 1 , valueLength, serverWrite) != valueLength {
91
- NSLog ( " Injection: Could not write magic to %d %p : %s " , serverSocket, serverWrite! , strerror ( errno) )
95
+ NSLog ( " Injection: Could not write magic to %d: %s " , serverSocket, strerror ( errno) )
92
96
return
93
97
}
94
98
95
99
if !#file. withCString ( {
96
100
filepath in
97
- value = Int32 ( strlen ( filepath) )
101
+ value = Int32 ( strlen ( filepath) + 1 )
98
102
return fwrite ( & value, 1 , valueLength, serverWrite) == valueLength &&
99
103
fwrite ( filepath, 1 , Int ( value) , serverWrite) == value
100
104
} ) {
101
- NSLog ( " Injection: Could not write filepath to %d %p : %s " , serverSocket, serverWrite! , strerror ( errno) )
105
+ NSLog ( " Injection: Could not write filepath to %d: %s " , serverSocket, strerror ( errno) )
102
106
return
103
107
}
104
108
fflush ( serverWrite)
@@ -109,45 +113,57 @@ open class AndroidInjection {
109
113
while fread ( & compressedLength, 1 , valueLength, serverRead) == valueLength &&
110
114
fread ( & uncompressedLength, 1 , valueLength, serverRead) == valueLength,
111
115
var compressedBuffer = malloc ( Int ( compressedLength) ) ,
112
- var uncompressedBuffer = malloc ( Int ( uncompressedLength) ) ,
113
- fread ( compressedBuffer, 1 , Int ( compressedLength) , serverRead) == compressedLength {
116
+ var uncompressedBuffer = malloc ( Int ( uncompressedLength) ) {
114
117
defer {
115
118
free ( compressedBuffer)
116
119
free ( uncompressedBuffer)
117
120
}
118
- if compressedLength == valueLength && uncompressedLength == valueLength {
121
+
122
+ if compressedLength == 1 && uncompressedLength == 1 {
119
123
continue
120
124
}
121
125
126
+ if fread ( compressedBuffer, 1 , Int ( compressedLength) , serverRead) != compressedLength {
127
+ NSLog ( " Injection: Could not read %d compressed bytes: %s " ,
128
+ compressedLength, strerror ( errno) )
129
+ break
130
+ }
131
+
122
132
NSLog ( " Injection: received %d/%d bytes " , compressedLength, uncompressedLength)
123
133
124
- var destLen = uLongf ( uncompressedLength)
125
- if uncompress ( uncompressedBuffer. assumingMemoryBound ( to: Bytef . self) , & destLen,
126
- compressedBuffer. assumingMemoryBound ( to: Bytef . self) ,
127
- uLong ( compressedLength) ) != Z_OK || destLen != uLongf ( uncompressedLength) {
128
- NSLog ( " Injection: uncompression failure " )
129
- break
134
+ let libraryPath : String
135
+ if uncompressedLength == 1 {
136
+ libraryPath = String ( cString: compressedBuffer. assumingMemoryBound ( to: UInt8 . self) )
130
137
}
138
+ else {
139
+ var destLen = uLongf ( uncompressedLength)
140
+ if uncompress ( uncompressedBuffer. assumingMemoryBound ( to: Bytef . self) , & destLen,
141
+ compressedBuffer. assumingMemoryBound ( to: Bytef . self) ,
142
+ uLong ( compressedLength) ) != Z_OK || destLen != uLongf ( uncompressedLength) {
143
+ NSLog ( " Injection: uncompression failure " )
144
+ break
145
+ }
131
146
132
- AndroidInjection . injectionNumber += 1
133
- let libraryPath = NSTemporaryDirectory ( ) + " injection \( AndroidInjection . injectionNumber) .so "
134
- let libraryFILE = fopen ( libraryPath, " w " )
135
- if libraryFILE == nil ||
136
- fwrite ( uncompressedBuffer, 1 , Int ( uncompressedLength) , libraryFILE) != uncompressedLength {
137
- NSLog ( " Injection: Could not write library file " )
138
- break
147
+ AndroidInjection . injectionNumber += 1
148
+ libraryPath = NSTemporaryDirectory ( ) + " injection \( AndroidInjection . injectionNumber) .so "
149
+ let libraryFILE = fopen ( libraryPath, " w " )
150
+ if libraryFILE == nil ||
151
+ fwrite ( uncompressedBuffer, 1 , Int ( uncompressedLength) , libraryFILE) != uncompressedLength {
152
+ NSLog ( " Injection: Could not write library file " )
153
+ break
154
+ }
155
+ fclose ( libraryFILE)
139
156
}
140
- fclose ( libraryFILE)
141
157
142
158
forMainThread ( {
143
- NSLog ( " Injection: Wrote to \( libraryPath) , injecting ..." )
159
+ NSLog ( " Injection: injecting \( libraryPath) ... " )
144
160
let error = loadAndInject ( library: libraryPath)
145
- var status = Int32 ( error == nil ? 0 : strlen ( error) )
161
+ var status = Int32 ( error == nil ? 0 : strlen ( error) + 1 )
146
162
if fwrite ( & status, 1 , valueLength, serverWrite) != valueLength {
147
- NSLog ( " Injection: Could not write status " )
163
+ NSLog ( " Injection: Could not write status: %s " , strerror ( errno ) )
148
164
}
149
165
if error != nil && fwrite ( error, 1 , Int ( status) , serverWrite) != status {
150
- NSLog ( " Injection: Could not write error string " )
166
+ NSLog ( " Injection: Could not write error string: %s " , strerror ( errno ) )
151
167
}
152
168
fflush ( serverWrite)
153
169
NSLog ( " Injection complete. " )
@@ -256,7 +272,8 @@ open class AndroidInjection {
256
272
}
257
273
258
274
let vtableOffset = byteAddr ( & existingClass. pointee. IVarDestroyer) - byteAddr( existingClass)
259
- let vtableLength = Int ( existingClass. pointee. ClassSize) - pointerSize * 2 - vtableOffset
275
+ let vtableLength = Int ( existingClass. pointee. ClassSize -
276
+ existingClass. pointee. ClassAddressPoint) - vtableOffset
260
277
NSLog ( " \( unsafeBitCast ( classMetadata, to: AnyClass . self) ) , vtable length: \( vtableLength) " )
261
278
memcpy ( byteAddr ( existingClass) + vtableOffset, byteAddr ( classMetadata) + vtableOffset, vtableLength)
262
279
}
0 commit comments