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