1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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"
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"
39 #include <unordered_map>
42 using namespace ::osl
;
43 using namespace ::com::sun::star::uno
;
45 namespace CPPU_CURRENT_NAMESPACE
{
49 struct Fake_type_info
{
50 virtual ~Fake_type_info() = delete;
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;
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 *>(
71 // copy __cxxabiv1::__class_type_info vtable into place
72 Fake_class_type_info
* fake
= reinterpret_cast<Fake_class_type_info
*>(buf
);
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 *>(
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
);
90 return reinterpret_cast<std::type_info
*>(
91 static_cast<Fake_type_info
*>(fake
));
97 #pragma GCC diagnostic push
98 #pragma GCC diagnostic ignored "-Wunused-function"
100 void dummy_can_throw_anything( char const * )
104 #pragma GCC diagnostic pop
107 static OUString
toUNOname( char const * p
)
109 #if OSL_DEBUG_LEVEL > 1
110 char const * start
= p
;
113 // example: N3com3sun4star4lang24IllegalArgumentExceptionE
115 OUStringBuffer
buf( 64 );
122 long n
= (*p
++ - '0');
123 while ('0' <= *p
&& '9' >= *p
)
128 buf
.appendAscii( p
, n
);
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() );
140 return buf
.makeStringAndClear();
146 typedef std::unordered_map
< OUString
, std::type_info
*, OUStringHash
> t_rtti_map
;
150 t_rtti_map m_generatedRttis
;
158 std::type_info
* getRTTI( typelib_CompoundTypeDescription
* );
162 : m_hApp( dlopen( nullptr, RTLD_LAZY
) )
172 std::type_info
* RTTI::getRTTI( typelib_CompoundTypeDescription
*pTypeDescr
)
174 std::type_info
* rtti
;
176 OUString
const & unoName
= OUString::unacquired(&pTypeDescr
->aBase
.pTypeName
);
178 MutexGuard
guard( m_mutex
);
179 t_rtti_map::const_iterator
iFind( m_rttis
.find( unoName
) );
180 if (iFind
== m_rttis
.end())
183 OStringBuffer
buf( 64 );
184 buf
.append( "_ZTIN" );
188 OUString
token( unoName
.getToken( 0, '.', index
) );
189 buf
.append( token
.getLength() );
190 OString
c_token( OUStringToOString( token
, RTL_TEXTENCODING_ASCII_US
) );
191 buf
.append( c_token
);
196 OString
symName( buf
.makeStringAndClear() );
197 rtti
= static_cast<std::type_info
*>(dlsym( m_hApp
, symName
.getStr() ));
201 std::pair
< t_rtti_map::iterator
, bool > insertion(
202 m_rttis
.insert( t_rtti_map::value_type( unoName
, rtti
) ) );
203 SAL_WARN_IF( !insertion
.second
,
205 "inserting new rtti failed" );
209 // try to lookup the symbol in the generated rtti map
210 t_rtti_map::const_iterator
iFind2( m_generatedRttis
.find( unoName
) );
211 if (iFind2
== m_generatedRttis
.end())
213 // we must generate it !
214 // symbol and rtti-name is nearly identical,
215 // the symbol is prefixed with _ZTI
216 char * rttiName
= strdup(symName
.getStr() + 4);
217 if (rttiName
== nullptr) {
218 throw std::bad_alloc();
220 #if OSL_DEBUG_LEVEL > 1
221 fprintf( stderr
,"generated rtti for %s\n", rttiName
);
223 if (pTypeDescr
->pBaseTypeDescription
)
225 // ensure availability of base
226 std::type_info
* base_rtti
= getRTTI(
227 pTypeDescr
->pBaseTypeDescription
);
228 rtti
= createFake_si_class_type_info(rttiName
, base_rtti
);
232 rtti
= createFake_class_type_info(rttiName
);
235 std::pair
< t_rtti_map::iterator
, bool > insertion(
236 m_generatedRttis
.insert( t_rtti_map::value_type( unoName
, rtti
) ) );
237 SAL_WARN_IF( !insertion
.second
,
239 "inserting new generated rtti failed" );
241 else // taking already generated rtti
243 rtti
= iFind2
->second
;
249 rtti
= iFind
->second
;
256 static void deleteException( void * pExc
)
258 __cxa_exception
const * header
= static_cast<__cxa_exception
const *>(pExc
) - 1;
259 // The libcxxabi commit
260 // <http://llvm.org/viewvc/llvm-project?view=revision&revision=303175>
261 // "[libcxxabi] Align unwindHeader on a double-word boundary" towards
262 // LLVM 5.0 changed the size of __cxa_exception by adding
264 // __attribute__((aligned))
266 // to the final member unwindHeader, on x86-64 effectively adding a hole of
267 // size 8 in front of that member (changing its offset from 88 to 96,
268 // sizeof(__cxa_exception) from 120 to 128, and alignof(__cxa_exception)
269 // from 8 to 16); a hack to dynamically determine whether we run against a
270 // LLVM 5 libcxxabi is to look at the exceptionDestructor member, which must
271 // point to this function (the use of __cxa_exception in fillUnoException is
272 // unaffected, as it only accesses members towards the start of the struct,
273 // through a pointer known to actually point at the start). The libcxxabi commit
274 // <https://github.com/llvm/llvm-project/commit/9ef1daa46edb80c47d0486148c0afc4e0d83ddcf>
275 // "Insert padding before the __cxa_exception header to ensure the thrown" in LLVM 6
276 // removes the need for this hack, so it can be removed again once we can be sure that we only
277 // run against libcxxabi from LLVM >= 6:
278 if (header
->exceptionDestructor
!= &deleteException
) {
279 header
= reinterpret_cast<__cxa_exception
const *>(
280 reinterpret_cast<char const *>(header
) - 8);
281 assert(header
->exceptionDestructor
== &deleteException
);
283 typelib_TypeDescription
* pTD
= nullptr;
284 OUString
unoName( toUNOname( header
->exceptionType
->name() ) );
285 ::typelib_typedescription_getByName( &pTD
, unoName
.pData
);
286 assert(pTD
&& "### unknown exception type! leaving out destruction => leaking!!!");
289 ::uno_destructData( pExc
, pTD
, cpp_release
);
290 ::typelib_typedescription_release( pTD
);
294 void raiseException( uno_Any
* pUnoExc
, uno_Mapping
* pUno2Cpp
)
296 #if OSL_DEBUG_LEVEL > 1
299 OUString::unacquired( &pUnoExc
->pType
->pTypeName
),
300 RTL_TEXTENCODING_ASCII_US
) );
301 fprintf( stderr
, "> uno exception occurred: %s\n", cstr
.getStr() );
304 std::type_info
* rtti
;
307 // construct cpp exception object
308 typelib_TypeDescription
* pTypeDescr
= nullptr;
309 TYPELIB_DANGER_GET( &pTypeDescr
, pUnoExc
->pType
);
313 throw RuntimeException(
314 "cannot get typedescription for type " +
315 OUString::unacquired( &pUnoExc
->pType
->pTypeName
) );
318 pCppExc
= __cxxabiv1::__cxa_allocate_exception( pTypeDescr
->nSize
);
319 ::uno_copyAndConvertData( pCppExc
, pUnoExc
->pData
, pTypeDescr
, pUno2Cpp
);
321 // destruct uno exception
322 ::uno_any_destruct( pUnoExc
, nullptr );
323 // avoiding locked counts
324 static RTTI rtti_data
;
325 rtti
= rtti_data
.getRTTI(reinterpret_cast<typelib_CompoundTypeDescription
*>(pTypeDescr
));
326 TYPELIB_DANGER_RELEASE( pTypeDescr
);
327 assert(rtti
&& "### no rtti for throwing exception!");
330 throw RuntimeException(
331 "no rtti for type " +
332 OUString::unacquired( &pUnoExc
->pType
->pTypeName
) );
336 // void __cxa_throw(void* thrown_exception,
337 // struct std::type_info * tinfo,
338 // void (*dest)(void*));
339 __cxxabiv1::__cxa_throw( pCppExc
, rtti
, deleteException
);
342 void fillUnoException(uno_Any
* pUnoExc
, uno_Mapping
* pCpp2Uno
)
344 __cxa_exception
* header
= __cxxabiv1::__cxa_get_globals()->caughtExceptions
;
347 RuntimeException
aRE( "no exception header!" );
348 Type
const & rType
= cppu::UnoType
<decltype(aRE
)>::get();
349 uno_type_any_constructAndConvert( pUnoExc
, &aRE
, rType
.getTypeLibType(), pCpp2Uno
);
350 SAL_WARN("bridges", aRE
.Message
);
354 // Very bad HACK to find out whether we run against a libcxxabi that has a new
355 // __cxa_exception::reserved member at the start, introduced with LLVM 10
356 // <https://github.com/llvm/llvm-project/commit/674ec1eb16678b8addc02a4b0534ab383d22fa77>
357 // "[libcxxabi] Insert padding in __cxa_exception struct for compatibility". The layout of the
358 // start of __cxa_exception is
360 // [8 byte void *reserve]
361 // 8 byte size_t referenceCount
363 // where the (bad, hacky) assumption is that reserve (if present) is null
364 // (__cxa_allocate_exception in at least LLVM 11 zero-fills the object, and nothing actively
365 // sets reserve) while referenceCount is non-null (__cxa_throw sets it to 1, and
366 // __cxa_decrement_exception_refcount destroys the exception as soon as it drops to 0; for a
367 // __cxa_dependent_exception, the referenceCount member is rather
369 // 8 byte void* primaryException
371 // but which also will always be set to a non-null value in __cxa_rethrow_primary_exception).
372 // As described in the definition of __cxa_exception
373 // (bridges/source/cpp_uno/gcc3_macosx_x86-64/share.hxx), this hack (together with the "#if 0"
374 // there) can be dropped once we can be sure that we only run against new libcxxabi that has the
376 if (*reinterpret_cast<void **>(header
) == nullptr) {
377 header
= reinterpret_cast<__cxa_exception
*>(reinterpret_cast<void **>(header
) + 1);
380 std::type_info
*exceptionType
= __cxxabiv1::__cxa_current_exception_type();
382 typelib_TypeDescription
* pExcTypeDescr
= nullptr;
383 OUString
unoName( toUNOname( exceptionType
->name() ) );
384 #if OSL_DEBUG_LEVEL > 1
385 OString
cstr_unoName( OUStringToOString( unoName
, RTL_TEXTENCODING_ASCII_US
) );
386 fprintf( stderr
, "> c++ exception occurred: %s\n", cstr_unoName
.getStr() );
388 typelib_typedescription_getByName( &pExcTypeDescr
, unoName
.pData
);
389 if (nullptr == pExcTypeDescr
)
391 RuntimeException
aRE( "exception type not found: " + unoName
);
392 Type
const & rType
= cppu::UnoType
<decltype(aRE
)>::get();
393 uno_type_any_constructAndConvert( pUnoExc
, &aRE
, rType
.getTypeLibType(), pCpp2Uno
);
394 SAL_WARN("bridges", aRE
.Message
);
398 // construct uno exception any
399 uno_any_constructAndConvert( pUnoExc
, header
->adjustedPtr
, pExcTypeDescr
, pCpp2Uno
);
400 typelib_typedescription_release( pExcTypeDescr
);
406 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */