LibreOffice Module bridges (master) 1
gcc3_ios/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
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
144class RTTI
145{
146 typedef std::unordered_map< OUString, std::type_info *, OUStringHash > t_rtti_map;
147
148 Mutex m_mutex;
151
152 void * m_hApp;
153
154public:
155 RTTI();
156 ~RTTI();
157
158 std::type_info * getRTTI( typelib_CompoundTypeDescription * );
159};
160
162 : m_hApp( dlopen( nullptr, RTLD_LAZY ) )
163{
164}
165
167{
168 dlclose( m_hApp );
169}
170
171
172std::type_info * RTTI::getRTTI( typelib_CompoundTypeDescription *pTypeDescr )
173{
174 std::type_info * rtti;
175
176 OUString const & unoName = OUString::unacquired(&pTypeDescr->aBase.pTypeName);
177
178 MutexGuard guard( m_mutex );
179 t_rtti_map::const_iterator iFind( m_rttis.find( unoName ) );
180 if (iFind == m_rttis.end())
181 {
182 // RTTI symbol
183 OStringBuffer buf( 64 );
184 buf.append( "_ZTIN" );
185 sal_Int32 index = 0;
186 do
187 {
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 );
192 }
193 while (index >= 0);
194 buf.append( 'E' );
195
196 OString symName( buf.makeStringAndClear() );
197 rtti = static_cast<std::type_info *>(dlsym( m_hApp, symName.getStr() ));
198
199 if (rtti)
200 {
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,
204 "bridges",
205 "inserting new rtti failed" );
206 }
207 else
208 {
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())
212 {
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();
219 }
220#if OSL_DEBUG_LEVEL > 1
221 fprintf( stderr,"generated rtti for %s\n", rttiName );
222#endif
223 if (pTypeDescr->pBaseTypeDescription)
224 {
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);
229 }
230 else
231 {
232 rtti = createFake_class_type_info(rttiName);
233 }
234
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,
238 "bridges",
239 "inserting new generated rtti failed" );
240 }
241 else // taking already generated rtti
242 {
243 rtti = iFind2->second;
244 }
245 }
246 }
247 else
248 {
249 rtti = iFind->second;
250 }
251
252 return rtti;
253}
254
255
256static void deleteException( void * pExc )
257{
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
263 //
264 // __attribute__((aligned))
265 //
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);
282 }
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!!!");
287 if (pTD)
288 {
289 ::uno_destructData( pExc, pTD, cpp_release );
290 ::typelib_typedescription_release( pTD );
291 }
292}
293
294void raiseException( uno_Any * pUnoExc, uno_Mapping * pUno2Cpp )
295{
296#if OSL_DEBUG_LEVEL > 1
297 OString cstr(
299 OUString::unacquired( &pUnoExc->pType->pTypeName ),
300 RTL_TEXTENCODING_ASCII_US ) );
301 fprintf( stderr, "> uno exception occurred: %s\n", cstr.getStr() );
302#endif
303 void * pCppExc;
304 std::type_info * rtti;
305
306 {
307 // construct cpp exception object
308 typelib_TypeDescription * pTypeDescr = nullptr;
309 TYPELIB_DANGER_GET( &pTypeDescr, pUnoExc->pType );
310 assert(pTypeDescr);
311 if (! pTypeDescr)
312 {
313 throw RuntimeException(
314 "cannot get typedescription for type " +
315 OUString::unacquired( &pUnoExc->pType->pTypeName ) );
316 }
317
318 pCppExc = __cxxabiv1::__cxa_allocate_exception( pTypeDescr->nSize );
319 ::uno_copyAndConvertData( pCppExc, pUnoExc->pData, pTypeDescr, pUno2Cpp );
320
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!");
328 if (! rtti)
329 {
330 throw RuntimeException(
331 "no rtti for type " +
332 OUString::unacquired( &pUnoExc->pType->pTypeName ) );
333 }
334 }
335
336 // void __cxa_throw(void* thrown_exception,
337 // struct std::type_info * tinfo,
338 // void (*dest)(void*));
340}
341
342void fillUnoException(uno_Any * pUnoExc, uno_Mapping * pCpp2Uno)
343{
345 if (! header)
346 {
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);
351 return;
352 }
353
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
359 //
360 // [8 byte void *reserve]
361 // 8 byte size_t referenceCount
362 //
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
368 //
369 // 8 byte void* primaryException
370 //
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
375 // reserve member:
376 if (*reinterpret_cast<void **>(header) == nullptr) {
377 header = reinterpret_cast<__cxa_exception *>(reinterpret_cast<void **>(header) + 1);
378 }
379
380 std::type_info *exceptionType = __cxxabiv1::__cxa_current_exception_type();
381
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() );
387#endif
388 typelib_typedescription_getByName( &pExcTypeDescr, unoName.pData );
389 if (nullptr == pExcTypeDescr)
390 {
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);
395 }
396 else
397 {
398 // construct uno exception any
399 uno_any_constructAndConvert( pUnoExc, header->adjustedPtr, pExcTypeDescr, pCpp2Uno );
400 typelib_typedescription_release( pExcTypeDescr );
401 }
402}
403
404}
405
406/* 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
char const * name
void * m_hApp
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 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 raiseException(uno_Any *pUnoExc, uno_Mapping *pUno2Cpp)
void __cxa_throw(void *thrown_exception, std::type_info *tinfo, void(*dest)(void *)) __attribute__((noreturn))
__cxa_eh_globals * __cxa_get_globals()
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