sewardj | d935068 | 2012-04-05 07:55:47 +0000 | [diff] [blame] | 1 | |
| 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> |
philippe | f7ec77f | 2014-11-24 17:46:41 +0000 | [diff] [blame] | 13 | #include <string.h> |
sewardj | d935068 | 2012-04-05 07:55:47 +0000 | [diff] [blame] | 14 | #include <assert.h> |
philippe | f7ec77f | 2014-11-24 17:46:41 +0000 | [diff] [blame] | 15 | #include "tests/sys_mman.h" |
| 16 | #include <sys/types.h> |
| 17 | #include <sys/stat.h> |
| 18 | #include <fcntl.h> |
sewardj | d935068 | 2012-04-05 07:55:47 +0000 | [diff] [blame] | 19 | #include "memcheck/memcheck.h" |
| 20 | |
| 21 | /* Cause memcheck to complain about the address "a" and so to print |
philippe | d0da968 | 2014-12-28 17:30:22 +0000 | [diff] [blame] | 22 | its best guess as to what "a" actually is.*/ |
sewardj | d935068 | 2012-04-05 07:55:47 +0000 | [diff] [blame] | 23 | void croak ( void* aV ) |
| 24 | { |
philippe | d0da968 | 2014-12-28 17:30:22 +0000 | [diff] [blame] | 25 | if(VALGRIND_CHECK_MEM_IS_ADDRESSABLE(aV,1) != 0) |
| 26 | return; |
sewardj | d935068 | 2012-04-05 07:55:47 +0000 | [diff] [blame] | 27 | char* a = (char*)aV; |
| 28 | char* undefp = malloc(1); |
| 29 | char saved = *a; |
| 30 | assert(undefp); |
| 31 | *a = *undefp; |
florian | 06bc722 | 2013-10-01 22:38:43 +0000 | [diff] [blame] | 32 | (void) VALGRIND_CHECK_MEM_IS_DEFINED(a, 1); |
sewardj | d935068 | 2012-04-05 07:55:47 +0000 | [diff] [blame] | 33 | *a = saved; |
| 34 | free(undefp); |
| 35 | } |
| 36 | |
| 37 | struct s1 |
| 38 | { |
| 39 | char c; |
| 40 | short s; |
| 41 | int i; |
| 42 | long l; |
| 43 | float f; |
| 44 | double d; |
| 45 | }; |
| 46 | |
| 47 | struct s1 S2[30]; |
| 48 | |
| 49 | int main ( void ) |
| 50 | { |
| 51 | struct s1 local; |
| 52 | struct s1* onheap = malloc(sizeof (struct s1)); |
philippe | f7ec77f | 2014-11-24 17:46:41 +0000 | [diff] [blame] | 53 | void *p, *q; |
| 54 | int fd; |
| 55 | int n; |
| 56 | char filename[256]; |
| 57 | |
sewardj | d935068 | 2012-04-05 07:55:47 +0000 | [diff] [blame] | 58 | assert(onheap); |
| 59 | croak(&onheap->i); |
| 60 | |
| 61 | croak( &S2[0].i ); |
| 62 | croak( &local.i ); |
philippe | f7ec77f | 2014-11-24 17:46:41 +0000 | [diff] [blame] | 63 | |
| 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 */ |
sewardj | 8eb8bab | 2015-07-21 14:44:28 +0000 | [diff] [blame] | 71 | snprintf(filename, sizeof(filename), "./valgrind-dw4-test.%ld", |
| 72 | (long) getpid()); |
philippe | f7ec77f | 2014-11-24 17:46:41 +0000 | [diff] [blame] | 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 | |
philippe | d0da968 | 2014-12-28 17:30:22 +0000 | [diff] [blame] | 85 | /* 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 | |
sewardj | d935068 | 2012-04-05 07:55:47 +0000 | [diff] [blame] | 95 | return 0; |
| 96 | } |