Skip to content

Commit

Permalink
a few sanitizer fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
martinus committed Sep 17, 2021
1 parent 3a6f5b3 commit 96cf4ba
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 11 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ if (NANOBENCH_STANDALONE_PROJECT)

# we have to globally set the property here, so it actually works https://cmake.org/pipermail/cmake/2010-March/036020.html
set_source_files_properties(
src/test/tutorial_fast_v1.cpp
src/test/tutorial_fast_v1.cpp
src/test/tutorial_fast_v2.cpp
PROPERTIES COMPILE_FLAGS "-fno-sanitize=integer")

Expand Down
19 changes: 11 additions & 8 deletions src/include/nanobench.h
Original file line number Diff line number Diff line change
Expand Up @@ -2404,6 +2404,14 @@ class LinuxPerformanceCounters {
return (a + divisor / 2) / divisor;
}

ANKERL_NANOBENCH_NO_SANITIZE("integer", "undefined")
static inline uint32_t mix(uint32_t x) noexcept {
x ^= x << 13;
x ^= x >> 17;
x ^= x << 5;
return x;
}

template <typename Op>
ANKERL_NANOBENCH_NO_SANITIZE("integer", "undefined")
void calibrate(Op&& op) {
Expand Down Expand Up @@ -2443,15 +2451,10 @@ class LinuxPerformanceCounters {
uint64_t const numIters = 100000U + (std::random_device{}() & 3);
uint64_t n = numIters;
uint32_t x = 1234567;
auto fn = [&]() {
x ^= x << 13;
x ^= x >> 17;
x ^= x << 5;
};

beginMeasure();
while (n-- > 0) {
fn();
x = mix(x);
}
endMeasure();
detail::doNotOptimizeAway(x);
Expand All @@ -2461,8 +2464,8 @@ class LinuxPerformanceCounters {
beginMeasure();
while (n-- > 0) {
// we now run *twice* so we can easily calculate the overhead
fn();
fn();
x = mix(x);
x = mix(x);
}
endMeasure();
detail::doNotOptimizeAway(x);
Expand Down
2 changes: 1 addition & 1 deletion src/scripts/all.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function build() {
${NICE} cmake --build .
rm -f ubsan.log*

UBSAN_OPTIONS=print_stacktrace=1:log_path=ubsan.log:suppressions=${ROOTDIR}/ubsan.supp ${NICE} ./nb
UBSAN_OPTIONS=print_stacktrace=1:log_path=ubsan.log:suppressions=${ROOTDIR}/ubsan.supp ./nb
if ls ubsan.log* 1> /dev/null 2>&1; then
cat ubsan.log*
exit 1
Expand Down
9 changes: 8 additions & 1 deletion src/test/example_random_number_generators.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
# if defined(__GNUC__) || defined(__clang__)
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wpedantic"
using uint128_t = unsigned __int128;
using uint128_t = unsigned __int128;
# pragma GCC diagnostic pop
# endif
#elif (defined(_MSC_VER) && SIZE_MAX == UINT64_MAX)
Expand Down Expand Up @@ -99,6 +99,7 @@ class NasamRng {
NasamRng(uint64_t seed) noexcept
: mState(seed) {}

ANKERL_NANOBENCH_NO_SANITIZE("integer", "undefined")
uint64_t operator()() noexcept {
auto x = mState++;

Expand All @@ -115,6 +116,7 @@ class NasamRng {
private:
// rotate right
template <typename T>
ANKERL_NANOBENCH_NO_SANITIZE("integer", "undefined")
static T rotr(T x, size_t k) {
return (x >> k) | (x << (8U * sizeof(T) - k));
}
Expand Down Expand Up @@ -143,6 +145,7 @@ class Sfc4 {
}
}

ANKERL_NANOBENCH_NO_SANITIZE("integer", "undefined")
uint64_t operator()() noexcept {
uint64_t tmp = mA + mB + mCounter++;
mA = mB ^ (mB >> 11U);
Expand All @@ -152,6 +155,7 @@ class Sfc4 {
}

private:
ANKERL_NANOBENCH_NO_SANITIZE("integer", "undefined")
static constexpr uint64_t rotl(uint64_t x, unsigned k) noexcept {
return (x << k) | (x >> (64U - k));
}
Expand Down Expand Up @@ -193,6 +197,7 @@ class RomuTrio {
}

private:
ANKERL_NANOBENCH_NO_SANITIZE("integer", "undefined")
static constexpr uint64_t rotl(uint64_t x, unsigned k) noexcept {
return (x << k) | (x >> (64U - k));
}
Expand Down Expand Up @@ -229,6 +234,7 @@ class RomuDuo {
}

private:
ANKERL_NANOBENCH_NO_SANITIZE("integer", "undefined")
static constexpr uint64_t rotl(uint64_t x, unsigned k) noexcept {
return (x << k) | (x >> (64U - k));
}
Expand Down Expand Up @@ -266,6 +272,7 @@ class RomuDuoJr {
}

private:
ANKERL_NANOBENCH_NO_SANITIZE("integer", "undefined")
static constexpr uint64_t rotl(uint64_t x, unsigned k) noexcept {
return (x << k) | (x >> (64U - k));
}
Expand Down
1 change: 1 addition & 0 deletions src/test/unit_romutrio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

namespace {

ANKERL_NANOBENCH_NO_SANITIZE("integer", "undefined")
constexpr uint64_t rotl(uint64_t x, unsigned k) noexcept {
return (x << k) | (x >> (64U - k));
}
Expand Down
3 changes: 3 additions & 0 deletions ubsan.supp
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# https://github.com/gcc-mirror/gcc/blob/master/libsanitizer/ubsan/ubsan_checks.inc
implicit-integer-sign-change:random.tcc
signed-integer-overflow:random.tcc
unsigned-integer-overflow:random.tcc
shift-base:random.tcc
shift-exponent:random.tcc
unsigned-integer-overflow:random.h
unsigned-integer-overflow:example_random_number_generators.cpp

0 comments on commit 96cf4ba

Please sign in to comment.