arm-non-eabi-gcc v10.3.1 generate wrong code with O2 and higher
Affects | Status | Importance | Assigned to | Milestone | |
---|---|---|---|---|---|
GNU Arm Embedded Toolchain |
New
|
Undecided
|
Unassigned |
Bug Description
- release version
arm-none-eabi-gcc (GNU Arm Embedded Toolchain 10.3-2021.10) 10.3.1 20210824 (release)
- whether the toolchain was rebuilt or you are using or binary package
package downloaded with STM32CubeProgrammer
Size: 2126664 byte (2076 KiB)
SHA256: 76aef3a90269c5d
- host machine
Intel i7, windows 10
- preprocessed testcase for us to reproduce
#include <stddef.h>
#include <stdint.h>
void * memset (void * _dst, int v, size_t size)
{
uint8_t* dst = _dst;
uint8_t* end = dst+size;
while(dst<end)
*dst++ = v;
return _dst;
}
compile with:
arm-none-eabi-gcc "test.c" -c -O2 -mthumb -o "test.elf"
arm-none-
output:
test.elf: file format elf32-littlearm
Disassembly of section .text:
00000000 <memset>:
0: 1883 adds r3, r0, r2
2: b510 push {r4, lr}
4: 0004 movs r4, r0
6: 4298 cmp r0, r3
8: d203 bcs.n 12 <memset+0x12>
a: 23ff movs r3, #255 ; 0xff
c: 4019 ands r1, r3
e: f7ff fffe bl 0 <memset>
12: 0020 movs r0, r4
14: bc10 pop {r4}
16: bc02 pop {r1}
18: 4708 bx r1
1a: 46c0 nop ; (mov r8, r8)
- symptom(s)
the binary code is broken, generating a stackoverflow... yeah, not the website, a crash
- error/warning message encountered
compiler is silent, on runtime you see smoke coming out of the cpu
Initially I thought that `dst+size` (adding an integer to a pointer to a void) might be undefined behaviour, but I did a test and even after rewriting it as `uint8_t *end = dst; end += size; `, the result is the same, so it might be a bug.