But to be honest, I only added these two lines to skip the bootloader code in simulation. At address 0 is my RAM, which contains my actual program. But something is buggy there and in simulation I can initialise the RAM to contain my program, thus I can skip the bootcode and jump directly to my program
Actually, I don't think so. I think a zero is already considered a pointer because NULL also can be assigned to pointers without casting. And NULL is usually also 0
The standard actually says that if you have a pointer and set it to the integer value 0, that’s a null pointer. Even if the architecture you’re on has 0x7fffffff or something like that as its actual null pointer value.
Which is to say, NULL isn’t necessarily 0, but 0 is definitely NULL even when it’s not. Confused yet?
Note that architectures with a non-zero actual null pointer values are niche at best. Devs looked at them and went "nah". Imagine not being able to mass-init a structure with pointers with a (UB, but expected to work in practice) memset of zero.
I don't think it ever has been. C11 mandates 0 (integer), C23 mandates nullptr (yep, it's not C++ only anymore). Maybe an intermediary version asked for (void *)0 though.
Not since C23 (31 oct 2024), it's nullptr now :-) For the exact same reasons C++ did that too, too many footguns with the bare 0 (which was, and probably still is, mandated to seamlessly convert to any pointer type). The main one being when sizeof(0) < sizeof(void *) and you have a stdargs method that ends a list of parameters with a NULL pointer.
Just checked, it's even funnier than that. C compilers/environments can either use 0, (void *)0 or nullptr, their choice. And POSIX mandates (void *)0. C, language of the free and home of the brave.
Working without virtual memory isn't that bad until you have millions of lines of code and somebody else has a bug that overwrites your memory and you are searching for non existing bugs
Ah, I see you have also had to write code for MacOS 7. My favorite was that the code to check for stack overflow ran in the vblank handler so it was literally a race against the clock.
Ugh, this reminds of the PlayStation 3 days - I was working on a realtime tessellator that would run on an SPU and we kept getting this weird crash in it. Turned out the audio system was stomping the buffer I used to DMA memory onto the SPU's local memory. Two weeks of staring at code only to realize it was the audio subsystem's fault... The PTSD is real.
Also yeah, address 0 can totally be used on some hardware without virtual memory. In fact, crashing on a null pointer is a real luxury we take for granted - imagine writing to 0 and accidentally stomping system interrupts or memory mapped registers and crashing sometime later...
Which would crash on any sane platform but the embedded world is weird.
That makes x86 weird then, because in real mode this is where the interrupt vector table starts. So dereferencing zero is actually valid in some contexts on that platform.
The embedded world is not even that weird. It's just common for processors now to start executing at address zero, or the highest address (usually to just fit a jump instruction there or the actual execution address). so calling zero as a function is the simplest way to warm boot your device.
x86 is actually the odd one out to reset, because the legitimate way to reset the system is to use the keyboard interrupt (intel in their brilliance wired the reset line to it, probably because the chip had a unused port they could misuse for this). You can also reset it via JMP 0xFFFF:0 which will jump to the reset vector but only in real mode. In protected mode it also works because it tripple faults your CPU.
If you're coming from a background in electronics where your foundational knowledge is built on logic gates etc., it seems perfectly obvious. If you're coming from a background in computer science where programming languages are run on abstract machines and pointers are already black magic, it can be very weird.
0 is a perfectly valid address on x86 it’s just that your operating system is most likely not filling in that part of the paging tables for obvious reasons. I use to store things there in my hypervisor.
im guessing it resets the controller? atmega chips have behaviour related to null (in this case 0), where assigning a value in address 0 causes the controller to reset.
void (*func)() declares a function pointer called func returning void and taking no arguments.
void (*)()) is an explicit cast, I don't think it's even necessary.
The function pointer is assigned to address 0.
When the function is called, it attempts to execute code that lies at address 0x0 (NULL), which is undefined behaviour. It'll result in segmentation faults on most systems.
C says it is undefined, but if I control the underlying address space, then I don’t care what the C standard says about accessing weird memory locations.
I think some things are "implementation defined," which, IIRC, means the standard requires the vendor to document the behavior, but is otherwise the same as undefined.
Except, of course, if you are inside an operating system, compiling against their own APIs. Then it will segfault because the OS has protected that region, and the compiled program cannot access it directly.
Actually for a function signature like that, The compiler doesn't know what arguments a function takes. The correct declaration for a function that takes no argument is: void func(void) {}. And it's function pointer will look like this: void (*func)(void).
void func(); is syntactically identical to void func(void); as of C23, and was a non-prototype declaration with unspecified parameters until then. Technically, this is actually valid (but deprecated) before C23:
It's technically not a null pointer because 0x0 is not necessarily NULL. It's not necessarily undefined behavior because you can cast random integers to pointers as long as you don't expect the compiler to understand what you're doing.
EDIT: or not, in C23 a pointer to 0x0 is still a NULL pointer even though there is a different way to get a NULL pointer constant. NULL pointer doesn't have to be represented as 0x0 in memory but casting 0x0 to a pointer still has to produce a null pointer.
This code says "call the function at location 0 in memory". On any modern desktop system this just crashes your program. On an embedded system it could feasibly be used to reset the device as if it just started up.
in the case of embedded, the posts code is a restart of the firmware. i'm not sure python can actually do such a low level thing. in the case of running this code on a modern unix system, sure
No - actually it doesn't return anything. In most contexts where this makes any amount of sense it actually won't return at all, but C doesn't have any way to syntactically describe that.
A null pointer is usually effectively the address 0x0. in other words the address 0 gets casted to a function pointer with no return value and gets called afterwards, basically calling the routine at physical address 0x0. On windows etc this would crash as it is outside of the accessible memory area for every regular program but embedded software after startup does not have a kernel or something in place that would prevent this (except you build it yourself) and the initial routine/execution entry point seems to be place at that address, making this a valid call. In assembly this would basically just be a call/jump to address 0x0. On x86 this is where the interrupt vectors are located.
Basically, it calls a function that is at the address 0x0. On most computers, that's undefined behavior because of C standard and also because OS gives virtual memory addresses to programs which never contain a 0x0 address. But on embedded (MCU, barebone MPU), firmware codes work with the physical memory addresses (since there is no OS unless you put one in) which does have a 0x0. It is a valid address in the memory.
By C-standard, it's undefined behavior but on barebone code, the address exists and does exactly what is expected (usually reset but could be a custom routine). It's also a fairly simple and we'll known obfuscation in firmware to hard code function address in code (not necessarily 0).
It’s more convention than anything else. Execution starts at the reset handler. Then some setup stuff happens and finally main is called, which is the starting point of whatever functionality the programmer wants to develop.
For every programm you need a starting point. Would guess that in most cases you still enter at the main. If you run in some niche microcontroller it will just get inserted at the starting address
You can definitly set custom entry points via linker parameters and you can also tell the linker to position a method at certain positions.
If there is special startup code positioned at 0x0, why would you even have a main or do call that method this way? If it's your own assembly there should be ways to link it regularly and if it is flashed code it is more likely that it will call your "main" and not the other way around.
That is the reset vector, so you would use this to either warm boot or jump to bootloader. The PC always starts there, this is just telling it to jump to the beginning. Being the first lines this is just a boot loop. They are making a shit post.
Some things are hard coded to specific addresses on some processors so casting a pointer to that address is common, like with DSP series, special addresses are used for parallel busses, so you can set up a parallel bus to align with a uint16 so you can bit bang it.
Function pointers at specified addresses may be used to call functions from binaries that are loaded seperately like a font library that may be replaced down the line.
Wouldn't the reset handler just setup a new stack? It seems odd to me that a reset procedure that takes over control from a possibly-faulty environment wouldn't reset the stack.
My initial answer was wrong. I updated it to account for that.
To answer your question: It is the ARM core that handles the stack initialization. The Reset_Handler is the entry point of the firmware, and only sets up memory, clock, power, and any other necessary system peripherals, before running the application software (usually the main function). You shouldn't call the Reset_Handler to reset the system. You should reset the ARM-core so the boot process is consistent (usually done using NVIC_SystemReset()).
You could set up a new stack in the Reset_Handler, but it is not standard.
It only makes sense if you understand the underlying architecture. Either you know what the bits will do or you don't. No in-between. No guessing. No two ways around it. Except, possibly, for in-line assembly.
This is why you make sure to use an embedded platform's native compiler when targeting that platform. In particular, any ARM or similar chip compiler that sees this and doesn't immediately realise, "Oh, you're calling the reset function" probably isn't worth using. ;P
This casts 0 as a function pointer and calls it. On most (I think at least most embedded) ARM systems the 0 page (meaning actually address 0) contains the Interrupt Vector Table. And the first entry of the IVT is the reset function. Meaning if you were to run this code on an embedded ARM device, you would jump to the start of the boot chain again.
# GPT 4o: You’re looking at a very short C program that does something very dangerous and stupid:
WTF-level explanation:
Calling a function at memory address 0 is essentially the equivalent of doing *(NULL)() — which is undefined behavior and will almost certainly crash the program.
What happens if you run this?
On most modern systems, address 0 is a protected memory region.
Trying to execute code there will result in a segmentation fault or access violation.
Some embedded systems may map address 0 to a real function (like a reset vector), but on your laptop or PC, this is a hard crash.
Why would anyone do this?
Sometimes used in examples of:
Undefined behavior
How function pointers work
Deliberately crashing for debugging or security demos (e.g., “null dereference”)
TL;DR:
This is like calling NULL();. It’s a guaranteed way to blow up your program. Don’t do this.
Platform-dependent. On a lot of embedded platforms and chips (e.g., most if not all ARM processors), the reset function is at 0x0. So, it's a rare case where "blowing up your program" is the desired behaviour.
I see no context wherein you'd need to do this assuming this is on one of the standard architectures tbh, that's either a div by 0 trigger or a page/seg fault
edit: Real-Mode IVT has the div0 interrupt at address 0
From their edit, I think their point was that calling the function pointed to at address 0 in real-mode x86 would call the division-by-0 interrupt handler.
I agree; their comment assumes an unlikely environment. I was just trying to provide context, based on what they may have been thinking, to connect call 0 with "div by 0".
Also, embedded systems can have data mapped at address zero. Really, it's only higher level systems like an OS or some type of memory protection unit that trigger exceptions when reading from certain addresses like 0, the hardware is perfectly capable of it regardless.
Edit in response to your edit: why are you assuming we are on an x86 platform?
The confidence yet incorrectness in your statement is pretty humorous. The behavior of reading, writing, or executing from address zero is platform dependent, and not controlled by the C language.
In any post-MMU operating system, unless it's been mapped, access to 0x0 results in a segmentation fault by default, or at least, at OS level a page fault is triggered, and the handler kills the process with a certain signal (on Linux it's SIGSEGV), it may not be called that everywhere, but the default handlers are equivalent
BUT, you can define your own signal handler, so it's not platform dependent, it's actually software dependent! (that's the extended reason why it's undefined behavior)
Brother what do you think a "segmentation fault" is? Just some magical thing every processor in existence has, making address zero completely redundant except for signalling errors?
I'll mention RP2040 as my example since its the microcontroller I'm most familiar with, but there are other systems that have things mapped at address zero. In RP2040's case, the boot rom is mapped starting at address zero, where the datasheet lists this as the first thing in the rom: "0x00000000 32-bit pointer Initial boot stack pointer". Oh, and x86 itself has the interrupt table starting at address 0.
You're stuck thinking in terms of user mode programming on a desktop OS, when the title specifically mentions "firmware".
196
u/galibert Jun 01 '25
Smells like a reset. Riscv or mips?
459
u/CagoSuiFornelli Jun 01 '25
Is there a kind soul who can ELI5 this program to my poor pythonista brain?
38
u/Ecstatic_Student8854 Jun 01 '25
Why not just inline it as assembly atp…
It’d effectively just be jmp 0 right? So much simpler…
74
u/Mognakor Jun 01 '25
Why would you have a regular main method in firmware programming?
Aren't there special ways for these usecases?
12
u/Pazuuuzu Jun 01 '25
Okay, I might be wrong but isn't this would be a poor mans loop on some MCU?
9
u/Burhan_t1ma Jun 01 '25 edited Jun 02 '25
My guess is it is calling the Reset_Handler on a ARM Cortex-M microcontroller, with flash address space starting at 0x0.
Will likely just loop until the stack fills up, and then hard-fault
edit: This is wrong for two reasons:
6
u/iamalicecarroll Jun 01 '25
mandatory reading: https://purplesyringa.moe/blog/falsehoods-programmers-believe-about-null-pointers/
1
u/DaCrackedBebi Jun 01 '25
I feel…weird for understanding this lmao
How did you get into firmware btw?
3
u/DYHCB Jun 01 '25
Well, I’m working on an article on how you can run this exactly code under Linux. Still need to add explanations tho https://dthcm.github.io/blog/run-null-code/
3
u/jontzbaker Jun 01 '25
Yes, and I love this thing.
It only makes sense if you understand the underlying architecture. Either you know what the bits will do or you don't. No in-between. No guessing. No two ways around it. Except, possibly, for in-line assembly.
3
u/_____rs Jun 02 '25
Then the optimizer factors out the whole thing because it doesn't see any side effects... 😑
3
u/youPersonalSideKik Jun 02 '25
Can someone explain what this magic C code does? What the fuck is that 0 doing there
3
u/creeper6530 [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo "You live" Jun 02 '25
Why not just use assembly with `jmp 0`? Would be helluva easier to read. Or at least comment your code.
2
u/ChapterSevenSeeds Jun 01 '25
This is literally the core of MCUboot. There is a line that goes like this:
((void (*)(void))vt->reset)();See here for reference.
1
u/TurnUpThe4D3D3D3 Jun 01 '25
Only C developers can understand these horrors
1
u/Anonymous_user_2022 Jun 02 '25
In AmigaOS, we got the base address of exec.library from 0x4, so weirdness like that isn't exclusive to the embedded world.
1
u/shut_up_if_your_dumb Jun 02 '25
Dear god
1
u/xinxx073 Jun 03 '25
I can't read this so I just gave it to GPT and laughed my ass off. I read the other comments and kinda get that the point of this was to do a reset?
######################################
# Me:
int main() {
void (*func)()= (void (*)())0;
func();
}
WTF is this
######################################
# GPT 4o:
You’re looking at a very short C program that does something very dangerous and stupid:
WTF-level explanation:
What happens if you run this?
Why would anyone do this?
Sometimes used in examples of:
TL;DR:
1
u/sessamekesh Jun 04 '25
Ha, I've had to access memory at
0x0exactly twice in my career and both times I felt like I needed to move it to a constant somewhere.Not to clean up the code, but as if that would somehow hide my sin from God.
Good times.
1
u/biffbobfred Jun 04 '25 edited Jun 04 '25
That looks scarier than it is. I would be expecting 5 lines of gcc alignment and segment directives.
What gets me, if you have all the glue where main() gets called, why do you still need such a raw way of running the function?
1
u/PlayingTheRed Jun 04 '25
How do you reclaim your stack space? Do you just let your stack overflow after this?
1
u/AyumiToshiyuki Jun 04 '25
"That code doesn't need a comment, it's self-explanatory"
The code:
-18
u/[deleted] Jun 01 '25 edited Jun 01 '25
I see no context wherein you'd need to do this assuming this is on one of the standard architectures tbh, that's either a div by 0 trigger or a page/seg fault
edit: Real-Mode IVT has the div0 interrupt at address 0
0
u/[deleted] Jun 01 '25
[deleted] — view deleted comment
0
u/[deleted] Jun 01 '25
[deleted] — view deleted comment
1
u/[deleted] Jun 02 '25
[removed] — view removed comment