Skip to content

Commit

Permalink
Merge pull request #24 from itzmeanjan/add-asan-ubsan
Browse files Browse the repository at this point in the history
Run Tests with ASAN, UBSAN
  • Loading branch information
itzmeanjan authored Dec 18, 2023
2 parents 04ac27b + 623f585 commit f62d289
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 42 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/test_ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,7 @@ jobs:
popd
- name: Execute Tests on ${{matrix.os}}
run: make -j
- name: Execute Tests with AddressSanitizer on ${{matrix.os}}
run: make asan_test -j
- name: Execute Tests with UndefinedBehaviourSanitizer on ${{matrix.os}}
run: make ubsan_test -j
44 changes: 37 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,27 @@ WARN_FLAGS = -Wall -Wextra -pedantic
OPT_FLAGS = -O3 -march=native
LINK_FLAGS = -flto
I_FLAGS = -I ./include
PERF_DEFS = -DCYCLES_PER_BYTE -DINSTRUCTIONS_PER_CYCLE
PERF_DEFS = -DCYCLES_PER_BYTE
ASAN_FLAGS = -g -O1 -fno-omit-frame-pointer -fno-optimize-sibling-calls -fsanitize=address # From https://clang.llvm.org/docs/AddressSanitizer.html
UBSAN_FLAGS = -g -O1 -fno-omit-frame-pointer -fno-optimize-sibling-calls -fsanitize=undefined -fsanitize=nullability # From https://clang.llvm.org/docs/UndefinedBehaviorSanitizer.html
# Note, *nullability* sanitization tests can't be run when compiled with GCC.

SRC_DIR = include
SHA3_SOURCES := $(wildcard $(SRC_DIR)/*.hpp)
BUILD_DIR = build

TEST_DIR = tests
TEST_SOURCES := $(wildcard $(TEST_DIR)/*.cpp)
TEST_BUILD_DIR := $(BUILD_DIR)/$(TEST_DIR)
ASAN_BUILD_DIR = $(TEST_BUILD_DIR)/asan
UBSAN_BUILD_DIR = $(TEST_BUILD_DIR)/ubsan
TEST_SOURCES := $(wildcard $(TEST_DIR)/*.cpp)
TEST_OBJECTS := $(addprefix $(TEST_BUILD_DIR)/, $(notdir $(patsubst %.cpp,%.o,$(TEST_SOURCES))))
ASAN_TEST_OBJECTS := $(addprefix $(ASAN_BUILD_DIR)/, $(notdir $(patsubst %.cpp,%.o,$(TEST_SOURCES))))
UBSAN_TEST_OBJECTS := $(addprefix $(UBSAN_BUILD_DIR)/, $(notdir $(patsubst %.cpp,%.o,$(TEST_SOURCES))))
TEST_LINK_FLAGS = -lgtest -lgtest_main
TEST_BINARY = $(TEST_BUILD_DIR)/test.out
ASAN_TEST_BINARY = $(ASAN_BUILD_DIR)/test.out
UBSAN_TEST_BINARY = $(UBSAN_BUILD_DIR)/test.out

BENCHMARK_DIR = benchmarks
BENCHMARK_SOURCES := $(wildcard $(BENCHMARK_DIR)/*.cpp)
Expand All @@ -30,27 +39,48 @@ PERF_BINARY = $(PERF_BUILD_DIR)/perf.out

all: test

$(BUILD_DIR):
$(TEST_BUILD_DIR):
mkdir -p $@

$(ASAN_BUILD_DIR):
mkdir -p $@

$(TEST_BUILD_DIR): $(BUILD_DIR)
$(UBSAN_BUILD_DIR):
mkdir -p $@

$(BENCHMARK_BUILD_DIR): $(BUILD_DIR)
$(BENCHMARK_BUILD_DIR):
mkdir -p $@

$(PERF_BUILD_DIR): $(BUILD_DIR)
$(PERF_BUILD_DIR):
mkdir -p $@

$(TEST_BUILD_DIR)/%.o: $(TEST_DIR)/%.cpp $(TEST_BUILD_DIR)
$(CXX) $(CXX_FLAGS) $(WARN_FLAGS) $(OPT_FLAGS) $(I_FLAGS) -c $< -o $@

$(ASAN_BUILD_DIR)/%.o: $(TEST_DIR)/%.cpp $(ASAN_BUILD_DIR)
$(CXX) $(CXX_FLAGS) $(WARN_FLAGS) $(ASAN_FLAGS) $(I_FLAGS) -c $< -o $@

$(UBSAN_BUILD_DIR)/%.o: $(TEST_DIR)/%.cpp $(UBSAN_BUILD_DIR)
$(CXX) $(CXX_FLAGS) $(WARN_FLAGS) $(UBSAN_FLAGS) $(I_FLAGS) -c $< -o $@

$(TEST_BINARY): $(TEST_OBJECTS)
$(CXX) $(OPT_FLAGS) $(LINK_FLAGS) $^ $(TEST_LINK_FLAGS) -o $@

$(ASAN_TEST_BINARY): $(ASAN_TEST_OBJECTS)
$(CXX) $(ASAN_FLAGS) $^ $(TEST_LINK_FLAGS) -o $@

$(UBSAN_TEST_BINARY): $(UBSAN_TEST_OBJECTS)
$(CXX) $(UBSAN_FLAGS) $^ $(TEST_LINK_FLAGS) -o $@

test: $(TEST_BINARY)
./$< --gtest_shuffle --gtest_random_seed=0

asan_test: $(ASAN_TEST_BINARY)
./$< --gtest_shuffle --gtest_random_seed=0

ubsan_test: $(UBSAN_TEST_BINARY)
./$< --gtest_shuffle --gtest_random_seed=0

$(BENCHMARK_BUILD_DIR)/%.o: $(BENCHMARK_DIR)/%.cpp $(BENCHMARK_BUILD_DIR)
$(CXX) $(CXX_FLAGS) $(WARN_FLAGS) $(OPT_FLAGS) $(I_FLAGS) -c $< -o $@

Expand All @@ -69,7 +99,7 @@ $(PERF_BINARY): $(PERF_OBJECTS)

perf: $(PERF_BINARY)
# Must build google-benchmark with libPFM, follow https://gist.github.com/itzmeanjan/05dc3e946f635d00c5e0b21aae6203a7
./$< --benchmark_min_warmup_time=.1 --benchmark_enable_random_interleaving=true --benchmark_repetitions=8 --benchmark_min_time=0.1s --benchmark_counters_tabular=true --benchmark_display_aggregates_only=true --benchmark_perf_counters=CYCLES,INSTRUCTIONS
./$< --benchmark_min_warmup_time=.1 --benchmark_enable_random_interleaving=true --benchmark_repetitions=8 --benchmark_min_time=0.1s --benchmark_counters_tabular=true --benchmark_display_aggregates_only=true --benchmark_perf_counters=CYCLES

.PHONY: format clean

Expand Down
20 changes: 0 additions & 20 deletions benchmarks/bench_hashing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,6 @@ bench_sha3_224(benchmark::State& state)
#ifdef CYCLES_PER_BYTE
state.counters["CYCLES/ BYTE"] = state.counters["CYCLES"] / bytes_processed;
#endif

#ifdef INSTRUCTIONS_PER_CYCLE
const double ipc = state.counters["INSTRUCTIONS"] / state.counters["CYCLES"];
state.counters["INSTRUCTIONS/ CYCLE"] = ipc;
#endif
}

// Benchmarks SHA3-256 hash function with variable length input message.
Expand Down Expand Up @@ -72,11 +67,6 @@ bench_sha3_256(benchmark::State& state)
#ifdef CYCLES_PER_BYTE
state.counters["CYCLES/ BYTE"] = state.counters["CYCLES"] / bytes_processed;
#endif

#ifdef INSTRUCTIONS_PER_CYCLE
const double ipc = state.counters["INSTRUCTIONS"] / state.counters["CYCLES"];
state.counters["INSTRUCTIONS/ CYCLE"] = ipc;
#endif
}

// Benchmarks SHA3-384 hash function with variable length input message.
Expand Down Expand Up @@ -109,11 +99,6 @@ bench_sha3_384(benchmark::State& state)
#ifdef CYCLES_PER_BYTE
state.counters["CYCLES/ BYTE"] = state.counters["CYCLES"] / bytes_processed;
#endif

#ifdef INSTRUCTIONS_PER_CYCLE
const double ipc = state.counters["INSTRUCTIONS"] / state.counters["CYCLES"];
state.counters["INSTRUCTIONS/ CYCLE"] = ipc;
#endif
}

// Benchmarks SHA3-512 hash function with variable length input message.
Expand Down Expand Up @@ -146,11 +131,6 @@ bench_sha3_512(benchmark::State& state)
#ifdef CYCLES_PER_BYTE
state.counters["CYCLES/ BYTE"] = state.counters["CYCLES"] / bytes_processed;
#endif

#ifdef INSTRUCTIONS_PER_CYCLE
const double ipc = state.counters["INSTRUCTIONS"] / state.counters["CYCLES"];
state.counters["INSTRUCTIONS/ CYCLE"] = ipc;
#endif
}

BENCHMARK(bench_sha3_224)
Expand Down
5 changes: 0 additions & 5 deletions benchmarks/bench_keccak.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,6 @@ bench_keccak_permutation(benchmark::State& state)
#ifdef CYCLES_PER_BYTE
state.counters["CYCLES/ BYTE"] = state.counters["CYCLES"] / bytes_processed;
#endif

#ifdef INSTRUCTIONS_PER_CYCLE
const double ipc = state.counters["INSTRUCTIONS"] / state.counters["CYCLES"];
state.counters["INSTRUCTIONS/ CYCLE"] = ipc;
#endif
}

BENCHMARK(bench_keccak_permutation)->Name("keccak-p[1600, 24]");
10 changes: 0 additions & 10 deletions benchmarks/bench_xof.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,6 @@ bench_shake128(benchmark::State& state)
#ifdef CYCLES_PER_BYTE
state.counters["CYCLES/ BYTE"] = state.counters["CYCLES"] / bytes_processed;
#endif

#ifdef INSTRUCTIONS_PER_CYCLE
const double ipc = state.counters["INSTRUCTIONS"] / state.counters["CYCLES"];
state.counters["INSTRUCTIONS/ CYCLE"] = ipc;
#endif
}

// Benchmarks SHAKE-256 extendable output function with variable length input
Expand Down Expand Up @@ -78,11 +73,6 @@ bench_shake256(benchmark::State& state)
#ifdef CYCLES_PER_BYTE
state.counters["CYCLES/ BYTE"] = state.counters["CYCLES"] / bytes_processed;
#endif

#ifdef INSTRUCTIONS_PER_CYCLE
const double ipc = state.counters["INSTRUCTIONS"] / state.counters["CYCLES"];
state.counters["INSTRUCTIONS/ CYCLE"] = ipc;
#endif
}

BENCHMARK(bench_shake128)
Expand Down

0 comments on commit f62d289

Please sign in to comment.