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