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