File: process_madvise.c

package info (click to toggle)
strace 6.13%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 71,488 kB
  • sloc: ansic: 176,497; sh: 9,675; makefile: 4,133; cpp: 885; awk: 353; perl: 267; exp: 62; sed: 9
file content (86 lines) | stat: -rw-r--r-- 2,431 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*
 * Check decoding of process_madvise syscall.
 *
 * Copyright (c) 2020-2024 Dmitry V. Levin <ldv@strace.io>
 * All rights reserved.
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

#include "tests.h"
#include "scno.h"
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/uio.h>

#ifndef FD0_PATH
# define FD0_PATH ""
#endif

static const char *errstr;

static long
k_process_madvise(const unsigned int pidfd,
		  const struct iovec *const addr,
		  const kernel_ulong_t len,
		  const unsigned int cmd,
		  const unsigned int flags)
{
	const kernel_ulong_t fill = (kernel_ulong_t) 0xdefaced00000000ULL;
	const kernel_ulong_t bad = (kernel_ulong_t) 0xbadc0dedbadc0dedULL;
	const kernel_ulong_t arg1 = fill | pidfd;
	const kernel_ulong_t arg2 = (uintptr_t) addr;
	const kernel_ulong_t arg3 = len;
	const kernel_ulong_t arg4 = fill | cmd;
	const kernel_ulong_t arg5 = fill | flags;
	const long rc = syscall(__NR_process_madvise,
				arg1, arg2, arg3, arg4, arg5, bad);
	errstr = sprintrc(rc);
	return rc;
}

int
main(void)
{
#ifdef PRINT_PATHS
	skip_if_unavailable("/proc/self/fd/");
#endif

	(void) close(0);
	if (open("/dev/full", O_WRONLY))
		perror_msg_and_fail("open");

	TAIL_ALLOC_OBJECT_CONST_ARR(struct iovec, iov, 2);
	fill_memory(iov, 2 * sizeof(*iov));

	k_process_madvise(0, iov, 2, 0, -1U);
	printf("process_madvise(0" FD0_PATH
	       ", [{iov_base=%p, iov_len=%lu}, {iov_base=%p, iov_len=%lu}]"
	       ", 2, MADV_NORMAL, %#x) = %s\n",
	       iov[0].iov_base, (unsigned long) iov[0].iov_len,
	       iov[1].iov_base, (unsigned long) iov[1].iov_len,
	       -1U, errstr);

	const kernel_ulong_t bogus_len = (kernel_ulong_t) 0xdeadbeefdeadbeef;
	k_process_madvise(-1, 0, bogus_len, 20, 0);
	printf("process_madvise(-1, NULL, %" PRI_klu ", MADV_COLD, 0) = %s\n",
	       bogus_len, errstr);

	const unsigned int bogus_flags = 0xcafef00d;
	k_process_madvise(0, iov + 2, 0, 21, bogus_flags);
	printf("process_madvise(0" FD0_PATH ", [], 0, MADV_PAGEOUT, %#x)"
	       " = %s\n", bogus_flags, errstr);

	const unsigned int bogus_cmd = 0xdeadc0de;
	k_process_madvise(-1, iov + 1, 2, bogus_cmd, 0);
	printf("process_madvise(-1, [{iov_base=%p, iov_len=%lu}, ... /* %p */]"
	       ", 2, %#x /* MADV_??? */, 0) = %s\n",
	       iov[1].iov_base, (unsigned long) iov[1].iov_len,
	       iov + 2, bogus_cmd, errstr);

	puts("+++ exited with 0 +++");
	return 0;
}