Skip to content

Commit

Permalink
Merge pull request #4 from usi-systems/github-actions
Browse files Browse the repository at this point in the history
Added CI pipeline
  • Loading branch information
pako-23 authored Sep 11, 2024
2 parents 6147d24 + 1f8987c commit 79392e5
Show file tree
Hide file tree
Showing 22 changed files with 153 additions and 180 deletions.
31 changes: 31 additions & 0 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
---
name: CI Pipeline


on:
push:
branches: [master]
pull_request:
workflow_dispatch:

jobs:
test:
runs-on: ${{ matrix.os }}

strategy:
matrix:
os: [macos-13, macos-14, ubuntu-22.04, ubuntu-24.04]
compiler_suite: [clang, gcc]

steps:
- uses: actions/checkout@v4

- name: Run tests
run: |
if [ "${{ matrix.compiler_suite }}" = 'clang' ]; then
make CC=clang CXX=clang++ FAIL_FAST=yes PARALLELISM=4
elif [ "${{ matrix.compiler_suite }}" = 'gcc' ]; then
make CC=gcc CXX=g++ FAIL_FAST=yes PARALLELISM=4
fi
env:
TERM: xterm
7 changes: 6 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
.PHONY: all
all: all-examples

FAIL_FAST ?= no
EXAMPLES = ex/example1 ex/example2 ex/example3 ex/example4 \
ex/example5 ex/example6 ex/example7 ex/example8 ex/memory_checks ex/memory_checks_cpp \
'ex ws/example1 ws' 'ex ws/example2 ws' 'ex ws/example3 ws' 'ex ws/example4 ws' \
'ex ws/example5 ws' 'ex ws/example6 ws' 'ex ws/example7 ws' 'ex ws/example8 ws'
PARALLELISM ?= 1

.PHONY: all-examples
all-examples:
@for ex in $(EXAMPLES); \
do test_result=PASS; \
cp -a basic_testing.h "$$ex"/tests ;\
$(MAKE) -C "$$ex" clean > /dev/null || test_result=FAIL; \
$(MAKE) -C "$$ex" TEST_COLORS=no > "$$ex".out 2>&1 || test_result=FAIL ; \
$(MAKE) -C "$$ex" -j $(PARALLELISM) TEST_COLORS=no > "$$ex".out 2>&1 || test_result=FAIL ; \
if test -r "$$ex".expected; \
then { IFS=''; while read l; \
do if grep -Fq "$$l" "$$ex".out; \
Expand All @@ -27,6 +29,9 @@ all-examples:
rm -f "$$ex".out; \
else echo "$$ex FAIL" ; \
echo "check '$$ex.out' and '$$ex.expected' to see what went wrong"; \
if [ "$(FAIL_FAST)" = "yes" ]; \
then exit 1; \
fi; \
fi; \
done

Expand Down
59 changes: 57 additions & 2 deletions basic_testing.h
Original file line number Diff line number Diff line change
Expand Up @@ -862,6 +862,49 @@ void * calloc (size_t nmemb, size_t size) {
return p;
}

#ifdef __APPLE__
BT_POSSIBLY_UNUSED
void * valloc (size_t size) {
static void *(*libc_valloc)(size_t) = NULL;

if (!libc_valloc)
libc_valloc = (void *(*)(size_t)) dlsym(RTLD_NEXT, "valloc");

return malloc (size);
}

BT_POSSIBLY_UNUSED
void * aligned_alloc (size_t aligned, size_t size) {
static void *(*libc_aligned_alloc)(size_t, size_t) = NULL;

if (!libc_aligned_alloc)
libc_aligned_alloc = (void *(*)(size_t, size_t)) dlsym(RTLD_NEXT, "aligned_alloc");

return malloc (size);
}

BT_POSSIBLY_UNUSED
void * reallocf (void * ptr, size_t size) {
static void *(*libc_reallocf)(void *, size_t) = NULL;

if (!libc_reallocf)
libc_reallocf = (void *(*)(void *, size_t)) dlsym(RTLD_NEXT, "reallocf");

void * ret = realloc (ptr, size);
if (!ret) free (ptr);
return ret;
}

BT_POSSIBLY_UNUSED
char * strdup (const char * s) {
size_t len = strlen (s);
char * ret = (char *) malloc (len + 1);
if (ret) strcpy (ret, s);
return ret;
}
#endif

#ifndef __APPLE__
BT_POSSIBLY_UNUSED
void * reallocarray (void * ptr, size_t nmemb, size_t size) {
static void *(*libc_reallocarray)(void *, size_t, size_t) = NULL;
Expand All @@ -877,6 +920,20 @@ void * reallocarray (void * ptr, size_t nmemb, size_t size) {
size_t len = nmemb * size;
return realloc (ptr, len);
}
#endif

BT_POSSIBLY_UNUSED
void qsort (void * base, size_t nmemb, size_t size, int (*compar)(const void *, const void *)) {
static void (*libc_qsort)(void * base, size_t nmemb, size_t size, int (*compar)(const void *, const void *)) = NULL;

if (!libc_qsort)
libc_qsort = (void (*)(void *, size_t, size_t, int (*)(const void *, const void *))) dlsym(RTLD_NEXT, "qsort");

int prev_bt_mem_checks_disabled = bt_mem_checks_disabled;
bt_mem_checks_disabled = 1;
libc_qsort(base, nmemb, size, compar);
bt_mem_checks_disabled = prev_bt_mem_checks_disabled;
}

BT_POSSIBLY_UNUSED
int vfprintf (FILE * stream, const char * format, va_list ap) {
Expand Down Expand Up @@ -1040,8 +1097,6 @@ char * ctime (const time_t * tp) {
return result;
}



#ifdef __cplusplus
}
#endif
Expand Down
8 changes: 2 additions & 6 deletions basic_testing.mk
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
# and/or
# PROGRAMS=

CFLAGS=-Wall -Werror -g $(COVERAGE_FLAGS)
CXXFLAGS=-Wall -Werror -g $(COVERAGE_FLAGS)
CFLAGS=-D_GNU_SOURCE -Wall -Werror -g $(COVERAGE_FLAGS)
CXXFLAGS=-D_GNU_SOURCE -Wall -Werror -std=c++11 -g $(COVERAGE_FLAGS)

COVERAGE_FLAGS=$(if $(WITH_COVERAGE),--coverage,)
SHELL=/bin/bash
Expand Down Expand Up @@ -184,10 +184,6 @@ check-io-sh: compile $(TESTS_IO) $(TESTS_SH) $(PROGRAMS_DRIVERS)
done; \
test_summary 'Summary: PASS '

BT_WRAPPED_SYMBOLS := malloc free realloc calloc reallocarray \
_Znwm _Znam _ZdlPvm _ZdaPv
BT_WRAP_FLAGS := $(foreach S,$(BT_WRAPPED_SYMBOLS),-Wl,--wrap=$(S))

# we must assume there are some C++ sources, so we must link with $(CXX)
#
$(TESTS_DIR)/%: $(TESTS_DIR)/%.o $(OBJECTS)
Expand Down
2 changes: 1 addition & 1 deletion ex ws/example7 ws/flipline.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ int main() {
if (c == '\n') {
if (i > 10) {
i = 0;
while (line[i] != 255) {
while ((int) line[i] != 255) {
line[i] = *p;
--i;
}
Expand Down
2 changes: 1 addition & 1 deletion ex/example7/flipline.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ int main() {
if (c == '\n') {
if (i > 10) {
i = 0;
while (line[i] != 255) {
while ((int) line[i] != 255) {
line[i] = *p;
--i;
}
Expand Down
12 changes: 0 additions & 12 deletions ex/memory_checks.expected
Original file line number Diff line number Diff line change
Expand Up @@ -81,21 +81,9 @@ Running test 05_byte_budget_allocations_debug... PASS
Running test 06_scheduled_failures... PASS
Running test 06_scheduled_failures_debug... PASS
Running test 07_calloc_reallocarray_failures... PASS
Running test 08_fopen_leak...
FAIL
run 'tests/08_fopen_leak' to see what went wrong
run 'tests/08_fopen_leak -d' with a debugger
Running test 08_stdlib_functions... PASS
Running test 08_strdup_leak...
leaked 8 bytes
FAIL
run 'tests/08_strdup_leak' to see what went wrong
run 'tests/08_strdup_leak -d' with a debugger
Running test 08_tmpfile_leak...
FAIL
run 'tests/08_tmpfile_leak' to see what went wrong
run 'tests/08_tmpfile_leak -d' with a debugger
Running test 08_tmpnum_leak...
FAIL
run 'tests/08_tmpnum_leak' to see what went wrong
run 'tests/08_tmpnum_leak -d' with a debugger
15 changes: 13 additions & 2 deletions ex/memory_checks/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ int array_append_reallocarray (struct array * array, int element) {
if (array->len == array->cap) {
size_t new_cap = array->cap ? 2*array->cap : initial_cap;

int * new_data = reallocarray(array->data, new_cap, sizeof(int));
int * new_data = wrapped_reallocarray (array->data, new_cap, sizeof(int));
if (!new_data) return 0;

array->data = new_data;
Expand All @@ -71,6 +71,17 @@ int array_append_reallocarray (struct array * array, int element) {
return 1;
}

void * wrapped_reallocarray (void * ptr, size_t nmemb, size_t size) {
#ifdef __APPLE__
if (nmemb == 0 || size == 0 || SIZE_MAX / nmemb <= size)
return NULL;

return realloc (ptr, nmemb*size);
#else
return reallocarray (ptr, nmemb, size);
#endif
}

void double_free (void) {
int a = 10;
void * p = malloc (10);
Expand All @@ -80,7 +91,7 @@ void double_free (void) {

void non_malloc_free (void) {
int a = 10;
int * p;
int * p = NULL;
if (a < 20) p = &a;
free (p);
}
Expand Down
1 change: 1 addition & 0 deletions ex/memory_checks/array.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ int array_element(struct array * array, size_t index);
int array_append(struct array * array, int value);
int array_append_reallocarray(struct array * array, int value);

void * wrapped_reallocarray (void * ptr, size_t nmemb, size_t size);
void double_free (void);
void non_malloc_free (void);
void * malloc_zero_size (void);
Expand Down
8 changes: 4 additions & 4 deletions ex/memory_checks/tests/07_calloc_reallocarray_failures.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "basic_testing.h"
#include <stdlib.h>

#include "../array.h"


TEST (compile) {
Expand Down Expand Up @@ -29,7 +29,7 @@ TEST (calloc_zero_size) {
TEST (overflow_reallocarray) {
int * p = malloc (sizeof (int));
CHECK (p != NULL);
CHECK (reallocarray (p, 1073741824, 1073741824) == NULL);
CHECK (wrapped_reallocarray (p, 1073741824, 1073741824) == NULL);
free (p);
TEST_PASSED;
}
Expand All @@ -38,7 +38,7 @@ TEST (overflow_reallocarray) {
TEST (reallocarray_zero_nmemb) {
int * p = malloc (sizeof (int));
CHECK (p != NULL);
CHECK (reallocarray (p, 0, 1073741824) == NULL);
CHECK (wrapped_reallocarray (p, 0, 1073741824) == NULL);
free (p);
TEST_PASSED;
}
Expand All @@ -47,7 +47,7 @@ TEST (reallocarray_zero_nmemb) {
TEST (reallocarray_zero_size) {
int * p = malloc (sizeof (int));
CHECK (p != NULL);
CHECK (reallocarray (p, 1073741824, 0) == NULL);
CHECK (wrapped_reallocarray (p, 1073741824, 0) == NULL);
free (p);
TEST_PASSED;
}
Expand Down
15 changes: 0 additions & 15 deletions ex/memory_checks/tests/08_fopen_leak.c

This file was deleted.

10 changes: 4 additions & 6 deletions ex/memory_checks/tests/08_stdlib_functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,8 @@ TEST (stdlib_conversion_no_leaks) {

TEST (rand_no_leaks) {
srand (10);
CHECK_CMP (rand (),==,1215069295);
int num = rand ();
CHECK_CMP (rand (),!=,num);
TEST_PASSED;
}

Expand Down Expand Up @@ -370,18 +371,15 @@ static void read_str (char *buf, const char *format, ...) {
va_end (ap);
}

TEST (stdio_string_no_leak) {
TEST (stdio_sprintf_scanf_no_leak) {
char buf[100];
char * name = tempnam (NULL, "somefile");
CHECK (name != NULL);
sprintf (buf, "testing %d", 10);
int x;
sscanf (buf, "testing %d", &x);
CHECK_CMP (x,==,10);
write_str (buf, "testing vsprintf %d", 11);
read_str (buf, "testing vsprintf %d", &x);
CHECK_CMP (x,==,11);
free (name);
TEST_PASSED;
}

Expand Down Expand Up @@ -416,4 +414,4 @@ MAIN_TEST_DRIVER (ctype_no_leaks,
bsearch_no_leaks,
time_no_leaks,
basic_file_operations_no_leak,
stdio_string_no_leak);
stdio_sprintf_scanf_no_leak);
15 changes: 0 additions & 15 deletions ex/memory_checks/tests/08_tmpfile_leak.c

This file was deleted.

12 changes: 0 additions & 12 deletions ex/memory_checks/tests/08_tmpnum_leak.c

This file was deleted.

12 changes: 0 additions & 12 deletions ex/memory_checks_cpp.expected
Original file line number Diff line number Diff line change
Expand Up @@ -169,21 +169,9 @@ Running test 05_byte_budget_allocations_debug... PASS
Running test 06_scheduled_failures... PASS
Running test 06_scheduled_failures_debug... PASS
Running test 07_calloc_reallocarray_failures... PASS
Running test 08_fopen_leak...
FAIL
run 'tests/08_fopen_leak' to see what went wrong
run 'tests/08_fopen_leak -d' with a debugger
Running test 08_stdlib_functions... PASS
Running test 08_strdup_leak...
leaked 8 bytes
FAIL
run 'tests/08_strdup_leak' to see what went wrong
run 'tests/08_strdup_leak -d' with a debugger
Running test 08_tmpfile_leak...
FAIL
run 'tests/08_tmpfile_leak' to see what went wrong
run 'tests/08_tmpfile_leak -d' with a debugger
Running test 08_tmpnum_leak...
FAIL
run 'tests/08_tmpnum_leak' to see what went wrong
run 'tests/08_tmpnum_leak -d' with a debugger
Loading

0 comments on commit 79392e5

Please sign in to comment.