public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "fredrik.hederstierna@securitas-direct.com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c/46483] New: Built-in memcpy() does not handle unaligned int for ARM
Date: Mon, 15 Nov 2010 14:53:00 -0000	[thread overview]
Message-ID: <bug-46483-4@http.gcc.gnu.org/bugzilla/> (raw)

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=46483

           Summary: Built-in memcpy() does not handle unaligned int for
                    ARM
           Product: gcc
           Version: 4.5.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: fredrik.hederstierna@securitas-direct.com


I had some problems with memcpy() when copying unaligned integers for ARM.

For intel target it worked fine, and I concluded that if I overloaded gcclib
weak memcpy-reference I had the same problem, but if I just overloaded memcpy
with a #define to my own function, it worked fine.

At first glance I though it was just a possible strict-aliasing violation, but
when having a closer look I'm not so sure, so I submit this to Bugzilla anyway
for validation.

I submit some lines of source-code that gives an example of the problem.

----------------------
#include <stdio.h>
----------------------
struct unaligned_int {
  char dummy1;
  char dummy2;
  char dummy3;
  unsigned int number;
} __attribute__((packed));
//---------------------
void *my_memcpy(void *dst, const void *src, size_t n)
{
  char *s = (char*)src;
  char *d = (char*)dst;
  while (n--) {
    *d++ = *s++;
  }
  return dst;
}
//---------------------
static void copy_x_into_struct(char *buf, unsigned int x)
{
  unsigned int i;
  struct unaligned_int* testp = (struct unaligned_int*)buf;

  memset(buf, 0xFF, sizeof(unsigned int));
  memcpy((void*)&(testp->number), &x, sizeof(unsigned int));

  printf("BUILT-IN MEMCPY TO %08x: ", &(testp->number));
  for (i = 0; i < sizeof(struct unaligned_int); i++) {
    printf(" %02x", buf[i]);
  }
  printf("\n");

  memset(buf, 0xFF, sizeof(unsigned int));
  my_memcpy((void*)&(testp->number), &x, sizeof(int));

  printf("USER-DEF MEMCPY TO %08x: ", &(testp->number));
  for (i = 0; i < sizeof(struct unaligned_int); i++) {
    printf(" %02x", buf[i]);
  }
  printf("\n");
}
//---------------------
int main(void)
{
  char buf[100];
  unsigned int x = 0x12345678;
  copy_x_into_struct(buf, x);
  return 0;
}

-------------------------

PROGRAM OUTPUT:

BUILT-IN MEMCPY TO 04016bd7:  78 56 34 12 00 00 00
USER-DEF MEMCPY TO 04016bd7:  ff ff ff 78 56 34 12

-------------------------

GCC-COMMAND-LINE:

arm-elf-gcc -g3 -ggdb3 -gdwarf-2 -mthumb -c -Wall -W -Wextra
-Wno-unused-parameter -mcpu=arm966e-s -Os -fno-omit-frame-pointer -fno-web
-mhard-float -mfpu=fpa -ffunction-sections -fdata-sections 
-o test.o test.c

-------------------------

TOOLCHAIN:  Attaching toolchain build-script.


             reply	other threads:[~2010-11-15 14:50 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-11-15 14:53 fredrik.hederstierna@securitas-direct.com [this message]
2010-11-15 15:03 ` [Bug c/46483] " fredrik.hederstierna@securitas-direct.com
2010-11-15 15:12 ` [Bug target/46483] " rguenth at gcc dot gnu.org
2010-11-15 21:06 ` mikpe at it dot uu.se
2010-11-15 21:30 ` mikpe at it dot uu.se
2010-11-15 21:33 ` mikpe at it dot uu.se
2010-11-15 21:41 ` pinskia at gcc dot gnu.org
2010-11-15 23:04 ` rguenth at gcc dot gnu.org
2010-11-16 10:36 ` mikpe at it dot uu.se
2010-11-16 12:32 ` mikpe at it dot uu.se
2010-11-16 13:18 ` mikpe at it dot uu.se
2010-11-16 13:44 ` mikpe at it dot uu.se
2010-11-16 19:28 ` mikpe at it dot uu.se
2010-12-03 10:02 ` ramana at gcc dot gnu.org
2011-12-10 23:04 ` [Bug middle-end/46483] [4.4/4.5 regression] built-in memcpy() does not handle unaligned int ebotcazou at gcc dot gnu.org
2012-06-20 12:42 ` [Bug middle-end/46483] [4.5 " rguenth at gcc dot gnu.org
2012-07-02 10:38 ` rguenth at gcc dot gnu.org

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-46483-4@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).