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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
|
# Copyright (c) 2019-2026 Vector 35 Inc
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to
# deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
# sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.
import ctypes
import struct
from typing import Optional, Generator, List, Union, NewType, Tuple, ClassVar, Mapping, Set, Callable, Any, Iterator, overload
from dataclasses import dataclass
from enum import Enum
# Binary Ninja components
from . import _binaryninjacore as core
from .enums import (
HighLevelILOperation, DataFlowQueryOption, FunctionGraphType, ILInstructionAttribute, StringType,
DisassemblyOption
)
from . import function
from . import binaryview
from . import architecture
from . import lowlevelil
from . import mediumlevelil
from . import basicblock
from . import types
from . import highlight
from . import flowgraph
from . import variable
from . import databuffer
from . import stringrecognizer
from . import types as _types
from .interaction import show_graph_report
from .commonil import (
BaseILInstruction, Tailcall, Syscall, Localcall, Comparison, Signed, UnaryOperation, BinaryOperation, SSA, Phi,
Loop, ControlFlow, Memory, Constant, Arithmetic, DoublePrecision, Terminal, FloatingPoint, Intrinsic, Return,
VariableInstruction, SSAVariableInstruction, SetVar, ILSourceLocation
)
from . import deprecation
TokenList = List['function.InstructionTextToken']
LinesType = Generator['function.DisassemblyTextLine', None, None]
ExpressionIndex = NewType('ExpressionIndex', int)
InstructionIndex = NewType('InstructionIndex', int)
Index = Union[ExpressionIndex, InstructionIndex]
InstructionOrExpression = Union['HighLevelILInstruction', Index]
HLILInstructionsType = Generator['HighLevelILInstruction', None, None]
HLILBasicBlocksType = Generator['HighLevelILBasicBlock', None, None]
OperandsType = Tuple[ExpressionIndex, ExpressionIndex, ExpressionIndex, ExpressionIndex, ExpressionIndex]
HighLevelILOperandType = Union['HighLevelILInstruction', 'lowlevelil.ILIntrinsic', 'variable.Variable',
'mediumlevelil.SSAVariable', List[int], List['variable.Variable'],
List['mediumlevelil.SSAVariable'], List['HighLevelILInstruction'], Optional[int], float,
'GotoLabel', variable.ConstantData, databuffer.DataBuffer]
VariablesList = List[Union['mediumlevelil.SSAVariable', 'variable.Variable']]
StringOrType = Union[str, '_types.Type', '_types.TypeBuilder']
ILInstructionAttributeSet = Union[Set[ILInstructionAttribute], List[ILInstructionAttribute]]
HighLevelILVisitorCallback = Callable[[str, HighLevelILOperandType, str, Optional['HighLevelILInstruction']], bool]
class VariableReferenceType(Enum):
Read = 0
Written = 1
AddressTaken = 2
@dataclass(frozen=True)
class HighLevelILOperationAndSize:
operation: HighLevelILOperation
size: int
def __repr__(self):
if self.size == 0:
return f"<HighLevelILOperationAndSize: {self.operation.name}>"
return f"<HighLevelILOperationAndSize: {self.operation.name} {self.size}>"
@dataclass
class GotoLabel:
function: 'HighLevelILFunction'
id: int
def __repr__(self):
return f"<GotoLabel: {self.name}>"
def __str__(self):
return self.name
@property
def label_id(self) -> int:
return self.id
@property
def name(self) -> str:
assert self.function.source_function is not None, "Cant get name of function without source_function"
return core.BNGetGotoLabelName(self.function.source_function.handle, self.id)
@name.setter
def name(self, value: str) -> None:
assert self.function.source_function is not None, "Cant set name of function without source_function"
core.BNSetUserGotoLabelName(self.function.source_function.handle, self.id, value)
@property
def definition(self) -> Optional['HighLevelILInstruction']:
return self.function.get_label(self.id)
@property
def uses(self) -> List['HighLevelILInstruction']:
return self.function.get_label_uses(self.id)
@dataclass(frozen=True, order=True)
class CoreHighLevelILInstruction:
operation: HighLevelILOperation
attributes: int
source_operand: int
size: int
operands: OperandsType
address: int
parent: ExpressionIndex
@classmethod
def from_BNHighLevelILInstruction(cls, instr: core.BNHighLevelILInstruction) -> 'CoreHighLevelILInstruction':
operands: OperandsType = tuple([ExpressionIndex(instr.operands[i]) for i in range(5)]) # type: ignore
return cls(
HighLevelILOperation(instr.operation), instr.attributes, instr.sourceOperand, instr.size, operands, instr.address,
instr.parent
)
@dataclass(frozen=True)
class HighLevelILInstruction(BaseILInstruction):
"""
``class HighLevelILInstruction`` High Level Intermediate Language Instructions form an abstract syntax tree of
the code. Control flow structures are present as high level constructs in the HLIL tree.
"""
function: 'HighLevelILFunction'
expr_index: ExpressionIndex
core_instr: CoreHighLevelILInstruction
as_ast: bool
instr_index: InstructionIndex
# ILOperations is deprecated and will be removed in a future version once BNIL Graph no longer uses it
# Use the visit methods visit, visit_all, and visit_operands
ILOperations: ClassVar[Mapping[HighLevelILOperation, List[Tuple[str, str]]]] = {
HighLevelILOperation.HLIL_NOP: [], HighLevelILOperation.HLIL_BLOCK: [("body", "expr_list")],
HighLevelILOperation.HLIL_IF: [("condition", "expr"), ("true", "expr"),
("false", "expr")], HighLevelILOperation.HLIL_WHILE: [("condition", "expr"),
("body", "expr")],
HighLevelILOperation.HLIL_WHILE_SSA: [("condition_phi", "expr"), ("condition", "expr"),
("body", "expr")], HighLevelILOperation.HLIL_DO_WHILE: [
("body", "expr"), ("condition", "expr")
], HighLevelILOperation.HLIL_DO_WHILE_SSA: [("body", "expr"),
("condition_phi", "expr"),
("condition", "expr")],
HighLevelILOperation.HLIL_FOR: [("init", "expr"), ("condition", "expr"), ("update", "expr"),
("body", "expr")], HighLevelILOperation.HLIL_FOR_SSA: [
("init", "expr"), ("condition_phi", "expr"), ("condition", "expr"),
("update", "expr"), ("body", "expr")
], HighLevelILOperation.HLIL_SWITCH: [
("condition", "expr"), ("default", "expr"), ("cases", "expr_list")
], HighLevelILOperation.HLIL_CASE: [("values", "expr_list"), ("body", "expr")],
HighLevelILOperation.HLIL_BREAK: [], HighLevelILOperation.HLIL_CONTINUE: [], HighLevelILOperation.HLIL_JUMP: [
("dest", "expr")
], HighLevelILOperation.HLIL_RET: [("src", "expr_list")], HighLevelILOperation.HLIL_NORET: [],
HighLevelILOperation.HLIL_UNREACHABLE: [],
HighLevelILOperation.HLIL_GOTO: [("target", "label")], HighLevelILOperation.HLIL_LABEL: [
("target", "label")
], HighLevelILOperation.HLIL_VAR_DECLARE: [("var", "var")], HighLevelILOperation.HLIL_VAR_INIT: [
("dest", "var"), ("src", "expr")
], HighLevelILOperation.HLIL_VAR_INIT_SSA: [
("dest", "var_ssa"), ("src", "expr")
], HighLevelILOperation.HLIL_ASSIGN: [("dest", "expr"),
("src", "expr")], HighLevelILOperation.HLIL_ASSIGN_UNPACK: [
("dest", "expr_list"), ("src", "expr")
], HighLevelILOperation.HLIL_ASSIGN_MEM_SSA: [("dest", "expr"),
("dest_memory", "int"),
("src", "expr"),
("src_memory", "int")],
HighLevelILOperation.HLIL_ASSIGN_UNPACK_MEM_SSA: [
("dest", "expr_list"), ("dest_memory", "int"), ("src", "expr"), ("src_memory", "int")
], HighLevelILOperation.HLIL_VAR: [("var", "var")], HighLevelILOperation.HLIL_VAR_SSA: [
("var", "var_ssa")
], HighLevelILOperation.HLIL_VAR_PHI: [("dest", "var_ssa"),
("src", "var_ssa_list")], HighLevelILOperation.HLIL_MEM_PHI: [
("dest", "int"), ("src", "int_list")
], HighLevelILOperation.HLIL_STRUCT_FIELD: [
("src", "expr"), ("offset", "int"), ("member_index", "member_index")
], HighLevelILOperation.HLIL_ARRAY_INDEX: [
("src", "expr"), ("index", "expr")
], HighLevelILOperation.HLIL_ARRAY_INDEX_SSA: [("src", "expr"),
("src_memory", "int"),
("index", "expr")],
HighLevelILOperation.HLIL_SPLIT: [("high", "expr"), ("low", "expr")], HighLevelILOperation.HLIL_DEREF: [
("src", "expr")
], HighLevelILOperation.HLIL_DEREF_FIELD: [
("src", "expr"), ("offset", "int"), ("member_index", "member_index")
], HighLevelILOperation.HLIL_DEREF_SSA: [
("src", "expr"), ("src_memory", "int")
], HighLevelILOperation.HLIL_DEREF_FIELD_SSA: [
("src", "expr"), ("src_memory", "int"), ("offset", "int"),
("member_index", "member_index")
], HighLevelILOperation.HLIL_ADDRESS_OF: [("src", "expr")], HighLevelILOperation.HLIL_PASS_BY_REF: [
("src", "expr")
], HighLevelILOperation.HLIL_RETURN_BY_REF: [
("src", "expr")
], HighLevelILOperation.HLIL_CONST: [
("constant", "int")
], HighLevelILOperation.HLIL_CONST_PTR: [("constant", "int")], HighLevelILOperation.HLIL_EXTERN_PTR: [
("constant", "int"), ("offset", "int")
], HighLevelILOperation.HLIL_FLOAT_CONST: [("constant", "float")], HighLevelILOperation.HLIL_IMPORT: [
("constant", "int")
], HighLevelILOperation.HLIL_CONST_DATA: [("constant", "ConstantData")], HighLevelILOperation.HLIL_CONST_DATA: [
("constant", "ConstantData")
], HighLevelILOperation.HLIL_ADD: [("left", "expr"), ("right", "expr")], HighLevelILOperation.HLIL_ADC: [
("left", "expr"), ("right", "expr"), ("carry", "expr")
], HighLevelILOperation.HLIL_SUB: [("left", "expr"), ("right", "expr")], HighLevelILOperation.HLIL_SBB: [
("left", "expr"), ("right", "expr"), ("carry", "expr")
], HighLevelILOperation.HLIL_AND: [("left", "expr"), ("right", "expr")], HighLevelILOperation.HLIL_OR: [
("left", "expr"), ("right", "expr")
], HighLevelILOperation.HLIL_XOR: [("left", "expr"), ("right", "expr")], HighLevelILOperation.HLIL_LSL: [
("left", "expr"), ("right", "expr")
], HighLevelILOperation.HLIL_LSR: [("left", "expr"), ("right", "expr")], HighLevelILOperation.HLIL_ASR: [
("left", "expr"), ("right", "expr")
], HighLevelILOperation.HLIL_ROL: [("left", "expr"), ("right", "expr")], HighLevelILOperation.HLIL_RLC: [
("left", "expr"), ("right", "expr"), ("carry", "expr")
], HighLevelILOperation.HLIL_ROR: [("left", "expr"), ("right", "expr")], HighLevelILOperation.HLIL_RRC: [
("left", "expr"), ("right", "expr"), ("carry", "expr")
], HighLevelILOperation.HLIL_MUL: [("left", "expr"), ("right", "expr")], HighLevelILOperation.HLIL_MULU_DP: [
("left", "expr"), ("right", "expr")
], HighLevelILOperation.HLIL_MULS_DP: [("left", "expr"), ("right", "expr")], HighLevelILOperation.HLIL_DIVU: [
("left", "expr"), ("right", "expr")
], HighLevelILOperation.HLIL_DIVU_DP: [("left", "expr"), ("right", "expr")], HighLevelILOperation.HLIL_DIVS: [
("left", "expr"), ("right", "expr")
], HighLevelILOperation.HLIL_DIVS_DP: [("left", "expr"), ("right", "expr")], HighLevelILOperation.HLIL_MODU: [
("left", "expr"), ("right", "expr")
], HighLevelILOperation.HLIL_MODU_DP: [("left", "expr"), ("right", "expr")], HighLevelILOperation.HLIL_MODS: [
("left", "expr"), ("right", "expr")
], HighLevelILOperation.HLIL_MODS_DP: [("left", "expr"), ("right", "expr")], HighLevelILOperation.HLIL_NEG: [
("src", "expr")
], HighLevelILOperation.HLIL_NOT: [("src", "expr")], HighLevelILOperation.HLIL_BSWAP: [
("src", "expr")
], HighLevelILOperation.HLIL_POPCNT: [("src", "expr")], HighLevelILOperation.HLIL_CLZ: [
("src", "expr")
], HighLevelILOperation.HLIL_CTZ: [("src", "expr")], HighLevelILOperation.HLIL_RBIT: [
("src", "expr")
], HighLevelILOperation.HLIL_CLS: [("src", "expr")], HighLevelILOperation.HLIL_MINS: [
("left", "expr"), ("right", "expr")
], HighLevelILOperation.HLIL_MAXS: [("left", "expr"), ("right", "expr")], HighLevelILOperation.HLIL_MINU: [
("left", "expr"), ("right", "expr")
], HighLevelILOperation.HLIL_MAXU: [("left", "expr"), ("right", "expr")], HighLevelILOperation.HLIL_ABS: [
("src", "expr")
], HighLevelILOperation.HLIL_SX: [
("src", "expr")
], HighLevelILOperation.HLIL_ZX: [("src", "expr")], HighLevelILOperation.HLIL_LOW_PART: [
("src", "expr")
], HighLevelILOperation.HLIL_CALL: [
("dest", "expr"), ("params", "expr_list")
], HighLevelILOperation.HLIL_CALL_SSA: [
("dest", "expr"), ("params", "expr_list"), ("dest_memory", "int"), ("src_memory", "int")
], HighLevelILOperation.HLIL_CMP_E: [("left", "expr"), ("right", "expr")], HighLevelILOperation.HLIL_CMP_NE: [
("left", "expr"), ("right", "expr")
], HighLevelILOperation.HLIL_CMP_SLT: [("left", "expr"), ("right", "expr")],
HighLevelILOperation.HLIL_CMP_ULT: [("left", "expr"), ("right", "expr")], HighLevelILOperation.HLIL_CMP_SLE: [
("left", "expr"), ("right", "expr")
], HighLevelILOperation.HLIL_CMP_ULE: [("left", "expr"),
("right", "expr")], HighLevelILOperation.HLIL_CMP_SGE: [
("left", "expr"), ("right", "expr")
], HighLevelILOperation.HLIL_CMP_UGE: [("left", "expr"),
("right", "expr")],
HighLevelILOperation.HLIL_CMP_SGT: [("left", "expr"), ("right", "expr")], HighLevelILOperation.HLIL_CMP_UGT: [
("left", "expr"), ("right", "expr")
], HighLevelILOperation.HLIL_TEST_BIT: [
("left", "expr"), ("right", "expr")
], HighLevelILOperation.HLIL_BOOL_TO_INT: [("src", "expr")], HighLevelILOperation.HLIL_ADD_OVERFLOW: [
("left", "expr"), ("right", "expr")
], HighLevelILOperation.HLIL_SYSCALL: [("params", "expr_list")], HighLevelILOperation.HLIL_SYSCALL_SSA: [
("params", "expr_list"), ("dest_memory", "int"), ("src_memory", "int")
], HighLevelILOperation.HLIL_TAILCALL: [
("dest", "expr"), ("params", "expr_list")
], HighLevelILOperation.HLIL_BP: [], HighLevelILOperation.HLIL_TRAP: [
("vector", "int")
], HighLevelILOperation.HLIL_INTRINSIC: [("intrinsic", "intrinsic"),
("params", "expr_list")], HighLevelILOperation.HLIL_INTRINSIC_SSA: [
("intrinsic", "intrinsic"), ("params", "expr_list"),
("dest_memory", "int"), ("src_memory", "int")
], HighLevelILOperation.HLIL_UNDEF: [],
HighLevelILOperation.HLIL_UNIMPL: [], HighLevelILOperation.HLIL_UNIMPL_MEM: [
("src", "expr")
], HighLevelILOperation.HLIL_FADD: [("left", "expr"), ("right", "expr")], HighLevelILOperation.HLIL_FSUB: [
("left", "expr"), ("right", "expr")
], HighLevelILOperation.HLIL_FMUL: [("left", "expr"), ("right", "expr")], HighLevelILOperation.HLIL_FDIV: [
("left", "expr"), ("right", "expr")
], HighLevelILOperation.HLIL_FSQRT: [("src", "expr")], HighLevelILOperation.HLIL_FNEG: [
("src", "expr")
], HighLevelILOperation.HLIL_FABS: [("src", "expr")], HighLevelILOperation.HLIL_FLOAT_TO_INT: [
("src", "expr")
], HighLevelILOperation.HLIL_INT_TO_FLOAT: [("src", "expr")], HighLevelILOperation.HLIL_FLOAT_CONV: [
("src", "expr")
], HighLevelILOperation.HLIL_ROUND_TO_INT: [("src", "expr")], HighLevelILOperation.HLIL_FLOOR: [
("src", "expr")
], HighLevelILOperation.HLIL_CEIL: [("src", "expr")], HighLevelILOperation.HLIL_FTRUNC: [
("src", "expr")
], HighLevelILOperation.HLIL_FCMP_E: [("left", "expr"), ("right", "expr")], HighLevelILOperation.HLIL_FCMP_NE: [
("left", "expr"), ("right", "expr")
], HighLevelILOperation.HLIL_FCMP_LT: [
("left", "expr"), ("right", "expr")
], HighLevelILOperation.HLIL_FCMP_LE: [
("left", "expr"), ("right", "expr")
], HighLevelILOperation.HLIL_FCMP_GE: [
("left", "expr"), ("right", "expr")
], HighLevelILOperation.HLIL_FCMP_GT: [("left", "expr"), ("right", "expr")], HighLevelILOperation.HLIL_FCMP_O: [
("left", "expr"), ("right", "expr")
], HighLevelILOperation.HLIL_FCMP_UO: [("left", "expr"), ("right", "expr")]
}
@staticmethod
def show_hlil_hierarchy():
"""
Opens a new tab showing the HLIL hierarchy which includes classes which can
easily be used with isinstance to match multiple types of IL instructions.
"""
graph = flowgraph.FlowGraph()
nodes = {}
for instruction in ILInstruction.values():
instruction.add_subgraph(graph, nodes)
show_graph_report("HLIL Class Hierarchy Graph", graph)
@classmethod
def create(
cls, func: 'HighLevelILFunction', expr_index: ExpressionIndex, as_ast: bool = True,
instr_index: Optional[InstructionIndex] = None
) -> 'HighLevelILInstruction':
assert func.arch is not None, "Attempted to create IL instruction with function missing an Architecture"
instr = core.BNGetHighLevelILByIndex(func.handle, expr_index, as_ast)
assert instr is not None, "core.BNGetHighLevelILByIndex returned None"
core_instr = CoreHighLevelILInstruction.from_BNHighLevelILInstruction(instr)
if instr_index is None:
instr_index = core.BNGetHighLevelILInstructionForExpr(func.handle, expr_index)
assert instr_index is not None, "core.BNGetHighLevelILInstructionForExpr returned None"
return ILInstruction[instr.operation](func, expr_index, core_instr, as_ast, instr_index)
def __str__(self):
settings = function.DisassemblySettings.default_settings()
settings.set_option(DisassemblyOption.DisableLineFormatting)
lines = self.get_lines(settings)
if lines is None:
return "invalid"
result = []
for line in lines:
cur = ""
for token in line.tokens:
cur += token.text
result.append(cur)
return '\n'.join(result)
def __repr__(self):
settings = function.DisassemblySettings.default_settings()
settings.set_option(DisassemblyOption.DisableLineFormatting)
lines = self.get_lines(settings)
continuation = ""
if lines is None:
first_line = "<invalid>"
else:
first_line = ""
for token in next(lines).tokens:
first_line += token.text
if len(list(lines)) > 1:
continuation = "..."
return f"<{self.__class__.__name__}: {first_line}{continuation}>"
def __eq__(self, other: 'HighLevelILInstruction'):
if not isinstance(other, HighLevelILInstruction):
return NotImplemented
return self.function == other.function and self.expr_index == other.expr_index
def __lt__(self, other: 'HighLevelILInstruction'):
if not isinstance(other, HighLevelILInstruction):
return NotImplemented
return self.function == other.function and self.expr_index < other.expr_index
def __le__(self, other: 'HighLevelILInstruction'):
if not isinstance(other, HighLevelILInstruction):
return NotImplemented
return self.function == other.function and self.expr_index <= other.expr_index
def __gt__(self, other: 'HighLevelILInstruction'):
if not isinstance(other, HighLevelILInstruction):
return NotImplemented
return self.function == other.function and self.expr_index > other.expr_index
def __ge__(self, other: 'HighLevelILInstruction'):
if not isinstance(other, HighLevelILInstruction):
return NotImplemented
return self.function == other.function and self.expr_index >= other.expr_index
def __hash__(self):
return hash((self.function, self.expr_index))
@property
def tokens(self) -> TokenList:
"""HLIL tokens taken from the HLIL text lines (read-only) -- does not include newlines or indentation, use lines for that information"""
settings = function.DisassemblySettings.default_settings()
settings.set_option(DisassemblyOption.DisableLineFormatting)
return [token for line in self.get_lines(settings) for token in line.tokens]
@property
def lines(self) -> LinesType:
"""HLIL text lines (read-only)"""
return self.get_lines()
@property
def prefix_operands(self) -> List[Union[HighLevelILOperandType, HighLevelILOperationAndSize]]:
"""All operands in the expression tree in prefix order"""
result: List[Union[HighLevelILOperandType,
HighLevelILOperationAndSize]] = [HighLevelILOperationAndSize(self.operation, self.size)]
for operand in self.operands:
if isinstance(operand, HighLevelILInstruction):
result.extend(operand.prefix_operands)
else:
result.append(operand)
return result
@property
def postfix_operands(self) -> List[Union[HighLevelILOperandType, HighLevelILOperationAndSize]]:
"""All operands in the expression tree in postfix order"""
result: List[Union[HighLevelILOperandType, HighLevelILOperationAndSize]] = []
for operand in self.operands:
if isinstance(operand, HighLevelILInstruction):
result.extend(operand.postfix_operands)
else:
result.append(operand)
result.append(HighLevelILOperationAndSize(self.operation, self.size))
return result
@property
def instr(self) -> 'HighLevelILInstruction':
"""The statement that this expression belongs to (read-only)"""
return self.function[self.instr_index]
@property
def ast(self) -> 'HighLevelILInstruction':
"""This expression with full AST printing (read-only)"""
if self.as_ast:
return self
return HighLevelILInstruction.create(self.function, self.expr_index, True)
@property
def non_ast(self) -> 'HighLevelILInstruction':
"""This expression without full AST printing (read-only)"""
if not self.as_ast:
return self
return HighLevelILInstruction.create(self.function, self.expr_index, False)
@property
def operation(self) -> HighLevelILOperation:
return self.core_instr.operation
@property
def size(self) -> int:
return self.core_instr.size
@property
def address(self) -> int:
return self.core_instr.address
@property
def source_operand(self) -> ExpressionIndex:
return ExpressionIndex(self.core_instr.source_operand)
@property
def source_location(self) -> ILSourceLocation:
return ILSourceLocation.from_instruction(self)
@property
def core_operands(self) -> OperandsType:
return self.core_instr.operands
@property
def instruction_operands(self) -> List['HighLevelILInstruction']:
result = []
for i in self.operands:
if isinstance(i, list):
result.extend([j for j in i if isinstance(j, HighLevelILInstruction)])
elif isinstance(i, HighLevelILInstruction):
result.append(i)
return result
@property
def vars_written(self) -> VariablesList:
"""List of variables value is written by this instruction"""
result = []
for i in self.operands:
if isinstance(i, HighLevelILInstruction):
result.extend(i.vars_written)
return result
@property
def vars_read(self) -> VariablesList:
"""Non-unique list of variables whose value is read by this instruction"""
non_read = [*self.vars_written, *self.vars_address_taken]
result = []
for v in self.vars:
if v in non_read:
non_read.remove(v)
continue
result.append(v)
return result
@property
def vars_address_taken(self) -> VariablesList:
"""
Non-unique list of variables whose address is taken by instruction
.. note:: This property has some nuance to it, so use carefully. This property will return only those variable which \
directly have their address taken such as `&var_4` or `&var_8.d` but not those which are involved in an address \
calculation such as `&(var_4 + 0)` or `&var_4[0]` even though they may be functionally equivalent.
"""
result = []
for operand in self.instruction_operands:
result.extend(operand.vars_address_taken)
return result
@property
def vars(self) -> VariablesList:
"""Non-unique list of variables read by instruction"""
result = []
for operand in self.operands:
if isinstance(operand, HighLevelILInstruction):
result.extend(operand.vars)
elif isinstance(operand, (variable.Variable, mediumlevelil.SSAVariable)):
result.append(operand)
elif isinstance(operand, list):
for sub_operand in operand:
if isinstance(sub_operand, (variable.Variable, mediumlevelil.SSAVariable)):
result.append(sub_operand)
elif isinstance(sub_operand, HighLevelILInstruction):
result.extend(sub_operand.vars)
return result
@property
def parent(self) -> Optional['HighLevelILInstruction']:
if self.core_instr.parent >= core.BNGetHighLevelILExprCount(self.function.handle):
return None
return HighLevelILInstruction.create(self.function, self.core_instr.parent, self.as_ast)
@property
def ssa_form(self) -> 'HighLevelILInstruction':
"""SSA form of expression (read-only)"""
assert self.function.ssa_form is not None
return HighLevelILInstruction.create(
self.function.ssa_form,
ExpressionIndex(core.BNGetHighLevelILSSAExprIndex(self.function.handle, self.expr_index)), self.as_ast
)
@property
def non_ssa_form(self) -> Optional['HighLevelILInstruction']:
"""Non-SSA form of expression (read-only)"""
if self.function.non_ssa_form is None:
return None
return HighLevelILInstruction.create(
self.function.non_ssa_form,
ExpressionIndex(core.BNGetHighLevelILNonSSAExprIndex(self.function.handle, self.expr_index)), self.as_ast
)
@property
def medium_level_il(self) -> Optional['mediumlevelil.MediumLevelILInstruction']:
"""Medium level IL form of this expression"""
expr = self.function.get_medium_level_il_expr_index(self.expr_index)
if expr is None:
return None
mlil = self.function.medium_level_il
if mlil is None:
return None
ssa_func = mlil.ssa_form
assert ssa_func is not None, "medium_level_il.ssa_form is None"
return mediumlevelil.MediumLevelILInstruction.create(ssa_func, expr)
@property
def mlil(self) -> Optional['mediumlevelil.MediumLevelILInstruction']:
"""Alias for medium_level_il"""
return self.medium_level_il
@property
def mlils(self) -> Optional[List['mediumlevelil.MediumLevelILInstruction']]:
result = []
for expr in self.function.get_medium_level_il_expr_indexes(self.expr_index):
mlil = self.function.medium_level_il
if mlil is None:
return
ssa_func = mlil.ssa_form
assert ssa_func is not None, "medium_level_il.ssa_form is None"
result.append(mediumlevelil.MediumLevelILInstruction.create(ssa_func, expr))
return result
@property
def low_level_il(self) -> Optional['lowlevelil.LowLevelILInstruction']:
"""Low level IL form of this expression"""
if self.mlil is None:
return None
return self.mlil.llil
@property
def llil(self) -> Optional['lowlevelil.LowLevelILInstruction']:
"""Alias for low_level_il"""
return self.low_level_il
@property
def llils(self) -> List['lowlevelil.ExpressionIndex']:
result = set()
mlils = self.mlils
if mlils is None:
return []
for mlil_expr in mlils:
for llil_expr in mlil_expr.llils:
result.add(llil_expr)
return list(result)
@property
def il_basic_block(self) -> Optional['HighLevelILBasicBlock']:
"""
IL basic block object containing this expression (read-only) (only available on finalized functions).
Returns None for HLIL_BLOCK expressions as these can contain multiple basic blocks.
"""
core_block = core.BNGetHighLevelILBasicBlockForInstruction(self.function.handle, self.instr_index)
assert core_block is not None, "core.BNGetHighLevelILBasicBlockForInstruction returned None"
if self.function.source_function is None:
return None
return HighLevelILBasicBlock(core_block, self.function, self.function.source_function.view)
@property
def value(self) -> 'variable.RegisterValue':
"""Value of expression if constant or a known value (read-only)"""
mlil = self.mlil
if mlil is None:
return variable.Undetermined()
return mlil.value
@property
def possible_values(self) -> 'variable.PossibleValueSet':
"""Possible values of expression using path-sensitive static data flow analysis (read-only)"""
mlil = self.mlil
if mlil is None:
return variable.PossibleValueSet()
return mlil.possible_values
@property
def expr_type(self) -> Optional['types.Type']:
"""Type of expression"""
result = core.BNGetHighLevelILExprType(self.function.handle, self.expr_index)
if result.type:
platform = None
if self.function.source_function:
platform = self.function.source_function.platform
return types.Type.create(
result.type, platform=platform, confidence=result.confidence
)
return None
@property
def attributes(self) -> Set[ILInstructionAttribute]:
"""The set of optional attributes placed on the instruction"""
result: Set[ILInstructionAttribute] = set()
for flag in ILInstructionAttribute:
if self.core_instr.attributes & flag.value != 0:
result.add(flag)
return result
def get_possible_values(self, options: Optional[List[DataFlowQueryOption]] = None) -> 'variable.PossibleValueSet':
mlil = self.mlil
if mlil is None:
return variable.PossibleValueSet()
if options is None:
options = []
return mlil.get_possible_values(options)
@property
def ssa_memory_version(self) -> int:
"""Version of active memory contents in SSA form for this instruction"""
return core.BNGetHighLevelILSSAMemoryVersionAtILInstruction(self.function.handle, self.instr_index)
def get_ssa_var_version(self, var: 'variable.Variable') -> int:
var_data = var.to_BNVariable()
return core.BNGetHighLevelILSSAVarVersionAtILInstruction(self.function.handle, var_data, self.instr_index)
def _get_int(self, operand_index: int) -> int:
value = self.core_instr.operands[operand_index]
return (value & ((1 << 63) - 1)) - (value & (1 << 63))
def _get_float(self, operand_index: int) -> float:
value = self.core_instr.operands[operand_index]
if self.core_instr.size == 4:
return struct.unpack("f", struct.pack("I", value & 0xffffffff))[0]
elif self.core_instr.size == 8:
return struct.unpack("d", struct.pack("Q", value))[0]
else:
return float(value)
def _get_constant_data(self, operand_index1: int, operand_index2: int) -> variable.ConstantData:
state = variable.RegisterValueType(self.core_instr.operands[operand_index1])
value = self.core_instr.operands[operand_index2]
return variable.ConstantData(value, 0, state, core.max_confidence, self.core_instr.size, self.function.source_function)
def _get_expr(self, operand_index: int) -> 'HighLevelILInstruction':
return HighLevelILInstruction.create(
self.function, ExpressionIndex(self.core_instr.operands[operand_index]),
self.as_ast
)
def _get_intrinsic(self, operand_index: int) -> 'lowlevelil.ILIntrinsic':
if self.function.arch is None:
raise ValueError("Attempting to create ILIntrinsic from function with no Architecture")
return lowlevelil.ILIntrinsic(
self.function.arch, architecture.IntrinsicIndex(self.core_instr.operands[operand_index])
)
def _get_var(self, operand_index: int) -> 'variable.Variable':
value = self.core_instr.operands[operand_index]
return variable.Variable.from_identifier(self.function, value)
def _get_var_ssa(self, operand_index1: int, operand_index2: int) -> 'mediumlevelil.SSAVariable':
var = variable.Variable.from_identifier(self.function, self.core_instr.operands[operand_index1])
version = self.core_instr.operands[operand_index2]
return mediumlevelil.SSAVariable(var, version)
def _get_var_ssa_dest_and_src(self, operand_index1: int, operand_index2: int) -> 'mediumlevelil.SSAVariable':
var = variable.Variable.from_identifier(self.function, self.core_instr.operands[operand_index1])
dest_version = self.core_instr.operands[operand_index2]
return mediumlevelil.SSAVariable(var, dest_version)
def _get_int_list(self, operand_index: int) -> List[int]:
count = ctypes.c_ulonglong()
operand_list = core.BNHighLevelILGetOperandList(self.function.handle, self.expr_index, operand_index, count)
assert operand_list is not None, "core.BNHighLevelILGetOperandList returned None"
value: List[int] = []
try:
for j in range(count.value):
value.append(operand_list[j])
return value
finally:
core.BNHighLevelILFreeOperandList(operand_list)
def _get_expr_list(self, operand_index1: int, _: int) -> List['HighLevelILInstruction']:
count = ctypes.c_ulonglong()
operand_list = core.BNHighLevelILGetOperandList(self.function.handle, self.expr_index, operand_index1, count)
assert operand_list is not None, "core.BNHighLevelILGetOperandList returned None"
value: List[HighLevelILInstruction] = []
try:
for j in range(count.value):
value.append(HighLevelILInstruction.create(self.function, operand_list[j], self.as_ast))
return value
finally:
core.BNHighLevelILFreeOperandList(operand_list)
def _get_var_ssa_list(self, operand_index1: int, _: int) -> List['mediumlevelil.SSAVariable']:
count = ctypes.c_ulonglong()
operand_list = core.BNHighLevelILGetOperandList(self.function.handle, self.expr_index, operand_index1, count)
assert operand_list is not None, "core.BNHighLevelILGetOperandList returned None"
value = []
try:
for j in range(count.value // 2):
var_id = operand_list[j * 2]
var_version = operand_list[(j*2) + 1]
value.append(
mediumlevelil.SSAVariable(variable.Variable.from_identifier(self.function, var_id), var_version)
)
return value
finally:
core.BNMediumLevelILFreeOperandList(operand_list)
def _get_member_index(self, operand_index: int) -> Optional[int]:
value = self.core_instr.operands[operand_index]
if (value & (1 << 63)) != 0:
value = None
return value
def _get_label(self, operand_index: int) -> GotoLabel:
return GotoLabel(self.function, self.core_instr.operands[operand_index])
def _get_constraint(self, operand_index: int) -> variable.PossibleValueSet:
value = core.BNGetCachedHighLevelILPossibleValueSet(self.function.handle, self.core_instr.operands[operand_index])
result = variable.PossibleValueSet(self.function.arch, value)
core.BNFreePossibleValueSet(value)
return result
@property
def raw_operands(self) -> OperandsType:
"""Raw operand expression indices as specified by the core structure (read-only)"""
return self.instr.operands
@property
def operands(self) -> List[HighLevelILOperandType]:
"""
Operands for the instruction
Consider using more specific APIs for ``src``, ``dest``, ``params``, etc where appropriate.
"""
return list(map(lambda x: x[1], self.detailed_operands))
@property
def detailed_operands(self) -> List[Tuple[str, HighLevelILOperandType, str]]:
"""
Returns a list of tuples containing the name of the operand, the operand, and the type of the operand.
Useful for iterating over all operands of an instruction and sub-instructions.
"""
return []
def traverse(self, cb: Callable[['HighLevelILInstruction', Any], Any], *args: Any, shallow: bool = True, **kwargs: Any) -> Iterator[Any]:
"""
``traverse`` is a generator that allows you to traverse the HLIL AST in a depth-first manner. It will yield the
result of the callback function for each node in the AST. Arguments can be passed to the callback function using
``args`` and ``kwargs``. See the `Developer Docs <https://docs.binary.ninja/dev/concepts.html#walking-ils>`_ for more examples.
:param Callable[[HighLevelILInstruction, Any], Any] cb: The callback function to call for each node in the HighLevelILInstruction
:param Any args: Custom user-defined arguments
:param bool shallow: Whether traversal occurs on block instructions
:param Any kwargs: Custom user-defined keyword arguments
:return: An iterator of the results of the callback function
:rtype: Iterator[Any]
:Example:
>>> def get_constant_less_than_value(inst: HighLevelILInstruction, value: int) -> int:
... if isinstance(inst, Constant) and inst.constant < value:
... return inst.constant
>>>
>>> for result in inst.traverse(get_constant_less_than_value, 10):
... print(f"Found a constant {result} < 10 in {repr(inst)}")
:Example:
>>> def get_import_data_var_with_name(inst: HighLevelILInstruction, name: str) -> Optional['DataVariable']:
... if isinstance(inst, HighLevelILImport):
... if bv.get_symbol_at(inst.constant).name == name:
... return bv.get_data_var_at(inst.constant)
>>>
>>> for result in inst.traverse(get_import_data_var_with_name, "__cxa_finalize", shallow=False):
... print(f"Found import at {result} in {repr(inst)}")
"""
if (result := cb(self, *args, **kwargs)) is not None:
yield result
blacklisted_op_names = {'true', 'false', 'body', 'cases', 'default'}
for op_name, op, _ in self.detailed_operands:
if shallow and op_name in blacklisted_op_names:
continue
if isinstance(op, HighLevelILInstruction):
yield from op.traverse(cb, *args, shallow=shallow, **kwargs)
elif isinstance(op, list) and all(isinstance(i, HighLevelILInstruction) for i in op):
for i in op:
yield from i.traverse(cb, *args, shallow=shallow, **kwargs) # type: ignore
@deprecation.deprecated(deprecated_in="4.0.4907", details="Use :py:func:`HighLevelILInstruction.traverse` instead.")
def visit_all(self, cb: HighLevelILVisitorCallback,
name: str = "root", parent: Optional['HighLevelILInstruction'] = None) -> bool:
"""
Visits all operands of this instruction and all operands of any sub-instructions.
Using pre-order traversal.
:param HighLevelILVisitorCallback cb: Callback function that takes the name of the operand, the operand, operand type, and parent instruction
:return: True if all instructions were visited, False if the callback returned False
"""
if cb(name, self, "HighLevelILInstruction", parent) == False:
return False
for name, op, opType in self.detailed_operands:
if isinstance(op, HighLevelILInstruction):
if not op.visit_all(cb, name, self):
return False
elif isinstance(op, list) and all(isinstance(i, HighLevelILInstruction) for i in op):
for i in op:
if not i.visit_all(cb, name, self): # type: ignore
return False
elif cb(name, op, opType, self) == False:
return False
return True
@deprecation.deprecated(deprecated_in="4.0.4907", details="Use :py:func:`HighLevelILInstruction.traverse` instead.")
def visit_operands(self, cb: HighLevelILVisitorCallback,
name: str = "root", parent: Optional['HighLevelILInstruction'] = None) -> bool:
"""
Visits all leaf operands of this instruction and any sub-instructions.
:param HighLevelILVisitorCallback cb: Callback function that takes the name of the operand, the operand, operand type, and parent instruction
:return: True if all instructions were visited, False if the callback returned False
"""
for name, op, opType in self.detailed_operands:
if isinstance(op, HighLevelILInstruction):
if not op.visit_operands(cb, name, self):
return False
elif isinstance(op, list) and all(isinstance(i, HighLevelILInstruction) for i in op):
for i in op:
if not i.visit_operands(cb, name, self): # type: ignore
return False
elif cb(name, op, opType, self) == False:
return False
return True
@deprecation.deprecated(deprecated_in="4.0.4907", details="Use :py:func:`HighLevelILInstruction.traverse` instead.")
def visit(self, cb: HighLevelILVisitorCallback,
name: str = "root", parent: Optional['HighLevelILInstruction'] = None) -> bool:
"""
Visits all HighLevelILInstructions in the operands of this instruction and any sub-instructions.
In the callback you provide, you likely only need to interact with the second argument (see the example below).
:param HighLevelILVisitorCallback cb: Callback function that takes the name of the operand, the operand, operand type, and parent instruction
:return: True if all instructions were visited, False if the callback returned False
:Example:
>>> def visitor(_a, inst, _c, _d) -> bool:
>>> if isinstance(inst, Constant):
>>> print(f"Found constant: {inst.constant}")
>>> return False # Stop recursion (once we find a constant, don't recurse in to any sub-instructions (which there won't actually be any...))
>>> # Otherwise, keep recursing the subexpressions of this instruction; if no return value is provided, it'll keep descending
>>>
>>> # Finds all constants used in the program
>>> for inst in bv.hlil_instructions:
>>> inst.visit(visitor)
"""
if cb(name, self, "HighLevelILInstruction", parent) == False:
return False
for name, op, _ in self.detailed_operands:
if isinstance(op, HighLevelILInstruction):
if not op.visit(cb, name, self):
return False
elif isinstance(op, list) and all(isinstance(i, HighLevelILInstruction) for i in op):
for i in op:
if not i.visit(cb, name, self): # type: ignore
return False
return True
@property
def has_side_effects(self) -> bool:
return core.BNHighLevelILHasSideEffects(self.function.handle, self.expr_index)
def get_lines(self, settings: Optional['function.DisassemblySettings'] = None) -> LinesType:
"""Gets HLIL text lines with optional settings"""
if settings is not None:
settings = settings.handle
count = ctypes.c_ulonglong()
lines = core.BNGetHighLevelILExprText(self.function.handle, self.expr_index, self.as_ast, count, settings)
assert lines is not None, "core.BNGetHighLevelILExprText returned None"
try:
for i in range(0, count.value):
addr = lines[i].addr
if lines[i].instrIndex != 0xffffffffffffffff:
il_instr = self.function[lines[i].instrIndex]
else:
il_instr = None
color = highlight.HighlightColor._from_core_struct(lines[i].highlight)
tokens = function.InstructionTextToken._from_core_struct(lines[i].tokens, lines[i].count)
yield function.DisassemblyTextLine(tokens, addr, il_instr, color)
finally:
core.BNFreeDisassemblyTextLines(lines, count.value)
@property
def can_collapse(self) -> bool:
"""If this instruction can be collapsed in rendered lines"""
return self.operation in [
HighLevelILOperation.HLIL_IF,
HighLevelILOperation.HLIL_WHILE,
HighLevelILOperation.HLIL_WHILE_SSA,
HighLevelILOperation.HLIL_DO_WHILE,
HighLevelILOperation.HLIL_DO_WHILE_SSA,
HighLevelILOperation.HLIL_FOR,
HighLevelILOperation.HLIL_FOR_SSA,
HighLevelILOperation.HLIL_SWITCH,
HighLevelILOperation.HLIL_CASE
]
def get_instruction_hash(self, discriminator: int) -> int:
"""
Hash of instruction matching the C++ HighLevelILInstruction::GetInstructionHash,
used for collapsed region matching.
:param discriminator: Extra value to include in the hash to differentiate regions
"""
def rotl(value, shift):
return ((value << shift) & 0xffffffffffffffff) | (value >> (64 - shift))
hash = self.operation.value
hash ^= rotl(self.address, 23)
hash ^= rotl(discriminator, 47)
return hash
@property
def derived_string_reference(self) -> Optional['binaryview.DerivedString']:
str = core.BNDerivedString()
if not core.BNGetHighLevelILDerivedStringReferenceForExpr(self.function.handle, self.expr_index, str):
return None
return binaryview.DerivedString._from_core_struct(str, True)
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILUnaryBase(HighLevelILInstruction, UnaryOperation):
@property
def src(self) -> HighLevelILInstruction:
return self._get_expr(0)
@property
def detailed_operands(self) -> List[Tuple[str, HighLevelILOperandType, str]]:
return [
("src", self.src, "HighLevelILInstruction"),
]
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILBinaryBase(HighLevelILInstruction, BinaryOperation):
@property
def left(self) -> HighLevelILInstruction:
return self._get_expr(0)
@property
def right(self) -> HighLevelILInstruction:
return self._get_expr(1)
@property
def detailed_operands(self) -> List[Tuple[str, HighLevelILOperandType, str]]:
return [
("left", self.left, "HighLevelILInstruction"),
("right", self.right, "HighLevelILInstruction"),
]
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILComparisonBase(HighLevelILBinaryBase, Comparison):
pass
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILCarryBase(HighLevelILInstruction, Arithmetic):
@property
def left(self) -> HighLevelILInstruction:
return self._get_expr(0)
@property
def right(self) -> HighLevelILInstruction:
return self._get_expr(1)
@property
def carry(self) -> HighLevelILInstruction:
return self._get_expr(2)
@property
def detailed_operands(self) -> List[Tuple[str, HighLevelILOperandType, str]]:
return [
("left", self.left, "HighLevelILInstruction"),
("right", self.right, "HighLevelILInstruction"),
("carry", self.carry, "HighLevelILInstruction"),
]
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILNop(HighLevelILInstruction):
pass
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILBlock(HighLevelILInstruction):
@property
def body(self) -> List[HighLevelILInstruction]:
return self._get_expr_list(0, 1)
def __iter__(self) -> Generator['HighLevelILInstruction', None, None]:
for expr in self.body:
yield expr
@property
def detailed_operands(self) -> List[Tuple[str, HighLevelILOperandType, str]]:
return [
("body", self.body, "List[HighLevelILInstruction]"),
]
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILIf(HighLevelILInstruction, ControlFlow):
@property
def condition(self) -> HighLevelILInstruction:
return self._get_expr(0)
@property
def true(self) -> HighLevelILInstruction:
return self._get_expr(1)
@property
def false(self) -> HighLevelILInstruction:
return self._get_expr(2)
@property
def detailed_operands(self) -> List[Tuple[str, HighLevelILOperandType, str]]:
return [
("condition", self.condition, "HighLevelILInstruction"),
("true", self.true, "HighLevelILInstruction"),
("false", self.false, "HighLevelILInstruction"),
]
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILWhile(HighLevelILInstruction, Loop):
@property
def condition(self) -> HighLevelILInstruction:
return self._get_expr(0)
@property
def body(self) -> HighLevelILInstruction:
return self._get_expr(1)
@property
def detailed_operands(self) -> List[Tuple[str, HighLevelILOperandType, str]]:
return [
("condition", self.condition, "HighLevelILInstruction"),
("body", self.body, "HighLevelILInstruction"),
]
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILWhileSsa(HighLevelILInstruction, Loop, SSA):
@property
def condition_phi(self) -> HighLevelILInstruction:
return self._get_expr(0)
@property
def condition(self) -> HighLevelILInstruction:
return self._get_expr(1)
@property
def body(self) -> HighLevelILInstruction:
return self._get_expr(2)
@property
def detailed_operands(self) -> List[Tuple[str, HighLevelILOperandType, str]]:
return [
("condition_phi", self.condition_phi, "HighLevelILInstruction"),
("condition", self.condition, "HighLevelILInstruction"),
("body", self.body, "HighLevelILInstruction"),
]
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILDoWhile(HighLevelILInstruction, Loop):
@property
def body(self) -> HighLevelILInstruction:
return self._get_expr(0)
@property
def condition(self) -> HighLevelILInstruction:
return self._get_expr(1)
@property
def detailed_operands(self) -> List[Tuple[str, HighLevelILOperandType, str]]:
return [
("body", self.body, "HighLevelILInstruction"),
("condition", self.condition, "HighLevelILInstruction"),
]
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILDoWhileSsa(HighLevelILInstruction, Loop, SSA):
@property
def body(self) -> HighLevelILInstruction:
return self._get_expr(0)
@property
def condition_phi(self) -> HighLevelILInstruction:
return self._get_expr(1)
@property
def condition(self) -> HighLevelILInstruction:
return self._get_expr(2)
@property
def detailed_operands(self) -> List[Tuple[str, HighLevelILOperandType, str]]:
return [
("body", self.body, "HighLevelILInstruction"),
("condition_phi", self.condition_phi, "HighLevelILInstruction"),
("condition", self.condition, "HighLevelILInstruction"),
]
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILFor(HighLevelILInstruction, Loop):
@property
def init(self) -> HighLevelILInstruction:
return self._get_expr(0)
@property
def condition(self) -> HighLevelILInstruction:
return self._get_expr(1)
@property
def update(self) -> HighLevelILInstruction:
return self._get_expr(2)
@property
def body(self) -> HighLevelILInstruction:
return self._get_expr(3)
@property
def detailed_operands(self) -> List[Tuple[str, HighLevelILOperandType, str]]:
return [
("init", self.init, "HighLevelILInstruction"),
("condition", self.condition, "HighLevelILInstruction"),
("update", self.update, "HighLevelILInstruction"),
("body", self.body, "HighLevelILInstruction"),
]
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILForSsa(HighLevelILInstruction, Loop, SSA):
@property
def init(self) -> HighLevelILInstruction:
return self._get_expr(0)
@property
def condition_phi(self) -> HighLevelILInstruction:
return self._get_expr(1)
@property
def condition(self) -> HighLevelILInstruction:
return self._get_expr(2)
@property
def update(self) -> HighLevelILInstruction:
return self._get_expr(3)
@property
def body(self) -> HighLevelILInstruction:
return self._get_expr(4)
@property
def detailed_operands(self) -> List[Tuple[str, HighLevelILOperandType, str]]:
return [
("init", self.init, "HighLevelILInstruction"),
("condition_phi", self.condition_phi, "HighLevelILInstruction"),
("condition", self.condition, "HighLevelILInstruction"),
("update", self.update, "HighLevelILInstruction"),
("body", self.body, "HighLevelILInstruction"),
]
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILSwitch(HighLevelILInstruction, ControlFlow):
@property
def condition(self) -> HighLevelILInstruction:
return self._get_expr(0)
@property
def default(self) -> HighLevelILInstruction:
return self._get_expr(1)
@property
def cases(self) -> List[HighLevelILInstruction]:
return self._get_expr_list(2, 3)
@property
def detailed_operands(self) -> List[Tuple[str, HighLevelILOperandType, str]]:
return [
("condition", self.condition, "HighLevelILInstruction"),
("default", self.default, "HighLevelILInstruction"),
("cases", self.cases, "List[HighLevelILInstruction]"),
]
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILCase(HighLevelILInstruction):
@property
def values(self) -> List[HighLevelILInstruction]:
return self._get_expr_list(0, 1)
@property
def body(self) -> HighLevelILInstruction:
return self._get_expr(2)
@property
def detailed_operands(self) -> List[Tuple[str, HighLevelILOperandType, str]]:
return [
("values", self.values, "List[HighLevelILInstruction]"),
("body", self.body, "HighLevelILInstruction"),
]
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILBreak(HighLevelILInstruction, Terminal):
pass
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILContinue(HighLevelILInstruction, ControlFlow):
pass
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILJump(HighLevelILInstruction, Terminal):
@property
def dest(self) -> HighLevelILInstruction:
return self._get_expr(0)
@property
def detailed_operands(self) -> List[Tuple[str, HighLevelILOperandType, str]]:
return [
("dest", self.dest, "HighLevelILInstruction"),
]
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILRet(HighLevelILInstruction, Return):
@property
def src(self) -> List[HighLevelILInstruction]:
return self._get_expr_list(0, 1)
@property
def detailed_operands(self) -> List[Tuple[str, HighLevelILOperandType, str]]:
return [
("src", self.src, "List[HighLevelILInstruction]"),
]
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILNoret(HighLevelILInstruction, Terminal):
pass
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILUnreachable(HighLevelILInstruction, Terminal):
pass
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILGoto(HighLevelILInstruction, Terminal):
@property
def target(self) -> GotoLabel:
return self._get_label(0)
@property
def detailed_operands(self) -> List[Tuple[str, HighLevelILOperandType, str]]:
return [
("target", self.target, "GotoLabel"),
]
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILLabel(HighLevelILInstruction):
@property
def target(self) -> GotoLabel:
return self._get_label(0)
@property
def detailed_operands(self) -> List[Tuple[str, HighLevelILOperandType, str]]:
return [
("target", self.target, "GotoLabel"),
]
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILVarDeclare(HighLevelILInstruction):
@property
def var(self) -> 'variable.Variable':
return self._get_var(0)
@property
def detailed_operands(self) -> List[Tuple[str, HighLevelILOperandType, str]]:
return [
("var", self.var, "Variable"),
]
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILVarInit(HighLevelILInstruction, SetVar):
@property
def dest(self) -> 'variable.Variable':
return self._get_var(0)
@property
def src(self) -> HighLevelILInstruction:
return self._get_expr(1)
@property
def detailed_operands(self) -> List[Tuple[str, HighLevelILOperandType, str]]:
return [
("dest", self.dest, "Variable"),
("src", self.src, "HighLevelILInstruction"),
]
@property
def vars_written(self) -> VariablesList:
return [self.dest]
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILVarInitSsa(HighLevelILInstruction, SetVar, SSA):
@property
def dest(self) -> 'mediumlevelil.SSAVariable':
return self._get_var_ssa(0, 1)
@property
def src(self) -> HighLevelILInstruction:
return self._get_expr(2)
@property
def detailed_operands(self) -> List[Tuple[str, HighLevelILOperandType, str]]:
return [
("dest", self.dest, "SSAVariable"),
("src", self.src, "HighLevelILInstruction"),
]
@property
def vars_written(self) -> VariablesList:
return [self.dest]
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILAssign(HighLevelILInstruction, SetVar):
@property
def dest(self) -> HighLevelILInstruction:
return self._get_expr(0)
@property
def src(self) -> HighLevelILInstruction:
return self._get_expr(1)
@property
def detailed_operands(self) -> List[Tuple[str, HighLevelILOperandType, str]]:
return [
("dest", self.dest, "HighLevelILInstruction"),
("src", self.src, "HighLevelILInstruction"),
]
@property
def vars_written(self) -> VariablesList:
if isinstance(self.dest, (HighLevelILSplit, HighLevelILVar, HighLevelILVarSsa)):
return [*self.dest.vars, *self.src.vars_written]
elif isinstance(self.dest, HighLevelILStructField):
return [*self.dest.vars, *self.src.vars_written]
else:
return [*self.dest.vars_written, *self.src.vars_written]
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILAssignUnpack(HighLevelILInstruction, SetVar):
@property
def dest(self) -> List[HighLevelILInstruction]:
return self._get_expr_list(0, 1)
@property
def src(self) -> HighLevelILInstruction:
return self._get_expr(2)
@property
def detailed_operands(self) -> List[Tuple[str, HighLevelILOperandType, str]]:
return [
("dest", self.dest, "List[HighLevelILInstruction]"),
("src", self.src, "HighLevelILInstruction"),
]
@property
def vars_written(self) -> VariablesList:
result = []
for i in self.dest:
if isinstance(i, (HighLevelILVar, HighLevelILVarSsa)):
result.append(i.var)
else:
result.extend(i.vars_written)
return result
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILAssignMemSsa(HighLevelILInstruction, SSA):
@property
def dest(self) -> HighLevelILInstruction:
return self._get_expr(0)
@property
def dest_memory(self) -> int:
return self._get_int(1)
@property
def src(self) -> HighLevelILInstruction:
return self._get_expr(2)
@property
def src_memory(self) -> int:
return self._get_int(3)
@property
def detailed_operands(self) -> List[Tuple[str, HighLevelILOperandType, str]]:
return [
("dest", self.dest, "HighLevelILInstruction"),
("dest_memory", self.dest_memory, "int"),
("src", self.src, "HighLevelILInstruction"),
("src_memory", self.src_memory, "int"),
]
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILAssignUnpackMemSsa(HighLevelILInstruction, SSA, Memory):
@property
def dest(self) -> List[HighLevelILInstruction]:
return self._get_expr_list(0, 1)
@property
def dest_memory(self) -> int:
return self._get_int(2)
@property
def src(self) -> HighLevelILInstruction:
return self._get_expr(3)
@property
def src_memory(self) -> int:
return self._get_int(4)
@property
def detailed_operands(self) -> List[Tuple[str, HighLevelILOperandType, str]]:
return [
("dest", self.dest, "List[HighLevelILInstruction]"),
("dest_memory", self.dest_memory, "int"),
("src", self.src, "HighLevelILInstruction"),
("src_memory", self.src_memory, "int"),
]
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILVar(HighLevelILInstruction, VariableInstruction):
@property
def var(self) -> 'variable.Variable':
return self._get_var(0)
@property
def detailed_operands(self) -> List[Tuple[str, HighLevelILOperandType, str]]:
return [
("var", self.var, "Variable"),
]
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILVarSsa(HighLevelILInstruction, SSAVariableInstruction):
@property
def var(self) -> 'mediumlevelil.SSAVariable':
return self._get_var_ssa(0, 1)
@property
def detailed_operands(self) -> List[Tuple[str, HighLevelILOperandType, str]]:
return [
("var", self.var, "SSAVariable"),
]
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILVarPhi(HighLevelILInstruction, Phi, SetVar):
@property
def dest(self) -> 'mediumlevelil.SSAVariable':
return self._get_var_ssa(0, 1)
@property
def src(self) -> List['mediumlevelil.SSAVariable']:
return self._get_var_ssa_list(2, 3)
@property
def vars_written(self) -> VariablesList:
return [self.dest]
@property
def detailed_operands(self) -> List[Tuple[str, HighLevelILOperandType, str]]:
return [
("dest", self.dest, "SSAVariable"),
("src", self.src, "List[SSAVariable]"),
]
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILMemPhi(HighLevelILInstruction, Memory, Phi):
@property
def dest(self) -> int:
return self._get_int(0)
@property
def src(self) -> List[int]:
return self._get_int_list(1)
@property
def detailed_operands(self) -> List[Tuple[str, HighLevelILOperandType, str]]:
return [
("dest", self.dest, "int"),
("src", self.src, "List[int]"),
]
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILStructField(HighLevelILInstruction):
@property
def src(self) -> HighLevelILInstruction:
return self._get_expr(0)
@property
def offset(self) -> int:
return self._get_int(1)
@property
def member_index(self) -> Optional[int]:
return self._get_member_index(2)
@property
def detailed_operands(self) -> List[Tuple[str, HighLevelILOperandType, str]]:
return [
("src", self.src, "HighLevelILInstruction"),
("offset", self.offset, "int"),
("member_index", self.member_index, "Optional[int]"),
]
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILArrayIndex(HighLevelILInstruction):
@property
def src(self) -> HighLevelILInstruction:
return self._get_expr(0)
@property
def index(self) -> HighLevelILInstruction:
return self._get_expr(1)
@property
def detailed_operands(self) -> List[Tuple[str, HighLevelILOperandType, str]]:
return [
("src", self.src, "HighLevelILInstruction"),
("index", self.index, "HighLevelILInstruction"),
]
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILArrayIndexSsa(HighLevelILInstruction, SSA):
@property
def src(self) -> HighLevelILInstruction:
return self._get_expr(0)
@property
def src_memory(self) -> int:
return self._get_int(1)
@property
def index(self) -> HighLevelILInstruction:
return self._get_expr(2)
@property
def detailed_operands(self) -> List[Tuple[str, HighLevelILOperandType, str]]:
return [
("src", self.src, "HighLevelILInstruction"),
("src_memory", self.src_memory, "int"),
("index", self.index, "HighLevelILInstruction"),
]
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILSplit(HighLevelILInstruction):
@property
def high(self) -> HighLevelILInstruction:
return self._get_expr(0)
@property
def low(self) -> HighLevelILInstruction:
return self._get_expr(1)
@property
def detailed_operands(self) -> List[Tuple[str, HighLevelILOperandType, str]]:
return [
("high", self.high, "HighLevelILInstruction"),
("low", self.low, "HighLevelILInstruction"),
]
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILDeref(HighLevelILUnaryBase):
pass
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILDerefField(HighLevelILInstruction):
@property
def src(self) -> HighLevelILInstruction:
return self._get_expr(0)
@property
def offset(self) -> int:
return self._get_int(1)
@property
def member_index(self) -> Optional[int]:
return self._get_member_index(2)
@property
def detailed_operands(self) -> List[Tuple[str, HighLevelILOperandType, str]]:
return [
("src", self.src, "HighLevelILInstruction"),
("offset", self.offset, "int"),
("member_index", self.member_index, "Optional[int]"),
]
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILDerefSsa(HighLevelILInstruction, SSA):
@property
def src(self) -> HighLevelILInstruction:
return self._get_expr(0)
@property
def src_memory(self) -> int:
return self._get_int(1)
@property
def detailed_operands(self) -> List[Tuple[str, HighLevelILOperandType, str]]:
return [
("src", self.src, "HighLevelILInstruction"),
("src_memory", self.src_memory, "int"),
]
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILDerefFieldSsa(HighLevelILInstruction, SSA):
@property
def src(self) -> HighLevelILInstruction:
return self._get_expr(0)
@property
def src_memory(self) -> int:
return self._get_int(1)
@property
def offset(self) -> int:
return self._get_int(2)
@property
def member_index(self) -> Optional[int]:
return self._get_member_index(3)
@property
def detailed_operands(self) -> List[Tuple[str, HighLevelILOperandType, str]]:
return [
("src", self.src, "HighLevelILInstruction"),
("src_memory", self.src_memory, "int"),
("offset", self.offset, "int"),
("member_index", self.member_index, "Optional[int]"),
]
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILAddressOf(HighLevelILUnaryBase):
@property
def vars_address_taken(self) -> VariablesList:
if isinstance(self.src, (HighLevelILVar, HighLevelILVarSsa)):
return [self.src.var]
elif isinstance(self.src, HighLevelILStructField) and isinstance(self.src.src, (HighLevelILVar, HighLevelILVarSsa)):
return [self.src.src.var]
return [*self.src.vars_address_taken]
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILPassByRef(HighLevelILUnaryBase):
pass
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILReturnByRef(HighLevelILUnaryBase):
pass
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILConst(HighLevelILInstruction, Constant):
@property
def constant(self) -> int:
return self._get_int(0)
@property
def detailed_operands(self) -> List[Tuple[str, HighLevelILOperandType, str]]:
return [
("constant", self.constant, "int"),
]
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILConstPtr(HighLevelILInstruction, Constant):
@property
def constant(self) -> int:
return self._get_int(0)
@property
def detailed_operands(self) -> List[Tuple[str, HighLevelILOperandType, str]]:
return [
("constant", self.constant, "int"),
]
@property
def string(self) -> Optional[Tuple[str, StringType]]:
return self.function.view.check_for_string_annotation_type(self.constant, True, True, 0)
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILExternPtr(HighLevelILInstruction, Constant):
@property
def constant(self) -> int:
return self._get_int(0)
@property
def offset(self) -> int:
return self._get_int(1)
@property
def detailed_operands(self) -> List[Tuple[str, HighLevelILOperandType, str]]:
return [
("constant", self.constant, "int"),
("offset", self.offset, "int"),
]
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILFloatConst(HighLevelILInstruction, Constant):
@property
def constant(self) -> float:
return self._get_float(0)
@property
def detailed_operands(self) -> List[Tuple[str, HighLevelILOperandType, str]]:
return [
("constant", self.constant, "float"),
]
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILImport(HighLevelILInstruction, Constant):
@property
def constant(self) -> int:
return self._get_int(0)
@property
def detailed_operands(self) -> List[Tuple[str, HighLevelILOperandType, str]]:
return [
("constant", self.constant, "int"),
]
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILConstData(HighLevelILInstruction, Constant):
@property
def constant(self) -> variable.ConstantData:
return self._get_constant_data(0, 1)
@property
def constant_data(self) -> variable.ConstantData:
return self._get_constant_data(0, 1)
@property
def detailed_operands(self) -> List[Tuple[str, HighLevelILOperandType, str]]:
return [
("constant", self.constant, "ConstantData"),
]
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILAdd(HighLevelILBinaryBase, Arithmetic):
pass
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILAdc(HighLevelILCarryBase):
pass
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILSub(HighLevelILBinaryBase, Arithmetic):
pass
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILSbb(HighLevelILCarryBase):
pass
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILAnd(HighLevelILBinaryBase, Arithmetic):
pass
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILOr(HighLevelILBinaryBase, Arithmetic):
pass
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILXor(HighLevelILBinaryBase, Arithmetic):
pass
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILLsl(HighLevelILBinaryBase, Arithmetic):
pass
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILLsr(HighLevelILBinaryBase, Arithmetic):
pass
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILAsr(HighLevelILBinaryBase, Arithmetic):
pass
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILRol(HighLevelILBinaryBase, Arithmetic):
pass
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILRlc(HighLevelILCarryBase):
pass
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILRor(HighLevelILCarryBase):
pass
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILRrc(HighLevelILCarryBase):
pass
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILMul(HighLevelILBinaryBase, Arithmetic):
pass
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILMuluDp(HighLevelILBinaryBase, DoublePrecision):
pass
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILMulsDp(Signed, HighLevelILBinaryBase, DoublePrecision):
pass
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILDivu(HighLevelILBinaryBase, Arithmetic):
pass
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILDivuDp(HighLevelILBinaryBase, DoublePrecision):
pass
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILDivs(HighLevelILBinaryBase, Signed):
pass
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILDivsDp(HighLevelILBinaryBase, Signed, DoublePrecision):
pass
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILModu(HighLevelILBinaryBase, Arithmetic):
pass
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILModuDp(HighLevelILBinaryBase, DoublePrecision):
pass
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILMods(HighLevelILBinaryBase, Signed):
pass
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILModsDp(HighLevelILBinaryBase, Signed, DoublePrecision):
pass
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILNeg(HighLevelILUnaryBase, Arithmetic):
pass
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILNot(HighLevelILUnaryBase, Arithmetic):
pass
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILBswap(HighLevelILUnaryBase, Arithmetic):
pass
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILPopcnt(HighLevelILUnaryBase, Arithmetic):
pass
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILClz(HighLevelILUnaryBase, Arithmetic):
pass
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILCtz(HighLevelILUnaryBase, Arithmetic):
pass
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILRbit(HighLevelILUnaryBase, Arithmetic):
pass
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILCls(HighLevelILUnaryBase, Arithmetic):
pass
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILMins(HighLevelILBinaryBase, Arithmetic, Signed):
pass
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILMaxs(HighLevelILBinaryBase, Arithmetic, Signed):
pass
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILMinu(HighLevelILBinaryBase, Arithmetic):
pass
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILMaxu(HighLevelILBinaryBase, Arithmetic):
pass
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILAbs(HighLevelILUnaryBase, Arithmetic):
pass
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILSx(HighLevelILUnaryBase, Arithmetic):
pass
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILZx(HighLevelILUnaryBase, Arithmetic):
pass
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILLowPart(HighLevelILUnaryBase, Arithmetic):
pass
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILCall(HighLevelILInstruction, Localcall):
@property
def dest(self) -> HighLevelILInstruction:
return self._get_expr(0)
@property
def params(self) -> List[HighLevelILInstruction]:
return self._get_expr_list(1, 2)
@property
def detailed_operands(self) -> List[Tuple[str, HighLevelILOperandType, str]]:
return [
("dest", self.dest, "HighLevelILInstruction"),
("params", self.params, "List[HighLevelILInstruction]"),
]
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILCallSsa(HighLevelILInstruction, Localcall, SSA):
@property
def dest(self) -> HighLevelILInstruction:
return self._get_expr(0)
@property
def params(self) -> List[HighLevelILInstruction]:
return self._get_expr_list(1, 2)
@property
def dest_memory(self) -> int:
return self._get_int(3)
@property
def src_memory(self) -> int:
return self._get_int(4)
@property
def detailed_operands(self) -> List[Tuple[str, HighLevelILOperandType, str]]:
return [
("dest", self.dest, "HighLevelILInstruction"),
("params", self.params, "List[HighLevelILInstruction]"),
("dest_memory", self.dest_memory, "int"),
("src_memory", self.src_memory, "int"),
]
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILCmpE(HighLevelILComparisonBase):
pass
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILCmpNe(HighLevelILComparisonBase):
pass
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILCmpSlt(HighLevelILComparisonBase, Signed):
pass
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILCmpUlt(HighLevelILComparisonBase):
pass
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILCmpSle(HighLevelILComparisonBase, Signed):
pass
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILCmpUle(HighLevelILComparisonBase):
pass
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILCmpSge(HighLevelILComparisonBase, Signed):
pass
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILCmpUge(HighLevelILComparisonBase):
pass
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILCmpSgt(HighLevelILComparisonBase, Signed):
pass
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILCmpUgt(HighLevelILComparisonBase):
pass
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILTestBit(HighLevelILComparisonBase):
pass
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILBoolToInt(HighLevelILUnaryBase):
pass
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILAddOverflow(HighLevelILBinaryBase, Arithmetic):
pass
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILSyscall(HighLevelILInstruction, Syscall):
@property
def params(self) -> List[HighLevelILInstruction]:
return self._get_expr_list(0, 1)
@property
def detailed_operands(self) -> List[Tuple[str, HighLevelILOperandType, str]]:
return [
("params", self.params, "List[HighLevelILInstruction]"),
]
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILSyscallSsa(HighLevelILInstruction, Syscall, SSA):
@property
def params(self) -> List[HighLevelILInstruction]:
return self._get_expr_list(0, 1)
@property
def dest_memory(self) -> int:
return self._get_int(2)
@property
def src_memory(self) -> int:
return self._get_int(3)
@property
def detailed_operands(self) -> List[Tuple[str, HighLevelILOperandType, str]]:
return [
("params", self.params, "List[HighLevelILInstruction]"),
("dest_memory", self.dest_memory, "int"),
("src_memory", self.src_memory, "int"),
]
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILTailcall(HighLevelILInstruction, Tailcall):
@property
def dest(self) -> HighLevelILInstruction:
return self._get_expr(0)
@property
def params(self) -> List[HighLevelILInstruction]:
return self._get_expr_list(1, 2)
@property
def detailed_operands(self) -> List[Tuple[str, HighLevelILOperandType, str]]:
return [
("dest", self.dest, "HighLevelILInstruction"),
("params", self.params, "List[HighLevelILInstruction]"),
]
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILBp(HighLevelILInstruction, Terminal):
pass
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILTrap(HighLevelILInstruction, Terminal):
@property
def vector(self) -> int:
return self._get_int(0)
@property
def detailed_operands(self) -> List[Tuple[str, HighLevelILOperandType, str]]:
return [
("vector", self.vector, "int"),
]
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILIntrinsic(HighLevelILInstruction, Intrinsic):
@property
def intrinsic(self) -> 'lowlevelil.ILIntrinsic':
return self._get_intrinsic(0)
@property
def params(self) -> List[HighLevelILInstruction]:
return self._get_expr_list(1, 2)
@property
def detailed_operands(self) -> List[Tuple[str, HighLevelILOperandType, str]]:
return [
("intrinsic", self.intrinsic, "ILIntrinsic"),
("params", self.params, "List[HighLevelILInstruction]"),
]
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILIntrinsicSsa(HighLevelILInstruction, SSA):
@property
def intrinsic(self) -> 'lowlevelil.ILIntrinsic':
return self._get_intrinsic(0)
@property
def params(self) -> List[HighLevelILInstruction]:
return self._get_expr_list(1, 2)
@property
def dest_memory(self) -> int:
return self._get_int(3)
@property
def src_memory(self) -> int:
return self._get_int(4)
@property
def detailed_operands(self) -> List[Tuple[str, HighLevelILOperandType, str]]:
return [
("intrinsic", self.intrinsic, "ILIntrinsic"),
("params", self.params, "List[HighLevelILInstruction]"),
("dest_memory", self.dest_memory, "int"),
("src_memory", self.src_memory, "int"),
]
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILUndef(HighLevelILInstruction, Terminal):
pass
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILUnimpl(HighLevelILInstruction):
pass
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILUnimplMem(HighLevelILUnaryBase, Memory):
pass
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILFadd(HighLevelILBinaryBase, FloatingPoint):
pass
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILFsub(HighLevelILBinaryBase, FloatingPoint):
pass
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILFmul(HighLevelILBinaryBase, FloatingPoint):
pass
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILFdiv(HighLevelILBinaryBase, FloatingPoint):
pass
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILFsqrt(HighLevelILUnaryBase, FloatingPoint):
pass
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILFneg(HighLevelILUnaryBase, FloatingPoint):
pass
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILFabs(HighLevelILUnaryBase, FloatingPoint):
pass
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILFloatToInt(HighLevelILUnaryBase, FloatingPoint):
pass
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILIntToFloat(HighLevelILUnaryBase, FloatingPoint):
pass
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILFloatConv(HighLevelILUnaryBase, FloatingPoint):
pass
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILRoundToInt(HighLevelILUnaryBase, FloatingPoint):
pass
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILFloor(HighLevelILUnaryBase, FloatingPoint):
pass
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILCeil(HighLevelILUnaryBase, FloatingPoint):
pass
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILFtrunc(HighLevelILUnaryBase, FloatingPoint):
pass
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILFcmpE(HighLevelILComparisonBase, FloatingPoint):
pass
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILFcmpNe(HighLevelILComparisonBase, FloatingPoint):
pass
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILFcmpLt(HighLevelILComparisonBase, FloatingPoint):
pass
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILFcmpLe(HighLevelILComparisonBase, FloatingPoint):
pass
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILFcmpGe(HighLevelILComparisonBase, FloatingPoint):
pass
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILFcmpGt(HighLevelILComparisonBase, FloatingPoint):
pass
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILFcmpO(HighLevelILComparisonBase, FloatingPoint):
pass
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILFcmpUo(HighLevelILComparisonBase, FloatingPoint):
pass
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILAssert(HighLevelILInstruction):
@property
def src(self) -> variable.Variable:
return self._get_var(0)
@property
def constraint(self) -> variable.PossibleValueSet:
return self._get_constraint(1)
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILAssertSsa(HighLevelILInstruction, SSA):
@property
def src(self) -> 'mediumlevelil.SSAVariable':
return self._get_var_ssa(0, 1)
@property
def constraint(self) -> variable.PossibleValueSet:
return self._get_constraint(2)
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILForceVer(HighLevelILInstruction):
@property
def dest(self) -> variable.Variable:
return self._get_var(0)
@property
def src(self) -> variable.Variable:
return self._get_var(1)
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILForceVerSsa(HighLevelILInstruction, SSA):
@property
def dest(self) -> 'mediumlevelil.SSAVariable':
return self._get_var_ssa(0, 1)
@property
def src(self) -> 'mediumlevelil.SSAVariable':
return self._get_var_ssa(2, 3)
ILInstruction = {
HighLevelILOperation.HLIL_NOP: HighLevelILNop, # ,
HighLevelILOperation.HLIL_BLOCK: HighLevelILBlock, # ("body", "expr_list"),
HighLevelILOperation.HLIL_IF: HighLevelILIf, # ("condition", "expr"), ("true", "expr"), ("false", "expr"),
HighLevelILOperation.HLIL_WHILE: HighLevelILWhile, # ("condition", "expr"), ("body", "expr"),
HighLevelILOperation.HLIL_WHILE_SSA:
HighLevelILWhileSsa, # ("condition_phi", "expr"), ("condition", "expr"), ("body", "expr"),
HighLevelILOperation.HLIL_DO_WHILE: HighLevelILDoWhile, # ("body", "expr"), ("condition", "expr"),
HighLevelILOperation.HLIL_DO_WHILE_SSA:
HighLevelILDoWhileSsa, # ("body", "expr"), ("condition_phi", "expr"), ("condition", "expr"),
HighLevelILOperation.HLIL_FOR:
HighLevelILFor, # ("init", "expr"), ("condition", "expr"), ("update", "expr"), ("body", "expr"),
HighLevelILOperation.HLIL_FOR_SSA:
HighLevelILForSsa, # ("init", "expr"), ("condition_phi", "expr"), ("condition", "expr"), ("update", "expr"), ("body", "expr"),
HighLevelILOperation.HLIL_SWITCH:
HighLevelILSwitch, # ("condition", "expr"), ("default", "expr"), ("cases", "expr_list"),
HighLevelILOperation.HLIL_CASE: HighLevelILCase, # ("values", "expr_list"), ("body", "expr"),
HighLevelILOperation.HLIL_BREAK: HighLevelILBreak, # ,
HighLevelILOperation.HLIL_CONTINUE: HighLevelILContinue, # ,
HighLevelILOperation.HLIL_JUMP: HighLevelILJump, # ("dest", "expr"),
HighLevelILOperation.HLIL_RET: HighLevelILRet, # ("src", "expr_list"),
HighLevelILOperation.HLIL_NORET: HighLevelILNoret, # ,
HighLevelILOperation.HLIL_UNREACHABLE: HighLevelILUnreachable, # ,
HighLevelILOperation.HLIL_GOTO: HighLevelILGoto, # ("target", "label"),
HighLevelILOperation.HLIL_LABEL: HighLevelILLabel, # ("target", "label"),
HighLevelILOperation.HLIL_VAR_DECLARE: HighLevelILVarDeclare, # ("var", "var"),
HighLevelILOperation.HLIL_VAR_INIT: HighLevelILVarInit, # ("dest", "var"), ("src", "expr"),
HighLevelILOperation.HLIL_VAR_INIT_SSA: HighLevelILVarInitSsa, # ("dest", "var_ssa"), ("src", "expr"),
HighLevelILOperation.HLIL_ASSIGN: HighLevelILAssign, # ("dest", "expr"), ("src", "expr"),
HighLevelILOperation.HLIL_ASSIGN_UNPACK: HighLevelILAssignUnpack, # ("dest", "expr_list"), ("src", "expr"),
HighLevelILOperation.HLIL_ASSIGN_MEM_SSA:
HighLevelILAssignMemSsa, # ("dest", "expr"), ("dest_memory", "int"), ("src", "expr"), ("src_memory", "int"),
HighLevelILOperation.HLIL_ASSIGN_UNPACK_MEM_SSA:
HighLevelILAssignUnpackMemSsa, # ("dest", "expr_list"), ("dest_memory", "int"), ("src", "expr"), ("src_memory", "int"),
HighLevelILOperation.HLIL_VAR: HighLevelILVar, # ("var", "var"),
HighLevelILOperation.HLIL_VAR_SSA: HighLevelILVarSsa, # ("var", "var_ssa"),
HighLevelILOperation.HLIL_VAR_PHI: HighLevelILVarPhi, # ("dest", "var_ssa"), ("src", "var_ssa_list"),
HighLevelILOperation.HLIL_MEM_PHI: HighLevelILMemPhi, # ("dest", "int"), ("src", "int_list"),
HighLevelILOperation.HLIL_ARRAY_INDEX: HighLevelILArrayIndex, # ("src", "expr"), ("index", "expr"),
HighLevelILOperation.HLIL_ARRAY_INDEX_SSA:
HighLevelILArrayIndexSsa, # ("src", "expr"), ("src_memory", "int"), ("index", "expr"),
HighLevelILOperation.HLIL_SPLIT: HighLevelILSplit, # ("high", "expr"), ("low", "expr"),
HighLevelILOperation.HLIL_DEREF: HighLevelILDeref, # ("src", "expr"),
HighLevelILOperation.HLIL_STRUCT_FIELD:
HighLevelILStructField, # ("src", "expr"), ("offset", "int"), ("member_index", "member_index"),
HighLevelILOperation.HLIL_DEREF_FIELD:
HighLevelILDerefField, # ("src", "expr"), ("offset", "int"), ("member_index", "member_index"),
HighLevelILOperation.HLIL_DEREF_SSA: HighLevelILDerefSsa, # ("src", "expr"), ("src_memory", "int"),
HighLevelILOperation.HLIL_DEREF_FIELD_SSA:
HighLevelILDerefFieldSsa, # ("src", "expr"), ("src_memory", "int"), ("offset", "int"), ("member_index", "member_index"),
HighLevelILOperation.HLIL_ADDRESS_OF: HighLevelILAddressOf, # ("src", "expr"),
HighLevelILOperation.HLIL_PASS_BY_REF: HighLevelILPassByRef, # ("src", "expr"),
HighLevelILOperation.HLIL_RETURN_BY_REF: HighLevelILReturnByRef, # ("src", "expr"),
HighLevelILOperation.HLIL_CONST: HighLevelILConst, # ("constant", "int"),
HighLevelILOperation.HLIL_CONST_PTR: HighLevelILConstPtr, # ("constant", "int"),
HighLevelILOperation.HLIL_EXTERN_PTR: HighLevelILExternPtr, # ("constant", "int"), ("offset", "int"),
HighLevelILOperation.HLIL_FLOAT_CONST: HighLevelILFloatConst, # ("constant", "float"),
HighLevelILOperation.HLIL_IMPORT: HighLevelILImport, # ("constant", "int"),
HighLevelILOperation.HLIL_CONST_DATA: HighLevelILConstData, # [("constant", "ConstantData")],
HighLevelILOperation.HLIL_ADD: HighLevelILAdd, # ("left", "expr"), ("right", "expr"),
HighLevelILOperation.HLIL_ADC: HighLevelILAdc, # ("left", "expr"), ("right", "expr"), ("carry", "expr"),
HighLevelILOperation.HLIL_SUB: HighLevelILSub, # ("left", "expr"), ("right", "expr"),
HighLevelILOperation.HLIL_SBB: HighLevelILSbb, # ("left", "expr"), ("right", "expr"), ("carry", "expr"),
HighLevelILOperation.HLIL_AND: HighLevelILAnd, # ("left", "expr"), ("right", "expr"),
HighLevelILOperation.HLIL_OR: HighLevelILOr, # ("left", "expr"), ("right", "expr"),
HighLevelILOperation.HLIL_XOR: HighLevelILXor, # ("left", "expr"), ("right", "expr"),
HighLevelILOperation.HLIL_LSL: HighLevelILLsl, # ("left", "expr"), ("right", "expr"),
HighLevelILOperation.HLIL_LSR: HighLevelILLsr, # ("left", "expr"), ("right", "expr"),
HighLevelILOperation.HLIL_ASR: HighLevelILAsr, # ("left", "expr"), ("right", "expr"),
HighLevelILOperation.HLIL_ROL: HighLevelILRol, # ("left", "expr"), ("right", "expr"),
HighLevelILOperation.HLIL_RLC: HighLevelILRlc, # ("left", "expr"), ("right", "expr"), ("carry", "expr"),
HighLevelILOperation.HLIL_ROR: HighLevelILRor, # ("left", "expr"), ("right", "expr"),
HighLevelILOperation.HLIL_RRC: HighLevelILRrc, # ("left", "expr"), ("right", "expr"), ("carry", "expr"),
HighLevelILOperation.HLIL_MUL: HighLevelILMul, # ("left", "expr"), ("right", "expr"),
HighLevelILOperation.HLIL_MULU_DP: HighLevelILMuluDp, # ("left", "expr"), ("right", "expr"),
HighLevelILOperation.HLIL_MULS_DP: HighLevelILMulsDp, # ("left", "expr"), ("right", "expr"),
HighLevelILOperation.HLIL_DIVU: HighLevelILDivu, # ("left", "expr"), ("right", "expr"),
HighLevelILOperation.HLIL_DIVU_DP: HighLevelILDivuDp, # ("left", "expr"), ("right", "expr"),
HighLevelILOperation.HLIL_DIVS: HighLevelILDivs, # ("left", "expr"), ("right", "expr"),
HighLevelILOperation.HLIL_DIVS_DP: HighLevelILDivsDp, # ("left", "expr"), ("right", "expr"),
HighLevelILOperation.HLIL_MODU: HighLevelILModu, # ("left", "expr"), ("right", "expr"),
HighLevelILOperation.HLIL_MODU_DP: HighLevelILModuDp, # ("left", "expr"), ("right", "expr"),
HighLevelILOperation.HLIL_MODS: HighLevelILMods, # ("left", "expr"), ("right", "expr"),
HighLevelILOperation.HLIL_MODS_DP: HighLevelILModsDp, # ("left", "expr"), ("right", "expr"),
HighLevelILOperation.HLIL_NEG: HighLevelILNeg, # ("src", "expr"),
HighLevelILOperation.HLIL_NOT: HighLevelILNot, # ("src", "expr"),
HighLevelILOperation.HLIL_BSWAP: HighLevelILBswap, # ("src", "expr"),
HighLevelILOperation.HLIL_POPCNT: HighLevelILPopcnt, # ("src", "expr"),
HighLevelILOperation.HLIL_CLZ: HighLevelILClz, # ("src", "expr"),
HighLevelILOperation.HLIL_CTZ: HighLevelILCtz, # ("src", "expr"),
HighLevelILOperation.HLIL_RBIT: HighLevelILRbit, # ("src", "expr"),
HighLevelILOperation.HLIL_CLS: HighLevelILCls, # ("src", "expr"),
HighLevelILOperation.HLIL_MINS: HighLevelILMins, # ("left", "expr"), ("right", "expr"),
HighLevelILOperation.HLIL_MAXS: HighLevelILMaxs, # ("left", "expr"), ("right", "expr"),
HighLevelILOperation.HLIL_MINU: HighLevelILMinu, # ("left", "expr"), ("right", "expr"),
HighLevelILOperation.HLIL_MAXU: HighLevelILMaxu, # ("left", "expr"), ("right", "expr"),
HighLevelILOperation.HLIL_ABS: HighLevelILAbs, # ("src", "expr"),
HighLevelILOperation.HLIL_SX: HighLevelILSx, # ("src", "expr"),
HighLevelILOperation.HLIL_ZX: HighLevelILZx, # ("src", "expr"),
HighLevelILOperation.HLIL_LOW_PART: HighLevelILLowPart, # ("src", "expr"),
HighLevelILOperation.HLIL_CALL: HighLevelILCall, # ("dest", "expr"), ("params", "expr_list"),
HighLevelILOperation.HLIL_CALL_SSA:
HighLevelILCallSsa, # ("dest", "expr"), ("params", "expr_list"), ("dest_memory", "int"), ("src_memory", "int"),
HighLevelILOperation.HLIL_CMP_E: HighLevelILCmpE, # ("left", "expr"), ("right", "expr"),
HighLevelILOperation.HLIL_CMP_NE: HighLevelILCmpNe, # ("left", "expr"), ("right", "expr"),
HighLevelILOperation.HLIL_CMP_SLT: HighLevelILCmpSlt, # ("left", "expr"), ("right", "expr"),
HighLevelILOperation.HLIL_CMP_ULT: HighLevelILCmpUlt, # ("left", "expr"), ("right", "expr"),
HighLevelILOperation.HLIL_CMP_SLE: HighLevelILCmpSle, # ("left", "expr"), ("right", "expr"),
HighLevelILOperation.HLIL_CMP_ULE: HighLevelILCmpUle, # ("left", "expr"), ("right", "expr"),
HighLevelILOperation.HLIL_CMP_SGE: HighLevelILCmpSge, # ("left", "expr"), ("right", "expr"),
HighLevelILOperation.HLIL_CMP_UGE: HighLevelILCmpUge, # ("left", "expr"), ("right", "expr"),
HighLevelILOperation.HLIL_CMP_SGT: HighLevelILCmpSgt, # ("left", "expr"), ("right", "expr"),
HighLevelILOperation.HLIL_CMP_UGT: HighLevelILCmpUgt, # ("left", "expr"), ("right", "expr"),
HighLevelILOperation.HLIL_TEST_BIT: HighLevelILTestBit, # ("left", "expr"), ("right", "expr"),
HighLevelILOperation.HLIL_BOOL_TO_INT: HighLevelILBoolToInt, # ("src", "expr"),
HighLevelILOperation.HLIL_ADD_OVERFLOW: HighLevelILAddOverflow, # ("left", "expr"), ("right", "expr"),
HighLevelILOperation.HLIL_SYSCALL: HighLevelILSyscall, # ("params", "expr_list"),
HighLevelILOperation.HLIL_SYSCALL_SSA:
HighLevelILSyscallSsa, # ("params", "expr_list"), ("dest_memory", "int"), ("src_memory", "int"),
HighLevelILOperation.HLIL_TAILCALL: HighLevelILTailcall, # ("dest", "expr"), ("params", "expr_list"),
HighLevelILOperation.HLIL_BP: HighLevelILBp, # ,
HighLevelILOperation.HLIL_TRAP: HighLevelILTrap, # ("vector", "int"),
HighLevelILOperation.HLIL_INTRINSIC: HighLevelILIntrinsic, # ("intrinsic", "intrinsic"), ("params", "expr_list"),
HighLevelILOperation.HLIL_INTRINSIC_SSA:
HighLevelILIntrinsicSsa, # ("intrinsic", "intrinsic"), ("params", "expr_list"), ("dest_memory", "int"), ("src_memory", "int"),
HighLevelILOperation.HLIL_UNDEF: HighLevelILUndef, # ,
HighLevelILOperation.HLIL_UNIMPL: HighLevelILUnimpl, # ,
HighLevelILOperation.HLIL_UNIMPL_MEM: HighLevelILUnimplMem, # ("src", "expr"),
HighLevelILOperation.HLIL_FADD: HighLevelILFadd, # ("left", "expr"), ("right", "expr"),
HighLevelILOperation.HLIL_FSUB: HighLevelILFsub, # ("left", "expr"), ("right", "expr"),
HighLevelILOperation.HLIL_FMUL: HighLevelILFmul, # ("left", "expr"), ("right", "expr"),
HighLevelILOperation.HLIL_FDIV: HighLevelILFdiv, # ("left", "expr"), ("right", "expr"),
HighLevelILOperation.HLIL_FSQRT: HighLevelILFsqrt, # ("src", "expr"),
HighLevelILOperation.HLIL_FNEG: HighLevelILFneg, # ("src", "expr"),
HighLevelILOperation.HLIL_FABS: HighLevelILFabs, # ("src", "expr"),
HighLevelILOperation.HLIL_FLOAT_TO_INT: HighLevelILFloatToInt, # ("src", "expr"),
HighLevelILOperation.HLIL_INT_TO_FLOAT: HighLevelILIntToFloat, # ("src", "expr"),
HighLevelILOperation.HLIL_FLOAT_CONV: HighLevelILFloatConv, # ("src", "expr"),
HighLevelILOperation.HLIL_ROUND_TO_INT: HighLevelILRoundToInt, # ("src", "expr"),
HighLevelILOperation.HLIL_FLOOR: HighLevelILFloor, # ("src", "expr"),
HighLevelILOperation.HLIL_CEIL: HighLevelILCeil, # ("src", "expr"),
HighLevelILOperation.HLIL_FTRUNC: HighLevelILFtrunc, # ("src", "expr"),
HighLevelILOperation.HLIL_FCMP_E: HighLevelILFcmpE, # ("left", "expr"), ("right", "expr"),
HighLevelILOperation.HLIL_FCMP_NE: HighLevelILFcmpNe, # ("left", "expr"), ("right", "expr"),
HighLevelILOperation.HLIL_FCMP_LT: HighLevelILFcmpLt, # ("left", "expr"), ("right", "expr"),
HighLevelILOperation.HLIL_FCMP_LE: HighLevelILFcmpLe, # ("left", "expr"), ("right", "expr"),
HighLevelILOperation.HLIL_FCMP_GE: HighLevelILFcmpGe, # ("left", "expr"), ("right", "expr"),
HighLevelILOperation.HLIL_FCMP_GT: HighLevelILFcmpGt, # ("left", "expr"), ("right", "expr"),
HighLevelILOperation.HLIL_FCMP_O: HighLevelILFcmpO, # ("left", "expr"), ("right", "expr"),
HighLevelILOperation.HLIL_FCMP_UO: HighLevelILFcmpUo, # ("left", "expr"), ("right", "expr"),
HighLevelILOperation.HLIL_ASSERT: HighLevelILAssert,
HighLevelILOperation.HLIL_ASSERT_SSA: HighLevelILAssertSsa,
HighLevelILOperation.HLIL_FORCE_VER: HighLevelILForceVer,
HighLevelILOperation.HLIL_FORCE_VER_SSA: HighLevelILForceVerSsa,
}
class HighLevelILFunction:
"""
``class HighLevelILFunction`` contains the a HighLevelILInstruction object that makes up the abstract syntax tree of
a function.
"""
def __init__(
self, arch: Optional['architecture.Architecture'] = None, handle: Optional[core.BNHighLevelILFunction] = None,
source_func: Optional['function.Function'] = None
):
self._arch = arch
self._source_function = source_func
if handle is not None:
HLILHandle = ctypes.POINTER(core.BNHighLevelILFunction)
_handle = ctypes.cast(handle, HLILHandle)
if self._source_function is None:
self._source_function = function.Function(handle=core.BNGetHighLevelILOwnerFunction(_handle))
if self._arch is None:
self._arch = self._source_function.arch
else:
if self._source_function is None:
raise ValueError("IL functions must be created with an associated function")
if self._arch is None:
self._arch = self._source_function.arch
if self._arch is None:
raise ValueError("IL functions must be created with an associated Architecture")
func_handle = self._source_function.handle
_handle = core.BNCreateHighLevelILFunction(self._arch.handle, func_handle)
assert self._source_function is not None
assert self._arch is not None
assert _handle is not None
self.handle = _handle
def __del__(self):
if core is not None:
core.BNFreeHighLevelILFunction(self.handle)
def __repr__(self):
arch = self.source_function.arch
form = ""
if self.il_form == FunctionGraphType.HighLevelILSSAFormFunctionGraph:
form += " ssa form"
if arch:
return f"<HighLevelILFunction{form}: {arch.name}@{self.source_function.start:#x}>"
else:
return f"<HighLevelILFunction{form}: {self.source_function.start:#x}>"
def __eq__(self, other):
if not isinstance(other, self.__class__):
return NotImplemented
return ctypes.addressof(self.handle.contents) == ctypes.addressof(other.handle.contents)
def __ne__(self, other):
if not isinstance(other, self.__class__):
return NotImplemented
return not (self == other)
def __hash__(self):
return hash(('HLIL', self._source_function))
def __len__(self):
return int(core.BNGetHighLevelILInstructionCount(self.handle))
def __getitem__(self, i: int) -> HighLevelILInstruction:
if isinstance(i, slice) or isinstance(i, tuple):
raise IndexError("expected integer index")
if i < -len(self) or i >= len(self):
raise IndexError("index out of range")
if i < 0:
i = len(self) + i
return HighLevelILInstruction.create(
self, ExpressionIndex(core.BNGetHighLevelILIndexForInstruction(self.handle, i)), False, InstructionIndex(i)
)
def __setitem__(self, i, j):
raise IndexError("instruction modification not implemented")
def __iter__(self) -> Generator['HighLevelILBasicBlock', None, None]:
count = ctypes.c_ulonglong()
blocks = core.BNGetHighLevelILBasicBlockList(self.handle, count)
assert blocks is not None, "core.BNGetHighLevelILBasicBlockList returned None"
view = None
if self._source_function is not None:
view = self._source_function.view
try:
for i in range(0, count.value):
core_block = core.BNNewBasicBlockReference(blocks[i])
assert core_block is not None, "core.BNNewBasicBlockReference returned None"
yield HighLevelILBasicBlock(core_block, self, view)
finally:
core.BNFreeBasicBlockList(blocks, count.value)
def __str__(self) -> str:
return str(self.root)
@property
def current_address(self) -> int:
"""Current IL Address (read/write)"""
return core.BNHighLevelILGetCurrentAddress(self.handle)
@current_address.setter
def current_address(self, value: int) -> None:
core.BNHighLevelILSetCurrentAddress(self.handle, self.arch.handle, value)
def set_current_address(self, value: int, arch: Optional['architecture.Architecture'] = None) -> None:
if arch is None:
arch = self.arch
core.BNHighLevelILSetCurrentAddress(self.handle, arch.handle, value)
@property
def root(self) -> Optional[HighLevelILInstruction]:
"""Root of the abstract syntax tree"""
expr_index = core.BNGetHighLevelILRootExpr(self.handle)
if expr_index >= core.BNGetHighLevelILExprCount(self.handle):
return None
return HighLevelILInstruction.create(self, ExpressionIndex(expr_index))
@root.setter
def root(self, value: HighLevelILInstruction) -> None:
core.BNSetHighLevelILRootExpr(self.handle, value.expr_index)
def _basic_block_list(self):
count = ctypes.c_ulonglong()
blocks = core.BNGetHighLevelILBasicBlockList(self.handle, count)
assert blocks is not None, "core.BNGetHighLevelILBasicBlockList returned None"
return count, blocks
def _instantiate_block(self, handle):
return HighLevelILBasicBlock(handle, self, self.view)
@property
def basic_blocks(self) -> 'function.HighLevelILBasicBlockList':
return function.HighLevelILBasicBlockList(self)
def get_basic_block_at(self, index: int) -> Optional['HighLevelILBasicBlock']:
"""
``get_basic_block_at`` returns the BasicBlock at the given HLIL instruction ``index``.
:param int index: Index of the HLIL instruction of the BasicBlock to retrieve.
:Example:
>>> current_il_function.get_basic_block_at(current_il_index)
<llil block: x86@19-26>
"""
block = core.BNGetHighLevelILBasicBlockForInstruction(self.handle, index)
if not block:
return None
view = None
if self._source_function is not None:
view = self._source_function.view
return HighLevelILBasicBlock(block, self, view)
def traverse(self, cb: Callable[['HighLevelILInstruction', Any], Any], *args: Any, **kwargs: Any) -> Iterator[Any]:
"""
``traverse`` iterates through all the instructions in the HighLevelILFunction and calls the callback function for
each instruction and sub-instruction. See the `Developer Docs <https://docs.binary.ninja/dev/concepts.html#walking-ils>`_ for more examples.
:param Callable[[HighLevelILInstruction, Any], Any] cb: The callback function to call for each node in the HighLevelILInstruction
:param Any args: Custom user-defined arguments
:param Any kwargs: Custom user-defined keyword arguments
:return: An iterator of the results of the callback function
:rtype: Iterator[Any]
:Example:
>>> # find all calls to memcpy where the third parameter is not a constant
>>> def find_non_constant_memcpy(i, target) -> HighLevelILInstruction:
... match i:
... case Localcall(dest=Constant(constant=c), params=[_, _, p]) if c == target and not isinstance(p, Constant):
... return i
>>> target_address = bv.get_symbol_by_raw_name('_memcpy').address
>>> for result in current_il_function.traverse(find_non_constant_memcpy, target_address):
... print(f"Found suspicious memcpy: {repr(i)}")
"""
root = self.root
if root is None:
raise ValueError("HighLevelILFunction has no root")
if not isinstance(root, HighLevelILBlock):
root = [root]
for instr in root:
yield from instr.traverse(cb, *args, shallow=False, **kwargs)
@deprecation.deprecated(deprecated_in="4.0.4907", details="Use :py:func:`HighLevelILFunction.traverse` instead.")
def visit(self, cb: HighLevelILVisitorCallback) -> bool:
"""
Iterates over all the instructions in the function and calls the callback function
for each instruction and each sub-instruction.
:param HighLevelILVisitorCallback cb: Callback function that takes the name of the operand, the operand, operand type, and parent instruction
:return: True if all instructions were visited, False if the callback function returned False.
"""
for instr in self.instructions:
if not instr.visit(cb):
return False
return True
@deprecation.deprecated(deprecated_in="4.0.4907", details="Use :py:func:`HighLevelILFunction.traverse` instead.")
def visit_all(self, cb: HighLevelILVisitorCallback) -> bool:
"""
Iterates over all the instructions in the function and calls the callback function for each instruction and their operands.
:param HighLevelILVisitorCallback cb: Callback function that takes the name of the operand, the operand, operand type, and parent instruction
:return: True if all instructions were visited, False if the callback function returned False.
"""
for instr in self.instructions:
if not instr.visit_all(cb):
return False
return True
@deprecation.deprecated(deprecated_in="4.0.4907", details="Use :py:func:`HighLevelILFunction.traverse` instead.")
def visit_operands(self, cb: HighLevelILVisitorCallback) -> bool:
"""
Iterates over all the instructions in the function and calls the callback function for each operand and
the operands of each sub-instruction.
:param HighLevelILVisitorCallback cb: Callback function that takes the name of the operand, the operand, operand type, and parent instruction
:return: True if all instructions were visited, False if the callback function returned False.
"""
for instr in self.instructions:
if not instr.visit_operands(cb):
return False
return True
@property
def instructions(self) -> Generator[HighLevelILInstruction, None, None]:
"""A generator of hlil instructions of the current function"""
for block in self.basic_blocks:
yield from block
@property
def ssa_form(self) -> 'HighLevelILFunction':
"""High level IL in SSA form (read-only)"""
result = core.BNGetHighLevelILSSAForm(self.handle)
assert result is not None, "core.BNGetHighLevelILSSAForm returned None"
return HighLevelILFunction(self._arch, result, self._source_function)
@property
def non_ssa_form(self) -> Optional['HighLevelILFunction']:
"""High level IL in non-SSA (default) form (read-only)"""
result = core.BNGetHighLevelILNonSSAForm(self.handle)
if not result:
return None
return HighLevelILFunction(self._arch, result, self._source_function)
@property
def arch(self) -> 'architecture.Architecture':
assert self._arch is not None
return self._arch
@property
def view(self) -> 'binaryview.BinaryView':
return self.source_function.view
@property
def source_function(self) -> 'function.Function':
assert self._source_function is not None
return self._source_function
@source_function.setter
def source_function(self, value: 'function.Function') -> None:
self._source_function = value
@property
def medium_level_il(self) -> Optional['mediumlevelil.MediumLevelILFunction']:
"""Medium level IL for this function"""
result = core.BNGetMediumLevelILForHighLevelILFunction(self.handle)
if not result:
return None
return mediumlevelil.MediumLevelILFunction(self._arch, result, self._source_function)
@property
def mlil(self) -> Optional['mediumlevelil.MediumLevelILFunction']:
"""Alias for medium_level_il"""
return self.medium_level_il
def get_ssa_instruction_index(self, instr: int) -> int:
return core.BNGetHighLevelILSSAInstructionIndex(self.handle, instr)
def get_non_ssa_instruction_index(self, instr: int) -> int:
return core.BNGetHighLevelILNonSSAInstructionIndex(self.handle, instr)
def get_ssa_var_definition(self, ssa_var: Union['mediumlevelil.SSAVariable', HighLevelILVarSsa]) -> Optional[HighLevelILInstruction]:
"""
Gets the instruction that contains the given SSA variable's definition.
Since SSA variables can only be defined once, this will return the single instruction where that occurs.
For SSA variable version 0s, which don't have definitions, this will return None instead.
"""
if isinstance(ssa_var, HighLevelILVarSsa):
ssa_var = ssa_var.var
if not isinstance(ssa_var, mediumlevelil.SSAVariable):
raise ValueError("Expected SSAVariable")
var_data = ssa_var.var.to_BNVariable()
result = core.BNGetHighLevelILSSAVarDefinition(self.handle, var_data, ssa_var.version)
if result >= core.BNGetHighLevelILExprCount(self.handle):
return None
return HighLevelILInstruction.create(self, ExpressionIndex(result))
def get_ssa_memory_definition(self, version: int) -> Optional[HighLevelILInstruction]:
result = core.BNGetHighLevelILSSAMemoryDefinition(self.handle, version)
if result >= core.BNGetHighLevelILExprCount(self.handle):
return None
return HighLevelILInstruction.create(self, ExpressionIndex(result))
def get_ssa_var_uses(self, ssa_var: Union['mediumlevelil.SSAVariable', HighLevelILVarSsa]) -> List[HighLevelILInstruction]:
"""
Gets all the instructions that use the given SSA variable.
"""
if isinstance(ssa_var, HighLevelILVarSsa):
ssa_var = ssa_var.var
if not isinstance(ssa_var, mediumlevelil.SSAVariable):
raise ValueError("Expected SSAVariable")
count = ctypes.c_ulonglong()
var_data = ssa_var.var.to_BNVariable()
instrs = core.BNGetHighLevelILSSAVarUses(self.handle, var_data, ssa_var.version, count)
assert instrs is not None, "core.BNGetHighLevelILSSAVarUses returned None"
result = []
for i in range(0, count.value):
result.append(HighLevelILInstruction.create(self, instrs[i]))
core.BNFreeILInstructionList(instrs)
return result
def get_ssa_memory_uses(self, version: int) -> List[HighLevelILInstruction]:
count = ctypes.c_ulonglong()
instrs = core.BNGetHighLevelILSSAMemoryUses(self.handle, version, count)
assert instrs is not None, "core.BNGetHighLevelILSSAMemoryUses returned None"
result = []
for i in range(0, count.value):
result.append(HighLevelILInstruction.create(self, instrs[i]))
core.BNFreeILInstructionList(instrs)
return result
def is_ssa_var_live(self, ssa_var: 'mediumlevelil.SSAVariable') -> bool:
"""
``is_ssa_var_live`` determines if ``ssa_var`` is live at any point in the function
:param SSAVariable ssa_var: the SSA variable to query
:return: whether the variable is live at any point in the function
:rtype: bool
"""
var_data = ssa_var.var.to_BNVariable()
return core.BNIsHighLevelILSSAVarLive(self.handle, var_data, ssa_var.version)
def is_var_live_at(self, var: 'variable.Variable', instr: InstructionIndex) -> bool:
"""
``is_var_live_at`` determines if ``var`` is live at a given point in the function
"""
return core.BNIsHighLevelILVarLiveAt(self.handle, var.to_BNVariable(), instr)
def is_ssa_var_live_at(self, ssa_var: 'mediumlevelil.SSAVariable', instr: InstructionIndex) -> bool:
"""
``is_ssa_var_live_at`` determines if ``ssa_var`` is live at a given point in the function; counts phi's as uses
"""
return core.BNIsHighLevelILSSAVarLiveAt(self.handle, ssa_var.var.to_BNVariable(), ssa_var.version, instr)
def get_var_definitions(self, var: 'variable.Variable') -> List[HighLevelILInstruction]:
count = ctypes.c_ulonglong()
var_data = var.to_BNVariable()
instrs = core.BNGetHighLevelILVariableDefinitions(self.handle, var_data, count)
assert instrs is not None, "core.BNGetHighLevelILVariableDefinitions returned None"
result = []
for i in range(0, count.value):
result.append(HighLevelILInstruction.create(self, instrs[i]))
core.BNFreeILInstructionList(instrs)
return result
def get_var_uses(self, var: 'variable.Variable') -> List[HighLevelILInstruction]:
count = ctypes.c_ulonglong()
var_data = var.to_BNVariable()
instrs = core.BNGetHighLevelILVariableUses(self.handle, var_data, count)
assert instrs is not None, "core.BNGetHighLevelILVariableUses returned None"
result = []
for i in range(0, count.value):
result.append(HighLevelILInstruction.create(self, instrs[i]))
core.BNFreeILInstructionList(instrs)
return result
def expr(
self, operation: Union[str, HighLevelILOperation], a: int = 0, b: int = 0, c: int = 0, d: int = 0, e: int = 0,
size: int = 0,
source_location: Optional['ILSourceLocation'] = None
) -> ExpressionIndex:
if isinstance(operation, str):
operation_value = HighLevelILOperation[operation]
else:
assert isinstance(operation, HighLevelILOperation)
operation_value = operation.value
if source_location is not None:
return ExpressionIndex(core.BNHighLevelILAddExprWithLocation(
self.handle,
operation_value,
source_location.address,
source_location.source_operand,
size,
a,
b,
c,
d,
e
))
else:
return ExpressionIndex(core.BNHighLevelILAddExpr(self.handle, operation_value, size, a, b, c, d, e))
def get_expr_count(self) -> int:
"""
``get_expr_count`` gives a the total number of expressions in this IL function
You can use this to enumerate all expressions in conjunction with :py:func:`get_expr`
.. warning :: Not all IL expressions are valid, even if their index is within the bounds of the function,
they might not be used by the function and might not contain properly structured data.
:return: The number of expressions in the function
"""
return core.BNGetHighLevelILExprCount(self.handle)
def get_expr(self, index: ExpressionIndex, as_ast: bool = True) -> Optional[HighLevelILInstruction]:
"""
``get_expr`` retrieves the IL expression at a given expression index in the function.
.. warning :: Not all IL expressions are valid, even if their index is within the bounds of the function,
they might not be used by the function and might not contain properly structured data.
:param index: Index of desired expression in function
:param as_ast: Whether to return the expression as a full AST or a single instruction (defaults to AST)
:return: A HighLevelILInstruction object for the expression, if it exists. Otherwise, None
"""
if index >= self.get_expr_count():
return None
return HighLevelILInstruction.create(self, index, as_ast)
def copy_expr(self, original: HighLevelILInstruction) -> ExpressionIndex:
"""
``copy_expr`` adds an expression to the function which is equivalent to the given expression
:param HighLevelILInstruction original: the original IL Instruction you want to copy
:return: The index of the newly copied expression
"""
return self.expr(
original.operation,
original.raw_operands[0],
original.raw_operands[1],
original.raw_operands[2],
original.raw_operands[3],
original.raw_operands[4],
original.size,
original.source_location
)
def replace_expr(self, original: InstructionOrExpression, new: InstructionOrExpression) -> None:
"""
``replace_expr`` allows modification of HLIL expressions
:param ExpressionIndex original: the ExpressionIndex to replace (may also be an expression index)
:param ExpressionIndex new: the ExpressionIndex to add to the current HighLevelILFunction (may also be an expression index)
:rtype: None
"""
if isinstance(original, HighLevelILInstruction):
original = original.expr_index
elif isinstance(original, int):
original = ExpressionIndex(original)
if isinstance(new, HighLevelILInstruction):
new = new.expr_index
elif isinstance(new, int):
new = ExpressionIndex(new)
core.BNReplaceHighLevelILExpr(self.handle, original, new)
def set_expr_attributes(self, expr: InstructionOrExpression, value: ILInstructionAttributeSet):
"""
``set_expr_attributes`` allows modification of instruction attributes but ONLY during lifting.
.. warning:: This function should ONLY be called as a part of a lifter. It will otherwise not do anything useful as there's no way to trigger re-analysis of IL levels at this time.
:param ExpressionIndex expr: the ExpressionIndex to replace (may also be an expression index)
:param set(ILInstructionAttribute) value: the set of attributes to place on the instruction
:rtype: None
"""
if isinstance(expr, HighLevelILInstruction):
expr = expr.expr_index
elif isinstance(expr, int):
expr = ExpressionIndex(expr)
result = 0
for flag in value:
result |= flag.value
core.BNSetHighLevelILExprAttributes(self.handle, expr, result)
def set_derived_string_reference_for_expr(self, expr: InstructionOrExpression, str: 'binaryview.DerivedString'):
if isinstance(expr, HighLevelILInstruction):
expr = expr.expr_index
elif isinstance(expr, int):
expr = ExpressionIndex(expr)
str_obj = str._to_core_struct(False)
core.BNSetHighLevelILDerivedStringReferenceForExpr(self.handle, expr, str_obj)
def remove_derived_string_reference_for_expr(self, expr: InstructionOrExpression):
if isinstance(expr, HighLevelILInstruction):
expr = expr.expr_index
elif isinstance(expr, int):
expr = ExpressionIndex(expr)
core.BNRemoveHighLevelILDerivedStringReferenceForExpr(self.handle, expr)
def nop(self, loc: Optional['ILSourceLocation'] = None) -> ExpressionIndex:
"""
``nop`` no operation, this instruction does nothing
:param ILSourceLocation loc: Location of expression
:return: The no operation expression
:rtype: ExpressionIndex
"""
return self.expr(HighLevelILOperation.HLIL_NOP, source_location=loc)
def block(
self, exprs: List[ExpressionIndex], loc: Optional['ILSourceLocation'] = None
) -> ExpressionIndex:
"""
``block`` a block expression containing multiple child expressions
:param List[ExpressionIndex] exprs: child expressions in the block
:param ILSourceLocation loc: location of returned expression
:return: The expression `` { exprs... } ``
:rtype: ExpressionIndex
"""
return self.expr(HighLevelILOperation.HLIL_BLOCK, len(exprs), self.add_operand_list(exprs), source_location=loc)
def if_expr(
self, condition: ExpressionIndex, true_expr: ExpressionIndex, false_expr: ExpressionIndex,
loc: Optional['ILSourceLocation'] = None
) -> ExpressionIndex:
"""
``if_expr`` an if-statement expression with a condition and true/false branches.
An ``else`` statement is included if the false_expr is not a NOP expression
:param ExpressionIndex condition: expression for the condition to test
:param ExpressionIndex true_expr: expression for the true branch
:param ExpressionIndex false_expr: expression for the false branch
:param ILSourceLocation loc: location of returned expression
:return: The expression ``if (condition) { true_expr } else { false_expr }``
:rtype: ExpressionIndex
"""
return self.expr(HighLevelILOperation.HLIL_IF, condition, true_expr, false_expr, source_location=loc)
def while_expr(
self, condition: ExpressionIndex, loop_expr: ExpressionIndex, loc: Optional['ILSourceLocation'] = None
) -> ExpressionIndex:
"""
``while_expr`` a while-loop expression with a condition and loop body.
:param ExpressionIndex condition: expression for the loop condition
:param ExpressionIndex loop_expr: expression for the loop body
:param ILSourceLocation loc: location of returned expression
:return: The expression ``while (condition) { loop_expr }``
:rtype: ExpressionIndex
"""
return self.expr(HighLevelILOperation.HLIL_WHILE, condition, loop_expr, source_location=loc)
def do_while_expr(
self, condition: ExpressionIndex, loop_expr: ExpressionIndex, loc: Optional['ILSourceLocation'] = None
) -> ExpressionIndex:
"""
``do_while_expr`` a do-while-loop expression with a condition and loop body.
:param ExpressionIndex condition: expression for the loop condition
:param ExpressionIndex loop_expr: expression for the loop body
:param ILSourceLocation loc: location of returned expression
:return: The expression ``do { loop_expr } while (condition)``
:rtype: ExpressionIndex
"""
return self.expr(HighLevelILOperation.HLIL_DO_WHILE, condition, loop_expr, source_location=loc)
def for_expr(
self, init_expr: ExpressionIndex, condition: ExpressionIndex, update_expr: ExpressionIndex, loop_expr: ExpressionIndex,
loc: Optional['ILSourceLocation'] = None
) -> ExpressionIndex:
"""
``for-expr`` a for-loop expression with an initializer, condition, updater, and loop body.
:param ExpressionIndex init_expr: expression for the loop initializer
:param ExpressionIndex condition: expression for the loop condition
:param ExpressionIndex update_expr: expression for the loop updater
:param ExpressionIndex loop_expr: expression for the loop body
:param ILSourceLocation loc: location of returned expression
:return: The expression ``for (init_expr ; condition ; update_expr) { loop_expr }``
:rtype: ExpressionIndex
"""
return self.expr(HighLevelILOperation.HLIL_FOR, init_expr, condition, update_expr, loop_expr, source_location=loc)
def switch(
self, condition: ExpressionIndex, default_expr: ExpressionIndex, cases: List[ExpressionIndex],
loc: Optional['ILSourceLocation'] = None
) -> ExpressionIndex:
"""
``switch`` a switch expression with a condition, cases, and default case
:param ExpressionIndex condition: expression for the switch condition
:param ExpressionIndex default_expr: expression for the default branch
:param List[ExpressionIndex] cases: list of expressions for the switch cases
:param ILSourceLocation loc: location of returned expression
:return: The expression ``switch (condition) { cases...: ... default: default_expr }``
:rtype: ExpressionIndex
"""
return self.expr(HighLevelILOperation.HLIL_SWITCH, condition, default_expr, len(cases), self.add_operand_list(cases), source_location=loc)
def case(
self, values: List[ExpressionIndex], expr: ExpressionIndex, loc: Optional['ILSourceLocation'] = None
) -> ExpressionIndex:
"""
``case`` a switch case for values ``values`` with body ``expr``
:param List[ExpressionIndex] values: matched values for the case
:param ExpressionIndex expr: body of switch case
:param ILSourceLocation loc: location of returned expression
:return: The expression ``case values...: { expr }``
:rtype: ExpressionIndex
"""
return self.expr(HighLevelILOperation.HLIL_CASE, len(values), self.add_operand_list(values), expr, source_location=loc)
def break_expr(self, loc: Optional['ILSourceLocation'] = None) -> ExpressionIndex:
"""
``break`` break out of a loop or switch statement
:param ILSourceLocation loc: location of returned expression
:return: The expression ``break``
:rtype: ExpressionIndex
"""
return self.expr(HighLevelILOperation.HLIL_BREAK, source_location=loc)
def continue_expr(self, loc: Optional['ILSourceLocation'] = None) -> ExpressionIndex:
"""
``continue`` continue to the top of a loop statement
:param ILSourceLocation loc: location of returned expression
:return: The expression ``continue``
:rtype: ExpressionIndex
"""
return self.expr(HighLevelILOperation.HLIL_CONTINUE, source_location=loc)
def jump(self, dest: ExpressionIndex, loc: Optional['ILSourceLocation'] = None) -> ExpressionIndex:
"""
``jump`` unconditionally branch to an expression by value
:param ExpressionIndex dest: target of the jump
:param ILSourceLocation loc: location of returned expression
:return: The expression ``jump(dest)``
:rtype: ExpressionIndex
"""
return self.expr(HighLevelILOperation.HLIL_JUMP, dest, source_location=loc)
def ret(self, sources: List[ExpressionIndex], loc: Optional['ILSourceLocation'] = None) -> ExpressionIndex:
"""
``ret`` returns an expression which jumps (branches) to the calling function,
returning a result specified by the expressions in ``sources``.
:param List[ExpressionIndex] sources: list of returned expressions
:param ILSourceLocation loc: location of returned expression
:return: The expression ``return sources...``
:rtype: ExpressionIndex
"""
return self.expr(HighLevelILOperation.HLIL_RET, len(sources), self.add_operand_list(sources), source_location=loc)
def no_ret(self, loc: Optional['ILSourceLocation'] = None) -> ExpressionIndex:
"""
``no_ret`` returns an expression that halts execution
:param ILSourceLocation loc: location of returned expression
:return: The expression ``noreturn``
:rtype: ExpressionIndex
"""
return self.expr(HighLevelILOperation.HLIL_NORET, source_location=loc)
def unreachable(self, loc: Optional['ILSourceLocation'] = None) -> ExpressionIndex:
"""
``unreachable`` returns an expression that is unreachable and should be omitted during analysis
:param ILSourceLocation loc: location of returned expression
:return: The expression ``unreachable``
:rtype: ExpressionIndex
"""
return self.expr(HighLevelILOperation.HLIL_UNREACHABLE, source_location=loc)
def goto(self, target: int, loc: Optional['ILSourceLocation'] = None) -> ExpressionIndex:
"""
``goto`` unconditionally branch to a label
:param int target: target of the goto
:param ILSourceLocation loc: location of returned expression
:return: The expression ``goto(target)``
:rtype: ExpressionIndex
"""
return self.expr(HighLevelILOperation.HLIL_GOTO, target, source_location=loc)
def label(self, target: int, loc: Optional['ILSourceLocation'] = None) -> ExpressionIndex:
"""
``label`` create a label expression at a target
:param int target: target of the label
:param ILSourceLocation loc: location of returned expression
:return: The expression ``target:``
:rtype: ExpressionIndex
"""
return self.expr(HighLevelILOperation.HLIL_LABEL, target, source_location=loc)
def var_declare(self, var: 'variable.Variable', loc: Optional['ILSourceLocation'] = None) -> ExpressionIndex:
"""
``var_declare`` declare a variable in the current scope
:param Variable var: location of variable being declared
:param ILSourceLocation loc: location of returned expression
:return: The expression ``var`` (no assignment or anything)
:rtype: ExpressionIndex
"""
return self.expr(HighLevelILOperation.HLIL_VAR_DECLARE, var.identifier, source_location=loc)
def var_init(
self, size: int, dest: 'variable.Variable', src: ExpressionIndex, loc: Optional['ILSourceLocation'] = None
) -> ExpressionIndex:
"""
``var_init`` declare and assign a variable in the current scope of size ``size``
:param int size: size of the variable
:param Variable dest: location of variable being declared
:param ExpressionIndex src: value being assigned to the variable
:param ILSourceLocation loc: location of returned expression
:return: The expression ``dest = src``
:rtype: ExpressionIndex
"""
return self.expr(HighLevelILOperation.HLIL_VAR_INIT, dest.identifier, src, size=size, source_location=loc)
def assign(
self, size: int, dest: ExpressionIndex, src: ExpressionIndex, loc: Optional['ILSourceLocation'] = None
) -> ExpressionIndex:
"""
``assign`` assign expression ``src`` to expression ``dest``
:param int size: size of the expression
:param ExpressionIndex dest: expression being assigned
:param ExpressionIndex src: value being assigned to the expression
:param ILSourceLocation loc: location of returned expression
:return: The expression ``dest = src``
:rtype: ExpressionIndex
"""
return self.expr(HighLevelILOperation.HLIL_ASSIGN, dest, src, size=size, source_location=loc)
def assign_unpack(
self, size: int, output: List[ExpressionIndex], src: ExpressionIndex, loc: Optional['ILSourceLocation'] = None
) -> ExpressionIndex:
"""
``assign_unpack`` assign expression ``src`` to a list of expressions in ``output`` of size ``size``
:param int size: size of the expression
:param List[ExpressionIndex] output: expressions being assigned
:param ExpressionIndex src: value being assigned to the expressions
:param ILSourceLocation loc: location of returned expression
:return: The expression ``output... = src``
:rtype: ExpressionIndex
"""
return self.expr(HighLevelILOperation.HLIL_ASSIGN_UNPACK, len(output), self.add_operand_list(output), src, size=size, source_location=loc)
def var(self, size: int, src: 'variable.Variable', loc: Optional['ILSourceLocation'] = None) -> ExpressionIndex:
"""
``var`` returns the variable ``src`` of size ``size``
:param int size: the size of the variable in bytes
:param Variable src: the variable being read
:param ILSourceLocation loc: location of returned expression
:return: An expression for the given variable
:rtype: ExpressionIndex
"""
return self.expr(HighLevelILOperation.HLIL_VAR, src.identifier, size=size, source_location=loc)
def struct_field(
self, size: int, src: ExpressionIndex, offset: int, member_index: int, loc: Optional['ILSourceLocation'] = None
) -> ExpressionIndex:
"""
``struct_field`` returns the structure field at offset ``offset`` and index ``member_index`` from expression ``src`` of size ``size``
:param int size: the size of the field in bytes
:param ExpressionIndex src: the expression being read
:param int offset: offset of field in the structure
:param int member_index: index of field in the structure
:param ILSourceLocation loc: location of returned expression
:return: The expression ``src:offset.size``
:rtype: ExpressionIndex
"""
return self.expr(HighLevelILOperation.HLIL_STRUCT_FIELD, src, offset, member_index, size=size, source_location=loc)
def split(
self, size: int, hi: ExpressionIndex, lo: ExpressionIndex, loc: Optional['ILSourceLocation'] = None
) -> ExpressionIndex:
"""
``var_split`` combines expressions ``hi`` and ``lo`` of size ``size`` into an expression of size ``2*size``
:param int size: the size of each expression in bytes
:param ExpressionIndex hi: the expression holding high part of value
:param ExpressionIndex lo: the expression holding low part of value
:param ILSourceLocation loc: location of returned expression
:return: The expression ``hi:lo``
:rtype: ExpressionIndex
"""
return self.expr(HighLevelILOperation.HLIL_SPLIT, hi, lo, size=size, source_location=loc)
def array_index(
self, size: int, src: ExpressionIndex, idx: ExpressionIndex, loc: Optional['ILSourceLocation'] = None
) -> ExpressionIndex:
"""
``array_index`` references an item at index ``idx`` in the array in ``src`` of size ``size``
:param int size: size of the item in the array
:param ExpressionIndex src: expression for the array
:param ExpressionIndex idx: expression for the index into the array
:param ILSourceLocation loc: location of returned expression
:return: The expression ``src[idx].size``
:rtype: ExpressionIndex
"""
return self.expr(HighLevelILOperation.HLIL_ARRAY_INDEX, src, idx, size=size, source_location=loc)
def deref(self, size: int, src: ExpressionIndex, loc: Optional['ILSourceLocation']) -> ExpressionIndex:
"""
``deref`` dereferences expression ``src`` and reads a value of size ``size``
:param int size: size of the read
:param ExpressionIndex src: expression being read
:param ILSourceLocation loc: location of returned expression
:return: The expression ``(*src).size``
:rtype: ExpressionIndex
"""
return self.expr(HighLevelILOperation.HLIL_DEREF, src, size=size, source_location=loc)
def deref_field(
self, size: int, src: ExpressionIndex, offset: int, member_index: int, loc: Optional['ILSourceLocation']
) -> ExpressionIndex:
"""
``deref_field`` dereferences structure field in expression ``src`` at offset ``offset`` and index ``member_index`` of size ``size``
:param int size: size of the read
:param ExpressionIndex src: expression of structure being read
:param int offset: offset of field in the structure
:param int member_index: index of field in the structure
:param ILSourceLocation loc: location of returned expression
:return: The expression ``src->offset.size``
:rtype: ExpressionIndex
"""
return self.expr(HighLevelILOperation.HLIL_DEREF_FIELD, src, offset, member_index, size=size, source_location=loc)
def address_of(self, src: ExpressionIndex, loc: Optional['ILSourceLocation'] = None) -> ExpressionIndex:
"""
``address_of`` takes the address of ``src``
:param ExpressionIndex src: the expression having its address taken
:param ILSourceLocation loc: location of returned expression
:return: The expression ``&src``
:rtype: ExpressionIndex
"""
return self.expr(HighLevelILOperation.HLIL_ADDRESS_OF, src, size=0, source_location=loc)
def pass_by_ref(self, size: int, src: ExpressionIndex, loc: Optional['ILSourceLocation'] = None) -> ExpressionIndex:
"""
``pass_by_ref`` indicates that ``value`` is being passed by reference to a call with a pointer size of ``size``
:param int size: the size of the pointer in bytes
:param ExpressionIndex src: the expression containing the reference being passed
:param ILSourceLocation loc: location of returned expression
:return: The expression ``ref *src``
:rtype: ExpressionIndex
"""
return self.expr(HighLevelILOperation.HLIL_PASS_BY_REF, src, size, source_location=loc)
def return_by_ref(self, size: int, dest: ExpressionIndex, loc: Optional['ILSourceLocation'] = None) -> ExpressionIndex:
"""
``return_by_ref`` indicates that ``dest`` is being returned by passing a reference to a call
:param int size: the size of the value in bytes
:param ExpressionIndex dest: the expression containing the target of the return value
:param ILSourceLocation loc: location of returned expression
:return: The expression ``ref dest``
:rtype: ExpressionIndex
"""
return self.expr(HighLevelILOperation.HLIL_RETURN_BY_REF, dest, size, source_location=loc)
def const(self, size: int, value: int, loc: Optional['ILSourceLocation'] = None) -> ExpressionIndex:
"""
``const`` returns an expression for the constant integer ``value`` of size ``size``
:param int size: the size of the constant in bytes
:param int value: integer value of the constant
:param ILSourceLocation loc: location of returned expression
:return: A constant expression of given value and size
:rtype: ExpressionIndex
"""
return self.expr(HighLevelILOperation.HLIL_CONST, value, size=size, source_location=loc)
def const_pointer(self, size: int, value: int, loc: Optional['ILSourceLocation'] = None) -> ExpressionIndex:
"""
``const_pointer`` returns an expression for the constant pointer ``value`` of size ``size``
:param int size: the size of the pointer in bytes
:param int value: address referenced by the pointer
:param ILSourceLocation loc: location of returned expression
:return: A constant expression of given value and size
:rtype: ExpressionIndex
"""
return self.expr(HighLevelILOperation.HLIL_CONST_PTR, value, size=size, source_location=loc)
def extern_pointer(
self, size: int, value: int, offset: int, loc: Optional['ILSourceLocation'] = None
) -> ExpressionIndex:
"""
``extern_pointer`` returns an expression for the external pointer ``value`` at offset ``offset`` of size ``size``
:param int size: the size of the pointer in bytes
:param int value: address referenced by the pointer
:param int offset: offset applied to the address
:param loc: location of returned expression
:return: A constant expression of given value and size
:rtype: ExpressionIndex
"""
return self.expr(HighLevelILOperation.HLIL_EXTERN_PTR, value, offset, size=size, source_location=loc)
def float_const_raw(self, size: int, value: int, loc: Optional['ILSourceLocation'] = None) -> ExpressionIndex:
"""
``float_const_raw`` returns an expression for the constant raw binary floating point
value ``value`` with size ``size``
:param int size: the size of the constant in bytes
:param int value: integer value for the raw binary representation of the constant
:param ILSourceLocation loc: location of returned expression
:return: A constant expression of given value and size
:rtype: ExpressionIndex
"""
return self.expr(HighLevelILOperation.HLIL_FLOAT_CONST, value, size=size, source_location=loc)
def float_const_single(self, value: float, loc: Optional['ILSourceLocation'] = None) -> ExpressionIndex:
"""
``float_const_single`` returns an expression for the single precision floating point value ``value``
:param float value: float value for the constant
:param ILSourceLocation loc: location of returned expression
:return: A constant expression of given value and size
:rtype: ExpressionIndex
"""
return self.expr(HighLevelILOperation.HLIL_FLOAT_CONST, struct.unpack("I", struct.pack("f", value))[0], size=4, source_location=loc)
def float_const_double(self, value: float, loc: Optional['ILSourceLocation'] = None) -> ExpressionIndex:
"""
``float_const_double`` returns an expression for the double precision floating point value ``value``
:param float value: float value for the constant
:param ILSourceLocation loc: location of returned expression
:return: A constant expression of given value and size
:rtype: ExpressionIndex
"""
return self.expr(HighLevelILOperation.HLIL_FLOAT_CONST, struct.unpack("Q", struct.pack("d", value))[0], size=8, source_location=loc)
def imported_address(self, size: int, value: int, loc: Optional['ILSourceLocation'] = None) -> ExpressionIndex:
"""
``imported_address`` returns an expression for an imported value with address ``value`` and size ``size``
:param int size: size of the imported value
:param int value: address of the imported value
:param ILSourceLocation loc: location of returned expression
:return: A constant expression of given value and size
:rtype: ExpressionIndex
"""
return self.expr(HighLevelILOperation.HLIL_IMPORT, value, size=size, source_location=loc)
def const_data(self, size: int, data: 'variable.ConstantData', loc: Optional['ILSourceLocation'] = None) -> ExpressionIndex:
"""
``const_data`` returns an expression for the constant data ``data``
:param int size: size of the data
:param ConstantData data: value of the data
:param ILSourceLocation loc: location of returned expression
:return: A constant expression of given value and size
:rtype: ExpressionIndex
"""
return self.expr(HighLevelILOperation.HLIL_CONST_DATA, data.type, data.value, size=size, source_location=loc)
def add(
self, size: int, a: ExpressionIndex, b: ExpressionIndex, loc: Optional['ILSourceLocation'] = None
) -> ExpressionIndex:
"""
``add`` adds expression ``a`` to expression ``b`` returning an expression of ``size`` bytes
:param int size: the size of the result in bytes
:param ExpressionIndex a: LHS expression
:param ExpressionIndex b: RHS expression
:param ILSourceLocation loc: location of returned expression
:return: The expression ``add.<size>(a, b)``
:rtype: ExpressionIndex
"""
return self.expr(HighLevelILOperation.HLIL_ADD,a, b, size=size, source_location=loc)
def add_carry(
self, size: int, a: ExpressionIndex, b: ExpressionIndex, carry: ExpressionIndex,
loc: Optional['ILSourceLocation'] = None
) -> ExpressionIndex:
"""
``add_carry`` adds expression ``a`` to expression ``b`` with carry from ``carry`` returning an expression of ``size`` bytes
:param int size: the size of the result in bytes
:param ExpressionIndex a: LHS expression
:param ExpressionIndex b: RHS expression
:param ExpressionIndex carry: Carried value expression
:param ILSourceLocation loc: location of returned expression
:return: The expression ``adc.<size>(a, b, carry)``
:rtype: ExpressionIndex
"""
return self.expr(HighLevelILOperation.HLIL_ADC, a, b, carry, size=size, source_location=loc)
def sub(
self, size: int, a: ExpressionIndex, b: ExpressionIndex, loc: Optional['ILSourceLocation'] = None
) -> ExpressionIndex:
"""
``sub`` subtracts expression ``a`` to expression ``b`` returning an expression of ``size`` bytes
:param int size: the size of the result in bytes
:param ExpressionIndex a: LHS expression
:param ExpressionIndex b: RHS expression
:param ILSourceLocation loc: location of returned expression
:return: The expression ``sub.<size>(a, b)``
:rtype: ExpressionIndex
"""
return self.expr(HighLevelILOperation.HLIL_SUB, a, b, size=size, source_location=loc)
def sub_borrow(
self, size: int, a: ExpressionIndex, b: ExpressionIndex, carry: ExpressionIndex,
loc: Optional['ILSourceLocation'] = None
) -> ExpressionIndex:
"""
``sub_borrow`` subtracts expression ``a`` to expression ``b`` with borrow from ``carry`` returning an expression of ``size`` bytes
:param int size: the size of the result in bytes
:param ExpressionIndex a: LHS expression
:param ExpressionIndex b: RHS expression
:param ExpressionIndex carry: Carried value expression
:param ILSourceLocation loc: location of returned expression
:return: The expression ``sbb.<size>(a, b, carry)``
:rtype: ExpressionIndex
"""
return self.expr(HighLevelILOperation.HLIL_SBB, a, b, carry, size=size, source_location=loc)
def and_expr(
self, size: int, a: ExpressionIndex, b: ExpressionIndex, loc: Optional['ILSourceLocation'] = None
) -> ExpressionIndex:
"""
``and_expr`` bitwise and's expression ``a`` and expression ``b`` returning an expression of ``size`` bytes
:param int size: the size of the result in bytes
:param ExpressionIndex a: LHS expression
:param ExpressionIndex b: RHS expression
:param ILSourceLocation loc: location of returned expression
:return: The expression ``and.<size>(a, b)``
:rtype: ExpressionIndex
"""
return self.expr(HighLevelILOperation.HLIL_AND, a, b, size=size, source_location=loc)
def or_expr(
self, size: int, a: ExpressionIndex, b: ExpressionIndex, loc: Optional['ILSourceLocation'] = None
) -> ExpressionIndex:
"""
``or_expr`` bitwise or's expression ``a`` and expression ``b`` returning an expression of ``size`` bytes
:param int size: the size of the result in bytes
:param ExpressionIndex a: LHS expression
:param ExpressionIndex b: RHS expression
:param ILSourceLocation loc: location of returned expression
:return: The expression ``or.<size>(a, b)``
:rtype: ExpressionIndex
"""
return self.expr(HighLevelILOperation.HLIL_OR, a, b, size=size, source_location=loc)
def xor_expr(
self, size: int, a: ExpressionIndex, b: ExpressionIndex, loc: Optional['ILSourceLocation'] = None
) -> ExpressionIndex:
"""
``xor_expr`` xor's expression ``a`` and expression ``b`` returning an expression of ``size`` bytes
:param int size: the size of the result in bytes
:param ExpressionIndex a: LHS expression
:param ExpressionIndex b: RHS expression
:param ILSourceLocation loc: location of returned expression
:return: The expression ``xor.<size>(a, b)``
:rtype: ExpressionIndex
"""
return self.expr(HighLevelILOperation.HLIL_XOR, a, b, size=size, source_location=loc)
def shift_left(
self, size: int, a: ExpressionIndex, b: ExpressionIndex, loc: Optional['ILSourceLocation'] = None
) -> ExpressionIndex:
"""
``shift_left`` left shifts expression ``a`` by expression ``b`` returning an expression of ``size`` bytes
:param int size: the size of the result in bytes
:param ExpressionIndex a: LHS expression
:param ExpressionIndex b: RHS expression
:param ILSourceLocation loc: location of returned expression
:return: The expression ``lsl.<size>(a, b)``
:rtype: ExpressionIndex
"""
return self.expr(HighLevelILOperation.HLIL_LSL, a, b, size=size, source_location=loc)
def logical_shift_right(
self, size: int, a: ExpressionIndex, b: ExpressionIndex, loc: Optional['ILSourceLocation'] = None
) -> ExpressionIndex:
"""
``logical_shift_right`` logically right shifts expression ``a`` by expression ``b`` returning an expression of ``size`` bytes
:param int size: the size of the result in bytes
:param ExpressionIndex a: LHS expression
:param ExpressionIndex b: RHS expression
:param ILSourceLocation loc: location of returned expression
:return: The expression ``lsr.<size>(a, b)``
:rtype: ExpressionIndex
"""
return self.expr(HighLevelILOperation.HLIL_LSR, a, b, size=size, source_location=loc)
def arith_shift_right(
self, size: int, a: ExpressionIndex, b: ExpressionIndex, loc: Optional['ILSourceLocation'] = None
) -> ExpressionIndex:
"""
``arith_shift_right`` arithmetically right shifts expression ``a`` by expression ``b`` returning an expression of ``size`` bytes
:param int size: the size of the result in bytes
:param ExpressionIndex a: LHS expression
:param ExpressionIndex b: RHS expression
:param ILSourceLocation loc: location of returned expression
:return: The expression ``asr.<size>(a, b)``
:rtype: ExpressionIndex
"""
return self.expr(HighLevelILOperation.HLIL_ASR, a, b, size=size, source_location=loc)
def rotate_left(
self, size: int, a: ExpressionIndex, b: ExpressionIndex, loc: Optional['ILSourceLocation'] = None
) -> ExpressionIndex:
"""
``rotate_left`` bitwise rotates left expression ``a`` by expression ``b`` returning an expression of ``size`` bytes
:param int size: the size of the result in bytes
:param ExpressionIndex a: LHS expression
:param ExpressionIndex b: RHS expression
:param ILSourceLocation loc: location of returned expression
:return: The expression ``rol.<size>(a, b)``
:rtype: ExpressionIndex
"""
return self.expr(HighLevelILOperation.HLIL_ROL, a, b, size=size, source_location=loc)
def rotate_left_carry(
self, size: int, a: ExpressionIndex, b: ExpressionIndex, carry: ExpressionIndex,
loc: Optional['ILSourceLocation'] = None
) -> ExpressionIndex:
"""
``rotate_left_carry`` bitwise rotates left expression ``a`` by expression ``b`` with carry from ``carry`` returning an expression of ``size`` bytes
:param int size: the size of the result in bytes
:param ExpressionIndex a: LHS expression
:param ExpressionIndex b: RHS expression
:param ExpressionIndex carry: Carried value expression
:param ILSourceLocation loc: location of returned expression
:return: The expression ``rlc.<size>(a, b, carry)``
:rtype: ExpressionIndex
"""
return self.expr(HighLevelILOperation.HLIL_RLC, a, b, carry, size=size, source_location=loc)
def rotate_right(
self, size: int, a: ExpressionIndex, b: ExpressionIndex, loc: Optional['ILSourceLocation'] = None
) -> ExpressionIndex:
"""
``rotate_right`` bitwise rotates right expression ``a`` by expression ``b`` returning an expression of ``size`` bytes
:param int size: the size of the result in bytes
:param ExpressionIndex a: LHS expression
:param ExpressionIndex b: RHS expression
:param ILSourceLocation loc: location of returned expression
:return: The expression ``ror.<size>(a, b)``
:rtype: ExpressionIndex
"""
return self.expr(HighLevelILOperation.HLIL_ROR, a, b, size=size, source_location=loc)
def rotate_right_carry(
self, size: int, a: ExpressionIndex, b: ExpressionIndex, carry: ExpressionIndex,
loc: Optional['ILSourceLocation'] = None
) -> ExpressionIndex:
"""
``rotate_right_carry`` bitwise rotates right expression ``a`` by expression ``b`` with carry from ``carry`` returning an expression of ``size`` bytes
:param int size: the size of the result in bytes
:param ExpressionIndex a: LHS expression
:param ExpressionIndex b: RHS expression
:param ExpressionIndex carry: Carried value expression
:param ILSourceLocation loc: location of returned expression
:return: The expression ``rrc.<size>(a, b, carry)``
:rtype: ExpressionIndex
"""
return self.expr(HighLevelILOperation.HLIL_RRC, a, b, carry, size=size, source_location=loc)
def mult(
self, size: int, a: ExpressionIndex, b: ExpressionIndex, loc: Optional['ILSourceLocation'] = None
) -> ExpressionIndex:
"""
``mult`` multiplies expression ``a`` by expression ``b`` and returns an expression.
Both the operands and return value are ``size`` bytes as the product's upper half is discarded.
:param int size: the size of the result and input operands, in bytes
:param ExpressionIndex a: LHS expression
:param ExpressionIndex b: RHS expression
:param ILSourceLocation loc: location of returned expression
:return: The expression ``mult.<size>(a, b)``
:rtype: ExpressionIndex
"""
return self.expr(HighLevelILOperation.HLIL_MUL, a, b, size=size, source_location=loc)
def mult_double_prec_signed(
self, size: int, a: ExpressionIndex, b: ExpressionIndex, loc: Optional['ILSourceLocation'] = None
) -> ExpressionIndex:
"""
``mult_double_prec_signed`` signed multiplies expression ``a`` by expression ``b`` and returns an expression.
Both the operands are ``size`` bytes and the returned expression is of size ``2*size`` bytes.
:param int size: the size of the result and input operands, in bytes
:param ExpressionIndex a: LHS expression
:param ExpressionIndex b: RHS expression
:param ILSourceLocation loc: location of returned expression
:return: The expression ``muls.dp.<2*size>(a, b)``
:rtype: ExpressionIndex
"""
return self.expr(HighLevelILOperation.HLIL_MULS_DP, a, b, size=size, source_location=loc)
def mult_double_prec_unsigned(
self, size: int, a: ExpressionIndex, b: ExpressionIndex, loc: Optional['ILSourceLocation'] = None
) -> ExpressionIndex:
"""
``mult_double_prec_unsigned`` unsigned multiplies expression ``a`` by expression ``b`` and returnisan expression.
Both the operands are ``size`` bytes and the returned expression is of size ``2*size`` bytes.
:param int size: the size of the result and input operands, in bytes
:param ExpressionIndex a: LHS expression
:param ExpressionIndex b: RHS expression
:param ILSourceLocation loc: location of returned expression
:return: The expression ``mulu.dp.<2*size>(a, b)``
:rtype: ExpressionIndex
"""
return self.expr(HighLevelILOperation.HLIL_MULU_DP, a, b, size=size, source_location=loc)
def min_signed(
self, size: int, a: ExpressionIndex, b: ExpressionIndex, loc: Optional['ILSourceLocation'] = None
) -> ExpressionIndex:
"""
``min_signed`` signed minimum of expressions ``a`` and ``b`` returning an expression of ``size`` bytes
:param int size: the size of the result in bytes
:param ExpressionIndex a: LHS expression
:param ExpressionIndex b: RHS expression
:param ILSourceLocation loc: location of returned expression
:return: The expression ``mins.<size>(a, b)``
:rtype: ExpressionIndex
"""
return self.expr(HighLevelILOperation.HLIL_MINS, a, b, size=size, source_location=loc)
def max_signed(
self, size: int, a: ExpressionIndex, b: ExpressionIndex, loc: Optional['ILSourceLocation'] = None
) -> ExpressionIndex:
"""
``max_signed`` signed maximum of expressions ``a`` and ``b`` returning an expression of ``size`` bytes
:param int size: the size of the result in bytes
:param ExpressionIndex a: LHS expression
:param ExpressionIndex b: RHS expression
:param ILSourceLocation loc: location of returned expression
:return: The expression ``maxs.<size>(a, b)``
:rtype: ExpressionIndex
"""
return self.expr(HighLevelILOperation.HLIL_MAXS, a, b, size=size, source_location=loc)
def min_unsigned(
self, size: int, a: ExpressionIndex, b: ExpressionIndex, loc: Optional['ILSourceLocation'] = None
) -> ExpressionIndex:
"""
``min_unsigned`` unsigned minimum of expressions ``a`` and ``b`` returning an expression of ``size`` bytes
:param int size: the size of the result in bytes
:param ExpressionIndex a: LHS expression
:param ExpressionIndex b: RHS expression
:param ILSourceLocation loc: location of returned expression
:return: The expression ``minu.<size>(a, b)``
:rtype: ExpressionIndex
"""
return self.expr(HighLevelILOperation.HLIL_MINU, a, b, size=size, source_location=loc)
def max_unsigned(
self, size: int, a: ExpressionIndex, b: ExpressionIndex, loc: Optional['ILSourceLocation'] = None
) -> ExpressionIndex:
"""
``max_unsigned`` unsigned maximum of expressions ``a`` and ``b`` returning an expression of ``size`` bytes
:param int size: the size of the result in bytes
:param ExpressionIndex a: LHS expression
:param ExpressionIndex b: RHS expression
:param ILSourceLocation loc: location of returned expression
:return: The expression ``maxu.<size>(a, b)``
:rtype: ExpressionIndex
"""
return self.expr(HighLevelILOperation.HLIL_MAXU, a, b, size=size, source_location=loc)
def div_signed(
self, size: int, a: ExpressionIndex, b: ExpressionIndex, loc: Optional['ILSourceLocation'] = None
) -> ExpressionIndex:
"""
``div_signed`` signed divides expression ``a`` by expression ``b`` and returns an expression.
Both the operands and return value are ``size`` bytes.
:param int size: the size of the result and input operands, in bytes
:param ExpressionIndex a: LHS expression
:param ExpressionIndex b: RHS expression
:param ILSourceLocation loc: location of returned expression
:return: The expression ``divs.<size>(a, b)``
:rtype: ExpressionIndex
"""
return self.expr(HighLevelILOperation.HLIL_DIVS, a, b, size=size, source_location=loc)
def div_double_prec_signed(
self, size: int, a: ExpressionIndex, b: ExpressionIndex, loc: Optional['ILSourceLocation'] = None
) -> ExpressionIndex:
"""
``div_double_prec_signed`` signed divides double precision expression ``a`` by expression ``b`` and returns an expression.
The first operand is of size ``2*size`` bytes and the other operand and return value are of size ``size`` bytes.
:param int size: the size of the result and input operands, in bytes
:param ExpressionIndex a: LHS expression
:param ExpressionIndex b: RHS expression
:param ILSourceLocation loc: location of returned expression
:return: The expression ``divs.dp.<size>(a, b)``
:rtype: ExpressionIndex
"""
return self.expr(HighLevelILOperation.HLIL_DIVS_DP, a, b, size=size, source_location=loc)
def div_unsigned(
self, size: int, a: ExpressionIndex, b: ExpressionIndex, loc: Optional['ILSourceLocation'] = None
) -> ExpressionIndex:
"""
``div_unsigned`` unsigned divides expression ``a`` by expression ``b`` and returns an expression.
Both the operands and return value are ``size`` bytes.
:param int size: the size of the result and input operands, in bytes
:param ExpressionIndex a: LHS expression
:param ExpressionIndex b: RHS expression
:param ILSourceLocation loc: location of returned expression
:return: The expression ``divu.<size>(a, b)``
:rtype: ExpressionIndex
"""
return self.expr(HighLevelILOperation.HLIL_DIVU, a, b, size=size, source_location=loc)
def div_double_prec_unsigned(
self, size: int, a: ExpressionIndex, b: ExpressionIndex, loc: Optional['ILSourceLocation'] = None
) -> ExpressionIndex:
"""
``div_double_prec_unsigned`` unsigned divides double precision expression ``a`` by expression ``b`` and returns an expression.
The first operand is of size ``2*size`` bytes and the other operand and return value are of size ``size`` bytes.
:param int size: the size of the result and input operands, in bytes
:param ExpressionIndex a: LHS expression
:param ExpressionIndex b: RHS expression
:param ILSourceLocation loc: location of returned expression
:return: The expression ``divu.dp.<size>(a, b)``
:rtype: ExpressionIndex
"""
return self.expr(HighLevelILOperation.HLIL_DIVU_DP, a, b, size=size, source_location=loc)
def mod_signed(
self, size: int, a: ExpressionIndex, b: ExpressionIndex, loc: Optional['ILSourceLocation'] = None
) -> ExpressionIndex:
"""
``mod_signed`` signed modulus expression ``a`` by expression ``b`` and returns an expression.
Both the operands and return value are ``size`` bytes.
:param int size: the size of the result and input operands, in bytes
:param ExpressionIndex a: LHS expression
:param ExpressionIndex b: RHS expression
:param ILSourceLocation loc: location of returned expression
:return: The expression ``mods.<size>(a, b)``
:rtype: ExpressionIndex
"""
return self.expr(HighLevelILOperation.HLIL_MODS, a, b, size=size, source_location=loc)
def mod_double_prec_signed(
self, size: int, a: ExpressionIndex, b: ExpressionIndex, loc: Optional['ILSourceLocation'] = None
) -> ExpressionIndex:
"""
``mod_double_prec_signed`` signed modulus double precision expression ``a`` by expression ``b`` and returns an expression.
The first operand is of size ``2*size`` bytes and the other operand and return value are of size ``size`` bytes.
:param int size: the size of the result and input operands, in bytes
:param ExpressionIndex a: LHS expression
:param ExpressionIndex b: RHS expression
:param ILSourceLocation loc: location of returned expression
:return: The expression ``mods.dp.<size>(a, b)``
:rtype: ExpressionIndex
"""
return self.expr(HighLevelILOperation.HLIL_MODS_DP, a, b, size=size, source_location=loc)
def mod_unsigned(
self, size: int, a: ExpressionIndex, b: ExpressionIndex, loc: Optional['ILSourceLocation'] = None
) -> ExpressionIndex:
"""
``mod_unsigned`` unsigned modulus expression ``a`` by expression ``b`` and returns an expression.
Both the operands and return value are ``size`` bytes.
:param int size: the size of the result and input operands, in bytes
:param ExpressionIndex a: LHS expression
:param ExpressionIndex b: RHS expression
:param ILSourceLocation loc: location of returned expression
:return: The expression ``modu.<size>(a, b)``
:rtype: ExpressionIndex
"""
return self.expr(HighLevelILOperation.HLIL_MODU, a, b, size=size, source_location=loc)
def mod_double_prec_unsigned(
self, size: int, a: ExpressionIndex, b: ExpressionIndex, loc: Optional['ILSourceLocation'] = None
) -> ExpressionIndex:
"""
``mod_double_prec_unsigned`` unsigned modulus double precision expression ``a`` by expression ``b`` and returns an expression.
The first operand is of size ``2*size`` bytes and the other operand and return value are of size ``size`` bytes.
:param int size: the size of the result and input operands, in bytes
:param ExpressionIndex a: LHS expression
:param ExpressionIndex b: RHS expression
:param ILSourceLocation loc: location of returned expression
:return: The expression ``modu.dp.<size>(a, b)``
:rtype: ExpressionIndex
"""
return self.expr(HighLevelILOperation.HLIL_MODU_DP, a, b, size=size, source_location=loc)
def neg_expr(self, size: int, value: ExpressionIndex, loc: Optional['ILSourceLocation'] = None) -> ExpressionIndex:
"""
``neg_expr`` two's complement sign negation of expression ``value`` of size ``size``
:param int size: the size of the result in bytes
:param ExpressionIndex value: the expression to negate
:param ILSourceLocation loc: location of returned expression
:return: The expression ``neg.<size>(value)``
:rtype: ExpressionIndex
"""
return self.expr(HighLevelILOperation.HLIL_NEG, value, size=size, source_location=loc)
def not_expr(self, size: int, value: ExpressionIndex, loc: Optional['ILSourceLocation'] = None) -> ExpressionIndex:
"""
``not_expr`` bitwise inversion of expression ``value`` of size ``size``
:param int size: the size of the result in bytes
:param ExpressionIndex value: the expression to bitwise invert
:param ILSourceLocation loc: location of returned expression
:return: The expression ``not.<size>(value)``
:rtype: ExpressionIndex
"""
return self.expr(HighLevelILOperation.HLIL_NOT, value, size=size, source_location=loc)
def byte_swap(self, size: int, value: ExpressionIndex, loc: Optional['ILSourceLocation'] = None) -> ExpressionIndex:
"""
``byte_swap`` reverses the byte order of expression ``value`` of size ``size``
:param int size: the size of the result in bytes
:param ExpressionIndex value: the expression to byte swap
:param ILSourceLocation loc: location of returned expression
:return: The expression ``bswap.<size>(value)``
:rtype: ExpressionIndex
"""
return self.expr(HighLevelILOperation.HLIL_BSWAP, value, size=size, source_location=loc)
def population_count(self, size: int, value: ExpressionIndex, loc: Optional['ILSourceLocation'] = None) -> ExpressionIndex:
"""
``population_count`` counts the number of set bits in expression ``value`` of size ``size``
:param int size: the size of the result in bytes
:param ExpressionIndex value: the expression to count set bits in
:param ILSourceLocation loc: location of returned expression
:return: The expression ``popcnt.<size>(value)``
:rtype: ExpressionIndex
"""
return self.expr(HighLevelILOperation.HLIL_POPCNT, value, size=size, source_location=loc)
def count_leading_zeros(self, size: int, value: ExpressionIndex, loc: Optional['ILSourceLocation'] = None) -> ExpressionIndex:
"""
``count_leading_zeros`` counts the leading zero bits in expression ``value`` of size ``size``. The result is
``8 * size`` when ``value`` is zero.
:param int size: the size of the result in bytes
:param ExpressionIndex value: the expression to count leading zero bits in
:param ILSourceLocation loc: location of returned expression
:return: The expression ``clz.<size>(value)``
:rtype: ExpressionIndex
"""
return self.expr(HighLevelILOperation.HLIL_CLZ, value, size=size, source_location=loc)
def count_trailing_zeros(self, size: int, value: ExpressionIndex, loc: Optional['ILSourceLocation'] = None) -> ExpressionIndex:
"""
``count_trailing_zeros`` counts the trailing zero bits in expression ``value`` of size ``size``. The result is
``8 * size`` when ``value`` is zero.
:param int size: the size of the result in bytes
:param ExpressionIndex value: the expression to count trailing zero bits in
:param ILSourceLocation loc: location of returned expression
:return: The expression ``ctz.<size>(value)``
:rtype: ExpressionIndex
"""
return self.expr(HighLevelILOperation.HLIL_CTZ, value, size=size, source_location=loc)
def reverse_bits(self, size: int, value: ExpressionIndex, loc: Optional['ILSourceLocation'] = None) -> ExpressionIndex:
"""
``reverse_bits`` reverses the bit order of expression ``value`` of size ``size``
:param int size: the size of the result in bytes
:param ExpressionIndex value: the expression to reverse the bits of
:param ILSourceLocation loc: location of returned expression
:return: The expression ``rbit.<size>(value)``
:rtype: ExpressionIndex
"""
return self.expr(HighLevelILOperation.HLIL_RBIT, value, size=size, source_location=loc)
def count_leading_signs(self, size: int, value: ExpressionIndex, loc: Optional['ILSourceLocation'] = None) -> ExpressionIndex:
"""
``count_leading_signs`` counts the leading sign bits in expression ``value`` of size ``size`` (the number of bits
below the sign bit that match it)
:param int size: the size of the result in bytes
:param ExpressionIndex value: the expression to count leading sign bits in
:param ILSourceLocation loc: location of returned expression
:return: The expression ``cls.<size>(value)``
:rtype: ExpressionIndex
"""
return self.expr(HighLevelILOperation.HLIL_CLS, value, size=size, source_location=loc)
def absolute_value(self, size: int, value: ExpressionIndex, loc: Optional['ILSourceLocation'] = None) -> ExpressionIndex:
"""
``absolute_value`` signed absolute value of expression ``value`` of size ``size``
:param int size: the size of the result in bytes
:param ExpressionIndex value: the expression to take the absolute value of
:param ILSourceLocation loc: location of returned expression
:return: The expression ``abs.<size>(value)``
:rtype: ExpressionIndex
"""
return self.expr(HighLevelILOperation.HLIL_ABS, value, size=size, source_location=loc)
def sign_extend(self, size: int, value: ExpressionIndex, loc: Optional['ILSourceLocation'] = None) -> ExpressionIndex:
"""
``sign_extend`` two's complement sign-extends the expression in ``value`` to ``size`` bytes
:param int size: the size of the result in bytes
:param ExpressionIndex value: the expression to sign extend
:param ILSourceLocation loc: location of returned expression
:return: The expression ``sx.<size>(value)``
:rtype: ExpressionIndex
"""
return self.expr(HighLevelILOperation.HLIL_SX, value, size=size, source_location=loc)
def zero_extend(self, size: int, value: ExpressionIndex, loc: Optional['ILSourceLocation'] = None) -> ExpressionIndex:
"""
``zero_extend`` zero-extends the expression in ``value`` to ``size`` bytes
:param int size: the size of the result in bytes
:param ExpressionIndex value: the expression to zero extend
:param ILSourceLocation loc: location of returned expression
:return: The expression ``zx.<size>(value)``
:rtype: ExpressionIndex
"""
return self.expr(HighLevelILOperation.HLIL_ZX, value, size=size, source_location=loc)
def low_part(self, size: int, value: ExpressionIndex, loc: Optional['ILSourceLocation'] = None) -> ExpressionIndex:
"""
``low_part`` truncates the expression in ``value`` to ``size`` bytes
:param int size: the size of the result in bytes
:param ExpressionIndex value: the expression to zero extend
:param ILSourceLocation loc: location of returned expression
:return: The expression ``(value).<size>``
:rtype: ExpressionIndex
"""
return self.expr(HighLevelILOperation.HLIL_LOW_PART, value, size=size, source_location=loc)
def call(
self, dest: ExpressionIndex, params: List[ExpressionIndex],
loc: Optional['ILSourceLocation'] = None
) -> ExpressionIndex:
"""
``call`` returns an expression which calls the function in the expression ``dest``
with the parameters defined in ``params``
:param ExpressionIndex dest: the expression to call
:param List[ExpressionIndex] params: parameter variables
:param ILSourceLocation loc: location of returned expression
:return: The expression ``call(dest, params...)``
:rtype: ExpressionIndex
"""
return self.expr(
HighLevelILOperation.HLIL_CALL,
dest,
len(params),
self.add_operand_list(params),
size=0,
source_location=loc
)
def system_call(
self, params: List[ExpressionIndex], loc: Optional['ILSourceLocation'] = None
) -> ExpressionIndex:
"""
``system_call`` returns an expression which performs a system call
with the parameters defined in ``params``
:param List[ExpressionIndex] params: parameter variables
:param ILSourceLocation loc: location of returned expression
:return: The expression ``syscall(dest, params...)``
:rtype: ExpressionIndex
"""
return self.expr(
HighLevelILOperation.HLIL_SYSCALL,
len(params),
self.add_operand_list(params),
size=0,
source_location=loc
)
def tailcall(
self, dest: ExpressionIndex, params: List[ExpressionIndex],
loc: Optional['ILSourceLocation'] = None
) -> ExpressionIndex:
"""
``tailcall`` returns an expression which tailcalls the function in the expression ``dest``
with the parameters defined in ``params``
:param ExpressionIndex dest: the expression to call
:param List[ExpressionIndex] params: parameter variables
:param ILSourceLocation loc: location of returned expression
:return: The expression ``tailcall(dest, params...)``
:rtype: ExpressionIndex
"""
return self.expr(
HighLevelILOperation.HLIL_TAILCALL,
dest,
len(params),
self.add_operand_list(params),
size=0,
source_location=loc
)
def compare_equal(
self, size: int, a: ExpressionIndex, b: ExpressionIndex, loc: Optional['ILSourceLocation'] = None
) -> ExpressionIndex:
"""
``compare_equal`` returns comparison expression of size ``size`` checking if expression ``a`` is equal to
expression ``b``
:param int size: size in bytes
:param ExpressionIndex a: LHS of comparison
:param ExpressionIndex b: RHS of comparison
:param ILSourceLocation loc: location of returned expression
:return: a comparison expression.
:rtype: ExpressionIndex
"""
return self.expr(HighLevelILOperation.HLIL_CMP_E, a, b, size=size, source_location=loc)
def compare_not_equal(
self, size: int, a: ExpressionIndex, b: ExpressionIndex, loc: Optional['ILSourceLocation'] = None
) -> ExpressionIndex:
"""
``compare_not_equal`` returns comparison expression of size ``size`` checking if expression ``a`` is not equal to
expression ``b``
:param int size: size in bytes
:param ExpressionIndex a: LHS of comparison
:param ExpressionIndex b: RHS of comparison
:param ILSourceLocation loc: location of returned expression
:return: a comparison expression.
:rtype: ExpressionIndex
"""
return self.expr(HighLevelILOperation.HLIL_CMP_NE, a, b, size=size, source_location=loc)
def compare_signed_less_than(
self, size: int, a: ExpressionIndex, b: ExpressionIndex, loc: Optional['ILSourceLocation'] = None
) -> ExpressionIndex:
"""
``compare_signed_less_than`` returns comparison expression of size ``size`` checking if expression ``a`` is
signed less than expression ``b``
:param int size: size in bytes
:param ExpressionIndex a: LHS of comparison
:param ExpressionIndex b: RHS of comparison
:param ILSourceLocation loc: location of returned expression
:return: a comparison expression.
:rtype: ExpressionIndex
"""
return self.expr(HighLevelILOperation.HLIL_CMP_SLT, a, b, size=size, source_location=loc)
def compare_unsigned_less_than(
self, size: int, a: ExpressionIndex, b: ExpressionIndex, loc: Optional['ILSourceLocation'] = None
) -> ExpressionIndex:
"""
``compare_unsigned_less_than`` returns comparison expression of size ``size`` checking if expression ``a`` is
unsigned less than expression ``b``
:param int size: size in bytes
:param ExpressionIndex a: LHS of comparison
:param ExpressionIndex b: RHS of comparison
:param ILSourceLocation loc: location of returned expression
:return: a comparison expression.
:rtype: ExpressionIndex
"""
return self.expr(HighLevelILOperation.HLIL_CMP_ULT, a, b, size=size, source_location=loc)
def compare_signed_less_equal(
self, size: int, a: ExpressionIndex, b: ExpressionIndex, loc: Optional['ILSourceLocation'] = None
) -> ExpressionIndex:
"""
``compare_signed_less_equal`` returns comparison expression of size ``size`` checking if expression ``a`` is
signed less than or equal to expression ``b``
:param int size: size in bytes
:param ExpressionIndex a: LHS of comparison
:param ExpressionIndex b: RHS of comparison
:param ILSourceLocation loc: location of returned expression
:return: a comparison expression.
:rtype: ExpressionIndex
"""
return self.expr(HighLevelILOperation.HLIL_CMP_SLE, a, b, size=size, source_location=loc)
def compare_unsigned_less_equal(
self, size: int, a: ExpressionIndex, b: ExpressionIndex, loc: Optional['ILSourceLocation'] = None
) -> ExpressionIndex:
"""
``compare_unsigned_less_equal`` returns comparison expression of size ``size`` checking if expression ``a`` is
unsigned less than or equal to expression ``b``
:param int size: size in bytes
:param ExpressionIndex a: LHS of comparison
:param ExpressionIndex b: RHS of comparison
:param ILSourceLocation loc: location of returned expression
:return: a comparison expression.
:rtype: ExpressionIndex
"""
return self.expr(HighLevelILOperation.HLIL_CMP_ULE, a, b, size=size, source_location=loc)
def compare_signed_greater_equal(
self, size: int, a: ExpressionIndex, b: ExpressionIndex, loc: Optional['ILSourceLocation'] = None
) -> ExpressionIndex:
"""
``compare_signed_greater_equal`` returns comparison expression of size ``size`` checking if expression ``a`` is
signed greater than or equal to expression ``b``
:param int size: size in bytes
:param ExpressionIndex a: LHS of comparison
:param ExpressionIndex b: RHS of comparison
:param ILSourceLocation loc: location of returned expression
:return: a comparison expression.
:rtype: ExpressionIndex
"""
return self.expr(HighLevelILOperation.HLIL_CMP_SGE, a, b, size=size, source_location=loc)
def compare_unsigned_greater_equal(
self, size: int, a: ExpressionIndex, b: ExpressionIndex, loc: Optional['ILSourceLocation'] = None
) -> ExpressionIndex:
"""
``compare_unsigned_greater_equal`` returns comparison expression of size ``size`` checking if expression ``a``
is unsigned greater than or equal to expression ``b``
:param int size: size in bytes
:param ExpressionIndex a: LHS of comparison
:param ExpressionIndex b: RHS of comparison
:param ILSourceLocation loc: location of returned expression
:return: a comparison expression.
:rtype: ExpressionIndex
"""
return self.expr(HighLevelILOperation.HLIL_CMP_UGE, a, b, size=size, source_location=loc)
def compare_signed_greater_than(
self, size: int, a: ExpressionIndex, b: ExpressionIndex, loc: Optional['ILSourceLocation'] = None
) -> ExpressionIndex:
"""
``compare_signed_greater_than`` returns comparison expression of size ``size`` checking if expression ``a`` is
signed greater than or equal to expression ``b``
:param int size: size in bytes
:param ExpressionIndex a: LHS of comparison
:param ExpressionIndex b: RHS of comparison
:param ILSourceLocation loc: location of returned expression
:return: a comparison expression.
:rtype: ExpressionIndex
"""
return self.expr(HighLevelILOperation.HLIL_CMP_SGT, a, b, size=size, source_location=loc)
def compare_unsigned_greater_than(
self, size: int, a: ExpressionIndex, b: ExpressionIndex, loc: Optional['ILSourceLocation'] = None
) -> ExpressionIndex:
"""
``compare_unsigned_greater_than`` returns comparison expression of size ``size`` checking if expression ``a`` is
unsigned greater than or equal to expression ``b``
:param int size: size in bytes
:param ExpressionIndex a: LHS of comparison
:param ExpressionIndex b: RHS of comparison
:param ILSourceLocation loc: location of returned expression
:return: a comparison expression.
:rtype: ExpressionIndex
"""
return self.expr(HighLevelILOperation.HLIL_CMP_UGT, a, b, size=size, source_location=loc)
def test_bit(self, size: int, a: ExpressionIndex, b: ExpressionIndex, loc: Optional['ILSourceLocation'] = None) -> ExpressionIndex:
"""
``test_bit`` returns an expression of size ``size`` that tells whether expression ``a`` has its bit with an
index of the expression ``b`` is set
:param int size: size in bytes
:param ExpressionIndex a: an expression to be tested
:param ExpressionIndex b: an expression for the index of the big
:param ILSourceLocation loc: location of returned expression
:return: the result expression.
:rtype: ExpressionIndex
"""
return self.expr(HighLevelILOperation.HLIL_TEST_BIT, a, b, size=size, source_location=loc)
def bool_to_int(self, size: int, a: ExpressionIndex, loc: Optional['ILSourceLocation'] = None) -> ExpressionIndex:
"""
``bool_to_int`` returns an expression of size ``size`` converting the boolean expression ``a`` to an integer
:param int size: size in bytes
:param ExpressionIndex a: boolean expression to be converted
:param ILSourceLocation loc: location of returned expression
:return: the converted integer expression.
:rtype: ExpressionIndex
"""
return self.expr(HighLevelILOperation.HLIL_BOOL_TO_INT, a, size=size, source_location=loc)
def breakpoint(self, loc: Optional['ILSourceLocation'] = None) -> ExpressionIndex:
"""
``breakpoint`` returns a processor breakpoint expression.
:param ILSourceLocation loc: location of returned expression
:return: a breakpoint expression.
:rtype: ExpressionIndex
"""
return self.expr(HighLevelILOperation.HLIL_BP, source_location=loc)
def trap(self, value: int, loc: Optional['ILSourceLocation'] = None) -> ExpressionIndex:
"""
``trap`` returns a processor trap (interrupt) expression of the given integer ``value``.
:param int value: trap (interrupt) number
:param ILSourceLocation loc: location of returned expression
:return: a trap expression.
:rtype: ExpressionIndex
"""
return self.expr(HighLevelILOperation.HLIL_TRAP, value, source_location=loc)
def intrinsic(
self, intrinsic: 'architecture.IntrinsicType', params: List[ExpressionIndex],
loc: Optional['ILSourceLocation'] = None
):
"""
``intrinsic`` return an intrinsic expression.
:param IntrinsicType intrinsic: which intrinsic to call
:param List[ExpressionIndex] params: parameters to intrinsic
:param ILSourceLocation loc: location of returned expression
:return: an intrinsic expression.
:rtype: ExpressionIndex
"""
return self.expr(
HighLevelILOperation.HLIL_INTRINSIC,
self.arch.get_intrinsic_index(intrinsic),
len(params),
self.add_operand_list(params),
size=0,
source_location=loc
)
def undefined(self, loc: Optional['ILSourceLocation'] = None) -> ExpressionIndex:
"""
``undefined`` returns the undefined expression. This should be used for instructions which perform functions but
aren't important for dataflow or partial emulation purposes.
:param ILSourceLocation loc: location of returned expression
:return: the undefined expression.
:rtype: ExpressionIndex
"""
return self.expr(HighLevelILOperation.HLIL_UNDEF, source_location=loc)
def unimplemented(self, loc: Optional['ILSourceLocation'] = None) -> ExpressionIndex:
"""
``unimplemented`` returns the unimplemented expression. This should be used for all instructions which aren't
implemented.
:param ILSourceLocation loc: location of returned expression
:return: the unimplemented expression.
:rtype: ExpressionIndex
"""
return self.expr(HighLevelILOperation.HLIL_UNIMPL, source_location=loc)
def unimplemented_memory_ref(self, size: int, addr: ExpressionIndex, loc: Optional['ILSourceLocation'] = None) -> ExpressionIndex:
"""
``unimplemented_memory_ref`` a memory reference to expression ``addr`` of size ``size`` with unimplemented operation.
:param int size: size in bytes of the memory reference
:param ExpressionIndex addr: expression to reference memory
:param ILSourceLocation loc: location of returned expression
:return: the unimplemented memory reference expression.
:rtype: ExpressionIndex
"""
return self.expr(HighLevelILOperation.HLIL_UNIMPL_MEM, addr, size=size, source_location=loc)
def float_add(
self, size: int, a: ExpressionIndex, b: ExpressionIndex, loc: Optional['ILSourceLocation'] = None
) -> ExpressionIndex:
"""
``float_add`` adds floating point expression ``a`` to expression ``b``
and returning an expression of ``size`` bytes.
:param int size: the size of the result in bytes
:param ExpressionIndex a: LHS expression
:param ExpressionIndex b: RHS expression
:param ILSourceLocation loc: location of returned expression
:return: The expression ``fadd.<size>(a, b)``
:rtype: ExpressionIndex
"""
return self.expr(HighLevelILOperation.HLIL_FADD,a, b, size=size, source_location=loc)
def float_sub(
self, size: int, a: ExpressionIndex, b: ExpressionIndex, loc: Optional['ILSourceLocation'] = None
) -> ExpressionIndex:
"""
``float_sub`` subtracts floating point expression ``b`` from expression ``a``
and returning an expression of ``size`` bytes.
:param int size: the size of the result in bytes
:param ExpressionIndex a: LHS expression
:param ExpressionIndex b: RHS expression
:param ILSourceLocation loc: location of returned expression
:return: The expression ``fsub.<size>(a, b)``
:rtype: ExpressionIndex
"""
return self.expr(HighLevelILOperation.HLIL_FSUB, a, b, size=size, source_location=loc)
def float_mult(
self, size: int, a: ExpressionIndex, b: ExpressionIndex, loc: Optional['ILSourceLocation'] = None
) -> ExpressionIndex:
"""
``float_mult`` multiplies floating point expression ``a`` by expression ``b``
and returning an expression of ``size`` bytes.
:param int size: the size of the result in bytes
:param ExpressionIndex a: LHS expression
:param ExpressionIndex b: RHS expression
:param ILSourceLocation loc: location of returned expression
:return: The expression ``fmul.<size>(a, b)``
:rtype: ExpressionIndex
"""
return self.expr(HighLevelILOperation.HLIL_FMUL, a, b, size=size, source_location=loc)
def float_div(
self, size: int, a: ExpressionIndex, b: ExpressionIndex, loc: Optional['ILSourceLocation'] = None
) -> ExpressionIndex:
"""
``float_div`` divides floating point expression ``a`` by expression ``b``
and returning an expression of ``size`` bytes.
:param int size: the size of the result in bytes
:param ExpressionIndex a: LHS expression
:param ExpressionIndex b: RHS expression
:param ILSourceLocation loc: location of returned expression
:return: The expression ``fdiv.<size>(a, b)``
:rtype: ExpressionIndex
"""
return self.expr(HighLevelILOperation.HLIL_FDIV, a, b, size=size, source_location=loc)
def float_sqrt(
self, size: int, value: ExpressionIndex, loc: Optional['ILSourceLocation'] = None
) -> ExpressionIndex:
"""
``float_sqrt`` returns square root of floating point expression ``value`` of size ``size``
:param int size: the size of the result in bytes
:param ExpressionIndex value: the expression to calculate the square root of
:param ILSourceLocation loc: location of returned expression
:return: The expression ``sqrt.<size>(value)``
:rtype: ExpressionIndex
"""
return self.expr(HighLevelILOperation.HLIL_FSQRT, value, size=size, source_location=loc)
def float_neg(
self, size: int, value: ExpressionIndex, loc: Optional['ILSourceLocation'] = None
) -> ExpressionIndex:
"""
``float_neg`` returns sign negation of floating point expression ``value`` of size ``size``
:param int size: the size of the result in bytes
:param ExpressionIndex value: the expression to negate
:param ILSourceLocation loc: location of returned expression
:return: The expression ``fneg.<size>(value)``
:rtype: ExpressionIndex
"""
return self.expr(HighLevelILOperation.HLIL_FNEG, value, size=size, source_location=loc)
def float_abs(
self, size: int, value: ExpressionIndex, loc: Optional['ILSourceLocation'] = None
) -> ExpressionIndex:
"""
``float_abs`` returns absolute value of floating point expression ``value`` of size ``size``
:param int size: the size of the result in bytes
:param ExpressionIndex value: the expression to get the absolute value of
:param ILSourceLocation loc: location of returned expression
:return: The expression ``fabs.<size>(value)``
:rtype: ExpressionIndex
"""
return self.expr(HighLevelILOperation.HLIL_FABS, value, size=size, source_location=loc)
def float_to_int(
self, size: int, value: ExpressionIndex, loc: Optional['ILSourceLocation'] = None
) -> ExpressionIndex:
"""
``float_to_int`` returns integer value of floating point expression ``value`` of size ``size``
:param int size: the size of the result in bytes
:param ExpressionIndex value: the expression to convert to an int
:param ILSourceLocation loc: location of returned expression
:return: The expression ``int.<size>(value)``
:rtype: ExpressionIndex
"""
return self.expr(HighLevelILOperation.HLIL_FLOAT_TO_INT, value, size=size, source_location=loc)
def int_to_float(
self, size: int, value: ExpressionIndex, loc: Optional['ILSourceLocation'] = None
) -> ExpressionIndex:
"""
``int_to_float`` returns floating point value of integer expression ``value`` of size ``size``
:param int size: the size of the result in bytes
:param ExpressionIndex value: the expression to convert to a float
:param ILSourceLocation loc: location of returned expression
:return: The expression ``float.<size>(value)``
:rtype: ExpressionIndex
"""
return self.expr(HighLevelILOperation.HLIL_INT_TO_FLOAT, value, size=size, source_location=loc)
def float_convert(
self, size: int, value: ExpressionIndex, loc: Optional['ILSourceLocation'] = None
) -> ExpressionIndex:
"""
``int_to_float`` converts floating point value of expression ``value`` to size ``size``
:param int size: the size of the result in bytes
:param ExpressionIndex value: the expression to negate
:param ILSourceLocation loc: location of returned expression
:return: The expression ``fconvert.<size>(value)``
:rtype: ExpressionIndex
"""
return self.expr(HighLevelILOperation.HLIL_FLOAT_CONV, value, size=size, source_location=loc)
def round_to_int(
self, size: int, value: ExpressionIndex, loc: Optional['ILSourceLocation'] = None
) -> ExpressionIndex:
"""
``round_to_int`` rounds a floating point value to the nearest integer
:param int size: the size of the result in bytes
:param ExpressionIndex value: the expression to round to the nearest integer
:param ILSourceLocation loc: location of returned expression
:return: The expression ``roundint.<size>(value)``
:rtype: ExpressionIndex
"""
return self.expr(HighLevelILOperation.HLIL_ROUND_TO_INT, value, size=size, source_location=loc)
def floor(
self, size: int, value: ExpressionIndex, loc: Optional['ILSourceLocation'] = None
) -> ExpressionIndex:
"""
``floor`` rounds a floating point value to an integer towards negative infinity
:param int size: the size of the result in bytes
:param ExpressionIndex value: the expression to round down
:param ILSourceLocation loc: location of returned expression
:return: The expression ``roundint.<size>(value)``
:rtype: ExpressionIndex
"""
return self.expr(HighLevelILOperation.HLIL_FLOOR, value, size=size, source_location=loc)
def ceil(
self, size: int, value: ExpressionIndex, loc: Optional['ILSourceLocation'] = None
) -> ExpressionIndex:
"""
``ceil`` rounds a floating point value to an integer towards positive infinity
:param int size: the size of the result in bytes
:param ExpressionIndex value: the expression to round up
:param ILSourceLocation loc: location of returned expression
:return: The expression ``roundint.<size>(value)``
:rtype: ExpressionIndex
"""
return self.expr(HighLevelILOperation.HLIL_CEIL, value, size=size, source_location=loc)
def float_trunc(
self, size: int, value: ExpressionIndex, loc: Optional['ILSourceLocation'] = None
) -> ExpressionIndex:
"""
``float_trunc`` rounds a floating point value to an integer towards zero
:param int size: the size of the result in bytes
:param ExpressionIndex value: the expression to truncate
:param ILSourceLocation loc: location of returned expression
:return: The expression ``roundint.<size>(value)``
:rtype: ExpressionIndex
"""
return self.expr(HighLevelILOperation.HLIL_FTRUNC, value, size=size, source_location=loc)
def float_compare_equal(
self, size: int, a: ExpressionIndex, b: ExpressionIndex, loc: Optional['ILSourceLocation'] = None
) -> ExpressionIndex:
"""
``float_compare_equal`` returns floating point comparison expression of size ``size`` checking if
expression ``a`` is equal to expression ``b``
:param int size: the size of the operands in bytes
:param ExpressionIndex a: LHS expression
:param ExpressionIndex b: RHS expression
:param ILSourceLocation loc: location of returned expression
:return: The expression ``a f== b``
:rtype: ExpressionIndex
"""
return self.expr(HighLevelILOperation.HLIL_FCMP_E, a, b, size=size, source_location=loc)
def float_compare_not_equal(
self, size: int, a: ExpressionIndex, b: ExpressionIndex, loc: Optional['ILSourceLocation'] = None
) -> ExpressionIndex:
"""
``float_compare_not_equal`` returns floating point comparison expression of size ``size`` checking if
expression ``a`` is not equal to expression ``b``
:param int size: the size of the operands in bytes
:param ExpressionIndex a: LHS expression
:param ExpressionIndex b: RHS expression
:param ILSourceLocation loc: location of returned expression
:return: The expression ``a f!= b``
:rtype: ExpressionIndex
"""
return self.expr(HighLevelILOperation.HLIL_FCMP_NE, a, b, size=size, source_location=loc)
def float_compare_less_than(
self, size: int, a: ExpressionIndex, b: ExpressionIndex, loc: Optional['ILSourceLocation'] = None
) -> ExpressionIndex:
"""
``float_compare_less_than`` returns floating point comparison expression of size ``size`` checking if
expression ``a`` is less than expression ``b``
:param int size: the size of the operands in bytes
:param ExpressionIndex a: LHS expression
:param ExpressionIndex b: RHS expression
:param ILSourceLocation loc: location of returned expression
:return: The expression ``a f< b``
:rtype: ExpressionIndex
"""
return self.expr(HighLevelILOperation.HLIL_FCMP_LT, a, b, size=size, source_location=loc)
def float_compare_less_equal(
self, size: int, a: ExpressionIndex, b: ExpressionIndex, loc: Optional['ILSourceLocation'] = None
) -> ExpressionIndex:
"""
``float_compare_less_equal`` returns floating point comparison expression of size ``size`` checking if
expression ``a`` is less than or equal to expression ``b``
:param int size: the size of the operands in bytes
:param ExpressionIndex a: LHS expression
:param ExpressionIndex b: RHS expression
:param ILSourceLocation loc: location of returned expression
:return: The expression ``a f<= b``
:rtype: ExpressionIndex
"""
return self.expr(HighLevelILOperation.HLIL_FCMP_LE, a, b, size=size, source_location=loc)
def float_compare_greater_equal(
self, size: int, a: ExpressionIndex, b: ExpressionIndex, loc: Optional['ILSourceLocation'] = None
) -> ExpressionIndex:
"""
``float_compare_greater_equal`` returns floating point comparison expression of size ``size`` checking if
expression ``a`` is greater than or equal to expression ``b``
:param int size: the size of the operands in bytes
:param ExpressionIndex a: LHS expression
:param ExpressionIndex b: RHS expression
:param ILSourceLocation loc: location of returned expression
:return: The expression ``a f>= b``
:rtype: ExpressionIndex
"""
return self.expr(HighLevelILOperation.HLIL_FCMP_GE, a, b, size=size, source_location=loc)
def float_compare_greater_than(
self, size: int, a: ExpressionIndex, b: ExpressionIndex, loc: Optional['ILSourceLocation'] = None
) -> ExpressionIndex:
"""
``float_compare_greater_than`` returns floating point comparison expression of size ``size`` checking if
expression ``a`` is greater than expression ``b``
:param int size: the size of the operands in bytes
:param ExpressionIndex a: LHS expression
:param ExpressionIndex b: RHS expression
:param ILSourceLocation loc: location of returned expression
:return: The expression ``a f> b``
:rtype: ExpressionIndex
"""
return self.expr(HighLevelILOperation.HLIL_FCMP_GT, a, b, size=size, source_location=loc)
def float_compare_ordered(
self, size: int, a: ExpressionIndex, b: ExpressionIndex, loc: Optional['ILSourceLocation'] = None
) -> ExpressionIndex:
"""
``float_compare_ordered`` returns floating point comparison expression of size ``size`` checking if
expression ``a`` is ordered relative to expression ``b``
:param int size: the size of the operands in bytes
:param ExpressionIndex a: LHS expression
:param ExpressionIndex b: RHS expression
:param ILSourceLocation loc: location of returned expression
:return: The expression ``is_ordered(a, b)``
:rtype: ExpressionIndex
"""
return self.expr(HighLevelILOperation.HLIL_FCMP_O, a, b, size=size, source_location=loc)
def float_compare_unordered(
self, size: int, a: ExpressionIndex, b: ExpressionIndex, loc: Optional['ILSourceLocation'] = None
) -> ExpressionIndex:
"""
``float_compare_unordered`` returns floating point comparison expression of size ``size`` checking if
expression ``a`` is unordered relative to expression ``b``
:param int size: the size of the operands in bytes
:param ExpressionIndex a: LHS expression
:param ExpressionIndex b: RHS expression
:param ILSourceLocation loc: location of returned expression
:return: The expression ``is_unordered(a, b)``
:rtype: ExpressionIndex
"""
return self.expr(HighLevelILOperation.HLIL_FCMP_UO, a, b, size=size, source_location=loc)
def add_operand_list(self, operands: List[int]) -> ExpressionIndex:
"""
``add_operand_list`` returns an operand list expression for the given list of integer operands.
:param list(int) operands: list of operand numbers
:return: an operand list expression
:rtype: ExpressionIndex
"""
operand_list = (ctypes.c_ulonglong * len(operands))()
for i in range(len(operands)):
operand_list[i] = operands[i]
return ExpressionIndex(core.BNHighLevelILAddOperandList(self.handle, operand_list, len(operands)))
def finalize(self) -> None:
"""
``finalize`` ends the function and computes the list of basic blocks.
:rtype: None
"""
core.BNFinalizeHighLevelILFunction(self.handle)
def generate_ssa_form(self, variables: Optional[List["variable.Variable"]] = None) -> None:
"""
``generate_ssa_form`` generate SSA form given the current HLIL
:param list(Variable) variables: optional list of aliased variables
:rtype: None
"""
if variables is None:
variables = []
variable_list = (core.BNVariable * len(variables))()
for i in range(len(variables)):
variable_list[i] = variables[i].to_BNVariable()
core.BNGenerateHighLevelILSSAForm(self.handle, variable_list, len(variable_list))
def create_graph(self, settings: Optional['function.DisassemblySettings'] = None) -> 'flowgraph.CoreFlowGraph':
if settings is not None:
settings_obj = settings.handle
else:
settings_obj = None
return flowgraph.CoreFlowGraph(core.BNCreateHighLevelILFunctionGraph(self.handle, settings_obj))
def create_graph_immediate(self, settings: Optional['function.DisassemblySettings'] = None) -> 'flowgraph.CoreFlowGraph':
if settings is not None:
settings_obj = settings.handle
else:
settings_obj = None
return flowgraph.CoreFlowGraph(core.BNCreateHighLevelILImmediateFunctionGraph(self.handle, settings_obj))
@property
def il_form(self) -> FunctionGraphType:
if len(list(self.basic_blocks)) < 1:
return FunctionGraphType.InvalidILViewType
return FunctionGraphType(core.BNGetBasicBlockFunctionGraphType(list(self.basic_blocks)[0].handle))
@property
def vars(self) -> Union[List["variable.Variable"], List["mediumlevelil.SSAVariable"]]:
"""This gets just the HLIL variables - you may be interested in the union of `HighLevelIlFunction.source_function.parameter_vars` and `HighLevelIlFunction.aliased_vars` as well for all the variables used in the function"""
if self.source_function is None:
return []
if self.il_form == FunctionGraphType.HighLevelILSSAFormFunctionGraph:
return self.ssa_vars
if self.il_form == FunctionGraphType.HighLevelILFunctionGraph:
count = ctypes.c_ulonglong()
core_variables = core.BNGetHighLevelILVariables(self.handle, count)
assert core_variables is not None, "core.BNGetHighLevelILVariables returned None"
try:
result = []
for var_i in range(count.value):
result.append(
variable.Variable(
self, core_variables[var_i].type, core_variables[var_i].index, core_variables[var_i].storage
)
)
return result
finally:
core.BNFreeVariableList(core_variables)
return []
@property
def aliased_vars(self) -> List["variable.Variable"]:
"""This returns a list of Variables that are taken reference to and used elsewhere. You may also wish to consider `HighLevelIlFunction.vars` and `HighLevelIlFunction.source_function.parameter_vars`"""
if self.source_function is None:
return []
if self.il_form in [
FunctionGraphType.HighLevelILFunctionGraph, FunctionGraphType.HighLevelILSSAFormFunctionGraph
]:
count = ctypes.c_ulonglong()
core_variables = core.BNGetHighLevelILAliasedVariables(self.handle, count)
assert core_variables is not None, "core.BNGetHighLevelILAliasedVariables returned None"
try:
result = []
for var_i in range(count.value):
result.append(
variable.Variable(
self, core_variables[var_i].type, core_variables[var_i].index, core_variables[var_i].storage
)
)
return result
finally:
core.BNFreeVariableList(core_variables)
return []
@property
def ssa_vars(self) -> List["mediumlevelil.SSAVariable"]:
"""This gets just the HLIL SSA variables - you may be interested in the union of `HighLevelIlFunction.source_function.parameter_vars` and `HighLevelIlFunction.aliased_vars` for all the variables used in the function"""
if self.source_function is None:
return []
if self.il_form == FunctionGraphType.HighLevelILSSAFormFunctionGraph:
variable_count = ctypes.c_ulonglong()
core_variables = core.BNGetHighLevelILVariables(self.handle, variable_count)
assert core_variables is not None, "core.BNGetHighLevelILVariables returned None"
try:
result = []
for var_i in range(variable_count.value):
version_count = ctypes.c_ulonglong()
versions = core.BNGetHighLevelILVariableSSAVersions(
self.handle, core_variables[var_i], version_count
)
assert versions is not None, "core.BNGetHighLevelILVariableSSAVersions returned None"
try:
for version_i in range(version_count.value):
result.append(
mediumlevelil.SSAVariable(
variable.Variable(
self, core_variables[var_i].type, core_variables[var_i].index,
core_variables[var_i].storage
), versions[version_i]
)
)
finally:
core.BNFreeILInstructionList(versions)
return result
finally:
core.BNFreeVariableList(core_variables)
elif self.il_form == FunctionGraphType.HighLevelILFunctionGraph:
return self.ssa_form.ssa_vars
return []
def get_instruction_index_for_expr(self, expr: ExpressionIndex) -> Optional[InstructionIndex]:
result = core.BNGetHighLevelILInstructionForExpr(self.handle, expr)
if result >= core.BNGetHighLevelILInstructionCount(self.handle):
return None
return InstructionIndex(result)
def get_expr_index_for_instruction(self, instr: InstructionIndex) -> ExpressionIndex:
result = core.BNGetHighLevelILIndexForInstruction(self.handle, instr)
return ExpressionIndex(result)
def get_medium_level_il_expr_index(self, expr: ExpressionIndex) -> Optional['mediumlevelil.ExpressionIndex']:
medium_il = self.medium_level_il
if medium_il is None:
return None
medium_il = medium_il.ssa_form
if medium_il is None:
return None
result = core.BNGetMediumLevelILExprIndexFromHighLevelIL(self.handle, expr)
if result >= core.BNGetMediumLevelILExprCount(medium_il.handle):
return None
return mediumlevelil.ExpressionIndex(result)
def get_medium_level_il_expr_indexes(self, expr: ExpressionIndex) -> List['mediumlevelil.ExpressionIndex']:
count = ctypes.c_ulonglong()
exprs = core.BNGetMediumLevelILExprIndexesFromHighLevelIL(self.handle, expr, count)
assert exprs is not None, "core.BNGetMediumLevelILExprIndexesFromHighLevelIL returned None"
result = []
for i in range(0, count.value):
result.append(exprs[i])
core.BNFreeILInstructionList(exprs)
return result
def get_label(self, label_idx: int) -> Optional[HighLevelILInstruction]:
result = core.BNGetHighLevelILExprIndexForLabel(self.handle, label_idx)
if result >= core.BNGetHighLevelILExprCount(self.handle):
return None
return HighLevelILInstruction.create(self, ExpressionIndex(result))
def get_label_uses(self, label_idx: int) -> List[HighLevelILInstruction]:
count = ctypes.c_ulonglong()
uses = core.BNGetHighLevelILUsesForLabel(self.handle, label_idx, count)
assert uses is not None, "core.BNGetHighLevelILUsesForLabel returned None"
result = []
for i in range(0, count.value):
result.append(HighLevelILInstruction.create(self, uses[i]))
core.BNFreeILInstructionList(uses)
return result
def get_expr_type(self, expr_index: int) -> Optional['types.Type']:
"""
Get type of expression
:param int expr_index: index of the expression to retrieve
:rtype: Optional['types.Type']
"""
result = core.BNGetHighLevelILExprType(self.handle, expr_index)
if result.type:
platform = None
if self.source_function:
platform = self.source_function.platform
return types.Type.create(
result.type, platform=platform, confidence=result.confidence
)
return None
def set_expr_type(self, expr_index: int, expr_type: StringOrType) -> None:
"""
Set type of expression
This API is only meant for workflows or for debugging purposes, since the changes they make are not persistent
and get lost after a database save and reload. To make persistent changes to the analysis, one should use other
APIs to, for example, change the type of variables. The analysis will then propagate the type of the variable
and update the type of related expressions.
:param int expr_index: index of the expression to set
:param StringOrType: new type of the expression
"""
if isinstance(expr_type, str):
(expr_type, _) = self.view.parse_type_string(expr_type)
ic = expr_type.immutable_copy()
core.BNSetHighLevelILExprType(self.handle, expr_index, ic._to_core_struct())
class HighLevelILBasicBlock(basicblock.BasicBlock):
"""
The ``HighLevelILBasicBlock`` object is returned during analysis and should not be directly instantiated.
"""
def __init__(
self, handle: core.BNBasicBlockHandle, owner: HighLevelILFunction, view: Optional['binaryview.BinaryView']
):
super(HighLevelILBasicBlock, self).__init__(handle, view)
self._il_function = owner
def __iter__(self) -> Generator[HighLevelILInstruction, None, None]:
for idx in range(self.start, self.end):
yield self.il_function[idx]
@overload
def __getitem__(self, idx: int) -> 'HighLevelILInstruction': ...
@overload
def __getitem__(self, idx: slice) -> List['HighLevelILInstruction']: ...
def __getitem__(self, idx: Union[int, slice]) -> Union[List[HighLevelILInstruction], HighLevelILInstruction]:
size = self.end - self.start
if isinstance(idx, slice):
return [self[index] for index in range(*idx.indices(size))] # type: ignore
if idx > size or idx < -size:
raise IndexError("list index is out of range")
if idx >= 0:
return self.il_function[idx + self.start]
else:
return self.il_function[self.end + idx]
def _create_instance(self, handle: core.BNBasicBlockHandle):
"""Internal method by super to instantiate child instances"""
return HighLevelILBasicBlock(handle, self.il_function, self.view)
def __hash__(self):
return hash((self.start, self.end, self.il_function))
def __contains__(self, instruction):
if not isinstance(instruction, HighLevelILInstruction) or instruction.il_basic_block != self:
return False
if self.start <= instruction.instr_index <= self.end:
return True
else:
return False
def __repr__(self):
arch = self.arch
if arch:
return f"<{self.__class__.__name__}: {arch.name}@{self.start}-{self.end}>"
else:
return f"<{self.__class__.__name__}: {self.start}-{self.end}>"
@property
def instruction_count(self) -> int:
return self.end - self.start
@property
def il_function(self) -> HighLevelILFunction:
return self._il_function
|