Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use-of-uninitialized-value #414

Closed
AlexanderLanin opened this issue Aug 30, 2020 · 6 comments
Closed

use-of-uninitialized-value #414

AlexanderLanin opened this issue Aug 30, 2020 · 6 comments

Comments

@AlexanderLanin
Copy link
Contributor

AlexanderLanin commented Aug 30, 2020

Description

doctest triggers clangs MemorySanititzer so it's not possible to see what my code would do.

Steps to reproduce

mkdir build && cd build && CXXFLAGS=-fsanitize=memory LDFLAGS=-fsanitize=memory cmake .. -DCMAKE_BUILD_TYPE=Debug && make && examples/all_features/all_features

With e.g. clang 10, not sure about other versions.

Extra information

  • doctest version: tag: 2.4.0 = origin/master
  • Operating System: Ubuntu 18 and 20
  • Compiler+version: Clang 10 and trunk

Exact message

==10558==WARNING: MemorySanitizer: use-of-uninitialized-value
    #0 0x3c1f24 in std::_Rb_tree<std::pair<int, doctest::String>, std::pair<std::pair<int, doctest::String> const, doctest::IReporter* (*)(doctest::ContextOptions const&)>, std::_Select1st<std::pair<std::pair<int, doctest::String> const, doctest::IReporter* (*)(doctest::ContextOptions const&)> >, std::less<std::pair<int, doctest::String> >, std::allocator<std::pair<std::pair<int, doctest::String> const, doctest::IReporter* (*)(doctest::ContextOptions const&)> > >::_M_get_insert_unique_pos(std::pair<int, doctest::String> const&) /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_tree.h:2092:7
    #1 0x3c1968 in std::pair<std::_Rb_tree_iterator<std::pair<std::pair<int, doctest::String> const, doctest::IReporter* (*)(doctest::ContextOptions const&)> >, bool> std::_Rb_tree<std::pair<int, doctest::String>, std::pair<std::pair<int, doctest::String> const, doctest::IReporter* (*)(doctest::ContextOptions const&)>, std::_Select1st<std::pair<std::pair<int, doctest::String> const, doctest::IReporter* (*)(doctest::ContextOptions const&)> >, std::less<std::pair<int, doctest::String> >, std::allocator<std::pair<std::pair<int, doctest::String> const, doctest::IReporter* (*)(doctest::ContextOptions const&)> > >::_M_insert_unique<std::pair<std::pair<int, doctest::String> const, doctest::IReporter* (*)(doctest::ContextOptions const&)> >(std::pair<std::pair<int, doctest::String> const, doctest::IReporter* (*)(doctest::ContextOptions const&)>&&) /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_tree.h:2148:4
    #2 0x379abd in std::map<std::pair<int, doctest::String>, doctest::IReporter* (*)(doctest::ContextOptions const&), std::less<std::pair<int, doctest::String> >, std::allocator<std::pair<std::pair<int, doctest::String> const, doctest::IReporter* (*)(doctest::ContextOptions const&)> > >::insert(std::pair<std::pair<int, doctest::String> const, doctest::IReporter* (*)(doctest::ContextOptions const&)>&&) /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_map.h:809:21
    #3 0x330eb2 in doctest::detail::registerReporterImpl(char const*, int, doctest::IReporter* (*)(doctest::ContextOptions const&), bool) /home/alex/doctest/build/examples/all_features/../../../doctest/parts/doctest.cpp:3573:28
    #4 0x319f9e in int doctest::registerReporter<doctest::(anonymous namespace)::JUnitReporter>(char const*, int, bool) /home/alex/doctest/build/examples/all_features/../../../doctest/parts/doctest_fwd.h:1712:5
    #5 0x370f31 in __cxx_global_var_init.69 /home/alex/doctest/build/examples/all_features/../../../doctest/parts/doctest.cpp:2523:5
    #6 0x371af7 in _GLOBAL__sub_I_main.cpp /home/alex/doctest/build/examples/all_features/../../../examples/all_features/main.cpp
    #7 0x4738ac in __libc_csu_init (/home/alex/doctest/build/examples/all_features/all_features+0x4738ac)
    #8 0x7f94e1d0bb27 in __libc_start_main /build/glibc-2ORdQG/glibc-2.27/csu/../csu/libc-start.c:266
    #9 0x27a769 in _start (/home/alex/doctest/build/examples/all_features/all_features+0x27a769)

SUMMARY: MemorySanitizer: use-of-uninitialized-value /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_tree.h:2092:7 in std::_Rb_tree<std::pair<int, doctest::String>, std::pair<std::pair<int, doctest::String> const, doctest::IReporter* (*)(doctest::ContextOptions const&)>, std::_Select1st<std::pair<std::pair<int, doctest::String> const, doctest::IReporter* (*)(doctest::ContextOptions const&)> >, std::less<std::pair<int, doctest::String> >, std::allocator<std::pair<std::pair<int, doctest::String> const, doctest::IReporter* (*)(doctest::ContextOptions const&)> > >::_M_get_insert_unique_pos(std::pair<int, doctest::String> const&)
Exiting

Last non stl line is here:

namespace detail {
    void registerReporterImpl(const char* name, int priority, reporterCreatorFunc c, bool isReporter) {
        if(isReporter)
            getReporters().insert(reporterMap::value_type(reporterMap::key_type(priority, name), c)); //<< here!
        else
            getListeners().insert(reporterMap::value_type(reporterMap::key_type(priority, name), c));
    }
} // namespace detail
@onqtam
Copy link
Member

onqtam commented Aug 31, 2020

I'm pretty sure the issue might be that you are using an uninstrumented libc++: https://github.com/google/sanitizers/wiki/MemorySanitizerLibcxxHowTo

This issue has been reported quite a bit in the past :)

@AlexanderLanin
Copy link
Contributor Author

Oh... was not aware of that, sorry. Sounds like you are correct. Will report in ~2 hours...

@AlexanderLanin
Copy link
Contributor Author

Ok sorry, this is probably a little off topic. But has someone done this already? I'm stuck.

After following the steps above to compile libc++ with MemoryWithOrigins, I'm running:
CC=clang CXX=clang++ cmake .. -DCMAKE_CXX_FLAGS="-fsanitize=memory -stdlib=libc++ -L/home/alex/llvm/build-msan/lib -lc++abi -I/home/alex/llvm/build-msan/include -pthread" -DCMAKE_BUILD_TYPE=Debug -GNinja && ninja && examples/all_features/all_feature

But I end up with:

==16494==WARNING: MemorySanitizer: use-of-uninitialized-value
    #0 0x388074 in std::__1::basic_ostream<char, std::__1::char_traits<char> >& std::__1::__put_character_sequence<char, std::__1::char_traits<char> >(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, char const*, unsigned long) /usr/local/bin/../include/c++/v1/ostream:727:13
    #1 0x366f66 in std::__1::basic_ostream<char, std::__1::char_traits<char> >& std::__1::operator<<<std::__1::char_traits<char> >(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, char const*) /usr/local/bin/../include/c++/v1/ostream:869:12
    #2 0x35d2eb in doctest::(anonymous namespace)::ConsoleReporter::printVersion() /home/alex/doctest/build/../doctest/parts/doctest.cpp:2644:34
    #3 0x362c8c in doctest::(anonymous namespace)::ConsoleReporter::printIntro() /home/alex/doctest/build/../doctest/parts/doctest.cpp:2649:13
    #4 0x357891 in doctest::(anonymous namespace)::ConsoleReporter::test_run_start() /home/alex/doctest/build/../doctest/parts/doctest.cpp:2840:42
    #5 0x320239 in doctest::Context::run() /home/alex/doctest/build/../doctest/parts/doctest.cpp:3409:9
    #6 0x3288ac in main /home/alex/doctest/build/../examples/all_features/main.cpp:24:23
    #7 0x7f2d837c1b96 in __libc_start_main /build/glibc-2ORdQG/glibc-2.27/csu/../csu/libc-start.c:310
    #8 0x27c909 in _start (/home/alex/doctest/build/examples/all_features/all_features+0x27c909)

SUMMARY: MemorySanitizer: use-of-uninitialized-value /usr/local/bin/../include/c++/v1/ostream:727:13 in std::__1::basic_ostream<char, std::__1::char_traits<char> >& std::__1::__put_character_sequence<char, std::__1::char_traits<char> >(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, char const*, unsigned long)
Exiting

@onqtam
Copy link
Member

onqtam commented Sep 7, 2020

I just followed the steps (without the gtest-related ones) and it worked on my machine - first time getting msan to work!
First I did this:

# clone LLVM
git clone --depth=1 https://github.com/llvm/llvm-project
cd llvm-project
mkdir build; cd build
# configure cmake
cmake -GNinja ../llvm \
	-DCMAKE_BUILD_TYPE=Release \
	-DLLVM_ENABLE_PROJECTS="libcxx;libcxxabi" \
	-DCMAKE_C_COMPILER=clang \
	-DCMAKE_CXX_COMPILER=clang++ \
	-DLLVM_USE_SANITIZER=MemoryWithOrigins
# build the libraries
cmake --build . -- cxx cxxabi

Then I entered this: (notice how I've substituted /path/to/libcxx_msan/ from their documentation with /home/onqtam/llvm-project/build/)

MSAN_CFLAGS="-fsanitize=memory -stdlib=libc++ -L/home/onqtam/llvm-project/build/lib -lc++abi -I/home/onqtam/llvm-project/build/include -I/home/onqtam/llvm-project/build/include/c++/v1"

And after that I compiled this program:

#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include "doctest/doctest.h"
TEST_CASE("") {
    int a;
    CHECK(a == 1);
}

With this command (no need to pass the right include dirs to doctest because I include it properly in the .cpp file):

clang++ ${MSAN_CFLAGS} a.cpp -Wl,-rpath,/home/onqtam/llvm-project/build/lib

and running it gave me this:

onqtam@onqtam-ThinkPad-X1-Extreme:~/doctest$ ./a.out 
[doctest] doctest version is "2.4.0"
[doctest] run with "--help" for options
==10954==WARNING: MemorySanitizer: use-of-uninitialized-value
    #0 0x52f7f9 in doctest::detail::Result doctest::detail::Expression_lhs<int const&>::operator==<int>(int const&) (/home/onqtam/doctest/a.out+0x52f7f9)
    #1 0x4c69e7 in _DOCTEST_ANON_FUNC_8() (/home/onqtam/doctest/a.out+0x4c69e7)
    #2 0x4c1f6e in doctest::Context::run() (/home/onqtam/doctest/a.out+0x4c1f6e)
    #3 0x4c65b9 in main (/home/onqtam/doctest/a.out+0x4c65b9)
    #4 0x7fe41b5eb1e2 in __libc_start_main /build/glibc-5mDdLG/glibc-2.30/csu/../csu/libc-start.c:308:16
    #5 0x41fe6d in _start (/home/onqtam/doctest/a.out+0x41fe6d)

SUMMARY: MemorySanitizer: use-of-uninitialized-value (/home/onqtam/doctest/a.out+0x52f7f9) in doctest::detail::Result doctest::detail::Expression_lhs<int const&>::operator==<int>(int const&)
  ORIGIN: invalid (0). Might be a bug in MemorySanitizer origin tracking.
    This could still be a bug in your code, too!
Exiting

And when I initialized the variable a everything was fine. Hope this helps.

@AlexanderLanin
Copy link
Contributor Author

Thanks, I went through your steps and noticed I missed a crucial detail. The part about -Wl,-rpath, since they didn't put it inside of MSAN_CFLAGS.

Now it works fine for me. Thanks!

Here is what I ended up with:
CC=clang CXX=clang++ cmake .. -DCMAKE_CXX_FLAGS="-fsanitize=memory -stdlib=libc++ -L/home/alex/llvm/build-msan/lib -lc++abi -I/home/alex/llvm/build-msan/include -pthread -Wl,-rpath,/home/alex/llvm/build-msan-debug/lib" -DCMAKE_BUILD_TYPE=Debug -GNinja && ninja && examples/all_features/all_features

@onqtam
Copy link
Member

onqtam commented Sep 7, 2020

Glad it worked :)
I'll be closing this now. And maybe I should put it in the FAQ

@onqtam onqtam closed this as completed Sep 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants