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