aboutsummaryrefslogtreecommitdiff
path: root/none/tests/solaris/mmap_noreserve.c
blob: 975e95127b45d454b69688b4746a2126af52e641 (plain)
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
87
88
89
90
/* Maps several pages with or without MAP_NORESEVE.
   Mappings with MAP_NORESEVE do not show in /proc/self/xmap
   (only in /proc/self/rmap) until they actually materialize.
   Very nice from Solaris kernel :-(
 */

#include <dlfcn.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <wait.h>
#include <sys/mman.h>
#include <sys/param.h>

static void *do_map(int flags)
{
   flags |= MAP_PRIVATE | MAP_ANON;
   void *addr = mmap(0, PAGESIZE, PROT_READ | PROT_WRITE, flags, -1, 0);
   if (addr == NULL) {
      perror("mmap");
      exit(1);
   } else {
      return addr;
   }
}

static void *do_dlopen(const char *pathname)
{
   int mode = RTLD_LAZY | RTLD_LOCAL;
   void *handle = dlopen(pathname, mode);
   if (handle == NULL) {
      fprintf(stderr, "dlopen failed for %s: %s",
              pathname, dlerror());
      exit(1);
   } else {
      return handle;
   }
}

int main(int argc, const char *argv[])
{
   do_map(MAP_NORESERVE);
   do_dlopen("libm.so");
   do_map(0);
   do_map(0);
   do_map(MAP_NORESERVE);
   do_dlopen("liby.so");
   do_map(MAP_NORESERVE);
   do_map(0);
   do_map(0);
   do_map(MAP_NORESERVE);
   do_map(MAP_NORESERVE);
   do_dlopen("libz.so");
   do_map(MAP_NORESERVE);
   do_map(MAP_NORESERVE);
   do_map(0);

   pid_t pid = fork();
   if (pid == -1) {
      perror("fork");
      exit(1);
   }

   if (pid == 0) {
      do_map(MAP_NORESERVE);
      do_map(0);
      do_map(0);
      do_dlopen("libw.so");
      do_map(0);
      do_map(MAP_NORESERVE);
      do_map(MAP_NORESERVE);
      do_map(0);
      printf("CHILD: PASSED\n");
      fflush(stdout);
      return 0;
   }

   int status;
   if (waitpid(pid, &status, 0) != pid) {
      perror("waitpid");
   } else if ((WIFEXITED(status) != 0) && (WEXITSTATUS(status) == 0)) {
      printf("PASSED\n");
   } else {
      fprintf(stderr, "FAILED: child exited with unexpected status %s %d\n",
              WIFEXITED(status) ? "exit" : "signal", 
              WIFEXITED(status) ? WEXITSTATUS(status) : WTERMSIG(status));
   }

   return 0;
}