| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 "base/process/memory.h" | 5 #include "base/process/memory.h" |
| 6 | 6 |
| 7 #include <new.h> | 7 #include <new.h> |
| 8 #include <psapi.h> | 8 #include <psapi.h> |
| 9 #include <stddef.h> | 9 #include <stddef.h> |
| 10 | 10 #include <windows.h> |
| 11 #include "base/logging.h" | |
| 12 | 11 |
| 13 // malloc_unchecked is required to implement UncheckedMalloc properly. | 12 // malloc_unchecked is required to implement UncheckedMalloc properly. |
| 14 // It's provided by allocator_shim_win.cc but since that's not always present, | 13 // It's provided by allocator_shim_win.cc but since that's not always present, |
| 15 // we provide a default that falls back to regular malloc. | 14 // we provide a default that falls back to regular malloc. |
| 16 typedef void* (*MallocFn)(size_t); | 15 typedef void* (*MallocFn)(size_t); |
| 17 extern "C" void* (*const malloc_unchecked)(size_t); | 16 extern "C" void* (*const malloc_unchecked)(size_t); |
| 18 extern "C" void* (*const malloc_default)(size_t) = &malloc; | 17 extern "C" void* (*const malloc_default)(size_t) = &malloc; |
| 19 | 18 |
| 20 #if defined(_M_IX86) | 19 #if defined(_M_IX86) |
| 21 #pragma comment(linker, "/alternatename:_malloc_unchecked=_malloc_default") | 20 #pragma comment(linker, "/alternatename:_malloc_unchecked=_malloc_default") |
| 22 #elif defined(_M_X64) || defined(_M_ARM) | 21 #elif defined(_M_X64) || defined(_M_ARM) |
| 23 #pragma comment(linker, "/alternatename:malloc_unchecked=malloc_default") | 22 #pragma comment(linker, "/alternatename:malloc_unchecked=malloc_default") |
| 24 #else | 23 #else |
| 25 #error Unsupported platform | 24 #error Unsupported platform |
| 26 #endif | 25 #endif |
| 27 | 26 |
| 28 namespace base { | 27 namespace base { |
| 29 | 28 |
| 30 namespace { | 29 namespace { |
| 31 | 30 |
| 32 #pragma warning(push) | 31 #pragma warning(push) |
| 33 #pragma warning(disable: 4702) | 32 #pragma warning(disable: 4702) |
| 34 | 33 |
| 35 int OnNoMemory(size_t size) { | 34 int OnNoMemory(size_t size) { |
| 36 // Kill the process. This is important for security since most of code | 35 // Kill the process. This is important for security since most of code |
| 37 // does not check the result of memory allocation. | 36 // does not check the result of memory allocation. |
| 38 LOG(FATAL) << "Out of memory, size = " << size; | 37 // https://msdn.microsoft.com/en-us/library/het71c37.aspx |
| 38 ::RaiseException(win::kOomExceptionCode, EXCEPTION_NONCONTINUABLE, 0, |
| 39 nullptr); |
| 39 | 40 |
| 40 // Safety check, make sure process exits here. | 41 // Safety check, make sure process exits here. |
| 41 _exit(1); | 42 _exit(win::kOomExceptionCode); |
| 42 return 0; | 43 return 0; |
| 43 } | 44 } |
| 44 | 45 |
| 45 #pragma warning(pop) | 46 #pragma warning(pop) |
| 46 | 47 |
| 47 // HeapSetInformation function pointer. | 48 // HeapSetInformation function pointer. |
| 48 typedef BOOL (WINAPI* HeapSetFn)(HANDLE, HEAP_INFORMATION_CLASS, PVOID, SIZE_T); | 49 typedef BOOL (WINAPI* HeapSetFn)(HANDLE, HEAP_INFORMATION_CLASS, PVOID, SIZE_T); |
| 49 | 50 |
| 50 } // namespace | 51 } // namespace |
| 51 | 52 |
| 52 void EnableTerminationOnHeapCorruption() { | 53 void EnableTerminationOnHeapCorruption() { |
| 53 // Ignore the result code. Supported on XP SP3 and Vista. | 54 // Ignore the result code. Supported on XP SP3 and Vista. |
| 54 HeapSetInformation(NULL, HeapEnableTerminationOnCorruption, NULL, 0); | 55 HeapSetInformation(NULL, HeapEnableTerminationOnCorruption, NULL, 0); |
| 55 } | 56 } |
| 56 | 57 |
| 57 void EnableTerminationOnOutOfMemory() { | 58 void EnableTerminationOnOutOfMemory() { |
| 58 _set_new_handler(&OnNoMemory); | 59 _set_new_handler(&OnNoMemory); |
| 59 _set_new_mode(1); | 60 _set_new_mode(1); |
| 60 } | 61 } |
| 61 | 62 |
| 62 // Implemented using a weak symbol. | 63 // Implemented using a weak symbol. |
| 63 bool UncheckedMalloc(size_t size, void** result) { | 64 bool UncheckedMalloc(size_t size, void** result) { |
| 64 *result = malloc_unchecked(size); | 65 *result = malloc_unchecked(size); |
| 65 return *result != NULL; | 66 return *result != NULL; |
| 66 } | 67 } |
| 67 | 68 |
| 68 } // namespace base | 69 } // namespace base |
| OLD | NEW |