t_execregs.c revision 1.1 1 1.1 riastrad /* $NetBSD: t_execregs.c,v 1.1 2025/02/27 00:55:31 riastradh Exp $ */
2 1.1 riastrad
3 1.1 riastrad /*-
4 1.1 riastrad * Copyright (c) 2025 The NetBSD Foundation, Inc.
5 1.1 riastrad * All rights reserved.
6 1.1 riastrad *
7 1.1 riastrad * Redistribution and use in source and binary forms, with or without
8 1.1 riastrad * modification, are permitted provided that the following conditions
9 1.1 riastrad * are met:
10 1.1 riastrad * 1. Redistributions of source code must retain the above copyright
11 1.1 riastrad * notice, this list of conditions and the following disclaimer.
12 1.1 riastrad * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 riastrad * notice, this list of conditions and the following disclaimer in the
14 1.1 riastrad * documentation and/or other materials provided with the distribution.
15 1.1 riastrad *
16 1.1 riastrad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 1.1 riastrad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.1 riastrad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.1 riastrad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 1.1 riastrad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.1 riastrad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.1 riastrad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.1 riastrad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.1 riastrad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.1 riastrad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.1 riastrad * POSSIBILITY OF SUCH DAMAGE.
27 1.1 riastrad */
28 1.1 riastrad
29 1.1 riastrad #include <sys/cdefs.h>
30 1.1 riastrad __RCSID("$NetBSD: t_execregs.c,v 1.1 2025/02/27 00:55:31 riastradh Exp $");
31 1.1 riastrad
32 1.1 riastrad #include <sys/wait.h>
33 1.1 riastrad
34 1.1 riastrad #include <atf-c.h>
35 1.1 riastrad #include <err.h>
36 1.1 riastrad #include <inttypes.h>
37 1.1 riastrad #include <limits.h>
38 1.1 riastrad #include <stdio.h>
39 1.1 riastrad #include <string.h>
40 1.1 riastrad #include <unistd.h>
41 1.1 riastrad
42 1.1 riastrad #ifdef HAVE_EXECREGS_TEST
43 1.1 riastrad
44 1.1 riastrad #include "execregs.h"
45 1.1 riastrad #include "h_macros.h"
46 1.1 riastrad
47 1.1 riastrad static void
48 1.1 riastrad readregs(int rfd, register_t regs[static NEXECREGS])
49 1.1 riastrad {
50 1.1 riastrad uint8_t *p;
51 1.1 riastrad size_t n;
52 1.1 riastrad ssize_t nread;
53 1.1 riastrad
54 1.1 riastrad p = (void *)regs;
55 1.1 riastrad n = NEXECREGS*sizeof(regs[0]);
56 1.1 riastrad while (n) {
57 1.1 riastrad RL(nread = read(rfd, p, n));
58 1.1 riastrad ATF_CHECK_MSG((size_t)nread <= n,
59 1.1 riastrad "overlong read: %zu > %zu", (size_t)nread, n);
60 1.1 riastrad if (nread == 0)
61 1.1 riastrad break;
62 1.1 riastrad p += (size_t)nread;
63 1.1 riastrad n -= (size_t)nread;
64 1.1 riastrad }
65 1.1 riastrad ATF_REQUIRE_EQ_MSG(n, 0,
66 1.1 riastrad "truncated read, missing %zu bytes", n);
67 1.1 riastrad }
68 1.1 riastrad
69 1.1 riastrad static void
70 1.1 riastrad checkregs(const register_t regs[static NEXECREGS])
71 1.1 riastrad {
72 1.1 riastrad unsigned i;
73 1.1 riastrad
74 1.1 riastrad #if defined(__hppa__) || \
75 1.1 riastrad defined(__ia64__) || \
76 1.1 riastrad defined(__vax__) || \
77 1.1 riastrad defined(__x86_64__)
78 1.1 riastrad atf_tc_expect_fail("PR kern/59084: exec/spawn leaks register content");
79 1.1 riastrad #endif
80 1.1 riastrad
81 1.1 riastrad for (i = 0; i < NEXECREGS; i++) {
82 1.1 riastrad if (regs[i] != 0) {
83 1.1 riastrad for (i = 0; i < NEXECREGS; i++) {
84 1.1 riastrad fprintf(stderr, "[%s] %"PRIxREGISTER"\n",
85 1.1 riastrad regname[i], regs[i]);
86 1.1 riastrad }
87 1.1 riastrad fprintf(stderr, "\n");
88 1.1 riastrad atf_tc_fail("registers not zeroed");
89 1.1 riastrad }
90 1.1 riastrad }
91 1.1 riastrad }
92 1.1 riastrad
93 1.1 riastrad static void
94 1.1 riastrad testregs(int child, const int pipefd[static 2],
95 1.1 riastrad register_t regs[static NEXECREGS])
96 1.1 riastrad {
97 1.1 riastrad int status;
98 1.1 riastrad
99 1.1 riastrad RL(close(pipefd[1]));
100 1.1 riastrad
101 1.1 riastrad readregs(pipefd[0], regs);
102 1.1 riastrad
103 1.1 riastrad RL(waitpid(child, &status, 0));
104 1.1 riastrad ATF_CHECK_EQ_MSG(status, 0, "status=0x%x", status);
105 1.1 riastrad
106 1.1 riastrad checkregs(regs);
107 1.1 riastrad }
108 1.1 riastrad
109 1.1 riastrad #endif
110 1.1 riastrad
111 1.1 riastrad ATF_TC(execregszero);
112 1.1 riastrad ATF_TC_HEAD(execregszero, tc)
113 1.1 riastrad {
114 1.1 riastrad atf_tc_set_md_var(tc, "descr",
115 1.1 riastrad "Test execve(2) zeroes registers");
116 1.1 riastrad }
117 1.1 riastrad ATF_TC_BODY(execregszero, tc)
118 1.1 riastrad {
119 1.1 riastrad #ifdef HAVE_EXECREGS_TEST
120 1.1 riastrad char h_execregs[PATH_MAX];
121 1.1 riastrad int pipefd[2];
122 1.1 riastrad register_t regs[NEXECREGS];
123 1.1 riastrad pid_t child;
124 1.1 riastrad
125 1.1 riastrad RL(snprintf(h_execregs, sizeof(h_execregs), "%s/h_execregs",
126 1.1 riastrad atf_tc_get_config_var(tc, "srcdir")));
127 1.1 riastrad
128 1.1 riastrad RL(pipe(pipefd));
129 1.1 riastrad memset(regs, 0x5a, sizeof(regs));
130 1.1 riastrad
131 1.1 riastrad RL(child = fork());
132 1.1 riastrad if (child == 0) {
133 1.1 riastrad if (dup2(pipefd[1], STDOUT_FILENO) == -1)
134 1.1 riastrad err(1, "dup2");
135 1.1 riastrad if (closefrom(STDERR_FILENO + 1) == -1)
136 1.1 riastrad err(1, "closefrom");
137 1.1 riastrad if (execregschild(h_execregs) == -1)
138 1.1 riastrad err(1, "execve");
139 1.1 riastrad _exit(2);
140 1.1 riastrad }
141 1.1 riastrad
142 1.1 riastrad testregs(child, pipefd, regs);
143 1.1 riastrad #else
144 1.1 riastrad atf_tc_skip("missing test for PR kern/59084:"
145 1.1 riastrad " exec/spawn leaks register content");
146 1.1 riastrad #endif
147 1.1 riastrad }
148 1.1 riastrad
149 1.1 riastrad ATF_TC(spawnregszero);
150 1.1 riastrad ATF_TC_HEAD(spawnregszero, tc)
151 1.1 riastrad {
152 1.1 riastrad atf_tc_set_md_var(tc, "descr",
153 1.1 riastrad "Test posix_spawn(2) zeroes registers");
154 1.1 riastrad }
155 1.1 riastrad ATF_TC_BODY(spawnregszero, tc)
156 1.1 riastrad {
157 1.1 riastrad #ifdef HAVE_EXECREGS_TEST
158 1.1 riastrad char h_execregs[PATH_MAX];
159 1.1 riastrad int pipefd[2];
160 1.1 riastrad register_t regs[NEXECREGS];
161 1.1 riastrad pid_t child;
162 1.1 riastrad
163 1.1 riastrad RL(snprintf(h_execregs, sizeof(h_execregs), "%s/h_execregs",
164 1.1 riastrad atf_tc_get_config_var(tc, "srcdir")));
165 1.1 riastrad
166 1.1 riastrad RL(pipe(pipefd));
167 1.1 riastrad memset(regs, 0x5a, sizeof(regs));
168 1.1 riastrad
169 1.1 riastrad RL(child = spawnregschild(h_execregs, pipefd[1]));
170 1.1 riastrad
171 1.1 riastrad testregs(child, pipefd, regs);
172 1.1 riastrad #else
173 1.1 riastrad atf_tc_skip("missing test for PR kern/59084:"
174 1.1 riastrad " exec/spawn leaks register content");
175 1.1 riastrad #endif
176 1.1 riastrad }
177 1.1 riastrad
178 1.1 riastrad ATF_TP_ADD_TCS(tp)
179 1.1 riastrad {
180 1.1 riastrad
181 1.1 riastrad ATF_TP_ADD_TC(tp, execregszero);
182 1.1 riastrad ATF_TP_ADD_TC(tp, spawnregszero);
183 1.1 riastrad
184 1.1 riastrad return atf_no_error();
185 1.1 riastrad }
186