LibreOffice Module bridges (master)  1
rtti.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 <sal/config.h>
21 
22 #include <cassert>
23 #include <memory>
24 #include <typeinfo>
25 #include <unordered_map>
26 #include <utility>
27 #include <vector>
28 
29 #include <dlfcn.h>
30 
31 #include <osl/mutex.hxx>
32 #include <rtl/instance.hxx>
33 #include <rtl/strbuf.hxx>
34 #include <rtl/ustring.hxx>
35 #include <sal/log.hxx>
36 #include <typelib/typedescription.h>
37 
38 #include "rtti.hxx"
39 #include "share.hxx"
40 
41 namespace {
42 
43 class Generated {
44 public:
45  virtual ~Generated() {};
46 
47  virtual std::type_info * get() const = 0;
48 };
49 
50 class GeneratedPlain: public Generated {
51 public:
52  GeneratedPlain(std::unique_ptr<std::type_info> && info): info_(std::move(info)) {};
53 
54  std::type_info * get() const override { return info_.get(); }
55 
56 private:
57  std::unique_ptr<std::type_info> info_;
58 };
59 
60 class GeneratedPad: public Generated {
61 public:
62 public:
63  GeneratedPad(std::unique_ptr<char[]> && pad): pad_(std::move(pad)) {};
64 
65  ~GeneratedPad() override { get()->~type_info(); }
66 
67  std::type_info * get() const override final
68  { return reinterpret_cast<std::type_info *>(pad_.get()); }
69 
70 private:
71  std::unique_ptr<char[]> pad_;
72 };
73 
74 class RTTI
75 {
76  typedef std::unordered_map< OUString, std::type_info * > t_rtti_map;
77 
78  osl::Mutex m_mutex;
79  t_rtti_map m_rttis;
80  std::vector<OString> m_rttiNames;
81  std::unordered_map<OUString, std::unique_ptr<Generated>> m_generatedRttis;
82 
83 #if !defined ANDROID
84  void * m_hApp;
85 #endif
86 
87 public:
88  RTTI();
89  ~RTTI();
90 
91  std::type_info * getRTTI(typelib_TypeDescription const &);
92 };
93 
94 RTTI::RTTI()
95 #if !defined ANDROID
96  : m_hApp( dlopen( nullptr, RTLD_LAZY ) )
97 #endif
98 {
99 }
100 
101 RTTI::~RTTI()
102 {
103 #if !defined ANDROID
104  dlclose( m_hApp );
105 #endif
106 }
107 
108 std::type_info * RTTI::getRTTI(typelib_TypeDescription const & pTypeDescr)
109 {
110  std::type_info * rtti;
111 
112  OUString const & unoName = OUString::unacquired(&pTypeDescr.pTypeName);
113 
114  osl::MutexGuard guard( m_mutex );
115  t_rtti_map::const_iterator iFind( m_rttis.find( unoName ) );
116  if (iFind == m_rttis.end())
117  {
118  // RTTI symbol
119  OStringBuffer buf( 64 );
120  buf.append( "_ZTIN" );
121  sal_Int32 index = 0;
122  do
123  {
124  OUString token( unoName.getToken( 0, '.', index ) );
125  buf.append( token.getLength() );
126  OString c_token( OUStringToOString( token, RTL_TEXTENCODING_ASCII_US ) );
127  buf.append( c_token );
128  }
129  while (index >= 0);
130  buf.append( 'E' );
131 
132  OString symName( buf.makeStringAndClear() );
133 #if !defined ANDROID
134  rtti = static_cast<std::type_info *>(dlsym( m_hApp, symName.getStr() ));
135 #else
136  rtti = static_cast<std::type_info *>(dlsym( RTLD_DEFAULT, symName.getStr() ));
137 #endif
138 
139  if (rtti)
140  {
141  std::pair< t_rtti_map::iterator, bool > insertion (
142  m_rttis.insert( t_rtti_map::value_type( unoName, rtti ) ) );
143  SAL_WARN_IF( !insertion.second, "bridges", "key " << unoName << " already in rtti map" );
144  }
145  else
146  {
147  // try to lookup the symbol in the generated rtti map
148  auto iFind2( m_generatedRttis.find( unoName ) );
149  if (iFind2 == m_generatedRttis.end())
150  {
151  // we must generate it !
152  // symbol and rtti-name is nearly identical,
153  // the symbol is prefixed with _ZTI
154  char const * rttiName = symName.getStr() +4;
155 #if OSL_DEBUG_LEVEL > 1
156  fprintf( stderr,"generated rtti for %s\n", rttiName );
157 #endif
158  std::unique_ptr<Generated> newRtti;
159  switch (pTypeDescr.eTypeClass) {
160  case typelib_TypeClass_EXCEPTION:
161  {
162  typelib_CompoundTypeDescription const & ctd
163  = reinterpret_cast<
164  typelib_CompoundTypeDescription const &>(
165  pTypeDescr);
166  if (ctd.pBaseTypeDescription)
167  {
168  // ensure availability of base
169  std::type_info * base_rtti = getRTTI(
170  ctd.pBaseTypeDescription->aBase);
171  m_rttiNames.emplace_back(OString(rttiName));
172  std::unique_ptr<std::type_info> info(
174  m_rttiNames.back().getStr(), static_cast<__cxxabiv1::__class_type_info *>(base_rtti) ));
175  newRtti.reset(new GeneratedPlain(std::move(info)));
176  }
177  else
178  {
179  // this class has no base class
180  m_rttiNames.emplace_back(OString(rttiName));
181  std::unique_ptr<std::type_info> info(
182  new __cxxabiv1::__class_type_info(m_rttiNames.back().getStr()));
183  newRtti.reset(new GeneratedPlain(std::move(info)));
184  }
185  break;
186  }
187  case typelib_TypeClass_INTERFACE:
188  {
189  typelib_InterfaceTypeDescription const & itd
190  = reinterpret_cast<
191  typelib_InterfaceTypeDescription const &>(
192  pTypeDescr);
193  std::vector<std::type_info *> bases;
194  for (sal_Int32 i = 0; i != itd.nBaseTypes; ++i) {
195  bases.push_back(getRTTI(itd.ppBaseTypes[i]->aBase));
196  }
197  switch (itd.nBaseTypes) {
198  case 0:
199  {
200  m_rttiNames.emplace_back(OString(rttiName));
201  std::unique_ptr<std::type_info> info(
203  m_rttiNames.back().getStr()));
204  newRtti.reset(new GeneratedPlain(std::move(info)));
205  break;
206  }
207  case 1:
208  {
209  m_rttiNames.emplace_back(OString(rttiName));
210  std::unique_ptr<std::type_info> info(
212  m_rttiNames.back().getStr(),
213  static_cast<
215  bases[0])));
216  newRtti.reset(new GeneratedPlain(std::move(info)));
217  break;
218  }
219  default:
220  {
221  m_rttiNames.emplace_back(OString(rttiName));
222  auto pad = std::make_unique<char[]>(
224  + ((itd.nBaseTypes - 1)
225  * sizeof (
228  = new(pad.get())
230  m_rttiNames.back().getStr(),
232  info->__base_count = itd.nBaseTypes;
233  for (sal_Int32 i = 0; i != itd.nBaseTypes; ++i)
234  {
235  info->__base_info[i].__base_type
236  = static_cast<
238  bases[i]);
239  info->__base_info[i].__offset_flags
242  }
243  newRtti.reset(new GeneratedPad(std::move(pad)));
244  break;
245  }
246  }
247  break;
248  }
249  default:
250  assert(false); // cannot happen
251  }
252  rtti = newRtti->get();
253  if (newRtti) {
254  auto insertion (
255  m_generatedRttis.emplace(unoName, std::move(newRtti)));
256  SAL_WARN_IF( !insertion.second, "bridges", "key " << unoName << " already in generated rtti map" );
257  }
258  }
259  else // taking already generated rtti
260  {
261  rtti = iFind2->second->get();
262  }
263  }
264  }
265  else
266  {
267  rtti = iFind->second;
268  }
269 
270  return rtti;
271 }
272 
273 struct theRttiFactory: public rtl::Static<RTTI, theRttiFactory> {};
274 
275 }
276 
277 std::type_info * x86_64::getRtti(typelib_TypeDescription const & type) {
278  return theRttiFactory::get().getRTTI(type);
279 }
280 
281 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
t_rtti_map m_rttis
std::type_info * getRtti(typelib_TypeDescription const &type)
Definition: rtti.cxx:277
t_rtti_map m_generatedRttis
void * m_hApp
const BorderLinePrimitive2D *pCandidateB assert(pCandidateA)
OString OUStringToOString(std::u16string_view str, ConnectionSettings const *settings)
int i
Mutex m_mutex
tuple index
struct _typelib_TypeDescription typelib_TypeDescription
Definition: msvc/except.hxx:52
#define SAL_WARN_IF(condition, area, stream)