1#include <assert.h>
2#include <stdio.h>
3#include <sys/syscall.h>
4#include <sys/types.h>
5#include <unistd.h>
6
7// kernel brk() and libc brk() act quite differently...
8
9int main(void)
10{
11   int i;
12   void* orig_ds = sbrk(0);
13   void* ds = orig_ds;
14   void* vals[10];
15   void* res __attribute__((unused));
16#define EOL ((void*)( ~(long)0 ))
17   vals[0] = (void*)0;
18   vals[1] = (void*)1;
19   vals[2] = ds - 0x1;          // small shrink
20   vals[3] = ds;
21   vals[4] = ds + 0x1000;       // small growth
22   vals[5] = ds + 0x40000000;   // too-big growth
23   vals[6] = ds + 0x500;        // shrink a little, but still above start size
24   vals[7] = ds - 0x1;          // shrink below start size
25//   vals[8] = ds - 0x1000;       // shrink a lot below start size (into text)
26//   vals[9] = EOL;
27   vals[8] = EOL;
28
29   for (i = 0; EOL != vals[i]; i++) {
30      res = (void*)syscall(__NR_brk, vals[i]);
31   }
32
33   assert( 0 == brk(orig_ds) );  // libc brk()
34
35   for (i = 0; EOL != vals[i]; i++) {
36      res = (void*)(long)brk(vals[i]);
37   }
38
39   return 0;
40}
41