views:

56

answers:

1

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.

+6  A: 

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.

Zack
Thanks Zack, that's about what I figured. There's a little more context around the code, and read() wouldn't make much sense. (Though it's not my driver and I can't really change it.)
bstpierre
I meant to mention that you could include `memcheck.h`, then use `VALGRIND_MAKE_MEM_DEFINED` instead of the `memset`. That'll be faster.
Zack
Nice! Thanks again.
bstpierre