Line data Source code
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 <cstdio>
21 : #include <cstring>
22 : #include <dlfcn.h>
23 : #include <boost/unordered_map.hpp>
24 :
25 : #include <cxxabi.h>
26 : #ifndef _GLIBCXX_CDTOR_CALLABI // new in GCC 4.7 cxxabi.h
27 : #define _GLIBCXX_CDTOR_CALLABI
28 : #endif
29 :
30 : #include <rtl/strbuf.hxx>
31 : #include <rtl/ustrbuf.hxx>
32 : #include <osl/diagnose.h>
33 : #include <osl/mutex.hxx>
34 :
35 : #include <com/sun/star/uno/genfunc.hxx>
36 : #include "com/sun/star/uno/RuntimeException.hpp"
37 : #include <typelib/typedescription.hxx>
38 :
39 : #include "share.hxx"
40 :
41 : using namespace ::std;
42 : using namespace ::osl;
43 : using namespace ::rtl;
44 : using namespace ::com::sun::star::uno;
45 : using namespace ::__cxxabiv1;
46 :
47 :
48 : namespace CPPU_CURRENT_NAMESPACE
49 : {
50 :
51 0 : void dummy_can_throw_anything( char const * )
52 : {
53 0 : }
54 :
55 : //==================================================================================================
56 1752 : static OUString toUNOname( char const * p ) SAL_THROW(())
57 : {
58 : #if OSL_DEBUG_LEVEL > 1
59 : char const * start = p;
60 : #endif
61 :
62 : // example: N3com3sun4star4lang24IllegalArgumentExceptionE
63 :
64 1752 : OUStringBuffer buf( 64 );
65 : OSL_ASSERT( 'N' == *p );
66 1752 : ++p; // skip N
67 :
68 12269 : while ('E' != *p)
69 : {
70 : // read chars count
71 8765 : long n = (*p++ - '0');
72 19282 : while ('0' <= *p && '9' >= *p)
73 : {
74 1752 : n *= 10;
75 1752 : n += (*p++ - '0');
76 : }
77 8765 : buf.appendAscii( p, n );
78 8765 : p += n;
79 8765 : if ('E' != *p)
80 7013 : buf.append( (sal_Unicode)'.' );
81 : }
82 :
83 : #if OSL_DEBUG_LEVEL > 1
84 : OUString ret( buf.makeStringAndClear() );
85 : OString c_ret( OUStringToOString( ret, RTL_TEXTENCODING_ASCII_US ) );
86 : fprintf( stderr, "> toUNOname(): %s => %s\n", start, c_ret.getStr() );
87 : return ret;
88 : #else
89 1752 : return buf.makeStringAndClear();
90 : #endif
91 : }
92 :
93 : //==================================================================================================
94 : class RTTI
95 : {
96 : typedef boost::unordered_map< OUString, type_info *, OUStringHash > t_rtti_map;
97 :
98 : Mutex m_mutex;
99 : t_rtti_map m_rttis;
100 : t_rtti_map m_generatedRttis;
101 :
102 : void * m_hApp;
103 :
104 : public:
105 : RTTI() SAL_THROW(());
106 : ~RTTI() SAL_THROW(());
107 :
108 : type_info * getRTTI( typelib_CompoundTypeDescription * ) SAL_THROW(());
109 : };
110 : //__________________________________________________________________________________________________
111 18 : RTTI::RTTI() SAL_THROW(())
112 : #if defined(FREEBSD) && __FreeBSD_version < 702104
113 : : m_hApp( dlopen( 0, RTLD_NOW | RTLD_GLOBAL ) )
114 : #else
115 18 : : m_hApp( dlopen( 0, RTLD_LAZY ) )
116 : #endif
117 : {
118 18 : }
119 : //__________________________________________________________________________________________________
120 36 : RTTI::~RTTI() SAL_THROW(())
121 : {
122 18 : dlclose( m_hApp );
123 18 : }
124 :
125 : //__________________________________________________________________________________________________
126 1669 : type_info * RTTI::getRTTI( typelib_CompoundTypeDescription *pTypeDescr ) SAL_THROW(())
127 : {
128 : type_info * rtti;
129 :
130 1669 : OUString const & unoName = *(OUString const *)&pTypeDescr->aBase.pTypeName;
131 :
132 1669 : MutexGuard guard( m_mutex );
133 1669 : t_rtti_map::const_iterator iRttiFind( m_rttis.find( unoName ) );
134 1669 : if (iRttiFind == m_rttis.end())
135 : {
136 : // RTTI symbol
137 25 : OStringBuffer buf( 64 );
138 25 : buf.append( RTL_CONSTASCII_STRINGPARAM("_ZTIN") );
139 25 : sal_Int32 index = 0;
140 125 : do
141 : {
142 125 : OUString token( unoName.getToken( 0, '.', index ) );
143 125 : buf.append( token.getLength() );
144 125 : OString c_token( OUStringToOString( token, RTL_TEXTENCODING_ASCII_US ) );
145 125 : buf.append( c_token );
146 : }
147 : while (index >= 0);
148 25 : buf.append( 'E' );
149 :
150 25 : OString symName( buf.makeStringAndClear() );
151 : #if defined(FREEBSD) && __FreeBSD_version < 702104 /* #i22253# */
152 : rtti = (type_info *)dlsym( RTLD_DEFAULT, symName.getStr() );
153 : #else
154 25 : rtti = (type_info *)dlsym( m_hApp, symName.getStr() );
155 : #endif
156 :
157 25 : if (rtti)
158 : {
159 : pair< t_rtti_map::iterator, bool > insertion(
160 25 : m_rttis.insert( t_rtti_map::value_type( unoName, rtti ) ) );
161 : OSL_ENSURE( insertion.second, "### inserting new rtti failed?!" );
162 : }
163 : else
164 : {
165 : // try to lookup the symbol in the generated rtti map
166 0 : t_rtti_map::const_iterator iFind( m_generatedRttis.find( unoName ) );
167 0 : if (iFind == 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 0 : char const * rttiName = symName.getStr() +4;
173 : #if OSL_DEBUG_LEVEL > 1
174 : fprintf( stderr,"generated rtti for %s\n", rttiName );
175 : #endif
176 0 : if (pTypeDescr->pBaseTypeDescription)
177 : {
178 : // ensure availability of base
179 : type_info * base_rtti = getRTTI(
180 0 : (typelib_CompoundTypeDescription *)pTypeDescr->pBaseTypeDescription );
181 : rtti = new __si_class_type_info(
182 0 : strdup( rttiName ), (__class_type_info *)base_rtti );
183 : }
184 : else
185 : {
186 : // this class has no base class
187 0 : rtti = new __class_type_info( strdup( rttiName ) );
188 : }
189 :
190 : pair< t_rtti_map::iterator, bool > insertion(
191 0 : m_generatedRttis.insert( t_rtti_map::value_type( unoName, rtti ) ) );
192 : OSL_ENSURE( insertion.second, "### inserting new generated rtti failed?!" );
193 : }
194 : else // taking already generated rtti
195 : {
196 0 : rtti = iFind->second;
197 : }
198 25 : }
199 : }
200 : else
201 : {
202 1644 : rtti = iRttiFind->second;
203 : }
204 :
205 1669 : return rtti;
206 : }
207 :
208 : //--------------------------------------------------------------------------------------------------
209 : extern "C" {
210 1669 : static void _GLIBCXX_CDTOR_CALLABI deleteException( void * pExc )
211 : {
212 1669 : __cxa_exception const * header = ((__cxa_exception const *)pExc - 1);
213 1669 : typelib_TypeDescription * pTD = 0;
214 1669 : OUString unoName( toUNOname( header->exceptionType->name() ) );
215 1669 : ::typelib_typedescription_getByName( &pTD, unoName.pData );
216 : OSL_ENSURE( pTD, "### unknown exception type! leaving out destruction => leaking!!!" );
217 1669 : if (pTD)
218 : {
219 1669 : ::uno_destructData( pExc, pTD, cpp_release );
220 1669 : ::typelib_typedescription_release( pTD );
221 1669 : }
222 1669 : }
223 : }
224 :
225 : //==================================================================================================
226 1669 : void raiseException( uno_Any * pUnoExc, uno_Mapping * pUno2Cpp )
227 : {
228 : #if OSL_DEBUG_LEVEL > 1
229 : OString cstr(
230 : OUStringToOString(
231 : *reinterpret_cast< OUString const * >( &pUnoExc->pType->pTypeName ),
232 : RTL_TEXTENCODING_ASCII_US ) );
233 : fprintf( stderr, "> uno exception occurred: %s\n", cstr.getStr() );
234 : #endif
235 : void * pCppExc;
236 : type_info * rtti;
237 :
238 : {
239 : // construct cpp exception object
240 1669 : typelib_TypeDescription * pTypeDescr = 0;
241 1669 : TYPELIB_DANGER_GET( &pTypeDescr, pUnoExc->pType );
242 : OSL_ASSERT( pTypeDescr );
243 1669 : if (! pTypeDescr)
244 : {
245 : throw RuntimeException(
246 : OUString( RTL_CONSTASCII_USTRINGPARAM("cannot get typedescription for type ") ) +
247 0 : *reinterpret_cast< OUString const * >( &pUnoExc->pType->pTypeName ),
248 0 : Reference< XInterface >() );
249 : }
250 :
251 1669 : pCppExc = __cxa_allocate_exception( pTypeDescr->nSize );
252 1669 : ::uno_copyAndConvertData( pCppExc, pUnoExc->pData, pTypeDescr, pUno2Cpp );
253 :
254 : // destruct uno exception
255 1669 : ::uno_any_destruct( pUnoExc, 0 );
256 : // avoiding locked counts
257 : static RTTI * s_rtti = 0;
258 1669 : if (! s_rtti)
259 : {
260 18 : MutexGuard guard( Mutex::getGlobalMutex() );
261 18 : if (! s_rtti)
262 : {
263 : #ifdef LEAK_STATIC_DATA
264 : s_rtti = new RTTI();
265 : #else
266 18 : static RTTI rtti_data;
267 18 : s_rtti = &rtti_data;
268 : #endif
269 18 : }
270 : }
271 1669 : rtti = (type_info *)s_rtti->getRTTI( (typelib_CompoundTypeDescription *) pTypeDescr );
272 1669 : TYPELIB_DANGER_RELEASE( pTypeDescr );
273 : OSL_ENSURE( rtti, "### no rtti for throwing exception!" );
274 1669 : if (! rtti)
275 : {
276 : throw RuntimeException(
277 : OUString( RTL_CONSTASCII_USTRINGPARAM("no rtti for type ") ) +
278 0 : *reinterpret_cast< OUString const * >( &pUnoExc->pType->pTypeName ),
279 0 : Reference< XInterface >() );
280 : }
281 : }
282 :
283 1669 : __cxa_throw( pCppExc, rtti, deleteException );
284 : }
285 :
286 : //==================================================================================================
287 83 : void fillUnoException( __cxa_exception * header, uno_Any * pUnoExc, uno_Mapping * pCpp2Uno )
288 : {
289 83 : if (! header)
290 : {
291 : RuntimeException aRE(
292 : OUString( RTL_CONSTASCII_USTRINGPARAM("no exception header!") ),
293 0 : Reference< XInterface >() );
294 0 : Type const & rType = ::getCppuType( &aRE );
295 0 : uno_type_any_constructAndConvert( pUnoExc, &aRE, rType.getTypeLibType(), pCpp2Uno );
296 : #if OSL_DEBUG_LEVEL > 0
297 : OString cstr( OUStringToOString( aRE.Message, RTL_TEXTENCODING_ASCII_US ) );
298 : OSL_FAIL( cstr.getStr() );
299 : #endif
300 83 : return;
301 : }
302 :
303 83 : typelib_TypeDescription * pExcTypeDescr = 0;
304 83 : OUString unoName( toUNOname( header->exceptionType->name() ) );
305 : #if OSL_DEBUG_LEVEL > 1
306 : OString cstr_unoName( OUStringToOString( unoName, RTL_TEXTENCODING_ASCII_US ) );
307 : fprintf( stderr, "> c++ exception occurred: %s\n", cstr_unoName.getStr() );
308 : #endif
309 83 : typelib_typedescription_getByName( &pExcTypeDescr, unoName.pData );
310 83 : if (0 == pExcTypeDescr)
311 : {
312 : RuntimeException aRE(
313 0 : OUString( RTL_CONSTASCII_USTRINGPARAM("exception type not found: ") ) + unoName,
314 0 : Reference< XInterface >() );
315 0 : Type const & rType = ::getCppuType( &aRE );
316 0 : uno_type_any_constructAndConvert( pUnoExc, &aRE, rType.getTypeLibType(), pCpp2Uno );
317 : #if OSL_DEBUG_LEVEL > 0
318 : OString cstr( OUStringToOString( aRE.Message, RTL_TEXTENCODING_ASCII_US ) );
319 : OSL_FAIL( cstr.getStr() );
320 : #endif
321 : }
322 : else
323 : {
324 : // construct uno exception any
325 83 : uno_any_constructAndConvert( pUnoExc, header->adjustedPtr, pExcTypeDescr, pCpp2Uno );
326 83 : typelib_typedescription_release( pExcTypeDescr );
327 83 : }
328 : }
329 :
330 : }
331 :
332 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|