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