From 128dd3eb1f30fff24a07d2aa2b3e3ed72703670a Mon Sep 17 00:00:00 2001 From: Justin Traglia <95511699+jtraglia@users.noreply.github.com> Date: Mon, 13 Feb 2023 12:20:21 -0600 Subject: [PATCH] Disable compiler optimizations for tests (#133) --- .github/workflows/ckzg-test.yml | 2 +- src/Makefile | 13 ++++++++----- src/test_c_kzg_4844.c | 20 ++++++++++++++++++++ 3 files changed, 29 insertions(+), 6 deletions(-) diff --git a/.github/workflows/ckzg-test.yml b/.github/workflows/ckzg-test.yml index af9e62e..dc0b27c 100644 --- a/.github/workflows/ckzg-test.yml +++ b/.github/workflows/ckzg-test.yml @@ -32,7 +32,7 @@ jobs: - name: Generate coverage report run: | cd src - make test_cov + make coverage - name: Save coverage report uses: actions/upload-artifact@v3 with: diff --git a/src/Makefile b/src/Makefile index 1662ad4..69271d2 100644 --- a/src/Makefile +++ b/src/Makefile @@ -13,6 +13,9 @@ CFLAGS += -I../inc CFLAGS += -Wall -Wextra -Werror -O2 CFLAGS += -DFIELD_ELEMENTS_PER_BLOB=$(FIELD_ELEMENTS_PER_BLOB) +# Disable optimizations. Put after $CFLAGS. +NO_OPTIMIZE = -O0 + # Compiler flags for including blst. Put after source files. BLST = -L../lib -lblst @@ -45,13 +48,13 @@ all: c_kzg_4844.o @$(CC) $(CFLAGS) -c $< test_c_kzg_4844: test_c_kzg_4844.c c_kzg_4844.c - @$(CC) $(CFLAGS) -o $@ $< $(BLST) + @$(CC) $(CFLAGS) $(NO_OPTIMIZE) -o $@ $< $(BLST) test_c_kzg_4844_cov: test_c_kzg_4844.c c_kzg_4844.c - @$(CC) $(CFLAGS) $(COVERAGE) -o $@ $< $(BLST) + @$(CC) $(CFLAGS) $(NO_OPTIMIZE) $(COVERAGE) -o $@ $< $(BLST) test_c_kzg_4844_prof: test_c_kzg_4844.c c_kzg_4844.c - @$(CC) $(CFLAGS) $(PROFILE) -o $@ $< $(BLST) $(PROFILER) + @$(CC) $(CFLAGS) $(NO_OPTIMIZE) $(PROFILE) -o $@ $< $(BLST) $(PROFILER) .PHONY: blst blst: @@ -64,8 +67,8 @@ blst: test: test_c_kzg_4844 @./test_c_kzg_4844 -.PHONY: test_cov -test_cov: test_c_kzg_4844_cov +.PHONY: coverage +coverage: test_c_kzg_4844_cov @LLVM_PROFILE_FILE="ckzg.profraw" ./$< @$(XCRUN) llvm-profdata merge --sparse ckzg.profraw -o ckzg.profdata @$(XCRUN) llvm-cov show --instr-profile=ckzg.profdata --format=html \ diff --git a/src/test_c_kzg_4844.c b/src/test_c_kzg_4844.c index 954d14c..cf97022 100644 --- a/src/test_c_kzg_4844.c +++ b/src/test_c_kzg_4844.c @@ -105,6 +105,15 @@ static void test_c_kzg_malloc__fails_size_equal_to_zero(void) { ASSERT_EQUALS(ptr, NULL); } +static void test_c_kzg_malloc__fails_too_big(void) { + C_KZG_RET ret; + void *ptr = NULL; + + ret = c_kzg_malloc(&ptr, UINT64_MAX); + ASSERT_EQUALS(ret, C_KZG_MALLOC); + ASSERT_EQUALS(ptr, NULL); +} + static void test_c_kzg_calloc__succeeds_size_greater_than_zero(void) { C_KZG_RET ret; void *ptr = NULL; @@ -133,6 +142,15 @@ static void test_c_kzg_calloc__fails_size_equal_to_zero(void) { ASSERT_EQUALS(ptr, NULL); } +static void test_c_kzg_calloc__fails_too_big(void) { + C_KZG_RET ret; + void *ptr = NULL; + + ret = c_kzg_calloc(&ptr, UINT64_MAX, UINT64_MAX); + ASSERT_EQUALS(ret, C_KZG_MALLOC); + ASSERT_EQUALS(ptr, NULL); +} + /////////////////////////////////////////////////////////////////////////////// // Tests for blob_to_kzg_commitment /////////////////////////////////////////////////////////////////////////////// @@ -760,9 +778,11 @@ int main(void) { setup(); RUN(test_c_kzg_malloc__succeeds_size_greater_than_zero); RUN(test_c_kzg_malloc__fails_size_equal_to_zero); + RUN(test_c_kzg_malloc__fails_too_big); RUN(test_c_kzg_calloc__succeeds_size_greater_than_zero); RUN(test_c_kzg_calloc__fails_size_equal_to_zero); RUN(test_c_kzg_calloc__fails_count_equal_to_zero); + RUN(test_c_kzg_calloc__fails_too_big); RUN(test_blob_to_kzg_commitment__succeeds_x_less_than_modulus); RUN(test_blob_to_kzg_commitment__fails_x_equal_to_modulus); RUN(test_blob_to_kzg_commitment__fails_x_greater_than_modulus);