Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Valgrind errors when linked with -static -- Why?

Tags:

c++

valgrind

I have a test driver linked to a library I wrote. The library uses autotools so it produces both an archive (.a file) and a dynamic library (.so).

When I link my driver with 'g++ -static', presumably linking to the .a, valgrind lights up complaining repeatedly 'Conditional jump or move depends on uninitialised value(s)'. The first failure occurs before main in __pthread_initialize_minimal.

When I link without -static, presumably linking with the .so, I don't get any valgrind complaints.

Does anyone know why? Does valgrind just not work with -static?

UPDATE: I can't post even a pared down version of my driver because it links to a very large library that I couldn't possible pare down, but I notice that simplest of all programs

int main()
{
  return 0;
}

gives an error when linked with -static and run from valgrind:

==15449== Use of uninitialised value of size 8
==15449==    at 0x40B0F3: exit (in /home/jdgordo/src/t)

I should have included that I'm running on 64-bit Redhat 5.5.

like image 254
John Gordon Avatar asked Sep 21 '11 20:09

John Gordon


People also ask

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.

Can Valgrind be wrong?

Yes, there are false positives with Valgrind, that's why it has suppression files for particular glibc and gcc versions, for example.

How can I see Valgrind errors?

If you compile your program with the -g flag, Valgrind will show you the function names and line numbers where errors occur. Sometimes the actual bug occurs on a different line (particularly for uninitialized value errors) but the line number Valgrind tells you is a good starting point.

How do you fix the conditional jump in Valgrind?

Conditional jump or move depends on uninitialized value(s) This error is caused if you forget to initialize variables before using or accessing them. You can usually re-run valgrind with the flag --track-origins=yes to see where the uninitialized value came from.


2 Answers

Does valgrind just not work with -static?

It does. The problem is not in Valgrind, it's in glibc, which is not Valgrind clean. The glibc developers refused to fix these problems (because the problems are of a "don't care" nature, and fixing them costs (a few) cycles).

When you link dynamically, these errors come from libc.so.6, and can be easily suppressed, which is what Valgrind does by default.

But when you link statically, these errors come from your executable (which now includes code from libc.a), and so the default Valgrind suppressions don't suppress them.

You could write new suppressions (see Valgrind --gen-suppressions=yes documentation).

Or you could install and use glibc-audit.

like image 176
Employed Russian Avatar answered Oct 03 '22 23:10

Employed Russian


If the library causes problems in valgrind, you can only ignore those problems by writing suppression files.

One of problems I encountered is alocating something on the heap, like this :

// library
int * some = new int;

// main links the library
int main()
{
}

This example would report an error about leak.

EDIT : if you have the library's source, you can fix the error (use of uninitialized variable), and recompile it.

like image 22
BЈовић Avatar answered Oct 03 '22 23:10

BЈовић