Email or username:

Password:

Forgot your password?
nil :demisexual_flag: (U) [!].7z

oh my fucking god. so i was having a problem when i enabled optimizations when compiling the doom port. memcpy ended up overwriting itself. so i looked into what was happening, and apparently memcpy just kept calling itself over and over. the reason? i was compiling my own version of memcpy, because i wasn't using the standard library, but i also didn't use the "-ffreestanding" flag, so gcc assumed i *did* have the standard library. so gcc, in its infinite wisdom, saw a memcpy-like pattern in my memcpy and turned it into another call to memcpy, resulting in a stack overflow.

35 comments | Expand all CWs
J. "Henry" Waugh

@nil *nods dryly* so what's the problem here?

J. "Henry" Waugh

@nil (definitely a wtf moment, jokes aside. But I expect nothing less from GCC at this point)

Andrew

@jhwgh1968 @nil you want to call memcpy so I called memcpy. so what if that memcpy also calls memcpy? not my problem

not good osdev 🏳️‍⚧️

@nil i dont know much about riscv but even assuming that was correct surely such an optimization couldve just been like. a `jmp memcpy`

Cirnado Funkson :fists:
@mothcompute @nil wouldnt you still need to be able to know where to return? a simple branch couldn't cut it
Teknikal_Domain

@nil copy this memory to copy this memory to copy this memory to copy this memory to copy this memory to copy this memory to copy this memory to copy this memory to copy this memory to copy this memory to copy this memory to copy this memory to copy this memory to copy this memory to copy this memory to copy this memory to copy this memory to copy this memory to copy this memory to copy this memory to copy this memory to copy this memory to copy this memory to copy this memory to copy this memory to copy this memory to copy this memory to copy this memory to copy this memory to copy this memory to copy this memory to copy this memory to copy this memory to copy this memory to copy this memory to copy this memory to copy this memory to copy this memory to copy this memory to copy this memory to copy this memory to copy this memory to copy this memory to copy this memory to copy this memory to copy this memory to copy this memory to copy this memory...

@nil copy this memory to copy this memory to copy this memory to copy this memory to copy this memory to copy this memory to copy this memory to copy this memory to copy this memory to copy this memory to copy this memory to copy this memory to copy this memory to copy this memory to copy this memory to copy this memory to copy this memory to copy this memory to copy this memory to copy this memory to copy this memory to copy this memory to copy this memory to copy this memory to copy this memory...

✧✦✶✷Catherine✷✶✦✧

@ireneista @nil clang also does this! it's a classic firmware dev issue

you cannot implement the C standard library in (standard, non-freestanding) C by the explicit design of C

Eniko | Kitsune Tails out now!

@whitequark @ireneista @nil I'm honestly kind of confused why nostdlib and freestanding are separate switches

Nikita Lisitsa

@eniko @whitequark @ireneista @nil Afaik freestanding is an option for the compiler itself while nostdlib is effectively an option for the linker (or rather to control the linker inputs), maybe that's the reason they're separate

Emil J

@eniko @whitequark @ireneista @nil I think freestanding is "there is no memcpy" and nostdlib is "I brought my own memcpy" so you'd use one for your memcpy and friends and the other for the rest of your very custom system?

Brad Martin

@nil yo dawg, I put some memcpy in your memcpy

/usr/local/etc/rc.d/sudo200

@nil

*Cave Johnson voice* That's 100% more memcpy, per memcpy!

vxo

@nil wow. Wow. Thanks but no thanks XD

Benjamin Fry

@nil how long did it take you to debug this? It looks insane.

Legion495

@nil@furry.engineer
Fascinating.

Just a few days ago I saw some issue on wazuhs Github that memcpy caused some issue. They moved to memmove. Issue gone.

Sadly I am fuzzy on the details. Just noticed that this was the change.

Bersl

@nil In all fairness, assuming that a program wants to use the standard library is usually correct.

Drew Naylor

@nil I'm sorry, what?

uis

Wierd it didn't replace it with __builtin_memcpy instead.

Ε-ριςς
@uis memcpy and __builtin_memcpy are the same (unless -ffreestanding or -fno-builtins or maybe some other flag disables this behaviour)

@nil
Ariane

@nil In case it's helpful: I think the `-fno-builtin` or `-fno-builtin-memcpy` flags are better, since they won't disable all the other things that `-ffreestanding` disables.

<3

Mx Autumn :blobcatpumpkin:

@nil I was just discussing this with my dad the other day in explaining how sometimes the compiler is too intelligent and unless you know how to debug it and the resulting compiled code you’re stuck with a mystery you can’t solve; making some ASM experience mandatory for debug purposes.

Wilfried Klaebe

@carbontwelve It's not "too intelligent", it's "not intelligent enough". If it was intelligent enough, it would've seen it's seeing something memcpy-like because it's compiling a memcpy.

@nil

Marcus

@nil i dont have access to a computer with gcc atm, so I cannot check: does this happen at O1?

i'm also wondering how gcc detects this "pattern". if it's based on the function name, then that seems outright passive-aggressive. if it's based on the code structure, it seems really brittle and I would not expect that at low optimization levels.

Siguza

@nil yeah I think everyone who uses a C compiler runs into this eventually.

Next up is cross-compiling to a different arch that works just fine until you upgrade your toolchain and then you learn that the compiler is now emitting calls to libcompiler_rt.a, which you don't have because your target isn't your native arch, but so far it's been working fine because the previous version of the toolchain just happened to never emit any such calls for your codebase.

Resuna

@nil

> gcc, in its infinite wisdom, saw a memcpy-like pattern in my memcpy and turned it into another call to memcpy,

Insert Jurassic Park quotes here.

Ben Hutchings

@nil You can also use -fno-builtin-memcpy to disable this optimisation (and similar options for other standard library functions)

Go Up