dyld-832.7.1.tar.gz
[apple/dyld.git] / testing / kernel-cache-tests / kext-vtable-patching-overrides / test.py
1 #!/usr/bin/python2.7
2
3 import os
4 import KernelCollection
5
6 # This verifies that a kext can bind to another kext
7 # foo.kext exports foo and bar.kext uses it
8 # This is the same as kext-vtable-patching but checks that overrides of methods in parent classes
9 # are propagated to child classes
10 # Foo defines fooOverride().
11
12 def check(kernel_cache):
13 kernel_cache.buildKernelCollection("x86_64", "/kext-vtable-patching-overrides/main.kc", "/kext-vtable-patching-overrides/main.kernel", "/kext-vtable-patching-overrides/extensions", ["com.apple.foo", "com.apple.bar"], [])
14 kernel_cache.analyze("/kext-vtable-patching-overrides/main.kc", ["-layout", "-arch", "x86_64"])
15
16 assert len(kernel_cache.dictionary()["dylibs"]) == 3
17 assert kernel_cache.dictionary()["dylibs"][0]["name"] == "com.apple.kernel"
18 assert kernel_cache.dictionary()["dylibs"][1]["name"] == "com.apple.bar"
19 assert kernel_cache.dictionary()["dylibs"][2]["name"] == "com.apple.foo"
20
21 # Get the addresses for the symbols we are looking at. This will make it easier to work out the fixup slots
22 kernel_cache.analyze("/kext-vtable-patching-overrides/main.kc", ["-symbols", "-arch", "x86_64"])
23
24 # From bar, find the vtable and its override of foo()
25 # Bar::foo()
26 assert kernel_cache.dictionary()["dylibs"][1]["global-symbols"][3]["name"] == "__ZN3Bar3fooEv"
27 assert kernel_cache.dictionary()["dylibs"][1]["global-symbols"][3]["vmAddr"] == "0x24F10"
28
29 # From foo, we want to know where the vtable is, and the foo() and fooUsed0() slots in that vtable
30 # Foo::fooOverride()
31 assert kernel_cache.dictionary()["dylibs"][2]["global-symbols"][3]["name"] == "__ZN3Foo11fooOverrideEv"
32 assert kernel_cache.dictionary()["dylibs"][2]["global-symbols"][3]["vmAddr"] == "0x26BA0"
33 # Foo::foo()
34 assert kernel_cache.dictionary()["dylibs"][2]["global-symbols"][7]["name"] == "__ZN3Foo3fooEv"
35 assert kernel_cache.dictionary()["dylibs"][2]["global-symbols"][7]["vmAddr"] == "0x26B80"
36 # Foo::fooUsed0()
37 assert kernel_cache.dictionary()["dylibs"][2]["global-symbols"][8]["name"] == "__ZN3Foo8fooUsed0Ev"
38 assert kernel_cache.dictionary()["dylibs"][2]["global-symbols"][8]["vmAddr"] == "0x26BC0"
39
40 # From foo
41 # FooSub::fooOverride()
42 assert kernel_cache.dictionary()["dylibs"][2]["global-symbols"][21]["name"] == "__ZN6FooSub11fooOverrideEv"
43 assert kernel_cache.dictionary()["dylibs"][2]["global-symbols"][21]["vmAddr"] == "0x26EA0"
44 # Foo::foo()
45 assert kernel_cache.dictionary()["dylibs"][2]["global-symbols"][22]["name"] == "__ZN6FooSub3fooEv"
46 assert kernel_cache.dictionary()["dylibs"][2]["global-symbols"][22]["vmAddr"] == "0x26E80"
47
48
49 # Check the fixups
50 kernel_cache.analyze("/kext-vtable-patching-overrides/main.kc", ["-fixups", "-arch", "x86_64"])
51
52 # In vtable for Foo, we match the entry for Foo::foo() by looking for its value on the RHS of the fixup
53 assert kernel_cache.dictionary()["fixups"]["0x23150"] == "kc(0) + 0x26B80"
54 # Then the following fixup should be to Foo::fooOverride()
55 assert kernel_cache.dictionary()["fixups"]["0x23158"] == "kc(0) + 0x26BA0"
56 # Then the following fixup should be to Foo::fooUsed0()
57 assert kernel_cache.dictionary()["fixups"]["0x23160"] == "kc(0) + 0x26BC0"
58
59 # In vtable for FooSub, we match the entry for FooSub::foo() by looking for its value on the RHS of the fixup
60 assert kernel_cache.dictionary()["fixups"]["0x233A8"] == "kc(0) + 0x26E80"
61 # Then the following fixup should be to FooSub::fooOverride()
62 assert kernel_cache.dictionary()["fixups"]["0x233B0"] == "kc(0) + 0x26EA0"
63 # Then the following fixup should be to Foo::fooUsed0()
64 assert kernel_cache.dictionary()["fixups"]["0x233B8"] == "kc(0) + 0x26BC0"
65
66 # Now in bar, again match the entry for its Bar::foo() symbol
67 assert kernel_cache.dictionary()["fixups"]["0x21150"] == "kc(0) + 0x24F10"
68 # Then the following fixup should be to FooSub::fooOverride()
69 assert kernel_cache.dictionary()["fixups"]["0x21158"] == "kc(0) + 0x26EA0"
70 # And if the patching was correct, then following entry should be to Foo::fooUsed0()
71 assert kernel_cache.dictionary()["fixups"]["0x21160"] == "kc(0) + 0x26BC0"
72
73
74 # [~]> 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--
75 # [~]> xcrun -sdk macosx.internal cc -arch x86_64 -Wl,-kext -mkernel -nostdlib -Wl,-no_data_const foo.cpp -o extensions/foo.kext/foo -iwithsysroot /System/Library/Frameworks/Kernel.framework/Headers -DFOO_USED=1 -DFOO_OVERRIDE=1
76 # [~]> xcrun -sdk macosx.internal cc -arch x86_64 -Wl,-kext -mkernel -nostdlib -Wl,-no_data_const bar.cpp -o extensions/bar.kext/bar -iwithsysroot /System/Library/Frameworks/Kernel.framework/Headers
77 # [~]> rm -r extensions/*.kext/*.ld
78