NativeEnumerated.c vars NULL init and check
[com/asn1c.git] / tests / tests-c-compiler / check-assembly.sh
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
11         echo "Usage: $0 check-src/<check-NN.c>"
12         exit 1
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
31 args=$(echo "$source_short" | sed -E -e 's/\.c+$//')
32 source_obj=$(echo "$source_short" | sed -E -e 's/\.c+$/.o/')
33 ext=$(echo "$source_short" | sed -E -e 's/.*\.(c+)$/\1/')
34
35 OFS=$IFS
36 IFS="."
37 set $args
38 data_dir=$(echo "$1" | sed -e s/check-/data-/)
39 shift
40 IFS=$OFS
41 AFLAGS="$*"
42
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
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
71 cat <<END_MAKEFILE > "${testdir}/Makefile"
72 ${AUTOGENERATED}
73 COMMON_FLAGS= -I.
74 CFLAGS = \${COMMON_FLAGS} ${CFLAGS:-} -g -O1
75 CFLAGS += -DSRCDIR=../${srcdir}
76 CXXFLAGS = \${CFLAGS} ${CXXFLAGS}
77 LIBFUZZER_CFLAGS = ${LIBFUZZER_CFLAGS}
78 LDFLAGS = ${LDFLAGS:-}
79 ASAN_ENV_FLAGS = ${ASAN_ENV_FLAGS:-}
80
81 ASN_PROGRAM = check-program
82 ASN_PROGRAM_SRCS = ${source_short}
83
84 CC ?= ${CC}
85 CXX ?= ${CXX}
86
87 -include converter-example.mk
88
89 all: compiled-module
90         \$(MAKE) check-program
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                 -flink-skeletons                \\
97                 -Wdebug-compiler                \\
98                 ${AFLAGS} ${asn_module}
99         rm -f converter-example.c
100         @touch compiled-module
101
102 END_MAKEFILE
103
104
105 if [ "${MAKE_FUZZER}" != "yes" ]; then
106     CHECK_FUZZER="@echo \"No fuzzer defined, skipping.\""
107 cat <<END_MAKEFILE >> "${testdir}/Makefile"
108 .PHONY: check-fuzzer
109 check-fuzzer:
110 END_MAKEFILE
111 else
112     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}"
113 cat <<END_MAKEFILE >> "${testdir}/Makefile"
114 check-fuzzer: ${source_short} \$(ASN_LIBRARY)
115         ${CC} \$(CFLAGS) \$(LIBFUZZER_CFLAGS) \$(LDFLAGS) -o check-fuzzer \$^
116 END_MAKEFILE
117 fi
118
119 cat <<END_MAKEFILE >> "${testdir}/Makefile"
120
121 check-succeeded: compiled-module \$(ASN_LIBRARY) ${source_short}
122         \$(MAKE) check-program
123         \$(MAKE) check-fuzzer
124         @rm -f check-succeeded
125         \${ASAN_ENV_FLAGS} ./check-program
126         \$(MAKE) fuzz
127         @touch check-succeeded
128         @echo "OK: ${source_full}"
129
130 .PHONY: fuzz
131 fuzz:
132         ${CHECK_FUZZER}
133
134 check: check-succeeded
135
136 END_MAKEFILE
137
138 # Perform building and checking
139 ${TEST_DRIVER} ${MAKE:-make} -C "${testdir}" check
140
141 # Make sure the test is not marked as failed any longer.
142 rm -f "${testdir}-FAILED"