| 1 |
/* { dg-do run { target { { *-*-linux* *-*-gnu* *-*-uclinux* } && mmap } } } */ |
| 2 |
|
| 3 |
#include <unistd.h> |
| 4 |
#include <stdlib.h> |
| 5 |
#include <sys/mman.h> |
| 6 |
|
| 7 |
struct S { |
| 8 |
int a; |
| 9 |
}; |
| 10 |
struct M { |
| 11 |
int a, b; |
| 12 |
}; |
| 13 |
|
| 14 |
int __attribute__((noipa)) |
| 15 |
f(struct S *p, int c, int d) |
| 16 |
{ |
| 17 |
int r; |
| 18 |
if (c) |
| 19 |
{ |
| 20 |
if (d) |
| 21 |
r = p->a; |
| 22 |
else |
| 23 |
r = ((struct M*)p)->a; |
| 24 |
} |
| 25 |
else |
| 26 |
r = ((struct M*)p)->b; |
| 27 |
return r; |
| 28 |
} |
| 29 |
|
| 30 |
int main () |
| 31 |
{ |
| 32 |
long pgsz = sysconf(_SC_PAGESIZE); |
| 33 |
if (pgsz < sizeof (struct M)) |
| 34 |
return 0; |
| 35 |
char *p = mmap ((void *) 0, 2 * pgsz, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, |
| 36 |
-1, 0); |
| 37 |
if (p == MAP_FAILED) |
| 38 |
return 0; |
| 39 |
if (mprotect (p, pgsz, PROT_READ | PROT_WRITE)) |
| 40 |
return 0; |
| 41 |
struct S *q = (struct S *)(p + pgsz) - 1; |
| 42 |
q->a = 42; |
| 43 |
if (f (q, 1, 1) != 42) |
| 44 |
abort (); |
| 45 |
return 0; |
| 46 |
} |