I wonder if anybody is interested to test out the ExprEngine.
As a first step ... I want it to handle all types of data and expressions in C code.... pointers, arrays, structs, unions, aliases, dynamic memory, ....
For now, I do not try to handle control keywords well. for/while/switch/if/break/continue/goto. Please wait with testing that.
what missing handling can you find? I would think there is still a lot to fix. Please wait with C++ stuff.. we can fix that in the next step.
Of course I need to handle that later.. and not crash! But for now, please run gcc -fsyntax-only on the file or something and ensure it is valid C code.
Last edit: Daniel Marjamäki 2019-09-23
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I added a temporary hack for calloc. Pretend that it does not return NULL for now. The handling of buffers/arrays need to be fixed a lot! So it is interesting to focus a little extra at calloc etc usage.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
In this simple example i guess it is safe to assume that s1.a is 2 and x is 3.
There is no output for variables in the struct, are structs not implemented yet?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Interestingly Cppcheck already detects the division by zero, the expression engine shows unkwnon values.
I guess that because sizeof() is not implemented in the expression engine it is assumed that it could change the content of the array.
I would expect the value of sz and the array content to be known in this simple example.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Interestingly Cppcheck already detects the division by zero, the expression engine shows unkwnon values.
That is by intention.. In general, the value -128:127 means that the value can be 0.
please note that if you write buf[0] + buf[0] then the range will be -256:254. The value can be 0 but not 1. I intend to implement an algorithm for this that checks if a specific value in the range is mathematically possible.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I wonder if anybody is interested to test out the ExprEngine.
As a first step ... I want it to handle all types of data and expressions in C code.... pointers, arrays, structs, unions, aliases, dynamic memory, ....
For now, I do not try to handle control keywords well. for/while/switch/if/break/continue/goto. Please wait with testing that.
what missing handling can you find? I would think there is still a lot to fix. Please wait with C++ stuff.. we can fix that in the next step.
Here is an example that you can start with:
Cppcheck output:
In this output ... the "program state" is written at line 2 and 3.
Last edit: Daniel Marjamäki 2019-09-23
My goal is to have symbolic analysis in ExprEngine ... you can for instance try stuff like:
Elements in buf should be either uninitialized or 0.
y should be 0.
z should be uninitialized.
The current output says:
It says that all elements in buf are uninitialized. and y and z did not get any value. So this does not work fully.
output:
I would expect
x
to be2
at 3 since it is anunsigned char
.Last edit: versat 2019-09-23
results in:
I would not expect segmentation fault to happen.
Last edit: versat 2019-09-23
Output:
I would not expect a segfault here too.
Since you explicitly wrote about testing dynamic memory i tried this:
Output:
I would expect c to be
0+1
or something similar.thanks versat! I'll write down your examples and ensure we handle them asap!
.. that was exactly the kind of input I wanted to have. I hope cppcheck will do much better on the next iteration :-)
Your second example has a syntax error
Of course I need to handle that later.. and not crash! But for now, please run
gcc -fsyntax-only
on the file or something and ensure it is valid C code.Last edit: Daniel Marjamäki 2019-09-23
Oops, yes, that was not by intention. I added syntax check with the last example, too late.
With latest head ( https://github.com/danmar/cppcheck/commit/524c9f593699daa3c830ff553ba597c5ce099ce9 )
C code:
Output:
Code is syntactically correct so i would not expect a segfault here.
Last edit: versat 2019-09-24
This is fixed now and works like expected:
If i slightly change the code the result changes but should be the same:
Output is:
Should such calculation work or is planned to work later?
I added a temporary hack for
calloc
. Pretend that it does not return NULL for now. The handling of buffers/arrays need to be fixed a lot! So it is interesting to focus a little extra at calloc etc usage.Output:
Seems
sizeof()
is not implemented yet.The variable
sz
should have value9
and the last element of the arraystr
should be known to have the value0
.hmm yes sizeof() is not handled in ExprEngine yet. It should.
I am restructuring the array code to work better for dynamic arrays. The output will change...
Imagine such code:
The output should become something like:
=> the id for arr value is $3. The size is $1. Then comes assignments ... first all elements are uninitialized and then element $2 is zeroed.
That tells you that
arr[0]
will be either?
or0
. The value ofarr[$2]
is 0. The value ofarr[$4..]
is?
or0
. The value ofarr[expr<$2>]
will be?
.For this code i would expect that it is known that the array only contains
0
after the memset:But the values are still unknown:
I am not sure if
memset()
should work and if it should be tested, but i tried it since it is very common.The ExprEngine will need better information to have full analysis. I am thinking about adding some additional ExprEngine configuration.
Maybe it's best that you don't use standard functions now to start with..
Output:
In this simple example i guess it is safe to assume that
s1.a
is2
andx
is3
.There is no output for variables in the
struct
, arestruct
s not implemented yet?Output:
Interestingly Cppcheck already detects the division by zero, the expression engine shows unkwnon values.
I guess that because
sizeof()
is not implemented in the expression engine it is assumed that it could change the content of the array.I would expect the value of
sz
and the array content to be known in this simple example.That is by intention.. In general, the value -128:127 means that the value can be 0.
please note that if you write
buf[0] + buf[0]
then the range will be-256:254
. The value can be 0 but not 1. I intend to implement an algorithm for this that checks if a specific value in the range is mathematically possible.I restructured the
ArrayValue
... now I need to figure out how to split the execution path in the middle of a statement somehow. If the code is :then we need to split the programstate. one state where y is 5 and x will be 14. and one state where y is not 5 and x is something else..
but anyway, the arrayvalue seems to be printed well now..