4 #define ARRAY_LENGTH(array) (sizeof((array)) / sizeof(0[(array)]))
13 /* Obtain an address inside the stack using a dirty trick. */
14 uintptr_t local
= (uintptr_t)&fi
;
16 /* Open /proc/self/xmap for reading. */
17 fi
= fopen("/proc/self/xmap", "r");
23 /* Read the file until EOF or the stack is found. */
24 while (!done
&& !found
) {
27 items
= fread(map
, sizeof(map
[0]), ARRAY_LENGTH(map
), fi
);
28 if (items
!= ARRAY_LENGTH(map
)) {
36 /* Scan the read mappings. */
37 for (i
= 0; i
< items
; i
++)
38 if (map
[i
].pr_vaddr
<= local
39 && local
< map
[i
].pr_vaddr
+ map
[i
].pr_size
) {
40 /* Stack was found, validate it. */
42 if ((map
[i
].pr_mflags
& (MA_READ
| MA_WRITE
| MA_EXEC
))
43 != (MA_READ
| MA_WRITE
)) {
44 fprintf(stderr
, "Incorrect stack mapping detected.\n");
50 /* Check if the stack was indeed found. */
52 fprintf(stderr
, "Stack not found.\n");