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 <dlfcn.h>
30 #include <cxxabi.h>
31 #include <hash_map>
32 #include <sys/param.h>
33 
34 #include <rtl/strbuf.hxx>
35 #include <rtl/ustrbuf.hxx>
36 #include <osl/diagnose.h>
37 #include <osl/mutex.hxx>
38 
39 #include <com/sun/star/uno/genfunc.hxx>
40 #include "com/sun/star/uno/RuntimeException.hpp"
41 #include <typelib/typedescription.hxx>
42 #include <uno/any2.h>
43 
44 #include "share.hxx"
45 
46 
47 using namespace ::std;
48 using namespace ::osl;
49 using namespace ::rtl;
50 using namespace ::com::sun::star::uno;
51 using namespace ::__cxxabiv1;
52 
53 
54 namespace CPPU_CURRENT_NAMESPACE
55 {
56 
57 void dummy_can_throw_anything( char const * )
58 {
59 }
60 
61 //==================================================================================================
62 static OUString toUNOname( char const * p ) SAL_THROW( () )
63 {
64 #if OSL_DEBUG_LEVEL > 1
65     char const * start = p;
66 #endif
67 
68     // example: N3com3sun4star4lang24IllegalArgumentExceptionE
69 
70 	OUStringBuffer buf( 64 );
71     OSL_ASSERT( 'N' == *p );
72     ++p; // skip N
73 
74     while ('E' != *p)
75     {
76         // read chars count
77         long n = (*p++ - '0');
78         while ('0' <= *p && '9' >= *p)
79         {
80             n *= 10;
81             n += (*p++ - '0');
82         }
83         buf.appendAscii( p, n );
84         p += n;
85         if ('E' != *p)
86             buf.append( (sal_Unicode)'.' );
87     }
88 
89 #if OSL_DEBUG_LEVEL > 1
90     OUString ret( buf.makeStringAndClear() );
91     OString c_ret( OUStringToOString( ret, RTL_TEXTENCODING_ASCII_US ) );
92     fprintf( stderr, "> toUNOname(): %s => %s\n", start, c_ret.getStr() );
93     return ret;
94 #else
95     return buf.makeStringAndClear();
96 #endif
97 }
98 
99 //==================================================================================================
100 class RTTI
101 {
102     typedef hash_map< OUString, type_info *, OUStringHash > t_rtti_map;
103 
104     Mutex m_mutex;
105 	t_rtti_map m_rttis;
106     t_rtti_map m_generatedRttis;
107 
108     void * m_hApp;
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 #if __FreeBSD_version < 702104 /* #i22253# */
119     : m_hApp( dlopen( 0, RTLD_NOW | RTLD_GLOBAL ) )
120 #else
121     : m_hApp( dlopen( 0, RTLD_LAZY ) )
122 #endif
123 {
124 }
125 //__________________________________________________________________________________________________
126 RTTI::~RTTI() SAL_THROW( () )
127 {
128     dlclose( m_hApp );
129 }
130 
131 //__________________________________________________________________________________________________
132 type_info * RTTI::getRTTI( typelib_CompoundTypeDescription *pTypeDescr ) SAL_THROW( () )
133 {
134     type_info * rtti;
135 
136     OUString const & unoName = *(OUString const *)&pTypeDescr->aBase.pTypeName;
137 
138     MutexGuard guard( m_mutex );
139     t_rtti_map::const_iterator iFind( m_rttis.find( unoName ) );
140     if (iFind == m_rttis.end())
141     {
142         // RTTI symbol
143         OStringBuffer buf( 64 );
144         buf.append( RTL_CONSTASCII_STRINGPARAM("_ZTIN") );
145         sal_Int32 index = 0;
146         do
147         {
148             OUString token( unoName.getToken( 0, '.', index ) );
149             buf.append( token.getLength() );
150             OString c_token( OUStringToOString( token, RTL_TEXTENCODING_ASCII_US ) );
151             buf.append( c_token );
152         }
153         while (index >= 0);
154         buf.append( 'E' );
155 
156         OString symName( buf.makeStringAndClear() );
157 #if __FreeBSD_version < 702104 /* #i22253# */
158         rtti = (type_info *)dlsym( RTLD_DEFAULT, symName.getStr() );
159 #else
160         rtti = (type_info *)dlsym( m_hApp, symName.getStr() );
161 #endif
162 
163         if (rtti)
164         {
165             pair< t_rtti_map::iterator, bool > insertion(
166                 m_rttis.insert( t_rtti_map::value_type( unoName, rtti ) ) );
167             OSL_ENSURE( insertion.second, "### inserting new rtti failed?!" );
168         }
169         else
170         {
171             // try to lookup the symbol in the generated rtti map
172             t_rtti_map::const_iterator iFind2( m_generatedRttis.find( unoName ) );
173             if (iFind2 == 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() +4;
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 = iFind2->second;
203             }
204         }
205     }
206     else
207     {
208         rtti = iFind->second;
209     }
210 
211     return rtti;
212 }
213 
214 //--------------------------------------------------------------------------------------------------
215 static void deleteException( void * pExc )
216 {
217     __cxa_exception const * header = ((__cxa_exception const *)pExc - 1);
218     typelib_TypeDescription * pTD = 0;
219     OUString unoName( toUNOname( header->exceptionType->name() ) );
220     ::typelib_typedescription_getByName( &pTD, unoName.pData );
221     OSL_ENSURE( pTD, "### unknown exception type! leaving out destruction => leaking!!!" );
222     if (pTD)
223     {
224 		::uno_destructData( pExc, pTD, cpp_release );
225 		::typelib_typedescription_release( pTD );
226 	}
227 }
228 
229 //==================================================================================================
230 void raiseException( uno_Any * pUnoExc, uno_Mapping * pUno2Cpp )
231 {
232 #if OSL_DEBUG_LEVEL > 1
233     OString cstr(
234         OUStringToOString(
235             *reinterpret_cast< OUString const * >( &pUnoExc->pType->pTypeName ),
236             RTL_TEXTENCODING_ASCII_US ) );
237     fprintf( stderr, "> uno exception occured: %s\n", cstr.getStr() );
238 #endif
239     void * pCppExc;
240     type_info * rtti;
241 
242     {
243     // construct cpp exception object
244 	typelib_TypeDescription * pTypeDescr = 0;
245 	TYPELIB_DANGER_GET( &pTypeDescr, pUnoExc->pType );
246     OSL_ASSERT( pTypeDescr );
247     if (! pTypeDescr)
248     {
249         throw RuntimeException(
250             OUString( RTL_CONSTASCII_USTRINGPARAM("cannot get typedescription for type ") ) +
251             *reinterpret_cast< OUString const * >( &pUnoExc->pType->pTypeName ),
252             Reference< XInterface >() );
253     }
254 
255 	pCppExc = __cxa_allocate_exception( pTypeDescr->nSize );
256 	::uno_copyAndConvertData( pCppExc, pUnoExc->pData, pTypeDescr, pUno2Cpp );
257 
258 	// destruct uno exception
259 	::uno_any_destruct( pUnoExc, 0 );
260     // avoiding locked counts
261     static RTTI * s_rtti = 0;
262     if (! s_rtti)
263     {
264         MutexGuard guard( Mutex::getGlobalMutex() );
265         if (! s_rtti)
266         {
267 #ifdef LEAK_STATIC_DATA
268             s_rtti = new RTTI();
269 #else
270             static RTTI rtti_data;
271             s_rtti = &rtti_data;
272 #endif
273         }
274     }
275 	rtti = (type_info *)s_rtti->getRTTI( (typelib_CompoundTypeDescription *) pTypeDescr );
276     TYPELIB_DANGER_RELEASE( pTypeDescr );
277     OSL_ENSURE( rtti, "### no rtti for throwing exception!" );
278     if (! rtti)
279     {
280         throw RuntimeException(
281             OUString( RTL_CONSTASCII_USTRINGPARAM("no rtti for type ") ) +
282             *reinterpret_cast< OUString const * >( &pUnoExc->pType->pTypeName ),
283             Reference< XInterface >() );
284     }
285     }
286 
287 	__cxa_throw( pCppExc, rtti, deleteException );
288 }
289 
290 //==================================================================================================
291 void fillUnoException( __cxa_exception * header, uno_Any * pUnoExc, uno_Mapping * pCpp2Uno )
292 {
293     if (! header)
294     {
295         RuntimeException aRE(
296             OUString( RTL_CONSTASCII_USTRINGPARAM("no exception header!") ),
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         return;
305     }
306 
307 	typelib_TypeDescription * pExcTypeDescr = 0;
308     OUString unoName( toUNOname( header->exceptionType->name() ) );
309 #if OSL_DEBUG_LEVEL > 1
310     OString cstr_unoName( OUStringToOString( unoName, RTL_TEXTENCODING_ASCII_US ) );
311     fprintf( stderr, "> c++ exception occured: %s\n", cstr_unoName.getStr() );
312 #endif
313 	typelib_typedescription_getByName( &pExcTypeDescr, unoName.pData );
314     if (0 == pExcTypeDescr)
315     {
316         RuntimeException aRE(
317             OUString( RTL_CONSTASCII_USTRINGPARAM("exception type not found: ") ) + unoName,
318             Reference< XInterface >() );
319         Type const & rType = ::getCppuType( &aRE );
320         uno_type_any_constructAndConvert( pUnoExc, &aRE, rType.getTypeLibType(), pCpp2Uno );
321 #if OSL_DEBUG_LEVEL > 0
322         OString cstr( OUStringToOString( aRE.Message, RTL_TEXTENCODING_ASCII_US ) );
323         OSL_ENSURE( 0, cstr.getStr() );
324 #endif
325     }
326     else
327     {
328         // construct uno exception any
329         uno_any_constructAndConvert( pUnoExc, header->adjustedPtr, pExcTypeDescr, pCpp2Uno );
330         typelib_typedescription_release( pExcTypeDescr );
331     }
332 }
333 
334 }
335 
336