LibreOffice Module bridges (master) 1
gcc3_linux_loongarch64/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
41{
42void dummy_can_throw_anything(char const*) {}
43
44static OUString toUNOname(char const* p)
45{
46 // example: N3com3sun4star4lang24IllegalArgumentExceptionE
47
48 OUStringBuffer buf(64);
49 assert('N' == *p);
50 ++p; // skip N
51
52 while ('E' != *p)
53 {
54 // read chars count
55 long 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 ('E' != *p)
64 buf.append('.');
65 }
66
67 return buf.makeStringAndClear();
68}
69
70class RTTI
71{
72 typedef std::unordered_map<OUString, type_info*> t_rtti_map;
73
74 Mutex m_mutex;
77
78 void* m_hApp;
79
80public:
83
84 type_info* getRTTI(typelib_CompoundTypeDescription*);
85};
86
88 : m_hApp(dlopen(0, RTLD_LAZY))
89{
90}
91
92RTTI::~RTTI() { dlclose(m_hApp); }
93
94type_info* RTTI::getRTTI(typelib_CompoundTypeDescription* pTypeDescr)
95{
96 type_info* rtti;
97
98 OUString const& unoName = *(OUString const*)&pTypeDescr->aBase.pTypeName;
99
100 MutexGuard guard(m_mutex);
101 t_rtti_map::const_iterator iRttiFind(m_rttis.find(unoName));
102 if (iRttiFind == m_rttis.end())
103 {
104 // RTTI symbol
105 OStringBuffer buf(64);
106 buf.append("_ZTIN");
107 sal_Int32 index = 0;
108 do
109 {
110 OUString token(unoName.getToken(0, '.', index));
111 buf.append(token.getLength());
112 OString c_token(OUStringToOString(token, RTL_TEXTENCODING_ASCII_US));
113 buf.append(c_token);
114 } while (index >= 0);
115 buf.append('E');
116
117 OString symName(buf.makeStringAndClear());
118 rtti = (type_info*)dlsym(m_hApp, symName.getStr());
119
120 if (rtti)
121 {
122 pair<t_rtti_map::iterator, bool> insertion(
123 m_rttis.insert(t_rtti_map::value_type(unoName, rtti)));
124 assert(insertion.second && "### inserting new rtti failed?!");
125 }
126 else
127 {
128 // try to lookup the symbol in the generated rtti map
129 t_rtti_map::const_iterator iFind(m_generatedRttis.find(unoName));
130 if (iFind == m_generatedRttis.end())
131 {
132 // we must generate it !
133 // symbol and rtti-name is nearly identical,
134 // the symbol is prefixed with _ZTI
135 char const* rttiName = symName.getStr() + 4;
136 if (pTypeDescr->pBaseTypeDescription)
137 {
138 // ensure availability of base
139 type_info* base_rtti = getRTTI(
140 (typelib_CompoundTypeDescription*)pTypeDescr->pBaseTypeDescription);
141 rtti
142 = new __si_class_type_info(strdup(rttiName), (__class_type_info*)base_rtti);
143 }
144 else
145 {
146 // this class has no base class
147 rtti = new __class_type_info(strdup(rttiName));
148 }
149
150 pair<t_rtti_map::iterator, bool> insertion(
151 m_generatedRttis.insert(t_rtti_map::value_type(unoName, rtti)));
152 assert(insertion.second && "### inserting new generated rtti failed?!");
153 }
154 else // taking already generated rtti
155 {
156 rtti = iFind->second;
157 }
158 }
159 }
160 else
161 {
162 rtti = iRttiFind->second;
163 }
164
165 return rtti;
166}
167
168static void deleteException(void* pExc)
169{
170 __cxa_exception const* header = ((__cxa_exception const*)pExc - 1);
172 OUString unoName(toUNOname(header->exceptionType->name()));
173 ::typelib_typedescription_getByName(&pTD, unoName.pData);
174 assert(pTD && "### unknown exception type! leaving out destruction => leaking!!!");
175 if (pTD)
176 {
177 ::uno_destructData(pExc, pTD, cpp_release);
178 ::typelib_typedescription_release(pTD);
179 }
180}
181
182void raiseException(uno_Any* pUnoExc, uno_Mapping* pUno2Cpp)
183{
184 void* pCppExc;
185 type_info* rtti;
186
187 {
188 // construct cpp exception object
189 typelib_TypeDescription* pTypeDescr = 0;
190 TYPELIB_DANGER_GET(&pTypeDescr, pUnoExc->pType);
191 assert(pTypeDescr);
192 if (!pTypeDescr)
193 {
194 throw RuntimeException(OUString("cannot get typedescription for type ")
195 + OUString::unacquired(&pUnoExc->pType->pTypeName));
196 }
197
198 pCppExc = __cxa_allocate_exception(pTypeDescr->nSize);
199 ::uno_copyAndConvertData(pCppExc, pUnoExc->pData, pTypeDescr, pUno2Cpp);
200
201 // destruct uno exception
202 ::uno_any_destruct(pUnoExc, 0);
203 // avoiding locked counts
204 static RTTI rtti_data;
205 rtti = (type_info*)rtti_data.getRTTI((typelib_CompoundTypeDescription*)pTypeDescr);
206 TYPELIB_DANGER_RELEASE(pTypeDescr);
207 assert(rtti && "### no rtti for throwing exception!");
208 if (!rtti)
209 {
210 throw RuntimeException(OUString("no rtti for type ")
211 + OUString::unacquired(&pUnoExc->pType->pTypeName));
212 }
213 }
214
215 __cxa_throw(pCppExc, rtti, deleteException);
216}
217
218void fillUnoException(uno_Any* pUnoExc, uno_Mapping* pCpp2Uno)
219{
220 __cxa_exception* header = __cxa_get_globals()->caughtExceptions;
221 if (!header)
222 {
223 RuntimeException aRE("no exception header!");
224 Type const& rType = cppu::UnoType<decltype(aRE)>::get();
225 uno_type_any_constructAndConvert(pUnoExc, &aRE, rType.getTypeLibType(), pCpp2Uno);
226 SAL_WARN("bridges", aRE.Message);
227 return;
228 }
229
230 std::type_info* exceptionType = __cxa_current_exception_type();
231
232 typelib_TypeDescription* pExcTypeDescr = 0;
233 OUString unoName(toUNOname(exceptionType->name()));
234 typelib_typedescription_getByName(&pExcTypeDescr, unoName.pData);
235 if (0 == pExcTypeDescr)
236 {
237 RuntimeException aRE(OUString("exception type not found: ") + unoName);
238 Type const& rType = cppu::UnoType<decltype(aRE)>::get();
239 uno_type_any_constructAndConvert(pUnoExc, &aRE, rType.getTypeLibType(), pCpp2Uno);
240 SAL_WARN("bridges", aRE.Message);
241 }
242 else
243 {
244 // construct uno exception any
245 uno_any_constructAndConvert(pUnoExc, header->adjustedPtr, pExcTypeDescr, pCpp2Uno);
246 typelib_typedescription_release(pExcTypeDescr);
247 }
248}
249}
250
251/* 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()