1
0
mirror of https://git.FreeBSD.org/ports.git synced 2024-10-19 19:59:43 +00:00

Fix crash when libcxxrt exceptions been freed by a different language runtime,

and when libcxxrt itself free foreign exceptions.

Unwind API was specifically designed to allow mixed language environments where
exceptions from different runtimes interoperate. One particular example is
C++/LuaJit combination. Due to the issues in libcxxrt, databases/tarantool crashes
when linked against libcxxrt.

The issue was merged by upstream butat the moment not officially released.
https://github.com/pathscale/libcxxrt/pull/32

PR:		204786
Submitted by:	Nick Zavaritsky <mejedi@gmail.com>
Approved by:	Mahdi Mokhtari <mokhi64@gmail.com> (maintainer)
This commit is contained in:
Rodrigo Osorio 2016-05-31 20:07:21 +00:00
parent 03ecb47a77
commit ff3160fad3
Notes: svn2git 2021-03-31 03:12:20 +00:00
svn path=/head/; revision=416217
5 changed files with 203 additions and 2 deletions

View File

@ -2,7 +2,7 @@
PORTNAME= libcxxrt
PORTVERSION= 20131225
PORTREVISION= 2
PORTREVISION= 3
CATEGORIES= devel
MAINTAINER= mokhi64@gmail.com
@ -15,6 +15,7 @@ GH_ACCOUNT= pathscale
GH_TAGNAME= 2f150a6
USES= cmake compiler:c++11-lang
USE_LDCONFIG= yes
CXXFLAGS+= -nostdlib
do-install:

View File

@ -0,0 +1,48 @@
--- src/exception.cc.orig 2013-12-26 03:11:27 UTC
+++ src/exception.cc
@@ -304,13 +304,17 @@ static pthread_key_t eh_key;
static void exception_cleanup(_Unwind_Reason_Code reason,
struct _Unwind_Exception *ex)
{
- __cxa_free_exception(static_cast<void*>(ex));
+ // Exception layout:
+ // [__cxa_exception [_Unwind_Exception]] [exception object]
+ //
+ // __cxa_free_exception expects a pointer to the exception object
+ __cxa_free_exception(static_cast<void*>(ex + 1));
}
static void dependent_exception_cleanup(_Unwind_Reason_Code reason,
struct _Unwind_Exception *ex)
{
- __cxa_free_dependent_exception(static_cast<void*>(ex));
+ __cxa_free_dependent_exception(static_cast<void*>(ex + 1));
}
/**
@@ -340,7 +344,8 @@ static void thread_cleanup(void* thread_
if (info->foreign_exception_state != __cxa_thread_info::none)
{
_Unwind_Exception *e = reinterpret_cast<_Unwind_Exception*>(info->globals.caughtExceptions);
- e->exception_cleanup(_URC_FOREIGN_EXCEPTION_CAUGHT, e);
+ if (e->exception_cleanup)
+ e->exception_cleanup(_URC_FOREIGN_EXCEPTION_CAUGHT, e);
}
else
{
@@ -1270,12 +1275,13 @@ extern "C" void __cxa_end_catch()
if (ti->foreign_exception_state != __cxa_thread_info::none)
{
- globals->caughtExceptions = 0;
if (ti->foreign_exception_state != __cxa_thread_info::rethrown)
{
_Unwind_Exception *e = reinterpret_cast<_Unwind_Exception*>(ti->globals.caughtExceptions);
- e->exception_cleanup(_URC_FOREIGN_EXCEPTION_CAUGHT, e);
+ if (e->exception_cleanup)
+ e->exception_cleanup(_URC_FOREIGN_EXCEPTION_CAUGHT, e);
}
+ globals->caughtExceptions = 0;
ti->foreign_exception_state = __cxa_thread_info::none;
return;
}

View File

@ -0,0 +1,24 @@
--- test/CMakeLists.txt.orig 2013-12-26 03:11:27 UTC
+++ test/CMakeLists.txt
@@ -23,6 +23,11 @@ add_executable(cxxrt-test-shared ${CXXTE
set_property(TARGET cxxrt-test-shared PROPERTY LINK_FLAGS -nodefaultlibs)
target_link_libraries(cxxrt-test-shared cxxrt-shared pthread dl c)
+include_directories(${CMAKE_SOURCE_DIR}/src)
+add_executable(cxxrt-test-foreign-exceptions test_foreign_exceptions.cc)
+set_property(TARGET cxxrt-test-foreign-exceptions PROPERTY LINK_FLAGS "-nodefaultlibs -Wl,--wrap,_Unwind_RaiseException")
+target_link_libraries(cxxrt-test-foreign-exceptions cxxrt-static gcc_s pthread dl c)
+
add_test(cxxrt-test-static-test
${CMAKE_CURRENT_SOURCE_DIR}/run_test.sh
${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/cxxrt-test-static
@@ -35,6 +40,9 @@ add_test(cxxrt-test-shared-test
${CMAKE_CURRENT_BINARY_DIR}/expected_output.log
${CMAKE_CURRENT_BINARY_DIR}/test-shared-output.log)
+add_test(cxxrt-test-foreign-exceptions
+ ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/cxxrt-test-foreign-exceptions)
+
set(valgrind "valgrind -q")
if(TEST_VALGRIND)

View File

@ -0,0 +1,128 @@
--- test/test_foreign_exceptions.cc.orig 2016-05-29 13:30:15 UTC
+++ test/test_foreign_exceptions.cc
@@ -0,0 +1,125 @@
+#include <cstdio>
+#include <cstdlib>
+#include "unwind.h"
+
+#define EXCEPTION_CLASS(a,b,c,d,e,f,g,h) \
+ ((static_cast<uint64_t>(a) << 56) +\
+ (static_cast<uint64_t>(b) << 48) +\
+ (static_cast<uint64_t>(c) << 40) +\
+ (static_cast<uint64_t>(d) << 32) +\
+ (static_cast<uint64_t>(e) << 24) +\
+ (static_cast<uint64_t>(f) << 16) +\
+ (static_cast<uint64_t>(g) << 8) +\
+ (static_cast<uint64_t>(h)))
+
+// using ld --wrap=_Unwind_RaiseException hook feature
+extern "C" _Unwind_Reason_Code __real__Unwind_RaiseException (_Unwind_Exception *e);
+extern "C" _Unwind_Reason_Code __wrap__Unwind_RaiseException (_Unwind_Exception *e);
+
+extern "C" _Unwind_Reason_Code __wrap__Unwind_RaiseException (_Unwind_Exception *e)
+{
+ // clobber exception class forcing libcxx own exceptions to be treated
+ // as foreign exception within libcxx itself
+ e->exception_class = EXCEPTION_CLASS('F','O','R','E','I','G','N','\0');
+ __real__Unwind_RaiseException(e);
+}
+
+_Unwind_Exception global_e;
+
+enum test_status {
+ PENDING, PASSED, FAILED
+};
+
+const char test_status_str[][8] = {
+ "PENDING", "PASSED", "FAILED"
+};
+
+test_status test1_status = PENDING;
+test_status test2_status = PENDING;
+test_status test3_status = PENDING;
+
+void test2_exception_cleanup(_Unwind_Reason_Code code, _Unwind_Exception *e)
+{
+ fputs("(2) exception_cleanup called\n", stderr);
+ if (e != &global_e) {
+ fprintf(stderr, "(2) ERROR: unexpected ptr: expecting %p, got %p\n", &global_e, e);
+ test2_status = FAILED;
+ }
+ if (test2_status == PENDING)
+ test2_status = PASSED;
+}
+
+struct test3_exception
+{
+ static int counter;
+ ~test3_exception()
+ {
+ counter++;
+ fputs("(3) exception dtor\n", stderr);
+ }
+};
+int test3_exception::counter = 0;
+
+int main()
+{
+ ///////////////////////////////////////////////////////////////
+ fputs("(1) foreign exception, exception_cleanup=nullptr\n", stderr);
+ try
+ {
+ global_e.exception_class = 0;
+ global_e.exception_cleanup = 0;
+ __real__Unwind_RaiseException(&global_e);
+ }
+ catch (...)
+ {
+ }
+ test1_status = PASSED;
+ fputs("(1) PASS\n", stderr);
+
+ ///////////////////////////////////////////////////////////////
+ fputs("(2) foreign exception, exception_cleanup present\n", stderr);
+ try
+ {
+ global_e.exception_class = 0;
+ global_e.exception_cleanup = test2_exception_cleanup;
+ __real__Unwind_RaiseException(&global_e);
+ }
+ catch (...)
+ {
+ }
+ fprintf(stderr, "(2) %s\n", test_status_str[test2_status]);
+
+ ///////////////////////////////////////////////////////////////
+ fputs("(3) C++ exception in foreign environment\n", stderr);
+ int counter_expected;
+ try
+ {
+ // throw was rigged such that the runtime treats C++ exceptions
+ // as foreign ones
+ throw test3_exception();
+ }
+ catch (test3_exception&)
+ {
+ fputs("(3) ERROR: wrong catch\n", stderr);
+ test3_status = FAILED;
+ }
+ catch (...)
+ {
+ fputs("(3) catch(...)\n", stderr);
+ counter_expected = test3_exception::counter + 1;
+ // one more dtor immediately after we leave catch
+ }
+ if (test3_status == PENDING && test3_exception::counter != counter_expected) {
+ fputs("(3) ERROR: exception dtor didn't run\n", stderr);
+ test3_status = FAILED;
+ }
+ if (test3_status == PENDING)
+ test3_status = PASSED;
+ fprintf(stderr, "(3) %s\n", test_status_str[test3_status]);
+
+ ///////////////////////////////////////////////////////////////
+ if (test1_status == PASSED && test2_status == PASSED && test3_status == PASSED)
+ return EXIT_SUCCESS;
+ else
+ return EXIT_FAILURE;
+}

View File

@ -1,6 +1,6 @@
This library implements the Code Sourcery C++ ABI, as documented here:
http://www.codesourcery.com/public/cxx-abi/abi.html
WWW: http://www.codesourcery.com/public/cxx-abi/abi.html
It is intended to sit below an STL implementation, and provide features required
by the compiler for implementation of the C++ language.