Skip to content

Commit

Permalink
Add sanitizer support and fix leaks in tests (#167)
Browse files Browse the repository at this point in the history
  • Loading branch information
tezc authored Jun 22, 2023
1 parent 7592662 commit a634de6
Show file tree
Hide file tree
Showing 14 changed files with 1,119 additions and 102 deletions.
16 changes: 16 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,19 @@ jobs:
make gcov
- name: Upload to codecov
uses: codecov/codecov-action@v2

address-sanitizer:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Run tests
run: |
make tests SANITIZER=address
undefined-sanitizer:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Run tests
run: |
make tests SANITIZER=undefined
22 changes: 22 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
# Tests :
# mkdir build && cd build && cmake .. -DCMAKE_BUILD_TYPE=Debug
# make && make tests_full
# SANITIZER :
# mkdir build && cd build && cmake .. -DSANITIZER=address
# make && make check

project(raft C)
cmake_minimum_required(VERSION 3.7.2)
Expand Down Expand Up @@ -107,6 +110,25 @@ set(RAFT_SOURCE_FILES
src/raft_server_properties.c)

add_library(raft STATIC ${RAFT_SOURCE_FILES})

if (SANITIZER)
if ("${SANITIZER}" STREQUAL "address")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address")
add_link_options(-fsanitize=address)
elseif ("${SANITIZER}" STREQUAL "undefined")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=undefined")
add_link_options(-fsanitize=undefined)
elseif ("${SANITIZER}" STREQUAL "thread")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=thread")
add_link_options(-fsanitize=thread)
else ()
message(FATAL_ERROR "Unknown sanitizer: ${SANITIZER}")
endif ()

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fno-sanitize-recover=all -fno-omit-frame-pointer")
message(STATUS "Using sanitizer: ${SANITIZER}")
endif ()

target_compile_options(raft PRIVATE -g -Wall -Wextra -pedantic)
target_include_directories(raft PRIVATE include)

Expand Down
22 changes: 22 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,28 @@ INC = -I include
GCOV_CFLAGS = -fprofile-arcs -ftest-coverage
SHELL = /bin/bash
CFLAGS += -Iinclude -fno-omit-frame-pointer -fno-common -fsigned-char -g -O2 -fPIC

ifdef SANITIZER
ifeq ($(SANITIZER),address)
MALLOC = libc
CFLAGS += -fsanitize=address -fno-sanitize-recover=all -fno-omit-frame-pointer
LDFLAGS += -fsanitize=address
else
ifeq ($(SANITIZER),undefined)
MALLOC = libc
CFLAGS += -fsanitize=undefined -fno-sanitize-recover=all -fno-omit-frame-pointer
LDFLAGS += -fsanitize=undefined
else
ifeq ($(SANITIZER),thread)
CFLAGS += -fsanitize=thread -fno-sanitize-recover=all -fno-omit-frame-pointer
LDFLAGS += -fsanitize=thread
else
$(error "unknown sanitizer=${SANITIZER}")
endif
endif
endif
endif

ifeq ($(COVERAGE), 1)
CFLAGS += $(GCOV_CFLAGS)
TEST_CFLAGS = $(CFLAGS)
Expand Down
1 change: 1 addition & 0 deletions src/raft_log.c
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ int raft_log_poll(raft_log_t *me, raft_entry_t **etyp)

raft_entry_release(me->entries[me->front]);

me->entries[me->front] = NULL;
me->front++;
me->front = me->front % me->size;
me->count--;
Expand Down
20 changes: 20 additions & 0 deletions tests/CuTest.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ CuString* CuStringNew(void)
return str;
}

void CuStringFree(CuString *cu)
{
free(cu->buffer);
free(cu);
}

void CuStringResize(CuString* str, int newSize)
{
str->buffer = (char*) realloc(str->buffer, sizeof(char) * newSize);
Expand Down Expand Up @@ -119,6 +125,12 @@ CuTest* CuTestNew(const char* name, TestFunction function)
return tc;
}

void CuTestFree(CuTest *test)
{
free(test->name);
free(test);
}

void CuTestRun(CuTest* tc)
{
#if 0 /* debugging */
Expand Down Expand Up @@ -236,6 +248,14 @@ CuSuite* CuSuiteNew(void)
return testSuite;
}

void CuSuiteFree(CuSuite *testSuite)
{
for (int i = 0; i < testSuite->count; i++) {
CuTestFree(testSuite->list[i]);
}
free(testSuite);
}

void CuSuiteAdd(CuSuite* testSuite, CuTest *testCase)
{
assert(testSuite->count < MAX_TEST_CASES);
Expand Down
7 changes: 5 additions & 2 deletions tests/CuTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ typedef struct

void CuStringInit(CuString* str);
CuString* CuStringNew(void);
void CuStringFree(CuString *cu);
void CuStringRead(CuString* str, const char* path);
void CuStringAppend(CuString* str, const char* text);
void CuStringAppendChar(CuString* str, char ch);
Expand All @@ -39,16 +40,17 @@ typedef void (*TestFunction)(CuTest *);

struct CuTest
{
const char* name;
char *name;
TestFunction function;
int failed;
int ran;
const char* message;
const char *message;
jmp_buf *jumpBuf;
};

void CuTestInit(CuTest* t, const char* name, TestFunction function);
CuTest* CuTestNew(const char* name, TestFunction function);
void CuTestFree(CuTest *test);
void CuTestRun(CuTest* tc);

/* Internal versions of assert functions -- use the public versions */
Expand Down Expand Up @@ -104,6 +106,7 @@ typedef struct

void CuSuiteInit(CuSuite* testSuite);
CuSuite* CuSuiteNew(void);
void CuSuiteFree(CuSuite *testSuite);
void CuSuiteAdd(CuSuite* testSuite, CuTest *testCase);
void CuSuiteAddSuite(CuSuite* testSuite, CuSuite* testSuite2);
void CuSuiteRun(CuSuite* testSuite);
Expand Down
7 changes: 5 additions & 2 deletions tests/helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@

static raft_entry_t *__MAKE_ENTRY(int id, raft_term_t term, const char *data)
{
raft_entry_t *ety = raft_entry_new(data ? strlen(data) : 0);
raft_entry_t *ety = raft_entry_new(data ? strlen(data) + 1 : 0);
ety->id = id;
ety->term = term;
if (data) {
memcpy(ety->data, data, strlen(data));
memcpy(ety->data, data, strlen(data) + 1);
}
return ety;
}
Expand Down Expand Up @@ -36,6 +36,7 @@ static void __RAFT_APPEND_ENTRY(void *r, int id, raft_term_t term, const char *d
{
raft_entry_t *e = __MAKE_ENTRY(id, term, data);
raft_append_entry(r, e);
raft_entry_release(e);
raft_node_set_match_idx(raft_get_my_node(r), raft_get_current_idx(r));
}

Expand All @@ -45,6 +46,7 @@ static void __RAFT_APPEND_ENTRIES_SEQ_ID(void *r, int count, int id, raft_term_t
for (i = 0; i < count; i++) {
raft_entry_t *e = __MAKE_ENTRY(id++, term, data);
raft_append_entry(r, e);
raft_entry_release(e);
}
}

Expand All @@ -54,6 +56,7 @@ static void __RAFT_APPEND_ENTRIES_SEQ_ID_TERM(void *r, int count, int id, raft_t
for (i = 0; i < count; i++) {
raft_entry_t *e = __MAKE_ENTRY(id++, term++, data);
raft_append_entry(r, e);
raft_entry_release(e);
}
}

Expand Down
12 changes: 11 additions & 1 deletion tests/mock_send_functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,9 @@ void* sender_poll_msg_data(void* s)
{
sender_t* me = s;
msg_t* msg = llqueue_poll(me->outbox);
return NULL != msg ? msg->data : NULL;
void *data = msg ? msg->data : NULL;
free(msg);
return data;
}

void sender_set_raft(void* s, void* r)
Expand All @@ -164,10 +166,15 @@ void sender_poll_msgs(void* s)
{
case RAFT_APPENDENTRIES_REQ:
{
raft_appendentries_req_t *ae = m->data;
raft_appendentries_resp_t response;
raft_recv_appendentries(me->raft, m->sender, m->data, &response);
__append_msg(me, &response, RAFT_APPENDENTRIES_RESP,
sizeof(response), m->sender, me->raft);
for (raft_index_t i = 0; i < ae->n_entries; i++) {
raft_entry_release(ae->entries[i]);
}
free(ae->entries);
}
break;
case RAFT_APPENDENTRIES_RESP:
Expand Down Expand Up @@ -199,5 +206,8 @@ void sender_poll_msgs(void* s)
#endif
break;
}

free(m->data);
free(m);
}
}
Loading

0 comments on commit a634de6

Please sign in to comment.