run.sh 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #!/bin/sh
  2. # SPDX-License-Identifier: GPL-2.0
  3. #
  4. # This reads tests.txt for the list of LKDTM tests to invoke. Any marked
  5. # with a leading "#" are skipped. The rest of the line after the
  6. # test name is either the text to look for in dmesg for a "success",
  7. # or the rationale for why a test is marked to be skipped.
  8. #
  9. set -e
  10. TRIGGER=/sys/kernel/debug/provoke-crash/DIRECT
  11. CLEAR_ONCE=/sys/kernel/debug/clear_warn_once
  12. KSELFTEST_SKIP_TEST=4
  13. # Verify we have LKDTM available in the kernel.
  14. if [ ! -r $TRIGGER ] ; then
  15. /sbin/modprobe -q lkdtm || true
  16. if [ ! -r $TRIGGER ] ; then
  17. echo "Cannot find $TRIGGER (missing CONFIG_LKDTM?)"
  18. else
  19. echo "Cannot write $TRIGGER (need to run as root?)"
  20. fi
  21. # Skip this test
  22. exit $KSELFTEST_SKIP_TEST
  23. fi
  24. # Figure out which test to run from our script name.
  25. test=$(basename $0 .sh)
  26. # Look up details about the test from master list of LKDTM tests.
  27. line=$(grep -E '^#?'"$test"'\b' tests.txt)
  28. if [ -z "$line" ]; then
  29. echo "Skipped: missing test '$test' in tests.txt"
  30. exit $KSELFTEST_SKIP_TEST
  31. fi
  32. # Check that the test is known to LKDTM.
  33. if ! grep -E -q '^'"$test"'$' "$TRIGGER" ; then
  34. echo "Skipped: test '$test' missing in $TRIGGER!"
  35. exit $KSELFTEST_SKIP_TEST
  36. fi
  37. # Extract notes/expected output from test list.
  38. test=$(echo "$line" | cut -d" " -f1)
  39. if echo "$line" | grep -q ' ' ; then
  40. expect=$(echo "$line" | cut -d" " -f2-)
  41. else
  42. expect=""
  43. fi
  44. # If the test is commented out, report a skip
  45. if echo "$test" | grep -q '^#' ; then
  46. test=$(echo "$test" | cut -c2-)
  47. if [ -z "$expect" ]; then
  48. expect="crashes entire system"
  49. fi
  50. echo "Skipping $test: $expect"
  51. exit $KSELFTEST_SKIP_TEST
  52. fi
  53. # If no expected output given, assume an Oops with back trace is success.
  54. repeat=1
  55. if [ -z "$expect" ]; then
  56. expect="call trace:"
  57. else
  58. if echo "$expect" | grep -q '^repeat:' ; then
  59. repeat=$(echo "$expect" | cut -d' ' -f1 | cut -d: -f2)
  60. expect=$(echo "$expect" | cut -d' ' -f2-)
  61. fi
  62. fi
  63. # Prepare log for report checking
  64. LOG=$(mktemp --tmpdir -t lkdtm-log-XXXXXX)
  65. DMESG=$(mktemp --tmpdir -t lkdtm-dmesg-XXXXXX)
  66. cleanup() {
  67. rm -f "$LOG" "$DMESG"
  68. }
  69. trap cleanup EXIT
  70. # Reset WARN_ONCE counters so we trip it each time this runs.
  71. if [ -w $CLEAR_ONCE ] ; then
  72. echo 1 > $CLEAR_ONCE
  73. fi
  74. # Save existing dmesg so we can detect new content below
  75. dmesg > "$DMESG"
  76. # Since the kernel is likely killing the process writing to the trigger
  77. # file, it must not be the script's shell itself. i.e. we cannot do:
  78. # echo "$test" >"$TRIGGER"
  79. # Instead, use "cat" to take the signal. Since the shell will yell about
  80. # the signal that killed the subprocess, we must ignore the failure and
  81. # continue. However we don't silence stderr since there might be other
  82. # useful details reported there in the case of other unexpected conditions.
  83. for i in $(seq 1 $repeat); do
  84. echo "$test" | cat >"$TRIGGER" || true
  85. done
  86. # Record and dump the results
  87. dmesg | comm --nocheck-order -13 "$DMESG" - > "$LOG" || true
  88. cat "$LOG"
  89. # Check for expected output
  90. if grep -E -qi "$expect" "$LOG" ; then
  91. echo "$test: saw '$expect': ok"
  92. exit 0
  93. else
  94. if grep -E -qi XFAIL: "$LOG" ; then
  95. echo "$test: saw 'XFAIL': [SKIP]"
  96. exit $KSELFTEST_SKIP_TEST
  97. else
  98. echo "$test: missing '$expect': [FAIL]"
  99. exit 1
  100. fi
  101. fi