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