OSDN Git Service

c384d49c590bf5e9fe74ce38756716ef93d946bb
[pf3gnuchains/gcc-fork.git] / gcc / testsuite / gcc.dg / struct-ret-3.c
1 /* PR middle-end/31309 */
2 /* Origin: Peeter Joot <peeterj@ca.ibm.com> */
3
4 /* { dg-do run { target *-*-linux* } } */
5
6 #include <sys/mman.h>
7 #include <string.h>
8 #include <stdio.h>
9 #include <errno.h>
10 #include <unistd.h>
11
12 unsigned long ossAlignX(unsigned long i, unsigned long X)
13 {
14    return ((i + (X - 1)) & ~(unsigned long) (X - 1));
15 }
16
17 struct STRUCT_6_BYTES
18 {
19    unsigned char slot[sizeof(unsigned short)];
20    unsigned char page[sizeof(unsigned int)];
21 };
22
23 struct SQLU_DICT_INFO_0
24 {
25    void *pBlah;
26    char bSomeFlag1;
27    char bSomeFlag2;
28    struct STRUCT_6_BYTES dRID;
29 };
30
31 struct SQLU_DATAPART_0
32 {
33    struct SQLU_DICT_INFO_0 *pDictRidderInfo;
34 };
35
36 struct XXX
37 {
38    struct SQLU_DATAPART_0 *m_pDatapart;
39 };
40
41 struct STRUCT_6_BYTES INIT_6_BYTES_ZERO()
42 {
43    struct STRUCT_6_BYTES ridOut = {{0,0}, {0,0,0,0}};
44    return ridOut;
45 }
46
47 void Initialize(struct XXX *this, int iIndex)
48 {
49    struct SQLU_DICT_INFO_0 *pDictRidderInfo
50      = this->m_pDatapart[iIndex].pDictRidderInfo;
51    pDictRidderInfo->bSomeFlag1 = 0;
52    pDictRidderInfo->bSomeFlag2 = 0;
53    pDictRidderInfo->dRID = INIT_6_BYTES_ZERO();
54 }
55
56 int main(void)
57 {
58    int rc;
59
60    struct stuff
61    {
62       char c0[4096-sizeof(struct XXX)];
63       struct XXX o;
64       char c1[4096*2-sizeof(struct SQLU_DATAPART_0)];
65       struct SQLU_DATAPART_0 dp;
66       char c2[4096*2-sizeof(struct SQLU_DICT_INFO_0)];
67       struct SQLU_DICT_INFO_0 di;
68       char c3[4096];
69    };
70
71    char buf[sizeof(struct stuff)+4096];
72    struct stuff *u = (struct stuff *)ossAlignX((unsigned long)&buf[0], 4096);
73
74    /* This test assumes system memory page size of 4096 bytes or less.  */
75    if (sysconf(_SC_PAGESIZE) > 4096)
76      return 0;
77
78    memset(u, 1, sizeof(struct stuff));
79    u->c1[0] = '\xAA';
80    u->c2[0] = '\xBB';
81    u->c3[0] = '\xCC';
82
83    rc = mprotect(u->c1, 4096, PROT_NONE);
84    if (rc == -1)
85       printf("mprotect:c1: %d: %d(%s)\n", rc, errno, strerror(errno));
86
87    rc = mprotect(u->c2, 4096, PROT_NONE);
88    if (rc == -1)
89       printf("mprotect:c2: %d: %d(%s)\n", rc, errno, strerror(errno));
90
91    rc = mprotect(u->c3, 4096, PROT_NONE);
92    if (rc == -1)
93       printf("mprotect:c3: %d: %d(%s)\n", rc, errno, strerror(errno));
94
95    u->o.m_pDatapart = &u->dp;
96    u->dp.pDictRidderInfo = &u->di;
97    Initialize(&u->o, 0);
98
99    mprotect(u->c1, 4096, PROT_READ|PROT_WRITE);
100    mprotect(u->c2, 4096, PROT_READ|PROT_WRITE);
101    mprotect(u->c3, 4096, PROT_READ|PROT_WRITE);
102
103    return 0;
104 }