OpenCores
URL https://opencores.org/ocsvn/openrisc_me/openrisc_me/trunk

Subversion Repositories openrisc_me

[/] [openrisc/] [trunk/] [gnu-src/] [gcc-4.2.2/] [gcc/] [testsuite/] [gcc.c-torture/] [execute/] [builtins/] [strcpy-chk.c] - Diff between revs 149 and 154

Go to most recent revision | Only display areas with differences | Details | Blame | View Log

Rev 149 Rev 154
/* Copyright (C) 2004, 2005  Free Software Foundation.
/* Copyright (C) 2004, 2005  Free Software Foundation.
 
 
   Ensure builtin __strcpy_chk performs correctly.  */
   Ensure builtin __strcpy_chk performs correctly.  */
 
 
extern void abort (void);
extern void abort (void);
typedef __SIZE_TYPE__ size_t;
typedef __SIZE_TYPE__ size_t;
extern size_t strlen(const char *);
extern size_t strlen(const char *);
extern void *memcpy (void *, const void *, size_t);
extern void *memcpy (void *, const void *, size_t);
extern char *strcpy (char *, const char *);
extern char *strcpy (char *, const char *);
extern int memcmp (const void *, const void *, size_t);
extern int memcmp (const void *, const void *, size_t);
 
 
#include "chk.h"
#include "chk.h"
 
 
const char s1[] = "123";
const char s1[] = "123";
char p[32] = "";
char p[32] = "";
char *s2 = "defg";
char *s2 = "defg";
char *s3 = "FGH";
char *s3 = "FGH";
char *s4;
char *s4;
size_t l1 = 1;
size_t l1 = 1;
 
 
void
void
__attribute__((noinline))
__attribute__((noinline))
test1 (void)
test1 (void)
{
{
  chk_calls = 0;
  chk_calls = 0;
#ifndef __OPTIMIZE_SIZE__
#ifndef __OPTIMIZE_SIZE__
  strcpy_disallowed = 1;
  strcpy_disallowed = 1;
#else
#else
  strcpy_disallowed = 0;
  strcpy_disallowed = 0;
#endif
#endif
 
 
  if (strcpy (p, "abcde") != p || memcmp (p, "abcde", 6))
  if (strcpy (p, "abcde") != p || memcmp (p, "abcde", 6))
    abort ();
    abort ();
  if (strcpy (p + 16, "vwxyz" + 1) != p + 16 || memcmp (p + 16, "wxyz", 5))
  if (strcpy (p + 16, "vwxyz" + 1) != p + 16 || memcmp (p + 16, "wxyz", 5))
    abort ();
    abort ();
  if (strcpy (p + 1, "") != p + 1 || memcmp (p, "a\0cde", 6))
  if (strcpy (p + 1, "") != p + 1 || memcmp (p, "a\0cde", 6))
    abort ();
    abort ();
  if (strcpy (p + 3, "fghij") != p + 3 || memcmp (p, "a\0cfghij", 9))
  if (strcpy (p + 3, "fghij") != p + 3 || memcmp (p, "a\0cfghij", 9))
    abort ();
    abort ();
 
 
  /* Test at least one instance of the __builtin_ style.  We do this
  /* Test at least one instance of the __builtin_ style.  We do this
     to ensure that it works and that the prototype is correct.  */
     to ensure that it works and that the prototype is correct.  */
  if (__builtin_strcpy (p, "abcde") != p || memcmp (p, "abcde", 6))
  if (__builtin_strcpy (p, "abcde") != p || memcmp (p, "abcde", 6))
    abort ();
    abort ();
 
 
  strcpy_disallowed = 0;
  strcpy_disallowed = 0;
  if (chk_calls)
  if (chk_calls)
    abort ();
    abort ();
}
}
 
 
#ifndef MAX_OFFSET
#ifndef MAX_OFFSET
#define MAX_OFFSET (sizeof (long long))
#define MAX_OFFSET (sizeof (long long))
#endif
#endif
 
 
#ifndef MAX_COPY
#ifndef MAX_COPY
#define MAX_COPY (10 * sizeof (long long))
#define MAX_COPY (10 * sizeof (long long))
#endif
#endif
 
 
#ifndef MAX_EXTRA
#ifndef MAX_EXTRA
#define MAX_EXTRA (sizeof (long long))
#define MAX_EXTRA (sizeof (long long))
#endif
#endif
 
 
#define MAX_LENGTH (MAX_OFFSET + MAX_COPY + 1 + MAX_EXTRA)
#define MAX_LENGTH (MAX_OFFSET + MAX_COPY + 1 + MAX_EXTRA)
 
 
/* Use a sequence length that is not divisible by two, to make it more
/* Use a sequence length that is not divisible by two, to make it more
   likely to detect when words are mixed up.  */
   likely to detect when words are mixed up.  */
#define SEQUENCE_LENGTH 31
#define SEQUENCE_LENGTH 31
 
 
static union {
static union {
  char buf[MAX_LENGTH];
  char buf[MAX_LENGTH];
  long long align_int;
  long long align_int;
  long double align_fp;
  long double align_fp;
} u1, u2;
} u1, u2;
 
 
void
void
__attribute__((noinline))
__attribute__((noinline))
test2 (void)
test2 (void)
{
{
  int off1, off2, len, i;
  int off1, off2, len, i;
  char *p, *q, c;
  char *p, *q, c;
 
 
  for (off1 = 0; off1 < MAX_OFFSET; off1++)
  for (off1 = 0; off1 < MAX_OFFSET; off1++)
    for (off2 = 0; off2 < MAX_OFFSET; off2++)
    for (off2 = 0; off2 < MAX_OFFSET; off2++)
      for (len = 1; len < MAX_COPY; len++)
      for (len = 1; len < MAX_COPY; len++)
        {
        {
          for (i = 0, c = 'A'; i < MAX_LENGTH; i++, c++)
          for (i = 0, c = 'A'; i < MAX_LENGTH; i++, c++)
            {
            {
              u1.buf[i] = 'a';
              u1.buf[i] = 'a';
              if (c >= 'A' + SEQUENCE_LENGTH)
              if (c >= 'A' + SEQUENCE_LENGTH)
                c = 'A';
                c = 'A';
              u2.buf[i] = c;
              u2.buf[i] = c;
            }
            }
          u2.buf[off2 + len] = '\0';
          u2.buf[off2 + len] = '\0';
 
 
          p = strcpy (u1.buf + off1, u2.buf + off2);
          p = strcpy (u1.buf + off1, u2.buf + off2);
          if (p != u1.buf + off1)
          if (p != u1.buf + off1)
            abort ();
            abort ();
 
 
          q = u1.buf;
          q = u1.buf;
          for (i = 0; i < off1; i++, q++)
          for (i = 0; i < off1; i++, q++)
            if (*q != 'a')
            if (*q != 'a')
              abort ();
              abort ();
 
 
          for (i = 0, c = 'A' + off2; i < len; i++, q++, c++)
          for (i = 0, c = 'A' + off2; i < len; i++, q++, c++)
            {
            {
              if (c >= 'A' + SEQUENCE_LENGTH)
              if (c >= 'A' + SEQUENCE_LENGTH)
                c = 'A';
                c = 'A';
              if (*q != c)
              if (*q != c)
                abort ();
                abort ();
            }
            }
 
 
          if (*q++ != '\0')
          if (*q++ != '\0')
            abort ();
            abort ();
          for (i = 0; i < MAX_EXTRA; i++, q++)
          for (i = 0; i < MAX_EXTRA; i++, q++)
            if (*q != 'a')
            if (*q != 'a')
              abort ();
              abort ();
        }
        }
}
}
 
 
/* Test whether compile time checking is done where it should
/* Test whether compile time checking is done where it should
   and so is runtime object size checking.  */
   and so is runtime object size checking.  */
void
void
__attribute__((noinline))
__attribute__((noinline))
test3 (void)
test3 (void)
{
{
  struct A { char buf1[10]; char buf2[10]; } a;
  struct A { char buf1[10]; char buf2[10]; } a;
  char *r = l1 == 1 ? &a.buf1[5] : &a.buf2[4];
  char *r = l1 == 1 ? &a.buf1[5] : &a.buf2[4];
  char buf3[20];
  char buf3[20];
  int i;
  int i;
  const char *l;
  const char *l;
 
 
  /* The following calls should do runtime checking
  /* The following calls should do runtime checking
     - source length is not known, but destination is.  */
     - source length is not known, but destination is.  */
  chk_calls = 0;
  chk_calls = 0;
  strcpy (a.buf1 + 2, s3 + 3);
  strcpy (a.buf1 + 2, s3 + 3);
  strcpy (r, s3 + 2);
  strcpy (r, s3 + 2);
  r = l1 == 1 ? __builtin_alloca (4) : &a.buf2[7];
  r = l1 == 1 ? __builtin_alloca (4) : &a.buf2[7];
  strcpy (r, s2 + 2);
  strcpy (r, s2 + 2);
  strcpy (r + 2, s3 + 3);
  strcpy (r + 2, s3 + 3);
  r = buf3;
  r = buf3;
  for (i = 0; i < 4; ++i)
  for (i = 0; i < 4; ++i)
    {
    {
      if (i == l1 - 1)
      if (i == l1 - 1)
        r = &a.buf1[1];
        r = &a.buf1[1];
      else if (i == l1)
      else if (i == l1)
        r = &a.buf2[7];
        r = &a.buf2[7];
      else if (i == l1 + 1)
      else if (i == l1 + 1)
        r = &buf3[5];
        r = &buf3[5];
      else if (i == l1 + 2)
      else if (i == l1 + 2)
        r = &a.buf1[9];
        r = &a.buf1[9];
    }
    }
  strcpy (r, s2 + 4);
  strcpy (r, s2 + 4);
  if (chk_calls != 5)
  if (chk_calls != 5)
    abort ();
    abort ();
 
 
  /* Following have known destination and known source length,
  /* Following have known destination and known source length,
     so if optimizing certainly shouldn't result in the checking
     so if optimizing certainly shouldn't result in the checking
     variants.  */
     variants.  */
  chk_calls = 0;
  chk_calls = 0;
  strcpy (a.buf1 + 2, "");
  strcpy (a.buf1 + 2, "");
  strcpy (r, "a");
  strcpy (r, "a");
  r = l1 == 1 ? __builtin_alloca (4) : &a.buf2[7];
  r = l1 == 1 ? __builtin_alloca (4) : &a.buf2[7];
  strcpy (r, s1 + 1);
  strcpy (r, s1 + 1);
  r = buf3;
  r = buf3;
  l = "abc";
  l = "abc";
  for (i = 0; i < 4; ++i)
  for (i = 0; i < 4; ++i)
    {
    {
      if (i == l1 - 1)
      if (i == l1 - 1)
        r = &a.buf1[1], l = "e";
        r = &a.buf1[1], l = "e";
      else if (i == l1)
      else if (i == l1)
        r = &a.buf2[7], l = "gh";
        r = &a.buf2[7], l = "gh";
      else if (i == l1 + 1)
      else if (i == l1 + 1)
        r = &buf3[5], l = "jkl";
        r = &buf3[5], l = "jkl";
      else if (i == l1 + 2)
      else if (i == l1 + 2)
        r = &a.buf1[9], l = "";
        r = &a.buf1[9], l = "";
    }
    }
  strcpy (r, "");
  strcpy (r, "");
  /* Here, strlen (l) + 1 is known to be at most 4 and
  /* Here, strlen (l) + 1 is known to be at most 4 and
     __builtin_object_size (&buf3[16], 0) is 4, so this doesn't need
     __builtin_object_size (&buf3[16], 0) is 4, so this doesn't need
     runtime checking.  */
     runtime checking.  */
  strcpy (&buf3[16], l);
  strcpy (&buf3[16], l);
  /* Unknown destination and source, no checking.  */
  /* Unknown destination and source, no checking.  */
  strcpy (s4, s3);
  strcpy (s4, s3);
  if (chk_calls)
  if (chk_calls)
    abort ();
    abort ();
  chk_calls = 0;
  chk_calls = 0;
}
}
 
 
/* Test whether runtime and/or compile time checking catches
/* Test whether runtime and/or compile time checking catches
   buffer overflows.  */
   buffer overflows.  */
void
void
__attribute__((noinline))
__attribute__((noinline))
test4 (void)
test4 (void)
{
{
  struct A { char buf1[10]; char buf2[10]; } a;
  struct A { char buf1[10]; char buf2[10]; } a;
  char buf3[20];
  char buf3[20];
 
 
  chk_fail_allowed = 1;
  chk_fail_allowed = 1;
  /* Runtime checks.  */
  /* Runtime checks.  */
  if (__builtin_setjmp (chk_fail_buf) == 0)
  if (__builtin_setjmp (chk_fail_buf) == 0)
    {
    {
      strcpy (&a.buf2[9], s2 + 3);
      strcpy (&a.buf2[9], s2 + 3);
      abort ();
      abort ();
    }
    }
  if (__builtin_setjmp (chk_fail_buf) == 0)
  if (__builtin_setjmp (chk_fail_buf) == 0)
    {
    {
      strcpy (&a.buf2[7], s3 + strlen (s3) - 3);
      strcpy (&a.buf2[7], s3 + strlen (s3) - 3);
      abort ();
      abort ();
    }
    }
  /* This should be detectable at compile time already.  */
  /* This should be detectable at compile time already.  */
  if (__builtin_setjmp (chk_fail_buf) == 0)
  if (__builtin_setjmp (chk_fail_buf) == 0)
    {
    {
      strcpy (&buf3[19], "a");
      strcpy (&buf3[19], "a");
      abort ();
      abort ();
    }
    }
  chk_fail_allowed = 0;
  chk_fail_allowed = 0;
}
}
 
 
void
void
main_test (void)
main_test (void)
{
{
#ifndef __OPTIMIZE__
#ifndef __OPTIMIZE__
  /* Object size checking is only intended for -O[s123].  */
  /* Object size checking is only intended for -O[s123].  */
  return;
  return;
#endif
#endif
  __asm ("" : "=r" (s2) : "0" (s2));
  __asm ("" : "=r" (s2) : "0" (s2));
  __asm ("" : "=r" (s3) : "0" (s3));
  __asm ("" : "=r" (s3) : "0" (s3));
  __asm ("" : "=r" (l1) : "0" (l1));
  __asm ("" : "=r" (l1) : "0" (l1));
  test1 ();
  test1 ();
  test2 ();
  test2 ();
  s4 = p;
  s4 = p;
  test3 ();
  test3 ();
  test4 ();
  test4 ();
}
}
 
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.