]> granicus.if.org Git - strace/blob - tests/execveat.c
tests: add a list of executables without side effects
[strace] / tests / execveat.c
1 /*
2  * This file is part of execveat strace test.
3  *
4  * Copyright (c) 2015-2016 Dmitry V. Levin <ldv@altlinux.org>
5  * All rights reserved.
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  * 3. The name of the author may not be used to endorse or promote products
16  *    derived from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 #include "tests.h"
31 #include <asm/unistd.h>
32 #include "scno.h"
33
34 #ifdef __NR_execveat
35
36 # include <stdio.h>
37 # include <unistd.h>
38
39 # define FILENAME "test.execveat\nfilename"
40 # define Q_FILENAME "test.execveat\\nfilename"
41
42 static const char * const argv[] = {
43         FILENAME, "first", "second", (const char *) -1L,
44         (const char *) -2L, (const char *) -3L
45 };
46 static const char * const q_argv[] = {
47         Q_FILENAME, "first", "second"
48 };
49
50 static const char * const envp[] = {
51         "foobar=1", "foo\nbar=2", (const char *) -1L,
52         (const char *) -2L, (const char *) -3L
53 };
54 static const char * const q_envp[] = {
55         "foobar=1", "foo\\nbar=2"
56 };
57
58 int
59 main(void)
60 {
61         const char ** const tail_argv = tail_memdup(argv, sizeof(argv));
62         const char ** const tail_envp = tail_memdup(envp, sizeof(envp));
63
64         syscall(__NR_execveat, -100, FILENAME, tail_argv, tail_envp, 0x1100);
65         printf("execveat(AT_FDCWD, \"%s\""
66                ", [\"%s\", \"%s\", \"%s\", %p, %p, %p, ???]"
67 #if VERBOSE
68                ", [\"%s\", \"%s\", %p, %p, %p, ???]"
69 #else
70                ", [/* 5 vars, unterminated */]"
71 #endif
72                ", AT_SYMLINK_NOFOLLOW|AT_EMPTY_PATH) = -1 %s (%m)\n",
73                Q_FILENAME, q_argv[0], q_argv[1], q_argv[2],
74                argv[3], argv[4], argv[5],
75 #if VERBOSE
76                q_envp[0], q_envp[1], envp[2], envp[3], envp[4],
77 #endif
78                errno2name());
79
80         tail_argv[ARRAY_SIZE(q_argv)] = NULL;
81         tail_envp[ARRAY_SIZE(q_envp)] = NULL;
82
83         syscall(__NR_execveat, -100, FILENAME, tail_argv, tail_envp, 0x1100);
84         printf("execveat(AT_FDCWD, \"%s\", [\"%s\", \"%s\", \"%s\"]"
85 #if VERBOSE
86                ", [\"%s\", \"%s\"]"
87 #else
88                ", [/* 2 vars */]"
89 #endif
90                ", AT_SYMLINK_NOFOLLOW|AT_EMPTY_PATH) = -1 %s (%m)\n",
91                Q_FILENAME, q_argv[0], q_argv[1], q_argv[2],
92 #if VERBOSE
93                q_envp[0], q_envp[1],
94 #endif
95                errno2name());
96
97         syscall(__NR_execveat, -100, FILENAME, tail_argv + 2, tail_envp + 1, 0x1100);
98         printf("execveat(AT_FDCWD, \"%s\", [\"%s\"]"
99 #if VERBOSE
100                ", [\"%s\"]"
101 #else
102                ", [/* 1 var */]"
103 #endif
104                ", AT_SYMLINK_NOFOLLOW|AT_EMPTY_PATH) = -1 %s (%m)\n",
105                Q_FILENAME, q_argv[2],
106 #if VERBOSE
107                q_envp[1],
108 #endif
109                errno2name());
110
111         TAIL_ALLOC_OBJECT_CONST_PTR(char *, empty);
112         char **const efault = empty + 1;
113         *empty = NULL;
114
115         syscall(__NR_execveat, -100, FILENAME, empty, empty, 0x1100);
116         printf("execveat(AT_FDCWD, \"%s\", []"
117 #if VERBOSE
118                ", []"
119 #else
120                ", [/* 0 vars */]"
121 #endif
122                ", AT_SYMLINK_NOFOLLOW|AT_EMPTY_PATH) = -1 %s (%m)\n",
123                Q_FILENAME, errno2name());
124
125         char str_a[] = "012345678901234567890123456789012";
126         char str_b[] = "_abcdefghijklmnopqrstuvwxyz()[]{}";
127 #define DEFAULT_STRLEN ((unsigned int) sizeof(str_a) - 2)
128         char **const a = tail_alloc(sizeof(*a) * (DEFAULT_STRLEN + 2));
129         char **const b = tail_alloc(sizeof(*b) * (DEFAULT_STRLEN + 2));
130         unsigned int i;
131         for (i = 0; i <= DEFAULT_STRLEN; ++i) {
132                 a[i] = &str_a[i];
133                 b[i] = &str_b[i];
134         }
135         a[i] = b[i] = NULL;
136
137         syscall(__NR_execveat, -100, FILENAME, a, b, 0x1100);
138         printf("execveat(AT_FDCWD, \"%s\", [\"%.*s\"...", Q_FILENAME, DEFAULT_STRLEN, a[0]);
139         for (i = 1; i < DEFAULT_STRLEN; ++i)
140                 printf(", \"%s\"", a[i]);
141 #if VERBOSE
142         printf(", \"%s\"", a[i]);
143 #else
144         printf(", ...");
145 #endif
146 #if VERBOSE
147         printf("], [\"%.*s\"...", DEFAULT_STRLEN, b[0]);
148         for (i = 1; i <= DEFAULT_STRLEN; ++i)
149                 printf(", \"%s\"", b[i]);
150 #else
151         printf("], [/* %u vars */", DEFAULT_STRLEN + 1);
152 #endif
153         printf("], AT_SYMLINK_NOFOLLOW|AT_EMPTY_PATH) = -1 %s (%m)\n",
154                errno2name());
155
156         syscall(__NR_execveat, -100, FILENAME, a + 1, b + 1, 0x1100);
157         printf("execveat(AT_FDCWD, \"%s\", [\"%s\"", Q_FILENAME, a[1]);
158         for (i = 2; i <= DEFAULT_STRLEN; ++i)
159                 printf(", \"%s\"", a[i]);
160 #if VERBOSE
161         printf("], [\"%s\"", b[1]);
162         for (i = 2; i <= DEFAULT_STRLEN; ++i)
163                 printf(", \"%s\"", b[i]);
164 #else
165         printf("], [/* %d vars */", DEFAULT_STRLEN);
166 #endif
167         printf("], AT_SYMLINK_NOFOLLOW|AT_EMPTY_PATH) = -1 %s (%m)\n",
168                errno2name());
169
170         syscall(__NR_execveat, -100, FILENAME, NULL, efault, 0x1100);
171         printf("execveat(AT_FDCWD, \"%s\", NULL, %p"
172                ", AT_SYMLINK_NOFOLLOW|AT_EMPTY_PATH) = -1 %s (%m)\n",
173                Q_FILENAME, efault, errno2name());
174
175         syscall(__NR_execveat, -100, FILENAME, efault, NULL, 0x1100);
176         printf("execveat(AT_FDCWD, \"%s\", %p, NULL"
177                ", AT_SYMLINK_NOFOLLOW|AT_EMPTY_PATH) = -1 %s (%m)\n",
178                Q_FILENAME, efault, errno2name());
179
180         puts("+++ exited with 0 +++");
181         return 0;
182 }
183
184 #else
185
186 SKIP_MAIN_UNDEFINED("__NR_execveat")
187
188 #endif