Worth reading to the end just for the totally evil code snippet.
It was kind of foreshadowed to be evil when the author named it “skynet.c” I guess.
Reminds me of the Java-code we used to see around 2000.
With a RuntimeException try-catch at the top and then just print it and continue like nothing happened.
How much bad bugs, data corruption and weirdness did that practice cause?
How is that any different from kubernetes and “just restart it”? Its mostly the same practice ultimately, though with a bit more cleanup between failures.
I guess it depends on whether you keep any app state in memory. If you’re just funnelling data to a database maybe not much difference.
Now I start to wonder, how the correct code should look like (as opposed of jumping 10 bytes ahead).
Read DWARF to figure out next instruction?
Embed a decompiler to decode the faulty opcode length?
Increment the instruction pointer until you end up at a valid instruction (i.e., you don’t get SIGILL), of course ;)
I have code that does this by catching SIGILL too and bumping the instruction pointer along in response to that. https://github.com/RichardBarrell/snippets/blob/master/no_crash_kthxbai.c
Brilliant. I’m simultaneously horrified and amused.
That’d be a pretty great nerdcore MC name.
If you want to skip the offending instruction, à la Visual Basics “on error resume next”, you determine instruction length by looking at the code and then increment by that.
Figuring out the length requires understanding all the valid instruction formats for your CPU architecture. For some it’s almost trivial, say AVR has 16 bit instructions with very few exceptions for stuff like absolute call. For others, like x86, you need to have a fair bit of logic.
I am aware that the “just increment by 1” below are intended as a joke. However I still think it’s instructive to say that incrementing blindly might lead you to start decoding at some point in the middle of an instruction. This might still be a valid instruction, especially for dense instruction set encodings. In fact, jumping into the middle of operands was sometimes used on early microcomputers to achieve compact code.
Here’s a more correct approach: https://git.saucisseroyale.cc/emersion/c-safe
Just don’t compile it with -pg :)