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