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