Skip to content

Commit 68e86f4

Browse files
hbgitclaude
andcommitted
ci: add Valgrind memcheck job for OpenSSF dynamic analysis
Add an isolated valgrind-memcheck job that runs the ctest unit-test suite under Valgrind via `ctest -T memcheck`, in a Debug build without ASan/UBSan (sanitizer shadow memory conflicts with Valgrind's VM). - CMakeLists.txt: replace enable_testing() with include(CTest) so DartConfiguration.tcl is generated for `ctest -T memcheck` - valgrind.supp: versioned suppression file for known external false positives (glibc, libstdc++, GTest, LLVM, solvers) - Strict memcheck policy: --leak-check=full --show-leak-kinds=all --track-origins=yes --error-exitcode=1, plus a log-grep safety net and memcheck logs uploaded as artifact - -gdwarf-4 for readable stacks (Valgrind 3.18 vs clang-16 DWARF-5) Strengthens OpenSSF Best Practices dynamic_analysis criteria by adding leak detection, currently disabled in the ASan job (detect_leaks=0). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
1 parent 7105450 commit 68e86f4

3 files changed

Lines changed: 168 additions & 1 deletion

File tree

‎.github/workflows/ci.yml‎

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,3 +288,82 @@ jobs:
288288
bash tests/integration/test_wasm_pipeline.sh
289289
bash tests/integration/test_wasm_entrypoint.sh
290290
'
291+
292+
# ===========================================================
293+
# Job 5: Dynamic Analysis — Valgrind memcheck (OpenSSF)
294+
# ===========================================================
295+
# Build ISOLADO e SEM sanitizers: a shadow memory do ASan/UBSan
296+
# conflita com a VM do Valgrind. Debug + DWARF-4 para stacks
297+
# legíveis (Valgrind 3.18 não lê bem o DWARF-5 do clang-16).
298+
valgrind-memcheck:
299+
name: Valgrind Memcheck (ctest)
300+
runs-on: ubuntu-22.04
301+
timeout-minutes: 30
302+
303+
steps:
304+
- name: Checkout
305+
uses: actions/checkout@v4
306+
with:
307+
submodules: recursive
308+
309+
- name: Install LLVM 16 + Valgrind + WABT
310+
run: |
311+
wget -qO- https://apt.llvm.org/llvm-snapshot.gpg.key | sudo tee /etc/apt/trusted.gpg.d/apt.llvm.org.asc
312+
echo "deb http://apt.llvm.org/jammy/ llvm-toolchain-jammy-16 main" | sudo tee /etc/apt/sources.list.d/llvm-16.list
313+
sudo apt-get update
314+
sudo apt-get install -y \
315+
clang-16 llvm-16 llvm-16-dev libclang-16-dev \
316+
cmake ninja-build libboost-all-dev zlib1g-dev \
317+
valgrind
318+
# WABT 1.0.41 (apt tem 1.0.27, API incompatível com WasmRuntimeStubs.c)
319+
wget -q "https://github.com/WebAssembly/wabt/releases/download/1.0.41/wabt-1.0.41-linux-x64.tar.gz" -O /tmp/wabt.tar.gz
320+
sudo tar xzf /tmp/wabt.tar.gz -C /opt
321+
sudo ln -sf /opt/wabt-1.0.41/bin/wasm2c /usr/local/bin/wasm2c
322+
323+
- name: Configure CMake (Debug, sem sanitizers)
324+
run: |
325+
mkdir -p build && cd build
326+
cmake .. -G Ninja \
327+
-DLLVM_DIR=/usr/lib/llvm-16/lib/cmake/llvm \
328+
-DSKIP_LIB_FUZZER=ON \
329+
-DSKIP_KLEE=ON \
330+
-DENABLE_TEST=ON \
331+
-DCMAKE_BUILD_TYPE=Debug \
332+
-DCMAKE_C_FLAGS="-gdwarf-4" \
333+
-DCMAKE_CXX_FLAGS="-gdwarf-4" \
334+
-DMEMORYCHECK_COMMAND=/usr/bin/valgrind \
335+
-DMEMORYCHECK_SUPPRESSIONS_FILE="${{ github.workspace }}/valgrind.supp" \
336+
-DMEMORYCHECK_COMMAND_OPTIONS="--tool=memcheck --leak-check=full --show-leak-kinds=all --errors-for-leak-kinds=definite --track-origins=yes --num-callers=40 --error-exitcode=1 --gen-suppressions=all"
337+
env:
338+
CC: /usr/bin/clang-16
339+
CXX: /usr/bin/clang++-16
340+
341+
- name: Build
342+
run: cd build && ninja
343+
344+
- name: Run ctest under Valgrind (memcheck)
345+
run: cd build && ctest -T memcheck --output-on-failure
346+
347+
- name: Enforce zero memcheck defects
348+
# Rede de segurança além do --error-exitcode=1: falha se qualquer
349+
# log do memcheck registrar erros ou "definitely lost".
350+
run: |
351+
cd build
352+
shopt -s nullglob
353+
defects=0
354+
for log in Testing/Temporary/MemoryChecker.*.log; do
355+
if grep -qE "ERROR SUMMARY: [1-9][0-9]* errors|definitely lost: [1-9]" "$log"; then
356+
echo "::error::Valgrind encontrou defeitos em $log"
357+
cat "$log"
358+
defects=1
359+
fi
360+
done
361+
exit $defects
362+
363+
- name: Upload memcheck logs
364+
if: always()
365+
uses: actions/upload-artifact@v4
366+
with:
367+
name: valgrind-memcheck-logs
368+
path: build/Testing/Temporary/MemoryChecker.*.log
369+
if-no-files-found: ignore

‎CMakeLists.txt‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,9 @@ endif()
6161
include_directories(${PROJECT_SOURCE_DIR})
6262

6363
if(ENABLE_TEST)
64-
enable_testing()
64+
# include(CTest) em vez de enable_testing(): gera DartConfiguration.tcl,
65+
# necessário para `ctest -T memcheck` (job Valgrind no CI).
66+
include(CTest)
6567
include(cmake/FindGTest.cmake)
6668
message(STATUS "Adding tests")
6769
add_subdirectory(tests)

‎valgrind.supp‎

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# =============================================================================
2+
# Map2Check — Valgrind (memcheck) suppressions
3+
#
4+
# Regras:
5+
# * Cada supressão DEVE ter nome descritivo e comentário justificando-a.
6+
# * Supressões de código do Map2Check exigem link para issue rastreando o fix.
7+
# * Gere candidatas com: valgrind --gen-suppressions=all ./<teste>
8+
# =============================================================================
9+
10+
# --- glibc: pool interno do dlopen/ld.so nunca é liberado (falso positivo) ---
11+
{
12+
glibc-dlopen-reachable
13+
Memcheck:Leak
14+
match-leak-kinds: reachable
15+
fun:malloc
16+
...
17+
obj:*/ld-linux*.so*
18+
}
19+
{
20+
glibc-dlopen-calloc-reachable
21+
Memcheck:Leak
22+
match-leak-kinds: reachable
23+
fun:calloc
24+
...
25+
fun:_dl_open
26+
}
27+
28+
# --- libstdc++: pool de emergência para exceções (alocado uma vez, by design) ---
29+
{
30+
libstdcpp-eh-pool
31+
Memcheck:Leak
32+
match-leak-kinds: reachable
33+
fun:malloc
34+
...
35+
fun:__cxa_get_globals
36+
}
37+
38+
# --- GoogleTest: singletons/registro de testes vivos até o exit (reachable) ---
39+
{
40+
gtest-unittest-singleton
41+
Memcheck:Leak
42+
match-leak-kinds: reachable
43+
...
44+
fun:*testing*UnitTest*GetInstance*
45+
}
46+
47+
# --- LLVM/Clang backend: caches estáticos (ManagedStatic, cl::opt registry) ---
48+
# Só relevante se binários que linkam LLVM entrarem na suíte ctest.
49+
{
50+
llvm-managed-static
51+
Memcheck:Leak
52+
match-leak-kinds: reachable
53+
...
54+
fun:*llvm*ManagedStatic*
55+
}
56+
{
57+
llvm-cl-opt-registry
58+
Memcheck:Leak
59+
match-leak-kinds: reachable
60+
...
61+
fun:*llvm*cl*Option*
62+
}
63+
64+
# --- Solvers (Z3/STP/MiniSat): pools internos não liberados no shutdown ---
65+
# Só relevante em builds sem SKIP_KLEE.
66+
{
67+
z3-internal-pools
68+
Memcheck:Leak
69+
match-leak-kinds: reachable
70+
...
71+
obj:*libz3*
72+
}
73+
74+
# --- Map2Check: leaks INTENCIONAIS dos testes de rastreamento de alocação ---
75+
# Os testes unitários alocam sem liberar de propósito (testam o tracking de
76+
# alocações, não ownership) — mesmo motivo do detect_leaks=0 no job ASan.
77+
# Preencher com os stacks exatos gerados por --gen-suppressions=all na
78+
# primeira execução do job no CI. Exemplo do formato:
79+
# {
80+
# map2check-test-intentional-allocationlog
81+
# Memcheck:Leak
82+
# match-leak-kinds: definite
83+
# fun:malloc
84+
# ...
85+
# fun:*AllocationLogTest*
86+
# }

0 commit comments

Comments
 (0)