|
| 1 | +// Test to demonstrate compile-time disabling of container-overflow checks |
| 2 | +// in order to handle uninstrumented libraries |
| 3 | +// |
| 4 | +// Explore three options discussed as implementable in the santizer headers to reduce the need |
| 5 | +// for changes in library code if they include the sanitizer interface header sanitizer/common_interface_defs.h |
| 6 | +// instead of having their own forward declaration |
| 7 | +// |
| 8 | +// - force inlined alternative body - minimizes the need for optimizations to reduce bloat |
| 9 | +// - static declaration of alternate body - potential bloat if optimizer is not run |
| 10 | +// - use of #define to remove calls completely |
| 11 | + |
| 12 | +// Mimic a closed-source library compiled without ASan |
| 13 | +// RUN: %clangxx_asan -fno-sanitize=address -DSHARED_LIB %s -dynamiclib -o %t-closedsource.dylib |
| 14 | + |
| 15 | +// Mimic multiple files being linked into a single executable, |
| 16 | +// %t-object.o and %t-main compiled seperately and then linked together |
| 17 | +// |
| 18 | +// -fsanitize=address with container overflow turned on (default) |
| 19 | +// RUN: %clangxx_asan -DMULTI_SOURCE %s -c -o %t-object.o |
| 20 | +// RUN: %clangxx_asan %s -c -o %t-main.o |
| 21 | +// RUN: %clangxx_asan -o %t %t-main.o %t-object.o -framework %t-closedsource.dylib |
| 22 | +// RUN: not %run %t 2>&1 | FileCheck %s |
| 23 | +// |
| 24 | +// -fsanitize=address with container overflow turned off using ALWAYS_INLINE |
| 25 | +// RUN: %clangxx_asan -D__ASAN_DISABLE_CONTAINER_OVERFLOW__ -DMULTI_SOURCE %s -c -o %t-object.o |
| 26 | +// RUN: %clangxx_asan -D__ASAN_DISABLE_CONTAINER_OVERFLOW__ %s -c -o %t-main.o |
| 27 | +// RUN: %clangxx_asan -D__ASAN_DISABLE_CONTAINER_OVERFLOW__ -o %t %t-main.o %t-object.o -framework %t-closedsource.dylib |
| 28 | +// RUN: %run %t 2>&1 | FileCheck --check-prefix=CHECK-NO-CONTAINER-OVERFLOW %s |
| 29 | +// |
| 30 | +// -fsanitize=address with container overflow turned off using static linkage |
| 31 | +// RUN: %clangxx_asan -DUSE_STATIC -D__ASAN_DISABLE_CONTAINER_OVERFLOW__ -DMULTI_SOURCE %s -c -o %t-object.o |
| 32 | +// RUN: %clangxx_asan -DUSE_STATIC -D__ASAN_DISABLE_CONTAINER_OVERFLOW__ %s -c -o %t-main.o |
| 33 | +// RUN: %clangxx_asan -DUSE_STATIC -D__ASAN_DISABLE_CONTAINER_OVERFLOW__ -o %t %t-main.o %t-object.o -framework %t-closedsource.dylib |
| 34 | +// RUN: %run %t 2>&1 | FileCheck --check-prefix=CHECK-NO-CONTAINER-OVERFLOW %s |
| 35 | +// |
| 36 | +// -fsanitize=address with container overflow turned off using #define to remove the function calls |
| 37 | +// RUN: %clangxx_asan -DUSE_DEFINE -D__ASAN_DISABLE_CONTAINER_OVERFLOW__ -DMULTI_SOURCE %s -c -o %t-object.o |
| 38 | +// RUN: %clangxx_asan -DUSE_DEFINE -D__ASAN_DISABLE_CONTAINER_OVERFLOW__ %s -c -o %t-main.o |
| 39 | +// RUN: %clangxx_asan -DUSE_DEFINE -D__ASAN_DISABLE_CONTAINER_OVERFLOW__ -o %t %t-main.o %t-object.o -framework %t-closedsource.dylib |
| 40 | +// RUN: %run %t 2>&1 | FileCheck --check-prefix=CHECK-NO-CONTAINER-OVERFLOW %s |
| 41 | + |
| 42 | +#include <assert.h> |
| 43 | +#include <stdio.h> |
| 44 | + |
| 45 | +// Mimic sanitizer/common_interface_defs disabling the container overflow calls |
| 46 | +#if !__has_feature(address_sanitizer) || __ASAN_DISABLE_CONTAINER_OVERFLOW__ |
| 47 | + |
| 48 | +# if USE_DEFINE |
| 49 | +# define __sanitizer_annotate_contiguous_container(...) |
| 50 | +# else |
| 51 | + |
| 52 | +// in this test match the extern "C" declaration as <string.h> pulls in copies of the |
| 53 | +// declarations |
| 54 | +extern "C" { |
| 55 | +# if USE_STATIC |
| 56 | +static |
| 57 | +# else |
| 58 | + |
| 59 | +// clone of the ALWAYS_INLINE macro |
| 60 | +# if defined(_MSC_VER) |
| 61 | +# define ALWAYS_INLINE __forceinline |
| 62 | +# else // _MSC_VER |
| 63 | +# define ALWAYS_INLINE inline __attribute__((always_inline)) |
| 64 | +# endif // _MSC_VER |
| 65 | + |
| 66 | +ALWAYS_INLINE |
| 67 | +# endif |
| 68 | + void __sanitizer_annotate_contiguous_container(const void *beg, |
| 69 | + const void *end, |
| 70 | + const void *old_mid, |
| 71 | + const void *new_mid) {}; |
| 72 | +} |
| 73 | +# endif |
| 74 | + |
| 75 | +#else |
| 76 | + |
| 77 | +# include <sanitizer/common_interface_defs.h> |
| 78 | + |
| 79 | +#endif |
| 80 | + |
| 81 | +// Mimic template based container library header |
| 82 | +template <typename T> class Stack { |
| 83 | +private: |
| 84 | + T data[5]; |
| 85 | + size_t size; |
| 86 | + |
| 87 | +public: |
| 88 | + Stack() : size(0) { |
| 89 | + // Mark entire storage as unaddressable initially |
| 90 | +#if __has_feature(address_sanitizer) |
| 91 | + __sanitizer_annotate_contiguous_container(data, data + 5, data + 5, data); |
| 92 | +#endif |
| 93 | + } |
| 94 | + |
| 95 | + ~Stack() { |
| 96 | +#if __has_feature(address_sanitizer) |
| 97 | + __sanitizer_annotate_contiguous_container(data, data + 5, data + size, |
| 98 | + data + 5); |
| 99 | +#endif |
| 100 | + } |
| 101 | + |
| 102 | + void push(const T &value) { |
| 103 | + assert(size < 5 && "Stack overflow"); |
| 104 | +#if __has_feature(address_sanitizer) |
| 105 | + __sanitizer_annotate_contiguous_container(data, data + 5, data + size, |
| 106 | + data + size + 1); |
| 107 | +#endif |
| 108 | + data[size++] = value; |
| 109 | + } |
| 110 | + |
| 111 | + T pop() { |
| 112 | + assert(size > 0 && "Cannot pop from empty stack"); |
| 113 | + T result = data[--size]; |
| 114 | +#if __has_feature(address_sanitizer) |
| 115 | + __sanitizer_annotate_contiguous_container(data, data + 5, data + size + 1, |
| 116 | + data + size); |
| 117 | +#endif |
| 118 | + return result; |
| 119 | + } |
| 120 | +}; |
| 121 | + |
| 122 | +#if defined(SHARED_LIB) |
| 123 | +// Mimics a closed-source library compiled without ASan |
| 124 | + |
| 125 | +extern "C" void push_value_to_stack(Stack<int> &stack) { stack.push(42); } |
| 126 | + |
| 127 | +#elif defined(MULTI_SOURCE) |
| 128 | + |
| 129 | +// Mimic multiple source files in a single project compiled seperately |
| 130 | +extern "C" void push_value_to_stack(Stack<int> &stack); |
| 131 | + |
| 132 | +extern "C" void do_push_value_to_stack(Stack<int> &stack) { |
| 133 | + push_value_to_stack(stack); |
| 134 | +} |
| 135 | + |
| 136 | +#else |
| 137 | + |
| 138 | +extern "C" void do_push_value_to_stack(Stack<int> &stack); |
| 139 | + |
| 140 | +# include <string> |
| 141 | + |
| 142 | +int main(int argc, char *argv[]) { |
| 143 | + |
| 144 | + Stack<int> stack; |
| 145 | + do_push_value_to_stack(stack); |
| 146 | + |
| 147 | + // BOOM! uninstrumented library didn't update container bounds |
| 148 | + int value = stack.pop(); |
| 149 | + // CHECK: AddressSanitizer: container-overflow |
| 150 | + printf("Popped value: %d\n", value); |
| 151 | + assert(value == 42 && "Expected value 42"); |
| 152 | + |
| 153 | + printf("SUCCESS\n"); |
| 154 | + // CHECK-NO-CONTAINER-OVERFLOW: SUCCESS |
| 155 | + return 0; |
| 156 | +} |
| 157 | + |
| 158 | +#endif // SHARED_LIB |
0 commit comments