I am given this C code (the details of the code, including possible bugs, are not very relevant):
int read_leb128(char **ptr, char *end) {
int r = 0;
int s = 0;
char b;
do {
if ((intptr_t)*ptr >= (intptr_t)end) (exit(1));
b = *(*ptr)++;
r += (b & (char)0x7f) << s;
s += 7;
} while (b & (char)0x80);
return r;
}
and I want to throw some formal methods at it to rule out dangerous bugs.
In particular, I would like a assurance that this function does not modify any value besides *ptr
and only reads memory from *ptr
to end
(not inclusive).
It looked like Frama-C is a good framework for such verification, so I started to add annotations:
/*@
requires \valid(ptr);
requires \valid_read((*ptr) + (0 .. (end-*ptr)));
assigns *ptr;
*/
It seems that the Frama-C plugin that checks for invalid memory access is Eva, but running it on these files still prints:
[eva:alarm] foo.c:33: Warning:
out of bounds read. assert \valid_read(tmp);
(tmp from *ptr++)
Am I just expecting too much of the tool, or is there a way for Frama-C to verify this?
This is Frama-C 19.0 (Potassium).