Skip to content

Commit ff771fa

Browse files
committed
improve traverse concepts examples
1 parent 903b227 commit ff771fa

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

docs/dev/concepts.md

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,13 @@ t = [
6767
bv.get_symbol_by_raw_name('__builtin_strncpy').address
6868
]
6969

70-
list(current_hlil.traverse(find_strcpy, t))
70+
# Find the first call to a builtin:
71+
for result in current_hlil.traverse(find_strcpy, t):
72+
# Any logic should live here, not inside the callable which is just for
73+
# matching. Because this is a generator, it can fail fast when used for
74+
# search!
75+
print(result)
76+
break
7177

7278

7379
def get_memcpy_data(i, t) -> bytes:
@@ -77,7 +83,8 @@ def get_memcpy_data(i, t) -> bytes:
7783

7884
# Iterate through all instructions in the HLIL
7985
t = bv.get_symbol_by_raw_name('__builtin_memcpy').address
80-
list(current_hlil.traverse(get_memcpy_data, t))
86+
for i in current_hlil.traverse(get_memcpy_data, t):
87+
print(f"Found some memcpy data: {repr(i)}")
8188

8289

8390
# find all the calls to __builtin_strcpy and get their values
@@ -90,13 +97,20 @@ t = [
9097
bv.get_symbol_by_raw_name('__builtin_strcpy').address,
9198
bv.get_symbol_by_raw_name('__builtin_strncpy').address
9299
]
93-
list(current_hlil.traverse(find_strcpy, t))
100+
101+
for i in current_hlil.traverse(find_strcpy, t):
102+
print(i)
94103

95104
# collect the number of parameters for each function call
96105
def param_counter(i) -> int:
97106
match i:
98107
case HighLevelILCall():
99108
return len(i.params)
109+
110+
# Note that the results are a generator and usually anything that is found
111+
# should have processing done outside the callback, but you can always
112+
# convert it to a list like this:
113+
100114
list(current_hlil.traverse(param_counter))
101115

102116

@@ -105,6 +119,7 @@ def collect_call_target(i) -> None:
105119
match i:
106120
case HighLevelILCall(dest=HighLevelILConstPtr(constant=c)):
107121
return c
122+
108123
set([hex(a) for a in current_hlil.traverse(collect_call_target)])
109124

110125

@@ -113,6 +128,7 @@ def collect_this_vars(i) -> Variable:
113128
match i:
114129
case HighLevelILVar(var=v) if v.name == 'this':
115130
return v
131+
116132
list(v for v in current_hlil.traverse(collect_this_vars))
117133

118134
```

docs/img/getcompletion.png

161 KB
Loading

0 commit comments

Comments
 (0)