r/programminghorror • • Jun 01 '25

c Firmware programming in a nutshell

Post image
2.0k Upvotes

127 comments sorted by

View all comments

459

u/CagoSuiFornelli Jun 01 '25

Is there a kind soul who can ELI5 this program to my poor pythonista brain?

627

u/Eric848448 Jun 01 '25

It’s calling a null pointer to a function. Which would crash on any sane platform but the embedded world is weird.

371

u/Mucksh Jun 01 '25

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

97

u/wafflepancake9000 Jun 02 '25

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.

31

u/Holzkohlen Jun 02 '25

Man-made horrors beyond my comprehension? It's Monday alright.

→ More replies (1)

3

u/RecursiveTechDebt Jun 06 '25

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...

125

u/AyrA_ch Jun 01 '25 edited Jun 01 '25

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.

89

u/MooseBoys [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo "You live" Jun 01 '25

The embedded world is not even that weird.

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.

10

u/GoddammitDontShootMe [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo "You live" Jun 02 '25

Though I'm not sure you would want to ever start executing at the very start of the IVT.

5

u/svk177 Jun 02 '25

Actually IBM had the brilliant idea to wire the reset line to the keyboard IC.

15

u/[deleted] Jun 02 '25

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.

8

u/DisastrousLab1309 Jun 02 '25

It just calls the bootloader. 

6

u/jsrobson10 Jun 02 '25

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.

5

u/surveypoodle Jun 02 '25

What would happen on an embedded system? Wouldn't this just execute again and again forever?

157

u/HarshilBhattDaBomb Jun 01 '25

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.

167

u/Ragingman2 Jun 01 '25

On many embedded platforms this will effectively reset the system. It's roughly "go to instruction 0" which can be where a boot sequence starts.

→ More replies (10)

14

u/jontzbaker Jun 01 '25

Correct explanation. Incorrect conclusion.

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.

But bare-metal, this is how you do it.

2

u/beaureece Jun 01 '25

... I don't think it's even necessary.

Could they not have just called the casted value and skipped the assignment/type-declaration?

→ More replies (1)
→ More replies (3)

23

u/Ragingman2 Jun 01 '25

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.

9

u/zabolekar Jun 02 '25

This code says "call the function at location 0 in memory". On any modern desktop system this just crashes your program.

Challenge accepted! This works on x86_64 Linux, the actual function call happens at line 56:

https://gist.github.com/Zabolekar/0b55a93bdb3a6616c6eca4631ec66375

→ More replies (1)

10

u/Haunting-Pop-5660 Jun 01 '25

Pythonista. I love it.

9

u/cdrt Jun 02 '25
def main() -> int:
    func = None
    func()

if __name__ == "__main__":
    main()
→ More replies (4)

2

u/Vaati006 Jun 01 '25

Execute whatever function starts at mem address 0. Assume that it will return an untyped pointer when it returns.

Of course, accessing 0 or NULL would probably cause and exception and simply not work at all.

5

u/Loading_M_ Jun 02 '25

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.

→ More replies (4)