FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
binaryninja-api/python/highlevelil.py at dev · Vector35/binaryninja-api · GitHub
binaryninja-api/python/highlevelil.py at dev · Vector35/binaryninja-api · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
Accelerator
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
Uh oh!
There was an error while loading.
Please reload this page
.
Vector35
/
binaryninja-api
Public
Notifications
You must be signed in to change notification settings
Fork
294
Star
1.3k
Code
Issues
1.9k
Pull requests
37
Discussions
Actions
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
binaryninja-api
/
python
/
highlevelil.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
5270 lines (4384 loc) · 202 KB
Breadcrumbs
binaryninja-api
/
python
/
highlevelil.py
Copy path
File metadata and controls
5270 lines (4384 loc) · 202 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright (c) 2019-2026 Vector 35 Inc
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to
# deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
# sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.
import
ctypes
import
struct
from
typing
import
Optional
,
Generator
,
List
,
Union
,
NewType
,
Tuple
,
ClassVar
,
Mapping
,
Set
,
Callable
,
Any
,
Iterator
,
overload
from
dataclasses
import
dataclass
from
enum
import
Enum
# Binary Ninja components
from
.
import
_binaryninjacore
as
core
from
.
enums
import
(
HighLevelILOperation
,
DataFlowQueryOption
,
FunctionGraphType
,
ILInstructionAttribute
,
StringType
,
DisassemblyOption
)
from
.
import
function
from
.
import
binaryview
from
.
import
architecture
from
.
import
lowlevelil
from
.
import
mediumlevelil
from
.
import
basicblock
from
.
import
types
from
.
import
highlight
from
.
import
flowgraph
from
.
import
variable
from
.
import
databuffer
from
.
import
stringrecognizer
from
.
import
types
as
_types
from
.
interaction
import
show_graph_report
from
.
commonil
import
(
BaseILInstruction
,
Tailcall
,
Syscall
,
Localcall
,
Comparison
,
Signed
,
UnaryOperation
,
BinaryOperation
,
SSA
,
Phi
,
Loop
,
ControlFlow
,
Memory
,
Constant
,
Arithmetic
,
DoublePrecision
,
Terminal
,
FloatingPoint
,
Intrinsic
,
Return
,
VariableInstruction
,
SSAVariableInstruction
,
SetVar
,
ILSourceLocation
)
from
.
import
deprecation
TokenList
=
List
[
'function.InstructionTextToken'
]
LinesType
=
Generator
[
'function.DisassemblyTextLine'
,
None
,
None
]
ExpressionIndex
=
NewType
(
'ExpressionIndex'
,
int
)
InstructionIndex
=
NewType
(
'InstructionIndex'
,
int
)
Index
=
Union
[
ExpressionIndex
,
InstructionIndex
]
InstructionOrExpression
=
Union
[
'HighLevelILInstruction'
,
Index
]
HLILInstructionsType
=
Generator
[
'HighLevelILInstruction'
,
None
,
None
]
HLILBasicBlocksType
=
Generator
[
'HighLevelILBasicBlock'
,
None
,
None
]
OperandsType
=
Tuple
[
ExpressionIndex
,
ExpressionIndex
,
ExpressionIndex
,
ExpressionIndex
,
ExpressionIndex
]
HighLevelILOperandType
=
Union
[
'HighLevelILInstruction'
,
'lowlevelil.ILIntrinsic'
,
'variable.Variable'
,
'mediumlevelil.SSAVariable'
,
List
[
int
],
List
[
'variable.Variable'
],
List
[
'mediumlevelil.SSAVariable'
],
List
[
'HighLevelILInstruction'
],
Optional
[
int
],
float
,
'GotoLabel'
,
variable
.
ConstantData
,
databuffer
.
DataBuffer
]
VariablesList
=
List
[
Union
[
'mediumlevelil.SSAVariable'
,
'variable.Variable'
]]
StringOrType
=
Union
[
str
,
'_types.Type'
,
'_types.TypeBuilder'
]
ILInstructionAttributeSet
=
Union
[
Set
[
ILInstructionAttribute
],
List
[
ILInstructionAttribute
]]
HighLevelILVisitorCallback
=
Callable
[[
str
,
HighLevelILOperandType
,
str
,
Optional
[
'HighLevelILInstruction'
]],
bool
]
class
VariableReferenceType
(
Enum
):
Read
=
0
Written
=
1
AddressTaken
=
2
@
dataclass
(
frozen
=
True
)
class
HighLevelILOperationAndSize
:
operation
:
HighLevelILOperation
size
:
int
def
__repr__
(
self
):
if
self
.
size
==
0
:
return
f"<HighLevelILOperationAndSize:
{
self
.
operation
.
name
}
>"
return
f"<HighLevelILOperationAndSize:
{
self
.
operation
.
name
}
{
self
.
size
}
>"
@
dataclass
class
GotoLabel
:
function
:
'HighLevelILFunction'
id
:
int
def
__repr__
(
self
):
return
f"<GotoLabel:
{
self
.
name
}
>"
def
__str__
(
self
):
return
self
.
name
@
property
def
label_id
(
self
)
->
int
:
return
self
.
id
@
property
def
name
(
self
)
->
str
:
assert
self
.
function
.
source_function
is
not
None
,
"Cant get name of function without source_function"
return
core
.
BNGetGotoLabelName
(
self
.
function
.
source_function
.
handle
,
self
.
id
)
@
name
.
setter
def
name
(
self
,
value
:
str
)
->
None
:
assert
self
.
function
.
source_function
is
not
None
,
"Cant set name of function without source_function"
core
.
BNSetUserGotoLabelName
(
self
.
function
.
source_function
.
handle
,
self
.
id
,
value
)
@
property
def
definition
(
self
)
->
Optional
[
'HighLevelILInstruction'
]:
return
self
.
function
.
get_label
(
self
.
id
)
@
property
def
uses
(
self
)
->
List
[
'HighLevelILInstruction'
]:
return
self
.
function
.
get_label_uses
(
self
.
id
)
@
dataclass
(
frozen
=
True
,
order
=
True
)
class
CoreHighLevelILInstruction
:
operation
:
HighLevelILOperation
attributes
:
int
source_operand
:
int
size
:
int
operands
:
OperandsType
address
:
int
parent
:
ExpressionIndex
@
classmethod
def
from_BNHighLevelILInstruction
(
cls
,
instr
:
core
.
BNHighLevelILInstruction
)
->
'CoreHighLevelILInstruction'
:
operands
:
OperandsType
=
tuple
([
ExpressionIndex
(
instr
.
operands
[
i
])
for
i
in
range
(
5
)])
# type: ignore
return
cls
(
HighLevelILOperation
(
instr
.
operation
),
instr
.
attributes
,
instr
.
sourceOperand
,
instr
.
size
,
operands
,
instr
.
address
,
instr
.
parent
)
@
dataclass
(
frozen
=
True
)
class
HighLevelILInstruction
(
BaseILInstruction
):
"""
``class HighLevelILInstruction`` High Level Intermediate Language Instructions form an abstract syntax tree of
the code. Control flow structures are present as high level constructs in the HLIL tree.
"""
function
:
'HighLevelILFunction'
expr_index
:
ExpressionIndex
core_instr
:
CoreHighLevelILInstruction
as_ast
:
bool
instr_index
:
InstructionIndex
# ILOperations is deprecated and will be removed in a future version once BNIL Graph no longer uses it
# Use the visit methods visit, visit_all, and visit_operands
ILOperations
:
ClassVar
[
Mapping
[
HighLevelILOperation
,
List
[
Tuple
[
str
,
str
]]]]
=
{
HighLevelILOperation
.
HLIL_NOP
: [],
HighLevelILOperation
.
HLIL_BLOCK
: [(
"body"
,
"expr_list"
)],
HighLevelILOperation
.
HLIL_IF
: [(
"condition"
,
"expr"
), (
"true"
,
"expr"
),
(
"false"
,
"expr"
)],
HighLevelILOperation
.
HLIL_WHILE
: [(
"condition"
,
"expr"
),
(
"body"
,
"expr"
)],
HighLevelILOperation
.
HLIL_WHILE_SSA
: [(
"condition_phi"
,
"expr"
), (
"condition"
,
"expr"
),
(
"body"
,
"expr"
)],
HighLevelILOperation
.
HLIL_DO_WHILE
: [
(
"body"
,
"expr"
), (
"condition"
,
"expr"
)
],
HighLevelILOperation
.
HLIL_DO_WHILE_SSA
: [(
"body"
,
"expr"
),
(
"condition_phi"
,
"expr"
),
(
"condition"
,
"expr"
)],
HighLevelILOperation
.
HLIL_FOR
: [(
"init"
,
"expr"
), (
"condition"
,
"expr"
), (
"update"
,
"expr"
),
(
"body"
,
"expr"
)],
HighLevelILOperation
.
HLIL_FOR_SSA
: [
(
"init"
,
"expr"
), (
"condition_phi"
,
"expr"
), (
"condition"
,
"expr"
),
(
"update"
,
"expr"
), (
"body"
,
"expr"
)
],
HighLevelILOperation
.
HLIL_SWITCH
: [
(
"condition"
,
"expr"
), (
"default"
,
"expr"
), (
"cases"
,
"expr_list"
)
],
HighLevelILOperation
.
HLIL_CASE
: [(
"values"
,
"expr_list"
), (
"body"
,
"expr"
)],
HighLevelILOperation
.
HLIL_BREAK
: [],
HighLevelILOperation
.
HLIL_CONTINUE
: [],
HighLevelILOperation
.
HLIL_JUMP
: [
(
"dest"
,
"expr"
)
],
HighLevelILOperation
.
HLIL_RET
: [(
"src"
,
"expr_list"
)],
HighLevelILOperation
.
HLIL_NORET
: [],
HighLevelILOperation
.
HLIL_UNREACHABLE
: [],
HighLevelILOperation
.
HLIL_GOTO
: [(
"target"
,
"label"
)],
HighLevelILOperation
.
HLIL_LABEL
: [
(
"target"
,
"label"
)
],
HighLevelILOperation
.
HLIL_VAR_DECLARE
: [(
"var"
,
"var"
)],
HighLevelILOperation
.
HLIL_VAR_INIT
: [
(
"dest"
,
"var"
), (
"src"
,
"expr"
)
],
HighLevelILOperation
.
HLIL_VAR_INIT_SSA
: [
(
"dest"
,
"var_ssa"
), (
"src"
,
"expr"
)
],
HighLevelILOperation
.
HLIL_ASSIGN
: [(
"dest"
,
"expr"
),
(
"src"
,
"expr"
)],
HighLevelILOperation
.
HLIL_ASSIGN_UNPACK
: [
(
"dest"
,
"expr_list"
), (
"src"
,
"expr"
)
],
HighLevelILOperation
.
HLIL_ASSIGN_MEM_SSA
: [(
"dest"
,
"expr"
),
(
"dest_memory"
,
"int"
),
(
"src"
,
"expr"
),
(
"src_memory"
,
"int"
)],
HighLevelILOperation
.
HLIL_ASSIGN_UNPACK_MEM_SSA
: [
(
"dest"
,
"expr_list"
), (
"dest_memory"
,
"int"
), (
"src"
,
"expr"
), (
"src_memory"
,
"int"
)
],
HighLevelILOperation
.
HLIL_VAR
: [(
"var"
,
"var"
)],
HighLevelILOperation
.
HLIL_VAR_SSA
: [
(
"var"
,
"var_ssa"
)
],
HighLevelILOperation
.
HLIL_VAR_SSA_PARTIAL
: [
(
"var"
,
"var_ssa_dest_and_src"
), (
"prev"
,
"var_ssa_dest_and_src"
)
],
HighLevelILOperation
.
HLIL_VAR_PHI
: [(
"dest"
,
"var_ssa"
),
(
"src"
,
"var_ssa_list"
)],
HighLevelILOperation
.
HLIL_MEM_PHI
: [
(
"dest"
,
"int"
), (
"src"
,
"int_list"
)
],
HighLevelILOperation
.
HLIL_STRUCT_FIELD
: [
(
"src"
,
"expr"
), (
"offset"
,
"int"
), (
"member_index"
,
"member_index"
)
],
HighLevelILOperation
.
HLIL_ARRAY_INDEX
: [
(
"src"
,
"expr"
), (
"index"
,
"expr"
)
],
HighLevelILOperation
.
HLIL_ARRAY_INDEX_SSA
: [(
"src"
,
"expr"
),
(
"src_memory"
,
"int"
),
(
"index"
,
"expr"
)],
HighLevelILOperation
.
HLIL_SPLIT
: [(
"high"
,
"expr"
), (
"low"
,
"expr"
)],
HighLevelILOperation
.
HLIL_DEREF
: [
(
"src"
,
"expr"
)
],
HighLevelILOperation
.
HLIL_DEREF_FIELD
: [
(
"src"
,
"expr"
), (
"offset"
,
"int"
), (
"member_index"
,
"member_index"
)
],
HighLevelILOperation
.
HLIL_DEREF_SSA
: [
(
"src"
,
"expr"
), (
"src_memory"
,
"int"
)
],
HighLevelILOperation
.
HLIL_DEREF_FIELD_SSA
: [
(
"src"
,
"expr"
), (
"src_memory"
,
"int"
), (
"offset"
,
"int"
),
(
"member_index"
,
"member_index"
)
],
HighLevelILOperation
.
HLIL_ADDRESS_OF
: [(
"src"
,
"expr"
)],
HighLevelILOperation
.
HLIL_PASS_BY_REF
: [
(
"src"
,
"expr"
)
],
HighLevelILOperation
.
HLIL_RETURN_BY_REF
: [
(
"src"
,
"expr"
)
],
HighLevelILOperation
.
HLIL_CONST
: [
(
"constant"
,
"int"
)
],
HighLevelILOperation
.
HLIL_CONST_PTR
: [(
"constant"
,
"int"
)],
HighLevelILOperation
.
HLIL_EXTERN_PTR
: [
(
"constant"
,
"int"
), (
"offset"
,
"int"
)
],
HighLevelILOperation
.
HLIL_FLOAT_CONST
: [(
"constant"
,
"float"
)],
HighLevelILOperation
.
HLIL_IMPORT
: [
(
"constant"
,
"int"
)
],
HighLevelILOperation
.
HLIL_CONST_DATA
: [(
"constant"
,
"ConstantData"
)],
HighLevelILOperation
.
HLIL_CONST_DATA
: [
(
"constant"
,
"ConstantData"
)
],
HighLevelILOperation
.
HLIL_ADD
: [(
"left"
,
"expr"
), (
"right"
,
"expr"
)],
HighLevelILOperation
.
HLIL_ADC
: [
(
"left"
,
"expr"
), (
"right"
,
"expr"
), (
"carry"
,
"expr"
)
],
HighLevelILOperation
.
HLIL_SUB
: [(
"left"
,
"expr"
), (
"right"
,
"expr"
)],
HighLevelILOperation
.
HLIL_SBB
: [
(
"left"
,
"expr"
), (
"right"
,
"expr"
), (
"carry"
,
"expr"
)
],
HighLevelILOperation
.
HLIL_AND
: [(
"left"
,
"expr"
), (
"right"
,
"expr"
)],
HighLevelILOperation
.
HLIL_OR
: [
(
"left"
,
"expr"
), (
"right"
,
"expr"
)
],
HighLevelILOperation
.
HLIL_XOR
: [(
"left"
,
"expr"
), (
"right"
,
"expr"
)],
HighLevelILOperation
.
HLIL_LSL
: [
(
"left"
,
"expr"
), (
"right"
,
"expr"
)
],
HighLevelILOperation
.
HLIL_LSR
: [(
"left"
,
"expr"
), (
"right"
,
"expr"
)],
HighLevelILOperation
.
HLIL_ASR
: [
(
"left"
,
"expr"
), (
"right"
,
"expr"
)
],
HighLevelILOperation
.
HLIL_ROL
: [(
"left"
,
"expr"
), (
"right"
,
"expr"
)],
HighLevelILOperation
.
HLIL_RLC
: [
(
"left"
,
"expr"
), (
"right"
,
"expr"
), (
"carry"
,
"expr"
)
],
HighLevelILOperation
.
HLIL_ROR
: [(
"left"
,
"expr"
), (
"right"
,
"expr"
)],
HighLevelILOperation
.
HLIL_RRC
: [
(
"left"
,
"expr"
), (
"right"
,
"expr"
), (
"carry"
,
"expr"
)
],
HighLevelILOperation
.
HLIL_MUL
: [(
"left"
,
"expr"
), (
"right"
,
"expr"
)],
HighLevelILOperation
.
HLIL_MULU_DP
: [
(
"left"
,
"expr"
), (
"right"
,
"expr"
)
],
HighLevelILOperation
.
HLIL_MULS_DP
: [(
"left"
,
"expr"
), (
"right"
,
"expr"
)],
HighLevelILOperation
.
HLIL_DIVU
: [
(
"left"
,
"expr"
), (
"right"
,
"expr"
)
],
HighLevelILOperation
.
HLIL_DIVU_DP
: [(
"left"
,
"expr"
), (
"right"
,
"expr"
)],
HighLevelILOperation
.
HLIL_DIVS
: [
(
"left"
,
"expr"
), (
"right"
,
"expr"
)
],
HighLevelILOperation
.
HLIL_DIVS_DP
: [(
"left"
,
"expr"
), (
"right"
,
"expr"
)],
HighLevelILOperation
.
HLIL_MODU
: [
(
"left"
,
"expr"
), (
"right"
,
"expr"
)
],
HighLevelILOperation
.
HLIL_MODU_DP
: [(
"left"
,
"expr"
), (
"right"
,
"expr"
)],
HighLevelILOperation
.
HLIL_MODS
: [
(
"left"
,
"expr"
), (
"right"
,
"expr"
)
],
HighLevelILOperation
.
HLIL_MODS_DP
: [(
"left"
,
"expr"
), (
"right"
,
"expr"
)],
HighLevelILOperation
.
HLIL_NEG
: [
(
"src"
,
"expr"
)
],
HighLevelILOperation
.
HLIL_NOT
: [(
"src"
,
"expr"
)],
HighLevelILOperation
.
HLIL_BSWAP
: [
(
"src"
,
"expr"
)
],
HighLevelILOperation
.
HLIL_POPCNT
: [(
"src"
,
"expr"
)],
HighLevelILOperation
.
HLIL_CLZ
: [
(
"src"
,
"expr"
)
],
HighLevelILOperation
.
HLIL_CTZ
: [(
"src"
,
"expr"
)],
HighLevelILOperation
.
HLIL_RBIT
: [
(
"src"
,
"expr"
)
],
HighLevelILOperation
.
HLIL_CLS
: [(
"src"
,
"expr"
)],
HighLevelILOperation
.
HLIL_MINS
: [
(
"left"
,
"expr"
), (
"right"
,
"expr"
)
],
HighLevelILOperation
.
HLIL_MAXS
: [(
"left"
,
"expr"
), (
"right"
,
"expr"
)],
HighLevelILOperation
.
HLIL_MINU
: [
(
"left"
,
"expr"
), (
"right"
,
"expr"
)
],
HighLevelILOperation
.
HLIL_MAXU
: [(
"left"
,
"expr"
), (
"right"
,
"expr"
)],
HighLevelILOperation
.
HLIL_ABS
: [
(
"src"
,
"expr"
)
],
HighLevelILOperation
.
HLIL_SX
: [
(
"src"
,
"expr"
)
],
HighLevelILOperation
.
HLIL_ZX
: [(
"src"
,
"expr"
)],
HighLevelILOperation
.
HLIL_LOW_PART
: [
(
"src"
,
"expr"
)
],
HighLevelILOperation
.
HLIL_CALL
: [
(
"dest"
,
"expr"
), (
"params"
,
"expr_list"
)
],
HighLevelILOperation
.
HLIL_CALL_SSA
: [
(
"dest"
,
"expr"
), (
"params"
,
"expr_list"
), (
"dest_memory"
,
"int"
), (
"src_memory"
,
"int"
)
],
HighLevelILOperation
.
HLIL_CMP_E
: [(
"left"
,
"expr"
), (
"right"
,
"expr"
)],
HighLevelILOperation
.
HLIL_CMP_NE
: [
(
"left"
,
"expr"
), (
"right"
,
"expr"
)
],
HighLevelILOperation
.
HLIL_CMP_SLT
: [(
"left"
,
"expr"
), (
"right"
,
"expr"
)],
HighLevelILOperation
.
HLIL_CMP_ULT
: [(
"left"
,
"expr"
), (
"right"
,
"expr"
)],
HighLevelILOperation
.
HLIL_CMP_SLE
: [
(
"left"
,
"expr"
), (
"right"
,
"expr"
)
],
HighLevelILOperation
.
HLIL_CMP_ULE
: [(
"left"
,
"expr"
),
(
"right"
,
"expr"
)],
HighLevelILOperation
.
HLIL_CMP_SGE
: [
(
"left"
,
"expr"
), (
"right"
,
"expr"
)
],
HighLevelILOperation
.
HLIL_CMP_UGE
: [(
"left"
,
"expr"
),
(
"right"
,
"expr"
)],
HighLevelILOperation
.
HLIL_CMP_SGT
: [(
"left"
,
"expr"
), (
"right"
,
"expr"
)],
HighLevelILOperation
.
HLIL_CMP_UGT
: [
(
"left"
,
"expr"
), (
"right"
,
"expr"
)
],
HighLevelILOperation
.
HLIL_TEST_BIT
: [
(
"left"
,
"expr"
), (
"right"
,
"expr"
)
],
HighLevelILOperation
.
HLIL_BOOL_TO_INT
: [(
"src"
,
"expr"
)],
HighLevelILOperation
.
HLIL_ADD_OVERFLOW
: [
(
"left"
,
"expr"
), (
"right"
,
"expr"
)
],
HighLevelILOperation
.
HLIL_SYSCALL
: [(
"params"
,
"expr_list"
)],
HighLevelILOperation
.
HLIL_SYSCALL_SSA
: [
(
"params"
,
"expr_list"
), (
"dest_memory"
,
"int"
), (
"src_memory"
,
"int"
)
],
HighLevelILOperation
.
HLIL_TAILCALL
: [
(
"dest"
,
"expr"
), (
"params"
,
"expr_list"
)
],
HighLevelILOperation
.
HLIL_BP
: [],
HighLevelILOperation
.
HLIL_TRAP
: [
(
"vector"
,
"int"
)
],
HighLevelILOperation
.
HLIL_INTRINSIC
: [(
"intrinsic"
,
"intrinsic"
),
(
"params"
,
"expr_list"
)],
HighLevelILOperation
.
HLIL_INTRINSIC_SSA
: [
(
"intrinsic"
,
"intrinsic"
), (
"params"
,
"expr_list"
),
(
"dest_memory"
,
"int"
), (
"src_memory"
,
"int"
)
],
HighLevelILOperation
.
HLIL_UNDEF
: [],
HighLevelILOperation
.
HLIL_UNIMPL
: [],
HighLevelILOperation
.
HLIL_UNIMPL_MEM
: [
(
"src"
,
"expr"
)
],
HighLevelILOperation
.
HLIL_STRUCT_INIT
: [
(
"fields"
,
"expr_list"
)
],
HighLevelILOperation
.
HLIL_STRUCT_INIT_FIELD
: [
(
"offset"
,
"int"
), (
"member_index"
,
"member_index"
), (
"src"
,
"expr"
)
],
HighLevelILOperation
.
HLIL_FADD
: [(
"left"
,
"expr"
), (
"right"
,
"expr"
)],
HighLevelILOperation
.
HLIL_FSUB
: [
(
"left"
,
"expr"
), (
"right"
,
"expr"
)
],
HighLevelILOperation
.
HLIL_FMUL
: [(
"left"
,
"expr"
), (
"right"
,
"expr"
)],
HighLevelILOperation
.
HLIL_FDIV
: [
(
"left"
,
"expr"
), (
"right"
,
"expr"
)
],
HighLevelILOperation
.
HLIL_FSQRT
: [(
"src"
,
"expr"
)],
HighLevelILOperation
.
HLIL_FNEG
: [
(
"src"
,
"expr"
)
],
HighLevelILOperation
.
HLIL_FABS
: [(
"src"
,
"expr"
)],
HighLevelILOperation
.
HLIL_FLOAT_TO_INT
: [
(
"src"
,
"expr"
)
],
HighLevelILOperation
.
HLIL_INT_TO_FLOAT
: [(
"src"
,
"expr"
)],
HighLevelILOperation
.
HLIL_FLOAT_CONV
: [
(
"src"
,
"expr"
)
],
HighLevelILOperation
.
HLIL_ROUND_TO_INT
: [(
"src"
,
"expr"
)],
HighLevelILOperation
.
HLIL_FLOOR
: [
(
"src"
,
"expr"
)
],
HighLevelILOperation
.
HLIL_CEIL
: [(
"src"
,
"expr"
)],
HighLevelILOperation
.
HLIL_FTRUNC
: [
(
"src"
,
"expr"
)
],
HighLevelILOperation
.
HLIL_FCMP_E
: [(
"left"
,
"expr"
), (
"right"
,
"expr"
)],
HighLevelILOperation
.
HLIL_FCMP_NE
: [
(
"left"
,
"expr"
), (
"right"
,
"expr"
)
],
HighLevelILOperation
.
HLIL_FCMP_LT
: [
(
"left"
,
"expr"
), (
"right"
,
"expr"
)
],
HighLevelILOperation
.
HLIL_FCMP_LE
: [
(
"left"
,
"expr"
), (
"right"
,
"expr"
)
],
HighLevelILOperation
.
HLIL_FCMP_GE
: [
(
"left"
,
"expr"
), (
"right"
,
"expr"
)
],
HighLevelILOperation
.
HLIL_FCMP_GT
: [(
"left"
,
"expr"
), (
"right"
,
"expr"
)],
HighLevelILOperation
.
HLIL_FCMP_O
: [
(
"left"
,
"expr"
), (
"right"
,
"expr"
)
],
HighLevelILOperation
.
HLIL_FCMP_UO
: [(
"left"
,
"expr"
), (
"right"
,
"expr"
)]
}
@
staticmethod
def
show_hlil_hierarchy
():
"""
Opens a new tab showing the HLIL hierarchy which includes classes which can
easily be used with isinstance to match multiple types of IL instructions.
"""
graph
=
flowgraph
.
FlowGraph
()
nodes
=
{}
for
instruction
in
ILInstruction
.
values
():
instruction
.
add_subgraph
(
graph
,
nodes
)
show_graph_report
(
"HLIL Class Hierarchy Graph"
,
graph
)
@
classmethod
def
create
(
cls
,
func
:
'HighLevelILFunction'
,
expr_index
:
ExpressionIndex
,
as_ast
:
bool
=
True
,
instr_index
:
Optional
[
InstructionIndex
]
=
None
)
->
'HighLevelILInstruction'
:
assert
func
.
arch
is
not
None
,
"Attempted to create IL instruction with function missing an Architecture"
instr
=
core
.
BNGetHighLevelILByIndex
(
func
.
handle
,
expr_index
,
as_ast
)
assert
instr
is
not
None
,
"core.BNGetHighLevelILByIndex returned None"
core_instr
=
CoreHighLevelILInstruction
.
from_BNHighLevelILInstruction
(
instr
)
if
instr_index
is
None
:
instr_index
=
core
.
BNGetHighLevelILInstructionForExpr
(
func
.
handle
,
expr_index
)
assert
instr_index
is
not
None
,
"core.BNGetHighLevelILInstructionForExpr returned None"
return
ILInstruction
[
instr
.
operation
](
func
,
expr_index
,
core_instr
,
as_ast
,
instr_index
)
def
__str__
(
self
):
settings
=
function
.
DisassemblySettings
.
default_settings
()
settings
.
set_option
(
DisassemblyOption
.
DisableLineFormatting
)
lines
=
self
.
get_lines
(
settings
)
if
lines
is
None
:
return
"invalid"
result
=
[]
for
line
in
lines
:
cur
=
""
for
token
in
line
.
tokens
:
cur
+=
token
.
text
result
.
append
(
cur
)
return
'
\n
'
.
join
(
result
)
def
__repr__
(
self
):
settings
=
function
.
DisassemblySettings
.
default_settings
()
settings
.
set_option
(
DisassemblyOption
.
DisableLineFormatting
)
lines
=
self
.
get_lines
(
settings
)
continuation
=
""
if
lines
is
None
:
first_line
=
"<invalid>"
else
:
first_line
=
""
for
token
in
next
(
lines
).
tokens
:
first_line
+=
token
.
text
if
len
(
list
(
lines
))
>
1
:
continuation
=
"..."
return
f"<
{
self
.
__class__
.
__name__
}
:
{
first_line
}
{
continuation
}
>"
def
__eq__
(
self
,
other
:
'HighLevelILInstruction'
):
if
not
isinstance
(
other
,
HighLevelILInstruction
):
return
NotImplemented
return
self
.
function
==
other
.
function
and
self
.
expr_index
==
other
.
expr_index
def
__lt__
(
self
,
other
:
'HighLevelILInstruction'
):
if
not
isinstance
(
other
,
HighLevelILInstruction
):
return
NotImplemented
return
self
.
function
==
other
.
function
and
self
.
expr_index
<
other
.
expr_index
def
__le__
(
self
,
other
:
'HighLevelILInstruction'
):
if
not
isinstance
(
other
,
HighLevelILInstruction
):
return
NotImplemented
return
self
.
function
==
other
.
function
and
self
.
expr_index
<=
other
.
expr_index
def
__gt__
(
self
,
other
:
'HighLevelILInstruction'
):
if
not
isinstance
(
other
,
HighLevelILInstruction
):
return
NotImplemented
return
self
.
function
==
other
.
function
and
self
.
expr_index
>
other
.
expr_index
def
__ge__
(
self
,
other
:
'HighLevelILInstruction'
):
if
not
isinstance
(
other
,
HighLevelILInstruction
):
return
NotImplemented
return
self
.
function
==
other
.
function
and
self
.
expr_index
>=
other
.
expr_index
def
__hash__
(
self
):
return
hash
((
self
.
function
,
self
.
expr_index
))
@
property
def
tokens
(
self
)
->
TokenList
:
"""HLIL tokens taken from the HLIL text lines (read-only) -- does not include newlines or indentation, use lines for that information"""
settings
=
function
.
DisassemblySettings
.
default_settings
()
settings
.
set_option
(
DisassemblyOption
.
DisableLineFormatting
)
return
[
token
for
line
in
self
.
get_lines
(
settings
)
for
token
in
line
.
tokens
]
@
property
def
lines
(
self
)
->
LinesType
:
"""HLIL text lines (read-only)"""
return
self
.
get_lines
()
@
property
def
prefix_operands
(
self
)
->
List
[
Union
[
HighLevelILOperandType
,
HighLevelILOperationAndSize
]]:
"""All operands in the expression tree in prefix order"""
result
:
List
[
Union
[
HighLevelILOperandType
,
HighLevelILOperationAndSize
]]
=
[
HighLevelILOperationAndSize
(
self
.
operation
,
self
.
size
)]
for
operand
in
self
.
operands
:
if
isinstance
(
operand
,
HighLevelILInstruction
):
result
.
extend
(
operand
.
prefix_operands
)
else
:
result
.
append
(
operand
)
return
result
@
property
def
postfix_operands
(
self
)
->
List
[
Union
[
HighLevelILOperandType
,
HighLevelILOperationAndSize
]]:
"""All operands in the expression tree in postfix order"""
result
:
List
[
Union
[
HighLevelILOperandType
,
HighLevelILOperationAndSize
]]
=
[]
for
operand
in
self
.
operands
:
if
isinstance
(
operand
,
HighLevelILInstruction
):
result
.
extend
(
operand
.
postfix_operands
)
else
:
result
.
append
(
operand
)
result
.
append
(
HighLevelILOperationAndSize
(
self
.
operation
,
self
.
size
))
return
result
@
property
def
instr
(
self
)
->
'HighLevelILInstruction'
:
"""The statement that this expression belongs to (read-only)"""
return
self
.
function
[
self
.
instr_index
]
@
property
def
ast
(
self
)
->
'HighLevelILInstruction'
:
"""
This expression with full AST printing (read-only)
See `AST and Non-AST Forms <https://docs.binary.ninja/dev/bnil-hlil.html#ast-and-non-ast-forms>`_
"""
if
self
.
as_ast
:
return
self
return
HighLevelILInstruction
.
create
(
self
.
function
,
self
.
expr_index
,
True
)
@
property
def
non_ast
(
self
)
->
'HighLevelILInstruction'
:
"""
This expression without full AST printing (read-only)
See `AST and Non-AST Forms <https://docs.binary.ninja/dev/bnil-hlil.html#ast-and-non-ast-forms>`_
"""
if
not
self
.
as_ast
:
return
self
return
HighLevelILInstruction
.
create
(
self
.
function
,
self
.
expr_index
,
False
)
@
property
def
operation
(
self
)
->
HighLevelILOperation
:
return
self
.
core_instr
.
operation
@
property
def
size
(
self
)
->
int
:
return
self
.
core_instr
.
size
@
property
def
address
(
self
)
->
int
:
return
self
.
core_instr
.
address
@
property
def
source_operand
(
self
)
->
ExpressionIndex
:
return
ExpressionIndex
(
self
.
core_instr
.
source_operand
)
@
property
def
source_location
(
self
)
->
ILSourceLocation
:
return
ILSourceLocation
.
from_instruction
(
self
)
@
property
def
core_operands
(
self
)
->
OperandsType
:
return
self
.
core_instr
.
operands
@
property
def
instruction_operands
(
self
)
->
List
[
'HighLevelILInstruction'
]:
result
=
[]
for
i
in
self
.
operands
:
if
isinstance
(
i
,
list
):
result
.
extend
([
j
for
j
in
i
if
isinstance
(
j
,
HighLevelILInstruction
)])
elif
isinstance
(
i
,
HighLevelILInstruction
):
result
.
append
(
i
)
return
result
@
property
def
vars_written
(
self
)
->
VariablesList
:
"""List of variables value is written by this instruction"""
result
=
[]
for
i
in
self
.
operands
:
if
isinstance
(
i
,
HighLevelILInstruction
):
result
.
extend
(
i
.
vars_written
)
return
result
@
property
def
vars_read
(
self
)
->
VariablesList
:
"""Non-unique list of variables whose value is read by this instruction"""
non_read
=
[
*
self
.
vars_written
,
*
self
.
vars_address_taken
]
result
=
[]
for
v
in
self
.
vars
:
if
v
in
non_read
:
non_read
.
remove
(
v
)
continue
result
.
append
(
v
)
return
result
@
property
def
vars_address_taken
(
self
)
->
VariablesList
:
"""
Non-unique list of variables whose address is taken by instruction
.. note:: This property has some nuance to it, so use carefully. This property will return only those variable which
\
directly have their address taken such as `&var_4` or `&var_8.d` but not those which are involved in an address
\
calculation such as `&(var_4 + 0)` or `&var_4[0]` even though they may be functionally equivalent.
"""
result
=
[]
for
operand
in
self
.
instruction_operands
:
result
.
extend
(
operand
.
vars_address_taken
)
return
result
@
property
def
vars
(
self
)
->
VariablesList
:
"""Non-unique list of variables read by instruction"""
result
=
[]
for
operand
in
self
.
operands
:
if
isinstance
(
operand
,
HighLevelILInstruction
):
result
.
extend
(
operand
.
vars
)
elif
isinstance
(
operand
, (
variable
.
Variable
,
mediumlevelil
.
SSAVariable
)):
result
.
append
(
operand
)
elif
isinstance
(
operand
,
list
):
for
sub_operand
in
operand
:
if
isinstance
(
sub_operand
, (
variable
.
Variable
,
mediumlevelil
.
SSAVariable
)):
result
.
append
(
sub_operand
)
elif
isinstance
(
sub_operand
,
HighLevelILInstruction
):
result
.
extend
(
sub_operand
.
vars
)
return
result
@
property
def
parent
(
self
)
->
Optional
[
'HighLevelILInstruction'
]:
if
self
.
core_instr
.
parent
>=
core
.
BNGetHighLevelILExprCount
(
self
.
function
.
handle
):
return
None
return
HighLevelILInstruction
.
create
(
self
.
function
,
self
.
core_instr
.
parent
,
self
.
as_ast
)
@
property
def
ssa_form
(
self
)
->
'HighLevelILInstruction'
:
"""SSA form of expression (read-only)"""
assert
self
.
function
.
ssa_form
is
not
None
return
HighLevelILInstruction
.
create
(
self
.
function
.
ssa_form
,
ExpressionIndex
(
core
.
BNGetHighLevelILSSAExprIndex
(
self
.
function
.
handle
,
self
.
expr_index
)),
self
.
as_ast
)
@
property
def
non_ssa_form
(
self
)
->
Optional
[
'HighLevelILInstruction'
]:
"""Non-SSA form of expression (read-only)"""
if
self
.
function
.
non_ssa_form
is
None
:
return
None
return
HighLevelILInstruction
.
create
(
self
.
function
.
non_ssa_form
,
ExpressionIndex
(
core
.
BNGetHighLevelILNonSSAExprIndex
(
self
.
function
.
handle
,
self
.
expr_index
)),
self
.
as_ast
)
@
property
def
medium_level_il
(
self
)
->
Optional
[
'mediumlevelil.MediumLevelILInstruction'
]:
"""Medium level IL form of this expression"""
expr
=
self
.
function
.
get_medium_level_il_expr_index
(
self
.
expr_index
)
if
expr
is
None
:
return
None
mlil
=
self
.
function
.
medium_level_il
if
mlil
is
None
:
return
None
ssa_func
=
mlil
.
ssa_form
assert
ssa_func
is
not
None
,
"medium_level_il.ssa_form is None"
return
mediumlevelil
.
MediumLevelILInstruction
.
create
(
ssa_func
,
expr
)
@
property
def
mlil
(
self
)
->
Optional
[
'mediumlevelil.MediumLevelILInstruction'
]:
"""Alias for medium_level_il"""
return
self
.
medium_level_il
@
property
def
mlils
(
self
)
->
Optional
[
List
[
'mediumlevelil.MediumLevelILInstruction'
]]:
result
=
[]
for
expr
in
self
.
function
.
get_medium_level_il_expr_indexes
(
self
.
expr_index
):
mlil
=
self
.
function
.
medium_level_il
if
mlil
is
None
:
return
ssa_func
=
mlil
.
ssa_form
assert
ssa_func
is
not
None
,
"medium_level_il.ssa_form is None"
result
.
append
(
mediumlevelil
.
MediumLevelILInstruction
.
create
(
ssa_func
,
expr
))
return
result
@
property
def
low_level_il
(
self
)
->
Optional
[
'lowlevelil.LowLevelILInstruction'
]:
"""Low level IL form of this expression"""
if
self
.
mlil
is
None
:
return
None
return
self
.
mlil
.
llil
@
property
def
llil
(
self
)
->
Optional
[
'lowlevelil.LowLevelILInstruction'
]:
"""Alias for low_level_il"""
return
self
.
low_level_il
@
property
def
llils
(
self
)
->
List
[
'lowlevelil.ExpressionIndex'
]:
result
=
set
()
mlils
=
self
.
mlils
if
mlils
is
None
:
return
[]
for
mlil_expr
in
mlils
:
for
llil_expr
in
mlil_expr
.
llils
:
result
.
add
(
llil_expr
)
return
list
(
result
)
@
property
def
il_basic_block
(
self
)
->
Optional
[
'HighLevelILBasicBlock'
]:
"""
IL basic block object containing this expression (read-only) (only available on finalized functions).
Returns None for HLIL_BLOCK expressions as these can contain multiple basic blocks.
"""
core_block
=
core
.
BNGetHighLevelILBasicBlockForInstruction
(
self
.
function
.
handle
,
self
.
instr_index
)
assert
core_block
is
not
None
,
"core.BNGetHighLevelILBasicBlockForInstruction returned None"
if
self
.
function
.
source_function
is
None
:
return
None
return
HighLevelILBasicBlock
(
core_block
,
self
.
function
,
self
.
function
.
source_function
.
view
)
@
property
def
value
(
self
)
->
'variable.RegisterValue'
:
"""Value of expression if constant or a known value (read-only)"""
mlil
=
self
.
mlil
if
mlil
is
None
:
return
variable
.
Undetermined
()
return
mlil
.
value
@
property
def
possible_values
(
self
)
->
'variable.PossibleValueSet'
:
"""Possible values of expression using path-sensitive static data flow analysis (read-only)"""
mlil
=
self
.
mlil
if
mlil
is
None
:
return
variable
.
PossibleValueSet
()
return
mlil
.
possible_values
@
property
def
expr_type
(
self
)
->
Optional
[
'types.Type'
]:
"""Type of expression"""
result
=
core
.
BNGetHighLevelILExprType
(
self
.
function
.
handle
,
self
.
expr_index
)
if
result
.
type
:
platform
=
None
if
self
.
function
.
source_function
:
platform
=
self
.
function
.
source_function
.
platform
return
types
.
Type
.
create
(
result
.
type
,
platform
=
platform
,
confidence
=
result
.
confidence
)
return
None
@
property
def
attributes
(
self
)
->
Set
[
ILInstructionAttribute
]:
"""The set of optional attributes placed on the instruction"""
result
:
Set
[
ILInstructionAttribute
]
=
set
()
for
flag
in
ILInstructionAttribute
:
if
self
.
core_instr
.
attributes
&
flag
.
value
!=
0
:
result
.
add
(
flag
)
return
result
def
get_possible_values
(
self
,
options
:
Optional
[
List
[
DataFlowQueryOption
]]
=
None
)
->
'variable.PossibleValueSet'
:
mlil
=
self
.
mlil
if
mlil
is
None
:
return
variable
.
PossibleValueSet
()
if
options
is
None
:
options
=
[]
return
mlil
.
get_possible_values
(
options
)
@
property
def
ssa_memory_version
(
self
)
->
int
:
"""Version of active memory contents in SSA form for this instruction"""
return
core
.
BNGetHighLevelILSSAMemoryVersionAtILInstruction
(
self
.
function
.
handle
,
self
.
instr_index
)
def
get_ssa_var_version
(
self
,
var
:
'variable.Variable'
)
->
int
:
var_data
=
var
.
to_BNVariable
()
return
core
.
BNGetHighLevelILSSAVarVersionAtILInstruction
(
self
.
function
.
handle
,
var_data
,
self
.
instr_index
)
def
_get_int
(
self
,
operand_index
:
int
)
->
int
:
value
=
self
.
core_instr
.
operands
[
operand_index
]
return
(
value
&
((
1
<<
63
)
-
1
))
-
(
value
&
(
1
<<
63
))
def
_get_float
(
self
,
operand_index
:
int
)
->
float
:
value
=
self
.
core_instr
.
operands
[
operand_index
]
if
self
.
core_instr
.
size
==
4
:
return
struct
.
unpack
(
"f"
,
struct
.
pack
(
"I"
,
value
&
0xffffffff
))[
0
]
elif
self
.
core_instr
.
size
==
8
:
return
struct
.
unpack
(
"d"
,
struct
.
pack
(
"Q"
,
value
))[
0
]
else
:
return
float
(
value
)
def
_get_constant_data
(
self
,
operand_index1
:
int
,
operand_index2
:
int
)
->
variable
.
ConstantData
:
state
=
variable
.
RegisterValueType
(
self
.
core_instr
.
operands
[
operand_index1
])
value
=
self
.
core_instr
.
operands
[
operand_index2
]
return
variable
.
ConstantData
(
value
,
0
,
state
,
core
.
max_confidence
,
self
.
core_instr
.
size
,
self
.
function
.
source_function
)
def
_get_expr
(
self
,
operand_index
:
int
)
->
'HighLevelILInstruction'
:
return
HighLevelILInstruction
.
create
(
self
.
function
,
ExpressionIndex
(
self
.
core_instr
.
operands
[
operand_index
]),
self
.
as_ast
)
def
_get_intrinsic
(
self
,
operand_index
:
int
)
->
'lowlevelil.ILIntrinsic'
:
if
self
.
function
.
arch
is
None
:
raise
ValueError
(
"Attempting to create ILIntrinsic from function with no Architecture"
)
return
lowlevelil
.
ILIntrinsic
(
self
.
function
.
arch
,
architecture
.
IntrinsicIndex
(
self
.
core_instr
.
operands
[
operand_index
])
)
def
_get_var
(
self
,
operand_index
:
int
)
->
'variable.Variable'
:
value
=
self
.
core_instr
.
operands
[
operand_index
]
return
variable
.
Variable
.
from_identifier
(
self
.
function
,
value
)
def
_get_var_ssa
(
self
,
operand_index1
:
int
,
operand_index2
:
int
)
->
'mediumlevelil.SSAVariable'
:
var
=
variable
.
Variable
.
from_identifier
(
self
.
function
,
self
.
core_instr
.
operands
[
operand_index1
])
version
=
self
.
core_instr
.
operands
[
operand_index2
]
return
mediumlevelil
.
SSAVariable
(
var
,
version
)
def
_get_var_ssa_dest_and_src
(
self
,
operand_index1
:
int
,
operand_index2
:
int
)
->
'mediumlevelil.SSAVariable'
:
var
=
variable
.
Variable
.
from_identifier
(
self
.
function
,
self
.
core_instr
.
operands
[
operand_index1
])
dest_version
=
self
.
core_instr
.
operands
[
operand_index2
]
return
mediumlevelil
.
SSAVariable
(
var
,
dest_version
)
def
_get_int_list
(
self
,
operand_index
:
int
)
->
List
[
int
]:
count
=
ctypes
.
c_ulonglong
()
operand_list
=
core
.
BNHighLevelILGetOperandList
(
self
.
function
.
handle
,
self
.
expr_index
,
operand_index
,
count
)
assert
operand_list
is
not
None
,
"core.BNHighLevelILGetOperandList returned None"
value
:
List
[
int
]
=
[]
try
:
for
j
in
range
(
count
.
value
):
value
.
append
(
operand_list
[
j
])
return
value
finally
:
core
.
BNHighLevelILFreeOperandList
(
operand_list
)
def
_get_expr_list
(
self
,
operand_index1
:
int
,
_
:
int
)
->
List
[
'HighLevelILInstruction'
]:
count
=
ctypes
.
c_ulonglong
()
operand_list
=
core
.
BNHighLevelILGetOperandList
(
self
.
function
.
handle
,
self
.
expr_index
,
operand_index1
,
count
)
assert
operand_list
is
not
None
,
"core.BNHighLevelILGetOperandList returned None"
value
:
List
[
HighLevelILInstruction
]
=
[]
try
:
for
j
in
range
(
count
.
value
):
value
.
append
(
HighLevelILInstruction
.
create
(
self
.
function
,
operand_list
[
j
],
self
.
as_ast
))
return
value
finally
:
core
.
BNHighLevelILFreeOperandList
(
operand_list
)
def
_get_var_ssa_list
(
self
,
operand_index1
:
int
,
_
:
int
)
->
List
[
'mediumlevelil.SSAVariable'
]:
count
=
ctypes
.
c_ulonglong
()
operand_list
=
core
.
BNHighLevelILGetOperandList
(
self
.
function
.
handle
,
self
.
expr_index
,
operand_index1
,
count
)
assert
operand_list
is
not
None
,
"core.BNHighLevelILGetOperandList returned None"
value
=
[]
try
:
for
j
in
range
(
count
.
value
//
2
):
var_id
=
operand_list
[
j
*
2
]
var_version
=
operand_list
[(
j
*
2
)
+
1
]
value
.
append
(
mediumlevelil
.
SSAVariable
(
variable
.
Variable
.
from_identifier
(
self
.
function
,
var_id
),
var_version
)
)
return
value
finally
:
core
.
BNMediumLevelILFreeOperandList
(
operand_list
)
def
_get_member_index
(
self
,
operand_index
:
int
)
->
Optional
[
int
]:
value
=
self
.
core_instr
.
operands
[
operand_index
]
if
(
value
&
(
1
<<
63
))
!=
0
:
value
=
None
return
value
def
_get_label
(
self
,
operand_index
:
int
)
->
GotoLabel
:
return
GotoLabel
(
self
.
function
,
self
.
core_instr
.
operands
[
operand_index
])
def
_get_constraint
(
self
,
operand_index
:
int
)
->
variable
.
PossibleValueSet
:
value
=
core
.
BNGetCachedHighLevelILPossibleValueSet
(
self
.
function
.
handle
,
self
.
core_instr
.
operands
[
operand_index
])
result
=
variable
.
PossibleValueSet
(
self
.
function
.
arch
,
value
)
core
.
BNFreePossibleValueSet
(
value
)
return
result
@
property
def
raw_operands
(
self
)
->
OperandsType
:
"""Raw operand expression indices as specified by the core structure (read-only)"""
return
self
.
instr
.
operands
@
property
def
operands
(
self
)
->
List
[
HighLevelILOperandType
]:
"""
Operands for the instruction
Consider using more specific APIs for ``src``, ``dest``, ``params``, etc where appropriate.
"""
return
list
(
map
(
lambda
x
:
x
[
1
],
self
.
detailed_operands
))
@
property
def
detailed_operands
(
self
)
->
List
[
Tuple
[
str
,
HighLevelILOperandType
,
str
]]:
"""
Returns a list of tuples containing the name of the operand, the operand, and the type of the operand.
Useful for iterating over all operands of an instruction and sub-instructions.
"""
return
[]
def
traverse
(
self
,
cb
:
Callable
[[
'HighLevelILInstruction'
,
Any
],
Any
],
*
args
:
Any
,
shallow
:
bool
=
True
,
**
kwargs
:
Any
)
->
Iterator
[
Any
]:
"""
``traverse`` is a generator that allows you to traverse the HLIL AST in a depth-first manner. It will yield the
result of the callback function for each node in the AST. Arguments can be passed to the callback function using
``args`` and ``kwargs``. See the `Developer Docs <https://docs.binary.ninja/dev/concepts.html#walking-ils>`_ for more examples.
:param Callable[[HighLevelILInstruction, Any], Any] cb: The callback function to call for each node in the HighLevelILInstruction
:param Any args: Custom user-defined arguments
:param bool shallow: Whether traversal occurs on block instructions
:param Any kwargs: Custom user-defined keyword arguments
:return: An iterator of the results of the callback function
:rtype: Iterator[Any]
:Example:
>>> def get_constant_less_than_value(inst: HighLevelILInstruction, value: int) -> int:
... if isinstance(inst, Constant) and inst.constant < value:
... return inst.constant
>>>
>>> for result in inst.traverse(get_constant_less_than_value, 10):
... print(f"Found a constant {result} < 10 in {repr(inst)}")
:Example:
>>> def get_import_data_var_with_name(inst: HighLevelILInstruction, name: str) -> Optional['DataVariable']:
... if isinstance(inst, HighLevelILImport):
... if bv.get_symbol_at(inst.constant).name == name:
... return bv.get_data_var_at(inst.constant)
>>>
>>> for result in inst.traverse(get_import_data_var_with_name, "__cxa_finalize", shallow=False):
... print(f"Found import at {result} in {repr(inst)}")
"""
if
(
result
:=
cb
(
self
,
*
args
,
**
kwargs
))
is
not
None
:
yield
result
blacklisted_op_names
=
{
'true'
,
'false'
,
'body'
,
'cases'
,
'default'
}
for
op_name
,
op
,
_
in
self
.
detailed_operands
:
if
shallow
and
op_name
in
blacklisted_op_names
:
continue
if
isinstance
(
op
,
HighLevelILInstruction
):
yield
from
op
.
traverse
(
cb
,
*
args
,
shallow
=
shallow
,
**
kwargs
)
elif
isinstance
(
op
,
list
)
and
all
(
isinstance
(
i
,
HighLevelILInstruction
)
for
i
in
op
):
for
i
in
op
:
yield
from
i
.
traverse
(
cb
,
*
args
,
shallow
=
shallow
,
**
kwargs
)
# type: ignore
@
deprecation
.
deprecated
(
deprecated_in
=
"4.0.4907"
,
details
=
"Use :py:func:`HighLevelILInstruction.traverse` instead."
)
def
visit_all
(
self
,
cb
:
HighLevelILVisitorCallback
,
name
:
str
=
"root"
,
parent
:
Optional
[
'HighLevelILInstruction'
]
=
None
)
->
bool
:
"""
Visits all operands of this instruction and all operands of any sub-instructions.
Using pre-order traversal.
:param HighLevelILVisitorCallback cb: Callback function that takes the name of the operand, the operand, operand type, and parent instruction
:return: True if all instructions were visited, False if the callback returned False
"""
if
cb
(
name
,
self
,
"HighLevelILInstruction"
,
parent
)
==
False
:
return
False
for
name
,
op
,
opType
in
self
.
detailed_operands
:
if
isinstance
(
op
,
HighLevelILInstruction
):
if
not
op
.
visit_all
(
cb
,
name
,
self
):
return
False
elif
isinstance
(
op
,
list
)
and
all
(
isinstance
(
i
,
HighLevelILInstruction
)
for
i
in
op
):
for
i
in
op
:
if
not
i
.
visit_all
(
cb
,
name
,
self
):
# type: ignore
return
False
elif
cb
(
name
,
op
,
opType
,
self
)
==
False
:
return
False
return
True
@
deprecation
.
deprecated
(
deprecated_in
=
"4.0.4907"
,
details
=
"Use :py:func:`HighLevelILInstruction.traverse` instead."
)
def
visit_operands
(
self
,
cb
:
HighLevelILVisitorCallback
,
name
:
str
=
"root"
,
parent
:
Optional
[
'HighLevelILInstruction'
]
=
None
)
->
bool
:
"""
Visits all leaf operands of this instruction and any sub-instructions.
:param HighLevelILVisitorCallback cb: Callback function that takes the name of the operand, the operand, operand type, and parent instruction
:return: True if all instructions were visited, False if the callback returned False
"""
for
name
,
op
,
opType
in
self
.
detailed_operands
:
if
isinstance
(
op
,
HighLevelILInstruction
):
if
not
op
.
visit_operands
(
cb
,
name
,
self
):
return
False
elif
isinstance
(
op
,
list
)
and
all
(
isinstance
(
i
,
HighLevelILInstruction
)
for
i
in
op
):
for
i
in
op
:
if
not
i
.
visit_operands
(
cb
,
name
,
self
):
# type: ignore
return
False
elif
cb
(
name
,
op
,
opType
,
self
)
==
False
:
return
False
return
True
@
deprecation
.
deprecated
(
deprecated_in
=
"4.0.4907"
,
details
=
"Use :py:func:`HighLevelILInstruction.traverse` instead."
)
def
visit
(
self
,
cb
:
HighLevelILVisitorCallback
,
name
:
str
=
"root"
,
parent
:
Optional
[
'HighLevelILInstruction'
]
=
None
)
->
bool
:
"""
Visits all HighLevelILInstructions in the operands of this instruction and any sub-instructions.
In the callback you provide, you likely only need to interact with the second argument (see the example below).
:param HighLevelILVisitorCallback cb: Callback function that takes the name of the operand, the operand, operand type, and parent instruction
:return: True if all instructions were visited, False if the callback returned False
:Example:
>>> def visitor(_a, inst, _c, _d) -> bool:
>>> if isinstance(inst, Constant):
>>> print(f"Found constant: {inst.constant}")
>>> return False # Stop recursion (once we find a constant, don't recurse in to any sub-instructions (which there won't actually be any...))
>>> # Otherwise, keep recursing the subexpressions of this instruction; if no return value is provided, it'll keep descending
>>>
>>> # Finds all constants used in the program
>>> for inst in bv.hlil_instructions:
>>> inst.visit(visitor)
"""
if
cb
(
name
,
self
,
"HighLevelILInstruction"
,
parent
)
==
False
:
return
False
for
name
,
op
,
_
in
self
.
detailed_operands
:
if
isinstance
(
op
,
HighLevelILInstruction
):
if
not
op
.
visit
(
cb
,
name
,
self
):
return
False
elif
isinstance
(
op
,
list
)
and
all
(
isinstance
(
i
,
HighLevelILInstruction
)
for
i
in
op
):
for
i
in
op
:
if
not
i
.
visit
(
cb
,
name
,
self
):
# type: ignore
return
False
return
True
@
property
def
has_side_effects
(
self
)
->
bool
:
return
core
.
BNHighLevelILHasSideEffects
(
self
.
function
.
handle
,
self
.
expr_index
)
def
get_lines
(
self
,
settings
:
Optional
[
'function.DisassemblySettings'
]
=
None
)
->
LinesType
:
"""
Gets HLIL text lines with optional settings
.. note::
The instruction renders in whichever form it is currently in, so one that is not in AST form renders
without its nested body and ignores ``DisassemblyOption`` settings acting on nested bodies (such as
``ShowCollapseIndicators``). Instructions from iterating a function or its basic blocks are not in AST
form; use :py:func:`ast` or :py:func:`HighLevelILFunction.root`. See `AST and Non-AST Forms
<https://docs.binary.ninja/dev/bnil-hlil.html#ast-and-non-ast-forms>`_.
"""
if
settings
is
not
None
:
settings
=
settings
.
handle
count
=
ctypes
.
c_ulonglong
()
lines
=
core
.
BNGetHighLevelILExprText
(
self
.
function
.
handle
,
self
.
expr_index
,
self
.
as_ast
,
count
,
settings
)
assert
lines
is
not
None
,
"core.BNGetHighLevelILExprText returned None"
try
:
for
i
in
range
(
0
,
count
.
value
):
addr
=
lines
[
i
].
addr
if
lines
[
i
].
instrIndex
!=
0xffffffffffffffff
:
il_instr
=
self
.
function
[
lines
[
i
].
instrIndex
]
else
:
il_instr
=
None
color
=
highlight
.
HighlightColor
.
_from_core_struct
(
lines
[
i
].
highlight
)
tokens
=
function
.
InstructionTextToken
.
_from_core_struct
(
lines
[
i
].
tokens
,
lines
[
i
].
count
)
yield
function
.
DisassemblyTextLine
(
tokens
,
addr
,
il_instr
,
color
)
finally
:
core
.
BNFreeDisassemblyTextLines
(
lines
,
count
.
value
)
@
property
def
can_collapse
(
self
)
->
bool
:
"""If this instruction can be collapsed in rendered lines"""
return
self
.
operation
in
[
HighLevelILOperation
.
HLIL_IF
,
HighLevelILOperation
.
HLIL_WHILE
,
HighLevelILOperation
.
HLIL_WHILE_SSA
,
HighLevelILOperation
.
HLIL_DO_WHILE
,
HighLevelILOperation
.
HLIL_DO_WHILE_SSA
,
HighLevelILOperation
.
HLIL_FOR
,
HighLevelILOperation
.
HLIL_FOR_SSA
,
HighLevelILOperation
.
HLIL_SWITCH
,
HighLevelILOperation
.
HLIL_CASE
]
View remainder of file in raw view
Footer
© 2026 GitHub, Inc.
Footer navigation
Terms
Privacy
Security
Status
Community
Docs
Contact
You can’t perform that action at this time.
Back
|
FazBrowse Home
|
New Git URL