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