diff options
author | comex | 2015-01-25 21:56:22 -0500 |
---|---|---|
committer | comex | 2015-01-25 22:01:25 -0500 |
commit | 1b5ec6c220f3ea457077a17f4cf6003502a06bb0 (patch) | |
tree | 19f8594402414d15b94c237d0f99e6efb6ac415e | |
parent | this is dumb (diff) | |
download | substitute-1b5ec6c220f3ea457077a17f4cf6003502a06bb0.tar.gz |
right, fix cleanup. (compared to the old system, this avoids busywaiting, and properly frees the thread resources)
-rw-r--r-- | generated/darwin-inject-asm.S | 8 | ||||
-rw-r--r-- | lib/darwin/inject-asm-raw.c | 36 | ||||
-rw-r--r-- | lib/darwin/inject.c | 19 | ||||
-rw-r--r-- | test/injected-test-dylib.c | 26 | ||||
-rw-r--r-- | test/test-inject.c | 13 |
5 files changed, 89 insertions, 13 deletions
diff --git a/generated/darwin-inject-asm.S b/generated/darwin-inject-asm.S index bf4914a..34691f1 100644 --- a/generated/darwin-inject-asm.S +++ b/generated/darwin-inject-asm.S @@ -13,16 +13,16 @@ _inject_page_start: .align 2 .globl _inject_start_x86_64 _inject_start_x86_64: -.byte 0x55, 0x48, 0x89, 0xe5, 0x53, 0x50, 0x48, 0x89, 0xfb, 0x48, 0x8d, 0x15, 0x2c, 0x00, 0x00, 0x00, 0x48, 0x8d, 0x7d, 0xf4, 0x31, 0xf6, 0x48, 0x89, 0xd9, 0xff, 0x13, 0x48, 0x8b, 0x4b, 0x28, 0xb8, 0x69, 0x01, 0x00, 0x02, 0x31, 0xff, 0x31, 0xf6, 0x31, 0xd2, 0x0f, 0x05, 0xb9, 0xad, 0x0b, 0x00, 0x00, 0x31, 0xc0, 0xff, 0xd1, 0x48, 0x83, 0xc4, 0x08, 0x5b, 0x5d, 0xc3, 0x55, 0x48, 0x89, 0xe5, 0x53, 0x50, 0x48, 0x89, 0xfb, 0x48, 0x8b, 0x7b, 0x20, 0x31, 0xf6, 0xff, 0x53, 0x08, 0x48, 0x85, 0xc0, 0x74, 0x1c, 0x48, 0x8d, 0x35, 0x46, 0x00, 0x00, 0x00, 0x48, 0x89, 0xc7, 0xff, 0x53, 0x10, 0x48, 0x85, 0xc0, 0x74, 0x0a, 0x48, 0x8d, 0x7b, 0x38, 0x48, 0x8b, 0x73, 0x30, 0xff, 0xd0, 0xb8, 0x24, 0x00, 0x00, 0x01, 0x31, 0xd2, 0x31, 0xc9, 0x48, 0x8b, 0x7b, 0x28, 0x31, 0xf6, 0x0f, 0x05, 0x48, 0x8b, 0x43, 0x18, 0x48, 0x81, 0xe3, 0x00, 0xf0, 0xff, 0xff, 0xbe, 0x00, 0x20, 0x00, 0x00, 0x48, 0x89, 0xdf, 0x48, 0x83, 0xc4, 0x08, 0x5b, 0x5d, 0xff, 0xe0, 0x0f, 0x1f, 0x44, 0x00, 0x00, 0x73, 0x75, 0x62, 0x73, 0x74, 0x69, 0x74, 0x75, 0x74, 0x65, 0x5f, 0x69, 0x6e, 0x69, 0x74, 0x00 +.byte 0x55, 0x48, 0x89, 0xe5, 0x53, 0x50, 0x48, 0x89, 0xfb, 0xc7, 0x45, 0xf4, 0x00, 0x00, 0x00, 0x00, 0x48, 0x8d, 0x15, 0x32, 0x00, 0x00, 0x00, 0x48, 0x8d, 0x7d, 0xf4, 0x31, 0xf6, 0x48, 0x89, 0xd9, 0xff, 0x13, 0x8b, 0x7d, 0xf4, 0xff, 0x53, 0x08, 0x48, 0x8b, 0x4b, 0x30, 0xb8, 0x69, 0x01, 0x00, 0x02, 0x31, 0xff, 0x31, 0xf6, 0x31, 0xd2, 0x0f, 0x05, 0xb9, 0xad, 0x0b, 0x00, 0x00, 0x31, 0xc0, 0xff, 0xd1, 0x48, 0x83, 0xc4, 0x08, 0x5b, 0x5d, 0xc3, 0x55, 0x48, 0x89, 0xe5, 0x53, 0x50, 0x48, 0x89, 0xfb, 0x48, 0x8b, 0x7b, 0x28, 0x31, 0xf6, 0xff, 0x53, 0x10, 0x48, 0x85, 0xc0, 0x74, 0x1c, 0x48, 0x8d, 0x35, 0x49, 0x00, 0x00, 0x00, 0x48, 0x89, 0xc7, 0xff, 0x53, 0x18, 0x48, 0x85, 0xc0, 0x74, 0x0a, 0x48, 0x8d, 0x7b, 0x40, 0x48, 0x8b, 0x73, 0x38, 0xff, 0xd0, 0xb8, 0x24, 0x00, 0x00, 0x01, 0x31, 0xd2, 0x31, 0xc9, 0x48, 0x8b, 0x7b, 0x30, 0x31, 0xf6, 0x0f, 0x05, 0x48, 0x8b, 0x43, 0x20, 0x48, 0x81, 0xe3, 0x00, 0xf0, 0xff, 0xff, 0xbe, 0x00, 0x20, 0x00, 0x00, 0x48, 0x89, 0xdf, 0x48, 0x83, 0xc4, 0x08, 0x5b, 0x5d, 0xff, 0xe0, 0x0f, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00, 0x73, 0x75, 0x62, 0x73, 0x74, 0x69, 0x74, 0x75, 0x74, 0x65, 0x5f, 0x69, 0x6e, 0x69, 0x74, 0x00 .align 2 .globl _inject_start_i386 _inject_start_i386: -.byte 0x55, 0x89, 0xe5, 0x56, 0x83, 0xec, 0x24, 0x89, 0xce, 0xe8, 0x00, 0x00, 0x00, 0x00, 0x58, 0x89, 0x74, 0x24, 0x0c, 0x8d, 0x80, 0x55, 0x00, 0x00, 0x00, 0x89, 0x44, 0x24, 0x08, 0x8d, 0x45, 0xf8, 0x89, 0x04, 0x24, 0xc7, 0x44, 0x24, 0x04, 0x00, 0x00, 0x00, 0x00, 0xff, 0x16, 0x8b, 0x46, 0x14, 0x89, 0x45, 0xf4, 0xb8, 0x69, 0x01, 0x00, 0x00, 0xff, 0x75, 0xf4, 0x6a, 0x00, 0x6a, 0x00, 0x6a, 0x00, 0x6a, 0x00, 0x89, 0xe1, 0xe8, 0x00, 0x00, 0x00, 0x00, 0x5a, 0x81, 0xc2, 0x09, 0x00, 0x00, 0x00, 0x0f, 0x34, 0x83, 0xc4, 0x14, 0xb8, 0xad, 0x0b, 0x00, 0x00, 0xff, 0xd0, 0x83, 0xc4, 0x24, 0x5e, 0x5d, 0xc3, 0x55, 0x89, 0xe5, 0x57, 0x56, 0x83, 0xec, 0x10, 0xe8, 0x00, 0x00, 0x00, 0x00, 0x5f, 0x8b, 0x75, 0x08, 0x8b, 0x46, 0x10, 0x89, 0x04, 0x24, 0xc7, 0x44, 0x24, 0x04, 0x00, 0x00, 0x00, 0x00, 0xff, 0x56, 0x04, 0x85, 0xc0, 0x74, 0x23, 0x8d, 0x8f, 0x81, 0x00, 0x00, 0x00, 0x89, 0x4c, 0x24, 0x04, 0x89, 0x04, 0x24, 0xff, 0x56, 0x08, 0x85, 0xc0, 0x74, 0x0f, 0x8d, 0x4e, 0x1c, 0x8b, 0x56, 0x18, 0x89, 0x54, 0x24, 0x04, 0x89, 0x0c, 0x24, 0xff, 0xd0, 0x8b, 0x46, 0x14, 0x89, 0x45, 0xf4, 0xb8, 0xdc, 0xff, 0xff, 0xff, 0x6a, 0x00, 0x6a, 0x00, 0x6a, 0x00, 0xff, 0x75, 0xf4, 0x6a, 0x00, 0x89, 0xe1, 0xe8, 0x00, 0x00, 0x00, 0x00, 0x5a, 0x81, 0xc2, 0x09, 0x00, 0x00, 0x00, 0x0f, 0x34, 0x83, 0xc4, 0x14, 0x89, 0xf0, 0x25, 0x00, 0xf0, 0xff, 0xff, 0x89, 0x04, 0x24, 0xc7, 0x44, 0x24, 0x04, 0x00, 0x20, 0x00, 0x00, 0xff, 0x56, 0x0c, 0x83, 0xc4, 0x10, 0x5e, 0x5f, 0x5d, 0xc3, 0x73, 0x75, 0x62, 0x73, 0x74, 0x69, 0x74, 0x75, 0x74, 0x65, 0x5f, 0x69, 0x6e, 0x69, 0x74, 0x00 +.byte 0xe9, 0x25, 0x00, 0x00, 0x00, 0x55, 0x89, 0xe5, 0x81, 0xed, 0x00, 0x04, 0x00, 0x00, 0x8b, 0x54, 0x24, 0x08, 0x8b, 0x42, 0x10, 0x81, 0xe2, 0x00, 0xf0, 0xff, 0xff, 0x89, 0x55, 0x08, 0xc7, 0x45, 0x0c, 0x00, 0x20, 0x00, 0x00, 0x83, 0xc0, 0x03, 0xff, 0xe0, 0x55, 0x89, 0xe5, 0x56, 0x83, 0xec, 0x24, 0x89, 0xce, 0xe8, 0x00, 0x00, 0x00, 0x00, 0x58, 0xc7, 0x45, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x89, 0x74, 0x24, 0x0c, 0x8d, 0x80, 0x65, 0x00, 0x00, 0x00, 0x89, 0x44, 0x24, 0x08, 0x8d, 0x45, 0xf8, 0x89, 0x04, 0x24, 0xc7, 0x44, 0x24, 0x04, 0x00, 0x00, 0x00, 0x00, 0xff, 0x16, 0x8b, 0x45, 0xf8, 0x89, 0x04, 0x24, 0xff, 0x56, 0x04, 0x8b, 0x46, 0x18, 0x89, 0x45, 0xf4, 0xb8, 0x69, 0x01, 0x00, 0x00, 0xff, 0x75, 0xf4, 0x6a, 0x00, 0x6a, 0x00, 0x6a, 0x00, 0x6a, 0x00, 0x89, 0xe1, 0xe8, 0x00, 0x00, 0x00, 0x00, 0x5a, 0x81, 0xc2, 0x09, 0x00, 0x00, 0x00, 0x0f, 0x34, 0x83, 0xc4, 0x14, 0xb8, 0xad, 0x0b, 0x00, 0x00, 0xff, 0xd0, 0x83, 0xc4, 0x24, 0x5e, 0x5d, 0xc3, 0x55, 0x89, 0xe5, 0x57, 0x56, 0x83, 0xec, 0x10, 0xe8, 0x00, 0x00, 0x00, 0x00, 0x5f, 0x8b, 0x75, 0x08, 0x8b, 0x46, 0x14, 0x89, 0x04, 0x24, 0xc7, 0x44, 0x24, 0x04, 0x00, 0x00, 0x00, 0x00, 0xff, 0x56, 0x08, 0x85, 0xc0, 0x74, 0x23, 0x8d, 0x8f, 0x70, 0x00, 0x00, 0x00, 0x89, 0x4c, 0x24, 0x04, 0x89, 0x04, 0x24, 0xff, 0x56, 0x0c, 0x85, 0xc0, 0x74, 0x0f, 0x8d, 0x4e, 0x20, 0x8b, 0x56, 0x1c, 0x89, 0x54, 0x24, 0x04, 0x89, 0x0c, 0x24, 0xff, 0xd0, 0x8b, 0x46, 0x18, 0x89, 0x45, 0xf4, 0xb8, 0xdc, 0xff, 0xff, 0xff, 0x6a, 0x00, 0x6a, 0x00, 0x6a, 0x00, 0xff, 0x75, 0xf4, 0x6a, 0x00, 0x89, 0xe1, 0xe8, 0x00, 0x00, 0x00, 0x00, 0x5a, 0x81, 0xc2, 0x09, 0x00, 0x00, 0x00, 0x0f, 0x34, 0x83, 0xc4, 0x14, 0x83, 0xc4, 0x10, 0x5e, 0x5f, 0x5d, 0xe9, 0xeb, 0xfe, 0xff, 0xff, 0x73, 0x75, 0x62, 0x73, 0x74, 0x69, 0x74, 0x75, 0x74, 0x65, 0x5f, 0x69, 0x6e, 0x69, 0x74, 0x00 .align 2 .globl _inject_start_arm _inject_start_arm: -.byte 0x90, 0x40, 0x2d, 0xe9, 0x04, 0x70, 0x8d, 0xe2, 0x04, 0xd0, 0x4d, 0xe2, 0x00, 0x40, 0xa0, 0xe1, 0x0d, 0x00, 0xa0, 0xe1, 0x00, 0x90, 0x94, 0xe5, 0x2c, 0x20, 0x00, 0xe3, 0x00, 0x20, 0x40, 0xe3, 0x00, 0x10, 0xa0, 0xe3, 0x02, 0x20, 0x8f, 0xe0, 0x04, 0x30, 0xa0, 0xe1, 0x39, 0xff, 0x2f, 0xe1, 0x14, 0x30, 0x94, 0xe5, 0x69, 0xc1, 0x00, 0xe3, 0x00, 0x00, 0xa0, 0xe3, 0x00, 0x10, 0xa0, 0xe3, 0x00, 0x20, 0xa0, 0xe3, 0x80, 0x00, 0x00, 0xef, 0xad, 0x0b, 0x00, 0xe3, 0x30, 0xff, 0x2f, 0xe1, 0x04, 0xd0, 0x47, 0xe2, 0x90, 0x80, 0xbd, 0xe8, 0x90, 0x40, 0x2d, 0xe9, 0x00, 0x40, 0xa0, 0xe1, 0x00, 0x10, 0xa0, 0xe3, 0x04, 0x20, 0x94, 0xe5, 0x04, 0x70, 0x8d, 0xe2, 0x10, 0x00, 0x94, 0xe5, 0x32, 0xff, 0x2f, 0xe1, 0x00, 0x00, 0x50, 0xe3, 0x0a, 0x00, 0x00, 0x0a, 0x08, 0x20, 0x94, 0xe5, 0x48, 0x10, 0x00, 0xe3, 0x00, 0x10, 0x40, 0xe3, 0x01, 0x10, 0x8f, 0xe0, 0x32, 0xff, 0x2f, 0xe1, 0x00, 0x20, 0xa0, 0xe1, 0x00, 0x00, 0x52, 0xe3, 0x02, 0x00, 0x00, 0x0a, 0x18, 0x10, 0x94, 0xe5, 0x1c, 0x00, 0x84, 0xe2, 0x32, 0xff, 0x2f, 0xe1, 0x14, 0x00, 0x94, 0xe5, 0x23, 0xc0, 0xe0, 0xe3, 0x00, 0x10, 0xa0, 0xe3, 0x00, 0x20, 0xa0, 0xe3, 0x00, 0x30, 0xa0, 0xe3, 0x80, 0x00, 0x00, 0xef, 0x0c, 0x20, 0x94, 0xe5, 0x1f, 0x40, 0xcb, 0xe7, 0x02, 0x1a, 0xa0, 0xe3, 0x04, 0x00, 0xa0, 0xe1, 0x90, 0x40, 0xbd, 0xe8, 0x12, 0xff, 0x2f, 0xe1, 0x73, 0x75, 0x62, 0x73, 0x74, 0x69, 0x74, 0x75, 0x74, 0x65, 0x5f, 0x69, 0x6e, 0x69, 0x74, 0x00 +.byte 0x90, 0x40, 0x2d, 0xe9, 0x04, 0x70, 0x8d, 0xe2, 0x04, 0xd0, 0x4d, 0xe2, 0x44, 0x20, 0x00, 0xe3, 0x00, 0x40, 0xa0, 0xe1, 0x00, 0x00, 0xa0, 0xe3, 0x00, 0x20, 0x40, 0xe3, 0x00, 0x00, 0x8d, 0xe5, 0x02, 0x20, 0x8f, 0xe0, 0x00, 0x90, 0x94, 0xe5, 0x0d, 0x00, 0xa0, 0xe1, 0x00, 0x10, 0xa0, 0xe3, 0x04, 0x30, 0xa0, 0xe1, 0x39, 0xff, 0x2f, 0xe1, 0x04, 0x10, 0x94, 0xe5, 0x00, 0x00, 0x9d, 0xe5, 0x31, 0xff, 0x2f, 0xe1, 0x18, 0x30, 0x94, 0xe5, 0x69, 0xc1, 0x00, 0xe3, 0x00, 0x00, 0xa0, 0xe3, 0x00, 0x10, 0xa0, 0xe3, 0x00, 0x20, 0xa0, 0xe3, 0x80, 0x00, 0x00, 0xef, 0xad, 0x0b, 0x00, 0xe3, 0x30, 0xff, 0x2f, 0xe1, 0x04, 0xd0, 0x47, 0xe2, 0x90, 0x80, 0xbd, 0xe8, 0x90, 0x40, 0x2d, 0xe9, 0x00, 0x40, 0xa0, 0xe1, 0x00, 0x10, 0xa0, 0xe3, 0x08, 0x20, 0x94, 0xe5, 0x04, 0x70, 0x8d, 0xe2, 0x14, 0x00, 0x94, 0xe5, 0x32, 0xff, 0x2f, 0xe1, 0x00, 0x00, 0x50, 0xe3, 0x0a, 0x00, 0x00, 0x0a, 0x0c, 0x20, 0x94, 0xe5, 0x48, 0x10, 0x00, 0xe3, 0x00, 0x10, 0x40, 0xe3, 0x01, 0x10, 0x8f, 0xe0, 0x32, 0xff, 0x2f, 0xe1, 0x00, 0x20, 0xa0, 0xe1, 0x00, 0x00, 0x52, 0xe3, 0x02, 0x00, 0x00, 0x0a, 0x1c, 0x10, 0x94, 0xe5, 0x20, 0x00, 0x84, 0xe2, 0x32, 0xff, 0x2f, 0xe1, 0x18, 0x00, 0x94, 0xe5, 0x23, 0xc0, 0xe0, 0xe3, 0x00, 0x10, 0xa0, 0xe3, 0x00, 0x20, 0xa0, 0xe3, 0x00, 0x30, 0xa0, 0xe3, 0x80, 0x00, 0x00, 0xef, 0x10, 0x20, 0x94, 0xe5, 0x1f, 0x40, 0xcb, 0xe7, 0x02, 0x1a, 0xa0, 0xe3, 0x04, 0x00, 0xa0, 0xe1, 0x90, 0x40, 0xbd, 0xe8, 0x12, 0xff, 0x2f, 0xe1, 0x73, 0x75, 0x62, 0x73, 0x74, 0x69, 0x74, 0x75, 0x74, 0x65, 0x5f, 0x69, 0x6e, 0x69, 0x74, 0x00 .align 2 .globl _inject_start_arm64 _inject_start_arm64: -.byte 0xf4, 0x4f, 0xbe, 0xa9, 0xfd, 0x7b, 0x01, 0xa9, 0xfd, 0x43, 0x00, 0x91, 0xff, 0x43, 0x00, 0xd1, 0xf3, 0x03, 0x00, 0xaa, 0x68, 0x02, 0x40, 0xf9, 0x01, 0x00, 0x80, 0xd2, 0x22, 0x02, 0x00, 0x10, 0x1f, 0x20, 0x03, 0xd5, 0xe0, 0x33, 0x00, 0x91, 0xe3, 0x03, 0x13, 0xaa, 0x00, 0x01, 0x3f, 0xd6, 0x63, 0x16, 0x40, 0xf9, 0x30, 0x2d, 0x80, 0xd2, 0x00, 0x00, 0x80, 0xd2, 0x01, 0x00, 0x80, 0xd2, 0x02, 0x00, 0x80, 0xd2, 0x01, 0x10, 0x00, 0xd4, 0xa8, 0x75, 0x81, 0xd2, 0x00, 0x01, 0x3f, 0xd6, 0xbf, 0x43, 0x00, 0xd1, 0xfd, 0x7b, 0x41, 0xa9, 0xf4, 0x4f, 0xc2, 0xa8, 0xc0, 0x03, 0x5f, 0xd6, 0xf4, 0x4f, 0xbe, 0xa9, 0xfd, 0x7b, 0x01, 0xa9, 0xfd, 0x43, 0x00, 0x91, 0xf3, 0x03, 0x00, 0xaa, 0x68, 0x06, 0x40, 0xf9, 0x60, 0x12, 0x40, 0xf9, 0x01, 0x00, 0x80, 0x52, 0x00, 0x01, 0x3f, 0xd6, 0x40, 0x01, 0x00, 0xb4, 0x68, 0x0a, 0x40, 0xf9, 0x81, 0x02, 0x00, 0x10, 0x1f, 0x20, 0x03, 0xd5, 0x00, 0x01, 0x3f, 0xd6, 0xe8, 0x03, 0x00, 0xaa, 0x88, 0x00, 0x00, 0xb4, 0x60, 0xe2, 0x00, 0x91, 0x61, 0x1a, 0x40, 0xf9, 0x00, 0x01, 0x3f, 0xd6, 0x01, 0x00, 0x80, 0xd2, 0x02, 0x00, 0x80, 0xd2, 0x03, 0x00, 0x80, 0xd2, 0x60, 0x16, 0x40, 0xf9, 0x70, 0x04, 0x80, 0x92, 0x01, 0x10, 0x00, 0xd4, 0x60, 0xc6, 0x72, 0x92, 0x62, 0x0e, 0x40, 0xf9, 0xe1, 0x03, 0x73, 0xb2, 0xfd, 0x7b, 0x41, 0xa9, 0xf4, 0x4f, 0xc2, 0xa8, 0x40, 0x00, 0x1f, 0xd6, 0x73, 0x75, 0x62, 0x73, 0x74, 0x69, 0x74, 0x75, 0x74, 0x65, 0x5f, 0x69, 0x6e, 0x69, 0x74, 0x00 +.byte 0xf4, 0x4f, 0xbe, 0xa9, 0xfd, 0x7b, 0x01, 0xa9, 0xfd, 0x43, 0x00, 0x91, 0xff, 0x43, 0x00, 0xd1, 0xf3, 0x03, 0x00, 0xaa, 0xff, 0x0f, 0x00, 0xb9, 0x68, 0x02, 0x40, 0xf9, 0x01, 0x00, 0x80, 0xd2, 0x82, 0x02, 0x00, 0x10, 0x1f, 0x20, 0x03, 0xd5, 0xe0, 0x33, 0x00, 0x91, 0xe3, 0x03, 0x13, 0xaa, 0x00, 0x01, 0x3f, 0xd6, 0x68, 0x06, 0x40, 0xf9, 0xe0, 0x0f, 0x40, 0xb9, 0x00, 0x01, 0x3f, 0xd6, 0x63, 0x1a, 0x40, 0xf9, 0x30, 0x2d, 0x80, 0xd2, 0x00, 0x00, 0x80, 0xd2, 0x01, 0x00, 0x80, 0xd2, 0x02, 0x00, 0x80, 0xd2, 0x01, 0x10, 0x00, 0xd4, 0xa8, 0x75, 0x81, 0xd2, 0x00, 0x01, 0x3f, 0xd6, 0xbf, 0x43, 0x00, 0xd1, 0xfd, 0x7b, 0x41, 0xa9, 0xf4, 0x4f, 0xc2, 0xa8, 0xc0, 0x03, 0x5f, 0xd6, 0xf4, 0x4f, 0xbe, 0xa9, 0xfd, 0x7b, 0x01, 0xa9, 0xfd, 0x43, 0x00, 0x91, 0xf3, 0x03, 0x00, 0xaa, 0x68, 0x0a, 0x40, 0xf9, 0x60, 0x16, 0x40, 0xf9, 0x01, 0x00, 0x80, 0x52, 0x00, 0x01, 0x3f, 0xd6, 0x40, 0x01, 0x00, 0xb4, 0x68, 0x0e, 0x40, 0xf9, 0x81, 0x02, 0x00, 0x10, 0x1f, 0x20, 0x03, 0xd5, 0x00, 0x01, 0x3f, 0xd6, 0xe8, 0x03, 0x00, 0xaa, 0x88, 0x00, 0x00, 0xb4, 0x60, 0x02, 0x01, 0x91, 0x61, 0x1e, 0x40, 0xf9, 0x00, 0x01, 0x3f, 0xd6, 0x01, 0x00, 0x80, 0xd2, 0x02, 0x00, 0x80, 0xd2, 0x03, 0x00, 0x80, 0xd2, 0x60, 0x1a, 0x40, 0xf9, 0x70, 0x04, 0x80, 0x92, 0x01, 0x10, 0x00, 0xd4, 0x60, 0xc6, 0x72, 0x92, 0x62, 0x12, 0x40, 0xf9, 0xe1, 0x03, 0x73, 0xb2, 0xfd, 0x7b, 0x41, 0xa9, 0xf4, 0x4f, 0xc2, 0xa8, 0x40, 0x00, 0x1f, 0xd6, 0x73, 0x75, 0x62, 0x73, 0x74, 0x69, 0x74, 0x75, 0x74, 0x65, 0x5f, 0x69, 0x6e, 0x69, 0x74, 0x00 diff --git a/lib/darwin/inject-asm-raw.c b/lib/darwin/inject-asm-raw.c index 9b272e7..ca316f1 100644 --- a/lib/darwin/inject-asm-raw.c +++ b/lib/darwin/inject-asm-raw.c @@ -6,8 +6,14 @@ #define _PAGE_SIZE 0x1000 #endif +/* This is somewhat more complicated than it has to be because it does not use + * pthread_join, which depends on pthread_self, which would need to be + * initialized manually; the format of this has changed in the past, and could + * again. */ + struct baton { int (*pthread_create)(int *, void *, void *(*)(void *), void *); + int (*pthread_detach)(int); void *(*dlopen)(const char *, int); void *(*dlsym)(void *, const char *); int (*munmap)(void *, long); @@ -22,8 +28,9 @@ static int bsd_thread_func(void *); __attribute__((fastcall)) #endif void entry(struct baton *baton) { - int pt; + int pt = 0; baton->pthread_create(&pt, 0, (void *) bsd_thread_func, baton); + baton->pthread_detach(pt); manual_syscall(361 /* bsdthread_terminate */, 0, 0, 0, baton->sem_port); ((void (*)()) 0xbad)(); } @@ -39,6 +46,33 @@ static int bsd_thread_func(void *arg) { } manual_syscall(-36 /* semaphore_wait_trap */, baton->sem_port, 0, 0, 0); +#ifndef __i386__ + /* since we're munmapping our own code, this must be optimized into a jump + * (taill call elimination) */ unsigned long ptr = (unsigned long) baton & ~(_PAGE_SIZE - 1); return baton->munmap((void *) ptr, 0x2000); +#else + /* i386 can't normally eliminate tail calls in caller-cleanup calling + * conventions, unless the number of arguments is the same, so use a nasty + * hack */ + extern int jump_to_munmap(void *); + return jump_to_munmap(baton); +#endif } +#ifdef __i386__ +/* yuck */ +asm("jmp _entry;" + ".globl _jump_to_munmap;" + "_jump_to_munmap:" + "push %ebp;" + "mov %esp, %ebp;" + "sub $0x400, %ebp;" + "mov 8(%esp), %edx;" /* baton */ + "mov 16(%edx), %eax;" /* munmap */ + "and $~0xfff, %edx;" + "mov %edx, 8(%ebp);" + "movl $0x2000, 12(%ebp);" + "add $3, %eax;" /* !? */ + "jmp *%eax;" +); +#endif diff --git a/lib/darwin/inject.c b/lib/darwin/inject.c index 4e715d5..a74b057 100644 --- a/lib/darwin/inject.c +++ b/lib/darwin/inject.c @@ -390,7 +390,7 @@ got_symbol:; static int do_baton(const char *filename, size_t filelen, bool is64, mach_vm_address_t target_stackpage_end, mach_vm_address_t *target_stack_top_p, - uint64_t sym_addrs[static 4], + uint64_t sym_addrs[static 5], const struct shuttle *shuttle, size_t nshuttle, struct shuttle **target_shuttle_p, semaphore_t *sem_port_p, @@ -398,7 +398,7 @@ static int do_baton(const char *filename, size_t filelen, bool is64, char **error) { int ret; - size_t baton_len = 7 * (is64 ? 8 : 4); + size_t baton_len = 8 * (is64 ? 8 : 4); size_t shuttles_len = nshuttle * sizeof(struct shuttle); size_t filelen_rounded = (filelen + 7) & ~7; size_t total_len = baton_len + shuttles_len + filelen_rounded; @@ -480,6 +480,7 @@ static int do_baton(const char *filename, size_t filelen, bool is64, sym_addrs[1], sym_addrs[2], sym_addrs[3], + sym_addrs[4], target_stack_top + baton_len + shuttles_len, sem_port, nshuttle @@ -544,10 +545,12 @@ int substitute_dlopen_in_pid(int pid, const char *filename, int options, if ((ret = find_foreign_images(task, images, 3, error)) > 0) goto fail; - uint64_t pthread_create_addr, dlopen_addr, dlsym_addr, munmap_addr; + uint64_t pthread_create_addr, pthread_detach_addr; + uint64_t dlopen_addr, dlsym_addr, munmap_addr; cpu_type_t cputype; if (ret == FFI_SHORT_CIRCUIT) { pthread_create_addr = (uint64_t) pthread_create; + pthread_detach_addr = (uint64_t) pthread_detach; dlopen_addr = (uint64_t) dlopen; dlsym_addr = (uint64_t) dlsym; munmap_addr = (uint64_t) munmap; @@ -570,7 +573,7 @@ int substitute_dlopen_in_pid(int pid, const char *filename, int options, } syms[2]; } libs[3] = { {images[0].address, 2, {{"_dlopen", 0}, {"_dlsym", 0}}}, - {images[1].address, 1, {{"_pthread_create", 0}}}, + {images[1].address, 2, {{"_pthread_create", 0}, {"_pthread_detach", 0}}}, {images[2].address, 1, {{"_munmap", 0}}}, }; @@ -602,6 +605,8 @@ int substitute_dlopen_in_pid(int pid, const char *filename, int options, dlopen_addr = libs[0].syms[0].symaddr; dlsym_addr = libs[0].syms[1].symaddr; pthread_create_addr = libs[1].syms[0].symaddr; + pthread_detach_addr = libs[1].syms[1].symaddr; + munmap_addr = libs[2].syms[0].symaddr; } UNUSED @@ -632,7 +637,11 @@ int substitute_dlopen_in_pid(int pid, const char *filename, int options, goto fail; } - uint64_t sym_addrs[] = {pthread_create_addr, dlopen_addr, dlsym_addr, munmap_addr}; + uint64_t sym_addrs[] = {pthread_create_addr, + pthread_detach_addr, + dlopen_addr, + dlsym_addr, + munmap_addr}; mach_vm_address_t target_stack_top; if ((ret = do_baton(filename, filelen, cputype & CPU_ARCH_ABI64, target_code_page, &target_stack_top, diff --git a/test/injected-test-dylib.c b/test/injected-test-dylib.c new file mode 100644 index 0000000..9a92ff2 --- /dev/null +++ b/test/injected-test-dylib.c @@ -0,0 +1,26 @@ +#include "substitute-internal.h" +#include <stdio.h> +#include <mach/mach.h> +#include <assert.h> +__attribute__((constructor)) +static void hi() { + printf("constructor\n"); +} + +void substitute_init(struct shuttle *shuttle, size_t nshuttle) { + printf("substitute_init nshuttle=%zd\n", nshuttle); + assert(nshuttle == 1); + assert(shuttle[0].type == SUBSTITUTE_SHUTTLE_MACH_PORT); + struct { + mach_msg_header_t hdr; + char body[5]; + } msg; + msg.hdr.msgh_bits = MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND, 0); + msg.hdr.msgh_size = sizeof(msg); + msg.hdr.msgh_remote_port = shuttle[0].u.mach.port; + msg.hdr.msgh_local_port = 0; + msg.hdr.msgh_voucher_port = 0; + msg.hdr.msgh_id = 42; + strncpy(msg.body, "hello", 5); + assert(!mach_msg_send(&msg.hdr)); +} diff --git a/test/test-inject.c b/test/test-inject.c index 5d7bcff..fa5d029 100644 --- a/test/test-inject.c +++ b/test/test-inject.c @@ -5,6 +5,7 @@ #include <stdio.h> #include <stdlib.h> #include <assert.h> +#include <time.h> int main(int argc, char **argv) { if (argc <= 2) { @@ -20,16 +21,22 @@ int main(int argc, char **argv) { .u.mach.port = port, .u.mach.right_type = MACH_MSG_TYPE_MAKE_SEND} }; + clock_t a = clock(); int ret = substitute_dlopen_in_pid(pid, argv[2], 0, shuttles, 1, &error); - printf("ret=%d err=%s\n", ret, error); + clock_t b = clock(); + printf("ret=%d err=%s time=%ld\n", ret, error, (long) (b - a)); assert(!ret); free(error); static struct { mach_msg_header_t hdr; char body[5]; + mach_msg_trailer_t huh; } msg; - assert(!mach_msg_overwrite(NULL, MACH_RCV_MSG, 0, sizeof(msg), port, - MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL, &msg.hdr, 0)); + kern_return_t kr = mach_msg_overwrite(NULL, MACH_RCV_MSG, 0, sizeof(msg), port, + MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL, + &msg.hdr, 0); + printf("kr=%x\n", kr); + assert(!kr); printf("received '%.5s'\n", msg.body); } |