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 <string.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 public:
111     RTTI() SAL_THROW( () );
112     ~RTTI() SAL_THROW( () );
113 
114     type_info * getRTTI( typelib_CompoundTypeDescription * ) SAL_THROW( () );
115 };
116 //__________________________________________________________________________________________________
117 RTTI::RTTI() SAL_THROW( () )
118 {
119 }
120 //__________________________________________________________________________________________________
121 RTTI::~RTTI() SAL_THROW( () )
122 {
123 }
124 
125 //__________________________________________________________________________________________________
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 iRttiFind( m_rttis.find( unoName ) );
134     if (iRttiFind == 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             // try to lookup the symbol in the generated rtti map
152             t_rtti_map::const_iterator iFind( m_generatedRttis.find( unoName ) );
153             if (iFind == m_generatedRttis.end())
154             {
155                 // we must generate it !
156                 // symbol and rtti-name is nearly identical,
157                 // the symbol is prefixed with __ZTI
158                 char const * rttiName = symName.getStr() +5;
159 #if OSL_DEBUG_LEVEL > 1
160                 fprintf( stderr,"generated rtti for %s\n", rttiName );
161 #endif
162                 if (pTypeDescr->pBaseTypeDescription)
163                 {
164                     // ensure availability of base
165                     type_info * base_rtti = getRTTI(
166                         (typelib_CompoundTypeDescription *)pTypeDescr->pBaseTypeDescription );
167                     rtti = new __si_class_type_info(
168                         strdup( rttiName ), (__class_type_info *)base_rtti );
169                 }
170                 else
171                 {
172                     // this class has no base class
173                     rtti = new __class_type_info( strdup( rttiName ) );
174                 }
175 
176                 pair< t_rtti_map::iterator, bool > insertion(
177                     m_generatedRttis.insert( t_rtti_map::value_type( unoName, rtti ) ) );
178                 OSL_ENSURE( insertion.second, "### inserting new generated rtti failed?!" );
179             }
180             else // taking already generated rtti
181             {
182                 rtti = iFind->second;
183             }
184     }
185     else
186     {
187         rtti = iRttiFind->second;
188     }
189 
190     return rtti;
191 }
192 
193 //--------------------------------------------------------------------------------------------------
194 static void deleteException( void * pExc )
195 {
196     __cxa_exception const * header = ((__cxa_exception const *)pExc - 1);
197     typelib_TypeDescription * pTD = 0;
198     OUString unoName( toUNOname( header->exceptionType->name() ) );
199     ::typelib_typedescription_getByName( &pTD, unoName.pData );
200     OSL_ENSURE( pTD, "### unknown exception type! leaving out destruction => leaking!!!" );
201     if (pTD)
202     {
203 		::uno_destructData( pExc, pTD, cpp_release );
204 		::typelib_typedescription_release( pTD );
205 	}
206 }
207 
208 //==================================================================================================
209 void raiseException( uno_Any * pUnoExc, uno_Mapping * pUno2Cpp )
210 {
211 #if OSL_DEBUG_LEVEL > 1
212     OString cstr(
213         OUStringToOString(
214             *reinterpret_cast< OUString const * >( &pUnoExc->pType->pTypeName ),
215             RTL_TEXTENCODING_ASCII_US ) );
216     fprintf( stderr, "> uno exception occured: %s\n", cstr.getStr() );
217 #endif
218     void * pCppExc;
219     type_info * rtti;
220 
221     {
222     // construct cpp exception object
223 	typelib_TypeDescription * pTypeDescr = 0;
224 	TYPELIB_DANGER_GET( &pTypeDescr, pUnoExc->pType );
225     OSL_ASSERT( pTypeDescr );
226     if (! pTypeDescr)
227     {
228         throw RuntimeException(
229             OUString( RTL_CONSTASCII_USTRINGPARAM("cannot get typedescription for type ") ) +
230             *reinterpret_cast< OUString const * >( &pUnoExc->pType->pTypeName ),
231             Reference< XInterface >() );
232     }
233 
234 	pCppExc = __cxa_allocate_exception( pTypeDescr->nSize );
235 	::uno_copyAndConvertData( pCppExc, pUnoExc->pData, pTypeDescr, pUno2Cpp );
236 
237 	// destruct uno exception
238 	::uno_any_destruct( pUnoExc, 0 );
239     // avoiding locked counts
240     static RTTI * s_rtti = 0;
241     if (! s_rtti)
242     {
243         MutexGuard guard( Mutex::getGlobalMutex() );
244         if (! s_rtti)
245         {
246 #ifdef LEAK_STATIC_DATA
247             s_rtti = new RTTI();
248 #else
249             static RTTI rtti_data;
250             s_rtti = &rtti_data;
251 #endif
252         }
253     }
254 	rtti = (type_info *)s_rtti->getRTTI( (typelib_CompoundTypeDescription *) pTypeDescr );
255     TYPELIB_DANGER_RELEASE( pTypeDescr );
256     OSL_ENSURE( rtti, "### no rtti for throwing exception!" );
257     if (! rtti)
258     {
259         throw RuntimeException(
260             OUString( RTL_CONSTASCII_USTRINGPARAM("no rtti for type ") ) +
261             *reinterpret_cast< OUString const * >( &pUnoExc->pType->pTypeName ),
262             Reference< XInterface >() );
263     }
264     }
265 
266 	__cxa_throw( pCppExc, rtti, deleteException );
267 }
268 
269 //==================================================================================================
270 void fillUnoException( __cxa_exception * header, uno_Any * pUnoExc, uno_Mapping * pCpp2Uno )
271 {
272     if (! header)
273     {
274         RuntimeException aRE(
275             OUString( RTL_CONSTASCII_USTRINGPARAM("no exception header!") ),
276             Reference< XInterface >() );
277         Type const & rType = ::getCppuType( &aRE );
278         uno_type_any_constructAndConvert( pUnoExc, &aRE, rType.getTypeLibType(), pCpp2Uno );
279 #if OSL_DEBUG_LEVEL > 0
280         OString cstr( OUStringToOString( aRE.Message, RTL_TEXTENCODING_ASCII_US ) );
281         OSL_ENSURE( 0, cstr.getStr() );
282 #endif
283         return;
284     }
285 
286 	typelib_TypeDescription * pExcTypeDescr = 0;
287     OUString unoName( toUNOname( header->exceptionType->name() ) );
288 #if OSL_DEBUG_LEVEL > 1
289     OString cstr_unoName( OUStringToOString( unoName, RTL_TEXTENCODING_ASCII_US ) );
290     fprintf( stderr, "> c++ exception occured: %s\n", cstr_unoName.getStr() );
291 #endif
292 	typelib_typedescription_getByName( &pExcTypeDescr, unoName.pData );
293     if (0 == pExcTypeDescr)
294     {
295         RuntimeException aRE(
296             OUString( RTL_CONSTASCII_USTRINGPARAM("exception type not found: ") ) + unoName,
297             Reference< XInterface >() );
298         Type const & rType = ::getCppuType( &aRE );
299         uno_type_any_constructAndConvert( pUnoExc, &aRE, rType.getTypeLibType(), pCpp2Uno );
300 #if OSL_DEBUG_LEVEL > 0
301         OString cstr( OUStringToOString( aRE.Message, RTL_TEXTENCODING_ASCII_US ) );
302         OSL_ENSURE( 0, cstr.getStr() );
303 #endif
304     }
305     else
306     {
307         // construct uno exception any
308         uno_any_constructAndConvert( pUnoExc, header->adjustedPtr, pExcTypeDescr, pCpp2Uno );
309         typelib_typedescription_release( pExcTypeDescr );
310     }
311 }
312 
313 }
314 
315