Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does valgrind track memory initialization through drivers?

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.

like image 389
bstpierre Avatar asked Jul 12 '10 23:07

bstpierre


People also ask

How does valgrind detect memory leaks?

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.

How does valgrind work internally?

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.

What are the problems with valgrind?

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.

Is valgrind a dynamic library?

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.


1 Answers

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.

like image 189
zwol Avatar answered Oct 21 '22 03:10

zwol