Back to home page

OSCL-LXR

 
 

    


0001 #!/bin/sh
0002 # SPDX-License-Identifier: GPL-2.0
0003 #
0004 # Test for mishandling of splice() on pseudofilesystems, which should catch
0005 # bugs like 11990a5bd7e5 ("module: Correctly truncate sysfs sections output")
0006 #
0007 # Since splice fallback was removed as part of the set_fs() rework, many of these
0008 # tests expect to fail now. See https://lore.kernel.org/lkml/202009181443.C2179FB@keescook/
0009 set -e
0010 
0011 DIR=$(dirname "$0")
0012 
0013 ret=0
0014 
0015 expect_success()
0016 {
0017         title="$1"
0018         shift
0019 
0020         echo "" >&2
0021         echo "$title ..." >&2
0022 
0023         set +e
0024         "$@"
0025         rc=$?
0026         set -e
0027 
0028         case "$rc" in
0029         0)
0030                 echo "ok: $title succeeded" >&2
0031                 ;;
0032         1)
0033                 echo "FAIL: $title should work" >&2
0034                 ret=$(( ret + 1 ))
0035                 ;;
0036         *)
0037                 echo "FAIL: something else went wrong" >&2
0038                 ret=$(( ret + 1 ))
0039                 ;;
0040         esac
0041 }
0042 
0043 expect_failure()
0044 {
0045         title="$1"
0046         shift
0047 
0048         echo "" >&2
0049         echo "$title ..." >&2
0050 
0051         set +e
0052         "$@"
0053         rc=$?
0054         set -e
0055 
0056         case "$rc" in
0057         0)
0058                 echo "FAIL: $title unexpectedly worked" >&2
0059                 ret=$(( ret + 1 ))
0060                 ;;
0061         1)
0062                 echo "ok: $title correctly failed" >&2
0063                 ;;
0064         *)
0065                 echo "FAIL: something else went wrong" >&2
0066                 ret=$(( ret + 1 ))
0067                 ;;
0068         esac
0069 }
0070 
0071 do_splice()
0072 {
0073         filename="$1"
0074         bytes="$2"
0075         expected="$3"
0076         report="$4"
0077 
0078         out=$("$DIR"/splice_read "$filename" "$bytes" | cat)
0079         if [ "$out" = "$expected" ] ; then
0080                 echo "      matched $report" >&2
0081                 return 0
0082         else
0083                 echo "      no match: '$out' vs $report" >&2
0084                 return 1
0085         fi
0086 }
0087 
0088 test_splice()
0089 {
0090         filename="$1"
0091 
0092         echo "  checking $filename ..." >&2
0093 
0094         full=$(cat "$filename")
0095         rc=$?
0096         if [ $rc -ne 0 ] ; then
0097                 return 2
0098         fi
0099 
0100         two=$(echo "$full" | grep -m1 . | cut -c-2)
0101 
0102         # Make sure full splice has the same contents as a standard read.
0103         echo "    splicing 4096 bytes ..." >&2
0104         if ! do_splice "$filename" 4096 "$full" "full read" ; then
0105                 return 1
0106         fi
0107 
0108         # Make sure a partial splice see the first two characters.
0109         echo "    splicing 2 bytes ..." >&2
0110         if ! do_splice "$filename" 2 "$two" "'$two'" ; then
0111                 return 1
0112         fi
0113 
0114         return 0
0115 }
0116 
0117 ### /proc/$pid/ has no splice interface; these should all fail.
0118 expect_failure "proc_single_open(), seq_read() splice" test_splice /proc/$$/limits
0119 expect_failure "special open(), seq_read() splice" test_splice /proc/$$/comm
0120 
0121 ### /proc/sys/ has a splice interface; these should all succeed.
0122 expect_success "proc_handler: proc_dointvec_minmax() splice" test_splice /proc/sys/fs/nr_open
0123 expect_success "proc_handler: proc_dostring() splice" test_splice /proc/sys/kernel/modprobe
0124 expect_success "proc_handler: special read splice" test_splice /proc/sys/kernel/version
0125 
0126 ### /sys/ has no splice interface; these should all fail.
0127 if ! [ -d /sys/module/test_module/sections ] ; then
0128         expect_success "test_module kernel module load" modprobe test_module
0129 fi
0130 expect_failure "kernfs attr splice" test_splice /sys/module/test_module/coresize
0131 expect_failure "kernfs binattr splice" test_splice /sys/module/test_module/sections/.init.text
0132 
0133 exit $ret