]> granicus.if.org Git - strace/blob - tests/madvise.c
b9620fd5c6aea3abf2790a0474fd7c5f3bf2af57
[strace] / tests / madvise.c
1 /*
2  * Copyright (c) 2017 The strace developers.
3  * All rights reserved.
4  *
5  * SPDX-License-Identifier: GPL-2.0-or-later
6  */
7
8 #include "tests.h"
9 #include <stdint.h>
10 #include <stdio.h>
11 #include <sys/mman.h>
12 #include <unistd.h>
13 #include <asm/unistd.h>
14
15 static const char *errstr;
16
17 static long
18 k_madvise(const kernel_ulong_t addr,
19           const kernel_ulong_t length,
20           const kernel_ulong_t advice)
21 {
22         long rc = syscall(__NR_madvise, addr, length, advice);
23         errstr = sprintrc(rc);
24         return rc;
25 }
26
27 int
28 main(void)
29 {
30         const unsigned long length = get_page_size();
31         void *const addr = tail_alloc(length);
32         long rc;
33
34         rc = madvise(addr, length, MADV_NORMAL);
35         printf("madvise(%p, %lu, MADV_NORMAL) = %s\n",
36                addr, length, sprintrc(rc));
37
38         static const kernel_ulong_t advice =
39                 (kernel_ulong_t) 0xfacefeed00000000ULL | MADV_RANDOM;
40         rc = k_madvise((uintptr_t) addr, length, advice);
41         printf("madvise(%p, %lu, MADV_RANDOM) = %s\n",
42                addr, length, sprintrc(rc));
43
44         static const kernel_ulong_t bogus_length =
45                 (kernel_ulong_t) 0xfffffffffffffaceULL;
46         rc = k_madvise(0, bogus_length, MADV_SEQUENTIAL);
47         printf("madvise(NULL, %llu, MADV_SEQUENTIAL) = %s\n",
48                (unsigned long long) bogus_length, sprintrc(rc));
49
50         if (F8ILL_KULONG_SUPPORTED) {
51                 rc = k_madvise(f8ill_ptr_to_kulong(addr), length, MADV_NORMAL);
52                 printf("madvise(%#llx, %lu, MADV_NORMAL) = %s\n",
53                        (unsigned long long) f8ill_ptr_to_kulong(addr),
54                        length, sprintrc(rc));
55         }
56
57         puts("+++ exited with 0 +++");
58         return 0;
59 }