ftmemsim-valgrind/memcheck/tests/pointer-trace.c
Julian Seward 7c542ccd39 Add new files resulting from merging in the 2.4.0 line. Many of these
seem to be simply duplication of the x86 instruction set tests into
the addrcheck and helgrind trees.  I'm not sure what this duplication
achieves.



git-svn-id: svn://svn.valgrind.org/valgrind/trunk@3264
2005-03-10 23:23:45 +00:00

60 lines
1.4 KiB
C

/*
Make sure that leak-check's pointer tracing avoids traps
*/
#include <stdio.h>
#include "memcheck/memcheck.h"
#include <sys/mman.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
int main()
{
const int ptrbits = sizeof(void *) * 8;
const int stepbits = 14;
const int stepsize = (1 << stepbits);
const int nptrs = 1 << (ptrbits - stepbits);
char **volatile ptrs;
int i;
int fd;
char *map;
ptrs = malloc(nptrs * sizeof(char *));
for(i = 0; i < nptrs; i++)
ptrs[i] = (char *)(i << stepbits);
/* lay some traps */
map = mmap(0, stepsize * 2, PROT_NONE, MAP_PRIVATE|MAP_NORESERVE|MAP_ANONYMOUS, -1, 0);
if (map == (char *)-1)
perror("trap 1 failed");
map = mmap(0, stepsize * 2, PROT_WRITE, MAP_PRIVATE|MAP_NORESERVE|MAP_ANONYMOUS, -1, 0);
if (map == (char *)-1)
perror("trap 2 failed");
/* non-zero mmap of a zero-length file -> SIGBUS */
fd = open("./pointer-trace-test-file", O_RDWR | O_CREAT | O_EXCL, 0600);
unlink("./pointer-trace-test-file");
map = mmap(0, stepsize * 2, PROT_WRITE|PROT_READ, MAP_PRIVATE, fd, 0);
if (map == (char *)-1)
perror("trap 3 failed");
//printf("trap 3 = %p-%p\n", map, map+stepsize*2);
map = mmap(0, 256*1024, PROT_NONE, MAP_PRIVATE|MAP_NORESERVE|MAP_ANONYMOUS, -1, 0);
if (map == (char *)-1)
perror("trap 4 failed");
else {
munmap(map, 256*1024);
VALGRIND_MAKE_READABLE(map, 256*1024); /* great big fat lie */
}
VALGRIND_DO_LEAK_CHECK;
ptrs = 0;
return 0;
}