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