My 2 month beef with my own Linux environment. (Developer cautionary tale)
20 points by NICUP14
20 points by NICUP14
This should be a blog post, rather than a reddit post. Also, kudos for the persistence, I'm sure the author learned a lot.
I call this pattern "Maybe gravity is disabled": When you have a blind spot for the real problem, and everything else seems to be working, and you start to question basic assumptions about the universe itself. Maybe the compiler is broken? Maybe adding integers doesn't work if they're all mulitples of three? Maybe gravity was disabled around our office?
It's extremely common (myself included) and it takes a minute to realize you should take a step back and hit it from a different angle... that gravity is probably still enabled in our quadrant. :)
I'm surprised this did not break any further. Did you take a look at the generated code to see if it happens to leave most of the argument registers intact?
I'm surprised this did not break any further. Did you take a look at the generated code to see if it happens to leave most of the argument registers intact?
The “patch” is definitely dirty, but it somehow works fine in practice. Syscalls can take up to six arguments, and on aarch64 (armv8) those are all passed via registers, not the stack. So it remains untouched. Any leftover register values beyond what a given syscall expects are just ignored by the kernel anyway, so the extra garbage doesn’t cause trouble. For a 32bit system parameters are passed on the stack and this would cause more issues than it would solve XD
Even the final version shown here isn’t great - OP is forced to generate code that can only cope with a fixed number of varargs arguments because there’s no way to forward varargs to a subsequent function call inside an existing varargs function.
This wart was patched for the few functions that obviously need it in libc by adding functions like vprintf() that take a va_list * as a final argument, but it would have been much simpler for everyone, with the bonus of future proofing the language, if they’d just made it possible to forward varargs.
Also here to point that va_* utilities from <stdarg.h> are compiler built-ins (think of them like pre-defined macros) that aren't properly integrated into the language. This forces developers to craft ABI-dependent  unsafe solutions like this just to forward variadic arguments to the next function.