Home | History | Annotate | Line # | Download | only in kernel
      1  1.7  riastrad /*	$NetBSD: t_execregs.c,v 1.7 2025/04/27 14:30:03 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.7  riastrad __RCSID("$NetBSD: t_execregs.c,v 1.7 2025/04/27 14:30:03 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.2  riastrad #include "isqemu.h"
     46  1.1  riastrad #include "h_macros.h"
     47  1.1  riastrad 
     48  1.1  riastrad static void
     49  1.1  riastrad readregs(int rfd, register_t regs[static NEXECREGS])
     50  1.1  riastrad {
     51  1.1  riastrad 	uint8_t *p;
     52  1.1  riastrad 	size_t n;
     53  1.1  riastrad 	ssize_t nread;
     54  1.1  riastrad 
     55  1.1  riastrad 	p = (void *)regs;
     56  1.1  riastrad 	n = NEXECREGS*sizeof(regs[0]);
     57  1.1  riastrad 	while (n) {
     58  1.1  riastrad 		RL(nread = read(rfd, p, n));
     59  1.1  riastrad 		ATF_CHECK_MSG((size_t)nread <= n,
     60  1.1  riastrad 		    "overlong read: %zu > %zu", (size_t)nread, n);
     61  1.1  riastrad 		if (nread == 0)
     62  1.1  riastrad 			break;
     63  1.1  riastrad 		p += (size_t)nread;
     64  1.1  riastrad 		n -= (size_t)nread;
     65  1.1  riastrad 	}
     66  1.3  riastrad 	ATF_CHECK_EQ_MSG(n, 0,
     67  1.3  riastrad 	    "truncated read, missing %zu of %zu bytes",
     68  1.3  riastrad 	    n, NEXECREGS*sizeof(regs[0]));
     69  1.1  riastrad }
     70  1.1  riastrad 
     71  1.1  riastrad static void
     72  1.1  riastrad checkregs(const register_t regs[static NEXECREGS])
     73  1.1  riastrad {
     74  1.1  riastrad 	unsigned i;
     75  1.1  riastrad 
     76  1.2  riastrad #ifdef __hppa__
     77  1.2  riastrad 	if (isQEMU()) {
     78  1.2  riastrad 		atf_tc_expect_fail("PR port-hppa/59114: hppa:"
     79  1.2  riastrad 		    " eager fpu switching for qemu and/or spectre mitigation");
     80  1.2  riastrad 	}
     81  1.2  riastrad #endif
     82  1.2  riastrad 
     83  1.1  riastrad 	for (i = 0; i < NEXECREGS; i++) {
     84  1.1  riastrad 		if (regs[i] != 0) {
     85  1.1  riastrad 			for (i = 0; i < NEXECREGS; i++) {
     86  1.1  riastrad 				fprintf(stderr, "[%s] %"PRIxREGISTER"\n",
     87  1.1  riastrad 				    regname[i], regs[i]);
     88  1.1  riastrad 			}
     89  1.1  riastrad 			fprintf(stderr, "\n");
     90  1.1  riastrad 			atf_tc_fail("registers not zeroed");
     91  1.1  riastrad 		}
     92  1.1  riastrad 	}
     93  1.1  riastrad }
     94  1.1  riastrad 
     95  1.1  riastrad static void
     96  1.1  riastrad testregs(int child, const int pipefd[static 2],
     97  1.1  riastrad     register_t regs[static NEXECREGS])
     98  1.1  riastrad {
     99  1.1  riastrad 	int status;
    100  1.1  riastrad 
    101  1.1  riastrad 	RL(close(pipefd[1]));
    102  1.1  riastrad 
    103  1.1  riastrad 	readregs(pipefd[0], regs);
    104  1.1  riastrad 
    105  1.1  riastrad 	RL(waitpid(child, &status, 0));
    106  1.7  riastrad 	if (WIFSIGNALED(status)) {
    107  1.7  riastrad 		atf_tc_fail_nonfatal("child terminated on signal %d (%s)",
    108  1.7  riastrad 		    WTERMSIG(status), strsignal(WTERMSIG(status)));
    109  1.7  riastrad 	} else if (!WIFEXITED(status)) {
    110  1.7  riastrad 		atf_tc_fail_nonfatal("child terminated mysteriously,"
    111  1.7  riastrad 		    " status=0x%x",
    112  1.7  riastrad 		    status);
    113  1.7  riastrad 	} else {
    114  1.7  riastrad 		ATF_CHECK_MSG(WEXITSTATUS(status) == 0,
    115  1.7  riastrad 		    "child exited with code %d", WEXITSTATUS(status));
    116  1.7  riastrad 	}
    117  1.1  riastrad 
    118  1.1  riastrad 	checkregs(regs);
    119  1.1  riastrad }
    120  1.1  riastrad 
    121  1.1  riastrad #endif
    122  1.1  riastrad 
    123  1.1  riastrad ATF_TC(execregszero);
    124  1.1  riastrad ATF_TC_HEAD(execregszero, tc)
    125  1.1  riastrad {
    126  1.1  riastrad 	atf_tc_set_md_var(tc, "descr",
    127  1.1  riastrad 	    "Test execve(2) zeroes registers");
    128  1.1  riastrad }
    129  1.1  riastrad ATF_TC_BODY(execregszero, tc)
    130  1.1  riastrad {
    131  1.1  riastrad #ifdef HAVE_EXECREGS_TEST
    132  1.1  riastrad 	char h_execregs[PATH_MAX];
    133  1.1  riastrad 	int pipefd[2];
    134  1.1  riastrad 	register_t regs[NEXECREGS];
    135  1.1  riastrad 	pid_t child;
    136  1.1  riastrad 
    137  1.1  riastrad 	RL(snprintf(h_execregs, sizeof(h_execregs), "%s/h_execregs",
    138  1.1  riastrad 		atf_tc_get_config_var(tc, "srcdir")));
    139  1.1  riastrad 
    140  1.1  riastrad 	RL(pipe(pipefd));
    141  1.1  riastrad 	memset(regs, 0x5a, sizeof(regs));
    142  1.1  riastrad 
    143  1.1  riastrad 	RL(child = fork());
    144  1.1  riastrad 	if (child == 0) {
    145  1.1  riastrad 		if (dup2(pipefd[1], STDOUT_FILENO) == -1)
    146  1.1  riastrad 			err(1, "dup2");
    147  1.1  riastrad 		if (closefrom(STDERR_FILENO + 1) == -1)
    148  1.1  riastrad 			err(1, "closefrom");
    149  1.1  riastrad 		if (execregschild(h_execregs) == -1)
    150  1.1  riastrad 			err(1, "execve");
    151  1.1  riastrad 		_exit(2);
    152  1.1  riastrad 	}
    153  1.1  riastrad 
    154  1.1  riastrad 	testregs(child, pipefd, regs);
    155  1.1  riastrad #else
    156  1.1  riastrad 	atf_tc_skip("missing test for PR kern/59084:"
    157  1.1  riastrad 	    " exec/spawn leaks register content");
    158  1.1  riastrad #endif
    159  1.1  riastrad }
    160  1.1  riastrad 
    161  1.1  riastrad ATF_TC(spawnregszero);
    162  1.1  riastrad ATF_TC_HEAD(spawnregszero, tc)
    163  1.1  riastrad {
    164  1.1  riastrad 	atf_tc_set_md_var(tc, "descr",
    165  1.1  riastrad 	    "Test posix_spawn(2) zeroes registers");
    166  1.1  riastrad }
    167  1.1  riastrad ATF_TC_BODY(spawnregszero, tc)
    168  1.1  riastrad {
    169  1.1  riastrad #ifdef HAVE_EXECREGS_TEST
    170  1.1  riastrad 	char h_execregs[PATH_MAX];
    171  1.1  riastrad 	int pipefd[2];
    172  1.1  riastrad 	register_t regs[NEXECREGS];
    173  1.1  riastrad 	pid_t child;
    174  1.1  riastrad 
    175  1.1  riastrad 	RL(snprintf(h_execregs, sizeof(h_execregs), "%s/h_execregs",
    176  1.1  riastrad 		atf_tc_get_config_var(tc, "srcdir")));
    177  1.1  riastrad 
    178  1.1  riastrad 	RL(pipe(pipefd));
    179  1.1  riastrad 	memset(regs, 0x5a, sizeof(regs));
    180  1.1  riastrad 
    181  1.1  riastrad 	RL(child = spawnregschild(h_execregs, pipefd[1]));
    182  1.1  riastrad 
    183  1.1  riastrad 	testregs(child, pipefd, regs);
    184  1.1  riastrad #else
    185  1.1  riastrad 	atf_tc_skip("missing test for PR kern/59084:"
    186  1.1  riastrad 	    " exec/spawn leaks register content");
    187  1.1  riastrad #endif
    188  1.1  riastrad }
    189  1.1  riastrad 
    190  1.1  riastrad ATF_TP_ADD_TCS(tp)
    191  1.1  riastrad {
    192  1.1  riastrad 
    193  1.1  riastrad 	ATF_TP_ADD_TC(tp, execregszero);
    194  1.1  riastrad 	ATF_TP_ADD_TC(tp, spawnregszero);
    195  1.1  riastrad 
    196  1.1  riastrad 	return atf_no_error();
    197  1.1  riastrad }
    198