File: brk.c

package info (click to toggle)
valgrind 1%3A3.3.1-3
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 34,452 kB
  • ctags: 27,778
  • sloc: ansic: 234,398; sh: 14,186; xml: 11,662; perl: 4,410; asm: 3,135; makefile: 3,011; exp: 625; cpp: 255; haskell: 195
file content (44 lines) | stat: -rw-r--r-- 1,075 bytes parent folder | download
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
#include <assert.h>
#include <stdio.h>
#if !defined(_AIX)
# include <sys/syscall.h>
#endif
#include <sys/types.h>
#include <unistd.h>

// kernel brk() and libc brk() act quite differently...

int main(void)
{
   int i;
   void* orig_ds = sbrk(0);
   void* ds = orig_ds;
   void* vals[10];
   void* res;
#define EOL ((void*)( ~(long)0 ))
   vals[0] = (void*)0;
   vals[1] = (void*)1;
   vals[2] = ds - 0x1;          // small shrink
   vals[3] = ds;
   vals[4] = ds + 0x1000;       // small growth
   vals[5] = ds + 0x40000000;   // too-big growth
   vals[6] = ds + 0x500;        // shrink a little, but still above start size
   vals[7] = ds - 0x1;          // shrink below start size
//   vals[8] = ds - 0x1000;       // shrink a lot below start size (into text)
//   vals[9] = EOL;
   vals[8] = EOL;

   for (i = 0; EOL != vals[i]; i++) {
#     if !defined(_AIX)
      res = (void*)syscall(__NR_brk, vals[i]);
#     endif
   }

   assert( 0 == brk(orig_ds) );  // libc brk()

   for (i = 0; EOL != vals[i]; i++) {
      res = (void*)(long)brk(vals[i]);
   }

   return 0;
}