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