|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | + |
| 9 | +// REQUIRES: asan |
| 10 | +// UNSUPPORTED: c++03 |
| 11 | + |
| 12 | +// Test based on: https://bugs.chromium.org/p/chromium/issues/detail?id=1419798#c5 |
| 13 | +// Some allocators during deallocation may not call destructors and just reuse memory. |
| 14 | +// In those situations, one may want to deactivate annotations for a specific allocator. |
| 15 | +// It's possible with __asan_annotate_container_with_allocator template class. |
| 16 | +// This test confirms that those allocators work after turning off annotations. |
| 17 | +// |
| 18 | +// A context to this test is a situations when memory is repurposed and destructors are not called. |
| 19 | +// Related issue: https://github.com/llvm/llvm-project/issues/60384 |
| 20 | +// |
| 21 | +// That issue appeared in the past and was addressed here: https://reviews.llvm.org/D145628 |
| 22 | +// |
| 23 | +// There was also a discussion, if it's UB. |
| 24 | +// Related discussion: https://reviews.llvm.org/D136765#4155262 |
| 25 | +// Related notes: https://eel.is/c++draft/basic.life#6 |
| 26 | +// Probably it's no longer UB due a change in CWG2523. |
| 27 | +// https://cplusplus.github.io/CWG/issues/2523.html |
| 28 | +// |
| 29 | +// Therefore we make sure that it works that way, also because people rely on this behavior. |
| 30 | +// Annotations are turned off only, if a user explicitly turns off annotations for a specific allocator. |
| 31 | + |
| 32 | +#include <assert.h> |
| 33 | +#include <stdlib.h> |
| 34 | +#include <string> |
| 35 | +#include <new> |
| 36 | + |
| 37 | +// Allocator with pre-allocated (with malloc in constructor) buffers. |
| 38 | +// Memory may be freed without calling destructors. |
| 39 | +struct reuse_allocator { |
| 40 | + static size_t const N = 100; |
| 41 | + reuse_allocator() { |
| 42 | + for (size_t i = 0; i < N; ++i) |
| 43 | + __buffers[i] = malloc(8 * 1024); |
| 44 | + } |
| 45 | + ~reuse_allocator() { |
| 46 | + for (size_t i = 0; i < N; ++i) |
| 47 | + free(__buffers[i]); |
| 48 | + } |
| 49 | + void* alloc() { |
| 50 | + assert(__next_id < N); |
| 51 | + return __buffers[__next_id++]; |
| 52 | + } |
| 53 | + void reset() { __next_id = 0; } |
| 54 | + void* __buffers[N]; |
| 55 | + size_t __next_id = 0; |
| 56 | +} reuse_buffers; |
| 57 | + |
| 58 | +template <typename T> |
| 59 | +struct user_allocator { |
| 60 | + using value_type = T; |
| 61 | + user_allocator() = default; |
| 62 | + template <class U> |
| 63 | + user_allocator(user_allocator<U>) {} |
| 64 | + friend bool operator==(user_allocator, user_allocator) { return true; } |
| 65 | + friend bool operator!=(user_allocator x, user_allocator y) { return !(x == y); } |
| 66 | + |
| 67 | + T* allocate(size_t n) { |
| 68 | + if (n * sizeof(T) > 8 * 1024) |
| 69 | + throw std::bad_array_new_length(); |
| 70 | + return (T*)reuse_buffers.alloc(); |
| 71 | + } |
| 72 | + void deallocate(T*, size_t) noexcept {} |
| 73 | +}; |
| 74 | + |
| 75 | +// Turn off annotations for user_allocator: |
| 76 | +template <class T> |
| 77 | +struct std::__asan_annotate_container_with_allocator<user_allocator<T>> { |
| 78 | + static bool const value = false; |
| 79 | +}; |
| 80 | + |
| 81 | +int main(int, char**) { |
| 82 | + using S = std::basic_string<char, std::char_traits<char>, user_allocator<char>>; |
| 83 | + |
| 84 | + { |
| 85 | + // Create a string with a buffer from reuse allocator object: |
| 86 | + S* s = new (reuse_buffers.alloc()) S(); |
| 87 | + // Use string, so it's poisoned, if container annotations for that allocator are not turned off: |
| 88 | + for (int i = 0; i < 40; i++) |
| 89 | + s->push_back('a'); |
| 90 | + } |
| 91 | + // Reset the state of the allocator, don't call destructors, allow memory to be reused: |
| 92 | + reuse_buffers.reset(); |
| 93 | + { |
| 94 | + // Create a next string with the same allocator, so the same buffer due to the reset: |
| 95 | + S s; |
| 96 | + // Use memory inside the string again, if it's poisoned, an error will be raised: |
| 97 | + for (int i = 0; i < 60; i++) |
| 98 | + s.push_back('a'); |
| 99 | + } |
| 100 | + |
| 101 | + return 0; |
| 102 | +} |
0 commit comments