1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_bridges.hxx"
30 
31 #include <stdio.h>
32 #include <string.h>
33 #include <dlfcn.h>
34 #include <cxxabi.h>
35 #include <hash_map>
36 
37 #include <rtl/strbuf.hxx>
38 #include <rtl/ustrbuf.hxx>
39 #include <osl/diagnose.h>
40 #include <osl/mutex.hxx>
41 
42 #include <com/sun/star/uno/genfunc.hxx>
43 #include <typelib/typedescription.hxx>
44 #include <uno/any2.h>
45 
46 #include "share.hxx"
47 
48 
49 using namespace ::std;
50 using namespace ::osl;
51 using namespace ::rtl;
52 using namespace ::com::sun::star::uno;
53 using namespace ::__cxxabiv1;
54 
55 
56 namespace CPPU_CURRENT_NAMESPACE
57 {
58 
59 void dummy_can_throw_anything( char const * )
60 {
61 }
62 
63 //==================================================================================================
64 static OUString toUNOname( char const * p ) SAL_THROW( () )
65 {
66 #if OSL_DEBUG_LEVEL > 1
67     char const * start = p;
68 #endif
69 
70     // example: N3com3sun4star4lang24IllegalArgumentExceptionE
71 
72 	OUStringBuffer buf( 64 );
73     OSL_ASSERT( 'N' == *p );
74     ++p; // skip N
75 
76     while ('E' != *p)
77     {
78         // read chars count
79         long n = (*p++ - '0');
80         while ('0' <= *p && '9' >= *p)
81         {
82             n *= 10;
83             n += (*p++ - '0');
84         }
85         buf.appendAscii( p, n );
86         p += n;
87         if ('E' != *p)
88             buf.append( (sal_Unicode)'.' );
89     }
90 
91 #if OSL_DEBUG_LEVEL > 1
92     OUString ret( buf.makeStringAndClear() );
93     OString c_ret( OUStringToOString( ret, RTL_TEXTENCODING_ASCII_US ) );
94     fprintf( stderr, "> toUNOname(): %s => %s\n", start, c_ret.getStr() );
95     return ret;
96 #else
97     return buf.makeStringAndClear();
98 #endif
99 }
100 
101 //==================================================================================================
102 class RTTI
103 {
104     typedef hash_map< OUString, type_info *, OUStringHash > t_rtti_map;
105 
106     Mutex m_mutex;
107 	t_rtti_map m_rttis;
108     t_rtti_map m_generatedRttis;
109 
110     void * m_hApp;
111 
112 public:
113     RTTI() SAL_THROW( () );
114     ~RTTI() SAL_THROW( () );
115 
116     type_info * getRTTI( typelib_CompoundTypeDescription * ) SAL_THROW( () );
117 };
118 //__________________________________________________________________________________________________
119 RTTI::RTTI() SAL_THROW( () )
120     : m_hApp( dlopen( 0, RTLD_LAZY ) )
121 {
122 }
123 //__________________________________________________________________________________________________
124 RTTI::~RTTI() SAL_THROW( () )
125 {
126     dlclose( m_hApp );
127 }
128 
129 //__________________________________________________________________________________________________
130 type_info * RTTI::getRTTI( typelib_CompoundTypeDescription *pTypeDescr ) SAL_THROW( () )
131 {
132     type_info * rtti;
133 
134     OUString const & unoName = *(OUString const *)&pTypeDescr->aBase.pTypeName;
135 
136     MutexGuard guard( m_mutex );
137     t_rtti_map::const_iterator iRttiFind( m_rttis.find( unoName ) );
138     if (iRttiFind == m_rttis.end())
139     {
140         // RTTI symbol
141         OStringBuffer buf( 64 );
142         buf.append( RTL_CONSTASCII_STRINGPARAM("_ZTIN") );
143         sal_Int32 index = 0;
144         do
145         {
146             OUString token( unoName.getToken( 0, '.', index ) );
147             buf.append( token.getLength() );
148             OString c_token( OUStringToOString( token, RTL_TEXTENCODING_ASCII_US ) );
149             buf.append( c_token );
150         }
151         while (index >= 0);
152         buf.append( 'E' );
153 
154         OString symName( buf.makeStringAndClear() );
155         rtti = (type_info *)dlsym( m_hApp, symName.getStr() );
156 
157         if (rtti)
158         {
159             pair< t_rtti_map::iterator, bool > insertion(
160                 m_rttis.insert( t_rtti_map::value_type( unoName, rtti ) ) );
161             OSL_ENSURE( insertion.second, "### inserting new rtti failed?!" );
162         }
163         else
164         {
165             // try to lookup the symbol in the generated rtti map
166             t_rtti_map::const_iterator iFind( m_generatedRttis.find( unoName ) );
167             if (iFind == m_generatedRttis.end())
168             {
169                 // we must generate it !
170                 // symbol and rtti-name is nearly identical,
171                 // the symbol is prefixed with _ZTI
172                 char const * rttiName = symName.getStr() +4;
173 #if OSL_DEBUG_LEVEL > 1
174                 fprintf( stderr,"generated rtti for %s\n", rttiName );
175 #endif
176                 if (pTypeDescr->pBaseTypeDescription)
177                 {
178                     // ensure availability of base
179                     type_info * base_rtti = getRTTI(
180                         (typelib_CompoundTypeDescription *)pTypeDescr->pBaseTypeDescription );
181                     rtti = new __si_class_type_info(
182                         strdup( rttiName ), (__class_type_info *)base_rtti );
183                 }
184                 else
185                 {
186                     // this class has no base class
187                     rtti = new __class_type_info( strdup( rttiName ) );
188                 }
189 
190                 pair< t_rtti_map::iterator, bool > insertion(
191                     m_generatedRttis.insert( t_rtti_map::value_type( unoName, rtti ) ) );
192                 OSL_ENSURE( insertion.second, "### inserting new generated rtti failed?!" );
193             }
194             else // taking already generated rtti
195             {
196                 rtti = iFind->second;
197             }
198         }
199     }
200     else
201     {
202         rtti = iRttiFind->second;
203     }
204 
205     return rtti;
206 }
207 
208 //--------------------------------------------------------------------------------------------------
209 static void deleteException( void * pExc )
210 {
211     __cxa_exception const * header = ((__cxa_exception const *)pExc - 1);
212     typelib_TypeDescription * pTD = 0;
213     OUString unoName( toUNOname( header->exceptionType->name() ) );
214     ::typelib_typedescription_getByName( &pTD, unoName.pData );
215     OSL_ENSURE( pTD, "### unknown exception type! leaving out destruction => leaking!!!" );
216     if (pTD)
217     {
218 		::uno_destructData( pExc, pTD, cpp_release );
219 		::typelib_typedescription_release( pTD );
220 	}
221 }
222 
223 //==================================================================================================
224 void raiseException( uno_Any * pUnoExc, uno_Mapping * pUno2Cpp )
225 {
226     void * pCppExc;
227     type_info * rtti;
228 
229     {
230     // construct cpp exception object
231 	typelib_TypeDescription * pTypeDescr = 0;
232 	TYPELIB_DANGER_GET( &pTypeDescr, pUnoExc->pType );
233     OSL_ASSERT( pTypeDescr );
234     if (! pTypeDescr)
235         terminate();
236 
237 	pCppExc = __cxa_allocate_exception( pTypeDescr->nSize );
238 	::uno_copyAndConvertData( pCppExc, pUnoExc->pData, pTypeDescr, pUno2Cpp );
239 
240 	// destruct uno exception
241 	::uno_any_destruct( pUnoExc, 0 );
242     // avoiding locked counts
243     static RTTI * s_rtti = 0;
244     if (! s_rtti)
245     {
246         MutexGuard guard( Mutex::getGlobalMutex() );
247         if (! s_rtti)
248         {
249 #ifdef LEAK_STATIC_DATA
250             s_rtti = new RTTI();
251 #else
252             static RTTI rtti_data;
253             s_rtti = &rtti_data;
254 #endif
255         }
256     }
257 	rtti = (type_info *)s_rtti->getRTTI( (typelib_CompoundTypeDescription *) pTypeDescr );
258     TYPELIB_DANGER_RELEASE( pTypeDescr );
259     OSL_ENSURE( rtti, "### no rtti for throwing exception!" );
260     if (! rtti)
261         terminate();
262     }
263 
264 	__cxa_throw( pCppExc, rtti, deleteException );
265 }
266 
267 //==================================================================================================
268 void fillUnoException( __cxa_exception * header, uno_Any * pExc, uno_Mapping * pCpp2Uno )
269 {
270     OSL_ENSURE( header, "### no exception header!!!" );
271     if (! header)
272         terminate();
273 
274 	typelib_TypeDescription * pExcTypeDescr = 0;
275     OUString unoName( toUNOname( header->exceptionType->name() ) );
276 	::typelib_typedescription_getByName( &pExcTypeDescr, unoName.pData );
277     OSL_ENSURE( pExcTypeDescr, "### can not get type description for exception!!!" );
278     if (! pExcTypeDescr)
279         terminate();
280 
281     // construct uno exception any
282     ::uno_any_constructAndConvert( pExc, header->adjustedPtr, pExcTypeDescr, pCpp2Uno );
283     ::typelib_typedescription_release( pExcTypeDescr );
284 }
285 
286 }
287 
288