| 1 |
#!/bin/bash -eu |
| 2 |
# shellcheck disable=SC2086,SC2016 |
| 3 |
# |
| 4 |
# Flag variables below ($CFLAGS, $TCL_INCLUDE_SPEC, $TCL_LIB_SPEC, |
| 5 |
# $LIB_FUZZING_ENGINE, ...) are deliberately left unquoted so each word |
| 6 |
# splits into its own argument, per the standard OSS-Fuzz build.sh |
| 7 |
# convention; quoting would collapse a multi-flag string into one |
| 8 |
# argument and break the build. |
| 9 |
# |
| 10 |
# ClusterFuzzLite build script for lib/envmodules.c, the C extension |
| 11 |
# backing the "libtclenvmodules" Tcl package. Fuzz targets call the |
| 12 |
# extension's ObjCmd entry points directly against a real Tcl interpreter |
| 13 |
# (Tcl_CreateInterp), bypassing Envmodules_Init, so this links against |
| 14 |
# libtcl itself rather than the Tcl stub library the shipped module uses. |
| 15 |
|
| 16 |
MODROOT="$SRC/modules" |
| 17 |
FUZZDIR="$MODROOT/.clusterfuzzlite" |
| 18 |
|
| 19 |
# lib/configure is generated (gitignored, not checked into the repo); |
| 20 |
# the top-level ./configure normally creates it on demand via the same |
| 21 |
# 'autoreconf -i' before running it, which we replicate here since we |
| 22 |
# only need the C library, not a full top-level configure run. |
| 23 |
cd "$MODROOT/lib" |
| 24 |
autoreconf -i |
| 25 |
|
| 26 |
# Generate lib/config.h (PACKAGE_NAME, GETGROUPS_T, ...) through the |
| 27 |
# project's own TEA-based configure script. --disable-shared |
| 28 |
# --disable-stubs turns off USE_TCL_STUBS: the shipped module is a |
| 29 |
# stub-linked loadable extension whose Tcl_* calls only resolve once |
| 30 |
# Envmodules_Init() runs Tcl_InitStubs(), but these fuzz targets call the |
| 31 |
# ObjCmd entry points directly and link against libtcl itself, which |
| 32 |
# doesn't export tclStubsPtr/Tcl_InitStubs. |
| 33 |
TCLCONFDIR=$(dirname "$(find /usr -name tclConfig.sh | head -n1)") |
| 34 |
./configure --with-tcl="$TCLCONFDIR" --disable-shared --disable-stubs |
| 35 |
|
| 36 |
# shellcheck disable=SC1091 |
| 37 |
. "$TCLCONFDIR/tclConfig.sh" |
| 38 |
|
| 39 |
$CC $CFLAGS $TCL_INCLUDE_SPEC -I"$MODROOT/lib" \ |
| 40 |
-c "$MODROOT/lib/envmodules.c" -o "$WORK/envmodules.o" |
| 41 |
|
| 42 |
for fuzzer in fuzz_parsedatetimearg fuzz_readfile fuzz_getfilesindirectory; do |
| 43 |
$CC $CFLAGS $TCL_INCLUDE_SPEC -I"$MODROOT/lib" \ |
| 44 |
-c "$FUZZDIR/$fuzzer.c" -o "$WORK/$fuzzer.o" |
| 45 |
$CXX $CXXFLAGS -Wl,-rpath,'$ORIGIN/lib' \ |
| 46 |
"$WORK/$fuzzer.o" "$WORK/envmodules.o" \ |
| 47 |
$TCL_LIB_SPEC $LIB_FUZZING_ENGINE -o "$OUT/$fuzzer" |
| 48 |
done |
| 49 |
|
| 50 |
# Bundle libtcl next to the fuzz targets: $OUT ships without the |
| 51 |
# container's system packages. |
| 52 |
mkdir -p "$OUT/lib" |
| 53 |
TCL_LIBDIR=$(echo "$TCL_LIB_SPEC" | grep -oE -- '-L[^ ]+' | head -n1 | cut -c3-) |
| 54 |
cp -L "$TCL_LIBDIR"/libtcl8*.so* "$OUT/lib/" |