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/strbuf.hxx>
33 #include <rtl/ustring.hxx>
34 #include <sal/log.hxx>
35 #include <typelib/typedescription.h>
36 
37 #include "rtti.hxx"
38 #include "share.hxx"
39 
40 namespace {
41 
42 class Generated {
43 public:
44  virtual ~Generated() {};
45 
46  virtual std::type_info * get() const = 0;
47 };
48 
49 class GeneratedPlain: public Generated {
50 public:
51  GeneratedPlain(std::unique_ptr<std::type_info> && info): info_(std::move(info)) {};
52 
53  std::type_info * get() const override { return info_.get(); }
54 
55 private:
56  std::unique_ptr<std::type_info> info_;
57 };
58 
59 class GeneratedPad: public Generated {
60 public:
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  osl::Mutex m_mutex;
78  t_rtti_map m_rttis;
79  std::vector<OString> m_rttiNames;
80  std::unordered_map<OUString, std::unique_ptr<Generated>> m_generatedRttis;
81 
82 #if !defined ANDROID
83  void * m_hApp;
84 #endif
85 
86 public:
87  RTTI();
88  ~RTTI();
89 
90  std::type_info * getRTTI(typelib_TypeDescription const &);
91 };
92 
93 RTTI::RTTI()
94 #if !defined ANDROID
95  : m_hApp( dlopen( nullptr, RTLD_LAZY ) )
96 #endif
97 {
98 }
99 
100 RTTI::~RTTI()
101 {
102 #if !defined ANDROID
103  dlclose( m_hApp );
104 #endif
105 }
106 
107 std::type_info * RTTI::getRTTI(typelib_TypeDescription const & pTypeDescr)
108 {
109  OUString const & unoName = OUString::unacquired(&pTypeDescr.pTypeName);
110 
111  osl::MutexGuard guard( m_mutex );
112  t_rtti_map::const_iterator iFind( m_rttis.find( unoName ) );
113  if (iFind != m_rttis.end())
114  return iFind->second;
115 
116  std::type_info * rtti;
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  return rtti;
145  }
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  // taking already generated rtti
152  rtti = iFind2->second->get();
153  return rtti;
154  }
155 
156  // we must generate it !
157  // symbol and rtti-name is nearly identical,
158  // the symbol is prefixed with _ZTI
159  char const * rttiName = symName.getStr() +4;
160 
161  SAL_INFO("bridges", "Generated rtti for" << rttiName);
162 
163  std::unique_ptr<Generated> newRtti;
164  switch (pTypeDescr.eTypeClass) {
165  case typelib_TypeClass_EXCEPTION:
166  {
167  typelib_CompoundTypeDescription const & ctd
168  = reinterpret_cast<
169  typelib_CompoundTypeDescription const &>(
170  pTypeDescr);
171  if (ctd.pBaseTypeDescription)
172  {
173  // ensure availability of base
174  std::type_info * base_rtti = getRTTI(
175  ctd.pBaseTypeDescription->aBase);
176  m_rttiNames.emplace_back(OString(rttiName));
177  std::unique_ptr<std::type_info> info(
179  m_rttiNames.back().getStr(), static_cast<__cxxabiv1::__class_type_info *>(base_rtti) ));
180  newRtti.reset(new GeneratedPlain(std::move(info)));
181  }
182  else
183  {
184  // this class has no base class
185  m_rttiNames.emplace_back(OString(rttiName));
186  std::unique_ptr<std::type_info> info(
187  new __cxxabiv1::__class_type_info(m_rttiNames.back().getStr()));
188  newRtti.reset(new GeneratedPlain(std::move(info)));
189  }
190  break;
191  }
192  case typelib_TypeClass_INTERFACE:
193  {
194  typelib_InterfaceTypeDescription const & itd
195  = reinterpret_cast<
196  typelib_InterfaceTypeDescription const &>(
197  pTypeDescr);
198  std::vector<std::type_info *> bases;
199  for (sal_Int32 i = 0; i != itd.nBaseTypes; ++i) {
200  bases.push_back(getRTTI(itd.ppBaseTypes[i]->aBase));
201  }
202  switch (itd.nBaseTypes) {
203  case 0:
204  {
205  m_rttiNames.emplace_back(OString(rttiName));
206  std::unique_ptr<std::type_info> info(
208  m_rttiNames.back().getStr()));
209  newRtti.reset(new GeneratedPlain(std::move(info)));
210  break;
211  }
212  case 1:
213  {
214  m_rttiNames.emplace_back(OString(rttiName));
215  std::unique_ptr<std::type_info> info(
217  m_rttiNames.back().getStr(),
218  static_cast<
220  bases[0])));
221  newRtti.reset(new GeneratedPlain(std::move(info)));
222  break;
223  }
224  default:
225  {
226  m_rttiNames.emplace_back(OString(rttiName));
227  auto pad = std::make_unique<char[]>(
229  + ((itd.nBaseTypes - 1)
230  * sizeof (
233  = new(pad.get())
235  m_rttiNames.back().getStr(),
237  info->__base_count = itd.nBaseTypes;
238  for (sal_Int32 i = 0; i != itd.nBaseTypes; ++i)
239  {
240  info->__base_info[i].__base_type
241  = static_cast<
243  bases[i]);
244  info->__base_info[i].__offset_flags
247  }
248  newRtti.reset(new GeneratedPad(std::move(pad)));
249  break;
250  }
251  }
252  break;
253  }
254  default:
255  assert(false); // cannot happen
256  }
257  rtti = newRtti->get();
258  if (newRtti) {
259  auto insertion (
260  m_generatedRttis.emplace(unoName, std::move(newRtti)));
261  SAL_WARN_IF( !insertion.second, "bridges", "key " << unoName << " already in generated rtti map" );
262  }
263 
264  return rtti;
265 }
266 
267 }
268 
269 std::type_info * x86_64::getRtti(typelib_TypeDescription const & type) {
270  static RTTI theRttiFactory;
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:269
t_rtti_map m_generatedRttis
void * m_hApp
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)
#define SAL_INFO(area, stream)