161dff127SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
361dff127SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
461dff127SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
561dff127SAndrew Rist  * distributed with this work for additional information
661dff127SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
761dff127SAndrew Rist  * to you under the Apache License, Version 2.0 (the
861dff127SAndrew Rist  * "License"); you may not use this file except in compliance
961dff127SAndrew Rist  * with the License.  You may obtain a copy of the License at
1061dff127SAndrew Rist  *
1161dff127SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
1261dff127SAndrew Rist  *
1361dff127SAndrew Rist  * Unless required by applicable law or agreed to in writing,
1461dff127SAndrew Rist  * software distributed under the License is distributed on an
1561dff127SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
1661dff127SAndrew Rist  * KIND, either express or implied.  See the License for the
1761dff127SAndrew Rist  * specific language governing permissions and limitations
1861dff127SAndrew Rist  * under the License.
1961dff127SAndrew Rist  *
2061dff127SAndrew Rist  *************************************************************/
2161dff127SAndrew Rist 
2261dff127SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_bridges.hxx"
26cdf0e10cSrcweir 
272e13fcb7SPedro Giffuni #include <cstddef>
28fe9c34c9SPedro Giffuni #include <exception>
292e13fcb7SPedro Giffuni #include <typeinfo>
30fe9c34c9SPedro Giffuni 
31cdf0e10cSrcweir #include <stdio.h>
3281293574SPedro Giffuni #include <string.h>
33cdf0e10cSrcweir #include <dlfcn.h>
34cdf0e10cSrcweir #include <cxxabi.h>
35cdf0e10cSrcweir #include <hash_map>
36b0c85a7cSPedro Giffuni 
37cdf0e10cSrcweir #include <sys/param.h>
38cdf0e10cSrcweir 
39cdf0e10cSrcweir #include <rtl/strbuf.hxx>
40cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
41cdf0e10cSrcweir #include <osl/diagnose.h>
42cdf0e10cSrcweir #include <osl/mutex.hxx>
43cdf0e10cSrcweir 
44cdf0e10cSrcweir #include <com/sun/star/uno/genfunc.hxx>
45cdf0e10cSrcweir #include "com/sun/star/uno/RuntimeException.hpp"
46cdf0e10cSrcweir #include <typelib/typedescription.hxx>
47cdf0e10cSrcweir #include <uno/any2.h>
48cdf0e10cSrcweir 
49cdf0e10cSrcweir #include "share.hxx"
50cdf0e10cSrcweir 
51cdf0e10cSrcweir 
52cdf0e10cSrcweir using namespace ::std;
53cdf0e10cSrcweir using namespace ::osl;
54cdf0e10cSrcweir using namespace ::rtl;
55cdf0e10cSrcweir using namespace ::com::sun::star::uno;
56cdf0e10cSrcweir using namespace ::__cxxabiv1;
57cdf0e10cSrcweir 
58cdf0e10cSrcweir 
59cdf0e10cSrcweir namespace CPPU_CURRENT_NAMESPACE
60cdf0e10cSrcweir {
61cdf0e10cSrcweir 
dummy_can_throw_anything(char const *)62cdf0e10cSrcweir void dummy_can_throw_anything( char const * )
63cdf0e10cSrcweir {
64cdf0e10cSrcweir }
65cdf0e10cSrcweir 
66cdf0e10cSrcweir //==================================================================================================
toUNOname(char const * p)67cdf0e10cSrcweir static OUString toUNOname( char const * p ) SAL_THROW( () )
68cdf0e10cSrcweir {
69cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
70cdf0e10cSrcweir     char const * start = p;
71cdf0e10cSrcweir #endif
72cdf0e10cSrcweir 
73cdf0e10cSrcweir     // example: N3com3sun4star4lang24IllegalArgumentExceptionE
74cdf0e10cSrcweir 
75cdf0e10cSrcweir 	OUStringBuffer buf( 64 );
76cdf0e10cSrcweir     OSL_ASSERT( 'N' == *p );
77cdf0e10cSrcweir     ++p; // skip N
78cdf0e10cSrcweir 
79cdf0e10cSrcweir     while ('E' != *p)
80cdf0e10cSrcweir     {
81cdf0e10cSrcweir         // read chars count
82cdf0e10cSrcweir         long n = (*p++ - '0');
83cdf0e10cSrcweir         while ('0' <= *p && '9' >= *p)
84cdf0e10cSrcweir         {
85cdf0e10cSrcweir             n *= 10;
86cdf0e10cSrcweir             n += (*p++ - '0');
87cdf0e10cSrcweir         }
88cdf0e10cSrcweir         buf.appendAscii( p, n );
89cdf0e10cSrcweir         p += n;
90cdf0e10cSrcweir         if ('E' != *p)
91cdf0e10cSrcweir             buf.append( (sal_Unicode)'.' );
92cdf0e10cSrcweir     }
93cdf0e10cSrcweir 
94cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
95cdf0e10cSrcweir     OUString ret( buf.makeStringAndClear() );
96cdf0e10cSrcweir     OString c_ret( OUStringToOString( ret, RTL_TEXTENCODING_ASCII_US ) );
97cdf0e10cSrcweir     fprintf( stderr, "> toUNOname(): %s => %s\n", start, c_ret.getStr() );
98cdf0e10cSrcweir     return ret;
99cdf0e10cSrcweir #else
100cdf0e10cSrcweir     return buf.makeStringAndClear();
101cdf0e10cSrcweir #endif
102cdf0e10cSrcweir }
103cdf0e10cSrcweir 
104cdf0e10cSrcweir //==================================================================================================
105cdf0e10cSrcweir class RTTI
106cdf0e10cSrcweir {
107cdf0e10cSrcweir     typedef hash_map< OUString, type_info *, OUStringHash > t_rtti_map;
108cdf0e10cSrcweir 
109cdf0e10cSrcweir     Mutex m_mutex;
110cdf0e10cSrcweir 	t_rtti_map m_rttis;
111cdf0e10cSrcweir     t_rtti_map m_generatedRttis;
112cdf0e10cSrcweir 
113cdf0e10cSrcweir     void * m_hApp;
114cdf0e10cSrcweir 
115cdf0e10cSrcweir public:
116cdf0e10cSrcweir     RTTI() SAL_THROW( () );
117cdf0e10cSrcweir     ~RTTI() SAL_THROW( () );
118cdf0e10cSrcweir 
119cdf0e10cSrcweir     type_info * getRTTI( typelib_CompoundTypeDescription * ) SAL_THROW( () );
120cdf0e10cSrcweir };
121cdf0e10cSrcweir //__________________________________________________________________________________________________
RTTI()122cdf0e10cSrcweir RTTI::RTTI() SAL_THROW( () )
123cdf0e10cSrcweir     : m_hApp( dlopen( 0, RTLD_LAZY ) )
124cdf0e10cSrcweir {
125cdf0e10cSrcweir }
126cdf0e10cSrcweir //__________________________________________________________________________________________________
~RTTI()127cdf0e10cSrcweir RTTI::~RTTI() SAL_THROW( () )
128cdf0e10cSrcweir {
129cdf0e10cSrcweir     dlclose( m_hApp );
130cdf0e10cSrcweir }
131cdf0e10cSrcweir 
132cdf0e10cSrcweir //__________________________________________________________________________________________________
getRTTI(typelib_CompoundTypeDescription * pTypeDescr)133cdf0e10cSrcweir type_info * RTTI::getRTTI( typelib_CompoundTypeDescription *pTypeDescr ) SAL_THROW( () )
134cdf0e10cSrcweir {
135cdf0e10cSrcweir     type_info * rtti;
136cdf0e10cSrcweir 
137cdf0e10cSrcweir     OUString const & unoName = *(OUString const *)&pTypeDescr->aBase.pTypeName;
138cdf0e10cSrcweir 
139cdf0e10cSrcweir     MutexGuard guard( m_mutex );
140cdf0e10cSrcweir     t_rtti_map::const_iterator iFind( m_rttis.find( unoName ) );
141cdf0e10cSrcweir     if (iFind == m_rttis.end())
142cdf0e10cSrcweir     {
143cdf0e10cSrcweir         // RTTI symbol
144cdf0e10cSrcweir         OStringBuffer buf( 64 );
145cdf0e10cSrcweir         buf.append( RTL_CONSTASCII_STRINGPARAM("_ZTIN") );
146cdf0e10cSrcweir         sal_Int32 index = 0;
147cdf0e10cSrcweir         do
148cdf0e10cSrcweir         {
149cdf0e10cSrcweir             OUString token( unoName.getToken( 0, '.', index ) );
150cdf0e10cSrcweir             buf.append( token.getLength() );
151cdf0e10cSrcweir             OString c_token( OUStringToOString( token, RTL_TEXTENCODING_ASCII_US ) );
152cdf0e10cSrcweir             buf.append( c_token );
153cdf0e10cSrcweir         }
154cdf0e10cSrcweir         while (index >= 0);
155cdf0e10cSrcweir         buf.append( 'E' );
156cdf0e10cSrcweir 
157cdf0e10cSrcweir         OString symName( buf.makeStringAndClear() );
158b0c85a7cSPedro Giffuni         rtti = static_cast<type_info *>(dlsym( m_hApp, symName.getStr() ));
159cdf0e10cSrcweir 
160cdf0e10cSrcweir         if (rtti)
161cdf0e10cSrcweir         {
162cdf0e10cSrcweir             pair< t_rtti_map::iterator, bool > insertion(
163cdf0e10cSrcweir                 m_rttis.insert( t_rtti_map::value_type( unoName, rtti ) ) );
164cdf0e10cSrcweir             OSL_ENSURE( insertion.second, "### inserting new rtti failed?!" );
165cdf0e10cSrcweir         }
166cdf0e10cSrcweir         else
167cdf0e10cSrcweir         {
168cdf0e10cSrcweir             // try to lookup the symbol in the generated rtti map
16981293574SPedro Giffuni             t_rtti_map::const_iterator iFind2( m_generatedRttis.find( unoName ) );
17081293574SPedro Giffuni             if (iFind2 == m_generatedRttis.end())
171cdf0e10cSrcweir             {
172cdf0e10cSrcweir                 // we must generate it !
173cdf0e10cSrcweir                 // symbol and rtti-name is nearly identical,
174cdf0e10cSrcweir                 // the symbol is prefixed with _ZTI
175cdf0e10cSrcweir                 char const * rttiName = symName.getStr() +4;
176cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
177cdf0e10cSrcweir                 fprintf( stderr,"generated rtti for %s\n", rttiName );
1782e13fcb7SPedro Giffuni #ifndef __GLIBCXX__ /* #i124421# */
1792e13fcb7SPedro Giffuni                 const OString aCUnoName = OUStringToOString( unoName, RTL_TEXTENCODING_UTF8);
1802e13fcb7SPedro Giffuni                 OSL_TRACE( "TypeInfo for \"%s\" not found and cannot be generated.\n", aCUnoName.getStr());
1812e13fcb7SPedro Giffuni #endif /* __GLIBCXX__ */
182cdf0e10cSrcweir #endif
1832e13fcb7SPedro Giffuni #ifdef __GLIBCXX__ /* #i124421# */
184cdf0e10cSrcweir                 if (pTypeDescr->pBaseTypeDescription)
185cdf0e10cSrcweir                 {
186cdf0e10cSrcweir                     // ensure availability of base
187cdf0e10cSrcweir                     type_info * base_rtti = getRTTI(
188cdf0e10cSrcweir                         (typelib_CompoundTypeDescription *)pTypeDescr->pBaseTypeDescription );
189cdf0e10cSrcweir                     rtti = new __si_class_type_info(
190*1eb40c50SJim Jagielski                         strdup( rttiName ), static_cast<__class_type_info *>(base_rtti) );
191cdf0e10cSrcweir                 }
192cdf0e10cSrcweir                 else
193cdf0e10cSrcweir                 {
194cdf0e10cSrcweir                     // this class has no base class
195cdf0e10cSrcweir                     rtti = new __class_type_info( strdup( rttiName ) );
196cdf0e10cSrcweir                 }
1972e13fcb7SPedro Giffuni #else /* __GLIBCXX__ */
1982e13fcb7SPedro Giffuni                 rtti = NULL;
1992e13fcb7SPedro Giffuni #endif /* __GLIBCXX__ */
2002e13fcb7SPedro Giffuni 
201cdf0e10cSrcweir                 pair< t_rtti_map::iterator, bool > insertion(
202cdf0e10cSrcweir                     m_generatedRttis.insert( t_rtti_map::value_type( unoName, rtti ) ) );
203cdf0e10cSrcweir                 OSL_ENSURE( insertion.second, "### inserting new generated rtti failed?!" );
204cdf0e10cSrcweir             }
205cdf0e10cSrcweir             else // taking already generated rtti
206cdf0e10cSrcweir             {
20781293574SPedro Giffuni                 rtti = iFind2->second;
208cdf0e10cSrcweir             }
209cdf0e10cSrcweir         }
210cdf0e10cSrcweir     }
211cdf0e10cSrcweir     else
212cdf0e10cSrcweir     {
213cdf0e10cSrcweir         rtti = iFind->second;
214cdf0e10cSrcweir     }
215cdf0e10cSrcweir 
216cdf0e10cSrcweir     return rtti;
217cdf0e10cSrcweir }
218cdf0e10cSrcweir 
219cdf0e10cSrcweir //--------------------------------------------------------------------------------------------------
deleteException(void * pExc)220cdf0e10cSrcweir static void deleteException( void * pExc )
221cdf0e10cSrcweir {
222cdf0e10cSrcweir     __cxa_exception const * header = ((__cxa_exception const *)pExc - 1);
223cdf0e10cSrcweir     typelib_TypeDescription * pTD = 0;
224cdf0e10cSrcweir     OUString unoName( toUNOname( header->exceptionType->name() ) );
225cdf0e10cSrcweir     ::typelib_typedescription_getByName( &pTD, unoName.pData );
226cdf0e10cSrcweir     OSL_ENSURE( pTD, "### unknown exception type! leaving out destruction => leaking!!!" );
227cdf0e10cSrcweir     if (pTD)
228cdf0e10cSrcweir     {
229cdf0e10cSrcweir 		::uno_destructData( pExc, pTD, cpp_release );
230cdf0e10cSrcweir 		::typelib_typedescription_release( pTD );
231cdf0e10cSrcweir 	}
232cdf0e10cSrcweir }
233cdf0e10cSrcweir 
234cdf0e10cSrcweir //==================================================================================================
raiseException(uno_Any * pUnoExc,uno_Mapping * pUno2Cpp)235cdf0e10cSrcweir void raiseException( uno_Any * pUnoExc, uno_Mapping * pUno2Cpp )
236cdf0e10cSrcweir {
237cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
238cdf0e10cSrcweir     OString cstr(
239cdf0e10cSrcweir         OUStringToOString(
240cdf0e10cSrcweir             *reinterpret_cast< OUString const * >( &pUnoExc->pType->pTypeName ),
241cdf0e10cSrcweir             RTL_TEXTENCODING_ASCII_US ) );
24207a3d7f1SPedro Giffuni     fprintf( stderr, "> uno exception occurred: %s\n", cstr.getStr() );
243cdf0e10cSrcweir #endif
244cdf0e10cSrcweir     void * pCppExc;
245cdf0e10cSrcweir     type_info * rtti;
246cdf0e10cSrcweir 
247cdf0e10cSrcweir     {
248cdf0e10cSrcweir     // construct cpp exception object
249cdf0e10cSrcweir 	typelib_TypeDescription * pTypeDescr = 0;
250cdf0e10cSrcweir 	TYPELIB_DANGER_GET( &pTypeDescr, pUnoExc->pType );
251cdf0e10cSrcweir     OSL_ASSERT( pTypeDescr );
252cdf0e10cSrcweir     if (! pTypeDescr)
253cdf0e10cSrcweir     {
254cdf0e10cSrcweir         throw RuntimeException(
255cdf0e10cSrcweir             OUString( RTL_CONSTASCII_USTRINGPARAM("cannot get typedescription for type ") ) +
256cdf0e10cSrcweir             *reinterpret_cast< OUString const * >( &pUnoExc->pType->pTypeName ),
257cdf0e10cSrcweir             Reference< XInterface >() );
258cdf0e10cSrcweir     }
259cdf0e10cSrcweir 
260cdf0e10cSrcweir 	pCppExc = __cxa_allocate_exception( pTypeDescr->nSize );
261cdf0e10cSrcweir 	::uno_copyAndConvertData( pCppExc, pUnoExc->pData, pTypeDescr, pUno2Cpp );
262cdf0e10cSrcweir 
263cdf0e10cSrcweir 	// destruct uno exception
264cdf0e10cSrcweir 	::uno_any_destruct( pUnoExc, 0 );
265cdf0e10cSrcweir     // avoiding locked counts
266cdf0e10cSrcweir     static RTTI * s_rtti = 0;
267cdf0e10cSrcweir     if (! s_rtti)
268cdf0e10cSrcweir     {
269cdf0e10cSrcweir         MutexGuard guard( Mutex::getGlobalMutex() );
270cdf0e10cSrcweir         if (! s_rtti)
271cdf0e10cSrcweir         {
272cdf0e10cSrcweir #ifdef LEAK_STATIC_DATA
273cdf0e10cSrcweir             s_rtti = new RTTI();
274cdf0e10cSrcweir #else
275cdf0e10cSrcweir             static RTTI rtti_data;
276cdf0e10cSrcweir             s_rtti = &rtti_data;
277cdf0e10cSrcweir #endif
278cdf0e10cSrcweir         }
279cdf0e10cSrcweir     }
280cdf0e10cSrcweir 	rtti = (type_info *)s_rtti->getRTTI( (typelib_CompoundTypeDescription *) pTypeDescr );
281cdf0e10cSrcweir     TYPELIB_DANGER_RELEASE( pTypeDescr );
282cdf0e10cSrcweir     OSL_ENSURE( rtti, "### no rtti for throwing exception!" );
283cdf0e10cSrcweir     if (! rtti)
284cdf0e10cSrcweir     {
285cdf0e10cSrcweir         throw RuntimeException(
286cdf0e10cSrcweir             OUString( RTL_CONSTASCII_USTRINGPARAM("no rtti for type ") ) +
287cdf0e10cSrcweir             *reinterpret_cast< OUString const * >( &pUnoExc->pType->pTypeName ),
288cdf0e10cSrcweir             Reference< XInterface >() );
289cdf0e10cSrcweir     }
290cdf0e10cSrcweir     }
291cdf0e10cSrcweir 
292cdf0e10cSrcweir 	__cxa_throw( pCppExc, rtti, deleteException );
293cdf0e10cSrcweir }
294cdf0e10cSrcweir 
295cdf0e10cSrcweir //==================================================================================================
fillUnoException(__cxa_exception * header,uno_Any * pUnoExc,uno_Mapping * pCpp2Uno)296cdf0e10cSrcweir void fillUnoException( __cxa_exception * header, uno_Any * pUnoExc, uno_Mapping * pCpp2Uno )
297cdf0e10cSrcweir {
298cdf0e10cSrcweir     if (! header)
299cdf0e10cSrcweir     {
300cdf0e10cSrcweir         RuntimeException aRE(
301cdf0e10cSrcweir             OUString( RTL_CONSTASCII_USTRINGPARAM("no exception header!") ),
302cdf0e10cSrcweir             Reference< XInterface >() );
303cdf0e10cSrcweir         Type const & rType = ::getCppuType( &aRE );
304cdf0e10cSrcweir         uno_type_any_constructAndConvert( pUnoExc, &aRE, rType.getTypeLibType(), pCpp2Uno );
305cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 0
306cdf0e10cSrcweir         OString cstr( OUStringToOString( aRE.Message, RTL_TEXTENCODING_ASCII_US ) );
307cdf0e10cSrcweir         OSL_ENSURE( 0, cstr.getStr() );
308cdf0e10cSrcweir #endif
309cdf0e10cSrcweir         return;
310cdf0e10cSrcweir     }
311cdf0e10cSrcweir 
312cdf0e10cSrcweir 	typelib_TypeDescription * pExcTypeDescr = 0;
313cdf0e10cSrcweir     OUString unoName( toUNOname( header->exceptionType->name() ) );
314cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
315cdf0e10cSrcweir     OString cstr_unoName( OUStringToOString( unoName, RTL_TEXTENCODING_ASCII_US ) );
31607a3d7f1SPedro Giffuni     fprintf( stderr, "> c++ exception occurred: %s\n", cstr_unoName.getStr() );
317cdf0e10cSrcweir #endif
318cdf0e10cSrcweir 	typelib_typedescription_getByName( &pExcTypeDescr, unoName.pData );
319cdf0e10cSrcweir     if (0 == pExcTypeDescr)
320cdf0e10cSrcweir     {
321cdf0e10cSrcweir         RuntimeException aRE(
322cdf0e10cSrcweir             OUString( RTL_CONSTASCII_USTRINGPARAM("exception type not found: ") ) + unoName,
323cdf0e10cSrcweir             Reference< XInterface >() );
324cdf0e10cSrcweir         Type const & rType = ::getCppuType( &aRE );
325cdf0e10cSrcweir         uno_type_any_constructAndConvert( pUnoExc, &aRE, rType.getTypeLibType(), pCpp2Uno );
326cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 0
327cdf0e10cSrcweir         OString cstr( OUStringToOString( aRE.Message, RTL_TEXTENCODING_ASCII_US ) );
328cdf0e10cSrcweir         OSL_ENSURE( 0, cstr.getStr() );
329cdf0e10cSrcweir #endif
330cdf0e10cSrcweir     }
331cdf0e10cSrcweir     else
332cdf0e10cSrcweir     {
333cdf0e10cSrcweir         // construct uno exception any
334cdf0e10cSrcweir         uno_any_constructAndConvert( pUnoExc, header->adjustedPtr, pExcTypeDescr, pCpp2Uno );
335cdf0e10cSrcweir         typelib_typedescription_release( pExcTypeDescr );
336cdf0e10cSrcweir     }
337cdf0e10cSrcweir }
338cdf0e10cSrcweir 
339cdf0e10cSrcweir }
340cdf0e10cSrcweir 
341