]> granicus.if.org Git - strace/blob - tests/execveat.c
tests: extend coverage of execve and execveat syscalls
[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 <errno.h>
32 #include <stdio.h>
33 #include <unistd.h>
34 #include <sys/syscall.h>
35
36 #ifdef __NR_execveat
37
38 # define FILENAME "test.execveat\nfilename"
39 # define Q_FILENAME "test.execveat\\nfilename"
40
41 static const char * const argv[] = {
42         FILENAME, "first", "second", (const char *) -1L,
43         (const char *) -2L, (const char *) -3L
44 };
45 static const char * const q_argv[] = {
46         Q_FILENAME, "first", "second"
47 };
48
49 static const char * const envp[] = {
50         "foobar=1", "foo\nbar=2", (const char *) -1L,
51         (const char *) -2L, (const char *) -3L
52 };
53 static const char * const q_envp[] = {
54         "foobar=1", "foo\\nbar=2"
55 };
56
57 int
58 main(void)
59 {
60         const char ** const tail_argv = tail_memdup(argv, sizeof(argv));
61         const char ** const tail_envp = tail_memdup(envp, sizeof(envp));
62
63         syscall(__NR_execveat, -100, FILENAME, tail_argv, tail_envp, 0x1100);
64         printf("execveat(AT_FDCWD, \"%s\""
65                ", [\"%s\", \"%s\", \"%s\", %p, %p, %p, ???]"
66 #ifdef VERBOSE_EXECVEAT
67                ", [\"%s\", \"%s\", %p, %p, %p, ???]"
68 #else
69                ", [/* 5 vars, unterminated */]"
70 #endif
71                ", AT_SYMLINK_NOFOLLOW|AT_EMPTY_PATH) = -1 %s (%m)\n",
72                Q_FILENAME, q_argv[0], q_argv[1], q_argv[2],
73                argv[3], argv[4], argv[5],
74 #ifdef VERBOSE_EXECVEAT
75                q_envp[0], q_envp[1], envp[2], envp[3], envp[4],
76 #endif
77                errno == ENOSYS ? "ENOSYS" : "ENOENT");
78
79         tail_argv[ARRAY_SIZE(q_argv)] = NULL;
80         tail_envp[ARRAY_SIZE(q_envp)] = NULL;
81
82         syscall(__NR_execveat, -100, FILENAME, tail_argv, tail_envp, 0x1100);
83         printf("execveat(AT_FDCWD, \"%s\", [\"%s\", \"%s\", \"%s\"]"
84 #ifdef VERBOSE_EXECVEAT
85                ", [\"%s\", \"%s\"]"
86 #else
87                ", [/* 2 vars */]"
88 #endif
89                ", AT_SYMLINK_NOFOLLOW|AT_EMPTY_PATH) = -1 %s (%m)\n",
90                Q_FILENAME, q_argv[0], q_argv[1], q_argv[2],
91 #ifdef VERBOSE_EXECVEAT
92                q_envp[0], q_envp[1],
93 #endif
94                errno == ENOSYS ? "ENOSYS" : "ENOENT");
95
96         syscall(__NR_execveat, -100, FILENAME, tail_argv + 2, tail_envp + 1, 0x1100);
97         printf("execveat(AT_FDCWD, \"%s\", [\"%s\"]"
98 #ifdef VERBOSE_EXECVEAT
99                ", [\"%s\"]"
100 #else
101                ", [/* 1 var */]"
102 #endif
103                ", AT_SYMLINK_NOFOLLOW|AT_EMPTY_PATH) = -1 %s (%m)\n",
104                Q_FILENAME, q_argv[2],
105 #ifdef VERBOSE_EXECVEAT
106                q_envp[1],
107 #endif
108                errno == ENOSYS ? "ENOSYS" : "ENOENT");
109
110         const char **const empty = tail_alloc(sizeof(*empty));
111         *empty = NULL;
112
113         syscall(__NR_execveat, -100, FILENAME, empty, empty, 0x1100);
114         printf("execveat(AT_FDCWD, \"%s\", []"
115 #ifdef VERBOSE_EXECVEAT
116                ", []"
117 #else
118                ", [/* 0 vars */]"
119 #endif
120                ", AT_SYMLINK_NOFOLLOW|AT_EMPTY_PATH) = -1 %s (%m)\n",
121                Q_FILENAME, errno == ENOSYS ? "ENOSYS" : "ENOENT");
122
123         char str_a[] = "012345678901234567890123456789012";
124         char str_b[] = "_abcdefghijklmnopqrstuvwxyz()[]{}";
125 #define DEFAULT_STRLEN ((unsigned int) sizeof(str_a) - 2)
126         char **const a = tail_alloc(sizeof(*a) * (DEFAULT_STRLEN + 2));
127         char **const b = tail_alloc(sizeof(*b) * (DEFAULT_STRLEN + 2));
128         unsigned int i;
129         for (i = 0; i <= DEFAULT_STRLEN; ++i) {
130                 a[i] = &str_a[i];
131                 b[i] = &str_b[i];
132         }
133         a[i] = b[i] = NULL;
134
135         syscall(__NR_execveat, -100, FILENAME, a, b, 0x1100);
136         printf("execveat(AT_FDCWD, \"%s\", [\"%.*s\"...", Q_FILENAME, DEFAULT_STRLEN, a[0]);
137         for (i = 1; i < DEFAULT_STRLEN; ++i)
138                 printf(", \"%s\"", a[i]);
139 #ifdef VERBOSE_EXECVEAT
140         printf(", \"%s\"", a[i]);
141 #else
142         printf(", ...");
143 #endif
144 #ifdef VERBOSE_EXECVEAT
145         printf("], [\"%.*s\"...", DEFAULT_STRLEN, b[0]);
146         for (i = 1; i <= DEFAULT_STRLEN; ++i)
147                 printf(", \"%s\"", b[i]);
148 #else
149         printf("], [/* %u vars */", DEFAULT_STRLEN + 1);
150 #endif
151         printf("], AT_SYMLINK_NOFOLLOW|AT_EMPTY_PATH) = -1 %s (%m)\n",
152                errno == ENOSYS ? "ENOSYS" : "ENOENT");
153
154         syscall(__NR_execveat, -100, FILENAME, a + 1, b + 1, 0x1100);
155         printf("execveat(AT_FDCWD, \"%s\", [\"%s\"", Q_FILENAME, a[1]);
156         for (i = 2; i <= DEFAULT_STRLEN; ++i)
157                 printf(", \"%s\"", a[i]);
158 #ifdef VERBOSE_EXECVEAT
159         printf("], [\"%s\"", b[1]);
160         for (i = 2; i <= DEFAULT_STRLEN; ++i)
161                 printf(", \"%s\"", b[i]);
162 #else
163         printf("], [/* %d vars */", DEFAULT_STRLEN);
164 #endif
165         printf("], AT_SYMLINK_NOFOLLOW|AT_EMPTY_PATH) = -1 %s (%m)\n",
166                errno == ENOSYS ? "ENOSYS" : "ENOENT");
167
168         const void * const efault = tail_alloc(0);
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, errno == ENOSYS ? "ENOSYS" : "ENOENT");
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, errno == ENOSYS ? "ENOSYS" : "ENOENT");
179
180         puts("+++ exited with 0 +++");
181         return 0;
182 }
183
184 #else
185
186 SKIP_MAIN_UNDEFINED("__NR_execveat")
187
188 #endif