Add DoNotOptimize and use it in tests.

Bug: http://b/148307629
Test: treehugger
Change-Id: I3b1726ae55116f6553ea38fe163abdde179c21f0
This commit is contained in:
Elliott Hughes 2020-10-22 13:22:35 -07:00
parent 9aa6b15d79
commit 7cda75f1d3
4 changed files with 22 additions and 20 deletions

View File

@ -22,19 +22,20 @@
#include <stdint.h>
static void TestRounding(float expectation1, float expectation2) {
// volatile to prevent compiler optimizations.
// Volatile to prevent compile-time evaluation.
volatile float f = 1.968750f;
volatile float m = 0x1.0p23f;
volatile float x = f + m;
float x;
DoNotOptimize(x = f + m);
ASSERT_FLOAT_EQ(expectation1, x);
x = x - m;
DoNotOptimize(x = x - m);
ASSERT_EQ(expectation2, x);
}
static void DivideByZero() {
// volatile to prevent compiler optimizations.
// Volatile to prevent compile-time evaluation.
volatile float zero = 0.0f;
volatile float result __attribute__((unused)) = 123.0f / zero;
DoNotOptimize(123.0f / zero);
}
TEST(fenv, fesetround_fegetround_FE_TONEAREST) {

View File

@ -925,12 +925,8 @@ TEST(malloc, DISABLED_alloc_after_fork) {
std::thread* t = new std::thread([&stop] {
while (!stop) {
for (size_t size = kMinAllocationSize; size <= kMaxAllocationSize; size <<= 1) {
void* ptr = malloc(size);
if (ptr == nullptr) {
return;
}
// Make sure this value is not optimized away.
asm volatile("" : : "r,m"(ptr) : "memory");
void* ptr;
DoNotOptimize(ptr = malloc(size));
free(ptr);
}
}
@ -943,10 +939,9 @@ TEST(malloc, DISABLED_alloc_after_fork) {
pid_t pid;
if ((pid = fork()) == 0) {
for (size_t size = kMinAllocationSize; size <= kMaxAllocationSize; size <<= 1) {
void* ptr = malloc(size);
void* ptr;
DoNotOptimize(ptr = malloc(size));
ASSERT_TRUE(ptr != nullptr);
// Make sure this value is not optimized away.
asm volatile("" : : "r,m"(ptr) : "memory");
// Make sure we can touch all of the allocation.
memset(ptr, 0x1, size);
ASSERT_LE(size, malloc_usable_size(ptr));

View File

@ -676,14 +676,10 @@ TEST(UNISTD_TEST, gettid_caching_and_pthread_create) {
ASSERT_NE(static_cast<uint64_t>(parent_tid), reinterpret_cast<uint64_t>(result));
}
static void optimization_barrier(void* arg) {
asm volatile("" : : "r"(arg) : "memory");
}
__attribute__((noinline)) static void HwasanVforkTestChild() {
// Allocate a tagged region on stack and leave it there.
char x[10000];
optimization_barrier(x);
DoNotOptimize(x);
_exit(0);
}
@ -700,7 +696,7 @@ __attribute__((noinline, no_sanitize("hwaddress"))) static void HwasanVforkTestP
// Allocate a region on stack, but don't tag it (see the function attribute).
// This depends on unallocated stack space at current function entry being untagged.
char x[10000];
optimization_barrier(x);
DoNotOptimize(x);
// Verify that contents of x[] are untagged.
HwasanReadMemory(x, sizeof(x));
}

View File

@ -290,3 +290,13 @@ class FdLeakChecker {
size_t start_count_ = CountOpenFds();
};
// From <benchmark/benchmark.h>.
template <class Tp>
static inline void DoNotOptimize(Tp const& value) {
asm volatile("" : : "r,m"(value) : "memory");
}
template <class Tp>
static inline void DoNotOptimize(Tp& value) {
asm volatile("" : "+r,m"(value) : : "memory");
}