I need to debug an ugly and huge math C library, probably once produced by f2c. The code is abusing local static variables, and unfortunately somewhere it seems to exploit the fact that these are automatically initialized to 0. If its entry function is called with the same input twice, it is giving different results. If I unload the library and reload it again, it works correctly. It needs to be fast, so I would like to get rid of the load/unload.
My question is that how to uncover these errors with valgrind or by any other tool without manually walking through the entire code.
I am hunting places where a local static variable is declared, read first, and written only later. The problem is even further complicated by the fact that the static variables are sometimes passed further via pointers (yep – it is so ugly).
I understand that one can argue that mistakes like this should not be necessary detected by an automatic tool, as in some scenarios this is exactly the intended behaviour. Still, is there a way to make the auto-initialized local static variables “dirty”?
The devil is in the details, but this may work for you:
First, get Frama-C. If you are using Unix, your distribution may have a package. The package won’t be the last version but it may be good enough and it will save you some time if you install it this way.
Say your example is as below, only so much bigger that it’s not obvious what is wrong:
Type a command like:
Options
-lib-entry -main addmean “look at functionadd“. Option-depscomputes functional dependencies. You’ll find these “functional dependencies” in the log:This lists the actual inputs the results of
adddepend on, and the actual outputs computed from these inputs, including static variables read from and modified. A static variable that was properly initialized before being used would normally not appear as input, unless the analyzer was unable to determine that it was always initialized before being read from.The log shows
stateas dependency of\result. If you expected the returned result to depend only on the arguments (meaning two calls with the same arguments produce the same result), it’s a hint there may be something wrong here, with the variablestate.Another hint shown in the above lines is that the function modifies
state.This may help or not. Option
-lib-entrymeans that the analyzer does not assume that any non-const static variable has kept its value at the time the function under analysis is called, so that may be too imprecise for your code. There are ways around that, but then it is up to you whether you want to gamble the time it takes to learn these ways.EDIT: here is a more complex example:
On this example, the same command produces the results:
What you see for
initialize_2is an empty list of dependencies, meaning the function assigns nothing. I will make this case clearer by displaying an explicit message rather than just an empty list. If you know what any of the functionsinitialize_1,initialize_2oraddis supposed to do, you can compare this a priori knowledge to the results of the analysis and see that something is wrong forinitialize_2andadd.SECOND EDIT: and now my example shows something strange for
initialize_1, so perhaps I should explain that. Variablestate1depends onpin the sense thatpis used to write tostate1, and ifphad been different, then the final value ofstate1would have been different. Here is a last example:With the command
frama-c -deps t.c, the dependencies computed forinitialize_indexare:This means that each of the cells depends on
i(it may be modified ifiis the index of that particular cell). Each cell may also keep its value (ifiindicates another cell): this is indicated with the(and SELF)mention in the latest version, and was indicated with a more obscure(and default:true)in previous versions.