valgrind is reporting uninitialized memory errors from code like this:
unsigned char buf[100];
struct driver_command cmd;
cmd.len = sizeof(buf);
cmd.buf = buf;
ioctl(my_driver_fd, READ, &cmd);
for(i = 0; i < sizeof(buf); i++)
{
foo(buf[i]); /* <<--- uninit use error from valgrind */
}
If I memset() the buf before the driver call, the error goes away.
Can valgrind detect whether the linux driver is properly writing to the buffer? (I looked at the driver code, and it seems to be correct, but maybe I'm missing something.)
Or does it just pass the driver call through and has no way of knowing that the buffer has been written inside the kernel?
Thanks.
Detecting memory leaks with Valgrind MemcheckMemcheck tracks all memory reads, writes, allocations, and deallocations in a C or C++ program. The tool can detect many different memory errors. For instance, it detects reads or writes before or after allocated memory blocks.
Valgrind works by doing a just-in-time (JIT) translation of the input program into an equivalent version that has additional checking. For the memcheck tool, this means it literally looks at the x86 code in the executable, and detects what instructions represent memory accesses.
Valgrind reports two types of issues: memory errors and memory leaks. When a program dynamically allocates memory and forgets to later free it, it creates a leak. A memory leak generally won't cause a program to misbehave, crash, or give wrong answers, and is not an urgent situation.
Valgrind uses dynamic binary instrumentation, so you don't need to modify, recompile or relink your applications. Just prefix your command line with valgrind and everything works. Valgrind is not a toy.
Valgrind obviously can't trace execution into the kernel, but it does know the visible semantics of most system calls. But ioctl
is too unpredictable. If you had coded your driver so that that was a read
call, it would get it right. That's better practice anyway.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With