]>
git.saurik.com Git - apple/xnu.git/blob - tools/lldbmacros/kasan.py
3 from core
.configuration
import *
18 0xf1: 'STACK_LEFT_RZ',
20 0xf3: 'STACK_RIGHT_RZ',
22 0xf8: 'STACK_OOSCOPE',
26 0xfb: 'HEAP_RIGHT_RZ',
32 enable
= kern
.globals.kasan_enabled
37 def shadow_for_address(addr
, shift
):
38 return ((addr
>> 3) + shift
)
40 def address_for_shadow(addr
, shift
):
41 return ((addr
- shift
) << 3)
43 def get_shadow_byte(shadow_addr
):
44 return unsigned(kern
.GetValueFromAddress(shadow_addr
, 'uint8_t *')[0])
47 for (k
,v
) in shadow_strings
.iteritems():
48 print " {:02x}: {}".format(k
,v
)
50 def print_shadow_context(addr
, context
):
51 addr
= shadow_for_address(addr
, shift
)
52 base
= (addr
& ~
0xf) - 16 * context
53 shadow
= kern
.GetValueFromAddress(unsigned(base
), "uint8_t *")
55 print " "*17 + " 0 1 2 3 4 5 6 7 8 9 a b c d e f"
56 for x
in range(0, 2*context
+1):
59 for y
in xrange(x
*16, (x
+1)*16):
64 elif base
+y
+1 == addr
:
67 vals
+= "{}{:02x}{}".format(l
, sh
, r
)
69 print("{:x}:{}".format(base
+ 16*x
, vals
))
72 def print_alloc_free_entry(addr
, orig_ptr
):
73 h
= kern
.GetValueFromAddress(addr
, 'struct freelist_entry *')
74 asz
= unsigned(h
.size
)
75 usz
= unsigned(h
.user_size
)
76 pgsz
= unsigned(kern
.globals.page_size
)
80 if str(zone
.zone_name
).startswith("fakestack"):
81 alloc_type
= "fakestack"
85 leftrz
= unsigned(zone
.kasan_redzone
)
88 if asz
- usz
>= 2*pgsz
:
91 leftrz
= kasan_guard_size
93 rightrz
= asz
- usz
- leftrz
95 print "Freed {} object".format(alloc_type
)
96 print "Valid range: 0x{:x} -- 0x{:x} ({} bytes)".format(addr
+ leftrz
, addr
+ leftrz
+ usz
- 1, usz
)
97 print "Total range: 0x{:x} -- 0x{:x} ({} bytes)".format(addr
, addr
+ asz
- 1, asz
)
98 print "Offset: {} bytes".format(orig_ptr
- addr
- leftrz
)
99 print "Redzone: {} / {} bytes".format(leftrz
, rightrz
)
101 print "Zone: 0x{:x} <{:s}>".format(unsigned(zone
), zone
.zone_name
)
103 btframes
= unsigned(h
.frames
)
106 print "Free site backtrace ({} frames):".format(btframes
)
107 for i
in xrange(0, btframes
):
108 fr
= unsigned(kern
.globals.vm_kernel_slid_base
) + unsigned(h
.backtrace
[i
])
109 print " #{:}: {}".format(btframes
-i
-1, GetSourceInformationForAddress(fr
))
112 print_hexdump(addr
, asz
, 0)
116 def print_alloc_info(_addr
):
117 addr
= (_addr
& ~
0x7)
119 _shp
= shadow_for_address(_addr
, shift
)
120 _shbyte
= get_shadow_byte(_shp
)
121 _shstr
= shadow_byte_to_string(_shbyte
)
123 # If we're in a left redzone, scan to the start of the real allocation, where
124 # the header should live
126 while shbyte
== 0xfa:
128 shbyte
= get_shadow_byte(shadow_for_address(addr
, shift
))
130 # Search backwards for an allocation
132 while searchbytes
< 8*4096:
134 shp
= shadow_for_address(addr
, shift
)
135 shbyte
= get_shadow_byte(shp
)
136 shstr
= shadow_byte_to_string(shbyte
)
138 headerp
= addr
- alloc_header_sz
139 liveh
= kern
.GetValueFromAddress(headerp
, 'struct kasan_alloc_header *')
140 freeh
= kern
.GetValueFromAddress(addr
, 'struct freelist_entry *')
142 # heap allocations should only ever have these shadow values
143 if shbyte
not in (0,1,2,3,4,5,6,7, 0xfa, 0xfb, 0xfd, 0xf5):
144 print "No allocation found at 0x{:x} (found shadow {:x})".format(_addr
, shbyte
)
147 live_magic
= (addr
& 0xffffffff) ^
0xA110C8ED
148 free_magic
= (addr
& 0xffffffff) ^
0xF23333D
150 if live_magic
== unsigned(liveh
.magic
):
151 usz
= unsigned(liveh
.user_size
)
152 asz
= unsigned(liveh
.alloc_size
)
153 leftrz
= unsigned(liveh
.left_rz
)
154 base
= headerp
+ alloc_header_sz
- leftrz
156 if _addr
>= base
and _addr
< base
+ asz
:
157 footer
= kern
.GetValueFromAddress(addr
+ usz
, 'struct kasan_alloc_footer *')
158 rightrz
= asz
- usz
- leftrz
160 print "Live heap object"
161 print "Valid range: 0x{:x} -- 0x{:x} ({} bytes)".format(addr
, addr
+ usz
- 1, usz
)
162 print "Total range: 0x{:x} -- 0x{:x} ({} bytes)".format(base
, base
+ asz
- 1, asz
)
163 print "Offset: {} bytes (shadow: 0x{:02x} {})".format(_addr
- addr
, _shbyte
, _shstr
)
164 print "Redzone: {} / {} bytes".format(leftrz
, rightrz
)
166 btframes
= unsigned(liveh
.frames
)
168 print "Alloc site backtrace ({} frames):".format(btframes
)
169 for i
in xrange(0, btframes
):
170 fr
= unsigned(kern
.globals.vm_kernel_slid_base
) + unsigned(footer
.backtrace
[i
])
171 print " #{:}: {}".format(btframes
-i
-1, GetSourceInformationForAddress(fr
))
174 print_hexdump(base
, asz
, 0)
177 elif free_magic
== unsigned(freeh
.magic
):
178 asz
= unsigned(freeh
.size
)
179 if _addr
>= addr
and _addr
< addr
+ asz
:
180 print_alloc_free_entry(addr
, _addr
)
186 print "No allocation found at 0x{:x}".format(_addr
)
188 def shadow_byte_to_string(sb
):
189 return shadow_strings
.get(sb
, '??')
191 def print_whatis(_addr
, ctx
):
199 shbyte
= get_shadow_byte(shadow_for_address(addr
, shift
))
202 if shbyte
in [0xfa, 0xfb, 0xfd, 0xf5]:
203 print_alloc_info(_addr
)
206 if shbyte
not in [0,1,2,3,4,5,6,7,0xf8]:
207 print "Poisoned memory, shadow {:x} [{}]".format(shbyte
, shadow_byte_to_string(shbyte
))
211 extra
= "Out-of-scope"
213 # look for the base of the object
214 while shbyte
in [0,1,2,3,4,5,6,7,0xf8]:
220 shbyte
= get_shadow_byte(shadow_for_address(addr
, shift
))
223 print "No object found"
228 # If we did not find a left/mid redzone, we aren't in an object
229 if leftrz
not in [0xf1, 0xf2, 0xfa, 0xf9]:
230 print "No object found"
233 # now size the object
234 addr
= (_addr
& ~
0x7) + 8
235 shbyte
= get_shadow_byte(shadow_for_address(addr
, shift
))
236 while shbyte
in [0,1,2,3,4,5,6,7,0xf8]:
242 shbyte
= get_shadow_byte(shadow_for_address(addr
, shift
))
245 print "No object found"
249 # work out the type of the object from its redzone
251 if leftrz
== 0xf1 or leftrz
== 0xf2:
253 elif leftrz
== 0xf9 and rightrz
== 0xf9:
255 elif leftrz
== 0xfa and rightrz
== 0xfb:
256 print_alloc_info(_addr
)
259 print "{} {} object".format(extra
, objtype
)
260 print "Valid range: 0x{:x} -- 0x{:x} ({} bytes)".format(base
, base
+total_size
-1, total_size
)
261 print "Offset: {} bytes".format(_addr
- base
)
263 print_hexdump(base
, total_size
, 0)
265 def print_hexdump(base
, size
, ctx
):
266 start
= base
- 16*ctx
268 size
= min(size
+ 16*2*ctx
, 256)
271 data_array
= kern
.GetValueFromAddress(start
, "uint8_t *")
272 print_hex_data(data_array
[0:size
], start
, "Hexdump")
276 def kasan_subcommand(cmd
, args
, opts
):
279 addr
= long(args
[0], 0)
281 if cmd
in ['a2s', 'toshadow', 'fromaddr', 'fromaddress']:
282 print "0x{:016x}".format(shadow_for_address(addr
, shift
))
283 elif cmd
in ['s2a', 'toaddr', 'toaddress', 'fromshadow']:
284 print "0x{:016x}".format(address_for_shadow(addr
, shift
))
285 elif cmd
== 'shadow':
286 shadow
= shadow_for_address(addr
, shift
)
287 sb
= get_shadow_byte(shadow
)
288 print("0x{:02x} @ 0x{:016x} [{}]\n\n".format(sb
, shadow
, shadow_byte_to_string(sb
)))
289 ctx
= long(opts
.get("-C", 5))
290 print_shadow_context(addr
, ctx
)
291 elif cmd
== 'legend':
294 pages_used
= unsigned(kern
.globals.shadow_pages_used
)
295 pages_total
= unsigned(kern
.globals.shadow_pages_total
)
296 nkexts
= unsigned(kern
.globals.kexts_loaded
)
297 print "Offset: 0x{:016x}".format(shift
)
298 print "Shadow used: {} / {} ({:.1f}%)".format(pages_used
, pages_total
, 100.0*pages_used
/pages_total
)
299 print "Kexts loaded: {}".format(nkexts
)
300 elif cmd
== 'whatis':
301 ctx
= long(opts
.get("-C", 1))
302 print_whatis(addr
, ctx
)
303 elif cmd
== 'alloc' or cmd
== 'heap':
304 print_alloc_info(addr
)
306 @lldb_command('kasan', 'C:')
307 def Kasan(cmd_args
=None, cmd_options
={}):
308 """kasan <cmd> [opts..]
312 info basic KASan information
313 shadow <addr> print shadow around 'addr'
314 heap <addr> show info about heap object at 'addr'
315 whatis <addr> print whatever KASan knows about address
316 toshadow <addr> convert address to shadow pointer
317 toaddr <shdw> convert shadow pointer to address
318 legend print a shadow byte table
320 -C <num> : num lines of context to show"""
322 if not is_kasan_build():
323 print "KASan not enabled in build"
326 if len(cmd_args
) == 0:
331 shift
= unsigned(kern
.globals.__asan
_shadow
_memory
_dynamic
_address
)
333 # Since the VM is not aware of the KASan shadow mapping, accesses to it will
334 # fail. Setting kdp_read_io=1 avoids this check.
335 if GetConnectionProtocol() == "kdp" and unsigned(kern
.globals.kdp_read_io
) == 0:
336 print "Setting kdp_read_io=1 to allow KASan shadow reads"
337 if sizeof(kern
.globals.kdp_read_io
) == 4:
338 WriteInt32ToMemoryAddress(1, addressof(kern
.globals.kdp_read_io
))
339 elif sizeof(kern
.globals.kdp_read_io
) == 8:
340 WriteInt64ToMemoryAddress(1, addressof(kern
.globals.kdp_read_io
))
341 readio
= unsigned(kern
.globals.kdp_read_io
)
344 return kasan_subcommand(cmd_args
[0], cmd_args
[1:], cmd_options
)