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