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