File: brk.c

package info (click to toggle)
valgrind 1%3A3.12.0~svn20160714-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 120,428 kB
  • ctags: 70,855
  • sloc: ansic: 674,645; exp: 26,134; xml: 21,574; asm: 7,570; cpp: 7,567; makefile: 7,380; sh: 6,188; perl: 5,855; haskell: 195
file content (40 lines) | stat: -rw-r--r-- 1,036 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
#include <assert.h>
#include <stdio.h>
#include <sys/syscall.h>
#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 __attribute__((unused));
#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++) {
      res = (void*)syscall(__NR_brk, vals[i]);
   }

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

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

   return 0;
}