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  OUString const & unoName = OUString::unacquired(&pTypeDescr.pTypeName);
111 
112  osl::MutexGuard guard( m_mutex );
113  t_rtti_map::const_iterator iFind( m_rttis.find( unoName ) );
114  if (iFind != m_rttis.end())
115  return iFind->second;
116 
117  std::type_info * rtti;
118 
119  // RTTI symbol
120  OStringBuffer buf( 64 );
121  buf.append( "_ZTIN" );
122  sal_Int32 index = 0;
123  do
124  {
125  OUString token( unoName.getToken( 0, '.', index ) );
126  buf.append( token.getLength() );
127  OString c_token( OUStringToOString( token, RTL_TEXTENCODING_ASCII_US ) );
128  buf.append( c_token );
129  }
130  while (index >= 0);
131  buf.append( 'E' );
132 
133  OString symName( buf.makeStringAndClear() );
134 #if !defined ANDROID
135  rtti = static_cast<std::type_info *>(dlsym( m_hApp, symName.getStr() ));
136 #else
137  rtti = static_cast<std::type_info *>(dlsym( RTLD_DEFAULT, symName.getStr() ));
138 #endif
139 
140  if (rtti)
141  {
142  std::pair< t_rtti_map::iterator, bool > insertion (
143  m_rttis.insert( t_rtti_map::value_type( unoName, rtti ) ) );
144  SAL_WARN_IF( !insertion.second, "bridges", "key " << unoName << " already in rtti map" );
145  return rtti;
146  }
147 
148  // try to lookup the symbol in the generated rtti map
149  auto iFind2( m_generatedRttis.find( unoName ) );
150  if (iFind2 != m_generatedRttis.end())
151  {
152  // taking already generated rtti
153  rtti = iFind2->second->get();
154  return rtti;
155  }
156 
157  // we must generate it !
158  // symbol and rtti-name is nearly identical,
159  // the symbol is prefixed with _ZTI
160  char const * rttiName = symName.getStr() +4;
161 #if OSL_DEBUG_LEVEL > 1
162  fprintf( stderr,"generated rtti for %s\n", rttiName );
163 #endif
164  std::unique_ptr<Generated> newRtti;
165  switch (pTypeDescr.eTypeClass) {
166  case typelib_TypeClass_EXCEPTION:
167  {
168  typelib_CompoundTypeDescription const & ctd
169  = reinterpret_cast<
170  typelib_CompoundTypeDescription const &>(
171  pTypeDescr);
172  if (ctd.pBaseTypeDescription)
173  {
174  // ensure availability of base
175  std::type_info * base_rtti = getRTTI(
176  ctd.pBaseTypeDescription->aBase);
177  m_rttiNames.emplace_back(OString(rttiName));
178  std::unique_ptr<std::type_info> info(
180  m_rttiNames.back().getStr(), static_cast<__cxxabiv1::__class_type_info *>(base_rtti) ));
181  newRtti.reset(new GeneratedPlain(std::move(info)));
182  }
183  else
184  {
185  // this class has no base class
186  m_rttiNames.emplace_back(OString(rttiName));
187  std::unique_ptr<std::type_info> info(
188  new __cxxabiv1::__class_type_info(m_rttiNames.back().getStr()));
189  newRtti.reset(new GeneratedPlain(std::move(info)));
190  }
191  break;
192  }
193  case typelib_TypeClass_INTERFACE:
194  {
195  typelib_InterfaceTypeDescription const & itd
196  = reinterpret_cast<
197  typelib_InterfaceTypeDescription const &>(
198  pTypeDescr);
199  std::vector<std::type_info *> bases;
200  for (sal_Int32 i = 0; i != itd.nBaseTypes; ++i) {
201  bases.push_back(getRTTI(itd.ppBaseTypes[i]->aBase));
202  }
203  switch (itd.nBaseTypes) {
204  case 0:
205  {
206  m_rttiNames.emplace_back(OString(rttiName));
207  std::unique_ptr<std::type_info> info(
209  m_rttiNames.back().getStr()));
210  newRtti.reset(new GeneratedPlain(std::move(info)));
211  break;
212  }
213  case 1:
214  {
215  m_rttiNames.emplace_back(OString(rttiName));
216  std::unique_ptr<std::type_info> info(
218  m_rttiNames.back().getStr(),
219  static_cast<
221  bases[0])));
222  newRtti.reset(new GeneratedPlain(std::move(info)));
223  break;
224  }
225  default:
226  {
227  m_rttiNames.emplace_back(OString(rttiName));
228  auto pad = std::make_unique<char[]>(
230  + ((itd.nBaseTypes - 1)
231  * sizeof (
234  = new(pad.get())
236  m_rttiNames.back().getStr(),
238  info->__base_count = itd.nBaseTypes;
239  for (sal_Int32 i = 0; i != itd.nBaseTypes; ++i)
240  {
241  info->__base_info[i].__base_type
242  = static_cast<
244  bases[i]);
245  info->__base_info[i].__offset_flags
248  }
249  newRtti.reset(new GeneratedPad(std::move(pad)));
250  break;
251  }
252  }
253  break;
254  }
255  default:
256  assert(false); // cannot happen
257  }
258  rtti = newRtti->get();
259  if (newRtti) {
260  auto insertion (
261  m_generatedRttis.emplace(unoName, std::move(newRtti)));
262  SAL_WARN_IF( !insertion.second, "bridges", "key " << unoName << " already in generated rtti map" );
263  }
264 
265  return rtti;
266 }
267 
268 struct theRttiFactory: public rtl::Static<RTTI, theRttiFactory> {};
269 
270 }
271 
272 std::type_info * x86_64::getRtti(typelib_TypeDescription const & type) {
273  return theRttiFactory::get().getRTTI(type);
274 }
275 
276 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
t_rtti_map m_rttis
std::type_info * getRtti(typelib_TypeDescription const &type)
Definition: rtti.cxx:272
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)