forked from LSPosed/DexBuilder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dex_testcase_generator.cc
523 lines (475 loc) · 20.9 KB
/
dex_testcase_generator.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
/* DexBuilder
* Copyright (C) 2021 LSPosed
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* Copyright (C) 2018 The Android Open Source Project
* Modifications copyright (C) 2021 LSPosed Developers
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "dex_builder.h"
#include "slicer/dex_format.h"
#include "slicer/reader.h"
#include <cstdint>
#include <fstream>
#include <iterator>
#include <string>
#include <iostream>
#include <vector>
// Adding tests here requires changes in several other places. See README.md in
// the view_compiler directory for more information.
using namespace startop::dex;
using namespace std;
void GenerateTrivialDexFile(const string &outdir) {
DexBuilder dex_file;
ClassBuilder cbuilder{
dex_file.MakeClass("android.startop.test.testcases.Trivial")};
cbuilder.set_source_file("dex_testcase_generator.cc#GenerateTrivialDexFile");
slicer::MemView image{dex_file.CreateImage()};
std::ofstream out_file(outdir + "/trivial.dex");
out_file.write(image.ptr<const char>(), image.size());
}
// Generates test cases that test around 1 instruction.
void GenerateSimpleTestCases(const string &outdir) {
DexBuilder dex_file;
ClassBuilder cbuilder{
dex_file.MakeClass("android.startop.test.testcases.SimpleTests")};
cbuilder.set_source_file("dex_testcase_generator.cc#GenerateSimpleTestCases");
auto static_field =
cbuilder.CreateField("staticInteger", TypeDescriptor::Int);
static_field.Encode();
auto instance_field =
cbuilder.CreateField("instanceField", TypeDescriptor::Int);
instance_field.access_flags(dex::kAccPublic);
instance_field.Encode();
// int return5() { return 5; }
auto return5{
cbuilder.CreateMethod("return5", Prototype{TypeDescriptor::Int})};
{
LiveRegister r{return5.AllocRegister()};
return5.BuildConst(r, 5);
return5.BuildReturn(r);
}
return5.Encode();
// int return5() { return 5; }
auto returnInteger5{cbuilder.CreateMethod(
"returnInteger5", Prototype{TypeDescriptor::ObjectInt})};
[&](MethodBuilder &method) {
LiveRegister five{method.AllocRegister()};
method.BuildConst(five, 5);
LiveRegister object{method.AllocRegister()};
method.BuildNew(object, TypeDescriptor::ObjectInt,
Prototype{TypeDescriptor::Void, TypeDescriptor::Int}, five);
method.BuildUnBoxIfPrimitive(five, TypeDescriptor::ObjectInt, object);
method.BuildReturn(object, /*is_object=*/true);
}(returnInteger5);
returnInteger5.Encode();
// // int returnParam(int x) { return x; }
auto returnParam{cbuilder.CreateMethod(
"returnParam", Prototype{TypeDescriptor::Int, TypeDescriptor::Int})};
returnParam.BuildReturn(Value::Parameter(0));
returnParam.Encode();
// int returnStringLength(String x) { return x.length(); }
MethodDeclData string_length{dex_file.GetOrDeclareMethod(
TypeDescriptor::String, "length", Prototype{TypeDescriptor::Int})};
auto returnStringLength{cbuilder.CreateMethod(
"returnStringLength",
Prototype{TypeDescriptor::Int, TypeDescriptor::String})};
{
LiveRegister result = returnStringLength.AllocRegister();
returnStringLength.AddInstruction(Instruction::InvokeVirtual(
string_length.id, result, Value::Parameter(0)));
returnStringLength.BuildReturn(result);
}
returnStringLength.Encode();
// int returnIfZero(int x) { if (x == 0) { return 5; } else { return 3; } }
MethodBuilder returnIfZero{cbuilder.CreateMethod(
"returnIfZero", Prototype{TypeDescriptor::Int, TypeDescriptor::Int})};
{
LiveRegister resultIfZero{returnIfZero.AllocRegister()};
Value else_target{returnIfZero.MakeLabel()};
returnIfZero.AddInstruction(
Instruction::OpWithArgs(Instruction::Op::kBranchEqz, /*dest=*/{},
Value::Parameter(0), else_target));
// else branch
returnIfZero.BuildConst(resultIfZero, 3);
returnIfZero.AddInstruction(Instruction::OpWithArgs(
Instruction::Op::kReturn, /*dest=*/{}, resultIfZero));
// then branch
returnIfZero.AddInstruction(Instruction::OpWithArgs(
Instruction::Op::kBindLabel, /*dest=*/{}, else_target));
returnIfZero.BuildConst(resultIfZero, 5);
returnIfZero.AddInstruction(Instruction::OpWithArgs(
Instruction::Op::kReturn, /*dest=*/{}, resultIfZero));
}
returnIfZero.Encode();
// int returnIfNotZero(int x) { if (x != 0) { return 5; } else { return 3; } }
MethodBuilder returnIfNotZero{cbuilder.CreateMethod(
"returnIfNotZero", Prototype{TypeDescriptor::Int, TypeDescriptor::Int})};
{
LiveRegister resultIfNotZero{returnIfNotZero.AllocRegister()};
Value else_target{returnIfNotZero.MakeLabel()};
returnIfNotZero.AddInstruction(
Instruction::OpWithArgs(Instruction::Op::kBranchNEqz, /*dest=*/{},
Value::Parameter(0), else_target));
// else branch
returnIfNotZero.BuildConst(resultIfNotZero, 3);
returnIfNotZero.AddInstruction(Instruction::OpWithArgs(
Instruction::Op::kReturn, /*dest=*/{}, resultIfNotZero));
// then branch
returnIfNotZero.AddInstruction(Instruction::OpWithArgs(
Instruction::Op::kBindLabel, /*dest=*/{}, else_target));
returnIfNotZero.BuildConst(resultIfNotZero, 5);
returnIfNotZero.AddInstruction(Instruction::OpWithArgs(
Instruction::Op::kReturn, /*dest=*/{}, resultIfNotZero));
}
returnIfNotZero.Encode();
// Make sure backwards branches work too.
//
// Pseudo code for test:
// {
// zero = 0;
// result = 1;
// if (zero == 0) goto B;
// A:
// return result;
// B:
// result = 2;
// if (zero == 0) goto A;
// result = 3;
// return result;
// }
// If it runs correctly, this test should return 2.
MethodBuilder backwardsBranch{
cbuilder.CreateMethod("backwardsBranch", Prototype{TypeDescriptor::Int})};
[](MethodBuilder &method) {
LiveRegister zero = method.AllocRegister();
LiveRegister result = method.AllocRegister();
Value labelA = method.MakeLabel();
Value labelB = method.MakeLabel();
method.BuildConst(zero, 0);
method.BuildConst(result, 1);
method.AddInstruction(Instruction::OpWithArgs(Instruction::Op::kBranchEqz,
/*dest=*/{}, zero, labelB));
method.AddInstruction(Instruction::OpWithArgs(Instruction::Op::kBindLabel,
/*dest=*/{}, labelA));
method.BuildReturn(result);
method.AddInstruction(Instruction::OpWithArgs(Instruction::Op::kBindLabel,
/*dest=*/{}, labelB));
method.BuildConst(result, 2);
method.AddInstruction(Instruction::OpWithArgs(Instruction::Op::kBranchEqz,
/*dest=*/{}, zero, labelA));
method.BuildConst(result, 3);
method.BuildReturn(result);
}(backwardsBranch);
backwardsBranch.Encode();
// Test that we can make a null value. Basically:
//
// public static String returnNull() { return null; }
MethodBuilder returnNull{
cbuilder.CreateMethod("returnNull", Prototype{TypeDescriptor::String})};
[](MethodBuilder &method) {
LiveRegister zero = method.AllocRegister();
method.BuildConst(zero, 0);
method.BuildReturn(zero, /*is_object=*/true);
}(returnNull);
returnNull.Encode();
// Test that we can make String literals. Basically:
//
// public static String makeString() { return "Hello, World!"; }
MethodBuilder makeString{
cbuilder.CreateMethod("makeString", Prototype{TypeDescriptor::String})};
[](MethodBuilder &method) {
LiveRegister string = method.AllocRegister();
method.BuildConstString(string, "Hello, World!");
method.BuildReturn(string, /*is_object=*/true);
}(makeString);
makeString.Encode();
// Make sure strings are sorted correctly.
//
// int returnStringIfZeroAB(int x) { if (x == 0) { return "a"; } else { return
// "b"; } }
MethodBuilder returnStringIfZeroAB{cbuilder.CreateMethod(
"returnStringIfZeroAB",
Prototype{TypeDescriptor::String, TypeDescriptor::Int})};
[&](MethodBuilder &method) {
LiveRegister resultIfZero{method.AllocRegister()};
Value else_target{method.MakeLabel()};
method.AddInstruction(
Instruction::OpWithArgs(Instruction::Op::kBranchEqz, /*dest=*/{},
Value::Parameter(0), else_target));
// else branch
method.BuildConstString(resultIfZero, "b");
method.AddInstruction(Instruction::OpWithArgs(
Instruction::Op::kReturnObject, /*dest=*/{}, resultIfZero));
// then branch
method.AddInstruction(Instruction::OpWithArgs(Instruction::Op::kBindLabel,
/*dest=*/{}, else_target));
method.BuildConstString(resultIfZero, "a");
method.AddInstruction(Instruction::OpWithArgs(
Instruction::Op::kReturnObject, /*dest=*/{}, resultIfZero));
method.Encode();
}(returnStringIfZeroAB);
// int returnStringIfZeroAB(int x) { if (x == 0) { return "b"; } else { return
// "a"; } }
MethodBuilder returnStringIfZeroBA{cbuilder.CreateMethod(
"returnStringIfZeroBA",
Prototype{TypeDescriptor::String, TypeDescriptor::Int})};
[&](MethodBuilder &method) {
LiveRegister resultIfZero{method.AllocRegister()};
Value else_target{method.MakeLabel()};
method.AddInstruction(
Instruction::OpWithArgs(Instruction::Op::kBranchEqz, /*dest=*/{},
Value::Parameter(0), else_target));
// else branch
method.BuildConstString(resultIfZero, "a");
method.AddInstruction(Instruction::OpWithArgs(
Instruction::Op::kReturnObject, /*dest=*/{}, resultIfZero));
// then branch
method.AddInstruction(Instruction::OpWithArgs(Instruction::Op::kBindLabel,
/*dest=*/{}, else_target));
method.BuildConstString(resultIfZero, "b");
method.AddInstruction(Instruction::OpWithArgs(
Instruction::Op::kReturnObject, /*dest=*/{}, resultIfZero));
method.Encode();
}(returnStringIfZeroBA);
// Make sure we can invoke static methods that return an object
// String invokeStaticReturnObject(int n, int radix) { return
// java.lang.Integer.toString(n, radix); }
MethodBuilder invokeStaticReturnObject{cbuilder.CreateMethod(
"invokeStaticReturnObject",
Prototype{TypeDescriptor::String, TypeDescriptor::Int,
TypeDescriptor::Int})};
[&](MethodBuilder &method) {
LiveRegister result{method.AllocRegister()};
MethodDeclData to_string{dex_file.GetOrDeclareMethod(
TypeDescriptor::FromClassname("java.lang.Integer"), "toString",
Prototype{TypeDescriptor::String, TypeDescriptor::Int,
TypeDescriptor::Int})};
method.AddInstruction(Instruction::InvokeStaticObject(
to_string.id, result, Value::Parameter(0), Value::Parameter(1)));
method.BuildReturn(result, /*is_object=*/true);
method.Encode();
}(invokeStaticReturnObject);
// Make sure we can invoke virtual methods that return an object
// String invokeVirtualReturnObject(String s, int n) { return s.substring(n);
// }
MethodBuilder invokeVirtualReturnObject{cbuilder.CreateMethod(
"invokeVirtualReturnObject",
Prototype{TypeDescriptor::String, TypeDescriptor::String,
TypeDescriptor::Int})};
[&](MethodBuilder &method) {
LiveRegister result{method.AllocRegister()};
MethodDeclData substring{dex_file.GetOrDeclareMethod(
TypeDescriptor::String, "substring",
Prototype{TypeDescriptor::String, TypeDescriptor::Int})};
method.AddInstruction(Instruction::InvokeVirtualObject(
substring.id, result, Value::Parameter(0), Value::Parameter(1)));
method.BuildReturn(result, /*is_object=*/true);
method.Encode();
}(invokeVirtualReturnObject);
// Make sure we can cast objects
// String castObjectToString(Object o) { return (String)o; }
MethodBuilder castObjectToString{cbuilder.CreateMethod(
"castObjectToString",
Prototype{TypeDescriptor::String, TypeDescriptor::Object})};
[&](MethodBuilder &method) {
const ir::Type *type_def =
dex_file.GetOrAddType(TypeDescriptor::String.descriptor());
method.AddInstruction(Instruction::Cast(Value::Parameter(0),
Value::Type(type_def->orig_index)));
method.BuildReturn(Value::Parameter(0), /*is_object=*/true);
method.Encode();
}(castObjectToString);
// Read a static field
// int readStaticField() { return TestClass.staticInteger; }
MethodBuilder readStaticField{
cbuilder.CreateMethod("readStaticField", Prototype{TypeDescriptor::Int})};
[&](MethodBuilder &method) {
const ir::FieldDecl *field = dex_file.GetOrAddField(
cbuilder.descriptor(), "staticInteger", TypeDescriptor::Int);
LiveRegister result{method.AllocRegister()};
method.AddInstruction(
Instruction::GetStaticField(field->orig_index, result));
method.BuildReturn(result, /*is_object=*/false);
method.Encode();
}(readStaticField);
// Set a static field
// void setStaticField() { TestClass.staticInteger = 7; }
MethodBuilder setStaticField{
cbuilder.CreateMethod("setStaticField", Prototype{TypeDescriptor::Void})};
[&](MethodBuilder &method) {
const ir::FieldDecl *field = dex_file.GetOrAddField(
cbuilder.descriptor(), "staticInteger", TypeDescriptor::Int);
LiveRegister number{method.AllocRegister()};
method.BuildConst(number, 7);
method.AddInstruction(
Instruction::SetStaticField(field->orig_index, number));
method.BuildReturn();
method.Encode();
}(setStaticField);
// Read an instance field
// int readInstanceField(TestClass obj) { return obj.instanceField; }
MethodBuilder readInstanceField{cbuilder.CreateMethod(
"readInstanceField",
Prototype{TypeDescriptor::Int, cbuilder.descriptor()})};
[&](MethodBuilder &method) {
const ir::FieldDecl *field = dex_file.GetOrAddField(
cbuilder.descriptor(), "instanceField", TypeDescriptor::Int);
LiveRegister result{method.AllocRegister()};
method.AddInstruction(
Instruction::GetField(field->orig_index, result, Value::Parameter(0)));
method.BuildReturn(result, /*is_object=*/false);
method.Encode();
}(readInstanceField);
// Set an instance field
// void setInstanceField(TestClass obj) { obj.instanceField = 7; }
MethodBuilder setInstanceField{cbuilder.CreateMethod(
"setInstanceField",
Prototype{TypeDescriptor::Void, cbuilder.descriptor()})};
[&](MethodBuilder &method) {
const ir::FieldDecl *field = dex_file.GetOrAddField(
cbuilder.descriptor(), "instanceField", TypeDescriptor::Int);
LiveRegister number{method.AllocRegister()};
method.BuildConst(number, 7);
method.AddInstruction(
Instruction::SetField(field->orig_index, Value::Parameter(0), number));
method.BuildReturn();
method.Encode();
}(setInstanceField);
MethodBuilder newArray{
cbuilder.CreateMethod("newArray", Prototype{TypeDescriptor::Void})};
[&](MethodBuilder &method) {
LiveRegister index{method.AllocRegister()};
method.BuildConst(index, 5);
LiveRegister array{method.AllocRegister()};
method.BuildNewArray(array, TypeDescriptor::Object, index);
LiveRegister object{method.AllocRegister()};
for (int i = 0; i < 5; ++i) {
method.BuildConst(index, i);
method.BuildBoxIfPrimitive(object, TypeDescriptor::Int, index);
method.BuildAput(Instruction::Op::kAputObject, array, object, index);
}
method.BuildReturn();
method.Encode();
}(newArray);
slicer::MemView image{dex_file.CreateImage()};
std::ofstream out_file(outdir + "/simple.dex");
out_file.write(image.ptr<const char>(), image.size());
}
void GenerateHooker(const string &outdir) {
DexBuilder dex_file;
auto return_type = TypeDescriptor::Double;
const auto parameter_types =
std::vector{TypeDescriptor::Int,
TypeDescriptor::Long,
TypeDescriptor::FromClassname("io.github.lsposed.Test")};
ClassBuilder cbuilder{dex_file.MakeClass("LSPHooker")};
cbuilder.set_source_file("dex_testcase_generator.cc#GenerateHooker");
auto hooker_type =
TypeDescriptor::FromClassname("org.lsposed.lspd.hooker.LspHooker");
auto *hooker_field = cbuilder.CreateField("hooker", hooker_type)
.access_flags(dex::kAccStatic)
.Encode();
auto setupBuilder{cbuilder.CreateMethod(
"setup", Prototype{TypeDescriptor::Void, hooker_type})};
setupBuilder
.AddInstruction(Instruction::SetStaticObjectField(
hooker_field->decl->orig_index, Value::Parameter(0)))
.BuildReturn()
.Encode();
auto hookBuilder{cbuilder.CreateMethod(
"hook", Prototype{return_type, parameter_types})};
// allocate tmp frist because of wide
auto tmp{hookBuilder.AllocRegister()};
hookBuilder.BuildConst(tmp, parameter_types.size());
auto hook_params_array{hookBuilder.AllocRegister()};
hookBuilder.BuildNewArray(hook_params_array, TypeDescriptor::Object, tmp);
for (size_t i = 0u, j = 0u; i < parameter_types.size(); ++i, ++j) {
hookBuilder.BuildBoxIfPrimitive(Value::Parameter(j), parameter_types[i],
Value::Parameter(j));
hookBuilder.BuildConst(tmp, i);
hookBuilder.BuildAput(Instruction::Op::kAputObject, hook_params_array,
Value::Parameter(j), tmp);
if (parameter_types[i].is_wide()) ++j;
}
auto handle_hook_method{dex_file.GetOrDeclareMethod(
hooker_type, "handleHookedMethod",
Prototype{TypeDescriptor::Object, TypeDescriptor::Object.ToArray()})};
hookBuilder.AddInstruction(
Instruction::GetStaticObjectField(hooker_field->decl->orig_index, tmp));
hookBuilder.AddInstruction(Instruction::InvokeVirtualObject(
handle_hook_method.id, tmp, tmp, hook_params_array));
if (return_type == TypeDescriptor::Void) {
hookBuilder.BuildReturn();
} else if (return_type.is_primitive()) {
auto box_type{return_type.ToBoxType()};
const ir::Type *type_def = dex_file.GetOrAddType(box_type);
hookBuilder.AddInstruction(
Instruction::Cast(tmp, Value::Type(type_def->orig_index)));
hookBuilder.BuildUnBoxIfPrimitive(tmp, box_type, tmp);
hookBuilder.BuildReturn(tmp, false, return_type.is_wide());
} else {
const ir::Type *type_def = dex_file.GetOrAddType(return_type);
hookBuilder.AddInstruction(
Instruction::Cast(tmp, Value::Type(type_def->orig_index)));
hookBuilder.BuildReturn(tmp, true);
}
auto *hook_method = hookBuilder.Encode();
auto backup_builder{
cbuilder.CreateMethod("backup", Prototype{return_type, parameter_types})};
if (return_type == TypeDescriptor::Void) {
backup_builder.BuildReturn();
} else if(return_type.is_wide()){
LiveRegister zero = backup_builder.AllocRegister();
LiveRegister zero_wide = backup_builder.AllocRegister();
backup_builder.BuildConstWide(zero, 0);
backup_builder.BuildReturn(zero, /*is_object=*/true, true);
} else {
LiveRegister zero = backup_builder.AllocRegister();
LiveRegister zero_wide = backup_builder.AllocRegister();
backup_builder.BuildConst(zero, 0);
backup_builder.BuildReturn(zero, /*is_object=*/true, false);
}
auto *back_method = backup_builder.Encode();
slicer::MemView image{dex_file.CreateImage()};
std::ofstream out_file(outdir + "/hooker.dex");
out_file.write(image.ptr<const char>(), image.size());
}
int main(int argc, char **argv) {
assert(argc == 2);
string outdir = argv[1];
// ifstream in(outdir + "/test.dex");
// std::vector<uint8_t> buf{std::istreambuf_iterator<char>(in), std::istreambuf_iterator<char>()};
// dex::Reader reader(buf.data(), buf.size());
// reader.CreateFullIr();
// auto ir = reader.GetIr();
// auto idx = reader.FindClassIndex("Lio/github/xposed/test/T;");
// std::cout << ir->classes[idx]->direct_methods[1]->decl->prototype->shorty->c_str() << std::endl;
// GenerateTrivialDexFile(outdir);
// GenerateSimpleTestCases(outdir);
GenerateHooker(outdir);
}