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

chore(ci): run static/dynamic analysis #112

Merged
merged 1 commit into from
Oct 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
19 changes: 19 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,22 @@ name: Test
on: [push, pull_request]

jobs:
makefile-analysis:
name: makefile-analysis
runs-on: ubuntu-22.04
steps:
- name: Checkout
uses: actions/checkout@v3
- name: "Install analysis tools"
run: |
sudo apt-get update
sudo apt-get install -y clang-tools valgrind
- name: Run tests
run: ./test/ci/analysis.sh

makefile-test:
name: makefile-${{ matrix.runner }}-amd64-${{ matrix.compiler }} ${{ ((matrix.openmp == 1) && '+openmp') || '' }}
needs: makefile-analysis
runs-on: ${{ matrix.runner }}
strategy:
fail-fast: false
Expand All @@ -29,6 +43,7 @@ jobs:

cmake-test:
name: cmake-${{ matrix.runner }}-${{ matrix.platform }}
needs: makefile-analysis
runs-on: ${{ matrix.runner }}
strategy:
fail-fast: false
Expand Down Expand Up @@ -65,6 +80,7 @@ jobs:

alpine-makefile-test:
name: makefile-alpine-amd64-gcc
needs: makefile-analysis
runs-on: ubuntu-latest
container:
image: alpine:3.12
Expand All @@ -80,6 +96,7 @@ jobs:

alpine-cmake-test:
name: cmake-alpine-amd64-gcc
needs: makefile-analysis
runs-on: ubuntu-latest
container:
image: alpine:3.12
Expand All @@ -104,6 +121,7 @@ jobs:

alpine-alt-arch-makefile-test:
name: makefile-alpine-${{matrix.arch}}-${{matrix.cc}}
needs: makefile-analysis
runs-on: ubuntu-latest
strategy:
fail-fast: false
Expand All @@ -124,6 +142,7 @@ jobs:

alpine-alt-arch-cmake-test:
name: cmake-alpine-${{matrix.arch}}-${{matrix.cc}}
needs: makefile-analysis
runs-on: ubuntu-latest
strategy:
fail-fast: false
Expand Down
5 changes: 4 additions & 1 deletion test/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,15 @@ else
BENCH_LDFLAGS=-lrt
endif

.PHONY: clean test
.PHONY: clean test valgrind

test: clean test_base64 benchmark
./test_base64
./benchmark

valgrind: clean test_base64
valgrind --error-exitcode=2 ./test_base64

test_base64: test_base64.c codec_supported.o ../lib/libbase64.o
$(CC) $(CFLAGS) -o $@ $^

Expand Down
37 changes: 37 additions & 0 deletions test/ci/analysis.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/bin/bash
set -ve

MACHINE=$(uname -m)
export CC=gcc

uname -a
clang --version # make analyse
${CC} --version # make -C test valgrind

for USE_ASSEMBLY in 0 1; do
if [ "${MACHINE}" == "x86_64" ]; then
export SSSE3_CFLAGS="-mssse3 -DBASE64_SSSE3_USE_ASM=${USE_ASSEMBLY}"
export SSE41_CFLAGS="-msse4.1 -DBASE64_SSE41_USE_ASM=${USE_ASSEMBLY}"
export SSE42_CFLAGS="-msse4.2 -DBASE64_SSE42_USE_ASM=${USE_ASSEMBLY}"
export AVX_CFLAGS="-mavx -DBASE64_AVX_USE_ASM=${USE_ASSEMBLY}"
export AVX2_CFLAGS="-mavx2 -DBASE64_AVX2_USE_ASM=${USE_ASSEMBLY}"
# Temporarily disable AVX512; it is not available in CI yet.
# export AVX512_CFLAGS="-mavx512vl -mavx512vbmi"
elif [ "${MACHINE}" == "aarch64" ]; then
export NEON64_CFLAGS="-march=armv8-a"
elif [ "${MACHINE}" == "armv7l" ]; then
export NEON32_CFLAGS="-march=armv7-a -mfloat-abi=hard -mfpu=neon"
fi

if [ ${USE_ASSEMBLY} -eq 0 ]; then
echo "::group::analyze"
make analyze
echo "::endgroup::"
fi

echo "::group::valgrind (USE_ASSEMBLY=${USE_ASSEMBLY})"
make clean
make
make -C test valgrind
echo "::endgroup::"
done
24 changes: 21 additions & 3 deletions test/test_base64.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <stdbool.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "../include/libbase64.h"
#include "codec_supported.h"
#include "moby_dick.h"
Expand Down Expand Up @@ -92,7 +93,7 @@ assert_roundtrip (int flags, const char *src)
}

static int
test_char_table (int flags)
test_char_table (int flags, bool use_malloc)
{
bool fail = false;
char chr[256];
Expand All @@ -107,8 +108,24 @@ test_char_table (int flags)
for (int i = 0; i < 256; i++) {

size_t chrlen = 256 - i;
char* src = &chr[i];
if (use_malloc) {
src = malloc(chrlen); /* malloc/copy this so valgrind can find out-of-bound access */
if (src == NULL) {
printf(
"FAIL: encoding @ %d: allocation of %lu bytes failed\n",
i, (unsigned long)chrlen
);
fail = true;
continue;
}
memcpy(src, &chr[i], chrlen);
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, and very useful.


base64_encode(&chr[i], chrlen, enc, &enclen, BASE64_FORCE_PLAIN);
base64_encode(src, chrlen, enc, &enclen, flags);
if (use_malloc) {
free(src);
}

if (!base64_decode(enc, enclen, dec, &declen, flags)) {
printf("FAIL: decoding @ %d: decoding error\n", i);
Expand Down Expand Up @@ -341,7 +358,8 @@ test_one_codec (const char *codec, int flags)
fail |= assert_roundtrip(flags, vec[i].out);
}

fail |= test_char_table(flags);
fail |= test_char_table(flags, false); /* test with unaligned input buffer */
fail |= test_char_table(flags, true); /* test for out-of-bound input read */
fail |= test_streaming(flags);
fail |= test_invalid_dec_input(flags);

Expand Down