memcpy fails at len 3,7,11...
today I switched from gnu tools "4.8 2013q4" to "5.2 2015q4". I do not compile it self I use the current download.
What happens my application is no more able to run. I found out the app is crashing at a point where I use memcpy of the len=3.
I make some test an are the opinion all len (n*4-1) will be crash in this version.
What could be the reason? Can I report this to bugs?
Question information
- Language:
- English Edit question
- Status:
- Answered
- Assignee:
- No assignee Edit question
- Last query:
- Last reply:
Revision history for this message
![]() |
#1 |
Hi Erik,
We will need more information before we can investigate this. Can you please provide a reduced testcase which shows this behavior and your compilation command line with "-v" with the verbose output produced by it?
Cheers,
Andre
Revision history for this message
![]() |
#2 |
Hello Andre,
here a simple test which sould pass without problems.
int x;
char fieldA[
char fieldB[8]={0};
for(x=0;x<8;x++)
{
memcpy(fieldB, fieldA, x);
}
But it produce an abort if x=3 and 7.
Here the output with the "-v" option.
Building target: WP3_universal.elf
Invoking: Cross ARM C Linker
arm-none-eabi-gcc -mcpu=cortex-a5 -march=armv7-a -mthumb -mfloat-abi=hard -mfpu=vfpv4-d16 -mno-unaligned-
Using built-in specs.
COLLECT_
COLLECT_
Target: arm-none-eabi
Configured with: /home/build/
Thread model: single
gcc version 5.2.1 20151202 (release) [ARM/embedded-
COMPILER_
LIBRARY_
COLLECT_
c:/program files (x86)/gnu tools arm embedded/5.2 2015q4/
Finished building target: WP3_universal.elf
Best Erik
Revision history for this message
![]() |
#3 |
Hi Erik,
I'm not able to reproduce the faulty behavior using the sbrk implementation in librdimon.a (using --specs=
Hope this helps.
Cheers,
Andre
Revision history for this message
![]() |
#4 |
Hi, Erik,
It is unlikely to be something wrong with memcpy, which will fall back to byte-to-byte copy is any of input addresses is unaligned. What else you can do to help us reproducing the crash?
Thanks,
Joey
Revision history for this message
![]() |
#5 |
Hello Joey,
the problem seems to be if the input address aligned but the len =3.
memcpy is trying to made a byte transfer and then a halfword transfer, and this halfword transfer
is a self made unaligned access which fails. This is what I can see if I debug the crash.
Best Erik
Revision history for this message
![]() |
#7 |
Hi Erik,
Sorry about my message earlier about sbrk... for some reason my head was stuck on malloc. Memcpy obviously doesnt need sbrk for anything.
Now you mention that you see memcpy use a halfword transfer, which is weird. Looking at the implementation of newlib's memcpy we see that for small memcpys a byte-by-byte copy is used as Joey points out.
Can you compile with '-Wl,-Map=
Cheers,
Andre
Revision history for this message
![]() |
#8 |
Hi, Erik,
After a deeper diving into memcpy, there are two problems. One is in our side we picked the unexpected version of memcpy implementation which has optimizations to copy misaligned address. We will work on it for a fix. It explains why 4.8 works while 5.2 doesn't.
But in the other hand, libraries of this toolchain was not built to run on CPU that doesn't support mis-aligned memory access. They were built with assumption that mis-aligned memory address is supported. Memcpy in 4.8 for armv7-ar happen to work, but it is likely that other library function will break at mis-aligned access. The solution is to rebuild the libraries with flag -mno-unaligned-
Please confirm if the CPU you are running does not support mis-aligned memory access.
Thanks,
Joey
Revision history for this message
![]() |
#9 |
Hi all,
ok understand.
Here the copy of the map file:
c:/program files (x86)/gnu tools arm embedded/5.2 2015q4/
./source/
.data 0x00000000 0x0 c:/program files (x86)/gnu tools arm embedded/5.2 2015q4/
.bss 0x00000000 0x0 c:/program files (x86)/gnu tools arm embedded/5.2 2015q4/
.text 0x0030a23c 0xec c:/program files (x86)/gnu tools arm embedded/5.2 2015q4/
.ARM.attributes
Maybe you mean this lib.
In the meantime I have found out and use the nano part of the lib which has not the problem.
The CPU I have supports mis-aligned access not in the normal mode. There is a bit in CP15 which bings back this possibility.
But is is not recommend to use it. Because it has some additionally CPU cyles required for this. In the end the memcpy was the only problem. Because of this I would not switch the mode.
Best Erik
Can you help with this problem?
Provide an answer of your own, or ask Erik Schieweck for more information if necessary.