]>
git.saurik.com Git - apple/dyld.git/blob - testing/kernel-cache-tests/auxkc-vtable-patching-arm64e/test.py
4 import KernelCollection
6 # This verifies that a kext can bind to another kext
7 # foo.kext exports foo and bar.kext uses it
8 # We put foo.kext in the base KC so that the patch slot in bar.kext has to know to use the correct fixup level in the fixup chain
10 def check(kernel_cache
):
11 kernel_cache
.buildKernelCollection("arm64e", "/auxkc-vtable-patching-arm64e/main.kc", "/auxkc-vtable-patching-arm64e/main.kernel", "/auxkc-vtable-patching-arm64e/extensions", ["com.apple.foo"], [])
12 kernel_cache
.analyze("/auxkc-vtable-patching-arm64e/main.kc", ["-layout", "-arch", "arm64e"])
14 assert len(kernel_cache
.dictionary()["dylibs"]) == 2
15 assert kernel_cache
.dictionary()["dylibs"][0]["name"] == "com.apple.kernel"
16 assert kernel_cache
.dictionary()["dylibs"][1]["name"] == "com.apple.foo"
18 # Get the addresses for the symbols we are looking at. This will make it easier to work out the fixup slots
19 kernel_cache
.analyze("/auxkc-vtable-patching-arm64e/main.kc", ["-symbols", "-arch", "arm64e"])
21 # From foo, we want to know where the vtable is, and the foo() and fooUsed0() slots in that vtable
23 assert kernel_cache
.dictionary()["dylibs"][1]["global-symbols"][6]["name"] == "__ZN3Foo3fooEv"
24 assert kernel_cache
.dictionary()["dylibs"][1]["global-symbols"][6]["vmAddr"] == "0x20408"
26 assert kernel_cache
.dictionary()["dylibs"][1]["global-symbols"][7]["name"] == "__ZN3Foo8fooUsed0Ev"
27 assert kernel_cache
.dictionary()["dylibs"][1]["global-symbols"][7]["vmAddr"] == "0x20420"
31 kernel_cache
.analyze("/auxkc-vtable-patching-arm64e/main.kc", ["-fixups", "-arch", "arm64e"])
33 # In vtable for Foo, we match the entry for Foo::foo() by looking for its value on the RHS of the fixup
34 assert kernel_cache
.dictionary()["fixups"]["0x200F0"] == "kc(0) + 0x20408 auth(IA addr 49764)"
35 # Then the following fixup should be to Foo::fooUsed0()
36 assert kernel_cache
.dictionary()["fixups"]["0x200F8"] == "kc(0) + 0x20420 auth(IA addr 61962)"
39 # -----------------------------------------------------------
40 # Now build an aux cache using the baseline kernel collection
41 kernel_cache
.buildAuxKernelCollection("arm64e", "/auxkc-vtable-patching-arm64e/aux.kc", "/auxkc-vtable-patching-arm64e/main.kc", "", "/auxkc-vtable-patching-arm64e/extensions", ["com.apple.bar"], [])
42 kernel_cache
.analyze("/auxkc-vtable-patching-arm64e/aux.kc", ["-layout", "-arch", "arm64e"])
44 assert len(kernel_cache
.dictionary()["dylibs"]) == 1
45 assert kernel_cache
.dictionary()["dylibs"][0]["name"] == "com.apple.bar"
48 # Get the addresses for the symbols we are looking at. This will make it easier to work out the fixup slots
49 kernel_cache
.analyze("/auxkc-vtable-patching-arm64e/aux.kc", ["-symbols", "-arch", "arm64e"])
51 # From bar, find the vtable and its override of foo()
53 assert kernel_cache
.dictionary()["dylibs"][0]["global-symbols"][3]["name"] == "__ZN3Bar3fooEv"
54 assert kernel_cache
.dictionary()["dylibs"][0]["global-symbols"][3]["vmAddr"] == "0x1036C"
58 kernel_cache
.analyze("/auxkc-vtable-patching-arm64e/aux.kc", ["-fixups", "-arch", "arm64e"])
60 # Now in bar, again match the entry for its Bar::foo() symbol
61 assert kernel_cache
.dictionary()["fixups"]["0x140E8"] == "kc(3) + 0x1036C auth(IA addr 49764)"
62 # And if the patching was correct, then following entry should be to Foo::fooUsed0()
63 assert kernel_cache
.dictionary()["fixups"]["0x140F0"] == "kc(0) + 0x10420 auth(IA addr 61962)"
66 # [~]> xcrun -sdk iphoneos.internal cc -arch arm64e -Wl,-static -mkernel -nostdlib -Wl,-e,__start -Wl,-pie main.cpp -Wl,-pagezero_size,0x0 -o main.kernel -Wl,-image_base,0x10000 -Wl,-segaddr,__HIB,0x4000 -Wl,-add_split_seg_info -Wl,-install_name,/usr/lib/swift/split.seg.v2.hack -iwithsysroot /System/Library/Frameworks/Kernel.framework/Headers -Wl,-sectcreate,__LINKINFO,__symbolsets,SymbolSets.plist -Wl,-segprot,__LINKINFO,r--,r-- -Wl,-fixup_chains -Wl,-kernel
67 # [~]> xcrun -sdk iphoneos.internal cc -arch arm64e -Wl,-kext -mkernel -nostdlib -Wl,-add_split_seg_info -Wl,-no_data_const foo.cpp -o extensions/foo.kext/foo -iwithsysroot /System/Library/Frameworks/Kernel.framework/Headers -DFOO_USED=1
68 # [~]> xcrun -sdk iphoneos.internal cc -arch arm64e -Wl,-kext -mkernel -nostdlib -Wl,-add_split_seg_info -Wl,-data_const bar.cpp -o extensions/bar.kext/bar -iwithsysroot /System/Library/Frameworks/Kernel.framework/Headers
69 # [~]> rm -r extensions/*.kext/*.ld