File: mmap5.c

package info (click to toggle)
gdb-doc 16.3-1
  • links: PTS, VCS
  • area: non-free
  • in suites: forky, sid, trixie
  • size: 244,264 kB
  • sloc: ansic: 2,134,731; asm: 375,582; exp: 206,875; cpp: 73,639; makefile: 70,232; sh: 26,038; python: 13,697; yacc: 11,341; ada: 7,358; xml: 6,098; perl: 5,077; pascal: 3,389; tcl: 2,986; f90: 2,764; lisp: 1,984; cs: 879; lex: 738; sed: 228; awk: 181; objc: 137; fortran: 57
file content (91 lines) | stat: -rw-r--r-- 1,608 bytes parent folder | download | duplicates (9)
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
/*
#progos: linux
*/

#define _GNU_SOURCE
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/mman.h>

int main (int argc, char *argv[])
{
  int fd = open (argv[0], O_RDONLY);
  struct stat sb;
  int size;
  void *a;
  void *b;
  const char *str = "a string you'll only find in the program";

  if (fd == -1)
    {
      perror ("open");
      abort ();
    }

  if (fstat (fd, &sb) < 0)
    {
      perror ("fstat");
      abort ();
    }

  size = 8192;
#ifdef MMAP_SIZE1
  size = MMAP_SIZE1;
#endif

#ifndef MMAP_PROT1
#define MMAP_PROT1 PROT_READ | PROT_WRITE | PROT_EXEC
#endif

#ifndef MMAP_FLAGS1
#define MMAP_FLAGS1 MAP_PRIVATE | MAP_ANONYMOUS
#endif

  /* Get a page, any page.  */
  b = mmap (NULL, size, MMAP_PROT1, MMAP_FLAGS1, -1, 0);
  if (b == MAP_FAILED)
    abort ();

  /* Remember it, unmap it.  */
#ifndef NO_MUNMAP
  if (munmap (b, size) != 0)
    abort ();
#endif

#ifdef MMAP_ADDR2
  b = MMAP_ADDR2;
#endif

#ifndef MMAP_PROT2
#define MMAP_PROT2 PROT_READ | PROT_EXEC
#endif

#ifndef MMAP_FLAGS2
#define MMAP_FLAGS2 MAP_DENYWRITE | MAP_FIXED | MAP_PRIVATE
#endif

  size = sb.st_size;
#ifdef MMAP_SIZE2
  size = MMAP_SIZE2;
#endif

#define MMAP_TEST_BAD_ORIG \
 (a == MAP_FAILED || memmem (a, size, str, strlen (str) + 1) == NULL)
#ifndef MMAP_TEST_BAD
#define MMAP_TEST_BAD MMAP_TEST_BAD_ORIG
#endif

  /* Try mapping the now non-mapped page fixed.  */
  a = mmap (b, size, MMAP_PROT2, MMAP_FLAGS2, fd, 0);

  if (MMAP_TEST_BAD)
    abort ();

  printf ("pass\n");
  exit (0);
}