]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/valgrind/src/valgrind-3.6.0-svn/exp-ptrcheck/tests/mm.c
update
[l4.git] / l4 / pkg / valgrind / src / valgrind-3.6.0-svn / exp-ptrcheck / tests / mm.c
1 #include "tests/sys_mman.h"
2 #include <unistd.h>
3 #include "arith_include1.c"
4
5 // For some reason, the stack frame below __GI_write is disappearing.
6 // Therefore, if I don't want the write errors to be merged, I have to
7 // ensure they have a different stack trace.  I do this by using this
8 // function.  Weird.
9 void mywrite(char* buf, int len)
10 {
11    write(-1, buf, len);
12 }
13
14 int main(void)
15 {
16    struct sigaction sigsegv;
17    
18    char c __attribute__((unused));
19    
20    // This fails due to a bad fd (at one point I was not handling failing
21    // mmap() calls, and would have got a seg fault).
22    char* res1 = mmap(0, 0, PROT_READ, MAP_PRIVATE, -1, 0 );
23
24    // This succeeds but is meaningless.  Important thing is that the size is
25    // zero, so Annelid should not subtract one from the size when doing any
26    // range calculations.  (It did at one point, giving 0xffffffff, which
27    // screwed everything up.)
28    char* res2 = mmap(0, 0, PROT_READ, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0 );
29
30    // This succeeds and is useful.
31    char* res3 = mmap(0, getpagesize(), PROT_READ, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
32
33    assert(MAP_FAILED == res1);
34    assert(NULL       == res2);
35    assert(MAP_FAILED != res3 && NULL != res3);
36
37    // Intall SEGV handler 
38    sigsegv.sa_handler = SEGV_handler;
39    sigsegv.sa_flags   = 0;
40    assert( 0 == sigemptyset( &sigsegv.sa_mask ) );
41    assert( 0 == sigaction(SIGSEGV, &sigsegv, NULL) );
42
43    #define TTT(i) \
44       if (__builtin_setjmp(TTT_jmpbuf) == 0) { c = res3[i]; }
45
46    TTT(0);
47    TTT(-1);
48    mywrite(res3,   5);
49    mywrite(res3-1, 5);
50
51    assert( 0 == munmap(res3, getpagesize()) );
52
53    TTT(0);
54    TTT(-1);
55    mywrite(res3,   5);
56    mywrite(res3-1, 5);
57    
58    return 0;
59 }