Consequences of passing too few register parameters to a C function

Passing too few arguments to a C function leads to undefined behavior, ranging from stack corruption to hardware exceptions on architectures like Itanium.
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. Formally, the C and C++ languages say that if you call a function with the wrong number of parameters, the behavior is undefined. If you pass too few parameters on the stack, it results in stack imbalance and likely memory corruption. On most processors, the called function will try to use that register and read whatever uninitialized value happens to be lying in that register. Except on Itanium. One special Itanium quirk is the presence of the “Not a Thing” (NaT) bit. If your uninitialized output register happens to be a NaT left over from an earlier failed speculation, the called function might decide to spill the value onto the stack, raising a “NaT consumption” exception. Furthermore, on Itanium, the function call mechanism is architectural. Reading or writing a stack register that lies outside the current frame is required to raise an Illegal Operation fault. This is another case where the Itanium architecture strictly enforces programming rules, ensuring the correct number of parameters are passed.
Source: Hacker News
















