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