[email protected] | b4b3479 | 2014-06-14 08:29:37 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
avi | ebe805c | 2015-12-24 08:20:28 | [diff] [blame] | 5 | #include <stddef.h> |
[email protected] | b4b3479 | 2014-06-14 08:29:37 | [diff] [blame] | 6 | |
| 7 | #include "base/debug/alias.h" |
| 8 | #include "base/debug/asan_invalid_access.h" |
| 9 | #include "base/logging.h" |
| 10 | #include "base/memory/scoped_ptr.h" |
avi | ebe805c | 2015-12-24 08:20:28 | [diff] [blame] | 11 | #include "build/build_config.h" |
| 12 | |
| 13 | #if defined(OS_WIN) |
| 14 | #include <windows.h> |
| 15 | #endif |
[email protected] | b4b3479 | 2014-06-14 08:29:37 | [diff] [blame] | 16 | |
| 17 | namespace base { |
| 18 | namespace debug { |
| 19 | |
| 20 | namespace { |
| 21 | |
[email protected] | 8b084ec | 2014-07-31 03:12:03 | [diff] [blame] | 22 | #if defined(SYZYASAN) && defined(COMPILER_MSVC) |
[email protected] | acecfcd | 2014-07-28 18:25:56 | [diff] [blame] | 23 | // Disable warning C4530: "C++ exception handler used, but unwind semantics are |
| 24 | // not enabled". We don't want to change the compilation flags just for this |
| 25 | // test, and no exception should be triggered here, so this warning has no value |
| 26 | // here. |
| 27 | #pragma warning(push) |
| 28 | #pragma warning(disable: 4530) |
[email protected] | b4b3479 | 2014-06-14 08:29:37 | [diff] [blame] | 29 | // Corrupt a memory block and make sure that the corruption gets detected either |
| 30 | // when we free it or when another crash happens (if |induce_crash| is set to |
| 31 | // true). |
| 32 | NOINLINE void CorruptMemoryBlock(bool induce_crash) { |
| 33 | // NOTE(sebmarchand): We intentionally corrupt a memory block here in order to |
| 34 | // trigger an Address Sanitizer (ASAN) error report. |
| 35 | static const int kArraySize = 5; |
| 36 | int* array = new int[kArraySize]; |
| 37 | // Encapsulate the invalid memory access into a try-catch statement to prevent |
| 38 | // this function from being instrumented. This way the underflow won't be |
| 39 | // detected but the corruption will (as the allocator will still be hooked). |
| 40 | try { |
| 41 | // Declares the dummy value as volatile to make sure it doesn't get |
| 42 | // optimized away. |
| 43 | int volatile dummy = array[-1]--; |
| 44 | base::debug::Alias(const_cast<int*>(&dummy)); |
| 45 | } catch (...) { |
| 46 | } |
| 47 | if (induce_crash) |
| 48 | CHECK(false); |
| 49 | delete[] array; |
| 50 | } |
[email protected] | acecfcd | 2014-07-28 18:25:56 | [diff] [blame] | 51 | #pragma warning(pop) |
[email protected] | 8b084ec | 2014-07-31 03:12:03 | [diff] [blame] | 52 | #endif // SYZYASAN && COMPILER_MSVC |
[email protected] | b4b3479 | 2014-06-14 08:29:37 | [diff] [blame] | 53 | |
| 54 | } // namespace |
| 55 | |
| 56 | #if defined(ADDRESS_SANITIZER) || defined(SYZYASAN) |
| 57 | // NOTE(sebmarchand): We intentionally perform some invalid heap access here in |
| 58 | // order to trigger an AddressSanitizer (ASan) error report. |
| 59 | |
[email protected] | acecfcd | 2014-07-28 18:25:56 | [diff] [blame] | 60 | static const size_t kArraySize = 5; |
[email protected] | b4b3479 | 2014-06-14 08:29:37 | [diff] [blame] | 61 | |
| 62 | void AsanHeapOverflow() { |
thakis | fdc6e5f | 2016-02-24 22:12:37 | [diff] [blame] | 63 | // Declares the array as volatile to make sure it doesn't get optimized away. |
| 64 | scoped_ptr<volatile int[]> array( |
| 65 | const_cast<volatile int*>(new int[kArraySize])); |
| 66 | int dummy = array[kArraySize]; |
| 67 | base::debug::Alias(&dummy); |
[email protected] | b4b3479 | 2014-06-14 08:29:37 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | void AsanHeapUnderflow() { |
thakis | fdc6e5f | 2016-02-24 22:12:37 | [diff] [blame] | 71 | // Declares the array as volatile to make sure it doesn't get optimized away. |
| 72 | scoped_ptr<volatile int[]> array( |
| 73 | const_cast<volatile int*>(new int[kArraySize])); |
[email protected] | acecfcd | 2014-07-28 18:25:56 | [diff] [blame] | 74 | // 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 |
| 76 | // 'size_t', signed/unsigned mismatch". |
thakis | fdc6e5f | 2016-02-24 22:12:37 | [diff] [blame] | 77 | volatile int* underflow_address = &array[0] - 1; |
| 78 | int dummy = *underflow_address; |
| 79 | base::debug::Alias(&dummy); |
[email protected] | b4b3479 | 2014-06-14 08:29:37 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | void AsanHeapUseAfterFree() { |
thakis | fdc6e5f | 2016-02-24 22:12:37 | [diff] [blame] | 83 | // Declares the array as volatile to make sure it doesn't get optimized away. |
| 84 | scoped_ptr<volatile int[]> array( |
| 85 | const_cast<volatile int*>(new int[kArraySize])); |
| 86 | volatile int* dangling = array.get(); |
[email protected] | b4b3479 | 2014-06-14 08:29:37 | [diff] [blame] | 87 | array.reset(); |
thakis | fdc6e5f | 2016-02-24 22:12:37 | [diff] [blame] | 88 | int dummy = dangling[kArraySize / 2]; |
| 89 | base::debug::Alias(&dummy); |
[email protected] | b4b3479 | 2014-06-14 08:29:37 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | #endif // ADDRESS_SANITIZER || SYZYASAN |
| 93 | |
[email protected] | 8b084ec | 2014-07-31 03:12:03 | [diff] [blame] | 94 | #if defined(SYZYASAN) && defined(COMPILER_MSVC) |
[email protected] | b4b3479 | 2014-06-14 08:29:37 | [diff] [blame] | 95 | void AsanCorruptHeapBlock() { |
| 96 | CorruptMemoryBlock(false); |
| 97 | } |
| 98 | |
| 99 | void AsanCorruptHeap() { |
| 100 | CorruptMemoryBlock(true); |
| 101 | } |
[email protected] | 8b084ec | 2014-07-31 03:12:03 | [diff] [blame] | 102 | #endif // SYZYASAN && COMPILER_MSVC |
[email protected] | b4b3479 | 2014-06-14 08:29:37 | [diff] [blame] | 103 | |
| 104 | } // namespace debug |
| 105 | } // namespace base |