Analysis of Valgrind log for boost::uuid

2019-02-28 07:05发布

问题:

I am using boost::uuid in order to generate unique ids:

string UUid()
{
    boost::uuids::uuid uuid = boost::uuids::random_generator()();
    return boost::lexical_cast<std::string>(uuid);
}

When I use valgrind in order to analyse my code i get the following remarks:

Valgrind log

==47807== Conditional jump or move depends on uninitialised value(s)
==47807==    at 0x441D19: void boost::random::mersenne_twister_engine<unsigned int, 32ul, 624ul, 397ul, 31ul, 2567483615u, 11ul, 4294967295u, 7ul, 2636928640u, 15ul, 4022730752u, 18ul, 1812433253u>::seed<boost::uuids::detail::generator_iterator<boost::uuids::detail::seed_rng> >(boost::uuids::detail::generator_iterator<boost::uuids::detail::seed_rng>&, boost::uuids::detail::generator_iterator<boost::uuids::detail::seed_rng>) (mersenne_twister.hpp:177)
==47807==    by 0x4417EC: void boost::uuids::detail::seed<boost::random::mersenne_twister_engine<unsigned int, 32ul, 624ul, 397ul, 31ul, 2567483615u, 11ul, 4294967295u, 7ul, 2636928640u, 15ul, 4022730752u, 18ul, 1812433253u> >(boost::random::mersenne_twister_engine<unsigned int, 32ul, 624ul, 397ul, 31ul, 2567483615u, 11ul, 4294967295u, 7ul, 2636928640u, 15ul, 4022730752u, 18ul, 1812433253u>&) (seed_rng.hpp:249)
==47807==    by 0x440EAA: boost::uuids::basic_random_generator<boost::random::mersenne_twister_engine<unsigned int, 32ul, 624ul, 397ul, 31ul, 2567483615u, 11ul, 4294967295u, 7ul, 2636928640u, 15ul, 4022730752u, 18ul, 1812433253u> >::basic_random_generator() (random_generator.hpp:50)
==47807==    by 0x43B4D5: ManageDb::randomid() (ManageDb.cpp:92)
==47807==    by 0x43B57A: ManageDb::fillTables(std::vector<Entity, std::allocator<Entity> > const&) (ManageDb.cpp:103)
==47807==    by 0x40BBF7: DataLoader::extractData() (DataLoader.cpp:78)
==47807==    by 0x42EF26: main (main.cpp:30)
==47807==  Uninitialised value was created by a heap allocation
==47807==    at 0x4C2B0E0: operator new(unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==47807==    by 0x44042A: boost::uuids::detail::seed_rng::sha1_random_digest_() (seed_rng.hpp:167)
==47807==    by 0x44025B: boost::uuids::detail::seed_rng::operator()() (seed_rng.hpp:103)
==47807==    by 0x441C97: boost::uuids::detail::generator_iterator<boost::uuids::detail::seed_rng>::generator_iterator(boost::uuids::detail::seed_rng*) (seed_rng.hpp:218)
==47807==    by 0x4417C5: void boost::uuids::detail::seed<boost::random::mersenne_twister_engine<unsigned int, 32ul, 624ul, 397ul, 31ul, 2567483615u, 11ul, 4294967295u, 7ul, 2636928640u, 15ul, 4022730752u, 18ul, 1812433253u> >(boost::random::mersenne_twister_engine<unsigned int, 32ul, 624ul, 397ul, 31ul, 2567483615u, 11ul, 4294967295u, 7ul, 2636928640u, 15ul, 4022730752u, 18ul, 1812433253u>&) (seed_rng.hpp:247)
==47807==    by 0x440EAA: boost::uuids::basic_random_generator<boost::random::mersenne_twister_engine<unsigned int, 32ul, 624ul, 397ul, 31ul, 2567483615u, 11ul, 4294967295u, 7ul, 2636928640u, 15ul, 4022730752u, 18ul, 1812433253u> >::basic_random_generator() (random_generator.hpp:50)
==47807==    by 0x43B4D5: ManageDb::randomid() (ManageDb.cpp:92)
==47807==    by 0x43B57A: ManageDb::fillTables(std::vector<Entity, std::allocator<Entity> > const&) (ManageDb.cpp:103)
==47807==    by 0x40BBF7: DataLoader::extractData() (DataLoader.cpp:78)
==47807==    by 0x42EF26: main (main.cpp:30)
==47807== 

Questions

  • Why is valgrind generating these remarks for boost::uuid?
  • If it's a problem in boost can I ignore it?

Possible bug

  • According to this link Ticket #7248 (reopened Bugs) it is a possible bug. Fine if is a bug how can I make valgrind ignore it?

Valgrind command

valgrind --leak-check=full --track-origins=yes --suppressions=valgrind.supp ./MyProgram > valgrind-log.txt

valgrind.supp

# supression file for continuum with valgrind
# to generate each supression use: --gen-suppressions=yes option
# to use this supression file, use: --suppressions=<this filename>

{
Crypt_r
Memcheck:Cond
obj:/lib/libc-2.11.1.so
fun:__sha512_crypt_r
fun:crypt_r
}

{
Crypt_r use of uninitialised value of size 8
Memcheck:Value8
obj:/lib/libc-2.11.1.so
fun:__sha512_crypt_r
fun:crypt_r
}

{
String S_Create
Memcheck:Leak
fun:_Znwm
fun:_ZNSs4_Rep9_S_createEmmRKSaIcE
}

{
Mongo OID
Memcheck:Value8
fun:_ZN5mongo10toHexLowerEPKvi
fun:_ZNK5mongo3OID3strEv
}

回答1:

Please check http://www.boost.org/doc/libs/1_50_0/libs/uuid/uuid.html

The boost::uuids::basic_random_generator class default constructor seeds the random number generator with a SHA-1 hash of a number of different values including std::time(0), std::clock(), **uninitialized data**, value return from new unsigned int, etc..

...Using Valgrind produces a number of false positives with the default constructor of boost::uuids::basic_random_generator. One solution is to suppress the errors as described in Valgrind's documentation. Another solution is to use a different constructor of boost::uuids::basic_random_generator and explicitly pass in a random number generator.