LibreOffice Module bridges (master) 1
gcc3_linux_m68k/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 <sal/log.hxx>
27#include <osl/mutex.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
88 class RTTI
89 {
90 typedef std::unordered_map< OUString, type_info * > t_rtti_map;
91
92 Mutex m_mutex;
95
96 void * m_hApp;
97
98 public:
101
102 type_info * getRTTI(typelib_CompoundTypeDescription *);
103 };
104
105 RTTI::RTTI()
106 : m_hApp( dlopen( 0, RTLD_LAZY ) )
107 {
108 }
109
111 {
112 dlclose( m_hApp );
113 }
114
115
116 type_info * RTTI::getRTTI( typelib_CompoundTypeDescription *pTypeDescr )
117 {
118 type_info * rtti;
119
120 OUString const & unoName = *(OUString const *)&pTypeDescr->aBase.pTypeName;
121
122 MutexGuard guard( m_mutex );
123 t_rtti_map::const_iterator iRttiFind( m_rttis.find( unoName ) );
124 if (iRttiFind == m_rttis.end())
125 {
126 // RTTI symbol
127 OStringBuffer buf( 64 );
128 buf.append( "_ZTIN" );
129 sal_Int32 index = 0;
130 do
131 {
132 OUString token( unoName.getToken( 0, '.', index ) );
133 buf.append( token.getLength() );
134 OString c_token( OUStringToOString( token, RTL_TEXTENCODING_ASCII_US ) );
135 buf.append( c_token );
136 }
137 while (index >= 0);
138 buf.append( 'E' );
139
140 OString symName( buf.makeStringAndClear() );
141 rtti = (type_info *)dlsym( m_hApp, symName.getStr() );
142
143 if (rtti)
144 {
145 pair< t_rtti_map::iterator, bool > insertion(
146 m_rttis.insert( t_rtti_map::value_type( unoName, rtti ) ) );
147 assert(insertion.second && "### inserting new rtti failed?!");
148 }
149 else
150 {
151 // try to lookup the symbol in the generated rtti map
152 t_rtti_map::const_iterator iFind( m_generatedRttis.find( unoName ) );
153 if (iFind == m_generatedRttis.end())
154 {
155 // we must generate it !
156 // symbol and rtti-name is nearly identical,
157 // the symbol is prefixed with _ZTI
158 char const * rttiName = symName.getStr() +4;
159#if OSL_DEBUG_LEVEL > 1
160 fprintf( stderr,"generated rtti for %s\n", rttiName );
161#endif
162 if (pTypeDescr->pBaseTypeDescription)
163 {
164 // ensure availability of base
165 type_info * base_rtti = getRTTI(
166 (typelib_CompoundTypeDescription *)pTypeDescr->pBaseTypeDescription );
167 rtti = new __si_class_type_info(
168 strdup( rttiName ), (__class_type_info *)base_rtti );
169 }
170 else
171 {
172 // this class has no base class
173 rtti = new __class_type_info( strdup( rttiName ) );
174 }
175
176 pair< t_rtti_map::iterator, bool > insertion(
177 m_generatedRttis.insert( t_rtti_map::value_type( unoName, rtti ) ) );
178 assert(insertion.second && "### inserting new generated rtti failed?!");
179 }
180 else // taking already generated rtti
181 {
182 rtti = iFind->second;
183 }
184 }
185 }
186 else
187 {
188 rtti = iRttiFind->second;
189 }
190
191 return rtti;
192 }
193
194
195 static void deleteException( void * pExc )
196 {
197 __cxa_exception const * header = ((__cxa_exception const *)pExc - 1);
198 typelib_TypeDescription * pTD = 0;
199 OUString unoName( toUNOname( header->exceptionType->name() ) );
200 ::typelib_typedescription_getByName( &pTD, unoName.pData );
201 assert(pTD && "### unknown exception type! leaving out destruction => leaking!!!");
202 if (pTD)
203 {
204 ::uno_destructData( pExc, pTD, cpp_release );
205 ::typelib_typedescription_release( pTD );
206 }
207 }
208
209 void raiseException( uno_Any * pUnoExc, uno_Mapping * pUno2Cpp )
210 {
211#if OSL_DEBUG_LEVEL > 1
212 OString cstr(
214 OUString::unacquired( &pUnoExc->pType->pTypeName ),
215 RTL_TEXTENCODING_ASCII_US ) );
216 fprintf( stderr, "> uno exception occurred: %s\n", cstr.getStr() );
217#endif
218 void * pCppExc;
219 type_info * rtti;
220
221 {
222 // construct cpp exception object
223 typelib_TypeDescription * pTypeDescr = 0;
224 TYPELIB_DANGER_GET( &pTypeDescr, pUnoExc->pType );
225 assert(pTypeDescr);
226 if (! pTypeDescr)
227 {
228 throw RuntimeException(
229 OUString("cannot get typedescription for type ") +
230 OUString::unacquired( &pUnoExc->pType->pTypeName ) );
231 }
232
233 pCppExc = __cxa_allocate_exception( pTypeDescr->nSize );
234 ::uno_copyAndConvertData( pCppExc, pUnoExc->pData, pTypeDescr, pUno2Cpp );
235
236 // destruct uno exception
237 ::uno_any_destruct( pUnoExc, 0 );
238 // avoiding locked counts
239 static RTTI rtti_data;
240 rtti = (type_info*)rtti_data.getRTTI((typelib_CompoundTypeDescription*)pTypeDescr);
241 TYPELIB_DANGER_RELEASE( pTypeDescr );
242 assert(rtti && "### no rtti for throwing exception!");
243 if (! rtti)
244 {
245 throw RuntimeException(
246 OUString("no rtti for type ") +
247 OUString::unacquired( &pUnoExc->pType->pTypeName ) );
248 }
249 }
250
251
252 __cxa_throw( pCppExc, rtti, deleteException );
253 }
254
255 static void* getAdjustedPtr(__cxa_exception* header)
256 {
257 return header->adjustedPtr;
258 }
259
260 void fillUnoException(uno_Any * pUnoExc, uno_Mapping * pCpp2Uno)
261 {
262 __cxa_exception * header = __cxa_get_globals()->caughtExceptions;
263 if (! header)
264 {
265 RuntimeException aRE( "no exception header!" );
266 Type const & rType = cppu::UnoType<decltype(aRE)>::get();
267 uno_type_any_constructAndConvert( pUnoExc, &aRE, rType.getTypeLibType(), pCpp2Uno );
268 SAL_WARN("bridges", aRE.Message);
269 return;
270 }
271
272 std::type_info *exceptionType = __cxa_current_exception_type();
273
274 typelib_TypeDescription * pExcTypeDescr = 0;
275 OUString unoName( toUNOname( exceptionType->name() ) );
276#if OSL_DEBUG_LEVEL > 1
277 OString cstr_unoName( OUStringToOString( unoName, RTL_TEXTENCODING_ASCII_US ) );
278 fprintf( stderr, "> c++ exception occurred: %s\n", cstr_unoName.getStr() );
279#endif
280 typelib_typedescription_getByName( &pExcTypeDescr, unoName.pData );
281 if (0 == pExcTypeDescr)
282 {
283 RuntimeException aRE( OUString("exception type not found: ") + unoName );
284 Type const & rType = cppu::UnoType<decltype(aRE)>::get();
285 uno_type_any_constructAndConvert( pUnoExc, &aRE, rType.getTypeLibType(), pCpp2Uno );
286 SAL_WARN("bridges", aRE.Message);
287 }
288 else
289 {
290 // construct uno exception any
291 uno_any_constructAndConvert( pUnoExc, getAdjustedPtr(header), pExcTypeDescr, pCpp2Uno );
292 typelib_typedescription_release( pExcTypeDescr );
293 }
294 }
295}
296
297/* 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 *)
type_info * getRTTI(typelib_CompoundTypeDescription *)
std::unordered_map< OUString, type_info * > t_rtti_map
std::unordered_map< OUString, std::type_info *, OUStringHash > t_rtti_map
void * m_hApp
sal_Int32 nHack
sal_Int32 * pHack
void * p
sal_Int64 n
#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 * 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 * __cxa_allocate_exception(std::size_t thrown_size)
void fillUnoException(uno_Any *pUnoExc, uno_Mapping *pCpp2Uno)
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()