1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 #include <stdio.h>
24 #include <string.h>
25 #include <dlfcn.h>
26 #include <cxxabi.h>
27 #include <hash_map>
28 
29 #include <rtl/strbuf.hxx>
30 #include <rtl/ustrbuf.hxx>
31 #include <osl/diagnose.h>
32 #include <osl/mutex.hxx>
33 
34 #include <com/sun/star/uno/genfunc.hxx>
35 #include <typelib/typedescription.hxx>
36 #include <uno/any2.h>
37 
38 #include "share.hxx"
39 
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 
dummy_can_throw_anything(char const *)51 void dummy_can_throw_anything( char const * )
52 {
53 }
54 
55 //==================================================================================================
toUNOname(char const * p)56 static OUString toUNOname( char const * p ) SAL_THROW( () )
57 {
58 #if defined BRIDGES_DEBUG
59     char const * start = p;
60 #endif
61 
62     // example: N3com3sun4star4lang24IllegalArgumentExceptionE
63 
64 	OUStringBuffer buf( 64 );
65     OSL_ASSERT( 'N' == *p );
66     ++p; // skip N
67 
68     while ('E' != *p)
69     {
70         // read chars count
71         long n = (*p++ - '0');
72         while ('0' <= *p && '9' >= *p)
73         {
74             n *= 10;
75             n += (*p++ - '0');
76         }
77         buf.appendAscii( p, n );
78         p += n;
79         if ('E' != *p)
80             buf.append( (sal_Unicode)'.' );
81     }
82 
83 #if defined BRIDGES_DEBUG
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     return buf.makeStringAndClear();
90 #endif
91 }
92 
93 //==================================================================================================
94 class RTTI
95 {
96     typedef hash_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 //__________________________________________________________________________________________________
RTTI()111 RTTI::RTTI() SAL_THROW( () )
112     : m_hApp( dlopen( 0, RTLD_LAZY ) )
113 {
114 }
115 //__________________________________________________________________________________________________
~RTTI()116 RTTI::~RTTI() SAL_THROW( () )
117 {
118     dlclose( m_hApp );
119 }
120 
121 //__________________________________________________________________________________________________
getRTTI(typelib_CompoundTypeDescription * pTypeDescr)122 type_info * RTTI::getRTTI( typelib_CompoundTypeDescription *pTypeDescr ) SAL_THROW( () )
123 {
124     type_info * rtti;
125 
126     OUString const & unoName = *(OUString const *)&pTypeDescr->aBase.pTypeName;
127 
128     MutexGuard guard( m_mutex );
129     t_rtti_map::const_iterator iRttiFind( m_rttis.find( unoName ) );
130     if (iRttiFind == m_rttis.end())
131     {
132         // RTTI symbol
133         OStringBuffer buf( 64 );
134         buf.append( RTL_CONSTASCII_STRINGPARAM("_ZTIN") );
135         sal_Int32 index = 0;
136         do
137         {
138             OUString token( unoName.getToken( 0, '.', index ) );
139             buf.append( token.getLength() );
140             OString c_token( OUStringToOString( token, RTL_TEXTENCODING_ASCII_US ) );
141             buf.append( c_token );
142         }
143         while (index >= 0);
144         buf.append( 'E' );
145 
146         OString symName( buf.makeStringAndClear() );
147         rtti = (type_info *)dlsym( m_hApp, symName.getStr() );
148 
149         if (rtti)
150         {
151             pair< t_rtti_map::iterator, bool > insertion(
152                 m_rttis.insert( t_rtti_map::value_type( unoName, rtti ) ) );
153             OSL_ENSURE( insertion.second, "### inserting new rtti failed?!" );
154         }
155         else
156         {
157             // try to lookup the symbol in the generated rtti map
158             t_rtti_map::const_iterator iFind( m_generatedRttis.find( unoName ) );
159             if (iFind == m_generatedRttis.end())
160             {
161                 // we must generate it !
162                 // symbol and rtti-name is nearly identical,
163                 // the symbol is prefixed with _ZTI
164                 char const * rttiName = symName.getStr() +4;
165 #if defined BRIDGES_DEBUG
166                 fprintf( stderr,"generated rtti for %s\n", rttiName );
167 #endif
168                 if (pTypeDescr->pBaseTypeDescription)
169                 {
170                     // ensure availability of base
171                     type_info * base_rtti = getRTTI(
172                         (typelib_CompoundTypeDescription *)pTypeDescr->pBaseTypeDescription );
173                     rtti = new __si_class_type_info(
174                         strdup( rttiName ), (__class_type_info *)base_rtti );
175                 }
176                 else
177                 {
178                     // this class has no base class
179                     rtti = new __class_type_info( strdup( rttiName ) );
180                 }
181 
182                 pair< t_rtti_map::iterator, bool > insertion(
183                     m_generatedRttis.insert( t_rtti_map::value_type( unoName, rtti ) ) );
184                 OSL_ENSURE( insertion.second, "### inserting new generated rtti failed?!" );
185             }
186             else // taking already generated rtti
187             {
188                 rtti = iFind->second;
189             }
190         }
191     }
192     else
193     {
194         rtti = iRttiFind->second;
195     }
196 
197     return rtti;
198 }
199 
200 //--------------------------------------------------------------------------------------------------
deleteException(void * pExc)201 static void deleteException( void * pExc )
202 {
203     __cxa_exception const * header = ((__cxa_exception const *)pExc - 1);
204     typelib_TypeDescription * pTD = 0;
205     OUString unoName( toUNOname( header->exceptionType->name() ) );
206     ::typelib_typedescription_getByName( &pTD, unoName.pData );
207     OSL_ENSURE( pTD, "### unknown exception type! leaving out destruction => leaking!!!" );
208     if (pTD)
209     {
210 		::uno_destructData( pExc, pTD, cpp_release );
211 		::typelib_typedescription_release( pTD );
212 	}
213 }
214 
215 //==================================================================================================
raiseException(uno_Any * pUnoExc,uno_Mapping * pUno2Cpp)216 void raiseException( uno_Any * pUnoExc, uno_Mapping * pUno2Cpp )
217 {
218 #if defined BRIDGES_DEBUG
219     OString cstr(
220         OUStringToOString(
221             *reinterpret_cast< OUString const * >( &pUnoExc->pType->pTypeName ),
222             RTL_TEXTENCODING_ASCII_US ) );
223     fprintf( stderr, "> uno exception occured: %s\n", cstr.getStr() );
224 #endif
225     void * pCppExc;
226     type_info * rtti;
227 
228     {
229     // construct cpp exception object
230 	typelib_TypeDescription * pTypeDescr = 0;
231 	TYPELIB_DANGER_GET( &pTypeDescr, pUnoExc->pType );
232     OSL_ASSERT( pTypeDescr );
233     if (! pTypeDescr)
234     {
235         throw RuntimeException(
236             OUString( RTL_CONSTASCII_USTRINGPARAM("cannot get typedescription for type ") ) +
237             *reinterpret_cast< OUString const * >( &pUnoExc->pType->pTypeName ),
238             Reference< XInterface >() );
239     }
240 
241 	pCppExc = __cxa_allocate_exception( pTypeDescr->nSize );
242 	::uno_copyAndConvertData( pCppExc, pUnoExc->pData, pTypeDescr, pUno2Cpp );
243 
244 	// destruct uno exception
245 	::uno_any_destruct( pUnoExc, 0 );
246     // avoiding locked counts
247     static RTTI * s_rtti = 0;
248     if (! s_rtti)
249     {
250         MutexGuard guard( Mutex::getGlobalMutex() );
251         if (! s_rtti)
252         {
253 #ifdef LEAK_STATIC_DATA
254             s_rtti = new RTTI();
255 #else
256             static RTTI rtti_data;
257             s_rtti = &rtti_data;
258 #endif
259         }
260     }
261 	rtti = (type_info *)s_rtti->getRTTI( (typelib_CompoundTypeDescription *) pTypeDescr );
262     TYPELIB_DANGER_RELEASE( pTypeDescr );
263     OSL_ENSURE( rtti, "### no rtti for throwing exception!" );
264     if (! rtti)
265     {
266         throw RuntimeException(
267             OUString( RTL_CONSTASCII_USTRINGPARAM("no rtti for type ") ) +
268             *reinterpret_cast< OUString const * >( &pUnoExc->pType->pTypeName ),
269             Reference< XInterface >() );
270     }
271     }
272 
273 	__cxa_throw( pCppExc, rtti, deleteException );
274 }
275 
276 //==================================================================================================
fillUnoException(__cxa_exception * header,uno_Any * pUnoExc,uno_Mapping * pCpp2Uno)277 void fillUnoException( __cxa_exception * header, uno_Any * pUnoExc, uno_Mapping * pCpp2Uno )
278 {
279     if (! header)
280     {
281         RuntimeException aRE(
282             OUString( RTL_CONSTASCII_USTRINGPARAM("no exception header!") ),
283             Reference< XInterface >() );
284         Type const & rType = ::getCppuType( &aRE );
285         uno_type_any_constructAndConvert( pUnoExc, &aRE, rType.getTypeLibType(), pCpp2Uno );
286 #if defined _DEBUG
287         OString cstr( OUStringToOString( aRE.Message, RTL_TEXTENCODING_ASCII_US ) );
288         OSL_ENSURE( 0, cstr.getStr() );
289 #endif
290         return;
291     }
292 
293 	typelib_TypeDescription * pExcTypeDescr = 0;
294     OUString unoName( toUNOname( header->exceptionType->name() ) );
295 #if defined BRIDGES_DEBUG
296     OString cstr_unoName( OUStringToOString( unoName, RTL_TEXTENCODING_ASCII_US ) );
297     fprintf( stderr, "> c++ exception occured: %s\n", cstr_unoName.getStr() );
298 #endif
299 	typelib_typedescription_getByName( &pExcTypeDescr, unoName.pData );
300     if (0 == pExcTypeDescr)
301     {
302         RuntimeException aRE(
303             OUString( RTL_CONSTASCII_USTRINGPARAM("exception type not found: ") ) + unoName,
304             Reference< XInterface >() );
305         Type const & rType = ::getCppuType( &aRE );
306         uno_type_any_constructAndConvert( pUnoExc, &aRE, rType.getTypeLibType(), pCpp2Uno );
307 #if defined _DEBUG
308         OString cstr( OUStringToOString( aRE.Message, RTL_TEXTENCODING_ASCII_US ) );
309         OSL_ENSURE( 0, cstr.getStr() );
310 #endif
311     }
312     else
313     {
314         // construct uno exception any
315         uno_any_constructAndConvert( pUnoExc, header->adjustedPtr, pExcTypeDescr, pCpp2Uno );
316         typelib_typedescription_release( pExcTypeDescr );
317     }
318 }
319 
320 }
321 
322