aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMikhail Romanko <me@blankhex.com>2025-01-29 09:19:34 +0300
committerMikhail Romanko <me@blankhex.com>2025-01-29 09:19:34 +0300
commit6ede63e18fcbbdc9457b6ac7b5d7af9b673ec657 (patch)
treebb5264dd1410e9d3852cd3efda1a32c78010abcf
parent47c21a2035d7c80f0eee7d69499c921b131859e3 (diff)
downloadbhlib-6ede63e18fcbbdc9457b6ac7b5d7af9b673ec657.tar.gz
Add Asan profile, fix multiple bugs.
Added Asan profile to help catch and fix various bugs (and indeed, there were few of them). Additionally, fixed bhunit macro to process arguments only once.
-rw-r--r--CMakeLists.txt38
-rw-r--r--src/io.c3
-rwxr-xr-xtest/CMakeLists.txt13
-rw-r--r--test/src/testalgo.c2
-rw-r--r--test/src/testfile.c8
-rw-r--r--test/src/testhashmap.c2
-rwxr-xr-xunit/CMakeLists.txt2
-rwxr-xr-xunit/include/bh/unit.h31
8 files changed, 78 insertions, 21 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 761929d..e7f39c6 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -5,6 +5,42 @@ project(bhlib LANGUAGES C)
set(CMAKE_C_STANDARD 90)
set(CMAKE_C_STANDARD_REQUIRED ON)
+# Asan profile
+get_property(isMultiConfig GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)
+
+if(isMultiConfig)
+ if(NOT "Asan" IN_LIST CMAKE_CONFIGURATION_TYPES)
+ list(APPEND CMAKE_CONFIGURATION_TYPES Asan)
+ endif()
+else()
+ set(allowedBuildTypes Asan Debug Release RelWithDebInfo MinSizeRel)
+ set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "${allowedBuildTypes}")
+
+ if(CMAKE_BUILD_TYPE AND NOT CMAKE_BUILD_TYPE IN_LIST allowedBuildTypes)
+ message(FATAL_ERROR "Invalid build type: ${CMAKE_BUILD_TYPE}")
+ endif()
+endif()
+
+set(CMAKE_C_FLAGS_ASAN
+ "${CMAKE_C_FLAGS_DEBUG} -fsanitize=address,undefined -fno-omit-frame-pointer" CACHE STRING
+ "Flags used by the C compiler for Asan build type or configuration." FORCE
+)
+
+set(CMAKE_CXX_FLAGS_ASAN
+ "${CMAKE_CXX_FLAGS_DEBUG} -fsanitize=address,undefined -fno-omit-frame-pointer" CACHE STRING
+ "Flags used by the C++ compiler for Asan build type or configuration." FORCE
+)
+
+set(CMAKE_EXE_LINKER_FLAGS_ASAN
+ "${CMAKE_EXE_LINKER_FLAGS_DEBUG} -fsanitize=address,undefined -static-libasan" CACHE STRING
+ "Linker flags to be used to create executables for Asan build type." FORCE
+)
+
+set(CMAKE_SHARED_LINKER_FLAGS_ASAN
+ "${CMAKE_SHARED_LINKER_FLAGS_DEBUG} -fsanitize=address,undefined -static-libasan" CACHE STRING
+ "Linker lags to be used to create shared libraries for Asan build type." FORCE
+)
+
# Project includes
include(CheckIPOSupported)
include(CheckIncludeFile)
@@ -84,7 +120,7 @@ target_link_libraries(bhlib PUBLIC m)
if(MSVC)
target_compile_options(bhlib PRIVATE /W4 /WX)
else()
- target_compile_options(bhlib PRIVATE -Wall -Wextra -Wpedantic -Werror)
+ target_compile_options(bhlib PRIVATE -Wall -Wextra -Wpedantic -Werror -fstrict-aliasing)
endif()
# Coverage
diff --git a/src/io.c b/src/io.c
index b005307..7fc04cf 100644
--- a/src/io.c
+++ b/src/io.c
@@ -46,6 +46,9 @@ void bh_io_free(bh_io_t *io)
/* Call the IO device destruction handler */
io->func(io + 1, BH_IO_DESTROY_CB, NULL, NULL);
+
+ /* Deallocate object */
+ free(io);
}
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
index a95e1bc..19d1cae 100755
--- a/test/CMakeLists.txt
+++ b/test/CMakeLists.txt
@@ -2,9 +2,6 @@ set(CMAKE_C_STANDARD 90)
set(CMAKE_C_STANDARD_REQUIRED ON)
set(CMAKE_C_EXTENSIONS OFF)
-# Enable warnings and pedantics
-set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -pedantic")
-
# Enable testing
include(CTest)
enable_testing()
@@ -13,10 +10,20 @@ enable_testing()
file(GLOB TEST_FILES "src/*.c")
foreach(TEST_FILENAME ${TEST_FILES})
+ # Add test
get_filename_component(TEST_NAME ${TEST_FILENAME} NAME_WE)
add_executable("${TEST_NAME}" ${TEST_FILENAME})
target_link_libraries("${TEST_NAME}" bhlib bhunit)
add_test(NAME "${TEST_NAME}" COMMAND "${TEST_NAME}")
+
+ # Enable all compiler warnings
+ if(MSVC)
+ target_compile_options("${TEST_NAME}" PRIVATE /W4 /WX)
+ else()
+ target_compile_options("${TEST_NAME}" PRIVATE -Wall -Wextra -Wpedantic -Werror -fstrict-aliasing)
+ endif()
+
+ # Enable coverage
if(COVERAGE)
target_compile_options("${TEST_NAME}" PRIVATE -coverage)
target_link_options("${TEST_NAME}" PRIVATE -coverage)
diff --git a/test/src/testalgo.c b/test/src/testalgo.c
index be762f4..1eb8460 100644
--- a/test/src/testalgo.c
+++ b/test/src/testalgo.c
@@ -480,4 +480,4 @@ int main(int argc, char **argv)
bh_unit_add("heap_replace", check_heap_replace);
return bh_unit_run();
-} \ No newline at end of file
+}
diff --git a/test/src/testfile.c b/test/src/testfile.c
index 0f006a4..3cf0581 100644
--- a/test/src/testfile.c
+++ b/test/src/testfile.c
@@ -121,7 +121,7 @@ static int check_normal(void)
BH_VERIFY(bh_io_read(io, buffer, 5, &actual) == BH_OK);
BH_VERIFY(actual == 5);
BH_VERIFY(memcmp(buffer, "67890", 5) == 0);
- bh_io_close(io);
+ bh_io_free(io);
/* Check operations for read and write access */
BH_VERIFY((io = bh_file_new(FILENAME1)) != NULL);
@@ -226,7 +226,7 @@ static int check_truncate(void)
BH_VERIFY(bh_io_seek(io, 0, BH_IO_SEEK_SET) == BH_OK);
BH_VERIFY(bh_io_read(io, buffer, sizeof(buffer), &actual) == BH_OK);
BH_VERIFY(actual == 0);
- bh_io_close(io);
+ bh_io_free(io);
/* Check operations for read and write access */
BH_VERIFY((io = bh_file_new(FILENAME1)) != NULL);
@@ -321,7 +321,7 @@ static int check_exist(void)
BH_VERIFY(bh_io_read(io, buffer, 5, &actual) == BH_OK);
BH_VERIFY(actual == 5);
BH_VERIFY(memcmp(buffer, "67890", 5) == 0);
- bh_io_close(io);
+ bh_io_free(io);
/* Check operations for read and write access */
BH_VERIFY((io = bh_file_new(FILENAME1)) != NULL);
@@ -627,4 +627,4 @@ int main(int argc,
bh_unit_add("size", check_size);
return bh_unit_run();
-} \ No newline at end of file
+}
diff --git a/test/src/testhashmap.c b/test/src/testhashmap.c
index 9728977..b166e5a 100644
--- a/test/src/testhashmap.c
+++ b/test/src/testhashmap.c
@@ -157,7 +157,7 @@ static int lookup(void)
BH_VERIFY(bh_hashmap_at(hashmap, BH_INT2PTR(i * 4), NULL) == BH_OK);
BH_VERIFY(bh_hashmap_at(hashmap, BH_INT2PTR(i * 4), &value) == BH_OK);
- BH_VERIFY(BH_PTR2INT(value) == i);
+ BH_VERIFY(BH_PTR2INT(value) == (int)i);
}
/* Lookup non-existing elements */
diff --git a/unit/CMakeLists.txt b/unit/CMakeLists.txt
index 66ae482..28d7ce9 100755
--- a/unit/CMakeLists.txt
+++ b/unit/CMakeLists.txt
@@ -25,5 +25,5 @@ target_include_directories(bhunit PUBLIC include)
if(MSVC)
target_compile_options(bhunit PRIVATE /W4 /WX)
else()
- target_compile_options(bhunit PRIVATE -Wall -Wextra -Wpedantic -Werror)
+ target_compile_options(bhunit PRIVATE -Wall -Wextra -Wpedantic -Werror -fstrict-aliasing)
endif() \ No newline at end of file
diff --git a/unit/include/bh/unit.h b/unit/include/bh/unit.h
index 85785fd..6667ca4 100755
--- a/unit/include/bh/unit.h
+++ b/unit/include/bh/unit.h
@@ -2,26 +2,37 @@
#define BH_UNIT_H
#include <stdio.h>
+#include <math.h>
typedef int (*bh_unit_cb_t)(void);
#define BH_VERIFY(e) \
- do { if (!(e)) { \
- printf("%s:%d\t%s\n", __FILE__, __LINE__, #e); \
- return -1; \
- } } while(0)
+ do { \
+ if (!(e)) { \
+ printf("%s:%d\t%s\n", __FILE__, __LINE__, #e); \
+ return -1; \
+ } \
+ } while(0)
#define BH_FAIL(msg) \
- do { printf("%s:%d\t%s\n", __FILE__, __LINE__, msg); \
- return -1; } while(0)
+ do { \
+ printf("%s:%d\t%s\n", __FILE__, __LINE__, msg); \
+ return -1; \
+ } while(0)
#define BH_VERIFY_DELTA(x, y, e) \
- do { if ((((x)>(y))?((x)-(y)):((y)-(x)))>(e)) { \
- printf("%s:%d\t%s\n", __FILE__, __LINE__, #x " == " #y); \
- return -1; \
- } while (0)
+ do { \
+ double BH_VERIFY_DELTA = (x)-(y); \
+ if (BH_VERIFY_DELTA < 0.0) \
+ BH_VERIFY_DELTA = -BH_VERIFY_DELTA; \
+ if (BH_VERIFY_DELTA > (e)) { \
+ printf("%s:%d\t%s (differs by %f)\n", __FILE__, __LINE__, #x " == " #y, BH_VERIFY_DELTA); \
+ return -1; \
+ } \
+ } while(0)
+
void bh_unit_add(const char *name, bh_unit_cb_t func);
int bh_unit_run(void);