In TIE: Principled Reverse Engineering of Types in Binary Programs, Lee J. et al claim to solve most of the challenges in the process of C code decompilation that come with type reconstruction.
While producing a complex type where maybe a void *
could fit is definitely non-trivial, I was just thinking about a possible type recovery ambiguity for what merely concerns a variable of type int
or int *
.
int foo(int *a, int sz) {
int res = 0;
for (int *q = a; q < &a[sz]; q++)
res += *q;
return res;
}
In 32-bit architectures, the above code shows an example of such ambiguity for variable res
, as both int
and int *
are compatible with a 32-bit register, and the disassembled code is the same. If res
was a pointer though, it would be improperly used since we would be returning the address of an automatic variable (and would lead to undefined behaviour).
So can this example really be taken into account (is a valid instance)? Can you come up with an example where these two types generate ambiguity but are correctly used? Can we conclude that a variable can be inferred to be a pointer or not if it just gets dereferenced?