logoalt Hacker News

jagrsw11/07/20242 repliesview on HN

Just a friendly reminder that syscall() is a vararg function. Meaning, you can't just go throwing arguments at it (so maybe it's better to use this wrapper to avoid problems instead).

For example, on a 64-bit arch, this code would be sus.

syscall(__NR_syscall_taking_6_args, 1, 2, 3, 4, 5, 6);

Quiz: why

PS: it's a common mistake, so I thought I'd save you a trip down the debugging rabbit hole.


Replies

remram11/07/2024

A quiz is the opposite of saving someone effort.

show 2 replies
jstarks11/07/2024

I guess if the arch’s varargs conventions do something other than put each 32-bit value in a 64-bit “slot” (likely for inputs that end up on the stack, at least), then some of the arguments will not line up. Probably some of the last args will get combined into high/low parts of a 64-bit register when moved into registers to pass to the kernel. And then subsequent register inputs will get garbage from the stack.

Need to cast them to long or size_t or whatever to prevent this.

show 1 reply