Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "sandbox/win/src/sandbox_nt_util.h" | 5 #include "sandbox/win/src/sandbox_nt_util.h" |
| 6 | 6 |
| 7 #include "base/win/pe_image.h" | 7 #include "base/win/pe_image.h" |
| 8 #include "sandbox/win/src/sandbox_factory.h" | 8 #include "sandbox/win/src/sandbox_factory.h" |
| 9 #include "sandbox/win/src/target_services.h" | 9 #include "sandbox/win/src/target_services.h" |
| 10 | 10 |
| 11 namespace sandbox { | 11 namespace sandbox { |
| 12 | 12 |
| 13 // This is the list of all imported symbols from ntdll.dll. | 13 // This is the list of all imported symbols from ntdll.dll. |
| 14 SANDBOX_INTERCEPT NtExports g_nt = { NULL }; | 14 SANDBOX_INTERCEPT NtExports g_nt = { NULL }; |
| 15 | 15 |
| 16 } | |
|
rvargas (doing something else)
2012/09/18 18:56:28
I actually prefer if the anonymous namespace is no
Lei Zhang
2012/09/19 01:32:41
Done.
| |
| 17 | |
| 18 namespace { | 16 namespace { |
| 19 | 17 |
| 20 #if defined(_WIN64) | 18 #if defined(_WIN64) |
| 21 void* AllocateNearTo(void* source, size_t size) { | 19 void* AllocateNearTo(void* source, size_t size) { |
| 22 using sandbox::g_nt; | |
| 23 | |
| 24 // Start with 1 GB above the source. | 20 // Start with 1 GB above the source. |
| 25 const unsigned int kOneGB = 0x40000000; | 21 const unsigned int kOneGB = 0x40000000; |
| 26 void* base = reinterpret_cast<char*>(source) + kOneGB; | 22 void* base = reinterpret_cast<char*>(source) + kOneGB; |
| 27 SIZE_T actual_size = size; | 23 SIZE_T actual_size = size; |
| 28 ULONG_PTR zero_bits = 0; // Not the correct type if used. | 24 ULONG_PTR zero_bits = 0; // Not the correct type if used. |
| 29 ULONG type = MEM_RESERVE; | 25 ULONG type = MEM_RESERVE; |
| 30 | 26 |
| 31 if (reinterpret_cast<SIZE_T>(source) > 0x7ff80000000) { | 27 if (reinterpret_cast<SIZE_T>(source) > 0x7ff80000000) { |
| 32 // We are at the top of the address space. Let's try the highest available | 28 // We are at the top of the address space. Let's try the highest available |
| 33 // address. | 29 // address. |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 63 if (!NT_SUCCESS(ret)) { | 59 if (!NT_SUCCESS(ret)) { |
| 64 VERIFY_SUCCESS(g_nt.FreeVirtualMemory(NtCurrentProcess, &base, &size, | 60 VERIFY_SUCCESS(g_nt.FreeVirtualMemory(NtCurrentProcess, &base, &size, |
| 65 MEM_RELEASE)); | 61 MEM_RELEASE)); |
| 66 base = NULL; | 62 base = NULL; |
| 67 } | 63 } |
| 68 | 64 |
| 69 return base; | 65 return base; |
| 70 } | 66 } |
| 71 #else // defined(_WIN64). | 67 #else // defined(_WIN64). |
| 72 void* AllocateNearTo(void* source, size_t size) { | 68 void* AllocateNearTo(void* source, size_t size) { |
| 73 using sandbox::g_nt; | |
| 74 UNREFERENCED_PARAMETER(source); | 69 UNREFERENCED_PARAMETER(source); |
| 75 | 70 |
| 76 // In 32-bit processes allocations below 512k are predictable, so mark | 71 // In 32-bit processes allocations below 512k are predictable, so mark |
| 77 // anything in that range as reserved and retry until we get a good address. | 72 // anything in that range as reserved and retry until we get a good address. |
| 78 const void* const kMinAddress = reinterpret_cast<void*>(512 * 1024); | 73 const void* const kMinAddress = reinterpret_cast<void*>(512 * 1024); |
| 79 NTSTATUS ret; | 74 NTSTATUS ret; |
| 80 SIZE_T actual_size; | 75 SIZE_T actual_size; |
| 81 void* base; | 76 void* base; |
| 82 do { | 77 do { |
| 83 base = NULL; | 78 base = NULL; |
| 84 actual_size = 64 * 1024; | 79 actual_size = 64 * 1024; |
| 85 ret = g_nt.AllocateVirtualMemory(NtCurrentProcess, &base, 0, &actual_size, | 80 ret = g_nt.AllocateVirtualMemory(NtCurrentProcess, &base, 0, &actual_size, |
| 86 MEM_RESERVE, PAGE_NOACCESS); | 81 MEM_RESERVE, PAGE_NOACCESS); |
| 87 if (!NT_SUCCESS(ret)) | 82 if (!NT_SUCCESS(ret)) |
| 88 return NULL; | 83 return NULL; |
| 89 } while (base < kMinAddress); | 84 } while (base < kMinAddress); |
| 90 | 85 |
| 91 actual_size = size; | 86 actual_size = size; |
| 92 ret = g_nt.AllocateVirtualMemory(NtCurrentProcess, &base, 0, &actual_size, | 87 ret = g_nt.AllocateVirtualMemory(NtCurrentProcess, &base, 0, &actual_size, |
| 93 MEM_COMMIT, PAGE_READWRITE); | 88 MEM_COMMIT, PAGE_READWRITE); |
| 94 if (!NT_SUCCESS(ret)) | 89 if (!NT_SUCCESS(ret)) |
| 95 return NULL; | 90 return NULL; |
| 96 return base; | 91 return base; |
| 97 } | 92 } |
| 98 #endif // defined(_WIN64). | 93 #endif // defined(_WIN64). |
| 99 | 94 |
| 100 } // namespace. | 95 } // namespace. |
| 101 | 96 |
| 102 namespace sandbox { | |
| 103 | |
| 104 // Handle for our private heap. | 97 // Handle for our private heap. |
| 105 void* g_heap = NULL; | 98 void* g_heap = NULL; |
| 106 | 99 |
| 107 SANDBOX_INTERCEPT HANDLE g_shared_section; | 100 SANDBOX_INTERCEPT HANDLE g_shared_section; |
| 108 SANDBOX_INTERCEPT size_t g_shared_IPC_size = 0; | 101 SANDBOX_INTERCEPT size_t g_shared_IPC_size = 0; |
| 109 SANDBOX_INTERCEPT size_t g_shared_policy_size = 0; | 102 SANDBOX_INTERCEPT size_t g_shared_policy_size = 0; |
| 110 | 103 |
| 111 void* volatile g_shared_policy_memory = NULL; | 104 void* volatile g_shared_policy_memory = NULL; |
| 112 void* volatile g_shared_IPC_memory = NULL; | 105 void* volatile g_shared_IPC_memory = NULL; |
| 113 | 106 |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 159 // Create a new heap using default values for everything. | 152 // Create a new heap using default values for everything. |
| 160 void* heap = g_nt.RtlCreateHeap(HEAP_GROWABLE, NULL, 0, 0, NULL, NULL); | 153 void* heap = g_nt.RtlCreateHeap(HEAP_GROWABLE, NULL, 0, 0, NULL, NULL); |
| 161 if (!heap) | 154 if (!heap) |
| 162 return false; | 155 return false; |
| 163 | 156 |
| 164 if (NULL != _InterlockedCompareExchangePointer(&g_heap, heap, NULL)) { | 157 if (NULL != _InterlockedCompareExchangePointer(&g_heap, heap, NULL)) { |
| 165 // Somebody beat us to the memory setup. | 158 // Somebody beat us to the memory setup. |
| 166 g_nt.RtlDestroyHeap(heap); | 159 g_nt.RtlDestroyHeap(heap); |
| 167 } | 160 } |
| 168 } | 161 } |
| 169 return (g_heap) ? true : false; | 162 return (g_heap != NULL); |
| 170 } | 163 } |
| 171 | 164 |
| 172 // Physically reads or writes from memory to verify that (at this time), it is | 165 // Physically reads or writes from memory to verify that (at this time), it is |
| 173 // valid. Returns a dummy value. | 166 // valid. Returns a dummy value. |
| 174 int TouchMemory(void* buffer, size_t size_bytes, RequiredAccess intent) { | 167 int TouchMemory(void* buffer, size_t size_bytes, RequiredAccess intent) { |
| 175 const int kPageSize = 4096; | 168 const int kPageSize = 4096; |
| 176 int dummy = 0; | 169 int dummy = 0; |
| 177 char* start = reinterpret_cast<char*>(buffer); | 170 char* start = reinterpret_cast<char*>(buffer); |
| 178 char* end = start + size_bytes - 1; | 171 char* end = start + size_bytes - 1; |
| 179 | 172 |
| (...skipping 410 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 590 UNREFERENCED_PARAMETER(type); | 583 UNREFERENCED_PARAMETER(type); |
| 591 return buffer; | 584 return buffer; |
| 592 } | 585 } |
| 593 | 586 |
| 594 void __cdecl operator delete(void* memory, void* buffer, | 587 void __cdecl operator delete(void* memory, void* buffer, |
| 595 sandbox::AllocationType type) { | 588 sandbox::AllocationType type) { |
| 596 UNREFERENCED_PARAMETER(memory); | 589 UNREFERENCED_PARAMETER(memory); |
| 597 UNREFERENCED_PARAMETER(buffer); | 590 UNREFERENCED_PARAMETER(buffer); |
| 598 UNREFERENCED_PARAMETER(type); | 591 UNREFERENCED_PARAMETER(type); |
| 599 } | 592 } |
| OLD | NEW |