Lev Walkin | 700df49 | 2017-08-10 14:59:15 -0700 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | |
| 3 | # |
| 4 | # This script is designed to quickly create lots of files in underlying |
| 5 | # test-* directories, do lots of other magic stuff and exit cleanly. |
| 6 | # |
| 7 | |
| 8 | set -e |
| 9 | |
| 10 | if [ "x$1" = "x" ]; then |
Lev Walkin | 15734f9 | 2017-09-30 11:41:04 -0700 | [diff] [blame] | 11 | echo "Usage: $0 check-src/<check-NN.c>" |
| 12 | exit 1 |
Lev Walkin | 700df49 | 2017-08-10 14:59:15 -0700 | [diff] [blame] | 13 | fi |
| 14 | |
| 15 | srcdir="${srcdir:-.}" |
| 16 | abs_top_srcdir="${abs_top_srcdir:-$(pwd)/../../}" |
| 17 | abs_top_builddir="${abs_top_builddir:-$(pwd)/../../}" |
| 18 | |
| 19 | if echo "$*" | grep -q -- -- ; then |
| 20 | TEST_DRIVER=$(echo "$*" | sed -e 's/ -- .*/ -- /g') |
| 21 | source_full=$(echo "$*" | sed -e 's/.* //g') |
| 22 | else |
| 23 | TEST_DRIVER="" |
| 24 | source_full="$1" |
| 25 | fi |
| 26 | |
| 27 | # Compute the .asn1 spec name by the given file name. |
| 28 | source_short=$(echo "$source_full" | sed -e 's/.*\///') |
| 29 | testno=$(echo "$source_short" | cut -f2 -d'-' | cut -f1 -d'.') |
| 30 | |
Lev Walkin | b5cdc5d | 2017-09-15 21:57:46 -0700 | [diff] [blame] | 31 | args=$(echo "$source_short" | sed -E -e 's/\.c+$//') |
Lev Walkin | 387a8f0 | 2017-09-15 23:24:00 -0700 | [diff] [blame] | 32 | source_obj=$(echo "$source_short" | sed -E -e 's/\.c+$/.o/') |
Lev Walkin | 7b04d79 | 2017-09-16 00:04:18 -0700 | [diff] [blame] | 33 | ext=$(echo "$source_short" | sed -E -e 's/.*\.(c+)$/\1/') |
| 34 | |
Lev Walkin | 700df49 | 2017-08-10 14:59:15 -0700 | [diff] [blame] | 35 | OFS=$IFS |
| 36 | IFS="." |
| 37 | set $args |
Lev Walkin | c8c286a | 2017-09-18 03:19:37 -0700 | [diff] [blame] | 38 | data_dir=$(echo "$1" | sed -e s/check-/data-/) |
Lev Walkin | 700df49 | 2017-08-10 14:59:15 -0700 | [diff] [blame] | 39 | shift |
| 40 | IFS=$OFS |
| 41 | AFLAGS="$*" |
| 42 | |
Lev Walkin | c8c286a | 2017-09-18 03:19:37 -0700 | [diff] [blame] | 43 | if [ -d ${data_dir} ]; then |
| 44 | OPT_DATA_DIR="../${data_dir}" |
| 45 | else |
| 46 | OPT_DATA_DIR="" |
| 47 | fi |
| 48 | |
| 49 | if test "${LIBFUZZER_CFLAGS}" && grep LLVMFuzzer ${source_full} > /dev/null; |
| 50 | then |
| 51 | MAKE_FUZZER=yes |
| 52 | else |
| 53 | MAKE_FUZZER=no |
| 54 | fi |
| 55 | |
Lev Walkin | 700df49 | 2017-08-10 14:59:15 -0700 | [diff] [blame] | 56 | # Assume the test fails. Will be removed when it passes well. |
| 57 | testdir=test-${args} |
| 58 | if [ -f "${testdir}-FAILED" ]; then |
| 59 | rm -rf "${testdir}" |
| 60 | fi |
| 61 | touch "${testdir}-FAILED" |
| 62 | |
| 63 | mkdir -p "${testdir}" |
| 64 | ln -fns "../${source_full}" "${testdir}" |
| 65 | |
| 66 | asn_module=$(echo "${abs_top_srcdir}/tests/tests-asn1c-compiler/${testno}"-*.asn1) |
| 67 | |
| 68 | AUTOGENERATED="# This file is autogenerated by $0 ${source_full} ${AFLAGS}" |
| 69 | |
| 70 | # Create a common Makefile for the project |
Lev Walkin | 40b8a7a | 2017-10-08 22:36:29 -0700 | [diff] [blame] | 71 | cat <<END_MAKEFILE > "${testdir}/Makefile" |
Lev Walkin | 700df49 | 2017-08-10 14:59:15 -0700 | [diff] [blame] | 72 | ${AUTOGENERATED} |
Lev Walkin | 026055f | 2017-08-27 01:28:59 -0700 | [diff] [blame] | 73 | COMMON_FLAGS= -I. |
Lev Walkin | 24cf97d | 2017-09-15 20:34:25 -0700 | [diff] [blame] | 74 | CFLAGS = \${COMMON_FLAGS} ${CFLAGS:-} -g -O1 |
Lev Walkin | 40b8a7a | 2017-10-08 22:36:29 -0700 | [diff] [blame] | 75 | CFLAGS += -DSRCDIR=../${srcdir} |
Lev Walkin | b5cdc5d | 2017-09-15 21:57:46 -0700 | [diff] [blame] | 76 | CXXFLAGS = \${CFLAGS} ${CXXFLAGS} |
Lev Walkin | 387a8f0 | 2017-09-15 23:24:00 -0700 | [diff] [blame] | 77 | LIBFUZZER_CFLAGS = ${LIBFUZZER_CFLAGS} |
Lev Walkin | 700df49 | 2017-08-10 14:59:15 -0700 | [diff] [blame] | 78 | LDFLAGS = ${LDFLAGS:-} |
Vasil Velichkov | 5532a1d | 2017-10-10 17:58:49 +0300 | [diff] [blame] | 79 | ASAN_ENV_FLAGS = ${ASAN_ENV_FLAGS:-} |
Lev Walkin | 700df49 | 2017-08-10 14:59:15 -0700 | [diff] [blame] | 80 | |
Lev Walkin | 40b8a7a | 2017-10-08 22:36:29 -0700 | [diff] [blame] | 81 | ASN_PROGRAM = check-program |
| 82 | ASN_PROGRAM_SOURCES = ${source_short} |
| 83 | |
Lev Walkin | 700df49 | 2017-08-10 14:59:15 -0700 | [diff] [blame] | 84 | CC ?= ${CC} |
Lev Walkin | b5cdc5d | 2017-09-15 21:57:46 -0700 | [diff] [blame] | 85 | CXX ?= ${CXX} |
Lev Walkin | 700df49 | 2017-08-10 14:59:15 -0700 | [diff] [blame] | 86 | |
Lev Walkin | 40b8a7a | 2017-10-08 22:36:29 -0700 | [diff] [blame] | 87 | -include Makefile.am.example |
Lev Walkin | 700df49 | 2017-08-10 14:59:15 -0700 | [diff] [blame] | 88 | |
Lev Walkin | 40b8a7a | 2017-10-08 22:36:29 -0700 | [diff] [blame] | 89 | all: compiled-module |
| 90 | \$(MAKE) check-program |
Lev Walkin | 700df49 | 2017-08-10 14:59:15 -0700 | [diff] [blame] | 91 | |
| 92 | # Compile the corresponding .asn1 spec. |
| 93 | compiled-module: ${asn_module} ${abs_top_builddir}/asn1c/asn1c |
| 94 | ${abs_top_builddir}/asn1c/asn1c \\ |
| 95 | -S ${abs_top_srcdir}/skeletons \\ |
| 96 | -Wdebug-compiler \\ |
| 97 | ${AFLAGS} ${asn_module} |
Lev Walkin | 15b26e6 | 2017-09-26 23:04:57 -0700 | [diff] [blame] | 98 | rm -f converter-example.c |
Lev Walkin | 700df49 | 2017-08-10 14:59:15 -0700 | [diff] [blame] | 99 | @touch compiled-module |
| 100 | |
Lev Walkin | 40b8a7a | 2017-10-08 22:36:29 -0700 | [diff] [blame] | 101 | END_MAKEFILE |
Lev Walkin | 387a8f0 | 2017-09-15 23:24:00 -0700 | [diff] [blame] | 102 | |
| 103 | |
| 104 | if [ "${MAKE_FUZZER}" != "yes" ]; then |
Lev Walkin | 1900888 | 2017-09-27 02:29:27 +0000 | [diff] [blame] | 105 | CHECK_FUZZER="@echo \"No fuzzer defined, skipping.\"" |
Lev Walkin | 40b8a7a | 2017-10-08 22:36:29 -0700 | [diff] [blame] | 106 | cat <<END_MAKEFILE >> "${testdir}/Makefile" |
Lev Walkin | 1900888 | 2017-09-27 02:29:27 +0000 | [diff] [blame] | 107 | .PHONY: check-fuzzer |
Lev Walkin | 387a8f0 | 2017-09-15 23:24:00 -0700 | [diff] [blame] | 108 | check-fuzzer: |
Lev Walkin | 40b8a7a | 2017-10-08 22:36:29 -0700 | [diff] [blame] | 109 | END_MAKEFILE |
Lev Walkin | 387a8f0 | 2017-09-15 23:24:00 -0700 | [diff] [blame] | 110 | else |
Lev Walkin | dcae9ce | 2017-09-18 20:13:36 -0700 | [diff] [blame] | 111 | CHECK_FUZZER="ASAN_OPTIONS=detect_leaks=1 UBSAN_OPTIONS=print_stacktrace=1 ./check-fuzzer -timeout=3 -max_total_time=60 -max_len=512 ${OPT_DATA_DIR}" |
Lev Walkin | 40b8a7a | 2017-10-08 22:36:29 -0700 | [diff] [blame] | 112 | cat <<END_MAKEFILE >> "${testdir}/Makefile" |
| 113 | check-fuzzer: ${source_short} \$(ASN_LIBRARY) |
| 114 | ${CC} \$(CFLAGS) \$(LIBFUZZER_CFLAGS) \$(LDFLAGS) -o check-fuzzer \$^ |
| 115 | END_MAKEFILE |
Lev Walkin | 387a8f0 | 2017-09-15 23:24:00 -0700 | [diff] [blame] | 116 | fi |
| 117 | |
Lev Walkin | 40b8a7a | 2017-10-08 22:36:29 -0700 | [diff] [blame] | 118 | cat <<END_MAKEFILE >> "${testdir}/Makefile" |
Lev Walkin | 387a8f0 | 2017-09-15 23:24:00 -0700 | [diff] [blame] | 119 | |
Lev Walkin | 40b8a7a | 2017-10-08 22:36:29 -0700 | [diff] [blame] | 120 | check-succeeded: compiled-module \$(ASN_LIBRARY) ${source_short} |
| 121 | \$(MAKE) check-program |
Lev Walkin | 387a8f0 | 2017-09-15 23:24:00 -0700 | [diff] [blame] | 122 | \$(MAKE) check-fuzzer |
Lev Walkin | 700df49 | 2017-08-10 14:59:15 -0700 | [diff] [blame] | 123 | @rm -f check-succeeded |
Vasil Velichkov | 5532a1d | 2017-10-10 17:58:49 +0300 | [diff] [blame] | 124 | \${ASAN_ENV_FLAGS} ./check-program |
Lev Walkin | dcae9ce | 2017-09-18 20:13:36 -0700 | [diff] [blame] | 125 | \$(MAKE) fuzz |
Lev Walkin | 700df49 | 2017-08-10 14:59:15 -0700 | [diff] [blame] | 126 | @touch check-succeeded |
Lev Walkin | 1900888 | 2017-09-27 02:29:27 +0000 | [diff] [blame] | 127 | @echo "OK: ${source_full}" |
Lev Walkin | 700df49 | 2017-08-10 14:59:15 -0700 | [diff] [blame] | 128 | |
Lev Walkin | dcae9ce | 2017-09-18 20:13:36 -0700 | [diff] [blame] | 129 | .PHONY: fuzz |
| 130 | fuzz: |
| 131 | ${CHECK_FUZZER} |
| 132 | |
Lev Walkin | 700df49 | 2017-08-10 14:59:15 -0700 | [diff] [blame] | 133 | check: check-succeeded |
| 134 | |
Lev Walkin | 40b8a7a | 2017-10-08 22:36:29 -0700 | [diff] [blame] | 135 | END_MAKEFILE |
Lev Walkin | 700df49 | 2017-08-10 14:59:15 -0700 | [diff] [blame] | 136 | |
| 137 | # Perform building and checking |
Lev Walkin | 40b8a7a | 2017-10-08 22:36:29 -0700 | [diff] [blame] | 138 | ${TEST_DRIVER} ${MAKE:-make} -C "${testdir}" check |
Lev Walkin | 700df49 | 2017-08-10 14:59:15 -0700 | [diff] [blame] | 139 | |
| 140 | # Make sure the test is not marked as failed any longer. |
| 141 | rm -f "${testdir}-FAILED" |