xref: /openbsd/regress/sys/kern/exec_self/exec_self.c (revision 9ea232b5)
1 /*	$OpenBSD: exec_self.c,v 1.3 2024/01/23 10:27:12 anton Exp $	*/
2 /*
3  *	Written by Artur Grabowski <art@openbsd.org> 2002 Public Domain.
4  */
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <unistd.h>
8 #include <err.h>
9 #include <sys/types.h>
10 #include <sys/mman.h>
11 
12 struct {
13 	const char pad1[256*1024];	/* avoid read-ahead. */
14 	const char string[256*1024];	/* at least one page */
15 	const char pad2[256*1024];	/* avoid read-behind. */
16 } const blob __attribute__((section(".openbsd.mutable"))) = {
17 	"padding1",
18 	"the_test",
19 	"padding2"
20 };
21 
22 int
23 main(int argc, char **argv)
24 {
25 	int pgsz = getpagesize();
26 	vaddr_t va, off;
27 
28 	if (argc > 1) {
29 		return (0);
30 	}
31 	va = (vaddr_t)&blob;
32 	off = va & (pgsz - 1);
33 
34 	/* Make sure that nothing in the "blob" is cached. */
35 	if (madvise((void *)(va - off), sizeof(blob) + (off > 0 ? pgsz : 0),
36 	    MADV_FREE))
37 		err(1, "madvise");
38 
39 	if (execl(argv[0], argv[0], &blob.string, (char *)NULL))
40 		err(1, "execl");
41 
42 	/* NOTREACHED */
43 	return (1);
44 }
45