Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

github: Test with musl #682

Merged
merged 19 commits into from
Sep 2, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/build-and-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ jobs:
raft-core-integration-test \
raft-core-unit-test \
raft-uv-integration-test \
raft-addrinfo-integration-test \
raft-uv-unit-test

- name: Test
Expand Down
60 changes: 36 additions & 24 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ basic_dqlite_sources = \
src/lib/addr.c \
src/lib/buffer.c \
src/lib/fs.c \
src/lib/sm.c \
src/lib/threadpool.c \
src/lib/transport.c \
src/logger.c \
src/message.c \
Expand All @@ -60,7 +58,6 @@ basic_dqlite_sources = \
src/roles.c \
src/server.c \
src/stmt.c \
src/tracing.c \
src/transport.c \
src/translate.c \
src/tuple.c \
Expand All @@ -71,6 +68,7 @@ lib_LTLIBRARIES = libdqlite.la
libdqlite_la_CFLAGS = $(AM_CFLAGS) -fvisibility=hidden -DRAFT_API=''
libdqlite_la_LDFLAGS = $(AM_LDFLAGS) -version-info 0:1:0
libdqlite_la_SOURCES = $(basic_dqlite_sources)
libdqlite_la_LIBADD = libcommon.la

if BUILD_RAFT_ENABLED
libraft_la_SOURCES = \
Expand Down Expand Up @@ -128,9 +126,10 @@ libraft_la_SOURCES = \
libdqlite_la_SOURCES += $(libraft_la_SOURCES)
endif # BUILD_RAFT_ENABLED

# When adding a new test binary, make sure to update CI.
check_PROGRAMS = unit-test integration-test

check_LTLIBRARIES = libtest.la
check_LTLIBRARIES = libtest.la libcommon.la

libtest_la_CFLAGS = $(AM_CFLAGS) -DMUNIT_TEST_NAME_LEN=60 -Wno-unknown-warning-option -Wno-unused-result -Wno-conversion -Wno-uninitialized -Wno-maybe-uninitialized -Wno-strict-prototypes -Wno-old-style-definition
libtest_la_SOURCES = \
Expand All @@ -145,6 +144,16 @@ libtest_la_SOURCES = \
test/lib/sqlite.c \
test/lib/uv.c

# libcommon is the destination for code that is used by both the raft
# half and the dqlite half of the codebase, but isn't part of the raft
# API surface. It's also used to make these bits of code accessible to
# the unit tests without compiling the same few files repeatedly.
libcommon_la_CFLAGS = $(AM_CFLAGS)
libcommon_la_SOURCES = \
src/tracing.c \
src/lib/sm.c \
src/lib/threadpool.c

unit_test_SOURCES = $(basic_dqlite_sources)
unit_test_SOURCES += \
test/test_error.c \
Expand Down Expand Up @@ -172,7 +181,7 @@ unit_test_SOURCES += \
test/unit/main.c
unit_test_CFLAGS = $(AM_CFLAGS) -Wno-unknown-warning-option -Wno-uninitialized -Wno-maybe-uninitialized -Wno-float-equal -Wno-conversion
unit_test_LDFLAGS = $(AM_LDFLAGS)
unit_test_LDADD = libtest.la
unit_test_LDADD = libcommon.la libtest.la

if BUILD_RAFT_ENABLED
unit_test_LDADD += libraft.la
Expand All @@ -195,15 +204,16 @@ integration_test_LDADD = libtest.la libdqlite.la
if BUILD_RAFT_ENABLED
check_LTLIBRARIES += libraft.la

# When adding a new test binary, make sure to update CI.
check_PROGRAMS += \
raft-core-unit-test \
raft-core-integration-test \
raft-uv-unit-test \
raft-uv-integration-test \
raft-addrinfo-integration-test \
raft-core-fuzzy-test

libtest_la_SOURCES += \
test/raft/lib/addrinfo.c \
test/raft/lib/fault.c \
test/raft/lib/fsm.c \
test/raft/lib/heap.c \
Expand All @@ -217,12 +227,10 @@ libtest_la_SOURCES += \

libraft_la_CFLAGS = $(AM_CFLAGS)
libraft_la_LDFLAGS = $(UV_LIBS)
libraft_la_LIBADD = libcommon.la

raft_core_unit_test_SOURCES = \
$(libraft_la_SOURCES) \
src/lib/sm.c \
src/lib/threadpool.c \
src/tracing.c \
test/raft/unit/main_core.c \
test/raft/unit/test_byte.c \
test/raft/unit/test_compress.c \
Expand All @@ -234,12 +242,9 @@ raft_core_unit_test_SOURCES = \
test/raft/unit/test_snapshot.c

raft_core_unit_test_CFLAGS = $(AM_CFLAGS) -Wno-conversion
raft_core_unit_test_LDADD = libtest.la
raft_core_unit_test_LDADD = libcommon.la libtest.la

raft_core_integration_test_SOURCES = \
src/tracing.c \
src/lib/sm.c \
src/lib/threadpool.c \
test/raft/integration/main_core.c \
test/raft/integration/test_apply.c \
test/raft/integration/test_assign.c \
Expand All @@ -264,9 +269,6 @@ raft_core_integration_test_LDFLAGS = -no-install
raft_core_integration_test_LDADD = libtest.la libraft.la

raft_core_fuzzy_test_SOURCES = \
src/lib/sm.c \
src/lib/threadpool.c \
src/tracing.c \
test/raft/fuzzy/main_core.c \
test/raft/fuzzy/test_election.c \
test/raft/fuzzy/test_liveness.c \
Expand All @@ -277,7 +279,6 @@ raft_core_fuzzy_test_LDFLAGS = -no-install
raft_core_fuzzy_test_LDADD = libtest.la libraft.la

raft_uv_unit_test_SOURCES = \
src/tracing.c \
src/raft/err.c \
src/raft/heap.c \
src/raft/syscall.c \
Expand All @@ -289,16 +290,12 @@ raft_uv_unit_test_SOURCES = \
test/raft/unit/test_uv_os.c \
test/raft/unit/test_uv_writer.c
raft_uv_unit_test_CFLAGS = $(AM_CFLAGS) -Wno-conversion
raft_uv_unit_test_LDADD = libtest.la $(UV_LIBS)
raft_uv_unit_test_LDADD = libcommon.la libtest.la $(UV_LIBS)

# The integration/uv test is not linked to libraft, but built
# directly against the libraft sources in order to test some
# non-visible, non-API functions.
raft_uv_integration_test_SOURCES = \
$(libraft_la_SOURCES) \
src/tracing.c \
src/lib/sm.c \
src/lib/threadpool.c \
test/raft/integration/main_uv.c \
test/raft/integration/test_uv_init.c \
test/raft/integration/test_uv_append.c \
Expand All @@ -316,7 +313,22 @@ raft_uv_integration_test_SOURCES = \
test/raft/integration/test_uv_work.c
raft_uv_integration_test_CFLAGS = $(AM_CFLAGS) -Wno-type-limits -Wno-conversion
raft_uv_integration_test_LDFLAGS = -no-install
raft_uv_integration_test_LDADD = libtest.la $(UV_LIBS)
raft_uv_integration_test_LDADD = libtest.la libraft.la $(UV_LIBS)

# Some of the uv integration tests are broken out into a separate
# binary because they use a trick to override libc's getaddrinfo
# with a mock implementation. When libc is statically linked (a use-case
# that we care about), this trick doesn't work, and prevents any
# test in the same binary that calls getaddrinfo from working
# correctly.
raft_addrinfo_integration_test_SOURCES = \
test/raft/lib/addrinfo.c \
test/raft/integration/test_uv_tcp_connect_addrinfo.c \
test/raft/integration/test_uv_tcp_listen_addrinfo.c \
test/raft/integration/main_addrinfo.c
raft_addrinfo_integration_test_CFLAGS = $(AM_CFLAGS) -Wno-type-limits -Wno-conversion
raft_addrinfo_integration_test_LDFLAGS = -no-install
raft_addrinfo_integration_test_LDADD = libtest.la libraft.la $(UV_LIBS)

if LZ4_AVAILABLE
libdqlite_la_CFLAGS += -DLZ4_AVAILABLE $(LZ4_CFLAGS)
Expand Down Expand Up @@ -344,7 +356,7 @@ libsqlite3_la_SOURCES = sqlite3.c
libsqlite3_la_CFLAGS = -g3

unit_test_LDADD += libsqlite3.la
libdqlite_la_LIBADD = libsqlite3.la
libdqlite_la_LIBADD += libsqlite3.la
else
AM_LDFLAGS += $(SQLITE_LIBS)
endif
Expand Down
2 changes: 1 addition & 1 deletion configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ CC_CHECK_FLAGS_APPEND([AM_CFLAGS],[CFLAGS],[ \
-fexceptions \
-fstack-clash-protection \
-fstack-protector-strong \
-fasynchronous-unwind-tables \
-fno-exceptions \
cole-miller marked this conversation as resolved.
Show resolved Hide resolved
-fdiagnostics-show-option \
-Wall \
-Wextra \
Expand Down
14 changes: 7 additions & 7 deletions test/integration/test_vfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ static void tearDownRestorePendingByte(void *data)
tearDown(data);
}

#define PAGE_SIZE 512
#define DB_PAGE_SIZE 512

#define PRAGMA(DB, COMMAND) \
_rv = sqlite3_exec(DB, "PRAGMA " COMMAND, NULL, NULL, NULL); \
Expand Down Expand Up @@ -216,12 +216,12 @@ struct tx
if (_frames != NULL) { \
TX.page_numbers = \
munit_malloc(sizeof *TX.page_numbers * TX.n); \
TX.frames = munit_malloc(PAGE_SIZE * TX.n); \
TX.frames = munit_malloc(DB_PAGE_SIZE * TX.n); \
for (_i = 0; _i < TX.n; _i++) { \
dqlite_vfs_frame *_frame = &_frames[_i]; \
TX.page_numbers[_i] = _frame->page_number; \
memcpy(TX.frames + _i * PAGE_SIZE, \
_frame->data, PAGE_SIZE); \
memcpy(TX.frames + _i * DB_PAGE_SIZE, \
_frame->data, DB_PAGE_SIZE); \
sqlite3_free(_frame->data); \
} \
sqlite3_free(_frames); \
Expand Down Expand Up @@ -1502,7 +1502,7 @@ TEST(vfs, snapshotInitialDatabase, setUp, tearDown, 0, vfs_params)

SNAPSHOT("1", snapshot);

munit_assert_int(snapshot.n, ==, PAGE_SIZE);
munit_assert_int(snapshot.n, ==, DB_PAGE_SIZE);
page = snapshot.data;

munit_assert_int(memcmp(&page[16], page_size, 2), ==, 0);
Expand Down Expand Up @@ -1536,7 +1536,7 @@ TEST(vfs, snapshotAfterFirstTransaction, setUp, tearDown, 0, vfs_params)

SNAPSHOT("1", snapshot);

munit_assert_int(snapshot.n, ==, PAGE_SIZE + 32 + (24 + PAGE_SIZE) * 2);
munit_assert_int(snapshot.n, ==, DB_PAGE_SIZE + 32 + (24 + DB_PAGE_SIZE) * 2);
page = snapshot.data;

munit_assert_int(memcmp(&page[16], page_size, 2), ==, 0);
Expand Down Expand Up @@ -1571,7 +1571,7 @@ TEST(vfs, snapshotAfterCheckpoint, setUp, tearDown, 0, vfs_params)

SNAPSHOT("1", snapshot);

munit_assert_int(snapshot.n, ==, PAGE_SIZE * 2);
munit_assert_int(snapshot.n, ==, DB_PAGE_SIZE * 2);
page = snapshot.data;

munit_assert_int(memcmp(&page[16], page_size, 2), ==, 0);
Expand Down
67 changes: 20 additions & 47 deletions test/lib/heap.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <pthread.h>
#include <sqlite3.h>

#include "fault.h"
Expand Down Expand Up @@ -97,15 +98,6 @@ static void mem_wrap(sqlite3_mem_methods *m, sqlite3_mem_methods *wrap)
wrap->pAppData = &memFault;
}

/* Unwrap the given faulty memory management instance returning the original
* one. */
static void mem_unwrap(sqlite3_mem_methods *wrap, sqlite3_mem_methods *m)
{
(void)wrap;

*m = memFault.m;
}

/* Get the current number of outstanding malloc()'s without a matching free()
* and the total number of used memory. */
static void mem_stats(int *malloc_count, int *memory_used)
Expand All @@ -126,22 +118,12 @@ static void mem_stats(int *malloc_count, int *memory_used)
}
}

/* Ensure we're starting from a clean memory state with no allocations and
* optionally inject malloc failures. */
void test_heap_setup(const MunitParameter params[], void *user_data)
static void replace_sqlite_heap(void)
{
int malloc_count;
int memory_used;
const char *fault_delay;
const char *fault_repeat;
sqlite3_mem_methods mem;
sqlite3_mem_methods mem_fault;
int rc;

(void)params;
(void)user_data;

/* Install the faulty malloc implementation */
rc = sqlite3_config(SQLITE_CONFIG_GETMALLOC, &mem);
if (rc != SQLITE_OK) {
munit_errorf("can't get default mem: %s", sqlite3_errstr(rc));
Expand All @@ -153,6 +135,24 @@ void test_heap_setup(const MunitParameter params[], void *user_data)
if (rc != SQLITE_OK) {
munit_errorf("can't set faulty mem: %s", sqlite3_errstr(rc));
}
}

/* Ensure we're starting from a clean memory state with no allocations and
* optionally inject malloc failures. */
void test_heap_setup(const MunitParameter params[], void *user_data)
{
int malloc_count;
int memory_used;
const char *fault_delay;
const char *fault_repeat;
int rc;

(void)params;
(void)user_data;

static pthread_once_t once = PTHREAD_ONCE_INIT;
rc = pthread_once(&once, replace_sqlite_heap);
munit_assert_int(rc, ==, 0);

/* Check that memory is clean. */
mem_stats(&malloc_count, &memory_used);
Expand All @@ -177,34 +177,7 @@ void test_heap_setup(const MunitParameter params[], void *user_data)
/* Ensure we're starting leaving a clean memory behind. */
void test_heap_tear_down(void *data)
{
sqlite3_mem_methods mem;
cole-miller marked this conversation as resolved.
Show resolved Hide resolved
sqlite3_mem_methods mem_fault;
int rc;

(void)data;

int malloc_count;
int memory_used;

mem_stats(&malloc_count, &memory_used);
if (malloc_count > 0 || memory_used > 0) {
/* munit_errorf(
"teardown memory:\n bytes: %11d\n allocations: %5d\n",
memory_used, malloc_count); */
}

/* Restore default memory management. */
rc = sqlite3_config(SQLITE_CONFIG_GETMALLOC, &mem_fault);
if (rc != SQLITE_OK) {
munit_errorf("can't get faulty mem: %s", sqlite3_errstr(rc));
}

mem_unwrap(&mem_fault, &mem);

rc = sqlite3_config(SQLITE_CONFIG_MALLOC, &mem);
if (rc != SQLITE_OK) {
munit_errorf("can't reset default mem: %s", sqlite3_errstr(rc));
}
}

void test_heap_fault_config(int delay, int repeat)
Expand Down
1 change: 0 additions & 1 deletion test/lib/runner.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#define TEST_RUNNER_H

#include <signal.h>
#include <execinfo.h>
#include <stdio.h>
#include <stdlib.h>

Expand Down
3 changes: 3 additions & 0 deletions test/raft/integration/main_addrinfo.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#include "../lib/runner.h"

RUNNER("addrinfo")
Loading