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