blob: 574f97c7191a650f6c6f88b64e8c509e8a49e6c1 [file] [log] [blame]
sewardjd9350682012-04-05 07:55:47 +00001
2/* Check of variable location identification when using .debug_types. */
3
4/* Relevant compile flags are:
5
6 -Wall -g -I$prefix/include/valgrind -gdwarf-4 -fdebug-types-section
7
8 eg -Wall -g -I`pwd`/Inst/include/valgrind -gdwarf-4 -fdebug-types-section
9*/
10
11#include <stdio.h>
12#include <stdlib.h>
philippef7ec77f2014-11-24 17:46:41 +000013#include <string.h>
sewardjd9350682012-04-05 07:55:47 +000014#include <assert.h>
philippef7ec77f2014-11-24 17:46:41 +000015#include "tests/sys_mman.h"
16#include <sys/types.h>
17#include <sys/stat.h>
18#include <fcntl.h>
sewardjd9350682012-04-05 07:55:47 +000019#include "memcheck/memcheck.h"
20
21/* Cause memcheck to complain about the address "a" and so to print
philipped0da9682014-12-28 17:30:22 +000022 its best guess as to what "a" actually is.*/
sewardjd9350682012-04-05 07:55:47 +000023void croak ( void* aV )
24{
philipped0da9682014-12-28 17:30:22 +000025 if(VALGRIND_CHECK_MEM_IS_ADDRESSABLE(aV,1) != 0)
26 return;
sewardjd9350682012-04-05 07:55:47 +000027 char* a = (char*)aV;
28 char* undefp = malloc(1);
29 char saved = *a;
30 assert(undefp);
31 *a = *undefp;
florian06bc7222013-10-01 22:38:43 +000032 (void) VALGRIND_CHECK_MEM_IS_DEFINED(a, 1);
sewardjd9350682012-04-05 07:55:47 +000033 *a = saved;
34 free(undefp);
35}
36
37struct s1
38{
39 char c;
40 short s;
41 int i;
42 long l;
43 float f;
44 double d;
45};
46
47struct s1 S2[30];
48
49int main ( void )
50{
51 struct s1 local;
52 struct s1* onheap = malloc(sizeof (struct s1));
philippef7ec77f2014-11-24 17:46:41 +000053 void *p, *q;
54 int fd;
55 int n;
56 char filename[256];
57
sewardjd9350682012-04-05 07:55:47 +000058 assert(onheap);
59 croak(&onheap->i);
60
61 croak( &S2[0].i );
62 croak( &local.i );
philippef7ec77f2014-11-24 17:46:41 +000063
64 /* Describe anonymous mmap-ed */
65 p = mmap( 0, 16 * 1024, PROT_READ|PROT_WRITE,
66 MAP_PRIVATE|MAP_ANONYMOUS, -1, 0 );
67 assert(p != MAP_FAILED);
68 croak( p);
69
70 /* Describe file mmap-ed */
71 snprintf(filename, sizeof(filename), "./valgrind-dw4-test.%d",
72 getpid());
73
74 unlink(filename);
75
76 fd = open(filename, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
77 assert (fd > 0);
78 n = write(fd, filename, strlen(filename));
79 assert (n > 8);
80 q = mmap(NULL, 100, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
81 assert (q != MAP_FAILED);
82 croak( q);
83 unlink(filename);
84
philipped0da9682014-12-28 17:30:22 +000085 /* Describe memory in or past the heap end. */
86 void *addr = sbrk(0);
87 croak(addr); // in the first brk page, after brk_limit
88 sbrk(4 * 1024); // increase brk segment
89 croak(addr); // Now, must be inside.
90 addr = (void *) ((char*)addr + 2 * 1024);
91 croak(addr); // Must still be inside.
92 sbrk(-3*1024);
93 croak(addr); // Must now be after.
94
sewardjd9350682012-04-05 07:55:47 +000095 return 0;
96}