This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [PATCH, testsuite]: Committed: gcc.dg/struct-ret-3.c is valid only if target PAGE_SIZE <= 4096


Peeter Joot wrote:

> gcc.dg/struct-ret-3.c hardcodes array sizes and alignment values, so it
> is only valid when target PAGE_SIZE <= 4096. Arrays are aligned on 4096
> byte page boundaries, so mprotect can fail when system memory page size
> is i.e. 8192 bytes. Also, due to the structure of "struct stuff", test
> fields "dp" and "di" are just at the end of 8192 byte pages.
>
> Since the granularity of mprotect is PAGE_SIZE, PROT_NONE includes "dp"
> and "di" memory as well, leading to segfault when PAGE_SIZE is 8192
> bytes or more.
>
> Since VLA can't be used when an array is part of a record, just skip the
> test when PAGE_SIZE is bigger than 4096 bytes.


I'd guess that the original problem could potentially occur on other page size platforms too. I was going to suggest using alloca or malloc instead of the stack buffer (substituting the real page size value for the hardcode 4K), but in retrospect both of those are possibly porting issues (can alloca be used portably?, and can mprotect be used portably for malloc'd memory?).

Unless there is a reason that this test is in fact not valid for larger page size platforms (ie: that VLA thing), the best fix is probably to combine what you have done with a bigger hardcoded buffer size (64K say).

I have committed following testsuite patch to mainline SVN:


2009-01-06 Uros Bizjak <ubizjak@gmail.com>

   * gcc.dg/struct-ret-3.c (CHUNK_SIZE): New define.  Use CHUNK_SIZE
   instead of hardcoded number.  Increase CHUNK_SIZE to 16384 for
   large stack sizes.

Patch was tested on alphaev56-unknown-linux-gnu and x86_64-pc-linux-gnu {,-m32}.

Uros.

Index: struct-ret-3.c
===================================================================
--- struct-ret-3.c	(revision 143122)
+++ struct-ret-3.c	(working copy)
@@ -9,6 +9,12 @@
 #include <errno.h>
 #include <unistd.h>
 
+#if defined(STACK_SIZE) && (STACK_SIZE < 128*1024)
+ #define CHUNK_SIZE 4096
+#else
+ #define CHUNK_SIZE 16384
+#endif
+
 unsigned long ossAlignX(unsigned long i, unsigned long X)
 {
    return ((i + (X - 1)) & ~(unsigned long) (X - 1));
@@ -59,20 +65,22 @@
 
    struct stuff
    {
-      char c0[4096-sizeof(struct XXX)];
+      char c0[CHUNK_SIZE-sizeof(struct XXX)];
       struct XXX o;
-      char c1[4096*2-sizeof(struct SQLU_DATAPART_0)];
+      char c1[CHUNK_SIZE*2-sizeof(struct SQLU_DATAPART_0)];
       struct SQLU_DATAPART_0 dp;
-      char c2[4096*2-sizeof(struct SQLU_DICT_INFO_0)];
+      char c2[CHUNK_SIZE*2-sizeof(struct SQLU_DICT_INFO_0)];
       struct SQLU_DICT_INFO_0 di;
-      char c3[4096];
+      char c3[CHUNK_SIZE];
    };
 
-   char buf[sizeof(struct stuff)+4096];
-   struct stuff *u = (struct stuff *)ossAlignX((unsigned long)&buf[0], 4096);
+   char buf[sizeof(struct stuff)+CHUNK_SIZE];
+   struct stuff *u
+     = (struct stuff *)ossAlignX((unsigned long)&buf[0], CHUNK_SIZE);
 
-   /* This test assumes system memory page size of 4096 bytes or less.  */
-   if (sysconf(_SC_PAGESIZE) > 4096)
+   /* This test assumes system memory page
+      size of CHUNK_SIZE bytes or less.  */
+   if (sysconf(_SC_PAGESIZE) > CHUNK_SIZE)
      return 0;
 
    memset(u, 1, sizeof(struct stuff));
@@ -80,15 +88,15 @@
    u->c2[0] = '\xBB';
    u->c3[0] = '\xCC';
 
-   rc = mprotect(u->c1, 4096, PROT_NONE);
+   rc = mprotect(u->c1, CHUNK_SIZE, PROT_NONE);
    if (rc == -1)
       printf("mprotect:c1: %d: %d(%s)\n", rc, errno, strerror(errno));
 
-   rc = mprotect(u->c2, 4096, PROT_NONE);
+   rc = mprotect(u->c2, CHUNK_SIZE, PROT_NONE);
    if (rc == -1)
       printf("mprotect:c2: %d: %d(%s)\n", rc, errno, strerror(errno));
 
-   rc = mprotect(u->c3, 4096, PROT_NONE);
+   rc = mprotect(u->c3, CHUNK_SIZE, PROT_NONE);
    if (rc == -1)
       printf("mprotect:c3: %d: %d(%s)\n", rc, errno, strerror(errno));
 
@@ -96,9 +104,9 @@
    u->dp.pDictRidderInfo = &u->di;
    Initialize(&u->o, 0);
 
-   mprotect(u->c1, 4096, PROT_READ|PROT_WRITE);
-   mprotect(u->c2, 4096, PROT_READ|PROT_WRITE);
-   mprotect(u->c3, 4096, PROT_READ|PROT_WRITE);
+   mprotect(u->c1, CHUNK_SIZE, PROT_READ|PROT_WRITE);
+   mprotect(u->c2, CHUNK_SIZE, PROT_READ|PROT_WRITE);
+   mprotect(u->c3, CHUNK_SIZE, PROT_READ|PROT_WRITE);
 
    return 0;
 }

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]