1210753Srpaulo#!/usr/bin/ksh
2210753Srpaulo#
3210753Srpaulo# CDDL HEADER START
4210753Srpaulo#
5210753Srpaulo# The contents of this file are subject to the terms of the
6210753Srpaulo# Common Development and Distribution License (the "License").
7210753Srpaulo# You may not use this file except in compliance with the License.
8210753Srpaulo#
9210753Srpaulo# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10210753Srpaulo# or http://www.opensolaris.org/os/licensing.
11210753Srpaulo# See the License for the specific language governing permissions
12210753Srpaulo# and limitations under the License.
13210753Srpaulo#
14210753Srpaulo# When distributing Covered Code, include this CDDL HEADER in each
15210753Srpaulo# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16210753Srpaulo# If applicable, add the following below this CDDL HEADER, with the
17210753Srpaulo# fields enclosed by brackets "[]" replaced with your own identifying
18210753Srpaulo# information: Portions Copyright [yyyy] [name of copyright owner]
19210753Srpaulo#
20210753Srpaulo# CDDL HEADER END
21210753Srpaulo#
22210753Srpaulo
23210753Srpaulo#
24210753Srpaulo# Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
25210753Srpaulo# Use is subject to license terms.
26210753Srpaulo#
27210753Srpaulo
28210753Srpaulo#
29210753Srpaulo# This test verifies that a program that corrupts its own environment
30210753Srpaulo# without inducing a crash does not crash solely due to drti.o's use of
31210753Srpaulo# getenv(3C).
32210753Srpaulo#
33210753Srpaulo
34210753SrpauloPATH=/usr/bin:/usr/sbin:$PATH
35210753Srpaulo
36210753Srpauloif (( $# != 1 )); then
37210753Srpaulo	print -u2 'expected one argument: <dtrace-path>'
38210753Srpaulo	exit 2
39210753Srpaulofi
40210753Srpaulo
41210753Srpaulo#
42210753Srpaulo# jdtrace does not implement the -h option that is required to generate
43210753Srpaulo# C header files.
44210753Srpaulo#
45210753Srpauloif [[ "$1" == */jdtrace ]]; then
46210753Srpaulo	exit 0
47210753Srpaulofi
48210753Srpaulo
49210753Srpaulodtrace="$1"
50210753Srpaulostartdir="$PWD"
51210753Srpaulodir=$(mktemp -td drtiXXXXXX)
52210753Srpauloif (( $? != 0 )); then
53210753Srpaulo	print -u2 'Could not create safe temporary directory'
54210753Srpaulo	exit 2
55210753Srpaulofi
56210753Srpaulo
57210753Srpaulocd "$dir"
58210753Srpaulo
59210753Srpaulocat > Makefile <<EOF
60210753Srpauloall: main
61210753Srpaulo
62210753Srpaulomain: main.o prov.o
63210753Srpaulo	\$(CC) -o main main.o prov.o
64210753Srpaulo
65210753Srpaulomain.o: main.c prov.h
66210753Srpaulo	\$(CC) -c main.c
67210753Srpaulo
68210753Srpauloprov.h: prov.d
69210753Srpaulo	$dtrace -h -s prov.d
70210753Srpaulo
71210753Srpauloprov.o: prov.d main.o
72210753Srpaulo	$dtrace -G -32 -s prov.d main.o
73210753SrpauloEOF
74210753Srpaulo
75210753Srpaulocat > prov.d <<EOF
76210753Srpauloprovider tester {
77210753Srpaulo	probe entry();
78210753Srpaulo};
79210753SrpauloEOF
80210753Srpaulo
81210753Srpaulocat > main.c <<EOF
82210753Srpaulo#include <stdlib.h>
83210753Srpaulo#include <sys/sdt.h>
84210753Srpaulo#include "prov.h"
85210753Srpaulo
86210753Srpauloint
87210753Srpaulomain(int argc, char **argv, char **envp)
88210753Srpaulo{
89210753Srpaulo	envp[0] = (char*)0xff;
90210753Srpaulo	TESTER_ENTRY();
91210753Srpaulo	return 0;
92210753Srpaulo}
93210753SrpauloEOF
94210753Srpaulo
95210753Srpaulomake > /dev/null
96210753Srpaulostatus=$?
97210753Srpauloif (( $status != 0 )) ; then
98210753Srpaulo	print -u2 "failed to build"
99210753Srpauloelse
100210753Srpaulo	./main
101210753Srpaulo	status=$?
102210753Srpaulofi
103210753Srpaulo
104210753Srpaulocd "$startdir"
105210753Srpaulorm -rf "$dir"
106210753Srpaulo
107210753Srpauloexit $status
108