LibreOffice Module bridges (master)  1
gcc3_solaris_sparc/except.cxx
Go to the documentation of this file.
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 <stdio.h>
21 #include <dlfcn.h>
22 #include <cxxabi.h>
23 #include <rtl/strbuf.hxx>
24 #include <rtl/ustrbuf.hxx>
25 #include <sal/log.hxx>
26 #include <osl/mutex.hxx>
27 
28 #include <com/sun/star/uno/genfunc.hxx>
29 #include <typelib/typedescription.hxx>
30 #include <uno/any2.h>
31 #include <unordered_map>
32 #include "share.hxx"
33 
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 
44 void dummy_can_throw_anything( char const * )
45 {
46 }
47 
48 static OUString toUNOname( char const * p )
49 {
50 #if defined BRIDGES_DEBUG
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 defined BRIDGES_DEBUG
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  void * m_hApp;
94 
95 public:
96  RTTI();
97  ~RTTI();
98 
99  type_info * getRTTI( typelib_CompoundTypeDescription * );
100 };
101 
102 RTTI::RTTI()
103  : m_hApp( dlopen( 0, RTLD_LAZY ) )
104 {
105 }
106 
107 RTTI::~RTTI()
108 {
109  dlclose( m_hApp );
110 }
111 
112 
113 type_info * RTTI::getRTTI( typelib_CompoundTypeDescription *pTypeDescr )
114 {
115  type_info * rtti;
116 
117  OUString const & unoName = *(OUString const *)&pTypeDescr->aBase.pTypeName;
118 
119  MutexGuard guard( m_mutex );
120  t_rtti_map::const_iterator iFind( m_rttis.find( unoName ) );
121  if (iFind == m_rttis.end())
122  {
123  // RTTI symbol
124  OStringBuffer buf( 64 );
125  buf.append( "_ZTIN" );
126  sal_Int32 index = 0;
127  do
128  {
129  OUString token( unoName.getToken( 0, '.', index ) );
130  buf.append( token.getLength() );
131  OString c_token( OUStringToOString( token, RTL_TEXTENCODING_ASCII_US ) );
132  buf.append( c_token );
133  }
134  while (index >= 0);
135  buf.append( 'E' );
136 
137  OString symName( buf.makeStringAndClear() );
138  rtti = (type_info *)dlsym( m_hApp, symName.getStr() );
139 
140  if (rtti)
141  {
142  pair< t_rtti_map::iterator, bool > insertion(
143  m_rttis.insert( t_rtti_map::value_type( unoName, rtti ) ) );
144  assert(insertion.second && "### inserting new rtti failed?!");
145  }
146  else
147  {
148  // try to lookup the symbol in the generated rtti map
149  t_rtti_map::const_iterator iFind( m_generatedRttis.find( unoName ) );
150  if (iFind == m_generatedRttis.end())
151  {
152  // we must generate it !
153  // symbol and rtti-name is nearly identical,
154  // the symbol is prefixed with _ZTI
155  char const * rttiName = symName.getStr() +4;
156 #if defined BRIDGES_DEBUG
157  fprintf( stderr,"generated rtti for %s\n", rttiName );
158 #endif
159  if (pTypeDescr->pBaseTypeDescription)
160  {
161  // ensure availability of base
162  type_info * base_rtti = getRTTI(
163  (typelib_CompoundTypeDescription *)pTypeDescr->pBaseTypeDescription );
164  rtti = new __si_class_type_info(
165  strdup( rttiName ), (__class_type_info *)base_rtti );
166  }
167  else
168  {
169  // this class has no base class
170  rtti = new __class_type_info( strdup( rttiName ) );
171  }
172 
173  pair< t_rtti_map::iterator, bool > insertion(
174  m_generatedRttis.insert( t_rtti_map::value_type( unoName, rtti ) ) );
175  assert(insertion.second && "### inserting new generated rtti failed?!");
176  }
177  else // taking already generated rtti
178  {
179  rtti = iFind->second;
180  }
181  }
182  }
183  else
184  {
185  rtti = iFind->second;
186  }
187 
188  return rtti;
189 }
190 
191 
192 static void deleteException( void * pExc )
193 {
194  __cxa_exception const * header = ((__cxa_exception const *)pExc - 1);
195  typelib_TypeDescription * pTD = 0;
196  OUString unoName( toUNOname( header->exceptionType->name() ) );
197  ::typelib_typedescription_getByName( &pTD, unoName.pData );
198  assert(pTD && "### unknown exception type! leaving out destruction => leaking!!!");
199  if (pTD)
200  {
201  ::uno_destructData( pExc, pTD, cpp_release );
202  ::typelib_typedescription_release( pTD );
203  }
204 }
205 
206 void raiseException( uno_Any * pUnoExc, uno_Mapping * pUno2Cpp )
207 {
208 #if defined BRIDGES_DEBUG
209  OString cstr(
211  OUString::unacquired( &pUnoExc->pType->pTypeName ),
212  RTL_TEXTENCODING_ASCII_US ) );
213  fprintf( stderr, "> uno exception occurred: %s\n", cstr.getStr() );
214 #endif
215  void * pCppExc;
216  type_info * rtti;
217 
218  {
219  // construct cpp exception object
220  typelib_TypeDescription * pTypeDescr = 0;
221  TYPELIB_DANGER_GET( &pTypeDescr, pUnoExc->pType );
222  assert(pTypeDescr);
223  if (! pTypeDescr)
224  {
225  throw RuntimeException(
226  OUString("cannot get typedescription for type ") +
227  OUString::unacquired( &pUnoExc->pType->pTypeName ) );
228  }
229 
230  pCppExc = __cxa_allocate_exception( pTypeDescr->nSize );
231  ::uno_copyAndConvertData( pCppExc, pUnoExc->pData, pTypeDescr, pUno2Cpp );
232 
233  // destruct uno exception
234  ::uno_any_destruct( pUnoExc, 0 );
235  // avoiding locked counts
236  static RTTI rtti_data;
237  rtti = (type_info*)rtti_data.getRTTI((typelib_CompoundTypeDescription*)pTypeDescr);
238  TYPELIB_DANGER_RELEASE( pTypeDescr );
239  assert(rtti && "### no rtti for throwing exception!");
240  if (! rtti)
241  {
242  throw RuntimeException(
243  OUString("no rtti for type ") +
244  OUString::unacquired( &pUnoExc->pType->pTypeName ) );
245  }
246  }
247 
248  __cxa_throw( pCppExc, rtti, deleteException );
249 }
250 
251 void fillUnoException(uno_Any * pUnoExc, uno_Mapping * pCpp2Uno)
252 {
253  __cxa_exception * header = __cxa_get_globals()->caughtExceptions;
254  if (! header)
255  {
256  RuntimeException aRE( "no exception header!" );
257  Type const & rType = cppu::UnoType<decltype(aRE)>::get();
258  uno_type_any_constructAndConvert( pUnoExc, &aRE, rType.getTypeLibType(), pCpp2Uno );
259  SAL_WARN("bridges", aRE.Message);
260  return;
261  }
262 
263  std::type_info *exceptionType = __cxa_current_exception_type();
264 
265  typelib_TypeDescription * pExcTypeDescr = 0;
266  OUString unoName( toUNOname( exceptionType->name() ) );
267 #if defined BRIDGES_DEBUG
268  OString cstr_unoName( OUStringToOString( unoName, RTL_TEXTENCODING_ASCII_US ) );
269  fprintf( stderr, "> c++ exception occurred: %s\n", cstr_unoName.getStr() );
270 #endif
271  typelib_typedescription_getByName( &pExcTypeDescr, unoName.pData );
272  if (0 == pExcTypeDescr)
273  {
274  RuntimeException aRE( OUString("exception type not found: ") + unoName );
275  Type const & rType = cppu::UnoType<decltype(aRE)>::get();
276  uno_type_any_constructAndConvert( pUnoExc, &aRE, rType.getTypeLibType(), pCpp2Uno );
277  SAL_WARN("bridges", aRE.Message);
278  }
279  else
280  {
281  // construct uno exception any
282  uno_any_constructAndConvert( pUnoExc, header->adjustedPtr, pExcTypeDescr, pCpp2Uno );
283  typelib_typedescription_release( pExcTypeDescr );
284  }
285 }
286 
287 }
288 
289 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Type
static void deleteException(void *pExc)
static OUString toUNOname(char const *p)
struct _uno_Mapping uno_Mapping
Definition: msvc/except.hxx:32
void fillUnoException(uno_Any *pExc, uno_Mapping *pCpp2Uno)
sal_Int64 n
void * __cxa_allocate_exception(std::size_t thrown_size)
void SAL_CALL uno_type_any_constructAndConvert(uno_Any *pDest, void *pSource, typelib_TypeDescriptionReference *pType, uno_Mapping *mapping) SAL_THROW_EXTERN_C()
void * m_hApp
void SAL_CALL uno_any_constructAndConvert(uno_Any *pDest, void *pSource, typelib_TypeDescription *pTypeDescr, uno_Mapping *mapping) SAL_THROW_EXTERN_C()
const BorderLinePrimitive2D *pCandidateB assert(pCandidateA)
const sal_Int8 header[]
std::type_info * __cxa_current_exception_type()
OString OUStringToOString(std::u16string_view str, ConnectionSettings const *settings)
struct _uno_Any uno_Any
Definition: msvc/except.hxx:31
type_info * getRTTI(typelib_CompoundTypeDescription *)
tuple index
struct _typelib_TypeDescription typelib_TypeDescription
Definition: msvc/except.hxx:52
void SAL_CALL typelib_typedescription_release(typelib_TypeDescription *pTD) SAL_THROW_EXTERN_C()
void SAL_CALL typelib_typedescription_getByName(typelib_TypeDescription **ppRet, rtl_uString *pName) SAL_THROW_EXTERN_C()
void __cxa_throw(void *thrown_exception, std::type_info *tinfo, void(*dest)(void *)) __attribute__((noreturn))
std::unordered_map< OUString, type_info * > t_rtti_map
void raiseException(uno_Any *pUnoExc, uno_Mapping *pUno2Cpp)
__cxa_eh_globals * __cxa_get_globals()
void * p
#define SAL_WARN(area, stream)
void dummy_can_throw_anything(char const *)