| OLD | NEW |
| (Empty) | |
| 1 diff --git a/third_party/mach_override/README.chromium b/third_party/mach_overri
de/README.chromium |
| 2 index 8a7c3fd79e0b..d83803a2bf63 100644 |
| 3 --- a/third_party/mach_override/README.chromium |
| 4 +++ b/third_party/mach_override/README.chromium |
| 5 @@ -1,6 +1,7 @@ |
| 6 Name: mach_override |
| 7 Short Name: mach_override |
| 8 -Version: Newer than 1.2. HEAD from branch semver-1.x. |
| 9 +Version: 0 |
| 10 +Newer than 1.2. HEAD from branch semver-1.x. |
| 11 URL: https://github.com/rentzsch/mach_override |
| 12 Date: 2014-05-11 |
| 13 Revision: 919148f94db54fc04d287eb6a42c0c36b166bbfa |
| 14 @@ -16,4 +17,5 @@ mach_override includes a copy of libudis86 1.7.1, available se
parately from |
| 15 http://udis86.sourceforge.net/ and https://github.com/vmt/udis86 . |
| 16 |
| 17 |
| 18 -Local Modifications: None. |
| 19 +Local Modifications: Sped up allocation of branch island memory via usage of |
| 20 + vm_region to skip already allocated pages. |
| 21 diff --git a/third_party/mach_override/mach_override.c b/third_party/mach_overri
de/mach_override.c |
| 22 index 85a75e5c2067..2c2974fc1b4c 100644 |
| 23 --- a/third_party/mach_override/mach_override.c |
| 24 +++ b/third_party/mach_override/mach_override.c |
| 25 @@ -41,7 +41,7 @@ long kIslandTemplate[] = { |
| 26 #define kInstructionHi 10 |
| 27 #define kInstructionLo 11 |
| 28 |
| 29 -#elif defined(__i386__) |
| 30 +#elif defined(__i386__) |
| 31 |
| 32 #define kOriginalInstructionsSize 16 |
| 33 |
| 34 @@ -61,6 +61,7 @@ char kIslandTemplate[] = { |
| 35 #define kOriginalInstructionsSize 32 |
| 36 |
| 37 #define kJumpAddress kOriginalInstructionsSize + 6 |
| 38 +#define kMaxJumpOffset (0x7fffffffUL) |
| 39 |
| 40 char kIslandTemplate[] = { |
| 41 // kOriginalInstructionsSize nop instructions so that we |
| 42 @@ -267,7 +268,13 @@ mach_override_ptr( |
| 43 |
| 44 #if defined(__i386__) || defined(__x86_64__) |
| 45 if (!err) { |
| 46 - uint32_t addressOffset = ((char*)escapeIsland - (char*)originalF
unctionPtr - 5); |
| 47 + // TODO: On 64-bit, move to opcode FF/4 (jmp 64-bit absolute) in
stead of |
| 48 + // E9 (jmp 32-bit relative to RIP). Then we should update the |
| 49 + // allocateBranchIsland to simply allocate any page in the addre
ss space. |
| 50 + // See the 64-bit version of kIslandTemplate array. |
| 51 + int64_t addressOffset64 = ((char*)escapeIsland - (char*)original
FunctionPtr - 5); |
| 52 + int32_t addressOffset = ((char*)escapeIsland - (char*)originalFu
nctionPtr - 5); |
| 53 + assert(addressOffset64 == addressOffset); |
| 54 addressOffset = OSSwapInt32(addressOffset); |
| 55 |
| 56 jumpRelativeInstruction |= 0xE900000000000000LL; |
| 57 @@ -385,7 +392,7 @@ allocateBranchIsland( |
| 58 void *originalFunctionAddress) |
| 59 { |
| 60 assert( island ); |
| 61 - |
| 62 + |
| 63 mach_error_t err = err_none; |
| 64 |
| 65 if( allocateHigh ) { |
| 66 @@ -401,23 +408,42 @@ allocateBranchIsland( |
| 67 vm_address_t first = 0xfeffffff; |
| 68 vm_address_t last = 0xfe000000 + PAGE_SIZE; |
| 69 #elif defined(__x86_64__) |
| 70 - // 64-bit ASLR is in bits 13-28 |
| 71 - vm_address_t first = ((uint64_t)originalFunctionAddress & ~( (0x
FUL << 28) | (PAGE_SIZE - 1) ) ) | (0x1UL << 31); |
| 72 - vm_address_t last = (uint64_t)originalFunctionAddress & ~((0x1UL
<< 32) - 1); |
| 73 + // This logic is more complex due to the 32-bit limit of the jum
p out |
| 74 + // of the original function. Once that limitation is removed, we
can |
| 75 + // use vm_allocate with VM_FLAGS_ANYWHERE as in the i386 code ab
ove. |
| 76 + const uint64_t kPageMask = ~(PAGE_SIZE - 1); |
| 77 + vm_address_t first = (uint64_t)originalFunctionAddress - kMaxJum
pOffset; |
| 78 + first = (first & kPageMask) + PAGE_SIZE; // Align up to the next
page start |
| 79 + vm_address_t last = (uint64_t)originalFunctionAddress + kMaxJump
Offset; |
| 80 #endif |
| 81 |
| 82 page = first; |
| 83 int allocated = 0; |
| 84 vm_map_t task_self = mach_task_self(); |
| 85 |
| 86 - while( !err && !allocated && page != last ) { |
| 87 + while( !err && !allocated && page < last ) { |
| 88 |
| 89 err = vm_allocate( task_self, &page, PAGE_SIZE, 0 ); |
| 90 if( err == err_none ) |
| 91 allocated = 1; |
| 92 else if( err == KERN_NO_SPACE ) { |
| 93 #if defined(__x86_64__) |
| 94 - page -= PAGE_SIZE; |
| 95 + // This memory region is not suitable, skip it: |
| 96 + vm_address_t region_start = page; |
| 97 + vm_size_t region_size; |
| 98 + mach_msg_type_number_t int_count = VM_REGION_BAS
IC_INFO_COUNT_64; |
| 99 + vm_region_basic_info_data_64_t vm_region_info; |
| 100 + mach_port_t object_name; |
| 101 + kern_return_t region_result = |
| 102 + vm_region_64(task_self, ®ion_
start, ®ion_size, |
| 103 + VM_REGION_BASIC_INFO_64,
(vm_region_info_t)&vm_region_info, |
| 104 + &int_count, &object_name
); |
| 105 + if (region_result == KERN_SUCCESS) { |
| 106 + page = region_start + region_size; |
| 107 + } else { |
| 108 + err = region_result; |
| 109 + break; |
| 110 + } |
| 111 #else |
| 112 page += PAGE_SIZE; |
| 113 #endif |
| 114 @@ -438,7 +464,7 @@ allocateBranchIsland( |
| 115 } |
| 116 if( !err ) |
| 117 (**island).allocatedHigh = allocateHigh; |
| 118 - |
| 119 + |
| 120 return err; |
| 121 } |
| 122 |
| OLD | NEW |