LibreOffice Module bridges (master) 1
gcc3_linux_sparc64/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 <typelib/typedescription.hxx>
31#include <uno/any2.h>
32#include <unordered_map>
33#include "share.hxx"
34
35
36using namespace ::std;
37using namespace ::osl;
38using namespace ::com::sun::star::uno;
39using namespace ::__cxxabiv1;
40
41
43{
44
45void dummy_can_throw_anything( char const * )
46{
47}
48
49static OUString toUNOname( char const * p )
50{
51#if defined BRIDGES_DEBUG
52 char const * start = p;
53#endif
54
55 // example: N3com3sun4star4lang24IllegalArgumentExceptionE
56
57 OUStringBuffer buf( 64 );
58 assert( 'N' == *p );
59 ++p; // skip N
60
61 while ('E' != *p)
62 {
63 // read chars count
64 long n = (*p++ - '0');
65 while ('0' <= *p && '9' >= *p)
66 {
67 n *= 10;
68 n += (*p++ - '0');
69 }
70 buf.appendAscii( p, n );
71 p += n;
72 if ('E' != *p)
73 buf.append( '.' );
74 }
75
76#if defined BRIDGES_DEBUG
77 OUString ret( buf.makeStringAndClear() );
78 OString c_ret( OUStringToOString( ret, RTL_TEXTENCODING_ASCII_US ) );
79 fprintf( stderr, "> toUNOname(): %s => %s\n", start, c_ret.getStr() );
80 return ret;
81#else
82 return buf.makeStringAndClear();
83#endif
84}
85
86class RTTI
87{
88 typedef std::unordered_map< OUString, type_info * > t_rtti_map;
89
90 Mutex m_mutex;
93
94 void * m_hApp;
95
96public:
99
100 type_info * getRTTI( typelib_CompoundTypeDescription * );
101};
102
104 : m_hApp( dlopen( 0, RTLD_LAZY ) )
105{
106}
107
109{
110 dlclose( m_hApp );
111}
112
113
114type_info * RTTI::getRTTI( typelib_CompoundTypeDescription *pTypeDescr )
115{
116 type_info * rtti;
117
118 OUString const & unoName = *(OUString const *)&pTypeDescr->aBase.pTypeName;
119
120 MutexGuard guard( m_mutex );
121 t_rtti_map::const_iterator iFind( m_rttis.find( unoName ) );
122 if (iFind == m_rttis.end())
123 {
124 // RTTI symbol
125 OStringBuffer buf( 64 );
126 buf.append( "_ZTIN" );
127 sal_Int32 index = 0;
128 do
129 {
130 OUString token( unoName.getToken( 0, '.', index ) );
131 buf.append( token.getLength() );
132 OString c_token( OUStringToOString( token, RTL_TEXTENCODING_ASCII_US ) );
133 buf.append( c_token );
134 }
135 while (index >= 0);
136 buf.append( 'E' );
137
138 OString symName( buf.makeStringAndClear() );
139 rtti = (type_info *)dlsym( m_hApp, symName.getStr() );
140
141 if (rtti)
142 {
143 pair< t_rtti_map::iterator, bool > insertion(
144 m_rttis.insert( t_rtti_map::value_type( unoName, rtti ) ) );
145 assert(insertion.second);
146 }
147 else
148 {
149 // try to lookup the symbol in the generated rtti map
150 t_rtti_map::const_iterator iiFind( m_generatedRttis.find( unoName ) );
151 if (iiFind == m_generatedRttis.end())
152 {
153 // we must generate it !
154 // symbol and rtti-name is nearly identical,
155 // the symbol is prefixed with _ZTI
156 char const * rttiName = symName.getStr() +4;
157#if defined BRIDGES_DEBUG
158 fprintf( stderr,"generated rtti for %s\n", rttiName );
159#endif
160 if (pTypeDescr->pBaseTypeDescription)
161 {
162 // ensure availability of base
163 type_info * base_rtti = getRTTI(
164 (typelib_CompoundTypeDescription *)pTypeDescr->pBaseTypeDescription );
165 rtti = new __si_class_type_info(
166 strdup( rttiName ), (__class_type_info *)base_rtti );
167 }
168 else
169 {
170 // this class has no base class
171 rtti = new __class_type_info( strdup( rttiName ) );
172 }
173
174 pair< t_rtti_map::iterator, bool > insertion(
175 m_generatedRttis.insert( t_rtti_map::value_type( unoName, rtti ) ) );
176 assert(insertion.second);
177 }
178 else // taking already generated rtti
179 {
180 rtti = iiFind->second;
181 }
182 }
183 }
184 else
185 {
186 rtti = iFind->second;
187 }
188
189 return rtti;
190}
191
192
193static void deleteException( void * pExc )
194{
195 __cxa_exception const * header = ((__cxa_exception const *)pExc - 1);
196 typelib_TypeDescription * pTD = 0;
197 OUString unoName( toUNOname( header->exceptionType->name() ) );
198 ::typelib_typedescription_getByName( &pTD, unoName.pData );
199 assert(pTD && "### unknown exception type! leaving out destruction => leaking!!!");
200 if (pTD)
201 {
202 ::uno_destructData( pExc, pTD, cpp_release );
203 ::typelib_typedescription_release( pTD );
204 }
205}
206
207void raiseException( uno_Any * pUnoExc, uno_Mapping * pUno2Cpp )
208{
209#if defined BRIDGES_DEBUG
210 OString cstr(
212 OUString::unacquired( &pUnoExc->pType->pTypeName ),
213 RTL_TEXTENCODING_ASCII_US ) );
214 fprintf( stderr, "> uno exception occurred: %s\n", cstr.getStr() );
215#endif
216 void * pCppExc;
217 type_info * rtti;
218
219 {
220 // construct cpp exception object
221 typelib_TypeDescription * pTypeDescr = 0;
222 TYPELIB_DANGER_GET( &pTypeDescr, pUnoExc->pType );
223 assert(pTypeDescr);
224 if (! pTypeDescr)
225 {
226 throw RuntimeException(
227 OUString("cannot get typedescription for type ") +
228 OUString::unacquired( &pUnoExc->pType->pTypeName ) );
229 }
230
231 pCppExc = __cxa_allocate_exception( pTypeDescr->nSize );
232 ::uno_copyAndConvertData( pCppExc, pUnoExc->pData, pTypeDescr, pUno2Cpp );
233
234 // destruct uno exception
235 ::uno_any_destruct( pUnoExc, 0 );
236 // avoiding locked counts
237 static RTTI rtti_data;
238 rtti = (type_info*)rtti_data.getRTTI((typelib_CompoundTypeDescription*)pTypeDescr);
239 TYPELIB_DANGER_RELEASE( pTypeDescr );
240 assert(rtti);
241 if (! rtti)
242 {
243 throw RuntimeException(
244 OUString("no rtti for type ") +
245 OUString::unacquired( &pUnoExc->pType->pTypeName )
246 );
247 }
248 }
249
250 __cxa_throw( pCppExc, rtti, deleteException );
251}
252
253void fillUnoException(uno_Any * pUnoExc, uno_Mapping * pCpp2Uno)
254{
255 __cxa_exception * header = __cxa_get_globals()->caughtExceptions;
256 if (! header)
257 {
258 RuntimeException aRE( "no exception header!" );
259 Type const & rType = cppu::UnoType<decltype(aRE)>::get();
260 uno_type_any_constructAndConvert( pUnoExc, &aRE, rType.getTypeLibType(), pCpp2Uno );
261 SAL_WARN("bridges", aRE.Message);
262 return;
263 }
264
265 std::type_info *exceptionType = __cxa_current_exception_type();
266
267 typelib_TypeDescription * pExcTypeDescr = 0;
268 OUString unoName( toUNOname( exceptionType->name() ) );
269#if defined BRIDGES_DEBUG
270 OString cstr_unoName( OUStringToOString( unoName, RTL_TEXTENCODING_ASCII_US ) );
271 fprintf( stderr, "> c++ exception occurred: %s\n", cstr_unoName.getStr() );
272#endif
273 typelib_typedescription_getByName( &pExcTypeDescr, unoName.pData );
274 if (0 == pExcTypeDescr)
275 {
276 RuntimeException aRE( OUString("exception type not found: ") + unoName );
277 Type const & rType = cppu::UnoType<decltype(aRE)>::get();
278 uno_type_any_constructAndConvert( pUnoExc, &aRE, rType.getTypeLibType(), pCpp2Uno );
279 SAL_WARN("bridges", aRE.Message);
280 }
281 else
282 {
283 // construct uno exception any
284 uno_any_constructAndConvert( pUnoExc, header->adjustedPtr, pExcTypeDescr, pCpp2Uno );
285 typelib_typedescription_release( pExcTypeDescr );
286 }
287}
288
289}
290
291/* 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
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 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()