short_splice_read.sh 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #!/bin/sh
  2. # SPDX-License-Identifier: GPL-2.0
  3. #
  4. # Test for mishandling of splice() on pseudofilesystems, which should catch
  5. # bugs like 11990a5bd7e5 ("module: Correctly truncate sysfs sections output")
  6. #
  7. # Since splice fallback was removed as part of the set_fs() rework, many of these
  8. # tests expect to fail now. See https://lore.kernel.org/lkml/202009181443.C2179FB@keescook/
  9. set -e
  10. DIR=$(dirname "$0")
  11. ret=0
  12. expect_success()
  13. {
  14. title="$1"
  15. shift
  16. echo "" >&2
  17. echo "$title ..." >&2
  18. set +e
  19. "$@"
  20. rc=$?
  21. set -e
  22. case "$rc" in
  23. 0)
  24. echo "ok: $title succeeded" >&2
  25. ;;
  26. 1)
  27. echo "FAIL: $title should work" >&2
  28. ret=$(( ret + 1 ))
  29. ;;
  30. *)
  31. echo "FAIL: something else went wrong" >&2
  32. ret=$(( ret + 1 ))
  33. ;;
  34. esac
  35. }
  36. expect_failure()
  37. {
  38. title="$1"
  39. shift
  40. echo "" >&2
  41. echo "$title ..." >&2
  42. set +e
  43. "$@"
  44. rc=$?
  45. set -e
  46. case "$rc" in
  47. 0)
  48. echo "FAIL: $title unexpectedly worked" >&2
  49. ret=$(( ret + 1 ))
  50. ;;
  51. 1)
  52. echo "ok: $title correctly failed" >&2
  53. ;;
  54. *)
  55. echo "FAIL: something else went wrong" >&2
  56. ret=$(( ret + 1 ))
  57. ;;
  58. esac
  59. }
  60. do_splice()
  61. {
  62. filename="$1"
  63. bytes="$2"
  64. expected="$3"
  65. report="$4"
  66. out=$("$DIR"/splice_read "$filename" "$bytes" | cat)
  67. if [ "$out" = "$expected" ] ; then
  68. echo " matched $report" >&2
  69. return 0
  70. else
  71. echo " no match: '$out' vs $report" >&2
  72. return 1
  73. fi
  74. }
  75. test_splice()
  76. {
  77. filename="$1"
  78. echo " checking $filename ..." >&2
  79. full=$(cat "$filename")
  80. rc=$?
  81. if [ $rc -ne 0 ] ; then
  82. return 2
  83. fi
  84. two=$(echo "$full" | grep -m1 . | cut -c-2)
  85. # Make sure full splice has the same contents as a standard read.
  86. echo " splicing 4096 bytes ..." >&2
  87. if ! do_splice "$filename" 4096 "$full" "full read" ; then
  88. return 1
  89. fi
  90. # Make sure a partial splice see the first two characters.
  91. echo " splicing 2 bytes ..." >&2
  92. if ! do_splice "$filename" 2 "$two" "'$two'" ; then
  93. return 1
  94. fi
  95. return 0
  96. }
  97. ### /proc/$pid/ has no splice interface; these should all fail.
  98. expect_failure "proc_single_open(), seq_read() splice" test_splice /proc/$$/limits
  99. expect_failure "special open(), seq_read() splice" test_splice /proc/$$/comm
  100. ### /proc/sys/ has a splice interface; these should all succeed.
  101. expect_success "proc_handler: proc_dointvec_minmax() splice" test_splice /proc/sys/fs/nr_open
  102. expect_success "proc_handler: proc_dostring() splice" test_splice /proc/sys/kernel/modprobe
  103. expect_success "proc_handler: special read splice" test_splice /proc/sys/kernel/version
  104. ### /sys/ has no splice interface; these should all fail.
  105. if ! [ -d /sys/module/test_module/sections ] ; then
  106. expect_success "test_module kernel module load" modprobe test_module
  107. fi
  108. expect_success "kernfs attr splice" test_splice /sys/module/test_module/coresize
  109. expect_success "kernfs binattr splice" test_splice /sys/module/test_module/sections/.init.text
  110. exit $ret