162306a36Sopenharmony_ci#!/bin/sh 262306a36Sopenharmony_ci# SPDX-License-Identifier: GPL-2.0-only 362306a36Sopenharmony_ciset -e 462306a36Sopenharmony_ci 562306a36Sopenharmony_ci# Argument 1: Source file to build. 662306a36Sopenharmony_ciIN="$1" 762306a36Sopenharmony_cishift 862306a36Sopenharmony_ci# Extract just the filename for error messages below. 962306a36Sopenharmony_ciFILE="${IN##*/}" 1062306a36Sopenharmony_ci# Extract the function name for error messages below. 1162306a36Sopenharmony_ciFUNC="${FILE#*-}" 1262306a36Sopenharmony_ciFUNC="${FUNC%%-*}" 1362306a36Sopenharmony_ciFUNC="${FUNC%%.*}" 1462306a36Sopenharmony_ci# Extract the symbol to test for in build/symbol test below. 1562306a36Sopenharmony_ciWANT="__${FILE%%-*}" 1662306a36Sopenharmony_ci 1762306a36Sopenharmony_ci# Argument 2: Where to write the build log. 1862306a36Sopenharmony_ciOUT="$1" 1962306a36Sopenharmony_cishift 2062306a36Sopenharmony_ciTMP="${OUT}.tmp" 2162306a36Sopenharmony_ci 2262306a36Sopenharmony_ci# Argument 3: Path to "nm" tool. 2362306a36Sopenharmony_ciNM="$1" 2462306a36Sopenharmony_cishift 2562306a36Sopenharmony_ci 2662306a36Sopenharmony_ci# Remaining arguments are: $(CC) $(c_flags) 2762306a36Sopenharmony_ci 2862306a36Sopenharmony_ci# Clean up temporary file at exit. 2962306a36Sopenharmony_ci__cleanup() { 3062306a36Sopenharmony_ci rm -f "$TMP" 3162306a36Sopenharmony_ci} 3262306a36Sopenharmony_citrap __cleanup EXIT 3362306a36Sopenharmony_ci 3462306a36Sopenharmony_ci# Function names in warnings are wrapped in backticks under UTF-8 locales. 3562306a36Sopenharmony_ci# Run the commands with LANG=C so that grep output will not change. 3662306a36Sopenharmony_ciexport LANG=C 3762306a36Sopenharmony_ci 3862306a36Sopenharmony_cistatus= 3962306a36Sopenharmony_ci# Attempt to build a source that is expected to fail with a specific warning. 4062306a36Sopenharmony_ciif "$@" -Werror -c "$IN" -o "$OUT".o 2> "$TMP" ; then 4162306a36Sopenharmony_ci # If the build succeeds, either the test has failed or the 4262306a36Sopenharmony_ci # warning may only happen at link time (Clang). In that case, 4362306a36Sopenharmony_ci # make sure the expected symbol is unresolved in the symbol list. 4462306a36Sopenharmony_ci # If so, FORTIFY is working for this case. 4562306a36Sopenharmony_ci if ! $NM -A "$OUT".o | grep -m1 "\bU ${WANT}$" >>"$TMP" ; then 4662306a36Sopenharmony_ci status="warning: unsafe ${FUNC}() usage lacked '$WANT' symbol in $IN" 4762306a36Sopenharmony_ci fi 4862306a36Sopenharmony_cielse 4962306a36Sopenharmony_ci # If the build failed, check for the warning in the stderr. 5062306a36Sopenharmony_ci # GCC: 5162306a36Sopenharmony_ci # ./include/linux/fortify-string.h:316:25: error: call to '__write_overflow_field' declared with attribute warning: detected write beyond size of field (1st parameter); maybe use struct_group()? [-Werror=attribute-warning] 5262306a36Sopenharmony_ci # Clang 14: 5362306a36Sopenharmony_ci # ./include/linux/fortify-string.h:316:4: error: call to __write_overflow_field declared with 'warning' attribute: detected write beyond size of field (1st parameter); maybe use struct_group()? [-Werror,-Wattribute-warning] 5462306a36Sopenharmony_ci if ! grep -Eq -m1 "error: call to .?\b${WANT}\b.?" "$TMP" ; then 5562306a36Sopenharmony_ci status="warning: unsafe ${FUNC}() usage lacked '$WANT' warning in $IN" 5662306a36Sopenharmony_ci fi 5762306a36Sopenharmony_cifi 5862306a36Sopenharmony_ci 5962306a36Sopenharmony_ciif [ -n "$status" ]; then 6062306a36Sopenharmony_ci # Report on failure results, including compilation warnings. 6162306a36Sopenharmony_ci echo "$status" | tee "$OUT" >&2 6262306a36Sopenharmony_cielse 6362306a36Sopenharmony_ci # Report on good results, and save any compilation output to log. 6462306a36Sopenharmony_ci echo "ok: unsafe ${FUNC}() usage correctly detected with '$WANT' in $IN" >"$OUT" 6562306a36Sopenharmony_cifi 6662306a36Sopenharmony_cicat "$TMP" >>"$OUT" 67