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 // new 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):
274 if (header
->exceptionDestructor
!= &deleteException
) {
275 header
= reinterpret_cast<__cxa_exception
const *>(
276 reinterpret_cast<char const *>(header
) - 8);
277 assert(header
->exceptionDestructor
== &deleteException
);
279 typelib_TypeDescription
* pTD
= nullptr;
280 OUString
unoName( toUNOname( header
->exceptionType
->name() ) );
281 ::typelib_typedescription_getByName( &pTD
, unoName
.pData
);
282 assert(pTD
&& "### unknown exception type! leaving out destruction => leaking!!!");
285 ::uno_destructData( pExc
, pTD
, cpp_release
);
286 ::typelib_typedescription_release( pTD
);
290 void raiseException( uno_Any
* pUnoExc
, uno_Mapping
* pUno2Cpp
)
292 #if OSL_DEBUG_LEVEL > 1
295 OUString::unacquired( &pUnoExc
->pType
->pTypeName
),
296 RTL_TEXTENCODING_ASCII_US
) );
297 fprintf( stderr
, "> uno exception occurred: %s\n", cstr
.getStr() );
300 std::type_info
* rtti
;
303 // construct cpp exception object
304 typelib_TypeDescription
* pTypeDescr
= nullptr;
305 TYPELIB_DANGER_GET( &pTypeDescr
, pUnoExc
->pType
);
309 throw RuntimeException(
310 "cannot get typedescription for type " +
311 OUString::unacquired( &pUnoExc
->pType
->pTypeName
) );
314 pCppExc
= __cxxabiv1::__cxa_allocate_exception( pTypeDescr
->nSize
);
315 ::uno_copyAndConvertData( pCppExc
, pUnoExc
->pData
, pTypeDescr
, pUno2Cpp
);
317 // destruct uno exception
318 ::uno_any_destruct( pUnoExc
, nullptr );
319 // avoiding locked counts
320 static RTTI rtti_data
;
321 rtti
= rtti_data
.getRTTI(reinterpret_cast<typelib_CompoundTypeDescription
*>(pTypeDescr
));
322 TYPELIB_DANGER_RELEASE( pTypeDescr
);
323 assert(rtti
&& "### no rtti for throwing exception!");
326 throw RuntimeException(
327 "no rtti for type " +
328 OUString::unacquired( &pUnoExc
->pType
->pTypeName
) );
332 // void __cxa_throw(void* thrown_exception,
333 // struct std::type_info * tinfo,
334 // void (*dest)(void*));
335 __cxxabiv1::__cxa_throw( pCppExc
, rtti
, deleteException
);
338 void fillUnoException(uno_Any
* pUnoExc
, uno_Mapping
* pCpp2Uno
)
340 __cxa_exception
* header
= __cxxabiv1::__cxa_get_globals()->caughtExceptions
;
343 RuntimeException
aRE( "no exception header!" );
344 Type
const & rType
= cppu::UnoType
<decltype(aRE
)>::get();
345 uno_type_any_constructAndConvert( pUnoExc
, &aRE
, rType
.getTypeLibType(), pCpp2Uno
);
346 SAL_WARN("bridges", aRE
.Message
);
350 std::type_info
*exceptionType
= __cxxabiv1::__cxa_current_exception_type();
352 typelib_TypeDescription
* pExcTypeDescr
= nullptr;
353 OUString
unoName( toUNOname( exceptionType
->name() ) );
354 #if OSL_DEBUG_LEVEL > 1
355 OString
cstr_unoName( OUStringToOString( unoName
, RTL_TEXTENCODING_ASCII_US
) );
356 fprintf( stderr
, "> c++ exception occurred: %s\n", cstr_unoName
.getStr() );
358 typelib_typedescription_getByName( &pExcTypeDescr
, unoName
.pData
);
359 if (nullptr == pExcTypeDescr
)
361 RuntimeException
aRE( "exception type not found: " + unoName
);
362 Type
const & rType
= cppu::UnoType
<decltype(aRE
)>::get();
363 uno_type_any_constructAndConvert( pUnoExc
, &aRE
, rType
.getTypeLibType(), pCpp2Uno
);
364 SAL_WARN("bridges", aRE
.Message
);
368 // construct uno exception any
369 uno_any_constructAndConvert( pUnoExc
, header
->adjustedPtr
, pExcTypeDescr
, pCpp2Uno
);
370 typelib_typedescription_release( pExcTypeDescr
);
376 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */