Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 4eb81d2

Browse files
committedNov 11, 2017
[ubsan-minimal] Get rid of the libc++ dependency.
Summary: Use -nodefaultlibs. Replace std:atomic with sanitizer atomics. Reviewers: vitalybuka, kongyi, EricWF Subscribers: mgorny, llvm-commits Differential Revision: https://reviews.llvm.org/D39934 git-svn-id: https://llvm.org/svn/llvm-project/compiler-rt/trunk@317969 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 0c8f4a8 commit 4eb81d2

File tree

2 files changed

+23
-18
lines changed

2 files changed

+23
-18
lines changed
 

‎lib/ubsan_minimal/CMakeLists.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ include_directories(..)
99
set(UBSAN_CFLAGS ${SANITIZER_COMMON_CFLAGS})
1010
append_rtti_flag(OFF UBSAN_CFLAGS)
1111

12-
set(UBSAN_STANDALONE_CFLAGS ${SANITIZER_COMMON_CFLAGS})
13-
append_rtti_flag(OFF UBSAN_STANDALONE_CFLAGS)
12+
set(UBSAN_LINK_FLAGS ${SANITIZER_COMMON_LINK_FLAGS})
13+
14+
set(UBSAN_DYNAMIC_LIBS ${SANITIZER_COMMON_LINK_LIBS})
1415

1516
add_compiler_rt_component(ubsan-minimal)
1617

@@ -39,6 +40,7 @@ if(COMPILER_RT_HAS_UBSAN_MINIMAL)
3940
ARCHS ${UBSAN_SUPPORTED_ARCH}
4041
OBJECT_LIBS RTUbsan_minimal
4142
CFLAGS ${UBSAN_CFLAGS}
43+
LINK_FLAGS ${UBSAN_LINK_FLAGS}
4244
LINK_LIBS ${UBSAN_DYNAMIC_LIBS}
4345
PARENT_TARGET ubsan-minimal)
4446

‎lib/ubsan_minimal/ubsan_minimal_handlers.cc

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
#include <atomic>
1+
#include "sanitizer_common/sanitizer_atomic.h"
2+
23
#include <stdlib.h>
4+
#include <stdint.h>
35
#include <string.h>
46
#include <unistd.h>
57

@@ -8,37 +10,38 @@ static void message(const char *msg) {
810
}
911

1012
static const int kMaxCallerPcs = 20;
11-
static std::atomic<void *> caller_pcs[kMaxCallerPcs];
13+
static __sanitizer::atomic_uintptr_t caller_pcs[kMaxCallerPcs];
1214
// Number of elements in caller_pcs. A special value of kMaxCallerPcs + 1 means
1315
// that "too many errors" has already been reported.
14-
static std::atomic<int> caller_pcs_sz;
16+
static __sanitizer::atomic_uint32_t caller_pcs_sz;
1517

16-
__attribute__((noinline))
17-
static bool report_this_error(void *caller) {
18-
if (caller == nullptr) return false;
18+
__attribute__((noinline)) static bool report_this_error(void *caller_p) {
19+
uintptr_t caller = reinterpret_cast<uintptr_t>(caller_p);
20+
if (caller == 0) return false;
1921
while (true) {
20-
int sz = caller_pcs_sz.load(std::memory_order_relaxed);
21-
if (sz > kMaxCallerPcs) return false; // early exit
22+
unsigned sz = __sanitizer::atomic_load_relaxed(&caller_pcs_sz);
23+
if (sz > kMaxCallerPcs) return false; // early exit
2224
// when sz==kMaxCallerPcs print "too many errors", but only when cmpxchg
2325
// succeeds in order to not print it multiple times.
2426
if (sz > 0 && sz < kMaxCallerPcs) {
25-
void *p;
26-
for (int i = 0; i < sz; ++i) {
27-
p = caller_pcs[i].load(std::memory_order_relaxed);
28-
if (p == nullptr) break; // Concurrent update.
27+
uintptr_t p;
28+
for (unsigned i = 0; i < sz; ++i) {
29+
p = __sanitizer::atomic_load_relaxed(&caller_pcs[i]);
30+
if (p == 0) break; // Concurrent update.
2931
if (p == caller) return false;
3032
}
31-
if (p == nullptr) continue; // FIXME: yield?
33+
if (p == 0) continue; // FIXME: yield?
3234
}
3335

34-
if (!caller_pcs_sz.compare_exchange_strong(sz, sz + 1))
35-
continue; // Concurrent update! Try again from the start.
36+
if (!__sanitizer::atomic_compare_exchange_strong(
37+
&caller_pcs_sz, &sz, sz + 1, __sanitizer::memory_order_seq_cst))
38+
continue; // Concurrent update! Try again from the start.
3639

3740
if (sz == kMaxCallerPcs) {
3841
message("ubsan: too many errors\n");
3942
return false;
4043
}
41-
caller_pcs[sz].store(caller, std::memory_order_relaxed);
44+
__sanitizer::atomic_store_relaxed(&caller_pcs[sz], caller);
4245
return true;
4346
}
4447
}

0 commit comments

Comments
 (0)
Please sign in to comment.