File: brk.c

package info (click to toggle)
valgrind 1%3A3.24.0-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 176,332 kB
  • sloc: ansic: 795,029; exp: 26,134; xml: 23,472; asm: 14,393; cpp: 9,397; makefile: 7,464; sh: 6,122; perl: 5,446; python: 1,498; javascript: 981; awk: 166; csh: 1
file content (83 lines) | stat: -rw-r--r-- 1,621 bytes parent folder | download | duplicates (6)
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
/* Test for the brk syscall wrapper. */

#include <assert.h>
#include <errno.h>
#include <stddef.h>
#include <sys/syscall.h>

/* Data segment end. */
extern int _end;
static char *begin = (char *)&_end;

__attribute__((noinline))
static int test_begin(void)
{
   int res = 0;
   int tmp;

   /* Check that a value at the break is inaccessible. */
   if (*begin)
      res++;

   /* Allocate one byte and check that the last byte is accessible and
      initialized. */
   tmp = syscall(SYS_brk, begin + 1);
   assert(tmp != -1);
   if (*begin)
      res++;

   /* Deallocate one byte and check that the last byte is now inaccessible. */
   tmp = syscall(SYS_brk, begin);
   assert(tmp != -1);
   if (*begin)
      res++;

   return res;
}

__attribute__((noinline))
static void test_updown(void)
{
   int tmp;
   size_t i;

#define MAX_SIZE 8192
   /* Run up phase. */
   for (i = 0; i < MAX_SIZE; i++) {
      tmp = syscall(SYS_brk, begin + i);
      assert(tmp != -1);
   }

   /* Run down phase. */
   for (i = 0; i < MAX_SIZE; i++) {
      tmp = syscall(SYS_brk, begin + MAX_SIZE - 1 - i);
      assert(tmp != -1);
   }
#undef MAX_SIZE
}

__attribute__((noinline))
static void test_range(void)
{
   int tmp;

   tmp = syscall(SYS_brk, begin - 1);
   assert(tmp == -1);
   assert(errno == ENOMEM);

   /* Unified limit for 64-bit and 32-bit version. */
   unsigned long long impossible_limit = 0xffffff4fffffffULL;
   tmp = syscall(SYS_brk, impossible_limit);
   assert(tmp == -1);
   assert(errno == ENOMEM);
}

int main(void)
{
   int res;
   res = test_begin();
   test_updown();
   test_range();
   return res;
}