aboutsummaryrefslogtreecommitdiffstats
path: root/src/examples/buffer_with_hole.c
blob: 8d81009c7f84be54ea3f81db4cb902b13deabf8b (plain) (blame)
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
/* klaar@ida

   Create a buffer that spans invalid pages. Used to check memory
   verification.
 */
#include <stdio.h>
#include <stdlib.h>
#include <syscall.h>

/* global variables are placed by the compiler in the process image,
 * among the initialized variables
 * the process image is loaded at the bottom of the virtual memory space
 */
char global = 0;

int main (int argc, char *argv[])
{
   /* local variables are allocated on the proces stack during runtime
    * the stack is at top of the virtual memory space
    */
   char local = 0;
   int fd;
   int count;
   unsigned l, g;

   /* both adresses will of course be valid */
   printf("global: %p\n", &global);
   printf("local : %p\n", &local);

   /* and a pointer starting at the global variable adress, and ending
    * at the local variable addres will have valid start and end
    * but will all addresses (about 3 GB) inbetween be valid?
    */
   l = (unsigned)&local;
   g = (unsigned)&global;
   printf("size : %u MiB\n", (l - g)/1024/1024);

   create("somefile", 1000);
   fd = open("somefile");

   /* this tries to use the buffer (it should fail verification) */
   count = read(fd, &global, (l - g + 1));
   printf("read %i out of %u bytes\n", count, (l - g + 1));
   
   /* this tries to use a buffer probably ending before the stack */
   count = read(fd, &global, (l - g + 1) - 4096);
   printf("read %i out of %u bytes\n", count, (l - g + 1) - 4096);
   
   close(fd);

   return 0;
}