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