11.1Skamil# Copyright (c) 2018 The NetBSD Foundation, Inc.
21.1Skamil# All rights reserved.
31.1Skamil#
41.1Skamil# This code is derived from software contributed to The NetBSD Foundation
51.1Skamil# by Yang Zheng.
61.1Skamil#
71.1Skamil# Redistribution and use in source and binary forms, with or without
81.1Skamil# modification, are permitted provided that the following conditions
91.1Skamil# are met:
101.1Skamil# 1. Redistributions of source code must retain the above copyright
111.1Skamil#    notice, this list of conditions and the following disclaimer.
121.1Skamil# 2. Redistributions in binary form must reproduce the above copyright
131.1Skamil#    notice, this list of conditions and the following disclaimer in the
141.1Skamil#    documentation and/or other materials provided with the distribution.
151.1Skamil#
161.1Skamil# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
171.1Skamil# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
181.1Skamil# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
191.1Skamil# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
201.1Skamil# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
211.1Skamil# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
221.1Skamil# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
231.1Skamil# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
241.1Skamil# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
251.1Skamil# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
261.1Skamil# POSSIBILITY OF SUCH DAMAGE.
271.1Skamil#
281.1Skamil
291.1Skamiltest_target()
301.1Skamil{
311.1Skamil	SUPPORT='n'
321.1Skamil	if uname -m | grep -q "amd64" && command -v c++ >/dev/null 2>&1 && \
331.1Skamil		   ! echo __clang__ | c++ -E - | grep -q __clang__; then
341.1Skamil		# only clang with major version newer than 7 is supported
351.1Skamil		CLANG_MAJOR=`echo __clang_major__ | c++ -E - | grep -o '^[[:digit:]]'`
361.1Skamil		if [ "$CLANG_MAJOR" -ge "7" ]; then
371.1Skamil			SUPPORT='y'
381.1Skamil		fi
391.1Skamil	fi
401.1Skamil}
411.1Skamil
421.1Skamilatf_test_case heap
431.1Skamilheap_head() {
441.1Skamil	atf_set "descr" "Test memory sanitizer for uninitialized heap value case"
451.1Skamil	atf_set "require.progs" "c++ paxctl"
461.1Skamil}
471.1Skamil
481.1Skamilatf_test_case heap_profile
491.1Skamilheap_profile_head() {
501.1Skamil	atf_set "descr" "Test memory sanitizer for uninitialized heap value with profiling option"
511.1Skamil	atf_set "require.progs" "c++ paxctl"
521.1Skamil}
531.1Skamilatf_test_case heap_pic
541.1Skamilheap_pic_head() {
551.1Skamil	atf_set "descr" "Test memory sanitizer for uninitialized heap value with position independent code (PIC) flag"
561.1Skamil	atf_set "require.progs" "c++ paxctl"
571.1Skamil}
581.1Skamilatf_test_case heap_pie
591.1Skamilheap_pie_head() {
601.1Skamil	atf_set "descr" "Test memory sanitizer for uninitialized heap value with position independent execution (PIE) flag"
611.1Skamil	atf_set "require.progs" "c++ paxctl"
621.1Skamil}
631.1Skamil
641.1Skamilheap_body(){
651.1Skamil	cat > test.cc << EOF
661.1Skamil#include <stdlib.h>
671.1Skamilint main() { int *a = (int *)malloc(sizeof(int)); return *a; }
681.1SkamilEOF
691.1Skamil
701.1Skamil	c++ -fsanitize=memory -o test test.cc
711.1Skamil	paxctl +a test
721.1Skamil	atf_check -s ignore -o ignore -e match:"WARNING: MemorySanitizer: use-of-uninitialized-value" ./test
731.1Skamil}
741.1Skamil
751.1Skamilheap_profile_body(){
761.1Skamil	cat > test.cc << EOF
771.1Skamil#include <stdlib.h>
781.1Skamilint main() { int *a = (int *)malloc(sizeof(int)); return *a; }
791.1SkamilEOF
801.1Skamil
811.4Sskrll	c++ -fsanitize=memory -static -o test -pg test.cc
821.1Skamil	paxctl +a test
831.1Skamil	atf_check -s ignore -o ignore -e match:"WARNING: MemorySanitizer: use-of-uninitialized-value" ./test
841.1Skamil}
851.1Skamil
861.1Skamilheap_pic_body(){
871.1Skamil	cat > test.cc << EOF
881.1Skamil#include <stdio.h>
891.1Skamil#include <stdlib.h>
901.1Skamilint help(int);
911.1Skamilint main(int argc, char **argv) {return help(argc);}
921.1SkamilEOF
931.1Skamil
941.1Skamil	cat > pic.cc << EOF
951.1Skamil#include <stdlib.h>
961.1Skamilint help(int argc) { int *a = (int *)malloc(sizeof(int)); return *a; }
971.1SkamilEOF
981.1Skamil
991.1Skamil	c++ -fsanitize=memory -fPIC -shared -o libtest.so pic.cc
1001.1Skamil	c++ -o test test.cc -fsanitize=memory -L. -ltest
1011.1Skamil	paxctl +a test
1021.1Skamil
1031.1Skamil	export LD_LIBRARY_PATH=.
1041.1Skamil	atf_check -s ignore -o ignore -e match:"WARNING: MemorySanitizer: use-of-uninitialized-value" ./test
1051.1Skamil}
1061.1Skamilheap_pie_body(){
1071.3Sskrll
1081.1Skamil	#check whether -pie flag is supported on this architecture
1091.3Sskrll	if ! c++ -pie -dM -E - < /dev/null 2>/dev/null >/dev/null; then
1101.1Skamil		atf_set_skip "c++ -pie not supported on this architecture"
1111.1Skamil	fi
1121.1Skamil	cat > test.cc << EOF
1131.1Skamil#include <stdlib.h>
1141.1Skamilint main() { int *a = (int *)malloc(sizeof(int)); return *a; }
1151.1SkamilEOF
1161.1Skamil
1171.1Skamil	c++ -fsanitize=memory -o test -fpie -pie test.cc
1181.1Skamil	paxctl +a test
1191.1Skamil	atf_check -s ignore -o ignore -e match:"WARNING: MemorySanitizer: use-of-uninitialized-value" ./test
1201.1Skamil}
1211.1Skamil
1221.1Skamilatf_test_case target_not_supported
1231.1Skamiltarget_not_supported_head()
1241.1Skamil{
1251.1Skamil	atf_set "descr" "Test forced skip"
1261.1Skamil}
1271.1Skamil
1281.2Skamiltarget_not_supported_body()
1291.2Skamil{
1301.2Skamil	atf_skip "Target is not supported"
1311.2Skamil}
1321.2Skamil
1331.1Skamilatf_init_test_cases()
1341.1Skamil{
1351.1Skamil	test_target
1361.1Skamil	test $SUPPORT = 'n' && {
1371.1Skamil		atf_add_test_case target_not_supported
1381.1Skamil		return 0
1391.1Skamil	}
1401.1Skamil	atf_add_test_case heap
1411.1Skamil	atf_add_test_case heap_profile
1421.1Skamil	atf_add_test_case heap_pie
1431.1Skamil	atf_add_test_case heap_pic
1441.1Skamil}
145