In our exploration of calling conventions for various processors on Windows, we learned that in many cases, some of the parameters are passed in registers.
Suppose that there is a function that takes two parameters, but you know that the function ignores the second parameter if the first parameter is positive. What happens if you call the function with just one parameter (say, passing zero). The function should ignore the second parameter, so why does it matter that you didn’t pass one?
Even though the function doesn’t use the parameter, it still may decide to use the storage for that parameter as a conveniently provided scratch space. For example:
int blah(int a, int b) { if (a <= 0) { int c = f1(); f2(a); return c; } else { return f3(a, b); }
Is it okay to call blah with zero as its only parameter? You aren’t passing b , but the function doesn’t use b , so why does it matter?
Formally, the C and C++ languages say that if you call a function with the wrong number of parameters, the behavior is undefined, so officially, you’ve broken the rules and anything can happen.
But let’s look at what types of things could go wrong.
If you pass too few parameters on the stack, and it is a callee-clean calling convention, then the callee will clean too many bytes off the stack, resulting in stack imbalance and likely memory corruption.
Even if it’s not a callee-clean calling convention, the called function will think that the memory for the parameter is present, and it may use it as scratch space, resulting in memory corruption in the stack frame of the calling function.
In our example above, the compiler might realize, “Hey, I don’t need to allocate new memory for the variable c . I can just reuse the memory that holds the now-dead variable b .” In other words, it rewrites the function as
... continue reading