nss: upgrade to release 3.73
[LibreOffice.git] / bridges / source / cpp_uno / gcc3_macosx_x86-64 / except.cxx
blob60f5f6e40ef1e54c07899dd71761a1c2cc6a193a
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <sal/config.h>
22 #include <cassert>
23 #include <new>
24 #include <stdio.h>
25 #include <string.h>
26 #include <typeinfo>
28 #include <cxxabi.h>
29 #include <dlfcn.h>
31 #include <com/sun/star/uno/RuntimeException.hpp>
32 #include <com/sun/star/uno/genfunc.hxx>
33 #include <sal/log.hxx>
34 #include <osl/mutex.hxx>
35 #include <rtl/strbuf.hxx>
36 #include <rtl/ustrbuf.hxx>
37 #include <typelib/typedescription.h>
38 #include <uno/any2.h>
39 #include <unordered_map>
40 #include "share.hxx"
42 using namespace ::osl;
43 using namespace ::com::sun::star::uno;
45 namespace CPPU_CURRENT_NAMESPACE {
47 namespace {
49 struct Fake_type_info {
50 virtual ~Fake_type_info() = delete;
51 char const * name;
54 struct Fake_class_type_info: Fake_type_info {
55 virtual ~Fake_class_type_info() override = delete;
58 struct Fake_si_class_type_info: Fake_class_type_info {
59 virtual ~Fake_si_class_type_info() override = delete;
60 void const * base;
63 struct Base {};
64 struct Derived: Base {};
66 std::type_info * createFake_class_type_info(char const * name) {
67 char * buf = new char[sizeof (Fake_class_type_info)];
69 *reinterpret_cast<void **>(buf) = *reinterpret_cast<void * const *>(
70 &typeid(Base));
71 // copy __cxxabiv1::__class_type_info vtable into place
72 Fake_class_type_info * fake = reinterpret_cast<Fake_class_type_info *>(buf);
73 fake->name = name;
74 return reinterpret_cast<std::type_info *>(
75 static_cast<Fake_type_info *>(fake));
78 std::type_info * createFake_si_class_type_info(
79 char const * name, std::type_info const * base)
81 char * buf = new char[sizeof (Fake_si_class_type_info)];
83 *reinterpret_cast<void **>(buf) = *reinterpret_cast<void * const *>(
84 &typeid(Derived));
85 // copy __cxxabiv1::__si_class_type_info vtable into place
86 Fake_si_class_type_info * fake
87 = reinterpret_cast<Fake_si_class_type_info *>(buf);
88 fake->name = name;
89 fake->base = base;
90 return reinterpret_cast<std::type_info *>(
91 static_cast<Fake_type_info *>(fake));
96 #ifdef __GNUC__
97 #pragma GCC diagnostic push
98 #pragma GCC diagnostic ignored "-Wunused-function"
99 #endif
100 void dummy_can_throw_anything( char const * )
103 #ifdef __GNUC__
104 #pragma GCC diagnostic pop
105 #endif
107 static OUString toUNOname( char const * p )
109 #if OSL_DEBUG_LEVEL > 1
110 char const * start = p;
111 #endif
113 // example: N3com3sun4star4lang24IllegalArgumentExceptionE
115 OUStringBuffer buf( 64 );
116 assert( 'N' == *p );
117 ++p; // skip N
119 while ('E' != *p)
121 // read chars count
122 long n = *p++ - '0';
123 while ('0' <= *p && '9' >= *p)
125 n *= 10;
126 n += (*p++ - '0');
128 buf.appendAscii( p, n );
129 p += n;
130 if ('E' != *p)
131 buf.append( '.' );
134 #if OSL_DEBUG_LEVEL > 1
135 OUString ret( buf.makeStringAndClear() );
136 OString c_ret( OUStringToOString( ret, RTL_TEXTENCODING_ASCII_US ) );
137 fprintf( stderr, "> toUNOname(): %s => %s\n", start, c_ret.getStr() );
138 return ret;
139 #else
140 return buf.makeStringAndClear();
141 #endif
144 namespace {
146 class RTTI
148 typedef std::unordered_map< OUString, std::type_info * > t_rtti_map;
150 Mutex m_mutex;
151 t_rtti_map m_rttis;
152 t_rtti_map m_generatedRttis;
154 void * m_hApp;
156 public:
157 RTTI();
158 ~RTTI();
160 std::type_info * getRTTI( typelib_CompoundTypeDescription * );
165 RTTI::RTTI()
166 : m_hApp( dlopen( nullptr, RTLD_LAZY ) )
170 RTTI::~RTTI()
172 dlclose( m_hApp );
176 std::type_info * RTTI::getRTTI( typelib_CompoundTypeDescription *pTypeDescr )
178 std::type_info * rtti;
180 OUString const & unoName = OUString::unacquired(&pTypeDescr->aBase.pTypeName);
182 MutexGuard guard( m_mutex );
183 t_rtti_map::const_iterator iFind( m_rttis.find( unoName ) );
184 if (iFind == m_rttis.end())
186 // RTTI symbol
187 OStringBuffer buf( 64 );
188 buf.append( "_ZTIN" );
189 sal_Int32 index = 0;
192 OUString token( unoName.getToken( 0, '.', index ) );
193 buf.append( token.getLength() );
194 OString c_token( OUStringToOString( token, RTL_TEXTENCODING_ASCII_US ) );
195 buf.append( c_token );
197 while (index >= 0);
198 buf.append( 'E' );
200 OString symName( buf.makeStringAndClear() );
201 rtti = static_cast<std::type_info *>(dlsym( m_hApp, symName.getStr() ));
203 if (rtti)
205 std::pair< t_rtti_map::iterator, bool > insertion(
206 m_rttis.insert( t_rtti_map::value_type( unoName, rtti ) ) );
207 SAL_WARN_IF( !insertion.second,
208 "bridges",
209 "inserting new rtti failed" );
211 else
213 // try to lookup the symbol in the generated rtti map
214 t_rtti_map::const_iterator iFind2( m_generatedRttis.find( unoName ) );
215 if (iFind2 == m_generatedRttis.end())
217 // we must generate it !
218 // symbol and rtti-name is nearly identical,
219 // the symbol is prefixed with _ZTI
220 char * rttiName = strdup(symName.getStr() + 4);
221 if (rttiName == nullptr) {
222 throw std::bad_alloc();
224 #if OSL_DEBUG_LEVEL > 1
225 fprintf( stderr,"generated rtti for %s\n", rttiName );
226 #endif
227 if (pTypeDescr->pBaseTypeDescription)
229 // ensure availability of base
230 std::type_info * base_rtti = getRTTI(
231 pTypeDescr->pBaseTypeDescription );
232 rtti = createFake_si_class_type_info(rttiName, base_rtti);
234 else
236 rtti = createFake_class_type_info(rttiName);
239 std::pair< t_rtti_map::iterator, bool > insertion(
240 m_generatedRttis.insert( t_rtti_map::value_type( unoName, rtti ) ) );
241 SAL_WARN_IF( !insertion.second,
242 "bridges",
243 "inserting new generated rtti failed" );
245 else // taking already generated rtti
247 rtti = iFind2->second;
251 else
253 rtti = iFind->second;
256 return rtti;
260 static void deleteException( void * pExc )
262 __cxa_exception const * header = static_cast<__cxa_exception const *>(pExc) - 1;
263 // The libcxxabi commit
264 // <http://llvm.org/viewvc/llvm-project?view=revision&revision=303175>
265 // "[libcxxabi] Align unwindHeader on a double-word boundary" towards
266 // LLVM 5.0 changed the size of __cxa_exception by adding
268 // __attribute__((aligned))
270 // to the final member unwindHeader, on x86-64 effectively adding a hole of
271 // size 8 in front of that member (changing its offset from 88 to 96,
272 // sizeof(__cxa_exception) from 120 to 128, and alignof(__cxa_exception)
273 // from 8 to 16); a hack to dynamically determine whether we run against a
274 // LLVM 5 libcxxabi is to look at the exceptionDestructor member, which must
275 // point to this function (the use of __cxa_exception in fillUnoException is
276 // unaffected, as it only accesses members towards the start of the struct,
277 // through a pointer known to actually point at the start). The libcxxabi commit
278 // <https://github.com/llvm/llvm-project/commit/9ef1daa46edb80c47d0486148c0afc4e0d83ddcf>
279 // "Insert padding before the __cxa_exception header to ensure the thrown" in LLVM 6
280 // removes the need for this hack, so it can be removed again once we can be sure that we only
281 // run against libcxxabi from LLVM >= 6:
282 if (header->exceptionDestructor != &deleteException) {
283 header = reinterpret_cast<__cxa_exception const *>(
284 reinterpret_cast<char const *>(header) - 8);
285 assert(header->exceptionDestructor == &deleteException);
287 typelib_TypeDescription * pTD = nullptr;
288 OUString unoName( toUNOname( header->exceptionType->name() ) );
289 ::typelib_typedescription_getByName( &pTD, unoName.pData );
290 assert(pTD && "### unknown exception type! leaving out destruction => leaking!!!");
291 if (pTD)
293 ::uno_destructData( pExc, pTD, cpp_release );
294 ::typelib_typedescription_release( pTD );
298 void raiseException( uno_Any * pUnoExc, uno_Mapping * pUno2Cpp )
300 #if OSL_DEBUG_LEVEL > 1
301 OString cstr(
302 OUStringToOString(
303 OUString::unacquired( &pUnoExc->pType->pTypeName ),
304 RTL_TEXTENCODING_ASCII_US ) );
305 fprintf( stderr, "> uno exception occurred: %s\n", cstr.getStr() );
306 #endif
307 void * pCppExc;
308 std::type_info * rtti;
311 // construct cpp exception object
312 typelib_TypeDescription * pTypeDescr = nullptr;
313 TYPELIB_DANGER_GET( &pTypeDescr, pUnoExc->pType );
314 assert(pTypeDescr);
315 if (! pTypeDescr)
317 throw RuntimeException(
318 "cannot get typedescription for type " +
319 OUString::unacquired( &pUnoExc->pType->pTypeName ) );
322 pCppExc = __cxxabiv1::__cxa_allocate_exception( pTypeDescr->nSize );
323 ::uno_copyAndConvertData( pCppExc, pUnoExc->pData, pTypeDescr, pUno2Cpp );
325 // destruct uno exception
326 ::uno_any_destruct( pUnoExc, nullptr );
327 // avoiding locked counts
328 static RTTI rtti_data;
329 rtti = rtti_data.getRTTI(reinterpret_cast<typelib_CompoundTypeDescription*>(pTypeDescr));
330 TYPELIB_DANGER_RELEASE( pTypeDescr );
331 assert(rtti && "### no rtti for throwing exception!");
332 if (! rtti)
334 throw RuntimeException(
335 "no rtti for type " +
336 OUString::unacquired( &pUnoExc->pType->pTypeName ) );
340 __cxxabiv1::__cxa_throw( pCppExc, rtti, deleteException );
343 void fillUnoException(uno_Any * pUnoExc, uno_Mapping * pCpp2Uno)
345 __cxa_exception * header = __cxa_get_globals()->caughtExceptions;
346 if (! header)
348 RuntimeException aRE( "no exception header!" );
349 Type const & rType = cppu::UnoType<decltype(aRE)>::get();
350 uno_type_any_constructAndConvert( pUnoExc, &aRE, rType.getTypeLibType(), pCpp2Uno );
351 SAL_WARN("bridges", aRE.Message);
352 return;
355 // Very bad HACK to find out whether we run against a libcxxabi that has a new
356 // __cxa_exception::reserved member at the start, introduced with LLVM 10
357 // <https://github.com/llvm/llvm-project/commit/674ec1eb16678b8addc02a4b0534ab383d22fa77>
358 // "[libcxxabi] Insert padding in __cxa_exception struct for compatibility". The layout of the
359 // start of __cxa_exception is
361 // [8 byte void *reserve]
362 // 8 byte size_t referenceCount
364 // where the (bad, hacky) assumption is that reserve (if present) is null
365 // (__cxa_allocate_exception in at least LLVM 11 zero-fills the object, and nothing actively
366 // sets reserve) while referenceCount is non-null (__cxa_throw sets it to 1, and
367 // __cxa_decrement_exception_refcount destroys the exception as soon as it drops to 0; for a
368 // __cxa_dependent_exception, the referenceCount member is rather
370 // 8 byte void* primaryException
372 // but which also will always be set to a non-null value in __cxa_rethrow_primary_exception).
373 // As described in the definition of __cxa_exception
374 // (bridges/source/cpp_uno/gcc3_macosx_x86-64/share.hxx), this hack (together with the "#if 0"
375 // there) can be dropped once we can be sure that we only run against new libcxxabi that has the
376 // reserve member:
377 if (*reinterpret_cast<void **>(header) == nullptr) {
378 header = reinterpret_cast<__cxa_exception *>(reinterpret_cast<void **>(header) + 1);
381 std::type_info *exceptionType = __cxxabiv1::__cxa_current_exception_type();
383 typelib_TypeDescription * pExcTypeDescr = nullptr;
384 OUString unoName( toUNOname( exceptionType->name() ) );
385 #if OSL_DEBUG_LEVEL > 1
386 OString cstr_unoName( OUStringToOString( unoName, RTL_TEXTENCODING_ASCII_US ) );
387 fprintf( stderr, "> c++ exception occurred: %s\n", cstr_unoName.getStr() );
388 #endif
389 typelib_typedescription_getByName( &pExcTypeDescr, unoName.pData );
390 if (nullptr == pExcTypeDescr)
392 RuntimeException aRE( "exception type not found: " + unoName );
393 Type const & rType = cppu::UnoType<decltype(aRE)>::get();
394 uno_type_any_constructAndConvert( pUnoExc, &aRE, rType.getTypeLibType(), pCpp2Uno );
395 SAL_WARN("bridges", aRE.Message);
397 else
399 // construct uno exception any
400 uno_any_constructAndConvert( pUnoExc, header->adjustedPtr, pExcTypeDescr, pCpp2Uno );
401 typelib_typedescription_release( pExcTypeDescr );
407 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */