t_fuzzer_timeout.sh revision 1.2 1 1.1 kamil # Copyright (c) 2018 The NetBSD Foundation, Inc.
2 1.1 kamil # All rights reserved.
3 1.1 kamil #
4 1.1 kamil # This code is derived from software contributed to The NetBSD Foundation
5 1.1 kamil # by Yang Zheng.
6 1.1 kamil #
7 1.1 kamil # Redistribution and use in source and binary forms, with or without
8 1.1 kamil # modification, are permitted provided that the following conditions
9 1.1 kamil # are met:
10 1.1 kamil # 1. Redistributions of source code must retain the above copyright
11 1.1 kamil # notice, this list of conditions and the following disclaimer.
12 1.1 kamil # 2. Redistributions in binary form must reproduce the above copyright
13 1.1 kamil # notice, this list of conditions and the following disclaimer in the
14 1.1 kamil # documentation and/or other materials provided with the distribution.
15 1.1 kamil #
16 1.1 kamil # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 1.1 kamil # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.1 kamil # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.1 kamil # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 1.1 kamil # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.1 kamil # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.1 kamil # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.1 kamil # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.1 kamil # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.1 kamil # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.1 kamil # POSSIBILITY OF SUCH DAMAGE.
27 1.1 kamil #
28 1.1 kamil
29 1.1 kamil test_target()
30 1.1 kamil {
31 1.1 kamil SUPPORT='n'
32 1.1 kamil if uname -m | grep -q "amd64" && command -v c++ >/dev/null 2>&1 && \
33 1.1 kamil ! echo __clang__ | c++ -E - | grep -q __clang__; then
34 1.1 kamil # only clang with major version newer than 7 is supported
35 1.1 kamil CLANG_MAJOR=`echo __clang_major__ | c++ -E - | grep -o '^[[:digit:]]'`
36 1.1 kamil if [ "$CLANG_MAJOR" -ge "7" ]; then
37 1.1 kamil SUPPORT='y'
38 1.1 kamil fi
39 1.1 kamil fi
40 1.1 kamil }
41 1.1 kamil
42 1.1 kamil atf_test_case timeout
43 1.1 kamil timeout_head() {
44 1.1 kamil atf_set "descr" "Test thread sanitizer for timeout condition"
45 1.1 kamil atf_set "require.progs" "c++ paxctl"
46 1.1 kamil }
47 1.1 kamil
48 1.1 kamil atf_test_case timeout_profile
49 1.1 kamil timeout_profile_head() {
50 1.1 kamil atf_set "descr" "Test thread sanitizer for timeout with profiling option"
51 1.1 kamil atf_set "require.progs" "c++ paxctl"
52 1.1 kamil }
53 1.1 kamil atf_test_case timeout_pic
54 1.1 kamil timeout_pic_head() {
55 1.1 kamil atf_set "descr" "Test thread sanitizer for timeout with position independent code (PIC) flag"
56 1.1 kamil atf_set "require.progs" "c++ paxctl"
57 1.1 kamil }
58 1.1 kamil atf_test_case timeout_pie
59 1.1 kamil timeout_pie_head() {
60 1.1 kamil atf_set "descr" "Test thread sanitizer for timeout with position independent execution (PIE) flag"
61 1.1 kamil atf_set "require.progs" "c++ paxctl"
62 1.1 kamil }
63 1.1 kamil
64 1.1 kamil timeout_body(){
65 1.1 kamil cat > test.cc << EOF
66 1.1 kamil #include <stddef.h>
67 1.1 kamil #include <stdint.h>
68 1.1 kamil
69 1.1 kamil extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
70 1.1 kamil if (size > 0 && data[0] == 'b') while (1) ;
71 1.1 kamil return 0;
72 1.1 kamil }
73 1.1 kamil EOF
74 1.1 kamil
75 1.1 kamil c++ -fsanitize=fuzzer -o test test.cc
76 1.1 kamil paxctl +a test
77 1.1 kamil atf_check -s ignore -o ignore -e match:"ERROR: libFuzzer: timeout" ./test -timeout=5
78 1.1 kamil }
79 1.1 kamil
80 1.1 kamil timeout_profile_body(){
81 1.1 kamil cat > test.cc << EOF
82 1.1 kamil #include <stddef.h>
83 1.1 kamil #include <stdint.h>
84 1.1 kamil
85 1.1 kamil extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
86 1.1 kamil if (size > 0 && data[0] == 'b') while (1) ;
87 1.1 kamil return 0;
88 1.1 kamil }
89 1.1 kamil EOF
90 1.1 kamil
91 1.1 kamil c++ -fsanitize=fuzzer -o test -pg test.cc
92 1.1 kamil paxctl +a test
93 1.1 kamil atf_check -s ignore -o ignore -e match:"ERROR: libFuzzer: timeout" ./test -timeout=5
94 1.1 kamil }
95 1.1 kamil
96 1.1 kamil timeout_pic_body(){
97 1.1 kamil cat > test.cc << EOF
98 1.1 kamil #include <stddef.h>
99 1.1 kamil #include <stdint.h>
100 1.1 kamil int help(const uint8_t *data, size_t size);
101 1.1 kamil extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
102 1.1 kamil return help(data, size);
103 1.1 kamil }
104 1.1 kamil EOF
105 1.1 kamil
106 1.1 kamil cat > pic.cc << EOF
107 1.1 kamil #include <stddef.h>
108 1.1 kamil #include <stdint.h>
109 1.1 kamil
110 1.1 kamil int help(const uint8_t *data, size_t size) {
111 1.1 kamil if (size > 0 && data[0] == 'b') while (1) ;
112 1.1 kamil return 0;
113 1.1 kamil }
114 1.1 kamil EOF
115 1.1 kamil
116 1.1 kamil c++ -fsanitize=fuzzer -fPIC -shared -o libtest.so pic.cc
117 1.1 kamil c++ -o test test.cc -fsanitize=fuzzer -L. -ltest
118 1.1 kamil paxctl +a test
119 1.1 kamil
120 1.1 kamil export LD_LIBRARY_PATH=.
121 1.1 kamil atf_check -s ignore -o ignore -e match:"ERROR: libFuzzer: timeout" ./test -timeout=5
122 1.1 kamil }
123 1.1 kamil timeout_pie_body(){
124 1.1 kamil
125 1.1 kamil #check whether -pie flag is supported on this architecture
126 1.1 kamil if ! c++ -pie -dM -E - < /dev/null 2>/dev/null >/dev/null; then
127 1.1 kamil atf_set_skip "c++ -pie not supported on this architecture"
128 1.1 kamil fi
129 1.1 kamil cat > test.cc << EOF
130 1.1 kamil #include <stddef.h>
131 1.1 kamil #include <stdint.h>
132 1.1 kamil
133 1.1 kamil extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
134 1.1 kamil if (size > 0 && data[0] == 'b') while (1) ;
135 1.1 kamil return 0;
136 1.1 kamil }
137 1.1 kamil EOF
138 1.1 kamil
139 1.1 kamil c++ -fsanitize=fuzzer -o test -fpie -pie test.cc
140 1.1 kamil paxctl +a test
141 1.1 kamil atf_check -s ignore -o ignore -e match:"ERROR: libFuzzer: timeout" ./test -timeout=5
142 1.1 kamil }
143 1.1 kamil
144 1.1 kamil
145 1.1 kamil atf_test_case target_not_supported
146 1.1 kamil target_not_supported_head()
147 1.1 kamil {
148 1.1 kamil atf_set "descr" "Test forced skip"
149 1.1 kamil }
150 1.1 kamil
151 1.2 kamil target_not_supported_body()
152 1.2 kamil {
153 1.2 kamil atf_skip "Target is not supported"
154 1.2 kamil }
155 1.2 kamil
156 1.1 kamil atf_init_test_cases()
157 1.1 kamil {
158 1.1 kamil test_target
159 1.1 kamil test $SUPPORT = 'n' && {
160 1.1 kamil atf_add_test_case target_not_supported
161 1.1 kamil return 0
162 1.1 kamil }
163 1.1 kamil atf_add_test_case timeout
164 1.1 kamil atf_add_test_case timeout_profile
165 1.1 kamil atf_add_test_case timeout_pie
166 1.1 kamil atf_add_test_case timeout_pic
167 1.1 kamil }
168