-
Notifications
You must be signed in to change notification settings - Fork 58
/
sym_region_frag_machine.ml
1734 lines (1643 loc) · 59.9 KB
/
sym_region_frag_machine.ml
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
(*
Copyright (C) BitBlaze, 2009-2013, and copyright (C) 2010 Ensighta
Security Inc. All rights reserved.
*)
module V = Vine;;
open Exec_domain;;
open Exec_utils;;
open Exec_exceptions;;
open Exec_options;;
open Frag_simplify;;
open Formula_manager;;
open Query_engine;;
open Granular_memory;;
open Fragment_machine;;
open Decision_tree;;
open Sym_path_frag_machine;;
module SymRegionFragMachineFunctor =
functor (D : DOMAIN) ->
struct
module FormMan = FormulaManagerFunctor(D)
module GM = GranularMemoryFunctor(D)
module SPFM = SymPathFragMachineFunctor(D)
type region_location = SingleLocation of int option * int64
| TableLocation of int option * V.exp * int64
let reg_addr () = match !opt_arch with
| (X86|ARM) -> V.REG_32
| X64 -> V.REG_64
let addr_const addr = V.Constant(V.Int(reg_addr(), addr))
(* Sign extend a typed constant to a 64-bit constant *)
let fix_s ty v =
match ty with
| V.REG_1 -> fix_s1 v
| V.REG_8 -> fix_s8 v
| V.REG_16 -> fix_s16 v
| V.REG_32 -> fix_s32 v
| V.REG_64 -> v
| _ -> failwith "Bad type in fix_s"
let is_high_mask ty v =
let is_power_of_2_or_zero x =
Int64.logand x (Int64.pred x) = 0L
in
is_power_of_2_or_zero (Int64.succ (Int64.lognot (fix_s ty v)))
let floor_log2 = Vine_util.int64_floor_log2
(* Conservatively anayze the smallest number of non-zero
least-significant bits into which a value will fit. This is a fairly
quick way to tell if an expression could be an index, or to give a
bound on the size of a table. *)
let narrow_bitwidth form_man e =
let combine wd res = min wd res in
let f loop e =
match e with
| V.Constant(V.Int(ty, v)) -> 1 + floor_log2 v
| V.BinOp(V.BITAND, e1, e2) -> min (loop e1) (loop e2)
| V.BinOp(V.BITOR, e1, e2) -> max (loop e1) (loop e2)
| V.BinOp(V.XOR, e1, e2) -> max (loop e1) (loop e2)
| V.BinOp(V.PLUS, e1, e2) -> 1 + (max (loop e1) (loop e2))
| V.BinOp(V.TIMES, e1, e2) -> (loop e1) + (loop e2)
| V.BinOp(V.MOD, e1, e2) -> min (loop e1) (loop e2)
| V.Cast(V.CAST_UNSIGNED, V.REG_64, e1)
-> min 64 (loop e1)
| V.Cast((V.CAST_UNSIGNED|V.CAST_LOW), V.REG_32, e1)
-> min 32 (loop e1)
| V.Cast((V.CAST_UNSIGNED|V.CAST_LOW), V.REG_16, e1)
-> min 16 (loop e1)
| V.Cast((V.CAST_UNSIGNED|V.CAST_LOW), V.REG_8, e1)
-> min 8 (loop e1)
| V.Cast((V.CAST_UNSIGNED|V.CAST_LOW), V.REG_1, e1)
-> min 1 (loop e1)
| V.Cast(V.CAST_SIGNED, V.REG_32, e1) -> 32
| V.Cast(V.CAST_SIGNED, V.REG_16, e1) -> 16
| V.Cast(V.CAST_SIGNED, V.REG_8, e1) -> 8
| V.Cast(V.CAST_SIGNED, V.REG_1, e1) -> 1
(* High casts could be improved by treating like an RSHIFT *)
| V.Cast(V.CAST_HIGH, V.REG_32, e1) -> 32
| V.Cast(V.CAST_HIGH, V.REG_16, e1) -> 16
| V.Cast(V.CAST_HIGH, V.REG_8, e1) -> 8
| V.Cast(V.CAST_HIGH, V.REG_1, e1) -> 1
| V.Cast(_, _, _) ->
V.bits_of_width (Vine_typecheck.infer_type_fast e)
| V.Lval(V.Mem(_, _, V.REG_8)) -> 8
| V.Lval(V.Mem(_, _, V.REG_16)) -> 16
| V.Lval(V.Mem(_, _, V.REG_32)) -> 32
| V.Lval(V.Mem(_, _, _))
| V.Lval(V.Temp(_)) ->
V.bits_of_width (Vine_typecheck.infer_type_fast e)
| V.BinOp((V.EQ|V.NEQ|V.LT|V.LE|V.SLT|V.SLE), _, _) -> 1
| V.BinOp(V.LSHIFT, e1, V.Constant(V.Int(_, v))) ->
(loop e1) + (Int64.to_int v)
| V.BinOp(_, _, _) ->
V.bits_of_width (Vine_typecheck.infer_type_fast e)
| V.Ite(_, te, fe) -> max (loop te) (loop fe)
| V.UnOp(_)
| V.Let(_, _, _)
| V.Name(_)
| V.FBinOp(_, _, _, _)
| V.FUnOp(_, _, _)
| V.FCast(_, _, _, _) ->
V.bits_of_width (Vine_typecheck.infer_type_fast e)
| V.Constant(V.Str(_)) ->
failwith "Unhandled string in narrow_bitwidth"
| V.Unknown(_) ->
failwith "Unhandled unknown in narrow_bitwidth"
in
FormMan.map_expr_temp form_man e f combine
(* Similar to narrow_bitwidth, but count negative numbers of small
absolute value (i.e. with many leading 1s) as narrow as well. I
can't decide whether this would work better as a single function
with a flag: some of the cases are similar, but others aren't. *)
let narrow_bitwidth_signed form_man e =
let combine wd res = min wd res in
let f loop = function
| V.Constant(V.Int(ty, v)) ->
min (1 + floor_log2 v)
(1 + floor_log2 (Int64.neg (fix_s ty v)))
| V.BinOp(V.BITAND, e1, e2) -> max (loop e1) (loop e2)
| V.BinOp(V.BITOR, e1, e2) -> max (loop e1) (loop e2)
| V.BinOp(V.XOR, e1, e2) -> max (loop e1) (loop e2)
| V.BinOp(V.PLUS, e1, e2) -> 1 + (max (loop e1) (loop e2))
| V.BinOp(V.TIMES, e1, e2) -> (loop e1) + (loop e2)
| V.BinOp(V.MOD, e1, e2) -> min (loop e1) (loop e2)
| V.BinOp(V.SMOD, e1, e2) -> min (loop e1) (loop e2)
| V.Cast((V.CAST_UNSIGNED|V.CAST_SIGNED), V.REG_64, e1)
-> min 64 (loop e1)
| V.Cast((V.CAST_UNSIGNED|V.CAST_LOW|V.CAST_SIGNED), V.REG_32, e1)
-> min 32 (loop e1)
| V.Cast((V.CAST_UNSIGNED|V.CAST_LOW|V.CAST_SIGNED), V.REG_16, e1)
-> min 16 (loop e1)
| V.Cast((V.CAST_UNSIGNED|V.CAST_LOW|V.CAST_SIGNED), V.REG_8, e1)
-> min 8 (loop e1)
| V.Cast((V.CAST_UNSIGNED|V.CAST_LOW|V.CAST_SIGNED), V.REG_1, e1)
-> min 1 (loop e1)
| V.Cast(_, V.REG_32, _) -> 32
| V.Cast(_, V.REG_16, _) -> 16
| V.Cast(_, V.REG_8, _) -> 8
| V.Cast(_, V.REG_1, _) -> 1
| V.Cast(_, _, _) ->
V.bits_of_width (Vine_typecheck.infer_type_fast e)
| V.Lval(V.Mem(_, _, V.REG_8)) -> 8
| V.Lval(V.Mem(_, _, V.REG_16)) -> 16
| V.Lval(V.Mem(_, _, V.REG_32)) -> 32
| V.Lval(V.Mem(_, _, _))
| V.Lval(V.Temp(_)) ->
V.bits_of_width (Vine_typecheck.infer_type_fast e)
| V.BinOp((V.EQ|V.NEQ|V.LT|V.LE|V.SLT|V.SLE), _, _) -> 1
| V.BinOp(V.LSHIFT, e1, V.Constant(V.Int(_, v))) ->
(loop e1) + (Int64.to_int v)
| V.BinOp(_, _, _) ->
V.bits_of_width (Vine_typecheck.infer_type_fast e)
| V.Ite(_, te, fe) -> max (loop te) (loop fe)
| V.UnOp(_)
| V.Let(_, _, _)
| V.Name(_)
| V.FBinOp(_, _, _, _)
| V.FUnOp(_, _, _)
| V.FCast(_, _, _, _) ->
V.bits_of_width (Vine_typecheck.infer_type_fast e)
| V.Constant(V.Str(_)) ->
failwith "Unhandled string in narrow_bitwidth_signed"
| V.Unknown(_) ->
failwith "Unhandled unknown in narrow_bitwidth_signed"
in
FormMan.map_expr_temp form_man e f combine
let narrow_bitwidth_cutoff () =
match (!opt_narrow_bitwidth_cutoff, !opt_arch, (reg_addr ())) with
| ((Some i), _, _) -> i
| (_, ARM, V.REG_32) -> 15 (* ARM often uses lower memory regions *)
| (_, _, V.REG_32) -> 23
| (_, _, V.REG_64) -> 23 (* also experimented with 40,
not clear what's best *)
| (_, _, _) -> 23
let ctz i =
let rec loop = function
| 0L -> 64
| i when Int64.logand i 1L = 1L -> 0
| i when Int64.logand i 0xffffffffL = 0L ->
32 + loop (Int64.shift_right i 32)
| i when Int64.logand i 0xffffL = 0L ->
16 + loop (Int64.shift_right i 16)
| i when Int64.logand i 0xffL = 0L -> 8 + loop (Int64.shift_right i 8)
| i when Int64.logand i 0xfL = 0L -> 4 + loop (Int64.shift_right i 4)
| i when Int64.logand i 3L = 0L -> 2 + loop (Int64.shift_right i 2)
| i when Int64.logand i 1L = 0L -> 1 + loop (Int64.shift_right i 1)
| _ -> failwith "Unexpected else case in ctz"
in
loop i
let bitshift form_man e =
let combine wd res = min wd res in
let f loop e =
match e with
| V.Constant(V.Int(ty, v)) -> ctz v
| V.BinOp(V.BITAND, e1, e2) -> max (loop e1) (loop e2)
| V.BinOp(V.BITOR, e1, e2) -> min (loop e1) (loop e2)
| V.BinOp(V.LSHIFT, e1, V.Constant(V.Int(_, v))) ->
(loop e1) + (Int64.to_int v)
| V.BinOp(V.TIMES, e1, e2) -> (loop e1) + (loop e2)
| V.BinOp(V.PLUS, e1, e2) -> min (loop e1) (loop e2)
| V.Cast(_, V.REG_32, e1) -> min 32 (loop e1)
| V.Cast(_, V.REG_16, e1) -> min 16 (loop e1)
| V.Cast(_, V.REG_8, e1) -> min 8 (loop e1)
| V.Cast(_, V.REG_1, e1) -> min 1 (loop e1)
| V.Ite(_, te, fe) -> min (loop te) (loop fe)
| _ -> 0
in
FormMan.map_expr_temp form_man e f combine
(* OCaml's standard library has this for big ints but not regular ones *)
let rec gcd a b =
match (a, b) with
| (0, b) -> b
| (a, 0) -> a
| (a, b) when a > b -> gcd b (a mod b)
| _ -> gcd a (b mod a)
let stride form_man e =
let combine wd res = res in
let rec f loop e =
match e with
| V.BinOp((V.PLUS|V.MINUS), e1, e2) -> gcd (loop e1) (loop e2)
| V.BinOp(V.TIMES, e1, e2) -> (loop e1) * (loop e2)
| V.BinOp(V.LSHIFT, e1, V.Constant(V.Int(_, v)))
when v < 0x3fffffffL
-> (loop e1) lsl (Int64.to_int v)
| V.Constant(V.Int(_, k))
when k < 0x3fffffffL
-> Int64.to_int k
| e -> 1 lsl (bitshift form_man e)
in
FormMan.map_expr_temp form_man e f combine
let map_n fn n =
let l = ref [] in
for i = n downto 0 do
l := (fn i) :: !l
done;
!l
let split_terms e form_man =
let rec loop e =
match e with
| V.BinOp(V.PLUS, e1, e2) -> (loop e1) @ (loop e2)
(* | V.BinOp(V.BITAND, e, V.Constant(V.Int(ty, v)))
when is_high_mask ty v ->
(* x & 0xfffffff0 = x - (x & 0xf), etc. *)
(loop e) @
(loop
(V.UnOp(V.NEG,
V.BinOp(V.BITAND, e,
V.UnOp(V.NOT, V.Constant(V.Int(ty, v)))))))
| V.BinOp(V.BITOR, e1, e2) ->
let w1 = narrow_bitwidth form_man e1 and
w2 = narrow_bitwidth form_man e2 in
(* Printf.printf "In %s (OR) %s, widths are %d and %d\n" *)
(* (V.exp_to_string e1) (V.exp_to_string e2) w1 w2; *)
if min w1 w2 <= 8 then
(* x | y = x - (x & m) + ((x & m) | y)
where m is a bitmask >= y. *)
let (e_x, e_y, w) =
if w1 < w2 then
(e2, e1, w1)
else
(e1, e2, w2)
in
assert(w >= 0); (* x & 0 should have been optimized away *)
let mask = Int64.pred (Int64.shift_left 1L w) in
let ty_y = Vine_typecheck.infer_type None e_y in
let masked = V.BinOp(V.BITAND, e_x,
V.Constant(V.Int(ty_y, mask))) in
(loop e_x) @
[V.UnOp(V.NEG, masked);
V.BinOp(V.BITOR, masked, e_y)]
else
[e] *)
| V.Lval(V.Temp(var)) ->
FormMan.if_expr_temp form_man var
(fun e' -> loop e') [e] (fun v -> ())
| e -> [e]
in
loop e
type term_kind = | ConstantBase of int64
| ConstantOffset of int64
| ExprOffset of V.exp
| AmbiguousExpr of V.exp
| Symbol of V.exp
let rec classify_term form_man e =
match e with
| V.Constant(V.Int(V.REG_32, off))
when (Int64.abs (fix_s32 off)) < 0x4000L
-> ConstantOffset(off)
| V.Constant(V.Int(V.REG_64, off))
when (Int64.abs off) < 0x4000L
-> ConstantOffset(off)
| V.Constant(V.Int(V.REG_32, off)) when (fix_s32 off) > 0x8000000L
-> ConstantBase(off)
| V.Constant(V.Int(V.REG_32, off))
when off >= 0xc0000000L && off < 0xe1000000L (* Linux kernel *)
-> ConstantBase(off)
| V.Constant(V.Int(V.REG_32, off))
when off >= 0x80800000L && off < 0x88000000L (* ReactOS kernel *)
-> ConstantBase(off)
| V.Constant(V.Int(V.REG_32, off))
when off >= 0x82800000L && off < 0x94000000L (* Windows 7 kernel *)
-> ConstantBase(off)
| V.Constant(V.Int(V.REG_32, off))
when off >= 0xf88f0000L && off < 0xf88fffffL
(* ReactOS kernel stack *)
-> ConstantBase(off)
| V.Constant(V.Int(V.REG_32, off))
when off >= 0x9b200000L && off < 0x9b300000L
(* Windows 7 kernel stack *)
-> ConstantBase(off)
| V.Constant(V.Int(V.REG_32, off))
when off >= 0xff400000L && off < 0xffc00000L
(* Windows 7 kernel something *)
-> ConstantBase(off)
| V.Constant(V.Int(V.REG_32, off))
when off >= 0x7ff00000L && off < 0x80000000L
(* Windows 7 shared user/kernel something *)
-> ConstantBase(off)
| V.Constant(V.Int(V.REG_32, off))
when off >= 0x80000000L && off < 0xffffffffL
(* XXX let Windows 7 wander over the whole top half *)
-> ConstantBase(off)
| V.Constant(V.Int((V.REG_32|V.REG_64), off))
(* XXX -random-memory can produce any value at all *)
-> ConstantBase(off)
| V.UnOp(V.NEG, _) -> ExprOffset(e)
| V.BinOp(V.LSHIFT, _, V.Constant(V.Int(V.REG_8, (1L|2L|3L|4L|5L))))
-> ExprOffset(e)
| V.BinOp(V.TIMES, _, _)
-> ExprOffset(e)
| e when (narrow_bitwidth form_man e)
< (narrow_bitwidth_cutoff ())
-> ExprOffset(e)
| e when (narrow_bitwidth_signed form_man e)
< (narrow_bitwidth_cutoff ())
-> ExprOffset(e)
| V.BinOp(V.ARSHIFT, _, _)
-> ExprOffset(e)
| V.BinOp(V.RSHIFT, _, _)
-> ExprOffset(e)
| V.BinOp(V.LSHIFT, _, _)
-> ExprOffset(e)
| V.BinOp(V.BITOR,
V.BinOp(V.BITAND, V.Cast(V.CAST_SIGNED, _, _), x),
V.BinOp(V.BITAND, V.UnOp(V.NOT, V.Cast(V.CAST_SIGNED, _, _)),
y))
| V.BinOp(V.BITOR,
V.BinOp(V.BITAND, x, V.Cast(V.CAST_SIGNED, _, _)),
V.BinOp(V.BITAND, V.UnOp(V.NOT, V.Cast(V.CAST_SIGNED, _, _)),
y))
| V.BinOp(V.BITOR,
V.BinOp(V.BITAND, V.Cast(V.CAST_SIGNED, _, _), x),
V.BinOp(V.BITAND, y,
V.UnOp(V.NOT, V.Cast(V.CAST_SIGNED, _, _))))
| V.BinOp(V.BITOR,
V.BinOp(V.BITAND, x, V.Cast(V.CAST_SIGNED, _, _)),
V.BinOp(V.BITAND, y,
V.UnOp(V.NOT, V.Cast(V.CAST_SIGNED, _, _))))
| V.Ite(_, x, y)
->
(* ITE expression "_ ? x : y" *)
(match (classify_term form_man x), (classify_term form_man y) with
| (ExprOffset(_)|ConstantOffset(_)),
(ExprOffset(_)|ConstantOffset(_)) ->
ExprOffset(e)
| _ -> AmbiguousExpr(e)
)
(* Similar pattern where we don't have the sign extend, but
we do have something and its negation *)
| V.BinOp(V.BITOR,
V.BinOp(V.BITAND, c1, x),
V.BinOp(V.BITAND, V.UnOp(V.NOT, c2), y))
when c1 = c2
->
(* ITE expression "_ ? x : y" *)
(match (classify_term form_man x), (classify_term form_man y) with
| (ExprOffset(_)|ConstantOffset(_)),
(ExprOffset(_)|ConstantOffset(_)) ->
ExprOffset(e)
| _ -> AmbiguousExpr(e)
)
(* Occurs as an optimization of bitwise ITE: *)
| V.BinOp(V.BITAND, x, V.UnOp(V.NOT, V.Cast(V.CAST_SIGNED, _, _)))
| V.BinOp(V.BITAND, V.UnOp(V.NOT, V.Cast(V.CAST_SIGNED, _, _)), x)
| V.BinOp(V.BITAND, x, V.Cast(V.CAST_SIGNED, _, _))
| V.BinOp(V.BITAND, V.Cast(V.CAST_SIGNED, _, _), x) ->
(match (classify_term form_man x) with
| (ExprOffset(_)|ConstantOffset(_)) -> ExprOffset(e)
| _ -> AmbiguousExpr(e)
)
(* AND with negation of a small value used for rounding *)
| V.BinOp(V.BITAND, x, V.Constant(V.Int(V.REG_32, off)))
when (fix_u32 off) >= 0xffffff00L
->
(classify_term form_man x)
| V.BinOp(V.BITAND, x, V.Constant(V.Int(V.REG_64, off)))
when off >= 0xffffffffffffff00L
->
(classify_term form_man x)
(* Addition inside another operation (top-level addition should
be handled by split_terms) *)
| V.BinOp(V.PLUS, e1, e2)
->
(match (classify_term form_man e1), (classify_term form_man e2) with
| (ExprOffset(_)|ConstantOffset(_)),
(ExprOffset(_)|ConstantOffset(_)) -> ExprOffset(e)
| _,_ -> AmbiguousExpr(e))
(* | V.BinOp(V.BITAND, _, _) *)
(* | V.BinOp(V.BITOR, _, _) (* XXX happens in Windows 7, don't know why *) *)
(* -> ExprOffset(e) *)
| V.Cast(V.CAST_SIGNED, _, _) -> ExprOffset(e)
| V.Lval(_) -> Symbol(e)
| _ -> if (!opt_fail_offset_heuristic) then (
failwith ("Strange term "^(V.exp_to_string e)^" in address")
) else ExprOffset(e)
(* When we're not going to try for symbolic regions, just separate
the concrete terms from everything else; they should be the base *)
let classify_terms_simple e form_man =
let constants = ref 0L in
let rec loop e =
match e with
| V.BinOp(V.PLUS, e1, e2) -> (loop e1) @ (loop e2)
| V.Lval(V.Temp(var)) ->
FormMan.if_expr_temp form_man var
(fun e' -> loop e') [e] (fun v -> ())
| V.Constant(V.Int((V.REG_32|V.REG_64), n)) ->
constants := Int64.add !constants n;
[]
| e -> [e]
in
let terms = loop e in
(!constants, terms)
let classify_terms e form_man =
match (e, !opt_no_sym_regions) with
| (V.Constant(V.Int(_, k)), _) ->
(* Most common case: all concrete is a concrete base *)
([k], [], [], [], [])
| (e, true) ->
let (cbase, terms) = classify_terms_simple e form_man in
let cbases = if cbase = 0L then [] else [cbase] in
if !opt_trace_sym_addr_details then
Printf.printf "Extracted base address 0x%0Lx from %s\n"
cbase (V.exp_to_string e);
(cbases, [], terms, [], [])
| (_, _) ->
if !opt_trace_sym_addr_details then
Printf.printf "Analyzing addr expr %s\n" (V.exp_to_string e);
let l = List.map (classify_term form_man) (split_terms e form_man) in
let (cbases, coffs, eoffs, ambig, syms) =
(ref [], ref [], ref [], ref [], ref []) in
List.iter
(function
| ConstantBase(o) -> cbases := o :: !cbases
| ConstantOffset(o) -> coffs := o :: !coffs
| ExprOffset(e) -> eoffs := e :: !eoffs
| AmbiguousExpr(e) -> ambig := e :: !ambig
| Symbol(v) -> syms := v :: !syms)
l;
(!cbases, !coffs, !eoffs, !ambig, !syms)
let select_one l rand_func =
let split_list l =
let a = Array.of_list l in
let len = Array.length a in
let k = len / 2 in
((Array.to_list (Array.sub a 0 k)),
(Array.to_list (Array.sub a k (len - k))))
in
let rec loop l =
match l with
| [] -> failwith "Empty list in select_one"
| [a] -> (a, [])
| [a; b] -> if rand_func () then (a, [b]) else (b, [a])
| l -> let (h1, h2) = split_list l in
if rand_func () then
let (e, h1r) = loop h1 in
(e, h1r @ h2)
else
let (e, h2r) = loop h2 in
(e, h1 @ h2r)
in
loop l
let sum_list l =
match l with
| [] -> addr_const 0L
| [a] -> a
| e :: r -> List.fold_left (fun a b -> V.BinOp(V.PLUS, a, b))
e r
class sym_region_frag_machine (dt:decision_tree) = object(self)
inherit SPFM.sym_path_frag_machine dt as spfm
val mutable regions = []
val region_vals = Hashtbl.create 101
val mutable location_id = 0L
method set_eip i =
location_id <- i;
spfm#set_eip i
val sink_mem = new GM.granular_sink_memory
method private region r =
match r with
| None -> (sink_mem :> (GM.granular_memory))
| Some 0 -> (mem :> (GM.granular_memory))
| Some r_num -> List.nth regions (r_num - 1)
method private fresh_region =
let new_idx = 1 + List.length regions in
let region = (new GM.granular_hash_memory) and
name = "region_" ^ (string_of_int new_idx) in
regions <- regions @ [region];
if !opt_zero_memory then
spfm#on_missing_zero_m region
else
spfm#on_missing_symbol_m region name;
new_idx
method private region_for e =
try
Hashtbl.find region_vals e
with Not_found ->
let new_region = self#fresh_region in
Hashtbl.replace region_vals e new_region;
if !opt_trace_regions then
Printf.printf "Address %s is region %d\n"
(V.exp_to_string e) new_region;
new_region
method private is_region_base e =
Hashtbl.mem region_vals e
val mutable sink_regions = []
method private add_sink_region (e:Vine.exp) (size:int64) =
self#on_missing_symbol_m sink_mem "sink";
sink_regions <- ((self#region_for e), size) :: sink_regions
method private choose_conc_offset_uniform ty e ident =
let byte x = V.Constant(V.Int(V.REG_8, (Int64.of_int x))) in
let bits = ref 0L in
self#restore_path_cond
(fun () ->
if ty = V.REG_1 then
(* This special case avoids shifting REG_1s, which appears
to be legal in Vine IR but tickles bugs in multiple of
our solver backends. *)
let bit = self#extend_pc_random e false ident in
bits := (if bit then 1L else 0L)
else
for b = (V.bits_of_width ty) - 1 downto 0 do
let bit = self#extend_pc_random
(V.Cast(V.CAST_LOW, V.REG_1,
(V.BinOp(V.ARSHIFT, e,
(byte b))))) false (ident + b)
in
bits := (Int64.logor (Int64.shift_left !bits 1)
(if bit then 1L else 0L));
done);
!bits
method private choose_conc_offset_biased ty e ident =
let const x = V.Constant(V.Int(ty, x)) in
let rec try_list l =
match l with
| [] -> self#choose_conc_offset_uniform ty e ident
| v :: r ->
if self#extend_pc_random (V.BinOp(V.EQ, e, (const v))) false
(ident + 0x80 + (Int64.to_int v)) then
v
else
try_list r
in
let bits = ref 0L in
self#restore_path_cond
(fun () ->
bits := try_list
[0L; 1L; 2L; 4L; 8L; 16L; 32L; 64L; -1L; -2L; -4L; -8L]);
!bits
val mutable concrete_cache = Hashtbl.create 101
method private choose_conc_offset_cached ty e_orig ident =
let const x = V.Constant(V.Int(ty, x)) in
let e = form_man#tempify_exp e_orig ty in
let (bits, verb) =
if Hashtbl.mem concrete_cache e then
(Hashtbl.find concrete_cache e, "Reused")
else
(let bits =
(* match self#query_unique_value e ty with
| Some v -> v
| None -> *)
match !opt_offset_strategy with
| UniformStrat -> self#choose_conc_offset_uniform ty e
ident
| BiasedSmallStrat -> self#choose_conc_offset_biased ty e
ident
in
Hashtbl.replace concrete_cache e bits;
(bits, "Picked")) in
if !opt_trace_sym_addrs then
Printf.printf "%s concrete value 0x%Lx for %s\n"
verb bits (V.exp_to_string e);
self#add_to_path_cond (V.BinOp(V.EQ, e, (const bits)));
bits
method private concretize_inner ty e ident =
match e with
| V.Cast((V.CAST_UNSIGNED|V.CAST_SIGNED) as ckind, cty, e2) ->
if cty <> ty then
Printf.printf "Cast type is not %s in concretize_inner of %s\n"
(V.type_to_string ty) (V.exp_to_string e);
assert(cty = ty);
let ty2 = Vine_typecheck.infer_type None e2 in
let bits = self#choose_conc_offset_cached ty2 e2 ident in
let expand =
match (ckind, ty2) with
| (V.CAST_UNSIGNED, V.REG_32) -> fix_u32
| (V.CAST_UNSIGNED, V.REG_16) -> fix_u16
| (V.CAST_UNSIGNED, V.REG_8) -> fix_u8
| (V.CAST_UNSIGNED, V.REG_1) -> fix_u1
| (V.CAST_SIGNED, V.REG_32) -> fix_s32
| (V.CAST_SIGNED, V.REG_16) -> fix_s16
| (V.CAST_SIGNED, V.REG_8) -> fix_s8
| (V.CAST_SIGNED, V.REG_1) -> fix_s1
| _ -> failwith "unhandled cast kind in concretize_inner"
in
expand bits
| _ -> self#choose_conc_offset_cached ty e ident
method private concretize ty e ident =
dt#start_new_query;
let v = self#concretize_inner ty e ident in
dt#count_query;
v
val mutable sink_read_count = 0L
method private check_cond cond_e ident =
dt#start_new_query_binary;
let choices = ref None in
self#restore_path_cond
(fun () ->
let b = self#extend_pc_random cond_e false ident in
choices := dt#check_last_choices;
dt#count_query;
ignore(b));
!choices
method private region_expr e ident decide_fn =
if !opt_check_for_null then
(match
self#check_cond (V.BinOp(V.EQ, e, addr_const 0L))
(0x3100 + self#get_stmt_num)
with
| Some true -> Printf.printf "Can be null.\n"
| Some false -> Printf.printf "Cannot be null.\n"
| None -> Printf.printf "Can be null or non-null\n";
infl_man#maybe_measure_influence_deref e);
(* This start_new_query is needed because the selection of a
base address with random_case_split and the concretization of
offsets may create decision tree nodes. It should match with a
call to dt#count_query at every return from this method. *)
dt#start_new_query;
let (cbases, coffs, eoffs, ambig, syms) = classify_terms e form_man in
let eoffs = List.map simplify_fp eoffs in
if !opt_trace_sym_addr_details then
(Printf.printf "Concrete base terms: %s\n"
(String.concat " "
(List.map (Printf.sprintf "0x%08Lx") cbases));
Printf.printf "Concrete offset terms: %s\n"
(String.concat " "
(List.map (Printf.sprintf "0x%08Lx") coffs));
Printf.printf "Offset expression terms: %s\n"
(String.concat " "
(List.map V.exp_to_string eoffs));
Printf.printf "Ambiguous expression terms: %s\n"
(String.concat " "
(List.map V.exp_to_string ambig));
Printf.printf "Ambiguous symbol terms: %s\n"
(String.concat " "
(List.map V.exp_to_string syms)));
let cbase = List.fold_left Int64.add 0L cbases in
let (base, base_e, off_syms) = match (cbase, syms, ambig) with
| (0L, [], []) -> raise NullDereference
(* The following two cases are applicable when applying
table treatment for symbolic regions *)
| (0L, [], [e]) -> (Some(self#region_for e), Some e, [])
| (0L, [v], _) -> (Some(self#region_for v), Some v, ambig)
| (0L, [], el) -> (Some 0, None, el)
| (0L, vl, _) ->
let (bvar, rest_vars) =
(* We used to have logic here that checked whether one
of the symbols was known to have already been used as
a region base, and if so selected it. But the set of
region base variables expands during a run, so this
lead to decision tree inconsistencies. If we wanted
this heuristic, we need to do something more
complicated like always choose from among the same
set, but with preferences based on seen regions. For
now, omit that logic and always choose randomly from
among all the possibilties. *)
let split_count = ref (-1) in
select_one vl
(fun () ->
split_count := !split_count + 1;
self#random_case_split !opt_trace_decisions
(!split_count + 0x100 + ident))
in
if !opt_trace_sym_addrs then
Printf.printf "Choosing %s as the base address\n"
(V.exp_to_string bvar);
(Some(self#region_for bvar), Some bvar, rest_vars @ ambig)
| (off, vl, _) ->
(Some 0, None, vl @ ambig)
in
let cloc = Int64.add cbase (List.fold_left Int64.add 0L coffs) in
(* return a SingleLocation(region, offset)
or a TableLocation(region, off_expr, cloc) *)
match base with
| Some r
when List.exists (fun (r', _) -> r = r') sink_regions ->
let (r', size) =
List.find (fun (r', _) -> r = r') sink_regions in
Printf.printf "Ignoring access to sink region\n";
(let sat_dir = ref false in
self#restore_path_cond
(fun () ->
sat_dir := self#extend_pc_random
(V.BinOp(V.LT, e, addr_const size))
false (ident + 0x600));
if !sat_dir = true then
Printf.printf "Can be in bounds.\n"
else
Printf.printf "Can be out of bounds.\n");
sink_read_count <- Int64.add sink_read_count 0x10L;
dt#count_query;
SingleLocation(None, sink_read_count)
| _ ->
let off_expr = (sum_list (eoffs @ off_syms)) in
match decide_fn off_expr 0L with
| Some wd ->
let base_str = match (base, base_e) with
| (Some r, Some e) ->
Printf.sprintf "sym region %d with base = %s" r
(V.exp_to_string e)
| _ -> "concrete base"
in
if !opt_trace_tables then
Printf.printf
"Table treatment for %s and offset expr = %s\n"
base_str (V.exp_to_string off_expr);
dt#count_query;
TableLocation(base, off_expr, cloc)
| None ->
let coff = List.fold_left Int64.add 0L coffs in
let offset = Int64.add (Int64.add cbase coff)
(match (eoffs, off_syms) with
| ([], []) -> 0L
| (el, vel) ->
(self#concretize_inner (reg_addr())
(simplify_fp (sum_list (el @ vel))))
(ident + 0x200)) in
dt#count_query;
SingleLocation(base, (fix_u32 offset))
method private eval_addr_exp_region_conc_path e ident =
let term_is_known_base = function
| V.Lval(V.Temp(var)) -> form_man#known_region_base var
| _ -> false
in
let terms = split_terms e form_man in
let (known_bases, rest) =
List.partition term_is_known_base terms in
match known_bases with
| [] ->
let a = form_man#eval_expr e in
if !opt_trace_sym_addrs then
Printf.printf "Computed concrete value 0x%08Lx\n" a;
if !opt_solve_path_conditions then
(let cond = V.BinOp(V.EQ, e, addr_const a)
in
let sat = self#extend_pc_known cond false ident true in
assert(sat));
(Some 0, a)
| [V.Lval(V.Temp(var)) as vexp] ->
let sum = sum_list rest in
let a = form_man#eval_expr sum in
let a_const = addr_const a in
if !opt_trace_sym_addrs then
Printf.printf
"Computed concrete offset %s + 0x%08Lx\n"
(V.var_to_string var) a;
if !opt_solve_path_conditions &&
(sum <> a_const)
then
(let cond = V.BinOp(V.EQ, sum, a_const) in
let sat = self#extend_pc_known cond false
(ident + 0x400) true
in
assert(sat));
(Some(self#region_for vexp), a)
| [_] -> failwith "known_base invariant failure"
| _ -> failwith "multiple bases"
method private eval_addr_exp_region exp ident decide_fn =
let (to_concrete, to_symbolic) = match !opt_arch with
| (X86|ARM) -> (D.to_concrete_32, D.to_symbolic_32)
| X64 -> (D.to_concrete_64, D.to_symbolic_64)
in
let v = self#eval_int_exp_simplify exp in
try
SingleLocation(Some 0, to_concrete v)
with NotConcrete _ ->
let e = to_symbolic v in
let eip = self#get_eip in
if !opt_trace_sym_addrs then
Printf.printf "Symbolic address %s @ (0x%Lx)\n"
(V.exp_to_string e) eip;
if !opt_concrete_path then
let (r, addr) = self#eval_addr_exp_region_conc_path e ident in
SingleLocation(r, addr)
else
self#region_expr e ident decide_fn
(* Because we override handle_{load,store}, this should only be
called for jumps. *)
method eval_addr_exp exp =
match (self#eval_addr_exp_region exp 0xa000 (fun _ _ -> None)) with
| SingleLocation(r, addr) ->
(match r with
| Some 0 -> addr
| Some r_num ->
if !opt_trace_stopping then
(Printf.printf "Unsupported jump into symbolic region %d\n"
r_num;
if !opt_trace_end_jump = (Some self#get_eip) then
let e = D.to_symbolic_32 (self#eval_int_exp_simplify exp) in
let (cbases, coffs, eoffs, ambig, syms) =
classify_terms e form_man in
if cbases = [] && coffs = [] && eoffs = [] &&
ambig = [] && syms != [] then
Printf.printf "Completely symbolic load\n");
raise SymbolicJump
| None ->
if !opt_trace_stopping then
Printf.printf "Unsupported jump into sink region\n";
raise SymbolicJump)
| TableLocation(r, off_expr, cloc) ->
failwith "no table support for jumps, panic!"
method private register_num reg =
match reg with
| R_RAX | R_EAX | R0 -> 0
| R_RBX | R_EBX | R1 -> 1
| R_RCX | R_ECX | R2 -> 2
| R_RDX | R_EDX | R3 -> 3
| R_RSI | R_ESI | R4 -> 4
| R_RDI | R_EDI | R5 -> 5
| R_RBP | R_EBP | R6 -> 6
| R_RSP | R_ESP | R7 -> 7
| R_R8 | R8 -> 8
| R_R9 | R9 -> 9
| R_R10 | R10 -> 10
| R_R11 | R11 -> 11
| R_R12 | R12 -> 12
| R_R13 | R13 -> 13
| R_R14 | R14 -> 14
| R_R15 | R15 -> 15
| _ -> 15
method get_word_var_concretize reg do_influence name : int64 =
let v = self#get_int_var (Hashtbl.find reg_to_var reg) in
try (D.to_concrete_32 v)
with NotConcrete _ ->
let e = D.to_symbolic_32 v in
if do_influence then
(Printf.printf "Measuring symbolic %s influence..." name;
infl_man#measure_point_influence name e);
self#concretize V.REG_32 e (0x5000 + 0x100 * (self#register_num reg))
method get_long_var_concretize reg do_influence name : int64 =
let v = self#get_int_var (Hashtbl.find reg_to_var reg) in
try (D.to_concrete_64 v)
with NotConcrete _ ->
let e = D.to_symbolic_64 v in
if do_influence then
(Printf.printf "Measuring symbolic %s influence..." name;
infl_man#measure_point_influence name e);
self#concretize V.REG_64 e (0x5000 + 0x100 * (self#register_num reg))
method load_long_concretize addr do_influence name =
let v = self#load_long addr in
try (D.to_concrete_64 v)
with NotConcrete _ ->
let e = D.to_symbolic_64 v in
if do_influence then
(Printf.printf "Measuring symbolic %s influence..." name;
infl_man#measure_point_influence name e);
self#concretize V.REG_64 e 0x6800
method load_word_concretize addr do_influence name =
let v = self#load_word addr in
try (D.to_concrete_32 v)
with NotConcrete _ ->
let e = D.to_symbolic_32 v in
if do_influence then
(Printf.printf "Measuring symbolic %s influence..." name;
infl_man#measure_point_influence name e);
self#concretize V.REG_32 e 0x6400
method load_short_concretize addr do_influence name =
let v = self#load_short addr in
try (D.to_concrete_16 v)
with NotConcrete _ ->
let e = D.to_symbolic_16 v in
if do_influence then
(Printf.printf "Measuring symbolic %s influence..." name;
infl_man#measure_point_influence name e);
Int64.to_int (self#concretize V.REG_16 e 0x6200)
method load_byte_concretize addr do_influence name =
let v = self#load_byte addr in
try (D.to_concrete_8 v)
with NotConcrete _ ->
let e = D.to_symbolic_8 v in
if do_influence then
(Printf.printf "Measuring symbolic %s influence..." name;
infl_man#measure_point_influence name e);
Int64.to_int (self#concretize V.REG_8 e 0x6100)
method private maybe_concretize_binop op v1 v2 ty1 ty2 =
let conc t v =
match t with
| V.REG_1 ->
(try ignore(D.to_concrete_1 v); v
with NotConcrete _ ->
(D.from_concrete_1
(Int64.to_int
(self#concretize t (D.to_symbolic_1 v) 0x6b00))))
| V.REG_8 ->
(try ignore(D.to_concrete_8 v); v
with NotConcrete _ ->
(D.from_concrete_8
(Int64.to_int
(self#concretize t (D.to_symbolic_8 v) 0x6b00))))
| V.REG_16 ->
(try ignore(D.to_concrete_16 v); v
with NotConcrete _ ->
(D.from_concrete_16
(Int64.to_int
(self#concretize t (D.to_symbolic_16 v) 0x6b00))))
| V.REG_32 ->
(try ignore(D.to_concrete_32 v); v
with NotConcrete _ ->
(D.from_concrete_32
(self#concretize t (D.to_symbolic_32 v) 0x6b00)))
| V.REG_64 ->
(try ignore(D.to_concrete_64 v); v
with NotConcrete _ ->
(D.from_concrete_64
(self#concretize t (D.to_symbolic_64 v) 0x6b00)))
| _ -> failwith "Bad type in maybe_concretize_binop"
in
match op with
| V.DIVIDE | V.SDIVIDE | V.MOD | V.SMOD
when !opt_concretize_divisors
-> (v1, (conc ty2 v2))
| _ -> (v1, v2)
method private store_byte_region r addr b =
(self#region r)#store_byte addr b
method private store_short_region r addr s =
(self#region r)#store_short addr s