aboutsummaryrefslogtreecommitdiff
path: root/posix/tst-mmap.c
blob: e4db364301e14d92a3041180d1b3db4a49bf3185 (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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/mman.h>


int
main (void)
{
  int result = 0;
  FILE *fp;
  size_t c;
  char buf[1000];
  int fd;
  unsigned char *ptr;
  size_t ps = sysconf (_SC_PAGESIZE);
  void *mem;

  /* Create a file and put some data in it.  */
  fp = tmpfile ();
  if (fp == NULL)
    {
      printf ("Cannot create temporary file: %m\n");
      return 1;
    }
  fd = fileno (fp);

  for (c = 0; c < sizeof (buf); ++c)
    buf[c] = '0' + (c % 10);

  for (c = 0; c < 20; ++c)
    if (fwrite (buf, 1, sizeof (buf), fp) != sizeof (buf))
      {
	printf ("`fwrite' failed: %m\n");
	return 1;
      }
  fflush (fp);
  assert (ps + 1000 < c * sizeof (buf));

  /* First try something which is not allowed: map at an offset which is
     not module the pagesize.  */
  ptr = mmap (NULL, 1000, PROT_READ, MAP_SHARED, fd, ps - 1);
  if (ptr != MAP_FAILED)
    {
      puts ("mapping at offset with mod pagesize != 0 succeeded!");
      result = 1;
    }
  else if (errno != EINVAL && errno != ENOSYS)
    {
      puts ("wrong error value for mapping at offset with mod pagesize != 0: %m (should be EINVAL)");
      result = 1;
    }

  /* Try the same for mmap64.  */
  ptr = mmap64 (NULL, 1000, PROT_READ, MAP_SHARED, fd, ps - 1);
  if (ptr != MAP_FAILED)
    {
      puts ("mapping at offset with mod pagesize != 0 succeeded!");
      result = 1;
    }
  else if (errno != EINVAL && errno != ENOSYS)
    {
      puts ("wrong error value for mapping at offset with mod pagesize != 0: %m (should be EINVAL)");
      result = 1;
    }

  /* And the same for private mapping.  */
  ptr = mmap (NULL, 1000, PROT_READ, MAP_PRIVATE, fd, ps - 1);
  if (ptr != MAP_FAILED)
    {
      puts ("mapping at offset with mod pagesize != 0 succeeded!");
      result = 1;
    }
  else if (errno != EINVAL && errno != ENOSYS)
    {
      puts ("wrong error value for mapping at offset with mod pagesize != 0: %m (should be EINVAL)");
      result = 1;
    }

  /* Try the same for mmap64.  */
  ptr = mmap64 (NULL, 1000, PROT_READ, MAP_PRIVATE, fd, ps - 1);
  if (ptr != MAP_FAILED)
    {
      puts ("mapping at offset with mod pagesize != 0 succeeded!");
      result = 1;
    }
  else if (errno != EINVAL && errno != ENOSYS)
    {
      puts ("wrong error value for mapping at offset with mod pagesize != 0: %m (should be EINVAL)");
      result = 1;
    }

  /* Get a valid address.  */
  mem = malloc (2 * ps);
  if (mem != NULL)
    {
      /* Now we map at an address which is not mod pagesize.  */
      ptr = mmap (mem + 1, 1000, PROT_READ, MAP_SHARED | MAP_FIXED, fd, ps);
      if (ptr != MAP_FAILED)
	{
	  puts ("mapping at address with mod pagesize != 0 succeeded!");
	  result = 1;
	}
      else  if (errno != EINVAL && errno != ENOSYS)
	{
	  puts ("wrong error value for mapping at address with mod pagesize != 0: %m (should be EINVAL)");
	  result = 1;
	}

      /* Try the same for mmap64.  */
      ptr = mmap64 (mem + 1, 1000, PROT_READ, MAP_SHARED | MAP_FIXED, fd, ps);
      if (ptr != MAP_FAILED)
	{
	  puts ("mapping at address with mod pagesize != 0 succeeded!");
	  result = 1;
	}
      else  if (errno != EINVAL && errno != ENOSYS)
	{
	  puts ("wrong error value for mapping at address with mod pagesize != 0: %m (should be EINVAL)");
	  result = 1;
	}

      /* And again for MAP_PRIVATE.  */
      ptr = mmap (mem + 1, 1000, PROT_READ, MAP_PRIVATE | MAP_FIXED, fd, ps);
      if (ptr != MAP_FAILED)
	{
	  puts ("mapping at address with mod pagesize != 0 succeeded!");
	  result = 1;
	}
      else  if (errno != EINVAL && errno != ENOSYS)
	{
	  puts ("wrong error value for mapping at address with mod pagesize != 0: %m (should be EINVAL)");
	  result = 1;
	}

      /* Try the same for mmap64.  */
      ptr = mmap64 (mem + 1, 1000, PROT_READ, MAP_PRIVATE | MAP_FIXED, fd, ps);
      if (ptr != MAP_FAILED)
	{
	  puts ("mapping at address with mod pagesize != 0 succeeded!");
	  result = 1;
	}
      else  if (errno != EINVAL && errno != ENOSYS)
	{
	  puts ("wrong error value for mapping at address with mod pagesize != 0: %m (should be EINVAL)");
	  result = 1;
	}

      free (mem);
    }

  /* Now map the memory and see whether the content of the mapped area
     is correct.  */
  ptr = mmap (NULL, 1000, PROT_READ, MAP_SHARED, fd, ps);
  if (ptr == MAP_FAILED)
    {
      if (errno != ENOSYS)
	{
	  printf ("cannot mmap file: %m\n");
	  result = 1;
	}
    }
  else
    {
      for (c = ps; c < ps + 1000; ++c)
	if (ptr[c - ps] != '0' + (c % 10))
	  {
	    printf ("wrong data mapped at offset %d\n", c);
	    result = 1;
	  }
    }

  /* And for mmap64. */
  ptr = mmap64 (NULL, 1000, PROT_READ, MAP_SHARED, fd, ps);
  if (ptr == MAP_FAILED)
    {
      if (errno != ENOSYS)
	{
	  printf ("cannot mmap file: %m\n");
	  result = 1;
	}
    }
  else
    {
      for (c = ps; c < ps + 1000; ++c)
	if (ptr[c - ps] != '0' + (c % 10))
	  {
	    printf ("wrong data mapped at offset %d\n", c);
	    result = 1;
	  }
    }

  /* That's it.  */
  return result;
}