xrpld
Loading...
Searching...
No Matches
asan.cpp
1#include <atomic>
2#include <cstddef>
3#include <iostream>
4
5// Regression test: the compiler-rt sanitizer interface headers must be on the
6// include path. A bare on-PATH clang in the Nix CI env doesn't get them
7// propagated automatically, so this include would fail to compile with clang++
8// if the env isn't wired up correctly. abseil hits the same include during
9// sanitizer builds. LeakSanitizer ships with AddressSanitizer.
10#include <sanitizer/lsan_interface.h>
11
12#if defined(__clang__) || defined(__GNUC__)
13__attribute__((noinline))
14#elif defined(_MSC_VER)
15__declspec(noinline)
16#endif
17int
18read_after_free(volatile int* array, std::size_t index)
19{
20 std::atomic_signal_fence(std::memory_order_seq_cst);
21 int value = array[index];
22 std::atomic_signal_fence(std::memory_order_seq_cst);
23 return value;
24}
25
26int
27main()
28{
29 int* array = new int[5]{10, 20, 30, 40, 50};
30 delete[] array;
31
32 std::cout << "Value at index 2: " << read_after_free(array, 2) << std::endl;
33
34 return 0;
35}
T atomic_signal_fence(T... args)
T endl(T... args)