1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker * Copyright (c) Zilogic Systems Pvt. Ltd., 2018
4*49cdfc7eSAndroid Build Coastguard Worker * Email : [email protected]
5*49cdfc7eSAndroid Build Coastguard Worker */
6*49cdfc7eSAndroid Build Coastguard Worker
7*49cdfc7eSAndroid Build Coastguard Worker /*
8*49cdfc7eSAndroid Build Coastguard Worker * test cases for madvise(2) system call, advise value as "MADV_WIPEONFORK".
9*49cdfc7eSAndroid Build Coastguard Worker *
10*49cdfc7eSAndroid Build Coastguard Worker * DESCRIPTION
11*49cdfc7eSAndroid Build Coastguard Worker * Present the child process with zero-filled memory in this
12*49cdfc7eSAndroid Build Coastguard Worker * range after a fork(2).
13*49cdfc7eSAndroid Build Coastguard Worker * The MADV_WIPEONFORK operation can be applied only to
14*49cdfc7eSAndroid Build Coastguard Worker * private anonymous pages.
15*49cdfc7eSAndroid Build Coastguard Worker * Within the child created by fork(2), the MADV_WIPEONFORK
16*49cdfc7eSAndroid Build Coastguard Worker * setting remains in place on the specified map_address range.
17*49cdfc7eSAndroid Build Coastguard Worker * The MADV_KEEPONFORK operation undo the effect of MADV_WIPEONFORK.
18*49cdfc7eSAndroid Build Coastguard Worker *
19*49cdfc7eSAndroid Build Coastguard Worker * Test-Case 1 : madvise with "MADV_WIPEONFORK"
20*49cdfc7eSAndroid Build Coastguard Worker * flow : Map memory area as private anonymous page.
21*49cdfc7eSAndroid Build Coastguard Worker * Mark memory area as wipe-on-fork.
22*49cdfc7eSAndroid Build Coastguard Worker * On fork, child process memory should be zeroed.
23*49cdfc7eSAndroid Build Coastguard Worker *
24*49cdfc7eSAndroid Build Coastguard Worker * Test-Case 2 : madvise with "MADV_WIPEONFORK" and "ZERO" length
25*49cdfc7eSAndroid Build Coastguard Worker * flow : Map memory area as private anonymous page.
26*49cdfc7eSAndroid Build Coastguard Worker * Mark memory area as wipe-on-fork, with length zero.
27*49cdfc7eSAndroid Build Coastguard Worker * On fork, child process memory should be accessible.
28*49cdfc7eSAndroid Build Coastguard Worker *
29*49cdfc7eSAndroid Build Coastguard Worker * Test-Case 3 : "MADV_WIPEONFORK" on Grand child
30*49cdfc7eSAndroid Build Coastguard Worker * flow : Map memory area as private anonymous.
31*49cdfc7eSAndroid Build Coastguard Worker * Mark memory areas as wipe-on-fork.
32*49cdfc7eSAndroid Build Coastguard Worker * On fork, child process memory should be zeroed.
33*49cdfc7eSAndroid Build Coastguard Worker * In child, fork to create grand-child,
34*49cdfc7eSAndroid Build Coastguard Worker * memory should be zeroed.
35*49cdfc7eSAndroid Build Coastguard Worker *
36*49cdfc7eSAndroid Build Coastguard Worker * Test-Case 4 : Undo "MADV_WIPEONFORK" by "MADV_KEEPONFORK"
37*49cdfc7eSAndroid Build Coastguard Worker * flow : Map memory area as private anonymous page.
38*49cdfc7eSAndroid Build Coastguard Worker * Mark memory area as wipe-on-fork.
39*49cdfc7eSAndroid Build Coastguard Worker * Mark memory area as keep-on-fork.
40*49cdfc7eSAndroid Build Coastguard Worker * On fork, child process memory should be retained.
41*49cdfc7eSAndroid Build Coastguard Worker **/
42*49cdfc7eSAndroid Build Coastguard Worker
43*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
44*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
45*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
46*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
47*49cdfc7eSAndroid Build Coastguard Worker
48*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/mmap.h"
49*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
50*49cdfc7eSAndroid Build Coastguard Worker #include "tst_safe_macros.h"
51*49cdfc7eSAndroid Build Coastguard Worker
52*49cdfc7eSAndroid Build Coastguard Worker #define MAP_SIZE (16 * 1024)
53*49cdfc7eSAndroid Build Coastguard Worker
54*49cdfc7eSAndroid Build Coastguard Worker static char pattern[MAP_SIZE];
55*49cdfc7eSAndroid Build Coastguard Worker static char zero[MAP_SIZE];
56*49cdfc7eSAndroid Build Coastguard Worker
57*49cdfc7eSAndroid Build Coastguard Worker static const struct test_case {
58*49cdfc7eSAndroid Build Coastguard Worker int size;
59*49cdfc7eSAndroid Build Coastguard Worker int advise1;
60*49cdfc7eSAndroid Build Coastguard Worker int advise2;
61*49cdfc7eSAndroid Build Coastguard Worker char *exp;
62*49cdfc7eSAndroid Build Coastguard Worker int grand_child;
63*49cdfc7eSAndroid Build Coastguard Worker const char *desc;
64*49cdfc7eSAndroid Build Coastguard Worker } tcases[] = {
65*49cdfc7eSAndroid Build Coastguard Worker {MAP_SIZE, MADV_NORMAL, MADV_WIPEONFORK, zero, 0,
66*49cdfc7eSAndroid Build Coastguard Worker "MADV_WIPEONFORK zeroes memory in child"},
67*49cdfc7eSAndroid Build Coastguard Worker {0, MADV_NORMAL, MADV_WIPEONFORK, pattern, 0,
68*49cdfc7eSAndroid Build Coastguard Worker "MADV_WIPEONFORK with zero length does nothing"},
69*49cdfc7eSAndroid Build Coastguard Worker {MAP_SIZE, MADV_NORMAL, MADV_WIPEONFORK, zero, 1,
70*49cdfc7eSAndroid Build Coastguard Worker "MADV_WIPEONFORK zeroes memory in grand-child"},
71*49cdfc7eSAndroid Build Coastguard Worker {MAP_SIZE, MADV_WIPEONFORK, MADV_KEEPONFORK, pattern, 0,
72*49cdfc7eSAndroid Build Coastguard Worker "MADV_KEEPONFORK will undo MADV_WIPEONFORK"},
73*49cdfc7eSAndroid Build Coastguard Worker };
74*49cdfc7eSAndroid Build Coastguard Worker
cmp_area(char * addr,const struct test_case * tc)75*49cdfc7eSAndroid Build Coastguard Worker static void cmp_area(char *addr, const struct test_case *tc)
76*49cdfc7eSAndroid Build Coastguard Worker {
77*49cdfc7eSAndroid Build Coastguard Worker int i;
78*49cdfc7eSAndroid Build Coastguard Worker
79*49cdfc7eSAndroid Build Coastguard Worker for (i = 0; i < tc->size; i++) {
80*49cdfc7eSAndroid Build Coastguard Worker if (addr[i] != tc->exp[i]) {
81*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL, "In PID %d, addr[%d] = 0x%02x, "
82*49cdfc7eSAndroid Build Coastguard Worker "expected[%d] = 0x%02x", getpid(),
83*49cdfc7eSAndroid Build Coastguard Worker i, addr[i], i, tc->exp[i]);
84*49cdfc7eSAndroid Build Coastguard Worker break;
85*49cdfc7eSAndroid Build Coastguard Worker }
86*49cdfc7eSAndroid Build Coastguard Worker }
87*49cdfc7eSAndroid Build Coastguard Worker
88*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS, "In PID %d, Matched expected pattern", getpid());
89*49cdfc7eSAndroid Build Coastguard Worker }
90*49cdfc7eSAndroid Build Coastguard Worker
set_advice(char * addr,int size,int advise)91*49cdfc7eSAndroid Build Coastguard Worker static int set_advice(char *addr, int size, int advise)
92*49cdfc7eSAndroid Build Coastguard Worker {
93*49cdfc7eSAndroid Build Coastguard Worker TEST(madvise(addr, size, advise));
94*49cdfc7eSAndroid Build Coastguard Worker
95*49cdfc7eSAndroid Build Coastguard Worker if (TST_RET == -1) {
96*49cdfc7eSAndroid Build Coastguard Worker if (TST_ERR == EINVAL) {
97*49cdfc7eSAndroid Build Coastguard Worker tst_res(TCONF, "madvise(%p, %d, 0x%x) is not supported",
98*49cdfc7eSAndroid Build Coastguard Worker addr, size, advise);
99*49cdfc7eSAndroid Build Coastguard Worker } else {
100*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL | TTERRNO, "madvise(%p, %d, 0x%x)",
101*49cdfc7eSAndroid Build Coastguard Worker addr, size, advise);
102*49cdfc7eSAndroid Build Coastguard Worker }
103*49cdfc7eSAndroid Build Coastguard Worker
104*49cdfc7eSAndroid Build Coastguard Worker return 1;
105*49cdfc7eSAndroid Build Coastguard Worker }
106*49cdfc7eSAndroid Build Coastguard Worker
107*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS, "madvise(%p, %d, 0x%x)", addr, size, advise);
108*49cdfc7eSAndroid Build Coastguard Worker return 0;
109*49cdfc7eSAndroid Build Coastguard Worker }
110*49cdfc7eSAndroid Build Coastguard Worker
mem_map(void)111*49cdfc7eSAndroid Build Coastguard Worker static char *mem_map(void)
112*49cdfc7eSAndroid Build Coastguard Worker {
113*49cdfc7eSAndroid Build Coastguard Worker char *ptr;
114*49cdfc7eSAndroid Build Coastguard Worker
115*49cdfc7eSAndroid Build Coastguard Worker ptr = SAFE_MMAP(NULL, MAP_SIZE,
116*49cdfc7eSAndroid Build Coastguard Worker PROT_READ | PROT_WRITE,
117*49cdfc7eSAndroid Build Coastguard Worker MAP_PRIVATE | MAP_ANONYMOUS,
118*49cdfc7eSAndroid Build Coastguard Worker -1, 0);
119*49cdfc7eSAndroid Build Coastguard Worker
120*49cdfc7eSAndroid Build Coastguard Worker memcpy(ptr, pattern, MAP_SIZE);
121*49cdfc7eSAndroid Build Coastguard Worker
122*49cdfc7eSAndroid Build Coastguard Worker return ptr;
123*49cdfc7eSAndroid Build Coastguard Worker }
124*49cdfc7eSAndroid Build Coastguard Worker
test_madvise(unsigned int test_nr)125*49cdfc7eSAndroid Build Coastguard Worker static void test_madvise(unsigned int test_nr)
126*49cdfc7eSAndroid Build Coastguard Worker {
127*49cdfc7eSAndroid Build Coastguard Worker const struct test_case *tc = &tcases[test_nr];
128*49cdfc7eSAndroid Build Coastguard Worker char *addr;
129*49cdfc7eSAndroid Build Coastguard Worker pid_t pid;
130*49cdfc7eSAndroid Build Coastguard Worker
131*49cdfc7eSAndroid Build Coastguard Worker addr = mem_map();
132*49cdfc7eSAndroid Build Coastguard Worker
133*49cdfc7eSAndroid Build Coastguard Worker tst_res(TINFO, "%s", tc->desc);
134*49cdfc7eSAndroid Build Coastguard Worker if (set_advice(addr, tc->size, tc->advise1))
135*49cdfc7eSAndroid Build Coastguard Worker goto un_map;
136*49cdfc7eSAndroid Build Coastguard Worker
137*49cdfc7eSAndroid Build Coastguard Worker if (!set_advice(addr, tc->size, tc->advise2)) {
138*49cdfc7eSAndroid Build Coastguard Worker pid = SAFE_FORK();
139*49cdfc7eSAndroid Build Coastguard Worker
140*49cdfc7eSAndroid Build Coastguard Worker if (!pid) {
141*49cdfc7eSAndroid Build Coastguard Worker if (tc->grand_child) {
142*49cdfc7eSAndroid Build Coastguard Worker pid = SAFE_FORK();
143*49cdfc7eSAndroid Build Coastguard Worker
144*49cdfc7eSAndroid Build Coastguard Worker if (!pid) {
145*49cdfc7eSAndroid Build Coastguard Worker cmp_area(addr, tc);
146*49cdfc7eSAndroid Build Coastguard Worker exit(0);
147*49cdfc7eSAndroid Build Coastguard Worker }
148*49cdfc7eSAndroid Build Coastguard Worker } else {
149*49cdfc7eSAndroid Build Coastguard Worker cmp_area(addr, tc);
150*49cdfc7eSAndroid Build Coastguard Worker exit(0);
151*49cdfc7eSAndroid Build Coastguard Worker }
152*49cdfc7eSAndroid Build Coastguard Worker }
153*49cdfc7eSAndroid Build Coastguard Worker tst_reap_children();
154*49cdfc7eSAndroid Build Coastguard Worker }
155*49cdfc7eSAndroid Build Coastguard Worker
156*49cdfc7eSAndroid Build Coastguard Worker un_map:
157*49cdfc7eSAndroid Build Coastguard Worker SAFE_MUNMAP(addr, MAP_SIZE);
158*49cdfc7eSAndroid Build Coastguard Worker }
159*49cdfc7eSAndroid Build Coastguard Worker
setup(void)160*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
161*49cdfc7eSAndroid Build Coastguard Worker {
162*49cdfc7eSAndroid Build Coastguard Worker unsigned int i;
163*49cdfc7eSAndroid Build Coastguard Worker
164*49cdfc7eSAndroid Build Coastguard Worker for (i = 0; i < MAP_SIZE; i++)
165*49cdfc7eSAndroid Build Coastguard Worker pattern[i] = i % 0x03;
166*49cdfc7eSAndroid Build Coastguard Worker }
167*49cdfc7eSAndroid Build Coastguard Worker
168*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
169*49cdfc7eSAndroid Build Coastguard Worker .tcnt = ARRAY_SIZE(tcases),
170*49cdfc7eSAndroid Build Coastguard Worker .forks_child = 1,
171*49cdfc7eSAndroid Build Coastguard Worker .test = test_madvise,
172*49cdfc7eSAndroid Build Coastguard Worker .setup = setup,
173*49cdfc7eSAndroid Build Coastguard Worker };
174