LibreOffice Module bridges (master) 1
gcc3_linux_arm/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 <stdio.h>
21#include <string.h>
22#include <dlfcn.h>
23#include <cxxabi.h>
24#include <rtl/strbuf.hxx>
25#include <rtl/ustrbuf.hxx>
26#include <osl/mutex.hxx>
27#include <sal/log.hxx>
28
29#include <com/sun/star/uno/genfunc.hxx>
30#include <com/sun/star/uno/RuntimeException.hpp>
31#include <typelib/typedescription.hxx>
32#include <uno/any2.h>
33#include <unordered_map>
34#include "share.hxx"
35
36
37using namespace ::std;
38using namespace ::osl;
39using namespace ::com::sun::star::uno;
40using namespace ::__cxxabiv1;
41
42extern sal_Int32 * pHack;
43extern sal_Int32 nHack;
44
46{
47 void dummy_can_throw_anything( char const * )
48 {
49 }
50
51 static OUString toUNOname( char const * p )
52 {
53#if OSL_DEBUG_LEVEL > 1
54 char const * start = p;
55#endif
56
57 // example: N3com3sun4star4lang24IllegalArgumentExceptionE
58
59 OUStringBuffer buf( 64 );
60 assert( 'N' == *p );
61 ++p; // skip N
62
63 while ('E' != *p)
64 {
65 // read chars count
66 long n = *p++ - '0';
67 while ('0' <= *p && '9' >= *p)
68 {
69 n *= 10;
70 n += (*p++ - '0');
71 }
72 buf.appendAscii( p, n );
73 p += n;
74 if ('E' != *p)
75 buf.append( '.' );
76 }
77
78#if OSL_DEBUG_LEVEL > 1
79 OUString ret( buf.makeStringAndClear() );
80 OString c_ret( OUStringToOString( ret, RTL_TEXTENCODING_ASCII_US ) );
81 fprintf( stderr, "> toUNOname(): %s => %s\n", start, c_ret.getStr() );
82 return ret;
83#else
84 return buf.makeStringAndClear();
85#endif
86 }
87
88namespace {
89 class RTTI
90 {
91 typedef std::unordered_map< OUString, type_info * > t_rtti_map;
92
93 Mutex m_mutex;
96
97#ifndef ANDROID
98 void * m_hApp;
99#endif
100
101 public:
102 RTTI();
103 ~RTTI();
104
105 type_info * getRTTI(typelib_CompoundTypeDescription *);
106 };
107}
108
109 RTTI::RTTI()
110#ifndef ANDROID
111 : m_hApp( dlopen( nullptr, RTLD_LAZY ) )
112#endif
113 {
114 }
115
117 {
118#ifndef ANDROID
119 dlclose( m_hApp );
120#endif
121 }
122
123
124 type_info * RTTI::getRTTI( typelib_CompoundTypeDescription *pTypeDescr )
125 {
126 type_info * rtti;
127
128 OUString const & unoName = *reinterpret_cast<OUString const *>(&pTypeDescr->aBase.pTypeName);
129
130 MutexGuard guard( m_mutex );
131 t_rtti_map::const_iterator iFind( m_rttis.find( unoName ) );
132 if (iFind == m_rttis.end())
133 {
134 // RTTI symbol
135 OStringBuffer buf( 64 );
136 buf.append( "_ZTIN" );
137 sal_Int32 index = 0;
138 do
139 {
140 OUString token( unoName.getToken( 0, '.', index ) );
141 buf.append( token.getLength() );
142 OString c_token( OUStringToOString( token, RTL_TEXTENCODING_ASCII_US ) );
143 buf.append( c_token );
144 }
145 while (index >= 0);
146 buf.append( 'E' );
147
148 OString symName( buf.makeStringAndClear() );
149#ifndef ANDROID
150 rtti = static_cast<type_info *>(dlsym( m_hApp, symName.getStr() ));
151#else
152 rtti = (type_info *)dlsym( RTLD_DEFAULT, symName.getStr() );
153#endif
154
155 if (rtti)
156 {
157 pair< t_rtti_map::iterator, bool > insertion(
158 m_rttis.insert( t_rtti_map::value_type( unoName, rtti ) ) );
159 (void) insertion;
160 assert(insertion.second && "### inserting new rtti failed?!");
161 }
162 else
163 {
164 // try to lookup the symbol in the generated rtti map
165 t_rtti_map::const_iterator iFind2( m_generatedRttis.find( unoName ) );
166
167 if (iFind2 == m_generatedRttis.end())
168 {
169 // we must generate it !
170 // symbol and rtti-name is nearly identical,
171 // the symbol is prefixed with _ZTI
172
173#ifdef ANDROID
174 // This code is supposed to be used only used for
175 // inter-process UNO, says sberg. Thus it should
176 // be unnecessary and never reached for
177 // Android. But see above...
178
179 // assert(iFind2 != m_generatedRttis.end());
180 // return NULL;
181#endif
182 char const * rttiName = symName.getStr() +4;
183#if OSL_DEBUG_LEVEL > 1
184 fprintf( stderr,"generated rtti for %s\n", rttiName );
185#endif
186 if (pTypeDescr->pBaseTypeDescription)
187 {
188 // ensure availability of base
189 type_info * base_rtti = getRTTI( pTypeDescr->pBaseTypeDescription );
190 rtti = new __si_class_type_info(
191 strdup( rttiName ), static_cast<__class_type_info *>(base_rtti) );
192 }
193 else
194 {
195 // this class has no base class
196 rtti = new __class_type_info( strdup( rttiName ) );
197 }
198
199 pair< t_rtti_map::iterator, bool > insertion(
200 m_generatedRttis.insert( t_rtti_map::value_type( unoName, rtti ) ) );
201 (void) insertion;
202 assert(insertion.second && "### inserting new generated rtti failed?!");
203 }
204 else // taking already generated rtti
205 {
206 rtti = iFind2->second;
207 }
208 }
209 }
210 else
211 {
212 rtti = iFind->second;
213 }
214
215 return rtti;
216 }
217
218
219 static void deleteException( void * pExc )
220 {
221 __cxa_exception const * header = static_cast<__cxa_exception const *>(pExc) - 1;
222 typelib_TypeDescription * pTD = nullptr;
223 OUString unoName( toUNOname( header->exceptionType->name() ) );
224 ::typelib_typedescription_getByName( &pTD, unoName.pData );
225 assert(pTD && "### unknown exception type! leaving out destruction => leaking!!!");
226 if (pTD)
227 {
228 ::uno_destructData( pExc, pTD, cpp_release );
229 ::typelib_typedescription_release( pTD );
230 }
231 }
232
233 void raiseException( uno_Any * pUnoExc, uno_Mapping * pUno2Cpp )
234 {
235#if OSL_DEBUG_LEVEL > 1
236 OString cstr(
238 OUString::unacquired( &pUnoExc->pType->pTypeName ),
239 RTL_TEXTENCODING_ASCII_US ) );
240 fprintf( stderr, "> uno exception occurred: %s\n", cstr.getStr() );
241#endif
242 void * pCppExc;
243 type_info * rtti;
244
245 {
246 // construct cpp exception object
247 typelib_TypeDescription * pTypeDescr = nullptr;
248 TYPELIB_DANGER_GET( &pTypeDescr, pUnoExc->pType );
249 assert(pTypeDescr);
250 if (! pTypeDescr)
251 {
252 throw RuntimeException(
253 "cannot get typedescription for type " +
254 OUString::unacquired( &pUnoExc->pType->pTypeName ) );
255 }
256
257 pCppExc = __cxa_allocate_exception( pTypeDescr->nSize );
258 ::uno_copyAndConvertData( pCppExc, pUnoExc->pData, pTypeDescr, pUno2Cpp );
259
260 // destruct uno exception
261 ::uno_any_destruct( pUnoExc, nullptr );
262 // avoiding locked counts
263 static RTTI rtti_data;
264 rtti = rtti_data.getRTTI(reinterpret_cast<typelib_CompoundTypeDescription*>(pTypeDescr));
265 TYPELIB_DANGER_RELEASE( pTypeDescr );
266 assert(rtti && "### no rtti for throwing exception!");
267 if (! rtti)
268 {
269 throw RuntimeException(
270 "no rtti for type " +
271 OUString::unacquired( &pUnoExc->pType->pTypeName ) );
272 }
273 }
274
275 __cxa_throw( pCppExc, rtti, deleteException );
276 }
277
278#ifdef __ARM_EABI__
279 static void* getAdjustedPtr(__cxa_exception* header)
280 {
281 return reinterpret_cast<void*>(header->unwindHeader.barrier_cache.bitpattern[0]);
282 }
283#else
284 static void* getAdjustedPtr(__cxa_exception* header)
285 {
286 return header->adjustedPtr;
287 }
288#endif
289
290 void fillUnoException(uno_Any * pUnoExc, uno_Mapping * pCpp2Uno)
291 {
292 __cxa_exception * header = __cxa_get_globals()->caughtExceptions;
293 if (! header)
294 {
295 RuntimeException aRE( "no exception header!" );
296 Type const & rType = cppu::UnoType<decltype(aRE)>::get();
297 uno_type_any_constructAndConvert( pUnoExc, &aRE, rType.getTypeLibType(), pCpp2Uno );
298 SAL_WARN("bridges", aRE.Message);
299 return;
300 }
301
302 std::type_info *exceptionType = __cxa_current_exception_type();
303
304 typelib_TypeDescription * pExcTypeDescr = nullptr;
305 OUString unoName( toUNOname( exceptionType->name() ) );
306#if OSL_DEBUG_LEVEL > 1
307 OString cstr_unoName( OUStringToOString( unoName, RTL_TEXTENCODING_ASCII_US ) );
308 fprintf( stderr, "> c++ exception occurred: %s\n", cstr_unoName.getStr() );
309#endif
310 typelib_typedescription_getByName( &pExcTypeDescr, unoName.pData );
311 if (nullptr == pExcTypeDescr)
312 {
313 RuntimeException aRE( "exception type not found: " + unoName );
314 Type const & rType = cppu::UnoType<decltype(aRE)>::get();
315 uno_type_any_constructAndConvert( pUnoExc, &aRE, rType.getTypeLibType(), pCpp2Uno );
316 SAL_WARN("bridges", aRE.Message);
317 }
318 else
319 {
320 // construct uno exception any
321 uno_any_constructAndConvert( pUnoExc, getAdjustedPtr(header), pExcTypeDescr, pCpp2Uno );
322 typelib_typedescription_release( pExcTypeDescr );
323 }
324 }
325}
326
327/* 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()
type_info * getRTTI(typelib_CompoundTypeDescription *)
std::unordered_map< OUString, type_info * > t_rtti_map
sal_Int32 nHack
sal_Int32 * pHack
void * m_hApp
void * p
sal_Int64 n
#define SAL_WARN(area, stream)
struct _uno_Mapping uno_Mapping
Definition: msvc/except.hxx:32
struct _typelib_TypeDescription typelib_TypeDescription
Definition: msvc/except.hxx:52
struct _uno_Any uno_Any
Definition: msvc/except.hxx:31
static void * getAdjustedPtr(__cxa_exception *header)
static void deleteException(void *pExc)
__cxa_eh_globals * __cxa_get_globals()
void __cxa_throw(void *thrown_exception, std::type_info *tinfo, void(*dest)(void *)) __attribute__((noreturn))
void dummy_can_throw_anything(char const *)
void fillUnoException(uno_Any *pExc, uno_Mapping *pCpp2Uno)
void * __cxa_allocate_exception(std::size_t thrown_size)
std::type_info * __cxa_current_exception_type()
static OUString toUNOname(char const *p)
void raiseException(uno_Any *pUnoExc, uno_Mapping *pUno2Cpp)
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()