-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_wa
More file actions
3574 lines (3313 loc) · 100 KB
/
Copy path_wa
File metadata and controls
3574 lines (3313 loc) · 100 KB
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
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "pub_tool_basics.h"
#include "pub_tool_tooliface.h"
#include "pub_tool_libcassert.h" // tl_assert()
#include "pub_tool_libcprint.h" // VG_(printf)
#include "pub_tool_machine.h" // VG_(fnptr_to_fnentry)
#include "pub_tool_libcbase.h" // VG_(strcmp)
#include "pub_tool_options.h"
#include "pub_tool_guest.h"
#include "pub_tool_debuginfo.h"
#include "pub_core_threadstate.h"
#include "unistd-asm-arm.h"
#include "util.h"
#include "copy.h"
#include "do_oatplus.h"
#include "do_wrappers.h"
#include "do_oatplus.h"
#include "do_framework.h"
#include "do_oatdexparse.h"
#include "shadow_memory.h"
#include "taint_analysis.h"
#include "symbolic_execution.h"
#define STR_COMPARE
//#undef STR_COMPARE
#define PARSE_RET_PARAMETER
//#undef PARSE_RET_PARAMETER
#define FZ_DEBUG
#undef FZ_DEBUG
#if DBG_OAT_PARSE
Bool is_parse_oat = False;
#endif
static Bool mytest = False;
static HChar* unknown_str = "????";
// export VALGRIND_LIB=/home/fanatic/valgrind-3.8.1/inst/lib/valgrind/
UChar codeLayer[TG_N_THREADS] = {0};
static Char* clo_fnname = NULL;
int fd_to_taint = 0;
static HChar *libartFunc = NULL;
static Addr libartFuncReturnAddr = 0;
static DebugInfo *di_libart = NULL;
static Addr libart_text_addr = 0;
static UInt libart_text_size = 0;
static Addr base_oatdata_addr = 0;
static UInt base_oatdata_size = 0;
static Addr base_oatexec_addr = 0;
static UInt base_oatexec_size = 0;
static Addr boot_oatdata_addr = 0;
static UInt boot_oatdata_size = 0;
static Addr boot_oatexec_addr = 0;
static UInt boot_oatexec_size = 0;
Int do_start_method_index = -1;
HChar *do_start_method_name = NULL;
Int do_stop_method_index = -1;
HChar *do_stop_method_name = NULL;
Bool do_is_start = False;
Bool is_trace_irst = False;
static Bool do_method_trace = False;
/*------------- Added for ART framework tracking ------------------*/
DebugInfo* di_art = NULL;
typedef
enum {
NOTART_ADDR = 0,
LIBART_ADDR,
BASE_OATDATA_ADDR,
BASE_OATEXEC_ADDR,
BOOT_OATDATA_ADDR,
BOOT_OATEXEC_ADDR
} exeAddressType;
static INLINE
UInt isBaseAddr(Addr addr) {
if( (addr >= (base_oatdata_addr & 0xfffffffc)) && (addr < (base_oatdata_addr + base_oatdata_size)) )
return 2; //BASE_OATDATA_ADDR;
if( (addr >= (base_oatexec_addr & 0xfffffffc)) && (addr < (base_oatexec_addr + base_oatexec_size)) )
return 3; //BASE_OATEXEC_ADDR;
return 0;//NOTART_ADDR;
}
static INLINE
UInt isSysAddr(Addr addr)
{
return isSysLib(addr, NULL);
}
static INLINE
UInt isArtAddr(Addr addr)
{
if( (addr >= (libart_text_addr & 0xfffffffc)) && (addr < (libart_text_addr + libart_text_size)) )
return 1; //LIBART_ADDR;
if( (addr >= (base_oatdata_addr & 0xfffffffc)) && (addr < (base_oatdata_addr + base_oatdata_size)) )
return 2; //BASE_OATDATA_ADDR;
if( (addr >= (base_oatexec_addr & 0xfffffffc)) && (addr < (base_oatexec_addr + base_oatexec_size)) )
return 3; //BASE_OATEXEC_ADDR;
if( (addr >= (boot_oatdata_addr & 0xfffffffc)) && (addr < (boot_oatdata_addr + boot_oatdata_size)) )
return 4; //BOOT_OATDATA_ADDR;
if( (addr >= (boot_oatexec_addr & 0xfffffffc)) && (addr < (boot_oatexec_addr + boot_oatexec_size)) )
return 5; //BOOT_OATEXEC_ADDR;
return 0;//NOTART_ADDR;
}
void parseOatFile() {
HChar *soname = NULL;
Addr avma, oatdata, oatexec;
SizeT size, oatdataSize, oatexecSize;
DebugInfo* di = VG_(next_DebugInfo) (NULL);
while(di) {
soname = VG_(DebugInfo_get_soname)(di);
if(VG_(DebugInfo_is_oat)(di)) {
MY_LOGI("Meet oat file: %s\n", soname);
if(VG_(get_symbol_range_SLOW)(di, "oatdata", &oatdata, &oatdataSize)) {
MY_LOGI("oatdata: 0x%08x - 0x%08x len=%d\n", oatdata, oatdata+oatdataSize, oatdataSize);
if(VG_(get_symbol_range_SLOW)(di, "oatexec", &oatexec, &oatexecSize)) {
MY_LOGI("oatexec: 0x%08x - 0x%08x len=%d\n", oatexec, oatexec+oatexecSize, oatexecSize);
if( (VG_(strcmp)("classes.oat", soname) == 0) ) { // Custom oat file
#if DBG_OAT_PARSE
is_parse_oat = True;
#endif
oatDexParse(oatdata, oatdataSize, oatexec, oatexecSize);
#if DBG_OAT_PARSE
is_parse_oat = False;
#endif
base_oatdata_addr = oatdata;
base_oatdata_size = oatdataSize;
base_oatexec_addr = oatexec;
base_oatexec_size = oatexecSize;
} else if (( VG_(strcmp)("system@framework@boot.oat", soname) == 0) ) { // Framework oat file
if(boot_oatdata_addr == 0) {
oatDexParse(oatdata, oatdataSize, oatexec, oatexecSize);
boot_oatdata_addr = oatdata;
boot_oatdata_size = oatdataSize;
boot_oatexec_addr = oatexec;
boot_oatexec_size = oatexecSize;
}
} else {
#if DBG_OAT_PARSE
is_parse_oat = True;
#endif
oatDexParse(oatdata, oatdataSize, oatexec, oatexecSize);
#if DBG_OAT_PARSE
is_parse_oat = False;
#endif
}
}
}
} else {
MY_LOGI("Meet so file: %s\n", soname);
if(VG_(strcmp)("libart.so", soname) == 0) {
di_libart = di;
libart_text_addr = VG_(DebugInfo_get_text_avma) (di);
libart_text_size = VG_(DebugInfo_get_text_size) (di);
MY_LOGI("Meet so file: %s 0x%08x - 0x%08x\n", soname, libart_text_addr, libart_text_addr + libart_text_size);
//VG_(print_sym_table)(di);
}
}
di = VG_(next_DebugInfo)(di);
}
}
static void do_set_instrumentate(const HChar *reason, Bool state) {
do_method_trace = state; // Represent the instrumentation state
VG_(discard_translations_safely)( (Addr)0x1000, ~(SizeT)0xfff, "datatrace");
/*if (state) {
parseOatFile();
}*/
MY_LOGI("%s: Switch instrumentation %s ... \n",
reason, state ? "ON" : "OFF");
if (VG_(clo_verbosity) > 1)
VG_(message)(Vg_DebugMsg, "%s: instrumentation switched %s\n",
reason, state ? "ON" : "OFF");
}
static
INLINE Bool is_base_apk(Char *path) {
Int i = 0, j = 0, len = VG_(strlen)(path);
Char *fileName = NULL;
for(i = 0; i < len; i++) {
if(path[i] == '/')
j = i;
}
fileName = &path[j+1];
if(VG_(strcmp)("base.apk", fileName) == 0) {
return True;
}
return False;
}
static
Bool isFrameworkClass(HChar* desc) {
return False;
}
static
Bool do_handle_client_requests( ThreadId tid, UWord *arg, UWord *ret) {
switch (arg[0]) {
case VG_USERREQ__WRAPPER_LIBC_KILL:
{
MY_LOGI("[1]LIBC(%d):kill() pid=%d sig=%d res=%d\n", tid, arg[1], arg[2], arg[3]);
break;
}
case VG_USERREQ__WRAPPER_LIBC_EXIT:
{
MY_LOGI("[1]LIBC(%d):exit()\n", tid);
break;
}
case VG_USERREQ__WRAPPER_LIBC_ABORT:
{
MY_LOGI("[1]LIBC(%d):abort()\n", tid);
break;
}
case VG_USERREQ__WRAPPER_DLOPEN_PRE:
{
MY_LOGI("[0]LIBART(%d):dlopen() %s 0x%x\n",
tid, (HChar*)arg[1], arg[2]);
break;
}
case VG_USERREQ__WRAPPER_DLOPEN:
{
MY_LOGI("[1]LIBART(%d):dlopen() %s 0x%x res=0x%08x\n",
tid, (HChar*)arg[1], arg[2], arg[3]);
break;
}
case VG_USERREQ__WRAPPER_DLSYM_PRE:
{
MY_LOGI("[0]LIBART(%d):dlsym() %s handle=0x%08x\n",
tid, (HChar*)arg[2], arg[1]);
break;
}
case VG_USERREQ__WRAPPER_DLSYM:
{
MY_LOGI("[1]LIBART(%d):dlsym() %s handle=0x%08x res=0x%08x\n",
tid, (HChar*)arg[2], arg[1], arg[3]);
break;
}
case VG_USERREQ__WRAPPER_ART_FINDNATIVEMETHOD:
{
// void* FindNativeMethod(ArtMethod* m, std::string& detail)
struct ArtMethodPlus *pAMth = (struct ArtMethodPlus *)arg[2];
struct StdString *library = (struct StdString*)arg[3];
Addr codeAddr = (Addr)arg[4];
MY_LOGI("[0]LIBART(%d):FindNativeMethod() method=%s res=0x%08x\n", tid, library ? library->data : "NULL", codeAddr);
break;
}
case VG_USERREQ__WRAPPER_ART_LOADNATIVELIBRARY_PRE:
{
struct StdString *path = (struct StdString*)arg[1];
if(VG_(memcmp)(path->data, "/data/data/", 11) == 0) {
}
MY_LOGI("[0]LIBART(%d):LoadNativeLibrary() %s\n", tid, path ? path->data : "Unknown");
break;
}
case VG_USERREQ__WRAPPER_ART_LOADNATIVELIBRARY:
{
struct StdString *path = (struct StdString*)arg[2];
MY_LOGI("[1]LIBART(%d):LoadNativeLibrary() %s\n", tid, path ? path->data : "Unknown");
if(VG_(memcmp)(path->data, "/data/data/", 11) == 0) {
initSysLib();
parseOatFile();
} else {
if(do_is_start && path) {
addMonitorLib(path->data);
}
}
break;
}
case VG_USERREQ__WRAPPER_ART_OPENMEMORY_PRE:
{
struct StdString *location = (struct StdString*)arg[1];
struct MemMapPlus *pMemMapObj = (struct MemMapPlus*)arg[2];
MY_LOGI("[0]LIBART(%d):OpenMemory() pMemMapObj=0x%08x, location=%s\n",
tid, (Addr)pMemMapObj, location->data);
break;
}
case VG_USERREQ__WRAPPER_ART_OPENMEMORY:
{
struct DexFilePlus *pDexFileObj = (struct DexFilePlus*)arg[1];
struct StdString *location = (struct StdString*)arg[2];
struct MemMapPlus *pMemMapObj = (struct MemMapPlus*)arg[3];
MY_LOGI("[1]LIBART(%d):OpenMemory() pMemMapObj=0x%08x, location=%s, pDexFileObj=0x%08x\n",
tid, (Addr)pMemMapObj, location->data, (Addr)pDexFileObj);
#if 0
if(location->data) {
//if(is_base_apk(location->data))
if(VG_(memcmp)(location->data, "/data/app/", 10) == 0) {
do_set_instrumentate("start instrumentation", True);
if( do_is_start == False) {
initSysLib();
do_is_start = True;
}
}
}
#endif
break;
}
case VG_USERREQ__WRAPPER_ART_DEFINECLASS_PRE:
{
HChar *descriptor = (HChar*)arg[1];
if(isFrameworkClass(descriptor))
break;
MY_LOGI("[1]LIBART(%d):DefineClass()\n", tid);
break;
struct DexFilePlus *pDexFileObj = (struct DexFilePlus*)arg[2];
struct MemMapPlus *pMemMapObj = pDexFileObj->mem_map_;
struct DexHeader *pHeader = pDexFileObj->header_;
MY_LOGI("[0]LIBART(%d):DefineClass() %s pDexFileObj=0x%08x pMemMapObj=0x%08x 0x%08x-0x%08x 0x%08x %d\n",
tid, descriptor, (Addr)pDexFileObj, (Addr)pMemMapObj, pDexFileObj->begin_,
(Addr)pDexFileObj->begin_ + pDexFileObj->size_, (Addr)pHeader, pHeader->fileSize);
break;
}
case VG_USERREQ__WRAPPER_ART_DEFINECLASS:
{
HChar *descriptor = (HChar*)arg[1];
if(isFrameworkClass(descriptor))
break;
//MY_LOGI("[1]LIBART(%d):DefineClass()\n", tid);
//break;
struct DexFilePlus *pDexFileObj = (struct DexFilePlus*)arg[2];
struct MemMapPlus *pMemMapObj = pDexFileObj->mem_map_;
struct DexHeader *pHeader = pDexFileObj->header_;
MY_LOGI("[1]LIBART(%d):DefineClass() %s pDexFileObj=0x%08x pMemMapObj=0x%08x 0x%08x-0x%08x 0x%08x %d\n",
tid, descriptor, (Addr)pDexFileObj, (Addr)pMemMapObj, pDexFileObj->begin_,
(Addr)pDexFileObj->begin_ + pDexFileObj->size_, (Addr)pHeader, pHeader->fileSize);
if(VG_(strcmp)("Lcom/qihoo/util/StubApp3965810277;", descriptor) == 0) {
do_set_instrumentate("start instrumentation in DefineClass()", True);
do_is_start = True;
}
break;
}
case VG_USERREQ__WRAPPER_ART_CALLMETHODA:
{
HChar *fn_name = (HChar*)arg[1];
Int mid = arg[2];
Int type = arg[3];
Int invoke = arg[4];
MY_LOGI("[0]LIBART(%d) CallMethodA() %s mid=%d type=%dinvoke=%d\n", tid, fn_name, mid, type, invoke);
break;
}
case VG_USERREQ__WRAPPER_ART_CALLMETHODV:
{
HChar *fn_name = (HChar*)arg[1];
Int mid = arg[2];
Int type = arg[3];
Int invoke = arg[4];
MY_LOGI("[0]LIBART(%d) CallMethodV() %s mid=%d type=%dinvoke=%d\n", tid, fn_name, mid, type, invoke);
break;
}
case VG_USERREQ__WRAPPER_ART_INVOKEWITHVARARGS:
{
Int mid = (Int)arg[3];
MY_LOGI("[1]LIBART(%d):InvokeWithVarArgs(JNI Reflection) mid=%d\n",tid, mid);
break;
}
case VG_USERREQ__WRAPPER_ART_INVOKEWITHJVALUES:
{
Int mid = (Int)arg[3];
MY_LOGI("[1]LIBART(%d):InvokeWithJValues(JNI Reflection) mid=%d\n",tid, mid);
break;
}
case VG_USERREQ__WRAPPER_ART_INVOKEVIRTUALORINTERFACEWITHJVALUES:
{
Int mid = (Int)arg[3];
MY_LOGI("[1]LIBART(%d):InvokeVirtualOrInterfaceWithJValues(JNI Reflection) mid=%d\n",tid, mid);
break;
}
case VG_USERREQ__WRAPPER_ART_INVOKEVIRTUALORINTERFACEWITHVARARGS:
{
Int mid = (Int)arg[3];
MY_LOGI("[1]LIBART(%d):InvokVirtualOrInterfaceWithVarArgs(JNI Reflection) mid=%d\n",tid, mid);
break;
}
case VG_USERREQ__WRAPPER_ART_INVOKEMETHOD:
{
MY_LOGI("[1]LIBART(%d):InvokeMethod(Java Reflection) javaMethod=0x%08x\n", tid, arg[2]);
break;
}
case VG_USERREQ__WRAPPER_ART_INVOKEWITHARGARRAY:
{
struct ArtMethodPlus *pAMth = (struct ArtMethodPlus *)arg[1];
MY_LOGI("[1]LIBART(%d):InvokeWithArgArray() pArtMethod=0x%08x\n", tid, (Addr)pAMth);
break;
}
case VG_USERREQ__WRAPPER_ART_INVOKE_PRE:
{
if(do_is_start == False)
return False;
struct ArtMethodPlus *pAMth = (struct ArtMethodPlus *)arg[1];
MY_LOGI("[0]LIBART(%d):Invoke() pArtMethod=0x%08x\n", tid, (Addr)pAMth);
break;
}
case VG_USERREQ__WRAPPER_ART_INVOKE:
{
if(do_is_start == False)
return False;
struct ArtMethodPlus *pAMth = (struct ArtMethodPlus *)arg[1];
MY_LOGI("[1]LIBART(%d):Invoke() pArtMethod=0x%08x\n", tid, (Addr)pAMth);
break;
}
case VG_USERREQ__WRAPPER_ART_REGISTERNATIVE:
{
struct ArtMethodPlus *pAMth = (struct ArtMethodPlus *)arg[1];
if(do_is_start == False)
return False;
//MthNode* query_method_node(Addr codeAddr, Int index)
//MthNode *pNote = (MthNode*)query_method_node(pAMth->codeAddr, pAMth
MY_LOGI("[1]LIBART(%d):RegisterNative() pArtMethod=0x%08x nativeCode=0x%08x\n", tid, (Addr)pAMth, (Addr)arg[2]);
break;
}
case VG_USERREQ__WRAPPER_ART_GETSTRINGUTFCHARS:
{
HChar *str = (HChar*)arg[1];
if(do_is_start == False)
return False;
//if(VG_(memcmp)("aGlvZi5lbm", str, 10) == 0)
// str[1] = 'b';
MY_LOGI("[1]LIBART(%d):GetStringUTFChars() res=%s\n", tid, str);
break;
}
case VG_USERREQ__WRAPPER_ART_JNIFINDCLASS:
{
HChar *class_name = (HChar*)arg[2];
Addr res = (Addr)arg[3];
if(do_is_start == False)
return False;
MY_LOGI("[1]LIBART(%d):FindClass() %s jclass=0x%08x\n",tid, class_name, res);
break;
}
case VG_USERREQ__WRAPPER_ART_JNIGETMETHODID:
{
Addr cl = (Addr)arg[1];
HChar* mth_name = (HChar*)arg[2];
HChar* sig = (HChar*)arg[3];
Addr res = (Addr)arg[4];
MY_LOGI("[1]LIBART(%d):GetMethodID() jclass=0x%08x %s %s jmethodId=0x%08x\n",tid, cl, mth_name, sig, res);
break;
}
case VG_USERREQ__WRAPPER_ART_JNIGETSTATICMETHODID:
{
Addr cl = (Addr)arg[1];
HChar* mth_name = (HChar*)arg[2];
HChar* sig = (HChar*)arg[3];
Addr res = (Addr)arg[4];
MY_LOGI("[1]LIBART(%d):GetStaticMethodID() 0x%08x %s %s id=0x%08x\n",tid, cl, mth_name, sig, res);
break;
}
case VG_USERREQ__WRAPPER_ART_DEXFILE:
{
struct DexFilePlus *pDexFileObj = (struct DexFilePlus*)arg[1];
struct MemMapPlus *pMemMapObj = pDexFileObj->mem_map_;
Addr base = (Addr)arg[2];
UInt len = (UInt)arg[3];
struct StdString *location = (struct StdString*)arg[4];
Addr memmap = (Addr)arg[5];
MY_LOGI("[1]LIBART(%d):DexFile() pMemMapObj=0x%08x, location=%s, pDexFileObj=0x%08s\n",
tid, (Addr)pMemMapObj, location->data, (Addr)pDexFileObj);
break;
}
case VG_USERREQ__WRAPPER_CLASSLINKER_LINKCODE:
{
struct ArtMethodPlus *pAMth = (struct ArtMethodPlus *)arg[1];
MY_LOGI("[1]LIBART[%d]:LinkCode() flags=0x%08x dex_method_index=%d method_index=%d\n",
tid, pAMth->access_flags_, pAMth->dex_method_index_, arg[3]);
break;
}
case VG_USERREQ__WRAPPER_REP_MEMMOVE_OR_MEMCPY:
{
#ifdef STR_COMPARE
const HChar *s1 = (HChar*)arg[1];
const HChar *s2 = (HChar*)arg[2];
const Int n = (Int)arg[3];
if(is_trace_irst)
VG_(printf)("[MEM]: memcpy() s1=0x%08x(%s) s2=0x%08x(%s) n=%d\n", (Addr)s1, s1, (Addr)s2, s2, n);
#endif
break;
}
case VG_USERREQ__WRAPPER_REP_STRLEN:
{
#ifdef STR_COMPARE
const HChar *s1 = (HChar*)arg[1];
const Int n = (Int)arg[2];
if(is_trace_irst)
VG_(printf)("[MEM]: strlen() s=%s len=%d\n", s1, n);
#endif
break;
}
case VG_USERREQ__WRAPPER_REP_MEMCMP:
{
#ifdef STR_COMPARE
const HChar *s1 = (HChar*)arg[1];
const HChar *s2 = (HChar*)arg[2];
const Int n = (Int)arg[3];
if(is_trace_irst)
VG_(printf)("[MEM]: memcmp() s1=%s s2=%s n=%d\n", s1, s2, n);
#endif
break;
}
case VG_USERREQ__WRAPPER_REP_STRSTR:
{
#ifdef STR_COMPARE
const HChar *s1 = (HChar*)arg[1];
const HChar *s2 = (HChar*)arg[2];
if(is_trace_irst)
VG_(printf)("[MEM]: strstr() s1=%s s2=%s\n", s1, s2);
#endif
break;
}
case VG_USERREQ__WRAPPER_REP_STRCMP:
{
#ifdef STR_COMPARE
const HChar *s1 = (HChar*)arg[1];
const HChar *s2 = (HChar*)arg[2];
if(is_trace_irst)
VG_(printf)("[MEM]: strcmp() s1=%s s2=%s\n", s1, s2);
#endif
break;
}
case VG_USERREQ__WRAPPER_REP_STRNCMP:
{
#ifdef STR_COMPARE
const HChar *s1 = (HChar*)arg[1];
const HChar *s2 = (HChar*)arg[2];
const Int len = (Int)arg[3];
if(is_trace_irst)
VG_(printf)("[MEM]: strncmp() s1=%s s2=%s len=%d\n", tid, s1, s2, len);
#endif
break;
}
case VG_USERREQ__WRAPPER_ART_CALLSTATICOBJECTMETHODV_PRE:
{
const Addr jclass = (Addr)arg[2];
const Int mid = (Addr)arg[3];
if(codeLayer[tid] == 1)
MY_LOGI("[0]LIBART[%d]:CallStaticObjectMethodV jclass=0x%08x mid=0x%x\n", tid, jclass, mid);
break;
}
case VG_USERREQ__WRAPPER_ART_CALLSTATICOBJECTMETHODV:
{
const Addr jclass = (Addr)arg[2];
const Int mid = (Addr)arg[3];
if(codeLayer[tid] == 1)
MY_LOGI("[1]LIBART[%d]:CallStaticObjectMethodV jclass=0x%08x mid=0x%x\n", tid, jclass, mid);
break;
}
default:
{
return False;
}
}
return True;
}
/*-------------------------- End ----------------------------------*/
static
UInt valueOfConst(IRExpr* data)
{
UInt data_value = -1;
if (data->tag == Iex_Const)
{
switch (data->Iex.Const.con->tag)
{
case Ico_U1: data_value = data->Iex.Const.con->Ico.U1; break;
case Ico_U8: data_value = data->Iex.Const.con->Ico.U8; break;
case Ico_U16: data_value = data->Iex.Const.con->Ico.U16; break;
case Ico_V128:data_value = data->Iex.Const.con->Ico.V128; break;
case Ico_U32: data_value = data->Iex.Const.con->Ico.U32; break;
case Ico_F32i:data_value = data->Iex.Const.con->Ico.F32i; break;
case Ico_V256:data_value = data->Iex.Const.con->Ico.V256; break;
case Ico_U64: data_value = data->Iex.Const.con->Ico.U64; break;
case Ico_F64i:data_value = data->Iex.Const.con->Ico.F64i; break;
default: ppIRExpr(data); tl_assert(0);
}
}
return data_value;
}
Int sizeofIRType_bits(IRType ty)
{
switch (ty)
{
case Ity_I1: return 1;
case Ity_I8: return 8;
case Ity_I16: return 16;
case Ity_I32: return 32;
case Ity_I64: return 64;
case Ity_I128: return 128;
case Ity_F32: return 32;
case Ity_F64: return 64;
case Ity_D32: return 32;
case Ity_D64: return 64;
case Ity_D128: return 128;
case Ity_F128: return 128;
case Ity_V128: return 128;
case Ity_V256: return 256;
default: VG_(tool_panic)("sizeofIRType_bits");
}
}
/*
Bind the given expression to a new temporary, and return the temporary.
This effectively converts an arbitrary expression into an IRAtom.
*/
static IRExpr* assignNew(IRSB* sb_out, IRExpr* expr)
{
IRTemp tmp = newIRTemp(sb_out->tyenv, typeOfIRExpr(sb_out->tyenv, expr));
addStmtToIRSB(sb_out, IRStmt_WrTmp(tmp, expr));
return IRExpr_RdTmp(tmp);
}
static IRExpr* assignNew_HWord(IRSB* sb_out, IRExpr* expr)
{
IRTemp tmp = newIRTemp(sb_out->tyenv, Ity_I32), tmp1;
switch (typeOfIRExpr(sb_out->tyenv, expr))
{
case Ity_I1:
addStmtToIRSB(sb_out, IRStmt_WrTmp(tmp, IRExpr_Unop(Iop_1Uto32, expr)));
break;
case Ity_I8:
addStmtToIRSB(sb_out, IRStmt_WrTmp(tmp, IRExpr_Unop(Iop_8Uto32, expr)));
break;
case Ity_I16:
addStmtToIRSB(sb_out, IRStmt_WrTmp(tmp, IRExpr_Unop(Iop_16Uto32, expr)));
break;
case Ity_I32:
addStmtToIRSB(sb_out, IRStmt_WrTmp(tmp, expr));
break;
case Ity_I64:
tmp1 = newIRTemp(sb_out->tyenv, Ity_I64);
addStmtToIRSB(sb_out, IRStmt_WrTmp(tmp1, expr));
addStmtToIRSB(sb_out, IRStmt_WrTmp(tmp, IRExpr_Unop(Iop_64to32, IRExpr_RdTmp(tmp1))));
break;
/*
* case Ity_F32:
addStmtToIRSB(sb_out, IRStmt_WrTmp(tmp, IRExpr_Unop(Iop_F32toI32U, expr)));
break;
* case Ity_F64:
addStmtToIRSB(sb_out, IRStmt_WrTmp(tmp, IRExpr_Unop(Iop_F64toI32U, expr)));
break;*/
default:
/*VG_(printf)("Unknown: ");
ppIRExpr(expr);
ppIRType(typeOfIRExpr(sb_out->tyenv, expr));
VG_(printf)("\n");*/
return mkIRExpr_HWord(0xffffffff);
tl_assert(0);
//VG_(tool_panic)("assignNew_HWord");
}
return IRExpr_RdTmp(tmp);
}
static IRExpr* assignNew_ULong(IRSB* sb_out, IRExpr* expr)
{
IRTemp tmp = newIRTemp(sb_out->tyenv, Ity_I64);
switch (typeOfIRExpr(sb_out->tyenv, expr))
{
case Ity_I1:
addStmtToIRSB(sb_out, IRStmt_WrTmp(tmp, IRExpr_Unop(Iop_1Uto64, expr)));
break;
case Ity_I8:
addStmtToIRSB(sb_out, IRStmt_WrTmp(tmp, IRExpr_Unop(Iop_8Uto64, expr)));
break;
case Ity_I16:
addStmtToIRSB(sb_out, IRStmt_WrTmp(tmp, IRExpr_Unop(Iop_16Uto64, expr)));
break;
case Ity_I32:
addStmtToIRSB(sb_out, IRStmt_WrTmp(tmp, IRExpr_Unop(Iop_32Uto64, expr)));
break;
case Ity_I64:
addStmtToIRSB(sb_out, IRStmt_WrTmp(tmp, expr));
break;
default:
ppIRExpr(expr);
ppIRType(typeOfIRExpr(sb_out->tyenv, expr));
tl_assert(0);
//VG_(tool_panic)("assignNew_HWord");
}
return IRExpr_RdTmp(tmp);
}
//
// CONCOLIC EXECUTION HELPERS
//
static VG_REGPARM(4) void helper_instrument_Put(UInt offset, IRTemp data, Int value, UInt size)
//static VG_REGPARM(3) void helper_instrument_Put(UInt offset, IRTemp data, UInt size)
{
if (do_is_start == False)
return;
if (get_reg_from_offset(offset) == guest_INVALID)
{
tl_assert(!IRTemp_is_tainted(data));
return;
}
// Do taint propagation from tmporary to register
if (register_is_tainted(offset) != IRTemp_is_tainted(data))
{
#ifdef FZ_LOG_IR
if(data == IRTemp_INVALID) {
ST_LOGI("PUT(%d) <- 0x%x:I%d\n", offset, value, size);
} else {
ST_LOGI("PUT(%d) <- t%d:I%d | (0x%x)\n", offset, data, size, value);
}
#endif
flip_register(offset, IRTemp_is_tainted(data));
}
// Do symbolic execution
if (
#ifdef FZ_EXE_TAINT
IRTemp_is_tainted(data)
#else
True
#endif
) {
char dep[DEP_MAX_LEN] = {0};
#ifdef FZ_EXE_TAINT
char *dep_rhs = get_temporary_dep(data);
#else
char *dep_rhs = NULL;
char dep_tmp[DEP_MAX_LEN] = {0};
if( data == IRTemp_INVALID )
{
VG_(snprintf)(dep_tmp, DEP_MAX_LEN, "FIX:%d(%u)", size, value);
dep_rhs = dep_tmp;
} else {
dep_rhs = get_temporary_dep(data);
}
#endif
if(dep_rhs) {
//VG_(snprintf)(dep, DEP_MAX_LEN, "PUT(%s)", dep_rhs);
VG_(snprintf)(dep, DEP_MAX_LEN, "%s", dep_rhs);
update_register_dep(offset, size, dep);
} else {
free_register_dep(offset);
}
} else {
free_register_dep(offset);
}
}
static VG_REGPARM(4) void helper_instrument_PutI(UInt base, UInt ix, UInt bias, UInt nElems)
{
if (do_is_start == False)
return;
VG_(printf)("PutI()\n");
UInt index = base+((ix+bias)%nElems);
tl_assert(get_reg_from_offset(index) == guest_INVALID);
}
static VG_REGPARM(4) void helper_instrument_WrTmp_Get(IRTemp tmp, UInt offset, UInt value, UInt size)
{
if (do_is_start == False)
return;
// Do taint propagation
if (temporary_is_tainted(tmp) != register_is_tainted(offset))
{
#ifdef FZ_LOG_IR
ST_LOGI("t%d <- GET:I%d(%u) | 0x%x\n", tmp, size, offset, value);
#endif
flip_temporary(tmp);
}
// Do symbolic execution
if (
#ifdef FZ_EXE_TAINT
register_is_tainted(offset)
#else
True
#endif
) {
char dep[DEP_MAX_LEN] = {0};
char *tmp_rhs = NULL;
tmp_rhs = get_register_dep(offset);
if(tmp_rhs) {
//VG_(snprintf)(dep, DEP_MAX_LEN, "GET(%s)", tmp_rhs);
VG_(snprintf)(dep, DEP_MAX_LEN, "%s", tmp_rhs);
update_temporary_dep(tmp, dep, size);
} else {
free_temporary_dep(tmp);
}
} else {
free_temporary_dep(tmp);
}
}
static VG_REGPARM(4) void helper_instrument_WrTmp_GetI(UInt base, UInt ix, UInt bias, UInt nElems)
{
if (do_is_start == False)
return;
VG_(printf)("GetI()\n");
UInt index = base+((ix+bias)%nElems);
tl_assert(get_reg_from_offset(index) == guest_INVALID);
}
static VG_REGPARM(3)
void helper_instrument_WrTmp_RdTmp(IRTemp tmp_lhs, IRTemp tmp_rhs, UInt size)
{
if (do_is_start == False)
return;
if (temporary_is_tainted(tmp_lhs) != temporary_is_tainted(tmp_rhs))
{
#ifdef FZ_LOG_IR
ST_LOGI("t%d <- t%d:I%d\n", tmp_lhs, tmp_rhs, size);
#endif
flip_temporary(tmp_lhs);
}
if (
#ifdef FZ_EXE_TAINT
temporary_is_tainted(tmp_rhs)
#else
True
#endif
) {
char dep[DEP_MAX_LEN] = {0};
char *dep_rhs = NULL;
dep_rhs = get_temporary_dep(tmp_rhs);
if(dep_rhs) {
VG_(snprintf)(dep, DEP_MAX_LEN, "RdTmp(%s)", dep_rhs);
update_temporary_dep(tmp_lhs, dep, size);
} else {
free_temporary_dep(tmp_lhs);
}
}
else
{
free_temporary_dep(tmp_lhs);
}
}
static VG_REGPARM(4)
void helper_instrument_WrTmp_Triop_SetElem(IRStmt *clone, UInt size, UInt arg1_value, UInt arg3_value)
{
if (do_is_start == False)
return;
IRExpr *e1 = NULL, *e2 = NULL, *e3 = NULL;
IROp op = clone->Ist.WrTmp.data->Iex.Triop.details->op;
e1 = clone->Ist.WrTmp.data->Iex.Triop.details->arg1;
e2 = clone->Ist.WrTmp.data->Iex.Triop.details->arg2;
e3 = clone->Ist.WrTmp.data->Iex.Triop.details->arg3;
IRTemp tmp = clone->Ist.WrTmp.tmp;
IRTemp arg1 = (e1->tag == Iex_RdTmp) ? e1->Iex.RdTmp.tmp : IRTemp_INVALID;
Int arg2_value = valueOfConst(e2);
IRTemp arg3 = (e3->tag == Iex_RdTmp) ? e3->Iex.RdTmp.tmp : IRTemp_INVALID;
Int size0 = size & 0xff, size1 = (size >> 8) & 0xff, size2 = (size >> 16) & 0xff, size3 = (size >> 24) & 0xff;
char str[32] = {0}, dep[DEP_MAX_LEN] = {0};
IROp_to_str(op, str);
if ((temporary_is_tainted(tmp) != temporary_is_tainted(arg1))
|| (temporary_is_tainted(tmp) != temporary_is_tainted(arg3)))
{
#ifdef FZ_LOG_IR
ST_LOGI("t%d <- %s(t%d, 0x%x:I8, t%d) | (0x%x, 0x%x, 0x%x)\n",
tmp, str, arg1, arg2_value, arg3, arg1_value, arg2_value, arg3_value);
#endif
flip_temporary(tmp);
}
if (
#ifdef FZ_EXE_TAINT
IRTemp_is_tainted(arg1) || IRTemp_is_tainted(arg3)
#else
True
#endif
) {
char *tmp_rhs1 = NULL, *tmp_rhs3 = NULL;
//VG_(printf)("WrTmp_Binop: t%d=%s(t%d, t%d)\n", tmp, str, arg1, arg2);
if (
#ifdef FZ_EXE_TAINT
!IRTemp_is_tainted(arg1)
#else
arg1 == IRTemp_INVALID
#endif
) {
tmp_rhs3 = get_temporary_dep(arg3);
if(tmp_rhs3) {
VG_(snprintf)(dep, DEP_MAX_LEN, "%s(FIX:%d(%u),%u,%s)",
str, size1, arg1_value, arg2_value, tmp_rhs3);
update_temporary_dep(tmp, dep, size0);
} else {
free_temporary_dep(tmp);
}
return;
}
if (
#ifdef FZ_EXE_TAINT
!IRTemp_is_tainted(arg3)
#else
arg3 == IRTemp_INVALID
#endif
) {
tmp_rhs1 = get_temporary_dep(arg1);
if(tmp_rhs1) {
VG_(snprintf)(dep, DEP_MAX_LEN, "%s(%s,%u,FIX:%d(%u))",
str, tmp_rhs1, arg2_value, size3, arg3_value);
update_temporary_dep(tmp, dep, size0);
} else {
free_temporary_dep(tmp);
}
return;
}
tmp_rhs1 = get_temporary_dep(arg1);
tmp_rhs3 = get_temporary_dep(arg3);
if(tmp_rhs1 && tmp_rhs3) {
VG_(snprintf)(dep, DEP_MAX_LEN, "%s(%s,%u,%s)",
str, tmp_rhs1, arg2_value, tmp_rhs3);
update_temporary_dep(tmp, dep, size0);
} else {
free_temporary_dep(tmp);
}
return;
}
free_temporary_dep(tmp);
return;
}
static VG_REGPARM(4) void helper_instrument_WrTmp_Binop(IRStmt *clone, UInt size, UInt arg1_value, UInt arg2_value)
{
if (do_is_start == False)
return;
IRExpr *e1 = NULL, *e2 = NULL;
char str[32] = {0}, dep[DEP_MAX_LEN] = {0};
tl_assert(clone);
e1 = clone->Ist.WrTmp.data->Iex.Binop.arg1;
e2 = clone->Ist.WrTmp.data->Iex.Binop.arg2;
IROp op = clone->Ist.WrTmp.data->Iex.Binop.op;
IRTemp tmp = clone->Ist.WrTmp.tmp;
IRTemp arg1 = (e1->tag == Iex_RdTmp) ? e1->Iex.RdTmp.tmp : IRTemp_INVALID;
IRTemp arg2 = (e2->tag == Iex_RdTmp) ? e2->Iex.RdTmp.tmp : IRTemp_INVALID;
Int size0 = size & 0xff, size1 = (size >> 8) & 0xff, size2 = (size >> 16) & 0xff;
IROp_to_str(op, str);
/* if(op == Iop_GetElem8x8) {
ppIRStmt(clone);
VG_(printf)("(0x%x = GetElem8x8(0x%x, %d))\n", arg1_value, arg2_value);
}*/
//VG_(printf)("t%d=%s(t%d, t%d)\n", tmp, str, arg1, arg2);
if ((temporary_is_tainted(tmp) != IRTemp_is_tainted(arg1))
|| (temporary_is_tainted(tmp) != IRTemp_is_tainted(arg2))) {
#ifdef FZ_LOG_IR
ST_LOGI("t%d <- %s(t%d, t%d) | (0x%x 0x%x)\n", tmp, str, arg1, arg2, arg1_value, arg2_value);
#endif
flip_temporary(tmp);
}
if (
#ifdef FZ_EXE_TAINT
IRTemp_is_tainted(arg1) || IRTemp_is_tainted(arg2)
#else
True
#endif
) {
char *tmp_rhs1 = NULL, *tmp_rhs2 = NULL;
//VG_(printf)("WrTmp_Binop: t%d=%s(t%d, t%d)\n", tmp, str, arg1, arg2);
if (
#ifdef FZ_EXE_TAINT
!IRTemp_is_tainted(arg1)