test_fortify.sh 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #!/bin/sh
  2. # SPDX-License-Identifier: GPL-2.0-only
  3. set -e
  4. # Argument 1: Source file to build.
  5. IN="$1"
  6. shift
  7. # Extract just the filename for error messages below.
  8. FILE="${IN##*/}"
  9. # Extract the function name for error messages below.
  10. FUNC="${FILE#*-}"
  11. FUNC="${FUNC%%-*}"
  12. FUNC="${FUNC%%.*}"
  13. # Extract the symbol to test for in build/symbol test below.
  14. WANT="__${FILE%%-*}"
  15. # Argument 2: Where to write the build log.
  16. OUT="$1"
  17. shift
  18. TMP="${OUT}.tmp"
  19. # Argument 3: Path to "nm" tool.
  20. NM="$1"
  21. shift
  22. # Remaining arguments are: $(CC) $(c_flags)
  23. # Clean up temporary file at exit.
  24. __cleanup() {
  25. rm -f "$TMP"
  26. }
  27. trap __cleanup EXIT
  28. # Function names in warnings are wrapped in backticks under UTF-8 locales.
  29. # Run the commands with LANG=C so that grep output will not change.
  30. export LANG=C
  31. status=
  32. # Attempt to build a source that is expected to fail with a specific warning.
  33. if "$@" -Werror -c "$IN" -o "$OUT".o 2> "$TMP" ; then
  34. # If the build succeeds, either the test has failed or the
  35. # warning may only happen at link time (Clang). In that case,
  36. # make sure the expected symbol is unresolved in the symbol list.
  37. # If so, FORTIFY is working for this case.
  38. if ! $NM -A "$OUT".o | grep -m1 "\bU ${WANT}$" >>"$TMP" ; then
  39. status="warning: unsafe ${FUNC}() usage lacked '$WANT' symbol in $IN"
  40. fi
  41. else
  42. # If the build failed, check for the warning in the stderr.
  43. # GCC:
  44. # ./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]
  45. # Clang 14:
  46. # ./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]
  47. if ! grep -Eq -m1 "error: call to .?\b${WANT}\b.?" "$TMP" ; then
  48. status="warning: unsafe ${FUNC}() usage lacked '$WANT' warning in $IN"
  49. fi
  50. fi
  51. if [ -n "$status" ]; then
  52. # Report on failure results, including compilation warnings.
  53. echo "$status" | tee "$OUT" >&2
  54. else
  55. # Report on good results, and save any compilation output to log.
  56. echo "ok: unsafe ${FUNC}() usage correctly detected with '$WANT' in $IN" >"$OUT"
  57. fi
  58. cat "$TMP" >>"$OUT"