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
|
# coding=utf-8
# Copyright (c) 2015-2021 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
from typing import List, Generator, Optional, Union, Set, Mapping
import binaryninja
from . import _binaryninjacore as core
from . import decorators
from .enums import RegisterValueType, VariableSourceType, DeadStoreElimination
@decorators.passive
class LookupTableEntry(object):
def __init__(self, from_values:List[int], to_value):
self._from_values = from_values
self._to_value = to_value
def __repr__(self):
return f"[{', '.join([f'{i:#x}' for i in self.from_values])}] -> {self.to_value:#x}"
def __eq__(self, other):
if not isinstance(other, self.__class__):
return NotImplemented
return (self._from_values, self._to_value) == (other._from_values, other._to_value)
def __ne__(self, other):
if not isinstance(other, self.__class__):
return NotImplemented
return not (self == other)
def __hash__(self):
return hash((self._from_values, self._to_value))
@property
def from_values(self):
return self._from_values
@property
def to_value(self):
return self._to_value
@decorators.passive
class RegisterValue(object):
def __init__(self, arch:Optional['binaryninja.architecture.Architecture']=None,
value:core.BNRegisterValue=None, confidence:int=core.max_confidence):
self._is_constant = False
self._value = None
self._arch = None
# self._reg:Optional[Union[binaryninja.architecture.RegisterName, binaryninja.architecture.RegisterIndex]] = None
self._is_constant = False
self._offset = None
if value is None:
self._type = RegisterValueType.UndeterminedValue
else:
assert isinstance(value.value, int), "BNRegisterValue.value isn't an integer"
self._type = RegisterValueType(value.state)
if value.state == RegisterValueType.EntryValue:
self._arch = arch
if arch is not None:
self._value = arch.get_reg_name(binaryninja.architecture.RegisterIndex(value.value))
else:
self._value = value.value
elif (value.state == RegisterValueType.ConstantValue) or (value.state == RegisterValueType.ConstantPointerValue):
self._value = value.value
self._is_constant = True
elif value.state == RegisterValueType.StackFrameOffset:
self._offset = value.value
elif value.state == RegisterValueType.ImportedAddressValue:
self._value = value.value
self._confidence = confidence
def __repr__(self):
if self._type == RegisterValueType.EntryValue:
return f"<entry {self._value:s}>"
if self._type == RegisterValueType.ConstantValue:
return f"<const {self._value:#x}>"
if self._type == RegisterValueType.ConstantPointerValue:
return f"<const ptr {self._value:#x}>"
if self._type == RegisterValueType.StackFrameOffset:
return f"<stack frame offset {self._offset:#x}>"
if self._type == RegisterValueType.ReturnAddressValue:
return "<return address>"
if self._type == RegisterValueType.ImportedAddressValue:
return f"<imported address from entry {self._value:#x}>"
return "<undetermined>"
def __hash__(self):
if self._type in [RegisterValueType.ConstantValue, RegisterValueType.ConstantPointerValue, RegisterValueType.ImportedAddressValue, RegisterValueType.ReturnAddressValue, RegisterValueType.EntryValue]:
return hash(self._value)
elif self._type == RegisterValueType.StackFrameOffset:
return hash(self._offset)
def __eq__(self, other):
if self._type in [RegisterValueType.ConstantValue, RegisterValueType.ConstantPointerValue, RegisterValueType.ImportedAddressValue, RegisterValueType.ReturnAddressValue] and isinstance(other, int):
return self._value == other
elif self._type in [RegisterValueType.ConstantValue, RegisterValueType.ConstantPointerValue, RegisterValueType.ImportedAddressValue, RegisterValueType.ReturnAddressValue] and hasattr(other, 'type') and other.type == self._type:
return self._value == other.value
elif self._type == RegisterValueType.EntryValue and hasattr(other, "type") and other.type == self._type:
return self._value == other.reg
elif self._type == RegisterValueType.StackFrameOffset and hasattr(other, 'type') and other.type == self._type:
return self._offset == other.offset
elif self._type == RegisterValueType.StackFrameOffset and isinstance(other, int):
return self._offset == other
return NotImplemented
def __ne__(self, other):
if not isinstance(other, self.__class__):
return NotImplemented
return not (self == other)
def _to_api_object(self):
result = core.BNRegisterValue()
result.type = self._type
result._value = 0
if self._type == RegisterValueType.EntryValue:
if isinstance(self._value, binaryninja.architecture.RegisterName):
if self._arch is None:
raise Exception("Can not convert Variable to API object without an Architecture set")
result._value = self._arch.get_reg_index(self._value)
else:
result._value = self._value
elif (self._type == RegisterValueType.ConstantValue) or (self._type == RegisterValueType.ConstantPointerValue):
result._value = self._value
elif self._type == RegisterValueType.StackFrameOffset:
result._value = self._offset
elif self._type == RegisterValueType.ImportedAddressValue:
result._value = self._value
return result
@classmethod
def undetermined(cls):
return RegisterValue()
@classmethod
def entry_value(cls, arch:'binaryninja.architecture.Architecture', reg:'binaryninja.architecture.RegisterName') -> 'RegisterValue':
result = RegisterValue()
result._type = RegisterValueType.EntryValue
result._arch = arch
result._value = reg
return result
@staticmethod
def constant(value:int) -> 'RegisterValue':
result = RegisterValue()
result._type = RegisterValueType.ConstantValue
result._value = value
result._is_constant = True
return result
@staticmethod
def constant_ptr(value:int) -> 'RegisterValue':
result = RegisterValue()
result._type = RegisterValueType.ConstantPointerValue
result._value = value
result._is_constant = True
return result
@staticmethod
def stack_frame_offset(offset:int) -> 'RegisterValue':
result = RegisterValue()
result._type = RegisterValueType.StackFrameOffset
result._offset = offset
return result
@staticmethod
def imported_address(value) -> 'RegisterValue':
result = RegisterValue()
result._type = RegisterValueType.ImportedAddressValue
result._value = value
return result
@staticmethod
def return_address() -> 'RegisterValue':
result = RegisterValue()
result._type = RegisterValueType.ReturnAddressValue
return result
@property
def is_constant(self) -> bool:
"""Boolean for whether the RegisterValue is known to be constant (read-only)"""
return self._is_constant
@property
def type(self) -> RegisterValueType:
""":class:`~enums.RegisterValueType` (read-only)"""
return self._type
@property
def arch(self) -> Optional['binaryninja.architecture.Architecture']:
"""Architecture where it exists, None otherwise (read-only)"""
return self._arch
@property
def reg(self) -> 'binaryninja.architecture.RegisterName':
"""Register Name where the Architecture exists raises exception otherwise (read-only)"""
if not isinstance(self._value, binaryninja.architecture.RegisterName):
raise Exception("Attempting to access register when property doesn't exist")
return self._value
@property
def value(self) -> Optional[Union[int, 'binaryninja.architecture.RegisterName']]:
"""Value where it exists, None otherwise (read-only)"""
return self._value
@property
def offset(self) -> Optional[int]:
"""Offset where it exists, None otherwise (read-only)"""
return self._offset
@property
def confidence(self) -> Optional[int]:
"""Confidence where it exists, None otherwise (read-only)"""
return self._confidence
@decorators.passive
class ValueRange(object):
def __init__(self, start, end, step):
self._start = start
self._end = end
self._step = step
def __repr__(self):
if self.step == 1:
return f"<range: {self.start:#x} to {self.end:#x}>"
return f"<range: {self.start:#x} to {self.end:#x}, step {self.step:#x}>"
def __eq__(self, other):
if not isinstance(other, self.__class__):
return NotImplemented
return self.start == other.start and self.end == other.end and self.step == other.step
def __contains__(self, other):
if not isinstance(other, int):
return NotImplemented
return other in range(self._start, self._end, self._step)
@property
def start(self):
return self._start
@property
def end(self):
return self._end
@property
def step(self):
return self._step
@decorators.passive
class PossibleValueSet(object):
"""
`class PossibleValueSet` PossibleValueSet is used to define possible values
that a variable can take. It contains methods to instantiate different
value sets such as Constant, Signed/Unsigned Ranges, etc.
"""
def __init__(self, arch = None, value = None):
if value is None:
self._type = RegisterValueType.UndeterminedValue
return
self._type = RegisterValueType(value.state)
if value.state == RegisterValueType.EntryValue:
if arch is None:
self._reg = value.value
else:
self._reg = arch.get_reg_name(value.value)
elif value.state == RegisterValueType.ConstantValue:
self._value = value.value
elif value.state == RegisterValueType.ConstantPointerValue:
self._value = value.value
elif value.state == RegisterValueType.StackFrameOffset:
self._offset = value.value
elif value.state == RegisterValueType.SignedRangeValue:
self._offset = value.value
self._ranges = []
for i in range(0, value.count):
start = value.ranges[i].start
end = value.ranges[i].end
step = value.ranges[i].step
if start & (1 << 63):
start |= ~((1 << 63) - 1)
if end & (1 << 63):
end |= ~((1 << 63) - 1)
self._ranges.append(ValueRange(start, end, step))
elif value.state == RegisterValueType.UnsignedRangeValue:
self._offset = value.value
self._ranges = []
for i in range(0, value.count):
start = value.ranges[i].start
end = value.ranges[i].end
step = value.ranges[i].step
self._ranges.append(ValueRange(start, end, step))
elif value.state == RegisterValueType.LookupTableValue:
self._table = []
self._mapping = {}
for i in range(0, value.count):
from_list = []
for j in range(0, value.table[i].fromCount):
from_list.append(value.table[i].fromValues[j])
self._mapping[value.table[i].fromValues[j]] = value.table[i].toValue
self._table.append(LookupTableEntry(from_list, value.table[i].toValue))
elif (value.state == RegisterValueType.InSetOfValues) or (value.state == RegisterValueType.NotInSetOfValues):
self._values = set()
for i in range(0, value.count):
self._values.add(value.valueSet[i])
self._count = value.count
def __repr__(self):
if self._type == RegisterValueType.EntryValue:
return f"<entry {self.reg}>"
if self._type == RegisterValueType.ConstantValue:
return f"<const {self.value:#x}>"
if self._type == RegisterValueType.ConstantPointerValue:
return f"<const ptr {self.value:#x}>"
if self._type == RegisterValueType.StackFrameOffset:
return f"<stack frame offset {self._offset:#x}>"
if self._type == RegisterValueType.SignedRangeValue:
return f"<signed ranges: {repr(self.ranges)}>"
if self._type == RegisterValueType.UnsignedRangeValue:
return f"<unsigned ranges: {repr(self.ranges)}>"
if self._type == RegisterValueType.LookupTableValue:
return f"<table: {', '.join([repr(i) for i in self.table])}>"
if self._type == RegisterValueType.InSetOfValues:
return f"<in set([{', '.join(hex(i) for i in sorted(self.values))}])>"
if self._type == RegisterValueType.NotInSetOfValues:
return f"<not in set([{', '.join(hex(i) for i in sorted(self.values))}])>"
if self._type == RegisterValueType.ReturnAddressValue:
return "<return address>"
return "<undetermined>"
def __contains__(self, other):
if self.type in [RegisterValueType.ConstantValue, RegisterValueType.ConstantPointerValue] and isinstance(other, int):
return self.value == other
if self.type in [RegisterValueType.ConstantValue, RegisterValueType.ConstantPointerValue] and hasattr(other, "value"):
return self.value == other.value
if not isinstance(other, int):
return NotImplemented
#Initial implementation only checks numbers, no set logic
if self.type == RegisterValueType.StackFrameOffset:
return NotImplemented
if self.type in [RegisterValueType.SignedRangeValue, RegisterValueType.UnsignedRangeValue]:
for rng in self.ranges:
if other in rng:
return True
return False
if self.type == RegisterValueType.InSetOfValues:
return other in self.values
if self.type == RegisterValueType.NotInSetOfValues:
return not other in self.values
return NotImplemented
def __eq__(self, other):
if self.type in [RegisterValueType.ConstantValue, RegisterValueType.ConstantPointerValue] and isinstance(other, int):
return self.value == other
if not isinstance(other, self.__class__):
return NotImplemented
if self.type in [RegisterValueType.ConstantValue, RegisterValueType.ConstantPointerValue]:
return self.value == other.value
elif self.type == RegisterValueType.StackFrameOffset:
return self.offset == other.offset
elif self.type in [RegisterValueType.SignedRangeValue, RegisterValueType.UnsignedRangeValue]:
return self.ranges == other.ranges
elif self.type in [RegisterValueType.InSetOfValues, RegisterValueType.NotInSetOfValues]:
return self.values == other.values
elif self.type == RegisterValueType.UndeterminedValue and hasattr(other, 'type'):
return self.type == other.type
else:
return self == other
def __ne__(self, other):
if not isinstance(other, self.__class__):
return NotImplemented
return not (self == other)
def _to_api_object(self):
result = core.BNPossibleValueSet()
result.state = RegisterValueType(self.type)
if self.type == RegisterValueType.UndeterminedValue:
return result
elif self.type == RegisterValueType.ConstantValue:
result.value = self.value
elif self.type == RegisterValueType.ConstantPointerValue:
result.value = self.value
elif self.type == RegisterValueType.StackFrameOffset:
result.offset = self.value
elif self.type == RegisterValueType.SignedRangeValue:
result.offset = self.value
result.ranges = (core.BNValueRange * self.count)()
for i in range(0, self.count):
start = self.ranges[i].start
end = self.ranges[i].end
if start & (1 << 63):
start |= ~((1 << 63) - 1)
if end & (1 << 63):
end |= ~((1 << 63) - 1)
value_range = core.BNValueRange()
value_range.start = start
value_range.end = end
value_range.step = self.ranges[i].step
result.ranges[i] = value_range
result.count = self.count
elif self.type == RegisterValueType.UnsignedRangeValue:
result.offset = self.value
result.ranges = (core.BNValueRange * self.count)()
for i in range(0, self.count):
value_range = core.BNValueRange()
value_range.start = self.ranges[i].start
value_range.end = self.ranges[i].end
value_range.step = self.ranges[i].step
result.ranges[i] = value_range
result.count = self.count
elif self.type == RegisterValueType.LookupTableValue:
result.table = []
result.mapping = {}
for i in range(self.count):
from_list = []
for j in range(0, len(self.table[i].from_values)):
from_list.append(self.table[i].from_values[j])
result.mapping[self.table[i].from_values[j]] = result.table[i].to_value
result.table.append(LookupTableEntry(from_list, result.table[i].to_value))
result.count = self.count
elif (self.type == RegisterValueType.InSetOfValues) or (self.type == RegisterValueType.NotInSetOfValues):
values = (ctypes.c_longlong * self.count)()
i = 0
for value in self.values:
values[i] = value
i += 1
result.valueSet = ctypes.cast(values, ctypes.POINTER(ctypes.c_longlong))
result.count = self.count
return result
@property
def type(self) -> RegisterValueType:
return self._type
@property
def reg(self) -> 'binaryninja.architecture.RegisterName':
return self._reg
@property
def value(self) -> int:
return self._value
@property
def offset(self) -> int:
return self._offset
@property
def ranges(self) -> List[ValueRange]:
return self._ranges
@property
def table(self) -> List[LookupTableEntry]:
return self._table
@property
def mapping(self) -> Mapping[int, int]:
return self._mapping
@property
def values(self) -> Set[int]:
return self._values
@property
def count(self) -> int:
return self._count
@staticmethod
def undetermined() -> 'PossibleValueSet':
"""
Create a PossibleValueSet object of type UndeterminedValue.
:return: PossibleValueSet object of type UndeterminedValue
:rtype: PossibleValueSet
"""
return PossibleValueSet()
@staticmethod
def constant(value:int) -> 'PossibleValueSet':
"""
Create a constant valued PossibleValueSet object.
:param int value: Integer value of the constant
:rtype: PossibleValueSet
"""
result = PossibleValueSet()
result._type = RegisterValueType.ConstantValue
result._value = value
return result
@staticmethod
def constant_ptr(value:int) -> 'PossibleValueSet':
"""
Create constant pointer valued PossibleValueSet object.
:param int value: Integer value of the constant pointer
:rtype: PossibleValueSet
"""
result = PossibleValueSet()
result._type = RegisterValueType.ConstantPointerValue
result._value = value
return result
@staticmethod
def stack_frame_offset(offset:int) -> 'PossibleValueSet':
"""
Create a PossibleValueSet object for a stack frame offset.
:param int value: Integer value of the offset
:rtype: PossibleValueSet
"""
result = PossibleValueSet()
result._type = RegisterValueType.StackFrameOffset
result._offset = offset
return result
@staticmethod
def signed_range_value(ranges:List[ValueRange]) -> 'PossibleValueSet':
"""
Create a PossibleValueSet object for a signed range of values.
:param list(ValueRange) ranges: List of ValueRanges
:rtype: PossibleValueSet
:Example:
>>> v_1 = ValueRange(-5, -1, 1)
>>> v_2 = ValueRange(7, 10, 1)
>>> val = PossibleValueSet.signed_range_value([v_1, v_2])
<signed ranges: [<range: -0x5 to -0x1>, <range: 0x7 to 0xa>]>
"""
result = PossibleValueSet()
result._value = 0
result._type = RegisterValueType.SignedRangeValue
result._ranges = ranges
result._count = len(ranges)
return result
@staticmethod
def unsigned_range_value(ranges:List[ValueRange]) -> 'PossibleValueSet':
"""
Create a PossibleValueSet object for a unsigned signed range of values.
:param list(ValueRange) ranges: List of ValueRanges
:rtype: PossibleValueSet
:Example:
>>> v_1 = ValueRange(0, 5, 1)
>>> v_2 = ValueRange(7, 10, 1)
>>> val = PossibleValueSet.unsigned_range_value([v_1, v_2])
<unsigned ranges: [<range: 0x0 to 0x5>, <range: 0x7 to 0xa>]>
"""
result = PossibleValueSet()
result._value = 0
result._type = RegisterValueType.UnsignedRangeValue
result._ranges = ranges
result._count = len(ranges)
return result
@staticmethod
def in_set_of_values(values:Union[List[int], Set[int]]) -> 'PossibleValueSet':
"""
Create a PossibleValueSet object for a value in a set of values.
:param list(int) values: List of integer values
:rtype: PossibleValueSet
"""
result = PossibleValueSet()
result._type = RegisterValueType.InSetOfValues
result._values = set(values)
result._count = len(values)
return result
@staticmethod
def not_in_set_of_values(values) -> 'PossibleValueSet':
"""
Create a PossibleValueSet object for a value NOT in a set of values.
:param list(int) values: List of integer values
:rtype: PossibleValueSet
"""
result = PossibleValueSet()
result._type = RegisterValueType.NotInSetOfValues
result._values = set(values)
result._count = len(values)
return result
@staticmethod
def lookup_table_value(lookup_table, mapping) -> 'PossibleValueSet':
"""
Create a PossibleValueSet object for a value which is a member of a
lookuptable.
:param list(LookupTableEntry) lookup_table: List of table entries
:param dict of (int, int) mapping: Mapping used for resolution
:rtype: PossibleValueSet
"""
result = PossibleValueSet()
result._type = RegisterValueType.LookupTableValue
result._table = lookup_table
result._mapping = mapping
return result
@decorators.passive
class StackVariableReference(object):
def __init__(self, src_operand, t, name, var, ref_ofs, size):
self._source_operand = src_operand
self._type = t
self._name = name
self._var = var
self._referenced_offset = ref_ofs
self._size = size
if self._source_operand == 0xffffffff:
self._source_operand = None
def __repr__(self):
if self._source_operand is None:
if self._referenced_offset != self._var.storage:
return "<ref to %s%+#x>" % (self._name, self._referenced_offset - self._var.storage)
return "<ref to %s>" % self._name
if self._referenced_offset != self._var.storage:
return "<operand %d ref to %s%+#x>" % (self._source_operand, self._name, self._var.storage)
return "<operand %d ref to %s>" % (self._source_operand, self._name)
def __eq__(self, other):
if not isinstance(other, self.__class__):
return NotImplemented
return (self._source_operand, self._type, self._name, self._var, self._referenced_offset, self._size) == \
(other._source_operand, other._type, other._name, other._var, other._referenced_offset, other._size)
def __ne__(self, other):
if not isinstance(other, self.__class__):
return NotImplemented
return not (self == other)
def __hash__(self):
return hash((self._source_operand, self._type, self._name, self._var, self._referenced_offset, self._size))
@property
def source_operand(self):
return self._source_operand
@property
def type(self):
return self._type
@property
def name(self):
return self._name
@property
def var(self):
return self._var
@property
def referenced_offset(self):
return self._referenced_offset
@property
def size(self):
return self._size
@decorators.passive
class Variable(object):
def __init__(self, func, source_type, index, storage, name = None, var_type = None, identifier = None):
self._function = func
self._source_type = source_type
self._index = index
self._storage = storage
self._identifier = identifier
self._name = name
self._type = var_type
def __repr__(self):
if self.type is not None:
return f"<var {self.type.get_string_before_name()} {self.name}{self.type.get_string_after_name()}>"
else:
return f"<var {self.name}>"
def __str__(self):
return self.name
def __eq__(self, other):
if not isinstance(other, self.__class__):
return NotImplemented
return (self.identifier, self.function) == (other.identifier, other.function)
def __ne__(self, other):
if not isinstance(other, self.__class__):
return NotImplemented
return not (self == other)
def __lt__(self, other):
if not isinstance(other, self.__class__):
return NotImplemented
return (self.identifier, self.function) < (other.identifier, other.function)
def __gt__(self, other):
if not isinstance(other, self.__class__):
return NotImplemented
return (self.identifier, self.function) > (other.identifier, other.function)
def __le__(self, other):
if not isinstance(other, self.__class__):
return NotImplemented
return (self.identifier, self.function) <= (other.identifier, other.function)
def __ge__(self, other):
if not isinstance(other, self.__class__):
return NotImplemented
return (self.identifier, self.function) >= (other.identifier, other.function)
def __hash__(self):
return hash((self.identifier, self.function))
@property
def function(self) -> 'binaryninja.function.Function':
"""Function where the variable is defined"""
return self._function
@function.setter
def function(self, value:'binaryninja.function.Function'):
self._function = value
@property
def source_type(self) -> VariableSourceType:
""":class:`~enums.VariableSourceType`"""
if not isinstance(self._source_type, VariableSourceType):
self._source_type = VariableSourceType(self._source_type)
return self._source_type
@source_type.setter
def source_type(self, value:VariableSourceType) -> None:
self._source_type = value
@property
def index(self) -> int:
return self._index
@index.setter
def index(self, value:int) -> None:
self._index = value
@property
def storage(self) -> int:
"""Stack offset for StackVariableSourceType, register index for RegisterVariableSourceType"""
return self._storage
@storage.setter
def storage(self, value:int) -> None:
self._storage = value
@property
def identifier(self) -> int:
if self._identifier is None:
self._identifier = core.BNToVariableIdentifier(self.to_BNVariable())
return self._identifier
@property
def name(self):
"""Name of the variable"""
if self._name is None:
if self._function is not None:
self._name = core.BNGetVariableName(self._function.handle, self.to_BNVariable())
return self._name
@property
def type(self) -> Optional['binaryninja.types.Type']:
if self._type is None:
if self._function is not None:
var_type_conf = core.BNGetVariableType(self._function.handle, self.to_BNVariable())
if var_type_conf.type:
self._type = binaryninja.types.Type(var_type_conf.type, platform = self._function.platform, confidence = var_type_conf.confidence)
return self._type
def to_BNVariable(self):
v = core.BNVariable()
v.type = self.source_type
v.index = self._index
v.storage = self._storage
return v
@property
def dead_store_elimination(self):
if self._function is not None and self._identifier is not None:
return DeadStoreElimination(core.BNGetFunctionVariableDeadStoreElimination(self._function.handle, self.to_BNVariable()))
return None
@dead_store_elimination.setter
def dead_store_elimination(self, value):
core.BNSetFunctionVariableDeadStoreElimination(self._function.handle, self.to_BNVariable(), value)
@staticmethod
def from_identifier(func, identifier, name=None, var_type=None):
var = core.BNFromVariableIdentifier(identifier)
return Variable(func, VariableSourceType(var.type), var.index, var.storage, name, var_type, identifier)
@decorators.passive
class ConstantReference(object):
def __init__(self, val, size, ptr, intermediate):
self._value = val
self._size = size
self._pointer = ptr
self._intermediate = intermediate
def __repr__(self):
if self.pointer:
return "<constant pointer %#x>" % self.value
if self.size == 0:
return "<constant %#x>" % self.value
return "<constant %#x size %d>" % (self.value, self.size)
@property
def value(self):
return self._value
@property
def size(self):
return self._size
@property
def pointer(self):
return self._pointer
@property
def intermediate(self):
return self._intermediate
@decorators.passive
class UserVariableValueInfo(object):
def __init__(self, var, def_site, value):
self.var = var
self.def_site = def_site
self.value = value
def __repr__(self):
return "<user value for %s @ %s:%#x -> %s>" % (self.var, self.def_site.arch.name, self.def_site.addr, self.value)
@decorators.passive
class IndirectBranchInfo(object):
def __init__(self, source_arch, source_addr, dest_arch, dest_addr, auto_defined):
self.source_arch = source_arch
self.source_addr = source_addr
self.dest_arch = dest_arch
self.dest_addr = dest_addr
self.auto_defined = auto_defined
def __repr__(self):
return "<branch %s:%#x -> %s:%#x>" % (self.source_arch.name, self.source_addr, self.dest_arch.name, self.dest_addr)
@decorators.passive
class ParameterVariables(object):
def __init__(self, var_list:List[Variable], confidence:int=core.max_confidence, func:Optional['binaryninja.function.Function']=None):
self._vars = var_list
self._confidence = confidence
self._func = func
def __repr__(self):
return repr(self._vars)
def __len__(self):
return len(self._vars)
def __iter__(self) -> Generator['Variable', None, None]:
for var in self._vars:
yield var
def __getitem__(self, idx) -> 'Variable':
return self._vars[idx]
def __setitem__(self, idx:int, value:'Variable'):
self._vars[idx] = value
if self._func is not None:
self._func.parameter_vars = self
def with_confidence(self, confidence:int) -> 'ParameterVariables':
return ParameterVariables(list(self._vars), confidence, self._func)
@property
def vars(self) -> List['Variable']:
return self._vars
@property
def confidence(self) -> int:
return self._confidence
@property
def function(self) -> Optional['binaryninja.function.Function']:
return self._func
@decorators.passive
class AddressRange(object):
def __init__(self, start:int, end:int):
self._start = start
self._end = end
def __repr__(self):
return "<%#x-%#x>" % (self._start, self._end)
def __len__(self):
return self._end - self.start
def __eq__(self, other):
if not isinstance(other, self.__class__):
return NotImplemented
return (self._start, self._end) == (other._start, other._end)
def __ne__(self, other):
if not isinstance(other, self.__class__):
return NotImplemented
return not (self == other)
def __hash__(self):
return hash((self._start, self._end))
@property
def length(self) -> int:
return self._end - self._start
@property
def start(self) -> int:
return self._start
@property
def end(self) -> int:
return self._end
|