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 <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 #ifdef 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 #ifdef 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 #ifdef 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     void * pCppExc;
222     type_info * rtti;
223 
224     {
225     // construct cpp exception object
226 	typelib_TypeDescription * pTypeDescr = 0;
227 	TYPELIB_DANGER_GET( &pTypeDescr, pUnoExc->pType );
228     OSL_ASSERT( pTypeDescr );
229     if (! pTypeDescr)
230         terminate();
231 
232 	pCppExc = __cxa_allocate_exception( pTypeDescr->nSize );
233 	::uno_copyAndConvertData( pCppExc, pUnoExc->pData, pTypeDescr, pUno2Cpp );
234 
235 	// destruct uno exception
236 	::uno_any_destruct( pUnoExc, 0 );
237     // avoiding locked counts
238     static RTTI * s_rtti = 0;
239     if (! s_rtti)
240     {
241         MutexGuard guard( Mutex::getGlobalMutex() );
242         if (! s_rtti)
243         {
244 #ifdef LEAK_STATIC_DATA
245             s_rtti = new RTTI();
246 #else
247             static RTTI rtti_data;
248             s_rtti = &rtti_data;
249 #endif
250         }
251     }
252 	rtti = (type_info *)s_rtti->getRTTI( (typelib_CompoundTypeDescription *) pTypeDescr );
253     TYPELIB_DANGER_RELEASE( pTypeDescr );
254     OSL_ENSURE( rtti, "### no rtti for throwing exception!" );
255     if (! rtti)
256         terminate();
257     }
258 
259 	__cxa_throw( pCppExc, rtti, deleteException );
260 }
261 
262 //==================================================================================================
fillUnoException(__cxa_exception * header,uno_Any * pExc,uno_Mapping * pCpp2Uno)263 void fillUnoException( __cxa_exception * header, uno_Any * pExc, uno_Mapping * pCpp2Uno )
264 {
265     OSL_ENSURE( header, "### no exception header!!!" );
266     if (! header)
267         terminate();
268 
269 	typelib_TypeDescription * pExcTypeDescr = 0;
270     OUString unoName( toUNOname( header->exceptionType->name() ) );
271 	::typelib_typedescription_getByName( &pExcTypeDescr, unoName.pData );
272     OSL_ENSURE( pExcTypeDescr, "### can not get type description for exception!!!" );
273     if (! pExcTypeDescr)
274         terminate();
275 
276     // construct uno exception any
277     ::uno_any_constructAndConvert( pExc, header->adjustedPtr, pExcTypeDescr, pCpp2Uno );
278     ::typelib_typedescription_release( pExcTypeDescr );
279 }
280 
281 }
282 
283