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
|
// Copyright (c) 2015-2016 Vector 35 LLC
//
// 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.
#pragma once
#ifdef WIN32
#include <windows.h>
#endif
#include <stddef.h>
#include <string>
#include <vector>
#include <map>
#include <exception>
#include <functional>
#include <set>
#include <mutex>
#include "binaryninjacore.h"
#include "json/json.h"
#ifdef _MSC_VER
#define NOEXCEPT
#else
#define NOEXCEPT noexcept
#endif
namespace BinaryNinja
{
class RefCountObject
{
public:
int m_refs;
RefCountObject(): m_refs(0) {}
virtual ~RefCountObject() {}
void AddRef()
{
#ifdef WIN32
InterlockedIncrement((LONG*)&m_refs);
#else
__sync_fetch_and_add(&m_refs, 1);
#endif
}
void Release()
{
#ifdef WIN32
if (InterlockedDecrement((LONG*)&m_refs) == 0)
delete this;
#else
if (__sync_fetch_and_add(&m_refs, -1) == 1)
delete this;
#endif
}
};
template <class T, T* (*AddObjectReference)(T*), void (*FreeObjectReference)(T*)>
class CoreRefCountObject
{
void AddRefInternal()
{
#ifdef WIN32
InterlockedIncrement((LONG*)&m_refs);
#else
__sync_fetch_and_add(&m_refs, 1);
#endif
}
void ReleaseInternal()
{
#ifdef WIN32
if (InterlockedDecrement((LONG*)&m_refs) == 0)
delete this;
#else
if (__sync_fetch_and_add(&m_refs, -1) == 1)
delete this;
#endif
}
public:
int m_refs;
T* m_object;
CoreRefCountObject(): m_refs(0), m_object(nullptr) {}
virtual ~CoreRefCountObject() {}
T* GetObject() { return m_object; }
void AddRef()
{
if (m_object && (m_refs != 0))
AddObjectReference(m_object);
AddRefInternal();
}
void Release()
{
if (m_object)
FreeObjectReference(m_object);
ReleaseInternal();
}
void AddRefForRegistration()
{
AddRefInternal();
}
void ReleaseForRegistration()
{
m_object = nullptr;
ReleaseInternal();
}
};
template <class T>
class StaticCoreRefCountObject
{
void AddRefInternal()
{
#ifdef WIN32
InterlockedIncrement((LONG*)&m_refs);
#else
__sync_fetch_and_add(&m_refs, 1);
#endif
}
void ReleaseInternal()
{
#ifdef WIN32
if (InterlockedDecrement((LONG*)&m_refs) == 0)
delete this;
#else
if (__sync_fetch_and_add(&m_refs, -1) == 1)
delete this;
#endif
}
public:
int m_refs;
T* m_object;
StaticCoreRefCountObject(): m_refs(0), m_object(nullptr) {}
virtual ~StaticCoreRefCountObject() {}
T* GetObject() { return m_object; }
void AddRef()
{
AddRefInternal();
}
void Release()
{
ReleaseInternal();
}
void AddRefForRegistration()
{
AddRefInternal();
}
};
template <class T>
class Ref
{
T* m_obj;
public:
Ref<T>(): m_obj(NULL)
{
}
Ref<T>(T* obj): m_obj(obj)
{
if (m_obj)
m_obj->AddRef();
}
Ref<T>(const Ref<T>& obj): m_obj(obj.m_obj)
{
if (m_obj)
m_obj->AddRef();
}
~Ref<T>()
{
if (m_obj)
m_obj->Release();
}
Ref<T>& operator=(const Ref<T>& obj)
{
T* oldObj = m_obj;
m_obj = obj.m_obj;
if (m_obj)
m_obj->AddRef();
if (oldObj)
oldObj->Release();
return *this;
}
Ref<T>& operator=(T* obj)
{
T* oldObj = m_obj;
m_obj = obj;
if (m_obj)
m_obj->AddRef();
if (oldObj)
oldObj->Release();
return *this;
}
operator T*() const
{
return m_obj;
}
T* operator->() const
{
return m_obj;
}
T& operator*() const
{
return *m_obj;
}
bool operator!() const
{
return m_obj == NULL;
}
T* GetPtr() const
{
return m_obj;
}
};
class LogListener
{
static void LogMessageCallback(void* ctxt, BNLogLevel level, const char* msg);
static void CloseLogCallback(void* ctxt);
static BNLogLevel GetLogLevelCallback(void* ctxt);
public:
virtual ~LogListener() {}
static void RegisterLogListener(LogListener* listener);
static void UnregisterLogListener(LogListener* listener);
static void UpdateLogListeners();
virtual void LogMessage(BNLogLevel level, const std::string& msg) = 0;
virtual void CloseLog() {}
virtual BNLogLevel GetLogLevel() { return WarningLog; }
};
class Architecture;
class Platform;
class Type;
class DataBuffer;
/*! Logs to the error console with the given BNLogLevel.
\param level BNLogLevel debug log level
\param fmt C-style format string.
\param ... Variable arguments corresponding to the format string.
*/
void Log(BNLogLevel level, const char* fmt, ...);
/*! LogDebug only writes text to the error console if the console is set to log level: DebugLog
Log level DebugLog is the most verbose logging level.
\param fmt C-style format string.
\param ... Variable arguments corresponding to the format string.
*/
void LogDebug(const char* fmt, ...);
/*! LogInfo always writes text to the error console, and corresponds to the log level: InfoLog.
Log level InfoLog is the second most verbose logging level.
\param fmt C-style format string.
\param ... Variable arguments corresponding to the format string.
*/
void LogInfo(const char* fmt, ...);
/*! LogWarn writes text to the error console including a warning icon,
and also shows a warning icon in the bottom pane. LogWarn corresponds to the log level: WarningLog.
\param fmt C-style format string.
\param ... Variable arguments corresponding to the format string.
*/
void LogWarn(const char* fmt, ...);
/*! LogError writes text to the error console and pops up the error console. Additionall,
Errors in the console log include a error icon. LogError corresponds to the log level: ErrorLog.
\param fmt C-style format string.
\param ... Variable arguments corresponding to the format string.
*/
void LogError(const char* fmt, ...);
/*! LogAlert pops up a message box displaying the alert message and logs to the error console.
LogAlert corresponds to the log level: AlertLog.
\param fmt C-style format string.
\param ... Variable arguments corresponding to the format string.
*/
void LogAlert(const char* fmt, ...);
void LogToStdout(BNLogLevel minimumLevel);
void LogToStderr(BNLogLevel minimumLevel);
bool LogToFile(BNLogLevel minimumLevel, const std::string& path, bool append = false);
void CloseLogs();
std::string EscapeString(const std::string& s);
std::string UnescapeString(const std::string& s);
bool PreprocessSource(const std::string& source, const std::string& fileName,
std::string& output, std::string& errors,
const std::vector<std::string>& includeDirs = std::vector<std::string>());
void InitCorePlugins();
void InitUserPlugins();
std::string GetBundledPluginDirectory();
void SetBundledPluginDirectory(const std::string& path);
std::string GetUserPluginDirectory();
std::string GetPathRelativeToBundledPluginDirectory(const std::string& path);
std::string GetPathRelativeToUserPluginDirectory(const std::string& path);
bool ExecuteWorkerProcess(const std::string& path, const std::vector<std::string>& args, const DataBuffer& input,
std::string& output, std::string& errors);
std::string GetVersionString();
uint32_t GetBuildId();
bool AreAutoUpdatesEnabled();
void SetAutoUpdatesEnabled(bool enabled);
uint64_t GetTimeSinceLastUpdateCheck();
void UpdatesChecked();
std::string GetActiveUpdateChannel();
void SetActiveUpdateChannel(const std::string& channel);
void SetCurrentPluginLoadOrder(BNPluginLoadOrder order);
void AddRequiredPluginDependency(const std::string& name);
void AddOptionalPluginDependency(const std::string& name);
bool DemangleMS(Architecture* arch,
const std::string& mangledName,
Type** outType,
std::vector<std::string>& outVarName);
class DataBuffer
{
BNDataBuffer* m_buffer;
public:
DataBuffer();
DataBuffer(size_t len);
DataBuffer(const void* data, size_t len);
DataBuffer(const DataBuffer& buf);
DataBuffer(BNDataBuffer* buf);
~DataBuffer();
DataBuffer& operator=(const DataBuffer& buf);
BNDataBuffer* GetBufferObject() const { return m_buffer; }
void* GetData();
const void* GetData() const;
void* GetDataAt(size_t offset);
const void* GetDataAt(size_t offset) const;
size_t GetLength() const;
void SetSize(size_t len);
void Clear();
void Append(const void* data, size_t len);
void Append(const DataBuffer& buf);
void AppendByte(uint8_t val);
DataBuffer GetSlice(size_t start, size_t len);
uint8_t& operator[](size_t offset);
const uint8_t& operator[](size_t offset) const;
std::string ToEscapedString() const;
static DataBuffer FromEscapedString(const std::string& src);
std::string ToBase64() const;
static DataBuffer FromBase64(const std::string& src);
bool ZlibCompress(DataBuffer& output) const;
bool ZlibDecompress(DataBuffer& output) const;
};
class TemporaryFile: public CoreRefCountObject<BNTemporaryFile, BNNewTemporaryFileReference, BNFreeTemporaryFile>
{
public:
TemporaryFile();
TemporaryFile(const DataBuffer& contents);
TemporaryFile(const std::string& contents);
TemporaryFile(BNTemporaryFile* file);
bool IsValid() const { return m_object != nullptr; }
std::string GetPath() const;
DataBuffer GetContents();
};
class NavigationHandler
{
private:
BNNavigationHandler m_callbacks;
static char* GetCurrentViewCallback(void* ctxt);
static uint64_t GetCurrentOffsetCallback(void* ctxt);
static bool NavigateCallback(void* ctxt, const char* view, uint64_t offset);
public:
NavigationHandler();
virtual ~NavigationHandler() {}
BNNavigationHandler* GetCallbacks() { return &m_callbacks; }
virtual std::string GetCurrentView() = 0;
virtual uint64_t GetCurrentOffset() = 0;
virtual bool Navigate(const std::string& view, uint64_t offset) = 0;
};
class BinaryView;
class UndoAction
{
private:
std::string m_typeName;
BNActionType m_actionType;
static void FreeCallback(void* ctxt);
static void UndoCallback(void* ctxt, BNBinaryView* data);
static void RedoCallback(void* ctxt, BNBinaryView* data);
static char* SerializeCallback(void* ctxt);
public:
UndoAction(const std::string& name, BNActionType action);
virtual ~UndoAction() {}
const std::string& GetTypeName() const { return m_typeName; }
BNActionType GetActionType() const { return m_actionType; }
BNUndoAction GetCallbacks();
void Add(BNBinaryView* view);
virtual void Undo(BinaryView* data) = 0;
virtual void Redo(BinaryView* data) = 0;
virtual Json::Value Serialize() = 0;
};
class UndoActionType
{
protected:
std::string m_nameForRegister;
static bool DeserializeCallback(void* ctxt, const char* data, BNUndoAction* result);
public:
UndoActionType(const std::string& name);
virtual ~UndoActionType() {}
static void Register(UndoActionType* type);
virtual UndoAction* Deserialize(const Json::Value& data) = 0;
};
class FileMetadata: public CoreRefCountObject<BNFileMetadata, BNNewFileReference, BNFreeFileMetadata>
{
public:
FileMetadata();
FileMetadata(const std::string& filename);
FileMetadata(BNFileMetadata* file);
void Close();
void SetNavigationHandler(NavigationHandler* handler);
std::string GetFilename() const;
void SetFilename(const std::string& name);
bool IsModified() const;
bool IsAnalysisChanged() const;
void MarkFileModified();
void MarkFileSaved();
bool IsBackedByDatabase() const;
bool CreateDatabase(const std::string& name, BinaryView* data);
Ref<BinaryView> OpenExistingDatabase(const std::string& path);
bool SaveAutoSnapshot(BinaryView* data);
void BeginUndoActions();
void CommitUndoActions();
bool Undo();
bool Redo();
std::string GetCurrentView();
uint64_t GetCurrentOffset();
bool Navigate(const std::string& view, uint64_t offset);
BinaryNinja::Ref<BinaryNinja::BinaryView> GetViewOfType(const std::string& name);
};
class BinaryView;
class Function;
struct DataVariable;
class BinaryDataNotification
{
private:
BNBinaryDataNotification m_callbacks;
static void DataWrittenCallback(void* ctxt, BNBinaryView* data, uint64_t offset, size_t len);
static void DataInsertedCallback(void* ctxt, BNBinaryView* data, uint64_t offset, size_t len);
static void DataRemovedCallback(void* ctxt, BNBinaryView* data, uint64_t offset, uint64_t len);
static void FunctionAddedCallback(void* ctxt, BNBinaryView* data, BNFunction* func);
static void FunctionRemovedCallback(void* ctxt, BNBinaryView* data, BNFunction* func);
static void FunctionUpdatedCallback(void* ctxt, BNBinaryView* data, BNFunction* func);
static void DataVariableAddedCallback(void* ctxt, BNBinaryView* data, BNDataVariable* var);
static void DataVariableRemovedCallback(void* ctxt, BNBinaryView* data, BNDataVariable* var);
static void DataVariableUpdatedCallback(void* ctxt, BNBinaryView* data, BNDataVariable* var);
static void StringFoundCallback(void* ctxt, BNBinaryView* data, BNStringType type, uint64_t offset, size_t len);
static void StringRemovedCallback(void* ctxt, BNBinaryView* data, BNStringType type, uint64_t offset, size_t len);
public:
BinaryDataNotification();
virtual ~BinaryDataNotification() {}
BNBinaryDataNotification* GetCallbacks() { return &m_callbacks; }
virtual void OnBinaryDataWritten(BinaryView* view, uint64_t offset, size_t len) { (void)view; (void)offset; (void)len; }
virtual void OnBinaryDataInserted(BinaryView* view, uint64_t offset, size_t len) { (void)view; (void)offset; (void)len; }
virtual void OnBinaryDataRemoved(BinaryView* view, uint64_t offset, uint64_t len) { (void)view; (void)offset; (void)len; }
virtual void OnAnalysisFunctionAdded(BinaryView* view, Function* func) { (void)view; (void)func; }
virtual void OnAnalysisFunctionRemoved(BinaryView* view, Function* func) { (void)view; (void)func; }
virtual void OnAnalysisFunctionUpdated(BinaryView* view, Function* func) { (void)view; (void)func; }
virtual void OnDataVariableAdded(BinaryView* view, const DataVariable& var) { (void)view; (void)var; }
virtual void OnDataVariableRemoved(BinaryView* view, const DataVariable& var) { (void)view; (void)var; }
virtual void OnDataVariableUpdated(BinaryView* view, const DataVariable& var) { (void)view; (void)var; }
virtual void OnStringFound(BinaryView* data, BNStringType type, uint64_t offset, size_t len) { (void)data; (void)type; (void)offset; (void)len; }
virtual void OnStringRemoved(BinaryView* data, BNStringType type, uint64_t offset, size_t len) { (void)data; (void)type; (void)offset; (void)len; }
};
class FileAccessor
{
protected:
BNFileAccessor m_callbacks;
private:
static uint64_t GetLengthCallback(void* ctxt);
static size_t ReadCallback(void* ctxt, void* dest, uint64_t offset, size_t len);
static size_t WriteCallback(void* ctxt, uint64_t offset, const void* src, size_t len);
public:
FileAccessor();
FileAccessor(BNFileAccessor* accessor);
virtual ~FileAccessor() {}
BNFileAccessor* GetCallbacks() { return &m_callbacks; }
virtual bool IsValid() const = 0;
virtual uint64_t GetLength() const = 0;
virtual size_t Read(void* dest, uint64_t offset, size_t len) = 0;
virtual size_t Write(uint64_t offset, const void* src, size_t len) = 0;
};
class CoreFileAccessor: public FileAccessor
{
public:
CoreFileAccessor(BNFileAccessor* accessor);
virtual bool IsValid() const override { return true; }
virtual uint64_t GetLength() const override;
virtual size_t Read(void* dest, uint64_t offset, size_t len) override;
virtual size_t Write(uint64_t offset, const void* src, size_t len) override;
};
class Function;
class BasicBlock;
class Symbol: public CoreRefCountObject<BNSymbol, BNNewSymbolReference, BNFreeSymbol>
{
public:
Symbol(BNSymbolType type, const std::string& shortName, const std::string& fullName,
const std::string& rawName, uint64_t addr);
Symbol(BNSymbolType type, const std::string& name, uint64_t addr);
Symbol(BNSymbol* sym);
BNSymbolType GetType() const;
std::string GetShortName() const;
std::string GetFullName() const;
std::string GetRawName() const;
uint64_t GetAddress() const;
bool IsAutoDefined() const;
void SetAutoDefined(bool val);
static Ref<Symbol> ImportedFunctionFromImportAddressSymbol(Symbol* sym, uint64_t addr);
};
struct ReferenceSource
{
Ref<Function> func;
Ref<Architecture> arch;
uint64_t addr;
};
struct InstructionTextToken
{
BNInstructionTextTokenType type;
std::string text;
uint64_t value;
size_t size, operand;
InstructionTextToken();
InstructionTextToken(BNInstructionTextTokenType type, const std::string& text, uint64_t value = 0,
size_t size = 0, size_t operand = BN_INVALID_OPERAND);
};
struct DisassemblyTextLine
{
uint64_t addr;
std::vector<InstructionTextToken> tokens;
};
struct LinearDisassemblyPosition
{
Ref<Function> function;
Ref<BasicBlock> block;
uint64_t address;
};
struct LinearDisassemblyLine
{
BNLinearDisassemblyLineType type;
Ref<Function> function;
Ref<BasicBlock> block;
size_t lineOffset;
DisassemblyTextLine contents;
};
class DisassemblySettings;
class AnalysisCompletionEvent: public CoreRefCountObject<BNAnalysisCompletionEvent,
BNNewAnalysisCompletionEventReference, BNFreeAnalysisCompletionEvent>
{
protected:
std::function<void()> m_callback;
std::recursive_mutex m_mutex;
static void CompletionCallback(void* ctxt);
public:
AnalysisCompletionEvent(BinaryView* view, const std::function<void()>& callback);
void Cancel();
};
struct DataVariable
{
uint64_t address;
Ref<Type> type;
bool autoDiscovered;
};
struct NameAndType;
/*! BinaryView is the base class for creating views on binary data (e.g. ELF, PE, Mach-O).
BinaryView should be subclassed to create a new BinaryView
*/
class BinaryView: public CoreRefCountObject<BNBinaryView, BNNewViewReference, BNFreeBinaryView>
{
protected:
Ref<FileMetadata> m_file; //!< The underlying file
/*! BinaryView constructor
\param typeName name of the BinaryView (e.g. ELF, PE, Mach-O, ...)
\param file a file to create a view from
*/
BinaryView(const std::string& typeName, FileMetadata* file);
/*! PerformRead provides a mapping between the flat file and virtual offsets in the file.
\param dest the address to write len number of bytes.
\param offset the virtual offset to find and read len bytes from
....\param len the number of bytes to read from offset and write to dest
*/
virtual size_t PerformRead(void* dest, uint64_t offset, size_t len) { (void)dest; (void)offset; (void)len; return 0; }
virtual size_t PerformWrite(uint64_t offset, const void* data, size_t len) { (void)offset; (void)data; (void)len; return 0; }
virtual size_t PerformInsert(uint64_t offset, const void* data, size_t len) { (void)offset; (void)data; (void)len; return 0; }
virtual size_t PerformRemove(uint64_t offset, uint64_t len) { (void)offset; (void)len; return 0; }
virtual BNModificationStatus PerformGetModification(uint64_t offset) { (void)offset; return Original; }
virtual bool PerformIsValidOffset(uint64_t offset);
virtual bool PerformIsOffsetReadable(uint64_t offset);
virtual bool PerformIsOffsetWritable(uint64_t offset);
virtual bool PerformIsOffsetExecutable(uint64_t offset);
virtual uint64_t PerformGetNextValidOffset(uint64_t offset);
virtual uint64_t PerformGetStart() const { return 0; }
virtual uint64_t PerformGetLength() const { return 0; }
virtual uint64_t PerformGetEntryPoint() const { return 0; }
virtual bool PerformIsExecutable() const { return false; }
virtual BNEndianness PerformGetDefaultEndianness() const;
virtual size_t PerformGetAddressSize() const;
virtual bool PerformSave(FileAccessor* file) { (void)file; return false; }
void NotifyDataWritten(uint64_t offset, size_t len);
void NotifyDataInserted(uint64_t offset, size_t len);
void NotifyDataRemoved(uint64_t offset, uint64_t len);
private:
static bool InitCallback(void* ctxt);
static void FreeCallback(void* ctxt);
static size_t ReadCallback(void* ctxt, void* dest, uint64_t offset, size_t len);
static size_t WriteCallback(void* ctxt, uint64_t offset, const void* src, size_t len);
static size_t InsertCallback(void* ctxt, uint64_t offset, const void* src, size_t len);
static size_t RemoveCallback(void* ctxt, uint64_t offset, uint64_t len);
static BNModificationStatus GetModificationCallback(void* ctxt, uint64_t offset);
static bool IsValidOffsetCallback(void* ctxt, uint64_t offset);
static bool IsOffsetReadableCallback(void* ctxt, uint64_t offset);
static bool IsOffsetWritableCallback(void* ctxt, uint64_t offset);
static bool IsOffsetExecutableCallback(void* ctxt, uint64_t offset);
static uint64_t GetNextValidOffsetCallback(void* ctxt, uint64_t offset);
static uint64_t GetStartCallback(void* ctxt);
static uint64_t GetLengthCallback(void* ctxt);
static uint64_t GetEntryPointCallback(void* ctxt);
static bool IsExecutableCallback(void* ctxt);
static BNEndianness GetDefaultEndiannessCallback(void* ctxt);
static size_t GetAddressSizeCallback(void* ctxt);
static bool SaveCallback(void* ctxt, BNFileAccessor* file);
public:
BinaryView(BNBinaryView* view);
virtual bool Init() { return true; }
FileMetadata* GetFile() const { return m_file; }
std::string GetTypeName() const;
bool IsModified() const;
bool IsAnalysisChanged() const;
bool IsBackedByDatabase() const;
bool CreateDatabase(const std::string& path);
bool SaveAutoSnapshot();
void BeginUndoActions();
void AddUndoAction(UndoAction* action);
void CommitUndoActions();
bool Undo();
bool Redo();
std::string GetCurrentView();
uint64_t GetCurrentOffset();
bool Navigate(const std::string& view, uint64_t offset);
size_t Read(void* dest, uint64_t offset, size_t len);
DataBuffer ReadBuffer(uint64_t offset, size_t len);
size_t Write(uint64_t offset, const void* data, size_t len);
size_t WriteBuffer(uint64_t offset, const DataBuffer& data);
size_t Insert(uint64_t offset, const void* data, size_t len);
size_t InsertBuffer(uint64_t offset, const DataBuffer& data);
size_t Remove(uint64_t offset, uint64_t len);
BNModificationStatus GetModification(uint64_t offset);
std::vector<BNModificationStatus> GetModification(uint64_t offset, size_t len);
bool IsValidOffset(uint64_t offset) const;
bool IsOffsetReadable(uint64_t offset) const;
bool IsOffsetWritable(uint64_t offset) const;
bool IsOffsetExecutable(uint64_t offset) const;
uint64_t GetNextValidOffset(uint64_t offset) const;
uint64_t GetStart() const;
uint64_t GetEnd() const;
uint64_t GetLength() const;
uint64_t GetEntryPoint() const;
Ref<Architecture> GetDefaultArchitecture() const;
void SetDefaultArchitecture(Architecture* arch);
Ref<Platform> GetDefaultPlatform() const;
void SetDefaultPlatform(Platform* platform);
BNEndianness GetDefaultEndianness() const;
size_t GetAddressSize() const;
bool IsExecutable() const;
bool Save(FileAccessor* file);
bool Save(const std::string& path);
void RegisterNotification(BinaryDataNotification* notify);
void UnregisterNotification(BinaryDataNotification* notify);
void AddFunctionForAnalysis(Platform* platform, uint64_t addr);
void AddEntryPointForAnalysis(Platform* platform, uint64_t start);
void RemoveAnalysisFunction(Function* func);
void CreateUserFunction(Platform* platform, uint64_t start);
void RemoveUserFunction(Function* func);
void UpdateAnalysis();
void AbortAnalysis();
void DefineDataVariable(uint64_t addr, Type* type);
void DefineUserDataVariable(uint64_t addr, Type* type);
void UndefineDataVariable(uint64_t addr);
void UndefineUserDataVariable(uint64_t addr);
std::map<uint64_t, DataVariable> GetDataVariables();
bool GetDataVariableAtAddress(uint64_t addr, DataVariable& var);
std::vector<Ref<Function>> GetAnalysisFunctionList();
bool HasFunctions() const;
Ref<Function> GetAnalysisFunction(Platform* platform, uint64_t addr);
Ref<Function> GetRecentAnalysisFunctionForAddress(uint64_t addr);
std::vector<Ref<Function>> GetAnalysisFunctionsForAddress(uint64_t addr);
Ref<Function> GetAnalysisEntryPoint();
Ref<BasicBlock> GetRecentBasicBlockForAddress(uint64_t addr);
std::vector<Ref<BasicBlock>> GetBasicBlocksForAddress(uint64_t addr);
std::vector<ReferenceSource> GetCodeReferences(uint64_t addr);
std::vector<ReferenceSource> GetCodeReferences(uint64_t addr, uint64_t len);
Ref<Symbol> GetSymbolByAddress(uint64_t addr);
Ref<Symbol> GetSymbolByRawName(const std::string& name);
std::vector<Ref<Symbol>> GetSymbolsByName(const std::string& name);
std::vector<Ref<Symbol>> GetSymbols();
std::vector<Ref<Symbol>> GetSymbols(uint64_t start, uint64_t len);
std::vector<Ref<Symbol>> GetSymbolsOfType(BNSymbolType type);
std::vector<Ref<Symbol>> GetSymbolsOfType(BNSymbolType type, uint64_t start, uint64_t len);
void DefineAutoSymbol(Symbol* sym);
void UndefineAutoSymbol(Symbol* sym);
void DefineUserSymbol(Symbol* sym);
void UndefineUserSymbol(Symbol* sym);
void DefineImportedFunction(Symbol* importAddressSym, Function* func);
bool IsNeverBranchPatchAvailable(Architecture* arch, uint64_t addr);
bool IsAlwaysBranchPatchAvailable(Architecture* arch, uint64_t addr);
bool IsInvertBranchPatchAvailable(Architecture* arch, uint64_t addr);
bool IsSkipAndReturnZeroPatchAvailable(Architecture* arch, uint64_t addr);
bool IsSkipAndReturnValuePatchAvailable(Architecture* arch, uint64_t addr);
bool ConvertToNop(Architecture* arch, uint64_t addr);
bool AlwaysBranch(Architecture* arch, uint64_t addr);
bool InvertBranch(Architecture* arch, uint64_t addr);
bool SkipAndReturnValue(Architecture* arch, uint64_t addr, uint64_t value);
size_t GetInstructionLength(Architecture* arch, uint64_t addr);
std::vector<BNStringReference> GetStrings();
std::vector<BNStringReference> GetStrings(uint64_t start, uint64_t len);
Ref<AnalysisCompletionEvent> AddAnalysisCompletionEvent(const std::function<void()>& callback);
BNAnalysisProgress GetAnalysisProgress();
uint64_t GetNextFunctionStartAfterAddress(uint64_t addr);
uint64_t GetNextBasicBlockStartAfterAddress(uint64_t addr);
uint64_t GetNextDataAfterAddress(uint64_t addr);
uint64_t GetNextDataVariableAfterAddress(uint64_t addr);
uint64_t GetPreviousFunctionStartBeforeAddress(uint64_t addr);
uint64_t GetPreviousBasicBlockStartBeforeAddress(uint64_t addr);
uint64_t GetPreviousBasicBlockEndBeforeAddress(uint64_t addr);
uint64_t GetPreviousDataBeforeAddress(uint64_t addr);
uint64_t GetPreviousDataVariableBeforeAddress(uint64_t addr);
LinearDisassemblyPosition GetLinearDisassemblyPositionForAddress(uint64_t addr, DisassemblySettings* settings);
std::vector<LinearDisassemblyLine> GetPreviousLinearDisassemblyLines(LinearDisassemblyPosition& pos,
DisassemblySettings* settings);
std::vector<LinearDisassemblyLine> GetNextLinearDisassemblyLines(LinearDisassemblyPosition& pos,
DisassemblySettings* settings);
bool ParseTypeString(const std::string& text, NameAndType& result, std::string& errors);
std::map<std::string, Ref<Type>> GetTypes();
Ref<Type> GetTypeByName(const std::string& name);
bool IsTypeAutoDefined(const std::string& name);
void DefineType(const std::string& name, Type* type);
void DefineUserType(const std::string& name, Type* type);
void UndefineType(const std::string& name);
void UndefineUserType(const std::string& name);
};
class BinaryData: public BinaryView
{
public:
BinaryData(FileMetadata* file);
BinaryData(FileMetadata* file, const DataBuffer& data);
BinaryData(FileMetadata* file, const void* data, size_t len);
BinaryData(FileMetadata* file, const std::string& path);
BinaryData(FileMetadata* file, FileAccessor* accessor);
};
class Platform;
class BinaryViewType: public StaticCoreRefCountObject<BNBinaryViewType>
{
protected:
std::string m_nameForRegister, m_longNameForRegister;
static BNBinaryView* CreateCallback(void* ctxt, BNBinaryView* data);
static bool IsValidCallback(void* ctxt, BNBinaryView* data);
BinaryViewType(BNBinaryViewType* type);
public:
BinaryViewType(const std::string& name, const std::string& longName);
virtual ~BinaryViewType() {}
static void Register(BinaryViewType* type);
static Ref<BinaryViewType> GetByName(const std::string& name);
static std::vector<Ref<BinaryViewType>> GetViewTypes();
static std::vector<Ref<BinaryViewType>> GetViewTypesForData(BinaryView* data);
static void RegisterArchitecture(const std::string& name, uint32_t id, BNEndianness endian, Architecture* arch);
void RegisterArchitecture(uint32_t id, BNEndianness endian, Architecture* arch);
Ref<Architecture> GetArchitecture(uint32_t id, BNEndianness endian);
static void RegisterPlatform(const std::string& name, uint32_t id, Architecture* arch, Platform* platform);
static void RegisterDefaultPlatform(const std::string& name, Architecture* arch, Platform* platform);
void RegisterPlatform(uint32_t id, Architecture* arch, Platform* platform);
void RegisterDefaultPlatform(Architecture* arch, Platform* platform);
Ref<Platform> GetPlatform(uint32_t id, Architecture* arch);
std::string GetName();
std::string GetLongName();
virtual BinaryView* Create(BinaryView* data) = 0;
virtual bool IsTypeValidForData(BinaryView* data) = 0;
};
class CoreBinaryViewType: public BinaryViewType
{
public:
CoreBinaryViewType(BNBinaryViewType* type);
virtual BinaryView* Create(BinaryView* data) override;
virtual bool IsTypeValidForData(BinaryView* data) override;
};
class ReadException: public std::exception
{
public:
ReadException(): std::exception() {}
virtual const char* what() const NOEXCEPT { return "read out of bounds"; }
};
class BinaryReader
{
Ref<BinaryView> m_view;
BNBinaryReader* m_stream;
public:
BinaryReader(BinaryView* data, BNEndianness endian = LittleEndian);
~BinaryReader();
BNEndianness GetEndianness() const;
void SetEndianness(BNEndianness endian);
void Read(void* dest, size_t len);
DataBuffer Read(size_t len);
std::string ReadString(size_t len);
uint8_t Read8();
uint16_t Read16();
uint32_t Read32();
uint64_t Read64();
uint16_t ReadLE16();
uint32_t ReadLE32();
uint64_t ReadLE64();
uint16_t ReadBE16();
uint32_t ReadBE32();
uint64_t ReadBE64();
bool TryRead(void* dest, size_t len);
bool TryRead(DataBuffer& dest, size_t len);
bool TryReadString(std::string& dest, size_t len);
bool TryRead8(uint8_t& result);
bool TryRead16(uint16_t& result);
bool TryRead32(uint32_t& result);
bool TryRead64(uint64_t& result);
bool TryReadLE16(uint16_t& result);
bool TryReadLE32(uint32_t& result);
bool TryReadLE64(uint64_t& result);
bool TryReadBE16(uint16_t& result);
bool TryReadBE32(uint32_t& result);
bool TryReadBE64(uint64_t& result);
uint64_t GetOffset() const;
void Seek(uint64_t offset);
void SeekRelative(int64_t offset);
bool IsEndOfFile() const;
};
class WriteException: public std::exception
{
public:
WriteException(): std::exception() {}
virtual const char* what() const NOEXCEPT { return "write out of bounds"; }
};
class BinaryWriter
{
Ref<BinaryView> m_view;
BNBinaryWriter* m_stream;
public:
BinaryWriter(BinaryView* data, BNEndianness endian = LittleEndian);
~BinaryWriter();
BNEndianness GetEndianness() const;
void SetEndianness(BNEndianness endian);
void Write(const void* src, size_t len);
void Write(const DataBuffer& buf);
void Write(const std::string& str);
void Write8(uint8_t val);
void Write16(uint16_t val);
void Write32(uint32_t val);
void Write64(uint64_t val);
void WriteLE16(uint16_t val);
void WriteLE32(uint32_t val);
void WriteLE64(uint64_t val);
void WriteBE16(uint16_t val);
void WriteBE32(uint32_t val);
void WriteBE64(uint64_t val);
bool TryWrite(const void* src, size_t len);
bool TryWrite(const DataBuffer& buf);
bool TryWrite(const std::string& str);
bool TryWrite8(uint8_t val);
bool TryWrite16(uint16_t val);
bool TryWrite32(uint32_t val);
bool TryWrite64(uint64_t val);
bool TryWriteLE16(uint16_t val);
bool TryWriteLE32(uint32_t val);
bool TryWriteLE64(uint64_t val);
bool TryWriteBE16(uint16_t val);
bool TryWriteBE32(uint32_t val);
bool TryWriteBE64(uint64_t val);
uint64_t GetOffset() const;
void Seek(uint64_t offset);
void SeekRelative(int64_t offset);
};
struct TransformParameter
{
std::string name, longName;
size_t fixedLength; // Variable length if zero
};
class Transform: public StaticCoreRefCountObject<BNTransform>
{
protected:
BNTransformType m_typeForRegister;
std::string m_nameForRegister, m_longNameForRegister, m_groupForRegister;
Transform(BNTransform* xform);
static BNTransformParameterInfo* GetParametersCallback(void* ctxt, size_t* count);
static void FreeParametersCallback(BNTransformParameterInfo* params, size_t count);
static bool DecodeCallback(void* ctxt, BNDataBuffer* input, BNDataBuffer* output, BNTransformParameter* params, size_t paramCount);
static bool EncodeCallback(void* ctxt, BNDataBuffer* input, BNDataBuffer* output, BNTransformParameter* params, size_t paramCount);
static std::vector<TransformParameter> EncryptionKeyParameters(size_t fixedKeyLength = 0);
static std::vector<TransformParameter> EncryptionKeyAndIVParameters(size_t fixedKeyLength = 0, size_t fixedIVLength = 0);
public:
Transform(BNTransformType type, const std::string& name, const std::string& longName, const std::string& group);
static void Register(Transform* xform);
static Ref<Transform> GetByName(const std::string& name);
static std::vector<Ref<Transform>> GetTransformTypes();
BNTransformType GetType() const;
std::string GetName() const;
std::string GetLongName() const;
std::string GetGroup() const;
virtual std::vector<TransformParameter> GetParameters() const;
virtual bool Decode(const DataBuffer& input, DataBuffer& output, const std::map<std::string, DataBuffer>& params =
std::map<std::string, DataBuffer>());
virtual bool Encode(const DataBuffer& input, DataBuffer& output, const std::map<std::string, DataBuffer>& params =
std::map<std::string, DataBuffer>());
};
class CoreTransform: public Transform
{
public:
CoreTransform(BNTransform* xform);
virtual std::vector<TransformParameter> GetParameters() const override;
virtual bool Decode(const DataBuffer& input, DataBuffer& output, const std::map<std::string, DataBuffer>& params =
std::map<std::string, DataBuffer>()) override;
virtual bool Encode(const DataBuffer& input, DataBuffer& output, const std::map<std::string, DataBuffer>& params =
std::map<std::string, DataBuffer>()) override;
};
struct InstructionInfo: public BNInstructionInfo
{
InstructionInfo();
void AddBranch(BNBranchType type, uint64_t target = 0, Architecture* arch = nullptr, bool hasDelaySlot = false);
};
class LowLevelILFunction;
class FunctionRecognizer;
class CallingConvention;
typedef size_t ExprId;
/*!
The Architecture class is the base class for all CPU architectures. This provides disassembly, assembly,
patching, and IL translation lifting for a given architecture.
*/
class Architecture: public StaticCoreRefCountObject<BNArchitecture>
{
protected:
std::string m_nameForRegister;
Architecture(BNArchitecture* arch);
static void InitCallback(void* ctxt, BNArchitecture* obj);
static BNEndianness GetEndiannessCallback(void* ctxt);
static size_t GetAddressSizeCallback(void* ctxt);
static size_t GetDefaultIntegerSizeCallback(void* ctxt);
static size_t GetMaxInstructionLengthCallback(void* ctxt);
static size_t GetOpcodeDisplayLengthCallback(void* ctxt);
static bool GetInstructionInfoCallback(void* ctxt, const uint8_t* data, uint64_t addr,
size_t maxLen, BNInstructionInfo* result);
static bool GetInstructionTextCallback(void* ctxt, const uint8_t* data, uint64_t addr,
size_t* len, BNInstructionTextToken** result, size_t* count);
static void FreeInstructionTextCallback(BNInstructionTextToken* tokens, size_t count);
static bool GetInstructionLowLevelILCallback(void* ctxt, const uint8_t* data, uint64_t addr,
size_t* len, BNLowLevelILFunction* il);
static char* GetRegisterNameCallback(void* ctxt, uint32_t reg);
static char* GetFlagNameCallback(void* ctxt, uint32_t flag);
static char* GetFlagWriteTypeNameCallback(void* ctxt, uint32_t flags);
static uint32_t* GetFullWidthRegistersCallback(void* ctxt, size_t* count);
static uint32_t* GetAllRegistersCallback(void* ctxt, size_t* count);
static uint32_t* GetAllFlagsCallback(void* ctxt, size_t* count);
static uint32_t* GetAllFlagWriteTypesCallback(void* ctxt, size_t* count);
static BNFlagRole GetFlagRoleCallback(void* ctxt, uint32_t flag);
static uint32_t* GetFlagsRequiredForFlagConditionCallback(void* ctxt, BNLowLevelILFlagCondition cond, size_t* count);
static uint32_t* GetFlagsWrittenByFlagWriteTypeCallback(void* ctxt, uint32_t writeType, size_t* count);
static size_t GetFlagWriteLowLevelILCallback(void* ctxt, BNLowLevelILOperation op, size_t size, uint32_t flagWriteType,
uint32_t flag, BNRegisterOrConstant* operands, size_t operandCount, BNLowLevelILFunction* il);
static size_t GetFlagConditionLowLevelILCallback(void* ctxt, BNLowLevelILFlagCondition cond,
BNLowLevelILFunction* il);
static void FreeRegisterListCallback(void* ctxt, uint32_t* regs);
static void GetRegisterInfoCallback(void* ctxt, uint32_t reg, BNRegisterInfo* result);
static uint32_t GetStackPointerRegisterCallback(void* ctxt);
static uint32_t GetLinkRegisterCallback(void* ctxt);
static bool AssembleCallback(void* ctxt, const char* code, uint64_t addr, BNDataBuffer* result, char** errors);
static bool IsNeverBranchPatchAvailableCallback(void* ctxt, const uint8_t* data, uint64_t addr, size_t len);
static bool IsAlwaysBranchPatchAvailableCallback(void* ctxt, const uint8_t* data, uint64_t addr, size_t len);
static bool IsInvertBranchPatchAvailableCallback(void* ctxt, const uint8_t* data, uint64_t addr, size_t len);
static bool IsSkipAndReturnZeroPatchAvailableCallback(void* ctxt, const uint8_t* data, uint64_t addr, size_t len);
static bool IsSkipAndReturnValuePatchAvailableCallback(void* ctxt, const uint8_t* data, uint64_t addr, size_t len);
static bool ConvertToNopCallback(void* ctxt, uint8_t* data, uint64_t addr, size_t len);
static bool AlwaysBranchCallback(void* ctxt, uint8_t* data, uint64_t addr, size_t len);
static bool InvertBranchCallback(void* ctxt, uint8_t* data, uint64_t addr, size_t len);
static bool SkipAndReturnValueCallback(void* ctxt, uint8_t* data, uint64_t addr, size_t len, uint64_t value);
public:
Architecture(const std::string& name);
static void Register(Architecture* arch);
static Ref<Architecture> GetByName(const std::string& name);
static std::vector<Ref<Architecture>> GetList();
std::string GetName() const;
virtual BNEndianness GetEndianness() const = 0;
virtual size_t GetAddressSize() const = 0;
virtual size_t GetDefaultIntegerSize() const;
virtual size_t GetMaxInstructionLength() const;
virtual size_t GetOpcodeDisplayLength() const;
virtual bool GetInstructionInfo(const uint8_t* data, uint64_t addr, size_t maxLen, InstructionInfo& result) = 0;
virtual bool GetInstructionText(const uint8_t* data, uint64_t addr, size_t& len,
std::vector<InstructionTextToken>& result) = 0;
/*! GetInstructionLowLevelIL
Translates an instruction at addr and appends it onto the LowLevelILFunction& il.
\param data pointer to the instruction data to be translated
\param addr address of the instruction data to be translated
\param len length of the instruction data to be translated
\param il the LowLevelILFunction which
*/
virtual bool GetInstructionLowLevelIL(const uint8_t* data, uint64_t addr, size_t& len, LowLevelILFunction& il);
virtual std::string GetRegisterName(uint32_t reg);
virtual std::string GetFlagName(uint32_t flag);
virtual std::string GetFlagWriteTypeName(uint32_t flags);
virtual std::vector<uint32_t> GetFullWidthRegisters();
virtual std::vector<uint32_t> GetAllRegisters();
virtual std::vector<uint32_t> GetAllFlags();
virtual std::vector<uint32_t> GetAllFlagWriteTypes();
virtual BNFlagRole GetFlagRole(uint32_t flag);
virtual std::vector<uint32_t> GetFlagsRequiredForFlagCondition(BNLowLevelILFlagCondition cond);
virtual std::vector<uint32_t> GetFlagsWrittenByFlagWriteType(uint32_t writeType);
virtual ExprId GetFlagWriteLowLevelIL(BNLowLevelILOperation op, size_t size, uint32_t flagWriteType,
uint32_t flag, BNRegisterOrConstant* operands, size_t operandCount, LowLevelILFunction& il);
ExprId GetDefaultFlagWriteLowLevelIL(BNLowLevelILOperation op, size_t size, uint32_t flagWriteType,
uint32_t flag, BNRegisterOrConstant* operands, size_t operandCount, LowLevelILFunction& il);
virtual ExprId GetFlagConditionLowLevelIL(BNLowLevelILFlagCondition cond, LowLevelILFunction& il);
virtual BNRegisterInfo GetRegisterInfo(uint32_t reg);
virtual uint32_t GetStackPointerRegister();
virtual uint32_t GetLinkRegister();
std::vector<uint32_t> GetModifiedRegistersOnWrite(uint32_t reg);
uint32_t GetRegisterByName(const std::string& name);
virtual bool Assemble(const std::string& code, uint64_t addr, DataBuffer& result, std::string& errors);
/*! IsNeverBranchPatchAvailable returns true if the instruction at addr can be patched to never branch.
This is used in the UI to determine if "never branch" should be displayed in the right-click context
menu when right-clicking on an instruction.
\param arch the architecture of the instruction
\param addr the address of the instruction in question
*/
virtual bool IsNeverBranchPatchAvailable(const uint8_t* data, uint64_t addr, size_t len);
/*! IsAlwaysBranchPatchAvailable returns true if the instruction at addr can be patched to always branch.
This is used in the UI to determine if "always branch" should be displayed in the right-click context
menu when right-clicking on an instruction.
\param arch the architecture of the instruction
\param addr the address of the instruction in question
*/
virtual bool IsAlwaysBranchPatchAvailable(const uint8_t* data, uint64_t addr, size_t len);
/*! IsInvertBranchPatchAvailable returns true if the instruction at addr can be patched to invert the branch.
This is used in the UI to determine if "invert branch" should be displayed in the right-click context
menu when right-clicking on an instruction.
\param arch the architecture of the instruction
\param addr the address of the instruction in question
*/
virtual bool IsInvertBranchPatchAvailable(const uint8_t* data, uint64_t addr, size_t len);
/*! IsSkipAndReturnZeroPatchAvailable returns true if the instruction at addr is a call that can be patched to
return zero. This is used in the UI to determine if "skip and return zero" should be displayed in the
right-click context menu when right-clicking on an instruction.
\param arch the architecture of the instruction
\param addr the address of the instruction in question
*/
virtual bool IsSkipAndReturnZeroPatchAvailable(const uint8_t* data, uint64_t addr, size_t len);
/*! IsSkipAndReturnValuePatchAvailable returns true if the instruction at addr is a call that can be patched to
return a value. This is used in the UI to determine if "skip and return value" should be displayed in the
right-click context menu when right-clicking on an instruction.
\param arch the architecture of the instruction
\param addr the address of the instruction in question
*/
virtual bool IsSkipAndReturnValuePatchAvailable(const uint8_t* data, uint64_t addr, size_t len);
/*! ConvertToNop converts the instruction at addr to a no-operation instruction
\param arch the architecture of the instruction
\param addr the address of the instruction in question
*/
virtual bool ConvertToNop(uint8_t* data, uint64_t addr, size_t len);
/*! AlwaysBranch converts the conditional branch instruction at addr to an unconditional branch. This is called
when the right-click context menu item "always branch" is selected in the UI.
\param arch the architecture of the instruction
\param addr the address of the instruction in question
*/
virtual bool AlwaysBranch(uint8_t* data, uint64_t addr, size_t len);
/*! InvertBranch converts the conditional branch instruction at addr to its invert. This is called
when the right-click context menu item "invert branch" is selected in the UI.
\param arch the architecture of the instruction
\param addr the address of the instruction in question
*/
virtual bool InvertBranch(uint8_t* data, uint64_t addr, size_t len);
/*! SkipAndReturnValue converts the call instruction at addr to an instruction that simulates that call
returning a value. This is called when the right-click context menu item "skip and return value" is selected
in the UI.
\param arch the architecture of the instruction
\param addr the address of the instruction in question
*/
virtual bool SkipAndReturnValue(uint8_t* data, uint64_t addr, size_t len, uint64_t value);
void RegisterFunctionRecognizer(FunctionRecognizer* recog);
bool IsBinaryViewTypeConstantDefined(const std::string& type, const std::string& name);
uint64_t GetBinaryViewTypeConstant(const std::string& type, const std::string& name,
uint64_t defaultValue = 0);
void SetBinaryViewTypeConstant(const std::string& type, const std::string& name, uint64_t value);
bool ParseTypesFromSource(const std::string& source, const std::string& fileName,
std::map<std::string, Ref<Type>>& types, std::map<std::string, Ref<Type>>& variables,
std::map<std::string, Ref<Type>>& functions, std::string& errors,
const std::vector<std::string>& includeDirs = std::vector<std::string>());
bool ParseTypesFromSourceFile(const std::string& fileName, std::map<std::string, Ref<Type>>& types,
std::map<std::string, Ref<Type>>& variables,
std::map<std::string, Ref<Type>>& functions, std::string& errors,
const std::vector<std::string>& includeDirs = std::vector<std::string>());
void RegisterCallingConvention(CallingConvention* cc);
std::vector<Ref<CallingConvention>> GetCallingConventions();
Ref<CallingConvention> GetCallingConventionByName(const std::string& name);
void SetDefaultCallingConvention(CallingConvention* cc);
void SetCdeclCallingConvention(CallingConvention* cc);
void SetStdcallCallingConvention(CallingConvention* cc);
void SetFastcallCallingConvention(CallingConvention* cc);
Ref<CallingConvention> GetDefaultCallingConvention();
Ref<CallingConvention> GetCdeclCallingConvention();
Ref<CallingConvention> GetStdcallCallingConvention();
Ref<CallingConvention> GetFastcallCallingConvention();
Ref<Platform> GetStandalonePlatform();
};
class CoreArchitecture: public Architecture
{
public:
CoreArchitecture(BNArchitecture* arch);
virtual BNEndianness GetEndianness() const override;
virtual size_t GetAddressSize() const override;
virtual size_t GetDefaultIntegerSize() const override;
virtual size_t GetMaxInstructionLength() const override;
virtual size_t GetOpcodeDisplayLength() const override;
virtual bool GetInstructionInfo(const uint8_t* data, uint64_t addr, size_t maxLen, InstructionInfo& result) override;
virtual bool GetInstructionText(const uint8_t* data, uint64_t addr, size_t& len,
std::vector<InstructionTextToken>& result) override;
virtual bool GetInstructionLowLevelIL(const uint8_t* data, uint64_t addr, size_t& len, LowLevelILFunction& il) override;
virtual std::string GetRegisterName(uint32_t reg) override;
virtual std::string GetFlagName(uint32_t flag) override;
virtual std::string GetFlagWriteTypeName(uint32_t flags) override;
virtual std::vector<uint32_t> GetFullWidthRegisters() override;
virtual std::vector<uint32_t> GetAllRegisters() override;
virtual std::vector<uint32_t> GetAllFlags() override;
virtual std::vector<uint32_t> GetAllFlagWriteTypes() override;
virtual BNFlagRole GetFlagRole(uint32_t flag) override;
virtual std::vector<uint32_t> GetFlagsRequiredForFlagCondition(BNLowLevelILFlagCondition cond) override;
virtual std::vector<uint32_t> GetFlagsWrittenByFlagWriteType(uint32_t writeType) override;
virtual ExprId GetFlagWriteLowLevelIL(BNLowLevelILOperation op, size_t size, uint32_t flagWriteType,
uint32_t flag, BNRegisterOrConstant* operands, size_t operandCount, LowLevelILFunction& il) override;
virtual ExprId GetFlagConditionLowLevelIL(BNLowLevelILFlagCondition cond, LowLevelILFunction& il) override;
virtual BNRegisterInfo GetRegisterInfo(uint32_t reg) override;
virtual uint32_t GetStackPointerRegister() override;
virtual uint32_t GetLinkRegister() override;
virtual bool Assemble(const std::string& code, uint64_t addr, DataBuffer& result, std::string& errors) override;
virtual bool IsNeverBranchPatchAvailable(const uint8_t* data, uint64_t addr, size_t len) override;
virtual bool IsAlwaysBranchPatchAvailable(const uint8_t* data, uint64_t addr, size_t len) override;
virtual bool IsInvertBranchPatchAvailable(const uint8_t* data, uint64_t addr, size_t len) override;
virtual bool IsSkipAndReturnZeroPatchAvailable(const uint8_t* data, uint64_t addr, size_t len) override;
virtual bool IsSkipAndReturnValuePatchAvailable(const uint8_t* data, uint64_t addr, size_t len) override;
virtual bool ConvertToNop(uint8_t* data, uint64_t addr, size_t len) override;
virtual bool AlwaysBranch(uint8_t* data, uint64_t addr, size_t len) override;
virtual bool InvertBranch(uint8_t* data, uint64_t addr, size_t len) override;
virtual bool SkipAndReturnValue(uint8_t* data, uint64_t addr, size_t len, uint64_t value) override;
};
class Structure;
class Enumeration;
struct NameAndType
{
std::string name;
Ref<Type> type;
};
class Type: public CoreRefCountObject<BNType, BNNewTypeReference, BNFreeType>
{
public:
Type(BNType* type);
BNTypeClass GetClass() const;
uint64_t GetWidth() const;
size_t GetAlignment() const;
bool IsSigned() const;
bool IsConst() const;
bool IsVolatile() const;
bool IsFloat() const;
Ref<Type> GetChildType() const;
Ref<CallingConvention> GetCallingConvention() const;
std::vector<NameAndType> GetParameters() const;
bool HasVariableArguments() const;
bool CanReturn() const;
Ref<Structure> GetStructure() const;
Ref<Enumeration> GetEnumeration() const;
uint64_t GetElementCount() const;
void SetFunctionCanReturn(bool canReturn);
std::string GetString() const;
std::string GetTypeAndName(const std::vector<std::string>& name) const;
std::string GetStringBeforeName() const;
std::string GetStringAfterName() const;
Ref<Type> Duplicate() const;
static Ref<Type> VoidType();
static Ref<Type> BoolType();
static Ref<Type> IntegerType(size_t width, bool sign, const std::string& altName = "");
static Ref<Type> FloatType(size_t width, const std::string& typeName = "");
static Ref<Type> StructureType(Structure* strct);
static Ref<Type> EnumerationType(Architecture* arch, Enumeration* enm, size_t width = 0, bool issigned = false);
static Ref<Type> PointerType(Architecture* arch, Type* type, bool cnst = false, bool vltl = false,
BNReferenceType refType = PointerReferenceType);
static Ref<Type> ArrayType(Type* type, uint64_t elem);
static Ref<Type> FunctionType(Type* returnValue, CallingConvention* callingConvention,
const std::vector<NameAndType>& params, bool varArg = false);
static std::string GetQualifiedName(const std::vector<std::string>& names);
};
struct StructureMember
{
Ref<Type> type;
std::string name;
uint64_t offset;
};
class Structure: public CoreRefCountObject<BNStructure, BNNewStructureReference, BNFreeStructure>
{
public:
Structure(BNStructure* s);
std::vector<std::string> GetName() const;
void SetName(const std::vector<std::string>& name);
std::vector<StructureMember> GetMembers() const;
uint64_t GetWidth() const;
size_t GetAlignment() const;
bool IsPacked() const;
void SetPacked(bool packed);
bool IsUnion() const;
void SetUnion(bool u);
void AddMember(Type* type, const std::string& name);
void AddMemberAtOffset(Type* type, const std::string& name, uint64_t offset);
void RemoveMember(size_t idx);
};
struct EnumerationMember
{
std::string name;
uint64_t value;
bool isDefault;
};
class Enumeration: public CoreRefCountObject<BNEnumeration, BNNewEnumerationReference, BNFreeEnumeration>
{
public:
Enumeration(BNEnumeration* e);
std::vector<std::string> GetName() const;
void SetName(const std::vector<std::string>& name);
std::vector<EnumerationMember> GetMembers() const;
void AddMember(const std::string& name);
void AddMemberWithValue(const std::string& name, uint64_t value);
};
class DisassemblySettings: public CoreRefCountObject<BNDisassemblySettings,
BNNewDisassemblySettingsReference, BNFreeDisassemblySettings>
{
public:
DisassemblySettings();
DisassemblySettings(BNDisassemblySettings* settings);
bool IsOptionSet(BNDisassemblyOption option) const;
void SetOption(BNDisassemblyOption option, bool state = true);
size_t GetWidth() const;
void SetWidth(size_t width);
size_t GetMaximumSymbolWidth() const;
void SetMaximumSymbolWidth(size_t width);
};
class Function;
struct BasicBlockEdge
{
BNBranchType type;
uint64_t target;
Ref<Architecture> arch;
};
class BasicBlock: public CoreRefCountObject<BNBasicBlock, BNNewBasicBlockReference, BNFreeBasicBlock>
{
public:
BasicBlock(BNBasicBlock* block);
Ref<Function> GetFunction() const;
Ref<Architecture> GetArchitecture() const;
uint64_t GetStart() const;
uint64_t GetEnd() const;
uint64_t GetLength() const;
std::vector<BasicBlockEdge> GetOutgoingEdges() const;
bool HasUndeterminedOutgoingEdges() const;
void MarkRecentUse();
std::vector<std::vector<InstructionTextToken>> GetAnnotations();
std::vector<DisassemblyTextLine> GetDisassemblyText(DisassemblySettings* settings);
};
struct StackVariable
{
Ref<Type> type;
std::string name;
int64_t offset;
bool autoDefined;
};
struct StackVariableReference
{
uint32_t sourceOperand;
Ref<Type> type;
std::string name;
int64_t startingOffset;
int64_t referencedOffset;
};
struct IndirectBranchInfo
{
Ref<Architecture> sourceArch;
uint64_t sourceAddr;
Ref<Architecture> destArch;
uint64_t destAddr;
bool autoDefined;
};
struct ArchAndAddr
{
Ref<Architecture> arch;
uint64_t address;
ArchAndAddr(): arch(nullptr), address(0) {}
ArchAndAddr(Architecture* a, uint64_t addr): arch(a), address(addr) {}
};
struct LookupTableEntry
{
std::vector<int64_t> fromValues;
int64_t toValue;
};
struct RegisterValue
{
BNRegisterValueType state;
uint32_t reg; // For EntryValue and OffsetFromEntryValue, the original input register
int64_t value; // Offset for OffsetFromEntryValue, StackFrameOffset or RangeValue, value of register for ConstantValue
uint64_t rangeStart, rangeEnd, rangeStep; // Range of register, inclusive
std::vector<LookupTableEntry> table;
};
class FunctionGraph;
class Function: public CoreRefCountObject<BNFunction, BNNewFunctionReference, BNFreeFunction>
{
public:
Function(BNFunction* func);
Ref<Architecture> GetArchitecture() const;
Ref<Platform> GetPlatform() const;
uint64_t GetStart() const;
Ref<Symbol> GetSymbol() const;
bool WasAutomaticallyDiscovered() const;
bool CanReturn() const;
bool HasExplicitlyDefinedType() const;
std::vector<Ref<BasicBlock>> GetBasicBlocks() const;
void MarkRecentUse();
std::string GetCommentForAddress(uint64_t addr) const;
std::vector<uint64_t> GetCommentedAddresses() const;
void SetCommentForAddress(uint64_t addr, const std::string& comment);
Ref<LowLevelILFunction> GetLowLevelIL() const;
size_t GetLowLevelILForInstruction(Architecture* arch, uint64_t addr);
std::vector<size_t> GetLowLevelILExitsForInstruction(Architecture* arch, uint64_t addr);
RegisterValue GetRegisterValueAtInstruction(Architecture* arch, uint64_t addr, uint32_t reg);
RegisterValue GetRegisterValueAfterInstruction(Architecture* arch, uint64_t addr, uint32_t reg);
RegisterValue GetRegisterValueAtLowLevelILInstruction(size_t i, uint32_t reg);
RegisterValue GetRegisterValueAfterLowLevelILInstruction(size_t i, uint32_t reg);
RegisterValue GetStackContentsAtInstruction(Architecture* arch, uint64_t addr, int64_t offset, size_t size);
RegisterValue GetStackContentsAfterInstruction(Architecture* arch, uint64_t addr, int64_t offset, size_t size);
RegisterValue GetStackContentsAtLowLevelILInstruction(size_t i, int64_t offset, size_t size);
RegisterValue GetStackContentsAfterLowLevelILInstruction(size_t i, int64_t offset, size_t size);
RegisterValue GetParameterValueAtInstruction(Architecture* arch, uint64_t addr, Type* functionType, size_t i);
RegisterValue GetParameterValueAtLowLevelILInstruction(size_t instr, Type* functionType, size_t i);
std::vector<uint32_t> GetRegistersReadByInstruction(Architecture* arch, uint64_t addr);
std::vector<uint32_t> GetRegistersWrittenByInstruction(Architecture* arch, uint64_t addr);
std::vector<StackVariableReference> GetStackVariablesReferencedByInstruction(Architecture* arch, uint64_t addr);
Ref<LowLevelILFunction> GetLiftedIL() const;
size_t GetLiftedILForInstruction(Architecture* arch, uint64_t addr);
std::set<size_t> GetLiftedILFlagUsesForDefinition(size_t i, uint32_t flag);
std::set<size_t> GetLiftedILFlagDefinitionsForUse(size_t i, uint32_t flag);
std::set<uint32_t> GetFlagsReadByLiftedILInstruction(size_t i);
std::set<uint32_t> GetFlagsWrittenByLiftedILInstruction(size_t i);
Ref<Type> GetType() const;
void SetAutoType(Type* type);
void SetUserType(Type* type);
void ApplyImportedTypes(Symbol* sym);
void ApplyAutoDiscoveredType(Type* type);
Ref<FunctionGraph> CreateFunctionGraph();
std::map<int64_t, StackVariable> GetStackLayout();
void CreateAutoStackVariable(int64_t offset, Type* type, const std::string& name);
void CreateUserStackVariable(int64_t offset, Type* type, const std::string& name);
void DeleteAutoStackVariable(int64_t offset);
void DeleteUserStackVariable(int64_t offset);
bool GetStackVariableAtFrameOffset(int64_t offset, StackVariable& var);
void SetAutoIndirectBranches(Architecture* sourceArch, uint64_t source, const std::vector<ArchAndAddr>& branches);
void SetUserIndirectBranches(Architecture* sourceArch, uint64_t source, const std::vector<ArchAndAddr>& branches);
std::vector<IndirectBranchInfo> GetIndirectBranches();
std::vector<IndirectBranchInfo> GetIndirectBranchesAt(Architecture* arch, uint64_t addr);
std::vector<std::vector<InstructionTextToken>> GetBlockAnnotations(Architecture* arch, uint64_t addr);
BNIntegerDisplayType GetIntegerConstantDisplayType(Architecture* arch, uint64_t instrAddr, uint64_t value,
size_t operand);
void SetIntegerConstantDisplayType(Architecture* arch, uint64_t instrAddr, uint64_t value, size_t operand,
BNIntegerDisplayType type);
};
struct FunctionGraphEdge
{
BNBranchType type;
uint64_t target;
Ref<Architecture> arch;
std::vector<BNPoint> points;
};
class FunctionGraphBlock: public CoreRefCountObject<BNFunctionGraphBlock,
BNNewFunctionGraphBlockReference, BNFreeFunctionGraphBlock>
{
public:
FunctionGraphBlock(BNFunctionGraphBlock* block);
Ref<Architecture> GetArchitecture() const;
uint64_t GetStart() const;
uint64_t GetEnd() const;
int GetX() const;
int GetY() const;
int GetWidth() const;
int GetHeight() const;
std::vector<DisassemblyTextLine> GetLines() const;
std::vector<FunctionGraphEdge> GetOutgoingEdges() const;
};
class FunctionGraph: public RefCountObject
{
BNFunctionGraph* m_graph;
std::function<void()> m_completeFunc;
static void CompleteCallback(void* ctxt);
public:
FunctionGraph(BNFunctionGraph* graph);
~FunctionGraph();
BNFunctionGraph* GetGraphObject() const { return m_graph; }
Ref<Function> GetFunction() const;
int GetHorizontalBlockMargin() const;
int GetVerticalBlockMargin() const;
void SetBlockMargins(int horiz, int vert);
Ref<DisassemblySettings> GetSettings();
void StartLayout(BNFunctionGraphType = NormalFunctionGraph);
bool IsLayoutComplete();
void OnComplete(const std::function<void()>& func);
void Abort();
std::vector<Ref<FunctionGraphBlock>> GetBlocks() const;
int GetWidth() const;
int GetHeight() const;
std::vector<Ref<FunctionGraphBlock>> GetBlocksInRegion(int left, int top, int right, int bottom);
bool IsOptionSet(BNDisassemblyOption option) const;
void SetOption(BNDisassemblyOption option, bool state = true);
};
struct LowLevelILLabel: public BNLowLevelILLabel
{
LowLevelILLabel();
};
class LowLevelILFunction: public CoreRefCountObject<BNLowLevelILFunction,
BNNewLowLevelILFunctionReference, BNFreeLowLevelILFunction>
{
public:
LowLevelILFunction(Architecture* arch);
LowLevelILFunction(BNLowLevelILFunction* func);
uint64_t GetCurrentAddress() const;
void SetCurrentAddress(uint64_t addr);
void ClearIndirectBranches();
void SetIndirectBranches(const std::vector<ArchAndAddr>& branches);
ExprId AddExpr(BNLowLevelILOperation operation, size_t size, uint32_t flags,
ExprId a = 0, ExprId b = 0, ExprId c = 0, ExprId d = 0);
ExprId AddInstruction(ExprId expr);
ExprId Nop();
ExprId SetRegister(size_t size, uint32_t reg, ExprId val, uint32_t flags = 0);
ExprId SetRegisterSplit(size_t size, uint32_t high, uint32_t low, ExprId val);
ExprId SetFlag(uint32_t flag, ExprId val);
ExprId Load(size_t size, ExprId addr);
ExprId Store(size_t size, ExprId addr, ExprId val);
ExprId Push(size_t size, ExprId val);
ExprId Pop(size_t size);
ExprId Register(size_t size, uint32_t reg);
ExprId Const(size_t size, uint64_t val);
ExprId Flag(uint32_t reg);
ExprId FlagBit(size_t size, uint32_t flag, uint32_t bitIndex);
ExprId Add(size_t size, ExprId a, ExprId b, uint32_t flags = 0);
ExprId AddCarry(size_t size, ExprId a, ExprId b, uint32_t flags = 0);
ExprId Sub(size_t size, ExprId a, ExprId b, uint32_t flags = 0);
ExprId SubBorrow(size_t size, ExprId a, ExprId b, uint32_t flags = 0);
ExprId And(size_t size, ExprId a, ExprId b, uint32_t flags = 0);
ExprId Or(size_t size, ExprId a, ExprId b, uint32_t flags = 0);
ExprId Xor(size_t size, ExprId a, ExprId b, uint32_t flags = 0);
ExprId ShiftLeft(size_t size, ExprId a, ExprId b, uint32_t flags = 0);
ExprId LogicalShiftRight(size_t size, ExprId a, ExprId b, uint32_t flags = 0);
ExprId ArithShiftRight(size_t size, ExprId a, ExprId b, uint32_t flags = 0);
ExprId RotateLeft(size_t size, ExprId a, ExprId b, uint32_t flags = 0);
ExprId RotateLeftCarry(size_t size, ExprId a, ExprId b, uint32_t flags = 0);
ExprId RotateRight(size_t size, ExprId a, ExprId b, uint32_t flags = 0);
ExprId RotateRightCarry(size_t size, ExprId a, ExprId b, uint32_t flags = 0);
ExprId Mult(size_t size, ExprId a, ExprId b, uint32_t flags = 0);
ExprId MultDoublePrecUnsigned(size_t size, ExprId a, ExprId b, uint32_t flags = 0);
ExprId MultDoublePrecSigned(size_t size, ExprId a, ExprId b, uint32_t flags = 0);
ExprId DivUnsigned(size_t size, ExprId a, ExprId b, uint32_t flags = 0);
ExprId DivDoublePrecUnsigned(size_t size, ExprId high, ExprId low, ExprId div, uint32_t flags = 0);
ExprId DivSigned(size_t size, ExprId a, ExprId b, uint32_t flags = 0);
ExprId DivDoublePrecSigned(size_t size, ExprId high, ExprId low, ExprId div, uint32_t flags = 0);
ExprId ModUnsigned(size_t size, ExprId a, ExprId b, uint32_t flags = 0);
ExprId ModDoublePrecUnsigned(size_t size, ExprId high, ExprId low, ExprId div, uint32_t flags = 0);
ExprId ModSigned(size_t size, ExprId a, ExprId b, uint32_t flags = 0);
ExprId ModDoublePrecSigned(size_t size, ExprId high, ExprId low, ExprId div, uint32_t flags = 0);
ExprId Neg(size_t size, ExprId a, uint32_t flags = 0);
ExprId Not(size_t size, ExprId a, uint32_t flags = 0);
ExprId SignExtend(size_t size, ExprId a);
ExprId ZeroExtend(size_t size, ExprId a);
ExprId Jump(ExprId dest);
ExprId Call(ExprId dest);
ExprId Return(size_t dest);
ExprId NoReturn();
ExprId FlagCondition(BNLowLevelILFlagCondition cond);
ExprId CompareEqual(size_t size, ExprId a, ExprId b);
ExprId CompareNotEqual(size_t size, ExprId a, ExprId b);
ExprId CompareSignedLessThan(size_t size, ExprId a, ExprId b);
ExprId CompareUnsignedLessThan(size_t size, ExprId a, ExprId b);
ExprId CompareSignedLessEqual(size_t size, ExprId a, ExprId b);
ExprId CompareUnsignedLessEqual(size_t size, ExprId a, ExprId b);
ExprId CompareSignedGreaterEqual(size_t size, ExprId a, ExprId b);
ExprId CompareUnsignedGreaterEqual(size_t size, ExprId a, ExprId b);
ExprId CompareSignedGreaterThan(size_t size, ExprId a, ExprId b);
ExprId CompareUnsignedGreaterThan(size_t size, ExprId a, ExprId b);
ExprId TestBit(size_t size, ExprId a, ExprId b);
ExprId BoolToInt(size_t size, ExprId a);
ExprId SystemCall();
ExprId Breakpoint();
ExprId Trap(uint32_t num);
ExprId Undefined();
ExprId Unimplemented();
ExprId UnimplementedMemoryRef(size_t size, ExprId addr);
ExprId Goto(BNLowLevelILLabel& label);
ExprId If(ExprId operand, BNLowLevelILLabel& t, BNLowLevelILLabel& f);
void MarkLabel(BNLowLevelILLabel& label);
std::vector<uint64_t> GetOperandList(ExprId i, size_t listOperand);
ExprId AddLabelList(const std::vector<BNLowLevelILLabel*>& labels);
ExprId AddOperandList(const std::vector<ExprId> operands);
ExprId Operand(uint32_t n, ExprId expr);
BNLowLevelILInstruction operator[](size_t i) const;
size_t GetIndexForInstruction(size_t i) const;
size_t GetInstructionCount() const;
void AddLabelForAddress(Architecture* arch, ExprId addr);
BNLowLevelILLabel* GetLabelForAddress(Architecture* arch, ExprId addr);
void Finalize(Function* func = nullptr);
bool GetExprText(Architecture* arch, ExprId expr, std::vector<InstructionTextToken>& tokens);
bool GetInstructionText(Function* func, Architecture* arch, size_t i,
std::vector<InstructionTextToken>& tokens);
uint32_t GetTemporaryRegisterCount();
uint32_t GetTemporaryFlagCount();
std::vector<Ref<BasicBlock>> GetBasicBlocks() const;
};
class FunctionRecognizer
{
static bool RecognizeLowLevelILCallback(void* ctxt, BNBinaryView* data, BNFunction* func, BNLowLevelILFunction* il);
public:
FunctionRecognizer();
static void RegisterGlobalRecognizer(FunctionRecognizer* recog);
static void RegisterArchitectureFunctionRecognizer(Architecture* arch, FunctionRecognizer* recog);
virtual bool RecognizeLowLevelIL(BinaryView* data, Function* func, LowLevelILFunction* il);
};
class UpdateException: public std::exception
{
const std::string m_desc;
public:
UpdateException(const std::string& desc): std::exception(), m_desc(desc) {}
virtual const char* what() const NOEXCEPT { return m_desc.c_str(); }
};
struct UpdateChannel
{
std::string name;
std::string description;
std::string latestVersion;
static std::vector<UpdateChannel> GetList();
bool AreUpdatesAvailable();
BNUpdateResult UpdateToVersion(const std::string& version);
BNUpdateResult UpdateToVersion(const std::string& version,
const std::function<bool(uint64_t progress, uint64_t total)>& progress);
BNUpdateResult UpdateToLatestVersion();
BNUpdateResult UpdateToLatestVersion(const std::function<bool(uint64_t progress, uint64_t total)>& progress);
};
/*! UpdateVersion documentation
*/
struct UpdateVersion
{
std::string version;
std::string notes;
time_t time;
static std::vector<UpdateVersion> GetChannelVersions(const std::string& channel);
};
struct PluginCommandContext
{
Ref<BinaryView> view;
uint64_t address, length;
Ref<Function> function;
PluginCommandContext();
};
class PluginCommand
{
BNPluginCommand m_command;
struct RegisteredDefaultCommand
{
std::function<void(BinaryView*)> action;
std::function<bool(BinaryView*)> isValid;
};
struct RegisteredAddressCommand
{
std::function<void(BinaryView*, uint64_t)> action;
std::function<bool(BinaryView*, uint64_t)> isValid;
};
struct RegisteredRangeCommand
{
std::function<void(BinaryView*, uint64_t, uint64_t)> action;
std::function<bool(BinaryView*, uint64_t, uint64_t)> isValid;
};
struct RegisteredFunctionCommand
{
std::function<void(BinaryView*, Function*)> action;
std::function<bool(BinaryView*, Function*)> isValid;
};
static void DefaultPluginCommandActionCallback(void* ctxt, BNBinaryView* view);
static void AddressPluginCommandActionCallback(void* ctxt, BNBinaryView* view, uint64_t addr);
static void RangePluginCommandActionCallback(void* ctxt, BNBinaryView* view, uint64_t addr, uint64_t len);
static void FunctionPluginCommandActionCallback(void* ctxt, BNBinaryView* view, BNFunction* func);
static bool DefaultPluginCommandIsValidCallback(void* ctxt, BNBinaryView* view);
static bool AddressPluginCommandIsValidCallback(void* ctxt, BNBinaryView* view, uint64_t addr);
static bool RangePluginCommandIsValidCallback(void* ctxt, BNBinaryView* view, uint64_t addr, uint64_t len);
static bool FunctionPluginCommandIsValidCallback(void* ctxt, BNBinaryView* view, BNFunction* func);
public:
PluginCommand(const BNPluginCommand& cmd);
PluginCommand(const PluginCommand& cmd);
~PluginCommand();
PluginCommand& operator=(const PluginCommand& cmd);
static void Register(const std::string& name, const std::string& description,
const std::function<void(BinaryView* view)>& action);
static void Register(const std::string& name, const std::string& description,
const std::function<void(BinaryView* view)>& action,
const std::function<bool(BinaryView* view)>& isValid);
static void RegisterForAddress(const std::string& name, const std::string& description,
const std::function<void(BinaryView* view, uint64_t addr)>& action);
static void RegisterForAddress(const std::string& name, const std::string& description,
const std::function<void(BinaryView* view, uint64_t addr)>& action,
const std::function<bool(BinaryView* view, uint64_t addr)>& isValid);
static void RegisterForRange(const std::string& name, const std::string& description,
const std::function<void(BinaryView* view, uint64_t addr, uint64_t len)>& action);
static void RegisterForRange(const std::string& name, const std::string& description,
const std::function<void(BinaryView* view, uint64_t addr, uint64_t len)>& action,
const std::function<bool(BinaryView* view, uint64_t addr, uint64_t len)>& isValid);
static void RegisterForFunction(const std::string& name, const std::string& description,
const std::function<void(BinaryView* view, Function* func)>& action);
static void RegisterForFunction(const std::string& name, const std::string& description,
const std::function<void(BinaryView* view, Function* func)>& action,
const std::function<bool(BinaryView* view, Function* func)>& isValid);
static std::vector<PluginCommand> GetList();
static std::vector<PluginCommand> GetValidList(const PluginCommandContext& ctxt);
std::string GetName() const { return m_command.name; }
std::string GetDescription() const { return m_command.description; }
bool IsValid(const PluginCommandContext& ctxt) const;
void Execute(const PluginCommandContext& ctxt) const;
};
class CallingConvention: public CoreRefCountObject<BNCallingConvention,
BNNewCallingConventionReference, BNFreeCallingConvention>
{
protected:
CallingConvention(BNCallingConvention* cc);
CallingConvention(Architecture* arch, const std::string& name);
static void FreeCallback(void* ctxt);
static uint32_t* GetCallerSavedRegistersCallback(void* ctxt, size_t* count);
static uint32_t* GetIntegerArgumentRegistersCallback(void* ctxt, size_t* count);
static uint32_t* GetFloatArgumentRegistersCallback(void* ctxt, size_t* count);
static void FreeRegisterListCallback(void* ctxt, uint32_t* regs);
static bool AreArgumentRegistersSharedIndexCallback(void* ctxt);
static bool IsStackReservedForArgumentRegistersCallback(void* ctxt);
static uint32_t GetIntegerReturnValueRegisterCallback(void* ctxt);
static uint32_t GetHighIntegerReturnValueRegisterCallback(void* ctxt);
static uint32_t GetFloatReturnValueRegisterCallback(void* ctxt);
public:
Ref<Architecture> GetArchitecture() const;
std::string GetName() const;
virtual std::vector<uint32_t> GetCallerSavedRegisters();
virtual std::vector<uint32_t> GetIntegerArgumentRegisters();
virtual std::vector<uint32_t> GetFloatArgumentRegisters();
virtual bool AreArgumentRegistersSharedIndex();
virtual bool IsStackReservedForArgumentRegisters();
virtual uint32_t GetIntegerReturnValueRegister() = 0;
virtual uint32_t GetHighIntegerReturnValueRegister();
virtual uint32_t GetFloatReturnValueRegister();
};
class CoreCallingConvention: public CallingConvention
{
public:
CoreCallingConvention(BNCallingConvention* cc);
virtual std::vector<uint32_t> GetCallerSavedRegisters() override;
virtual std::vector<uint32_t> GetIntegerArgumentRegisters() override;
virtual std::vector<uint32_t> GetFloatArgumentRegisters() override;
virtual bool AreArgumentRegistersSharedIndex() override;
virtual bool IsStackReservedForArgumentRegisters() override;
virtual uint32_t GetIntegerReturnValueRegister() override;
virtual uint32_t GetHighIntegerReturnValueRegister() override;
virtual uint32_t GetFloatReturnValueRegister() override;
};
/*!
Platform base class. This should be subclassed when creating a new platform
*/
class Platform: public CoreRefCountObject<BNPlatform, BNNewPlatformReference, BNFreePlatform>
{
protected:
Platform(Architecture* arch, const std::string& name);
public:
Platform(BNPlatform* platform);
Ref<Architecture> GetArchitecture() const;
std::string GetName() const;
static void Register(const std::string& os, Platform* platform);
static Ref<Platform> GetByName(const std::string& name);
static std::vector<Ref<Platform>> GetList();
static std::vector<Ref<Platform>> GetList(Architecture* arch);
static std::vector<Ref<Platform>> GetList(const std::string& os);
static std::vector<Ref<Platform>> GetList(const std::string& os, Architecture* arch);
static std::vector<std::string> GetOSList();
Ref<CallingConvention> GetDefaultCallingConvention() const;
Ref<CallingConvention> GetCdeclCallingConvention() const;
Ref<CallingConvention> GetStdcallCallingConvention() const;
Ref<CallingConvention> GetFastcallCallingConvention() const;
std::vector<Ref<CallingConvention>> GetCallingConventions() const;
Ref<CallingConvention> GetSystemCallConvention() const;
void RegisterCallingConvention(CallingConvention* cc);
void RegisterDefaultCallingConvention(CallingConvention* cc);
void RegisterCdeclCallingConvention(CallingConvention* cc);
void RegisterStdcallCallingConvention(CallingConvention* cc);
void RegisterFastcallCallingConvention(CallingConvention* cc);
void SetSystemCallConvention(CallingConvention* cc);
Ref<Platform> GetRelatedPlatform(Architecture* arch);
void AddRelatedPlatform(Architecture* arch, Platform* platform);
};
}
|