Skip to content

Instantly share code, notes, and snippets.

@MMI
MMI / device-bootloader.c
Created January 27, 2022 17:08
Mac only C code to reboot an ESP32 device into bootloader mode
/*
* This code works on my Mac, developed in conjuntion with a logic analyzer.
*/
#include <stdlib.h>
#include <unistd.h>
#include <termios.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
@MMI
MMI / README.md
Created March 20, 2021 15:09
Undefined behaviour: ARM gcc cross-compiler may generate illegal instructions in place of known division by zero

Division By Zero Exceptions

Setup

On an embedded project, I recently had to debug a crash where the root cause was a division by zero. The offending code in question reduced to something like function foo() presented here.

To validate that it actually was a division by zero problem, I added the if block that printed "GOTCHA". After this change, I saw that the processor status register (CFSR) had the UNDEFINST bit set instead of the exepected DIVBYZERO bit. What?

Using godbolt we can see the compiler emitting an instruction 0xdeff in main... with no other code (suggesting that the compiler realized that the code will not work and simply stopped -- without warning, I might add).

Keybase proof

I hereby claim:

  • I am MMI on github.
  • I am georgn (https://keybase.io/georgn) on keybase.
  • I have a public key whose fingerprint is 51AA 9B57 8B41 9BA6 6BC7 E67B A093 678A 44DD 7643

To claim this, I am signing this object: