From a00b9229c3750df9c6c3edb4a057b407250880b4 Mon Sep 17 00:00:00 2001 From: Kostya Kortchinsky Date: Fri, 20 Jan 2017 18:32:18 +0000 Subject: [PATCH] [scudo] Replacing std::atomic with Sanitizer's atomics Summary: In an effort to getting rid of dependencies to external libraries, we are replacing atomic PackedHeader use of std::atomic with Sanitizer's atomic_uint64_t, which allows us to avoid -latomic. Reviewers: kcc, phosek, alekseyshl Reviewed By: alekseyshl Subscribers: llvm-commits Differential Revision: https://reviews.llvm.org/D28864 llvm-svn: 292630 --- compiler-rt/lib/scudo/scudo_allocator.cpp | 16 +++++++--------- compiler-rt/lib/scudo/scudo_allocator.h | 4 +--- compiler-rt/test/scudo/interface.cpp | 2 +- compiler-rt/test/scudo/lit.cfg | 2 -- compiler-rt/test/scudo/malloc.cpp | 2 +- compiler-rt/test/scudo/realloc.cpp | 2 +- 6 files changed, 11 insertions(+), 17 deletions(-) diff --git a/compiler-rt/lib/scudo/scudo_allocator.cpp b/compiler-rt/lib/scudo/scudo_allocator.cpp index 62fdf77..a6d6aaf 100644 --- a/compiler-rt/lib/scudo/scudo_allocator.cpp +++ b/compiler-rt/lib/scudo/scudo_allocator.cpp @@ -125,8 +125,7 @@ struct ScudoChunk : UnpackedHeader { UnpackedHeader NewUnpackedHeader; const AtomicPackedHeader *AtomicHeader = reinterpret_cast(this); - PackedHeader NewPackedHeader = - AtomicHeader->load(std::memory_order_relaxed); + PackedHeader NewPackedHeader = atomic_load_relaxed(AtomicHeader); NewUnpackedHeader = bit_cast(NewPackedHeader); return (NewUnpackedHeader.Checksum == computeChecksum(&NewUnpackedHeader)); } @@ -135,8 +134,7 @@ struct ScudoChunk : UnpackedHeader { void loadHeader(UnpackedHeader *NewUnpackedHeader) const { const AtomicPackedHeader *AtomicHeader = reinterpret_cast(this); - PackedHeader NewPackedHeader = - AtomicHeader->load(std::memory_order_relaxed); + PackedHeader NewPackedHeader = atomic_load_relaxed(AtomicHeader); *NewUnpackedHeader = bit_cast(NewPackedHeader); if (NewUnpackedHeader->Checksum != computeChecksum(NewUnpackedHeader)) { dieWithMessage("ERROR: corrupted chunk header at address %p\n", this); @@ -149,7 +147,7 @@ struct ScudoChunk : UnpackedHeader { PackedHeader NewPackedHeader = bit_cast(*NewUnpackedHeader); AtomicPackedHeader *AtomicHeader = reinterpret_cast(this); - AtomicHeader->store(NewPackedHeader, std::memory_order_relaxed); + atomic_store_relaxed(AtomicHeader, NewPackedHeader); } // Packs and stores the header, computing the checksum in the process. We @@ -162,10 +160,10 @@ struct ScudoChunk : UnpackedHeader { PackedHeader OldPackedHeader = bit_cast(*OldUnpackedHeader); AtomicPackedHeader *AtomicHeader = reinterpret_cast(this); - if (!AtomicHeader->compare_exchange_strong(OldPackedHeader, - NewPackedHeader, - std::memory_order_relaxed, - std::memory_order_relaxed)) { + if (!atomic_compare_exchange_strong(AtomicHeader, + &OldPackedHeader, + NewPackedHeader, + memory_order_relaxed)) { dieWithMessage("ERROR: race on chunk header at address %p\n", this); } } diff --git a/compiler-rt/lib/scudo/scudo_allocator.h b/compiler-rt/lib/scudo/scudo_allocator.h index b9fc82d..5f5225b3 100644 --- a/compiler-rt/lib/scudo/scudo_allocator.h +++ b/compiler-rt/lib/scudo/scudo_allocator.h @@ -22,8 +22,6 @@ # error "The Scudo hardened allocator is currently only supported on Linux." #endif -#include - namespace __scudo { enum AllocType : u8 { @@ -58,7 +56,7 @@ struct UnpackedHeader { u64 Salt : 8; }; -typedef std::atomic AtomicPackedHeader; +typedef atomic_uint64_t AtomicPackedHeader; COMPILER_CHECK(sizeof(UnpackedHeader) == sizeof(PackedHeader)); // Minimum alignment of 8 bytes for 32-bit, 16 for 64-bit diff --git a/compiler-rt/test/scudo/interface.cpp b/compiler-rt/test/scudo/interface.cpp index f935306..55de174 100644 --- a/compiler-rt/test/scudo/interface.cpp +++ b/compiler-rt/test/scudo/interface.cpp @@ -1,4 +1,4 @@ -// RUN: %clang_scudo %s -o %t +// RUN: %clang_scudo %s -lstdc++ -o %t // RUN: %run %t 2>&1 // Tests that the sanitizer interface functions behave appropriately. diff --git a/compiler-rt/test/scudo/lit.cfg b/compiler-rt/test/scudo/lit.cfg index 4eff2ce..b047630 100644 --- a/compiler-rt/test/scudo/lit.cfg +++ b/compiler-rt/test/scudo/lit.cfg @@ -19,9 +19,7 @@ config.suffixes = ['.c', '.cc', '.cpp'] # C flags. c_flags = ([config.target_cflags] + ["-std=c++11", - "-lstdc++", "-lrt", - "-latomic", "-ldl", "-pthread", "-fPIE", diff --git a/compiler-rt/test/scudo/malloc.cpp b/compiler-rt/test/scudo/malloc.cpp index cafc744..fbe57a8 100644 --- a/compiler-rt/test/scudo/malloc.cpp +++ b/compiler-rt/test/scudo/malloc.cpp @@ -1,4 +1,4 @@ -// RUN: %clang_scudo %s -o %t +// RUN: %clang_scudo %s -lstdc++ -o %t // RUN: %run %t 2>&1 // Tests that a regular workflow of allocation, memory fill and free works as diff --git a/compiler-rt/test/scudo/realloc.cpp b/compiler-rt/test/scudo/realloc.cpp index cc44595..d34e356 100644 --- a/compiler-rt/test/scudo/realloc.cpp +++ b/compiler-rt/test/scudo/realloc.cpp @@ -1,4 +1,4 @@ -// RUN: %clang_scudo %s -o %t +// RUN: %clang_scudo %s -lstdc++ -o %t // RUN: %run %t pointers 2>&1 // RUN: %run %t contents 2>&1 // RUN: not %run %t memalign 2>&1 | FileCheck %s -- 2.7.4