LibreOffice Module bridges (master)  1
gcc3_linux_x86-64/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 
21 #include <stdio.h>
22 #include <string.h>
23 
24 #include <rtl/ustrbuf.hxx>
25 #include <sal/log.hxx>
26 
27 #include <com/sun/star/uno/genfunc.hxx>
28 #include <com/sun/star/uno/RuntimeException.hpp>
29 #include <typelib/typedescription.hxx>
30 #include <uno/any2.h>
31 
32 #include "rtti.hxx"
33 #include "share.hxx"
34 
35 
36 using namespace ::std;
37 using namespace ::com::sun::star::uno;
38 using namespace ::__cxxabiv1;
39 
40 
41 namespace CPPU_CURRENT_NAMESPACE
42 {
43 
44 static OUString toUNOname( char const * p )
45 {
46 #if OSL_DEBUG_LEVEL > 1
47  char const * start = p;
48 #endif
49 
50  // example: N3com3sun4star4lang24IllegalArgumentExceptionE
51 
52  OUStringBuffer buf( 64 );
53  assert( *p == 'N' );
54  ++p; // skip N
55 
56  while (*p != 'E')
57  {
58  // read chars count
59  int n = *p++ - '0';
60  while ('0' <= *p && '9' >= *p)
61  {
62  n *= 10;
63  n += (*p++ - '0');
64  }
65  buf.appendAscii( p, n );
66  p += n;
67  if (*p != 'E')
68  buf.append( '.' );
69  }
70 
71 #if OSL_DEBUG_LEVEL > 1
72  OUString ret( buf.makeStringAndClear() );
73  OString c_ret( OUStringToOString( ret, RTL_TEXTENCODING_ASCII_US ) );
74  fprintf( stderr, "> toUNOname(): %s => %s\n", start, c_ret.getStr() );
75  return ret;
76 #else
77  return buf.makeStringAndClear();
78 #endif
79 }
80 
81 extern "C" {
82 static void _GLIBCXX_CDTOR_CALLABI deleteException( void * pExc )
83 {
84  __cxxabiv1::__cxa_exception const * header = static_cast<__cxxabiv1::__cxa_exception const *>(pExc) - 1;
85 #if defined _LIBCPPABI_VERSION // detect libc++abi
86  // First, the libcxxabi commit
87  // <http://llvm.org/viewvc/llvm-project?view=revision&revision=303175>
88  // "[libcxxabi] Align unwindHeader on a double-word boundary" towards
89  // LLVM 5.0 changed the size of __cxa_exception by adding
90  //
91  // __attribute__((aligned))
92  //
93  // to the final member unwindHeader, on x86-64 effectively adding a hole of
94  // size 8 in front of that member (changing its offset from 88 to 96,
95  // sizeof(__cxa_exception) from 120 to 128, and alignof(__cxa_exception)
96  // from 8 to 16); the "header1" hack below to dynamically determine whether we run against a
97  // LLVM 5 libcxxabi is to look at the exceptionDestructor member, which must
98  // point to this function (the use of __cxa_exception in fillUnoException is
99  // unaffected, as it only accesses members towards the start of the struct,
100  // through a pointer known to actually point at the start). The libcxxabi commit
101  // <https://github.com/llvm/llvm-project/commit/9ef1daa46edb80c47d0486148c0afc4e0d83ddcf>
102  // "Insert padding before the __cxa_exception header to ensure the thrown" in LLVM 6
103  // removes the need for this hack, so the "header1" hack can be removed again once we can be
104  // sure that we only run against libcxxabi from LLVM >= 6.
105  //
106  // Second, the libcxxabi commit
107  // <https://github.com/llvm/llvm-project/commit/674ec1eb16678b8addc02a4b0534ab383d22fa77>
108  // "[libcxxabi] Insert padding in __cxa_exception struct for compatibility" in LLVM 10 changed
109  // the layout of the start of __cxa_exception to
110  //
111  // [8 byte void *reserve]
112  // 8 byte size_t referenceCount
113  //
114  // so the "header2" hack below to dynamically determine whether we run against a LLVM >= 10
115  // libcxxabi is to look whether the exceptionDestructor (with its known value) has increased its
116  // offset by 8. As described in the definition of __cxa_exception
117  // (bridges/source/cpp_uno/gcc3_linux_x86-64/share.hxx), the "header2" hack (together with the
118  // "#if 0" in the definition of __cxa_exception and the corresponding hack in fillUnoException)
119  // can be dropped once we can be sure that we only run against new libcxxabi that has the
120  // reserve member.
121  if (header->exceptionDestructor != &deleteException) {
122  auto const header1 = reinterpret_cast<__cxa_exception const *>(
123  reinterpret_cast<char const *>(header) - 8);
124  if (header1->exceptionDestructor == &deleteException) {
125  header = header1;
126  } else {
127  auto const header2 = reinterpret_cast<__cxa_exception const *>(
128  reinterpret_cast<char const *>(header) + 8);
129  if (header2->exceptionDestructor == &deleteException) {
130  header = header2;
131  } else {
132  assert(false);
133  }
134  }
135  }
136 #endif
137  assert(header->exceptionDestructor == &deleteException);
138  typelib_TypeDescription * pTD = nullptr;
139  OUString unoName( toUNOname( header->exceptionType->name() ) );
140  ::typelib_typedescription_getByName( &pTD, unoName.pData );
141  assert(pTD && "### unknown exception type! leaving out destruction => leaking!!!");
142  if (pTD)
143  {
144  ::uno_destructData( pExc, pTD, cpp_release );
145  ::typelib_typedescription_release( pTD );
146  }
147 }
148 }
149 
150 void raiseException( uno_Any * pUnoExc, uno_Mapping * pUno2Cpp )
151 {
152 #if OSL_DEBUG_LEVEL > 1
153  OString cstr(
155  OUString::unacquired( &pUnoExc->pType->pTypeName ),
156  RTL_TEXTENCODING_ASCII_US ) );
157  fprintf( stderr, "> uno exception occurred: %s\n", cstr.getStr() );
158 #endif
159  void * pCppExc;
160  type_info * rtti;
161 
162  {
163  // construct cpp exception object
164  typelib_TypeDescription * pTypeDescr = nullptr;
165  TYPELIB_DANGER_GET( &pTypeDescr, pUnoExc->pType );
166  assert(pTypeDescr);
167  if (! pTypeDescr)
168  {
169  throw RuntimeException(
170  "cannot get typedescription for type " +
171  OUString::unacquired( &pUnoExc->pType->pTypeName ) );
172  }
173 
174  pCppExc = __cxxabiv1::__cxa_allocate_exception( pTypeDescr->nSize );
175  ::uno_copyAndConvertData( pCppExc, pUnoExc->pData, pTypeDescr, pUno2Cpp );
176 
177  // destruct uno exception
178  ::uno_any_destruct( pUnoExc, nullptr );
179  // avoiding locked counts
180  rtti = x86_64::getRtti(*pTypeDescr);
181  TYPELIB_DANGER_RELEASE( pTypeDescr );
182  assert(rtti && "### no rtti for throwing exception!");
183  if (! rtti)
184  {
185  throw RuntimeException(
186  "no rtti for type " +
187  OUString::unacquired( &pUnoExc->pType->pTypeName ) );
188  }
189  }
190 
191  __cxxabiv1::__cxa_throw( pCppExc, rtti, deleteException );
192 }
193 
194 void fillUnoException(uno_Any * pUnoExc, uno_Mapping * pCpp2Uno)
195 {
197  if (! header)
198  {
199  RuntimeException aRE( "no exception header!" );
200  Type const & rType = cppu::UnoType<decltype(aRE)>::get();
201  uno_type_any_constructAndConvert( pUnoExc, &aRE, rType.getTypeLibType(), pCpp2Uno );
202  SAL_WARN("bridges", aRE.Message);
203  return;
204  }
205 
206 #if defined _LIBCPPABI_VERSION // detect libc++abi
207  // Very bad HACK to find out whether we run against a libcxxabi that has a new
208  // __cxa_exception::reserved member at the start, introduced with LLVM 10
209  // <https://github.com/llvm/llvm-project/commit/674ec1eb16678b8addc02a4b0534ab383d22fa77>
210  // "[libcxxabi] Insert padding in __cxa_exception struct for compatibility". The layout of the
211  // start of __cxa_exception is
212  //
213  // [8 byte void *reserve]
214  // 8 byte size_t referenceCount
215  //
216  // where the (bad, hacky) assumption is that reserve (if present) is null
217  // (__cxa_allocate_exception in at least LLVM 11 zero-fills the object, and nothing actively
218  // sets reserve) while referenceCount is non-null (__cxa_throw sets it to 1, and
219  // __cxa_decrement_exception_refcount destroys the exception as soon as it drops to 0; for a
220  // __cxa_dependent_exception, the referenceCount member is rather
221  //
222  // 8 byte void* primaryException
223  //
224  // but which also will always be set to a non-null value in __cxa_rethrow_primary_exception).
225  // As described in the definition of __cxa_exception
226  // (bridges/source/cpp_uno/gcc3_linux_x86-64/share.hxx), this hack (together with the "#if 0"
227  // there) can be dropped once we can be sure that we only run against new libcxxabi that has the
228  // reserve member:
229  if (*reinterpret_cast<void **>(header) == nullptr) {
230  header = reinterpret_cast<__cxxabiv1::__cxa_exception*>(reinterpret_cast<void **>(header) + 1);
231  }
232 #endif
233 
234  std::type_info *exceptionType = __cxxabiv1::__cxa_current_exception_type();
235 
236  typelib_TypeDescription * pExcTypeDescr = nullptr;
237  OUString unoName( toUNOname( exceptionType->name() ) );
238 #if OSL_DEBUG_LEVEL > 1
239  OString cstr_unoName( OUStringToOString( unoName, RTL_TEXTENCODING_ASCII_US ) );
240  fprintf( stderr, "> c++ exception occurred: %s\n", cstr_unoName.getStr() );
241 #endif
242  typelib_typedescription_getByName( &pExcTypeDescr, unoName.pData );
243  if (pExcTypeDescr == nullptr)
244  {
245  RuntimeException aRE( "exception type not found: " + unoName );
246  Type const & rType = cppu::UnoType<decltype(aRE)>::get();
247  uno_type_any_constructAndConvert( pUnoExc, &aRE, rType.getTypeLibType(), pCpp2Uno );
248  SAL_WARN("bridges", aRE.Message);
249  }
250  else
251  {
252  // construct uno exception any
253  uno_any_constructAndConvert( pUnoExc, header->adjustedPtr, pExcTypeDescr, pCpp2Uno );
254  typelib_typedescription_release( pExcTypeDescr );
255  }
256 }
257 
258 }
259 
260 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Type
static void deleteException(void *pExc)
static OUString toUNOname(char const *p)
std::type_info * getRtti(typelib_TypeDescription const &type)
Definition: rtti.cxx:281
struct _uno_Mapping uno_Mapping
Definition: msvc/except.hxx:32
void fillUnoException(uno_Any *pExc, uno_Mapping *pCpp2Uno)
sal_Int64 n
std::type_info * __cxa_current_exception_type()
void SAL_CALL uno_type_any_constructAndConvert(uno_Any *pDest, void *pSource, typelib_TypeDescriptionReference *pType, uno_Mapping *mapping) SAL_THROW_EXTERN_C()
void SAL_CALL uno_any_constructAndConvert(uno_Any *pDest, void *pSource, typelib_TypeDescription *pTypeDescr, uno_Mapping *mapping) SAL_THROW_EXTERN_C()
const sal_Int8 header[]
OString OUStringToOString(std::u16string_view str, ConnectionSettings const *settings)
struct _uno_Any uno_Any
Definition: msvc/except.hxx:31
#define _GLIBCXX_CDTOR_CALLABI
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_allocate_exception(size_t thrown_size)
void raiseException(uno_Any *pUnoExc, uno_Mapping *pUno2Cpp)
__cxa_eh_globals * __cxa_get_globals()
void(* exceptionDestructor)(void *)
Definition: unwind-cxx.h:87
std::type_info * exceptionType
Definition: unwind-cxx.h:86
void * p
#define SAL_WARN(area, stream)
void __cxa_throw(void *thrown_exception, std::type_info *tinfo, void(*dest)(void *)) __attribute__((noreturn))