dimanche 21 juillet 2019

crash with char* exception in async

I'm experiencing a crash when compiling with clang++ the folowing code:

#include <future>
#include <iostream>

int main() {
    auto job = [] { throw "failed"; };

    auto f = std::async(std::launch::deferred, job );

    try {
        f.get();
    } catch (const char *&e) {
        std::cout << "boom! " << e << '\n';
    }
}

I must say I don't see anything bad with this, and compiling with g++ and passing valgrind on the resutling binary is ok.

That said, when compiling with clang:

clang++ sample.cpp -pthread -Wall -Werror

I get no error, but valgrind complains:

==10241== Invalid read of size 8
==10241==    at 0x401E74: main (in /tmp/a.out)
==10241==  Address 0x5d67e90 is 0 bytes after a block of size 112 alloc'd
==10241==    at 0x4C2EE3B: malloc (vg_replace_malloc.c:309)
==10241==    by 0x4ECCE31: __cxa_allocate_dependent_exception (in /usr/lib64/libstdc++.so.6.0.25)
==10241==    by 0x4ECDF46: std::rethrow_exception(std::__exception_ptr::exception_ptr) (in /usr/lib64/libstdc++.so.6.0.25)
==10241==    by 0x405075: std::__basic_future<void>::_M_get_result() const (in /tmp/a.out)
==10241==    by 0x404F6C: std::future<void>::get() (in /tmp/a.out)
==10241==    by 0x401E10: main (in /tmp/a.out)
==10241== 
boom! ==10241== 

And when I compile using libc++, the binary crashes:

clang++ -stdlib=libc++ sample.cpp -pthread -Wall -Werror

$ ./a.out 
Segmentation fault (core dumped)

And valgrind says:

==10334== 
==10334== Invalid read of size 8
==10334==    at 0x401834: main (in /tmp/a.out)
==10334==  Address 0x60c02a0 is 0 bytes after a block of size 128 alloc'd
==10334==    at 0x4C3147C: memalign (vg_replace_malloc.c:908)
==10334==    by 0x4C31589: posix_memalign (vg_replace_malloc.c:1072)
==10334==    by 0x5121AA0: ??? (in /usr/lib64/libc++abi.so.1.0)
==10334==    by 0x5123BFA: __cxa_rethrow_primary_exception (in /usr/lib64/libc++abi.so.1.0)
==10334==    by 0x4E7D678: std::rethrow_exception(std::exception_ptr) (in /usr/lib64/libc++.so.1.0)
==10334==    by 0x4E7E0F4: std::__1::__assoc_sub_state::copy() (in /usr/lib64/libc++.so.1.0)
==10334==    by 0x4E7E425: std::__1::future<void>::get() (in /usr/lib64/libc++.so.1.0)
==10334==    by 0x4017D0: main (in /tmp/a.out)
==10334== 
==10334== Invalid read of size 1
==10334==    at 0x4C32256: strlen (vg_replace_strmem.c:461)
==10334==    by 0x403DA4: std::__1::char_traits<char>::length(char const*) (in /tmp/a.out)
==10334==    by 0x40373B: 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*) (in /tmp/a.out)
==10334==    by 0x40183F: main (in /tmp/a.out)
==10334==  Address 0x0 is not stack'd, malloc'd or (recently) free'd
==10334== 
==10334== 
==10334== Process terminating with default action of signal 11 (SIGSEGV): dumping core
==10334==  Access not within mapped region at address 0x0
==10334==    at 0x4C32256: strlen (vg_replace_strmem.c:461)
==10334==    by 0x403DA4: std::__1::char_traits<char>::length(char const*) (in /tmp/a.out)
==10334==    by 0x40373B: 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*) (in /tmp/a.out)
==10334==    by 0x40183F: main (in /tmp/a.out)
==10334==  If you believe this happened as a result of a stack
==10334==  overflow in your program's main thread (unlikely but
==10334==  possible), you can try to increase the size of the
==10334==  main thread stack using the --main-stacksize= flag.
==10334==  The main thread stack size used in this run was 8388608.
boom! ==10334== 

What is wrong in here? Is it ok to use const char * exception in async?

additional info:

$ clang++ --version
clang version 6.0.1 (tags/RELEASE_601/final)

Aucun commentaire:

Enregistrer un commentaire