Skip to content

Commit

Permalink
revise sanitizer extern wrappers
Browse files Browse the repository at this point in the history
Summary: Make them a bit shorter with a bit less repetition. Performance-wise, trades away two direct calls for one indirect but perfectly-predictable call. And continues to optimize away in non-sanitizer builds.

Reviewed By: luciang

Differential Revision: D31673074

fbshipit-source-id: f6d8b66cf45a81788d1ec0c5b58241f4fc693001
  • Loading branch information
yfeldblum authored and facebook-github-bot committed Oct 17, 2021
1 parent 0ae0ccc commit e83309f
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 202 deletions.
17 changes: 10 additions & 7 deletions folly/memory/SanitizeAddress.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,20 @@
// https://github.com/llvm-mirror/compiler-rt/blob/master/include/sanitizer/asan_interface.h
extern "C" void* __asan_region_is_poisoned(void*, std::size_t);

namespace folly {
namespace detail {
namespace {

FOLLY_CREATE_EXTERN_ACCESSOR(
asan_region_is_poisoned_access_v, __asan_region_is_poisoned);

void* asan_region_is_poisoned_(void* const ptr, std::size_t len) {
constexpr auto fun =
asan_region_is_poisoned_access_v<kIsLibrarySanitizeAddress>;
return fun ? fun(ptr, len) : nullptr;
}
} // namespace

static constexpr auto const E = folly::kIsLibrarySanitizeAddress;

namespace folly {
namespace detail {

FOLLY_STORAGE_CONSTEXPR asan_region_is_poisoned_t* const
asan_region_is_poisoned_v = asan_region_is_poisoned_access_v<E>;

} // namespace detail
} // namespace folly
10 changes: 5 additions & 5 deletions folly/memory/SanitizeAddress.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ namespace folly {

namespace detail {

void* asan_region_is_poisoned_(void* ptr, std::size_t len);
using asan_region_is_poisoned_t = void*(void* ptr, std::size_t len);
extern asan_region_is_poisoned_t* const asan_region_is_poisoned_v;

}
} // namespace detail

// asan_region_is_poisoned
//
Expand All @@ -35,9 +36,8 @@ void* asan_region_is_poisoned_(void* ptr, std::size_t len);
// always returns nullptr.
FOLLY_ALWAYS_INLINE static void* asan_region_is_poisoned(
void* const ptr, std::size_t const len) {
return kIsSanitizeAddress //
? detail::asan_region_is_poisoned_(ptr, len)
: nullptr;
auto fun = detail::asan_region_is_poisoned_v;
return kIsSanitizeAddress ? fun(ptr, len) : nullptr;
}

} // namespace folly
159 changes: 57 additions & 102 deletions folly/synchronization/SanitizeThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,41 +23,40 @@
// header files.

extern "C" void AnnotateRWLockCreate(
const char* f, int l, const volatile void* addr);
char const* f, int l, const volatile void* addr);

extern "C" void AnnotateRWLockCreateStatic(
const char* f, int l, const volatile void* addr);
char const* f, int l, const volatile void* addr);

extern "C" void AnnotateRWLockDestroy(
const char* f, int l, const volatile void* addr);
char const* f, int l, const volatile void* addr);

extern "C" void AnnotateRWLockAcquired(
const char* f, int l, const volatile void* addr, long w);
char const* f, int l, const volatile void* addr, long w);

extern "C" void AnnotateRWLockReleased(
const char* f, int l, const volatile void* addr, long w);
char const* f, int l, const volatile void* addr, long w);

extern "C" void AnnotateBenignRaceSized(
const char* f,
char const* f,
int l,
const volatile void* addr,
long size,
const char* desc);
char const* desc);

extern "C" void AnnotateIgnoreReadsBegin(const char* f, int l);
extern "C" void AnnotateIgnoreReadsBegin(char const* f, int l);

extern "C" void AnnotateIgnoreReadsEnd(const char* f, int l);
extern "C" void AnnotateIgnoreReadsEnd(char const* f, int l);

extern "C" void AnnotateIgnoreWritesBegin(const char* f, int l);
extern "C" void AnnotateIgnoreWritesBegin(char const* f, int l);

extern "C" void AnnotateIgnoreWritesEnd(const char* f, int l);
extern "C" void AnnotateIgnoreWritesEnd(char const* f, int l);

extern "C" void AnnotateIgnoreSyncBegin(const char* f, int l);
extern "C" void AnnotateIgnoreSyncBegin(char const* f, int l);

extern "C" void AnnotateIgnoreSyncEnd(const char* f, int l);
extern "C" void AnnotateIgnoreSyncEnd(char const* f, int l);

namespace folly {
namespace detail {
namespace {

FOLLY_CREATE_EXTERN_ACCESSOR(
annotate_rwlock_create_access_v, AnnotateRWLockCreate);
Expand Down Expand Up @@ -95,92 +94,48 @@ FOLLY_CREATE_EXTERN_ACCESSOR(
FOLLY_CREATE_EXTERN_ACCESSOR(
annotate_ignore_sync_end_access_v, AnnotateIgnoreSyncEnd);

void annotate_rwlock_create_impl(
void const volatile* const addr, char const* const f, int const l) {
constexpr auto fun = annotate_rwlock_create_access_v<kIsSanitizeThread>;
return fun ? fun(f, l, addr) : void();
}

void annotate_rwlock_create_static_impl(
void const volatile* const addr, char const* const f, int const l) {
constexpr auto fun =
annotate_rwlock_create_static_access_v<kIsSanitizeThread>;
return fun ? fun(f, l, addr) : void();
}

void annotate_rwlock_destroy_impl(
void const volatile* const addr, char const* const f, int const l) {
constexpr auto fun = annotate_rwlock_destroy_access_v<kIsSanitizeThread>;
return fun ? fun(f, l, addr) : void();
}

void annotate_rwlock_acquired_impl(
void const volatile* const addr,
annotate_rwlock_level const w,
char const* const f,
int const l) {
constexpr auto fun = annotate_rwlock_acquired_access_v<kIsSanitizeThread>;
return fun ? fun(f, l, addr, static_cast<long>(w)) : void();
}

void annotate_rwlock_try_acquired_impl(
void const volatile* const addr,
annotate_rwlock_level const w,
bool const result,
char const* const f,
int const l) {
if (result) {
annotate_rwlock_acquired(addr, w, f, l);
}
}

void annotate_rwlock_released_impl(
void const volatile* const addr,
annotate_rwlock_level const w,
char const* const f,
int const l) {
constexpr auto fun = annotate_rwlock_released_access_v<kIsSanitizeThread>;
return fun ? fun(f, l, addr, static_cast<long>(w)) : void();
}

void annotate_benign_race_sized_impl(
const volatile void* addr,
long size,
const char* desc,
const char* f,
int l) {
constexpr auto fun = annotate_benign_race_sized_access_v<kIsSanitizeThread>;
return fun ? fun(f, l, addr, size, desc) : void();
}

void annotate_ignore_reads_begin_impl(const char* f, int l) {
constexpr auto fun = annotate_ignore_reads_begin_access_v<kIsSanitizeThread>;
return fun ? fun(f, l) : void();
}

void annotate_ignore_reads_end_impl(const char* f, int l) {
constexpr auto fun = annotate_ignore_reads_end_access_v<kIsSanitizeThread>;
return fun ? fun(f, l) : void();
}

void annotate_ignore_writes_begin_impl(const char* f, int l) {
constexpr auto fun = annotate_ignore_writes_begin_access_v<kIsSanitizeThread>;
return fun ? fun(f, l) : void();
}

void annotate_ignore_writes_end_impl(const char* f, int l) {
constexpr auto fun = annotate_ignore_writes_end_access_v<kIsSanitizeThread>;
return fun ? fun(f, l) : void();
}

void annotate_ignore_sync_begin_impl(const char* f, int l) {
constexpr auto fun = annotate_ignore_sync_begin_access_v<kIsSanitizeThread>;
return fun ? fun(f, l) : void();
}

void annotate_ignore_sync_end_impl(const char* f, int l) {
constexpr auto fun = annotate_ignore_sync_end_access_v<kIsSanitizeThread>;
return fun ? fun(f, l) : void();
}
} // namespace

static constexpr auto const E = folly::kIsSanitizeThread;

namespace folly {
namespace detail {

FOLLY_STORAGE_CONSTEXPR annotate_rwlock_cd_t* const annotate_rwlock_create_v =
annotate_rwlock_create_access_v<E>;

FOLLY_STORAGE_CONSTEXPR annotate_rwlock_cd_t* const
annotate_rwlock_create_static_v = annotate_rwlock_create_static_access_v<E>;

FOLLY_STORAGE_CONSTEXPR annotate_rwlock_cd_t* const annotate_rwlock_destroy_v =
annotate_rwlock_destroy_access_v<E>;

FOLLY_STORAGE_CONSTEXPR annotate_rwlock_ar_t* const annotate_rwlock_acquired_v =
annotate_rwlock_acquired_access_v<E>;

FOLLY_STORAGE_CONSTEXPR annotate_rwlock_ar_t* const annotate_rwlock_released_v =
annotate_rwlock_released_access_v<E>;

FOLLY_STORAGE_CONSTEXPR annotate_benign_race_sized_t* const
annotate_benign_race_sized_v = annotate_benign_race_sized_access_v<E>;

FOLLY_STORAGE_CONSTEXPR annotate_ignore_t* const annotate_ignore_reads_begin_v =
annotate_ignore_reads_begin_access_v<E>;

FOLLY_STORAGE_CONSTEXPR annotate_ignore_t* const annotate_ignore_reads_end_v =
annotate_ignore_reads_end_access_v<E>;

FOLLY_STORAGE_CONSTEXPR annotate_ignore_t* const
annotate_ignore_writes_begin_v = annotate_ignore_writes_begin_access_v<E>;

FOLLY_STORAGE_CONSTEXPR annotate_ignore_t* const annotate_ignore_writes_end_v =
annotate_ignore_writes_end_access_v<E>;

FOLLY_STORAGE_CONSTEXPR annotate_ignore_t* const annotate_ignore_sync_begin_v =
annotate_ignore_sync_begin_access_v<E>;

FOLLY_STORAGE_CONSTEXPR annotate_ignore_t* const annotate_ignore_sync_end_v =
annotate_ignore_sync_end_access_v<E>;

} // namespace detail
} // namespace folly
Loading

0 comments on commit e83309f

Please sign in to comment.