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