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 #include <stdio.h>
27 #include <string.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 <com/sun/star/uno/genfunc.hxx>
38 #include <typelib/typedescription.hxx>
39 #include <uno/any2.h>
40 
41 #include "share.hxx"
42 
43 
44 using namespace ::std;
45 using namespace ::osl;
46 using namespace ::rtl;
47 using namespace ::com::sun::star::uno;
48 using namespace ::__cxxabiv1;
49 
50 
51 namespace CPPU_CURRENT_NAMESPACE
52 {
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 defined BRIDGES_DEBUG
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 defined BRIDGES_DEBUG
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 iFind( m_rttis.find( unoName ) );
133     if (iFind == 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 iiFind( m_generatedRttis.find( unoName ) );
162             if (iiFind == 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 defined BRIDGES_DEBUG
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 = iiFind->second;
192             }
193         }
194     }
195     else
196     {
197         rtti = iFind->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 defined BRIDGES_DEBUG
222     OString cstr(
223         OUStringToOString(
224             *reinterpret_cast< OUString const * >( &pUnoExc->pType->pTypeName ),
225             RTL_TEXTENCODING_ASCII_US ) );
226     fprintf( stderr, "> uno exception occurred: %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 	__cxa_throw( pCppExc, rtti, deleteException );
277 }
278 
279 //==================================================================================================
fillUnoException(__cxa_exception * header,uno_Any * pUnoExc,uno_Mapping * pCpp2Uno)280 void fillUnoException( __cxa_exception * header, uno_Any * pUnoExc, uno_Mapping * pCpp2Uno )
281 {
282     if (! header)
283     {
284         RuntimeException aRE(
285             OUString( RTL_CONSTASCII_USTRINGPARAM("no exception header!") ),
286             Reference< XInterface >() );
287         Type const & rType = ::getCppuType( &aRE );
288         uno_type_any_constructAndConvert( pUnoExc, &aRE, rType.getTypeLibType(), pCpp2Uno );
289 #if defined _DEBUG
290         OString cstr( OUStringToOString( aRE.Message, RTL_TEXTENCODING_ASCII_US ) );
291         OSL_ENSURE( 0, cstr.getStr() );
292 #endif
293         return;
294     }
295 
296 	typelib_TypeDescription * pExcTypeDescr = 0;
297     OUString unoName( toUNOname( header->exceptionType->name() ) );
298 #if defined BRIDGES_DEBUG
299     OString cstr_unoName( OUStringToOString( unoName, RTL_TEXTENCODING_ASCII_US ) );
300     fprintf( stderr, "> c++ exception occurred: %s\n", cstr_unoName.getStr() );
301 #endif
302 	typelib_typedescription_getByName( &pExcTypeDescr, unoName.pData );
303     if (0 == pExcTypeDescr)
304     {
305         RuntimeException aRE(
306             OUString( RTL_CONSTASCII_USTRINGPARAM("exception type not found: ") ) + unoName,
307             Reference< XInterface >() );
308         Type const & rType = ::getCppuType( &aRE );
309         uno_type_any_constructAndConvert( pUnoExc, &aRE, rType.getTypeLibType(), pCpp2Uno );
310 #if defined _DEBUG
311         OString cstr( OUStringToOString( aRE.Message, RTL_TEXTENCODING_ASCII_US ) );
312         OSL_ENSURE( 0, cstr.getStr() );
313 #endif
314     }
315     else
316     {
317         // construct uno exception any
318         uno_any_constructAndConvert( pUnoExc, header->adjustedPtr, pExcTypeDescr, pCpp2Uno );
319         typelib_typedescription_release( pExcTypeDescr );
320     }
321 }
322 
323 }
324 
325