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