2010-02-09 17:44:31     dnsmasq2 crashes in memcpy due to misaligned address violation

Document created by Aaronwu Employee on Aug 20, 2013
Version 1Show Document
  • View in full screen mode

2010-02-09 17:44:31     dnsmasq2 crashes in memcpy due to misaligned address violation

Doug Bailey (UNITED STATES)

Message: 85861   

 

I am having a problem with dnsmasq2 from the 2009R1.1-RC4 source tag and being

built with bfin-linux-uclibc-gcc 4.3.3

 

At line 610 of cache.c I am encountering a call to memcpy that causes a

misaligned address error.  Any time the address of addr (i.e. the source

address) is not long word aligned, dnsmasq2 is throwing the error.  I never see

the memcpy destination be anything but long word aligned.

 

An example of a call that fails is:

&new->addr.addr=0x21c0018, addr=0xeed5f6, addrlen=4

 

Why memcpy in this situation would be throwing these errors but no other

application is having the problem is beyond me. (I simulated the same operation

in a stand alone app and had no problems.)

 

cache.c is built as follows:

bfin-linux-uclibc-gcc -Wall -c -pipe -Wall -g -O2      -mcpu=bf537-0.2 -DNO_GETTEXT -DHAVE_ISC_READER -DDO_PRELOAD -g -O2 -DNO_IPV6 cache.c

 

The link of dnsmasq is:

bfin-linux-uclibc-gcc     -mcpu=bf537-0.2 -o dnsmasq  cache.o rfc1035.o util.o option.o forward.o network.o dnsmasq.o netlink.o isc.o dhcp.o lease.o helper.o rfc2131.o preload.o

 

Thanks

 

Doug

QuoteReplyEditDelete

 

 

2010-02-09 17:52:05     Re: dnsmasq2 crashes in memcpy due to misaligned address violation

Mike Frysinger (UNITED STATES)

Message: 85862   

 

you can try changing the call from memcpy to memmove to see if it makes a difference

QuoteReplyEditDelete

 

 

2010-02-09 18:10:46     Re: dnsmasq2 crashes in memcpy due to misaligned address violation

Doug Bailey (UNITED STATES)

Message: 85863   

 

Same result.

 

(gdb) p &new->addr.addr

$1 = (struct all_addr *) 0x21c4018

(gdb) p addr

$2 = (struct all_addr *) 0xeeb5f6

 

 

If I make my own simple memcpy function and replace the one instance in cache.c, all works fine.

QuoteReplyEditDelete

 

 

2010-02-10 03:15:56     Re: dnsmasq2 crashes in memcpy due to misaligned address violation

Wolfgang Muees (GERMANY)

Message: 85885   

 

Doug,

 

trxy to *locate* the memcpy function which is throwing the exception.

 

I suspect that somewhere in the include files or in the libs a special, unique memcpy function/macro is used.

 

Maybe you have to enable a HAVE_MEMCPY in a config.h file?

 

regards

 

Wolfgang

TranslateQuoteReplyEditDelete

 

 

2010-02-10 10:52:28     Re: dnsmasq2 crashes in memcpy due to misaligned address violation

Doug Bailey (UNITED STATES)

Message: 85917   

 

It is a MACRO implementation of the memcpy call.   Here is the code that is being substituted for the call:

 

0x020b3b28 <cache_insert+388>:    P2 = R0;

 

0x020b3b2a <cache_insert+390>:    R0 = [P2];     <--- Offending instruction

 

0x020b3b2c <cache_insert+392>:    [P5 + 0x14] = R0;

 

0x020b3b2e <cache_insert+394>:    P2 = [P4 + 0x98];

 

Unfortunately, I have not easily found the macro that is causing this code replacement.  I am not aware of anything in config.h that is triggering the replacement.

 

Doug

QuoteReplyEditDelete

 

 

2010-02-10 11:58:02     Re: dnsmasq2 crashes in memcpy due to misaligned address violation

Doug Bailey (UNITED STATES)

Message: 85921   

 

Removing the -O2 from the build causes memcpy to be called.  I'm not sure what causes that function to be inlined with the O2 optimizations.

QuoteReplyEditDelete

 

 

2010-02-10 15:03:01     Re: dnsmasq2 crashes in memcpy due to misaligned address violation

Robin Getz (UNITED STATES)

Message: 85925   

 

Doug:

 

If the compiler believes that memcpy is aligned, it will inline things.

 

Mike may be able to remember what causes this. I know we have had issues with this in the past, but it was normally from not declaring a struct as packed or something...

 

-Robin

QuoteReplyEditDelete

 

 

2010-02-10 16:55:17     Re: dnsmasq2 crashes in memcpy due to misaligned address violation

Mike Frysinger (UNITED STATES)

Message: 85929   

 

the previous method was to use memmove() because gcc wouldnt optimize that, but i guess that hack doesnt work

 

Bernd's opinion was that the code is broken if it has a pointer declared with certain types (like u32*) but wasnt actually aligned to a u32.  so if you have a function that may be operating with arbitrary aligned data, you have to declare it void* and properly align it before you go assigning the pointer to anything with higher alignment.

QuoteReplyEditDelete

 

 

2010-02-11 10:20:27     Re: dnsmasq2 crashes in memcpy due to misaligned address violation

Doug Bailey (UNITED STATES)

Message: 85986   

 

Here is the problem isolated to a small code fragment.  It runs with no optizations and dies with O2.

 

The problem is as Mike states.

 

#include <stdint.h>

#include <stdio.h>

#include <string.h>

 

struct in_addr {

    uint32_t    s_addr;

};

 

struct all_addr {

  union {

    struct in_addr addr4;

  } addr;

};

 

int main(int argc, char ** argv)

{

    struct all_addr dst;

    struct all_addr *pts;

    int count;

    uint8_t buf[16];

    uint8_t * src;

  

    printf ("Memcpy test\n");

    src = buf;

    for (count =0; count < 4; count++, src++) {

        pts = (struct all_addr *)src;

        printf("Copying from %p to %p\n", pts, &dst);

        sleep(1);

        memcpy(&dst, pts, 4);

    }

    return 0;

}

QuoteReplyEditDelete

 

 

2010-02-11 10:31:46     Re: dnsmasq2 crashes in memcpy due to misaligned address violation

Robin Getz (UNITED STATES)

Message: 85988   

 

Doug:

 

And the fix is as Mike stated - fix the source. if you force src to be alighed - it should be fine.

 

-Robin

QuoteReplyEditDelete

 

 

2010-02-11 11:59:45     Re: dnsmasq2 crashes in memcpy due to misaligned address violation

Doug Bailey (UNITED STATES)

Message: 85990   

 

Or use dnsmasq 2.50 in trunk which has fixed this issue already.  (and which I should have tried much earlier instead of going down this rabbit hole.) 

Attachments

    Outcomes