LibreOffice Module bridges (master) 1
gcc3_macosx_x86-64/except.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#include <sal/config.h>
21
22#include <cassert>
23#include <new>
24#include <stdio.h>
25#include <string.h>
26#include <typeinfo>
27
28#include <cxxabi.h>
29#include <dlfcn.h>
30
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"
41
42using namespace ::osl;
43using namespace ::com::sun::star::uno;
44
45namespace CPPU_CURRENT_NAMESPACE {
46
47namespace {
48
49struct Fake_type_info {
50 virtual ~Fake_type_info() = delete;
51 char const * name;
52};
53
54struct Fake_class_type_info: Fake_type_info {
55 virtual ~Fake_class_type_info() override = delete;
56};
57
58struct Fake_si_class_type_info: Fake_class_type_info {
59 virtual ~Fake_si_class_type_info() override = delete;
60 void const * base;
61};
62
63struct Base {};
64struct Derived: Base {};
65
66std::type_info * createFake_class_type_info(char const * name) {
67 char * buf = new char[sizeof (Fake_class_type_info)];
68
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));
76}
77
78std::type_info * createFake_si_class_type_info(
79 char const * name, std::type_info const * base)
80{
81 char * buf = new char[sizeof (Fake_si_class_type_info)];
82
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));
92}
93
94}
95
96#ifdef __GNUC__
97#pragma GCC diagnostic push
98#pragma GCC diagnostic ignored "-Wunused-function"
99#endif
100void dummy_can_throw_anything( char const * )
101{
102}
103#ifdef __GNUC__
104#pragma GCC diagnostic pop
105#endif
106
107static OUString toUNOname( char const * p )
108{
109#if OSL_DEBUG_LEVEL > 1
110 char const * start = p;
111#endif
112
113 // example: N3com3sun4star4lang24IllegalArgumentExceptionE
114
115 OUStringBuffer buf( 64 );
116 assert( 'N' == *p );
117 ++p; // skip N
118
119 while ('E' != *p)
120 {
121 // read chars count
122 long n = *p++ - '0';
123 while ('0' <= *p && '9' >= *p)
124 {
125 n *= 10;
126 n += (*p++ - '0');
127 }
128 buf.appendAscii( p, n );
129 p += n;
130 if ('E' != *p)
131 buf.append( '.' );
132 }
133
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
142}
143
144namespace {
145
146class RTTI
147{
148 typedef std::unordered_map< OUString, std::type_info * > t_rtti_map;
149
150 Mutex m_mutex;
153
154 void * m_hApp;
155
156public:
157 RTTI();
158 ~RTTI();
159
160 std::type_info * getRTTI( typelib_CompoundTypeDescription * );
161};
162
163}
164
166 : m_hApp( dlopen( nullptr, RTLD_LAZY ) )
167{
168}
169
170RTTI::~RTTI()
171{
172 dlclose( m_hApp );
173}
174
175
176std::type_info * RTTI::getRTTI( typelib_CompoundTypeDescription *pTypeDescr )
177{
178 std::type_info * rtti;
179
180 OUString const & unoName = OUString::unacquired(&pTypeDescr->aBase.pTypeName);
181
182 MutexGuard guard( m_mutex );
183 t_rtti_map::const_iterator iFind( m_rttis.find( unoName ) );
184 if (iFind == m_rttis.end())
185 {
186 // RTTI symbol
187 OStringBuffer buf( 64 );
188 buf.append( "_ZTIN" );
189 sal_Int32 index = 0;
190 do
191 {
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 );
196 }
197 while (index >= 0);
198 buf.append( 'E' );
199
200 OString symName( buf.makeStringAndClear() );
201 rtti = static_cast<std::type_info *>(dlsym( m_hApp, symName.getStr() ));
202
203 if (rtti)
204 {
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" );
210 }
211 else
212 {
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())
216 {
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();
223 }
224#if OSL_DEBUG_LEVEL > 1
225 fprintf( stderr,"generated rtti for %s\n", rttiName );
226#endif
227 if (pTypeDescr->pBaseTypeDescription)
228 {
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);
233 }
234 else
235 {
236 rtti = createFake_class_type_info(rttiName);
237 }
238
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" );
244 }
245 else // taking already generated rtti
246 {
247 rtti = iFind2->second;
248 }
249 }
250 }
251 else
252 {
253 rtti = iFind->second;
254 }
255
256 return rtti;
257}
258
259
260static void deleteException( void * pExc )
261{
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
267 //
268 // __attribute__((aligned))
269 //
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);
286 }
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)
292 {
293 ::uno_destructData( pExc, pTD, cpp_release );
294 ::typelib_typedescription_release( pTD );
295 }
296}
297
298void raiseException( uno_Any * pUnoExc, uno_Mapping * pUno2Cpp )
299{
300#if OSL_DEBUG_LEVEL > 1
301 OString cstr(
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;
309
310 {
311 // construct cpp exception object
312 typelib_TypeDescription * pTypeDescr = nullptr;
313 TYPELIB_DANGER_GET( &pTypeDescr, pUnoExc->pType );
314 assert(pTypeDescr);
315 if (! pTypeDescr)
316 {
317 throw RuntimeException(
318 "cannot get typedescription for type " +
319 OUString::unacquired( &pUnoExc->pType->pTypeName ) );
320 }
321
322 pCppExc = __cxxabiv1::__cxa_allocate_exception( pTypeDescr->nSize );
323 ::uno_copyAndConvertData( pCppExc, pUnoExc->pData, pTypeDescr, pUno2Cpp );
324
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)
333 {
334 throw RuntimeException(
335 "no rtti for type " +
336 OUString::unacquired( &pUnoExc->pType->pTypeName ) );
337 }
338 }
339
341}
342
343void fillUnoException(uno_Any * pUnoExc, uno_Mapping * pCpp2Uno)
344{
345 __cxa_exception * header = __cxa_get_globals()->caughtExceptions;
346 if (! header)
347 {
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;
353 }
354
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
360 //
361 // [8 byte void *reserve]
362 // 8 byte size_t referenceCount
363 //
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
369 //
370 // 8 byte void* primaryException
371 //
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);
379 }
380
381 std::type_info *exceptionType = __cxxabiv1::__cxa_current_exception_type();
382
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)
391 {
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);
396 }
397 else
398 {
399 // construct uno exception any
400 uno_any_constructAndConvert( pUnoExc, header->adjustedPtr, pExcTypeDescr, pCpp2Uno );
401 typelib_typedescription_release( pExcTypeDescr );
402 }
403}
404
405}
406
407/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
constexpr sal_Int8 header[]
void SAL_CALL uno_type_any_constructAndConvert(uno_Any *pDest, void *pSource, typelib_TypeDescriptionReference *pType, uno_Mapping *mapping) SAL_THROW_EXTERN_C()
void SAL_CALL uno_any_constructAndConvert(uno_Any *pDest, void *pSource, typelib_TypeDescription *pTypeDescr, uno_Mapping *mapping) SAL_THROW_EXTERN_C()
std::type_info * getRTTI(typelib_CompoundTypeDescription *)
std::unordered_map< OUString, std::type_info *, OUStringHash > t_rtti_map
void const * base
t_rtti_map m_rttis
char const * name
t_rtti_map m_generatedRttis
CPPU_CURRENT_NAMESPACE::__cxa_eh_globals * __cxa_get_globals() noexcept
void * p
sal_Int64 n
#define SAL_WARN_IF(condition, area, stream)
#define SAL_WARN(area, stream)
struct _uno_Mapping uno_Mapping
Definition: msvc/except.hxx:33
struct _typelib_TypeDescription typelib_TypeDescription
Definition: msvc/except.hxx:53
struct _uno_Any uno_Any
Definition: msvc/except.hxx:32
static OUString toUNOname(OUString const &rRTTIname) noexcept
static void deleteException(void *pExc)
void dummy_can_throw_anything(char const *)
void fillUnoException(uno_Any *pUnoExc, uno_Mapping *pCpp2Uno)
static OUString toUNOname(char const *p)
void __cxa_throw(void *thrown_exception, std::type_info *tinfo, void(*dest)(void *)) __attribute__((noreturn))
void * __cxa_allocate_exception(size_t thrown_size)
std::type_info * __cxa_current_exception_type()
Type
index
OString OUStringToOString(std::u16string_view str, ConnectionSettings const *settings)
void SAL_CALL typelib_typedescription_release(typelib_TypeDescription *pTD) SAL_THROW_EXTERN_C()
void SAL_CALL typelib_typedescription_getByName(typelib_TypeDescription **ppRet, rtl_uString *pName) SAL_THROW_EXTERN_C()
Base