Project

General

Profile

Bug #3786 ยป mmap-test-3786.c

Test case. - Theo Schlossnagle, 2013-05-25 01:53 PM

 
1
#include <stdio.h>
2
#include <stdlib.h>
3
#include <sys/mman.h>
4

    
5
#define ITER 1000
6
#define SIZE 1048576
7
#define HINT (caddr_t)(uintptr_t)0x7000000
8
static int flags = MAP_PRIVATE|MAP_ANON;
9

    
10
void nearby() {
11
  void *p = 0, *ptrs[ITER] = { 0 };
12
  int i;
13
  printf("Nearby test... ");
14
  for (i=0; i < ITER && p < (void *)0x80000000; i++)
15
    ptrs[i] = p = mmap(HINT, SIZE,
16
                       PROT_READ|PROT_WRITE, flags, -1, 0);
17
  for (i=0; i < ITER; i++)
18
    if (ptrs[i] != NULL && ptrs[i] != MAP_FAILED)
19
      munmap(ptrs[i], SIZE);
20
  if(p < (void *)0x80000000) printf("passed.\n");
21
  else printf("failed => %p.\n", p);
22
}
23

    
24
void fixed(char *detail) {
25
  void *p = 0, *p2 = 0;
26
  printf("Fixed test(%s)... ", detail);
27
  p = mmap(HINT, SIZE, PROT_READ|PROT_WRITE, flags|MAP_FIXED, -1, 0);
28
  if(p != HINT) printf("failed (initial fixed mapping).\n");
29
  else {
30
    p2 = mmap(HINT, SIZE, PROT_READ|PROT_WRITE, flags|MAP_FIXED, -1, 0);
31
    if(p2 == p) printf("passed.\n");
32
    else {
33
      printf("failed => %p.\n", p2);
34
      munmap(p2, SIZE);
35
    }
36
    munmap(p, SIZE);
37
  }
38
}
39

    
40
int main() {
41
  void *obstacle;
42
  nearby();
43

    
44
  fixed("simple");
45

    
46
  obstacle = mmap((HINT + 0x10000), SIZE/2, PROT_READ|PROT_WRITE,
47
                  flags, -1, 0);
48
  if ((caddr_t)obstacle >= HINT || (caddr_t)obstacle < (HINT + 0x10000)) {
49
    fixed("with obstacle");
50
  }
51
  else printf(" *** could not create obstacle for fixed(2) ***\n");
52
  if (obstacle != MAP_FAILED) munmap(obstacle, SIZE/2);
53
  return 0;
54
}
    (1-1/1)