]>
git.saurik.com Git - apple/dyld.git/blob - testing/kernel-cache-tests/kernel-vtable-patching-arm64e/test.py
4 import KernelCollection
6 # This tests verifies that the vtable in bar.kext is patched
7 # But also that this can be done against a subclass in the kernel, not just
9 # Note this is the same as kernel-vtable-patching but with a large base address and chained fixups
11 def findGlobalSymbolVMAddr(kernel_cache
, dylib_index
, symbol_name
):
12 for symbol_and_addr
in kernel_cache
.dictionary()["dylibs"][dylib_index
]["global-symbols"]:
13 if symbol_and_addr
["name"] == symbol_name
:
14 return symbol_and_addr
["vmAddr"]
17 def findFixupVMAddr(kernel_cache
, fixup_name
):
18 for fixup_vmaddr
, fixup_target
in kernel_cache
.dictionary()["fixups"].iteritems():
19 if fixup_target
== fixup_name
:
23 def offsetVMAddr(vmAddr
, offset
):
24 het_int
= int(vmAddr
, 16)
25 het_int
= het_int
+ offset
26 return ''.join([ '0x', hex(het_int
).upper()[2:] ])
28 def check(kernel_cache
):
30 kernel_cache
.buildKernelCollection("arm64e", "/kernel-vtable-patching-arm64e/main.kc", "/kernel-vtable-patching-arm64e/main.kernel", "/kernel-vtable-patching-arm64e/extensions", ["com.apple.bar"], [])
31 kernel_cache
.analyze("/kernel-vtable-patching-arm64e/main.kc", ["-layout", "-arch", "arm64e"])
33 assert len(kernel_cache
.dictionary()["dylibs"]) == 2
34 assert kernel_cache
.dictionary()["dylibs"][0]["name"] == "com.apple.kernel"
35 assert kernel_cache
.dictionary()["dylibs"][1]["name"] == "com.apple.bar"
37 # Get the addresses for the symbols we are looking at. This will make it easier to work out the fixup slots
38 kernel_cache
.analyze("/kernel-vtable-patching-arm64e/main.kc", ["-symbols", "-arch", "arm64e"])
40 # From foo, we want to know where the vtable is, and the foo() and fooUsed0() slots in that vtable
42 fooClassFooVMAddr
= findGlobalSymbolVMAddr(kernel_cache
, 0, "__ZN3Foo3fooEv")
44 print "fooClassFooVMAddr: " + fooClassFooVMAddr
47 fooClassUsed0VMAddr
= findGlobalSymbolVMAddr(kernel_cache
, 0, "__ZN3Foo8fooUsed0Ev")
49 print "fooClassUsed0VMAddr: " + fooClassUsed0VMAddr
51 # From bar, find the vtable and its override of foo()
53 barClassFooVMAddr
= findGlobalSymbolVMAddr(kernel_cache
, 1, "__ZN3Bar3fooEv")
55 print "barClassFooVMAddr: " + barClassFooVMAddr
59 kernel_cache
.analyze("/kernel-vtable-patching-arm64e/main.kc", ["-fixups", "-arch", "arm64e"])
62 # In vtable for Foo, we match the entry for Foo::foo() by looking for its value on the RHS of the fixup
63 fooFooFixupAddr
= findFixupVMAddr(kernel_cache
, "kc(0) + " + fooClassFooVMAddr
+ " auth(IA addr 49764)")
65 print "fooFooFixupAddr: " + fooFooFixupAddr
66 # Then the following fixup should be to Foo::fooUsed0()
67 fooFooNextFixupAddr
= offsetVMAddr(fooFooFixupAddr
, 8)
69 print "fooFooNextFixupAddr: " + fooFooNextFixupAddr
70 assert kernel_cache
.dictionary()["fixups"][fooFooNextFixupAddr
] == "kc(0) + " + fooClassUsed0VMAddr
+ " auth(IA addr 61962)"
73 # Now in bar, again match the entry for its Bar::foo() symbol
74 barFooFixupAddr
= findFixupVMAddr(kernel_cache
, "kc(0) + " + barClassFooVMAddr
+ " auth(IA addr 49764)")
76 print "barFooFixupAddr: " + barFooFixupAddr
77 # And if the patching was correct, then following entry should be to Foo::fooUsed0()
78 barFooNextFixupAddr
= offsetVMAddr(barFooFixupAddr
, 8)
80 print "barFooNextFixupAddr: " + barFooNextFixupAddr
81 assert kernel_cache
.dictionary()["fixups"][barFooNextFixupAddr
] == "kc(0) + " + fooClassUsed0VMAddr
+ " auth(IA addr 61962)"
83 # [~]> xcrun -sdk iphoneos.internal cc -arch arm64e -Wl,-static -mkernel -nostdlib -Wl,-add_split_seg_info -Wl,-e,__start -Wl,-pie main.cpp foo.cpp -Wl,-pagezero_size,0x0 -Wl,-rename_section,__TEXT,__text,__TEXT_EXEC,__text -o main.kernel -Wl,-install_name,/usr/lib/swift/split.seg.v2.hack -Wl,-image_base,0xfffffff000000000 -iwithsysroot /System/Library/Frameworks/Kernel.framework/Headers -Wl,-sectcreate,__LINKINFO,__symbolsets,SymbolSets.plist -Wl,-segprot,__LINKINFO,r--,r-- -DFOO_USED=1 -Wl,-kernel -Wl,-fixup_chains
84 # [~]> xcrun -sdk iphoneos.internal cc -arch arm64e -Wl,-kext -mkernel -nostdlib -Wl,-add_split_seg_info -Wl,-no_data_const bar.cpp -o extensions/bar.kext/bar -iwithsysroot /System/Library/Frameworks/Kernel.framework/Headers -Wl,-fixup_chains
85 # [~]> rm -r extensions/*.kext/*.ld