1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
11  *   Licensed to the Apache Software Foundation (ASF) under one or more
12  *   contributor license agreements. See the NOTICE file distributed
13  *   with this work for additional information regarding copyright
14  *   ownership. The ASF licenses this file to you under the Apache
15  *   License, Version 2.0 (the "License"); you may not use this file
16  *   except in compliance with the License. You may obtain a copy of
17  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
20 
21 #include <stdio.h>
22 #include <strings.h>
23 #include <dlfcn.h>
24 #include <cxxabi.h>
25 #include <rtl/strbuf.hxx>
26 #include <rtl/ustrbuf.hxx>
27 #include <sal/log.hxx>
28 #include <osl/mutex.hxx>
29 
30 #include <com/sun/star/uno/genfunc.hxx>
31 #include "com/sun/star/uno/RuntimeException.hpp"
32 #include <typelib/typedescription.hxx>
33 #include <uno/any2.h>
34 #include <unordered_map>
35 #include "share.hxx"
36 
37 
38 using namespace ::std;
39 using namespace ::osl;
40 using namespace ::com::sun::star::uno;
41 using namespace ::__cxxabiv1;
42 
43 
44 namespace CPPU_CURRENT_NAMESPACE
45 {
46 
dummy_can_throw_anything(char const *)47 void dummy_can_throw_anything( char const * )
48 {
49 }
50 
toUNOname(char const * p)51 static OUString toUNOname( char const * p )
52 {
53 #if OSL_DEBUG_LEVEL > 1
54     char const * start = p;
55 #endif
56 
57     // example: N3com3sun4star4lang24IllegalArgumentExceptionE
58 
59     OUStringBuffer buf( 64 );
60     assert( 'N' == *p );
61     ++p; // skip N
62 
63     while ('E' != *p)
64     {
65         // read chars count
66         long n = (*p++ - '0');
67         while ('0' <= *p && '9' >= *p)
68         {
69             n *= 10;
70             n += (*p++ - '0');
71         }
72         buf.appendAscii( p, n );
73         p += n;
74         if ('E' != *p)
75             buf.append( '.' );
76     }
77 
78 #if OSL_DEBUG_LEVEL > 1
79     OUString ret( buf.makeStringAndClear() );
80     OString c_ret( OUStringToOString( ret, RTL_TEXTENCODING_ASCII_US ) );
81     fprintf( stderr, "> toUNOname(): %s => %s\n", start, c_ret.getStr() );
82     return ret;
83 #else
84     return buf.makeStringAndClear();
85 #endif
86 }
87 
88 class RTTI
89 {
90     typedef std::unordered_map< OUString, type_info * > t_rtti_map;
91 
92     Mutex m_mutex;
93     t_rtti_map m_rttis;
94     t_rtti_map m_generatedRttis;
95 
96     void * m_hApp;
97 
98 public:
99     RTTI();
100     ~RTTI();
101 
102     type_info * getRTTI( typelib_CompoundTypeDescription * );
103 };
104 
RTTI()105 RTTI::RTTI()
106     : m_hApp( dlopen( 0, RTLD_LAZY ) )
107 {
108 }
109 
~RTTI()110 RTTI::~RTTI()
111 {
112     dlclose( m_hApp );
113 }
114 
115 
getRTTI(typelib_CompoundTypeDescription * pTypeDescr)116 type_info * RTTI::getRTTI( typelib_CompoundTypeDescription *pTypeDescr )
117 {
118     type_info * rtti;
119 
120     OUString const & unoName = *(OUString const *)&pTypeDescr->aBase.pTypeName;
121 
122     MutexGuard guard( m_mutex );
123     t_rtti_map::const_iterator iFind( m_rttis.find( unoName ) );
124     if (iFind == m_rttis.end())
125     {
126         // RTTI symbol
127         OStringBuffer buf( 64 );
128         buf.append( "_ZTIN" );
129         sal_Int32 index = 0;
130         do
131         {
132             OUString token( unoName.getToken( 0, '.', index ) );
133             buf.append( token.getLength() );
134             OString c_token( OUStringToOString( token, RTL_TEXTENCODING_ASCII_US ) );
135             buf.append( c_token );
136         }
137         while (index >= 0);
138         buf.append( 'E' );
139 
140         OString symName( buf.makeStringAndClear() );
141         rtti = (type_info *)dlsym( m_hApp, symName.getStr() );
142 
143         if (rtti)
144         {
145             pair< t_rtti_map::iterator, bool > insertion(
146                 m_rttis.insert( t_rtti_map::value_type( unoName, rtti ) ) );
147             assert(insertion.second && "### inserting new rtti failed?!");
148         }
149         else
150         {
151             // try to lookup the symbol in the generated rtti map
152             t_rtti_map::const_iterator iFind( m_generatedRttis.find( unoName ) );
153             if (iFind == m_generatedRttis.end())
154             {
155                 // we must generate it !
156                 // symbol and rtti-name is nearly identical,
157                 // the symbol is prefixed with _ZTI
158                 char const * rttiName = symName.getStr() +4;
159 #if OSL_DEBUG_LEVEL > 1
160                 fprintf( stderr,"generated rtti for %s\n", rttiName );
161 #endif
162                 if (pTypeDescr->pBaseTypeDescription)
163                 {
164                     // ensure availability of base
165                     type_info * base_rtti = getRTTI(
166                         (typelib_CompoundTypeDescription *)pTypeDescr->pBaseTypeDescription );
167                     rtti = new __si_class_type_info(
168                         strdup( rttiName ), (__class_type_info *)base_rtti );
169                 }
170                 else
171                 {
172                     // this class has no base class
173                     rtti = new __class_type_info( strdup( rttiName ) );
174                 }
175 
176                 pair< t_rtti_map::iterator, bool > insertion(
177                     m_generatedRttis.insert( t_rtti_map::value_type( unoName, rtti ) ) );
178                 assert(insertion.second && "### inserting new generated rtti failed?!");
179             }
180             else // taking already generated rtti
181             {
182                 rtti = iFind->second;
183             }
184         }
185     }
186     else
187     {
188         rtti = iFind->second;
189     }
190 
191     return rtti;
192 }
193 
194 
deleteException(void * pExc)195 static void deleteException( void * pExc )
196 {
197     __cxa_exception const * header = ((__cxa_exception const *)pExc - 1);
198     typelib_TypeDescription * pTD = 0;
199     OUString unoName( toUNOname( header->exceptionType->name() ) );
200     ::typelib_typedescription_getByName( &pTD, unoName.pData );
201     assert(pTD && "### unknown exception type! leaving out destruction => leaking!!!");
202     if (pTD)
203     {
204         ::uno_destructData( pExc, pTD, cpp_release );
205         ::typelib_typedescription_release( pTD );
206     }
207 }
208 
raiseException(uno_Any * pUnoExc,uno_Mapping * pUno2Cpp)209 void raiseException( uno_Any * pUnoExc, uno_Mapping * pUno2Cpp )
210 {
211 #if OSL_DEBUG_LEVEL > 1
212     OString cstr(
213         OUStringToOString(
214             OUString::unacquired( &pUnoExc->pType->pTypeName ),
215             RTL_TEXTENCODING_ASCII_US ) );
216     fprintf( stderr, "> uno exception occurred: %s\n", cstr.getStr() );
217 #endif
218     void * pCppExc;
219     type_info * rtti;
220 
221     {
222     // construct cpp exception object
223     typelib_TypeDescription * pTypeDescr = 0;
224     TYPELIB_DANGER_GET( &pTypeDescr, pUnoExc->pType );
225     assert(pTypeDescr);
226     if (! pTypeDescr)
227     {
228         throw RuntimeException(
229             OUString("cannot get typedescription for type ") +
230             OUString::unacquired( &pUnoExc->pType->pTypeName ) );
231     }
232 
233     pCppExc = __cxa_allocate_exception( pTypeDescr->nSize );
234     ::uno_copyAndConvertData( pCppExc, pUnoExc->pData, pTypeDescr, pUno2Cpp );
235 
236     // destruct uno exception
237     ::uno_any_destruct( pUnoExc, 0 );
238     // avoiding locked counts
239     static RTTI rtti_data;
240     rtti = (type_info*)rtti_data.getRTTI((typelib_CompoundTypeDescription*)pTypeDescr);
241     TYPELIB_DANGER_RELEASE( pTypeDescr );
242     assert(rtti);
243     if (! rtti)
244     {
245         throw RuntimeException(
246             OUString("no rtti for type ") +
247             OUString::unacquired( &pUnoExc->pType->pTypeName ) );
248     }
249     }
250 
251     __cxa_throw( pCppExc, rtti, deleteException );
252 }
253 
fillUnoException(uno_Any * pUnoExc,uno_Mapping * pCpp2Uno)254 void fillUnoException(uno_Any * pUnoExc, uno_Mapping * pCpp2Uno)
255 {
256     __cxa_exception * header = __cxa_get_globals()->caughtExceptions;
257     if (! header)
258     {
259         RuntimeException aRE( "no exception header!" );
260         Type const & rType = cppu::UnoType<decltype(aRE)>::get();
261         uno_type_any_constructAndConvert( pUnoExc, &aRE, rType.getTypeLibType(), pCpp2Uno );
262         SAL_WARN("bridges", aRE.Message);
263         return;
264     }
265 
266     std::type_info *exceptionType = __cxa_current_exception_type();
267 
268     typelib_TypeDescription * pExcTypeDescr = 0;
269     OUString unoName( toUNOname( exceptionType->name() ) );
270 #if OSL_DEBUG_LEVEL > 1
271     OString cstr_unoName( OUStringToOString( unoName, RTL_TEXTENCODING_ASCII_US ) );
272     fprintf( stderr, "> c++ exception occurred: %s\n", cstr_unoName.getStr() );
273 #endif
274     typelib_typedescription_getByName( &pExcTypeDescr, unoName.pData );
275     if (0 == pExcTypeDescr)
276     {
277         RuntimeException aRE( OUString("exception type not found: ") + unoName );
278         Type const & rType = cppu::UnoType<decltype(aRE)>::get();
279         uno_type_any_constructAndConvert( pUnoExc, &aRE, rType.getTypeLibType(), pCpp2Uno );
280         SAL_WARN("bridges", aRE.Message);
281     }
282     else
283     {
284         // construct uno exception any
285         uno_any_constructAndConvert( pUnoExc, header->adjustedPtr, pExcTypeDescr, pCpp2Uno );
286         typelib_typedescription_release( pExcTypeDescr );
287     }
288 }
289 
290 }
291 
292 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
293