dyld-832.7.1.tar.gz
[apple/dyld.git] / testing / kernel-cache-tests / pageablekc-vtable-patching / test.py
1 #!/usr/bin/python2.7
2
3 import os
4 import KernelCollection
5
6 # This verifies that a kext can patch vtables against another kext
7 # 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
8
9 def check(kernel_cache):
10 kernel_cache.buildKernelCollection("x86_64", "/pageablekc-vtable-patching/main.kc", "/pageablekc-vtable-patching/main.kernel", "/pageablekc-vtable-patching/extensions", ["com.apple.foo"], [])
11 kernel_cache.analyze("/pageablekc-vtable-patching/main.kc", ["-layout", "-arch", "x86_64"])
12
13 assert len(kernel_cache.dictionary()["dylibs"]) == 2
14 assert kernel_cache.dictionary()["dylibs"][0]["name"] == "com.apple.kernel"
15 assert kernel_cache.dictionary()["dylibs"][1]["name"] == "com.apple.foo"
16
17 # Get the addresses for the symbols we are looking at. This will make it easier to work out the fixup slots
18 kernel_cache.analyze("/pageablekc-vtable-patching/main.kc", ["-symbols", "-arch", "x86_64"])
19
20 # From foo, we want to know where the vtable is, and the foo() and fooUsed0() slots in that vtable
21 # Foo::foo()
22 assert kernel_cache.dictionary()["dylibs"][1]["global-symbols"][6]["name"] == "__ZN3Foo3fooEv"
23 assert kernel_cache.dictionary()["dylibs"][1]["global-symbols"][6]["vmAddr"] == "0x16ED0"
24 # Foo::fooUsed0()
25 assert kernel_cache.dictionary()["dylibs"][1]["global-symbols"][7]["name"] == "__ZN3Foo8fooUsed0Ev"
26 assert kernel_cache.dictionary()["dylibs"][1]["global-symbols"][7]["vmAddr"] == "0x16EF0"
27
28
29 # Check the fixups
30 kernel_cache.analyze("/pageablekc-vtable-patching/main.kc", ["-fixups", "-arch", "x86_64"])
31
32 # In vtable for Foo, we match the entry for Foo::foo() by looking for its value on the RHS of the fixup
33 assert kernel_cache.dictionary()["fixups"]["0x1D150"] == "kc(0) + 0x16ED0"
34 # Then the following fixup should be to Foo::fooUsed0()
35 assert kernel_cache.dictionary()["fixups"]["0x1D158"] == "kc(0) + 0x16EF0"
36
37
38 # -----------------------------------------------------------
39 # Now build an pageable cache using the baseline kernel collection
40 kernel_cache.buildPageableKernelCollection("x86_64", "/pageablekc-vtable-patching/pageable.kc", "/pageablekc-vtable-patching/main.kc", "/pageablekc-vtable-patching/extensions", ["com.apple.bar"], [])
41 kernel_cache.analyze("/pageablekc-vtable-patching/pageable.kc", ["-layout", "-arch", "x86_64"])
42
43 assert len(kernel_cache.dictionary()["dylibs"]) == 1
44 assert kernel_cache.dictionary()["dylibs"][0]["name"] == "com.apple.bar"
45
46
47 # Get the addresses for the symbols we are looking at. This will make it easier to work out the fixup slots
48 kernel_cache.analyze("/pageablekc-vtable-patching/pageable.kc", ["-symbols", "-arch", "x86_64"])
49
50 # From bar, find the vtable and its override of foo()
51 # Bar::foo()
52 assert kernel_cache.dictionary()["dylibs"][0]["global-symbols"][3]["name"] == "__ZN3Bar3fooEv"
53 assert kernel_cache.dictionary()["dylibs"][0]["global-symbols"][3]["vmAddr"] == "0x4F10"
54
55
56 # Check the fixups
57 kernel_cache.analyze("/pageablekc-vtable-patching/pageable.kc", ["-fixups", "-arch", "x86_64"])
58
59 # In bar, again match the entry for its Bar::foo() symbol
60 assert kernel_cache.dictionary()["dylibs"][0]["fixups"]["0x8150"] == "kc(1) + 0x4F10"
61 # And if the patching was correct, then following entry should be to Foo::fooUsed0()
62 assert kernel_cache.dictionary()["dylibs"][0]["fixups"]["0x8158"] == "kc(0) + 0x12EF0"
63
64
65 # [~]> xcrun -sdk macosx.internal cc -arch x86_64 -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--
66 # [~]> xcrun -sdk macosx.internal cc -arch x86_64 -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
67 # [~]> xcrun -sdk macosx.internal cc -arch x86_64 -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
68 # [~]> rm -r extensions/*.kext/*.ld
69