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