| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/debug/asan_invalid_access.h" |
| 6 |
| 5 #include <stddef.h> | 7 #include <stddef.h> |
| 6 | 8 |
| 9 #include <memory> |
| 10 |
| 7 #include "base/debug/alias.h" | 11 #include "base/debug/alias.h" |
| 8 #include "base/debug/asan_invalid_access.h" | |
| 9 #include "base/logging.h" | 12 #include "base/logging.h" |
| 10 #include "base/memory/scoped_ptr.h" | |
| 11 #include "build/build_config.h" | 13 #include "build/build_config.h" |
| 12 | 14 |
| 13 #if defined(OS_WIN) | 15 #if defined(OS_WIN) |
| 14 #include <windows.h> | 16 #include <windows.h> |
| 15 #endif | 17 #endif |
| 16 | 18 |
| 17 namespace base { | 19 namespace base { |
| 18 namespace debug { | 20 namespace debug { |
| 19 | 21 |
| 20 namespace { | 22 namespace { |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 54 } // namespace | 56 } // namespace |
| 55 | 57 |
| 56 #if defined(ADDRESS_SANITIZER) || defined(SYZYASAN) | 58 #if defined(ADDRESS_SANITIZER) || defined(SYZYASAN) |
| 57 // NOTE(sebmarchand): We intentionally perform some invalid heap access here in | 59 // NOTE(sebmarchand): We intentionally perform some invalid heap access here in |
| 58 // order to trigger an AddressSanitizer (ASan) error report. | 60 // order to trigger an AddressSanitizer (ASan) error report. |
| 59 | 61 |
| 60 static const size_t kArraySize = 5; | 62 static const size_t kArraySize = 5; |
| 61 | 63 |
| 62 void AsanHeapOverflow() { | 64 void AsanHeapOverflow() { |
| 63 // Declares the array as volatile to make sure it doesn't get optimized away. | 65 // Declares the array as volatile to make sure it doesn't get optimized away. |
| 64 scoped_ptr<volatile int[]> array( | 66 std::unique_ptr<volatile int[]> array( |
| 65 const_cast<volatile int*>(new int[kArraySize])); | 67 const_cast<volatile int*>(new int[kArraySize])); |
| 66 int dummy = array[kArraySize]; | 68 int dummy = array[kArraySize]; |
| 67 base::debug::Alias(&dummy); | 69 base::debug::Alias(&dummy); |
| 68 } | 70 } |
| 69 | 71 |
| 70 void AsanHeapUnderflow() { | 72 void AsanHeapUnderflow() { |
| 71 // Declares the array as volatile to make sure it doesn't get optimized away. | 73 // Declares the array as volatile to make sure it doesn't get optimized away. |
| 72 scoped_ptr<volatile int[]> array( | 74 std::unique_ptr<volatile int[]> array( |
| 73 const_cast<volatile int*>(new int[kArraySize])); | 75 const_cast<volatile int*>(new int[kArraySize])); |
| 74 // We need to store the underflow address in a temporary variable as trying to | 76 // We need to store the underflow address in a temporary variable as trying to |
| 75 // access array[-1] will trigger a warning C4245: "conversion from 'int' to | 77 // access array[-1] will trigger a warning C4245: "conversion from 'int' to |
| 76 // 'size_t', signed/unsigned mismatch". | 78 // 'size_t', signed/unsigned mismatch". |
| 77 volatile int* underflow_address = &array[0] - 1; | 79 volatile int* underflow_address = &array[0] - 1; |
| 78 int dummy = *underflow_address; | 80 int dummy = *underflow_address; |
| 79 base::debug::Alias(&dummy); | 81 base::debug::Alias(&dummy); |
| 80 } | 82 } |
| 81 | 83 |
| 82 void AsanHeapUseAfterFree() { | 84 void AsanHeapUseAfterFree() { |
| 83 // Declares the array as volatile to make sure it doesn't get optimized away. | 85 // Declares the array as volatile to make sure it doesn't get optimized away. |
| 84 scoped_ptr<volatile int[]> array( | 86 std::unique_ptr<volatile int[]> array( |
| 85 const_cast<volatile int*>(new int[kArraySize])); | 87 const_cast<volatile int*>(new int[kArraySize])); |
| 86 volatile int* dangling = array.get(); | 88 volatile int* dangling = array.get(); |
| 87 array.reset(); | 89 array.reset(); |
| 88 int dummy = dangling[kArraySize / 2]; | 90 int dummy = dangling[kArraySize / 2]; |
| 89 base::debug::Alias(&dummy); | 91 base::debug::Alias(&dummy); |
| 90 } | 92 } |
| 91 | 93 |
| 92 #endif // ADDRESS_SANITIZER || SYZYASAN | 94 #endif // ADDRESS_SANITIZER || SYZYASAN |
| 93 | 95 |
| 94 #if defined(SYZYASAN) && defined(COMPILER_MSVC) | 96 #if defined(SYZYASAN) && defined(COMPILER_MSVC) |
| 95 void AsanCorruptHeapBlock() { | 97 void AsanCorruptHeapBlock() { |
| 96 CorruptMemoryBlock(false); | 98 CorruptMemoryBlock(false); |
| 97 } | 99 } |
| 98 | 100 |
| 99 void AsanCorruptHeap() { | 101 void AsanCorruptHeap() { |
| 100 CorruptMemoryBlock(true); | 102 CorruptMemoryBlock(true); |
| 101 } | 103 } |
| 102 #endif // SYZYASAN && COMPILER_MSVC | 104 #endif // SYZYASAN && COMPILER_MSVC |
| 103 | 105 |
| 104 } // namespace debug | 106 } // namespace debug |
| 105 } // namespace base | 107 } // namespace base |
| OLD | NEW |