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