LibreOffice Module bridges (master)  1
vtablefactory.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 
21 #include <vtablefactory.hxx>
22 
23 #include <vtables.hxx>
24 
25 #include <osl/thread.h>
26 #include <osl/security.hxx>
27 #include <osl/file.hxx>
28 #include <osl/mutex.hxx>
29 #include <rtl/alloc.h>
30 #include <rtl/ustring.hxx>
31 #include <sal/log.hxx>
32 #include <sal/types.h>
33 #include <typelib/typedescription.hxx>
34 
35 #include <memory>
36 #include <new>
37 #include <unordered_map>
38 #include <vector>
39 
40 #if defined SAL_UNX
41 #include <unistd.h>
42 #include <string.h>
43 #include <errno.h>
44 #include <sys/mman.h>
45 #elif defined _WIN32
46 #define WIN32_LEAN_AND_MEAN
47 #include <windows.h>
48 #else
49 #error Unsupported platform
50 #endif
51 
52 #if defined USE_DOUBLE_MMAP
53 #include <fcntl.h>
54 #endif
55 
56 #if defined MACOSX && defined __aarch64__
57 #include <pthread.h>
58 #endif
59 
61 
62 namespace {
63 
64 extern "C" void * allocExec(
65  SAL_UNUSED_PARAMETER rtl_arena_type *, sal_Size * size)
66 {
67  std::size_t pagesize;
68 #if defined SAL_UNX
69 #if defined FREEBSD || defined NETBSD || defined OPENBSD || defined DRAGONFLY || defined HAIKU
70  pagesize = getpagesize();
71 #else
72  pagesize = sysconf(_SC_PAGESIZE);
73 #endif
74 #elif defined _WIN32
75  SYSTEM_INFO info;
76  GetSystemInfo(&info);
77  pagesize = info.dwPageSize;
78 #else
79 #error Unsupported platform
80 #endif
81  std::size_t n = (*size + (pagesize - 1)) & ~(pagesize - 1);
82  void * p;
83 #if defined SAL_UNX
84 #if defined MACOSX
85  p = mmap(
86  nullptr, n, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE | MAP_ANON | MAP_JIT, -1,
87  0);
88  if (p != MAP_FAILED) {
89  goto done;
90  }
91  {
92  auto const e = errno;
93  SAL_INFO("bridges.osx", "mmap failed with " << e);
94  if (e != EINVAL) {
95  p = nullptr;
96  goto done;
97  }
98  }
99  // At least some macOS 10.13 machines are reported to fail the above mmap with EINVAL (see
100  // tdf#134754 "Crash on macOS 10.13 opening local HSQLDB-based odb file in Base on LibreOffice 7
101  // rc1", so in that case retry with the "traditional" approach:
102 #endif
103  p = mmap(
104  nullptr, n, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1,
105  0);
106  if (p == MAP_FAILED) {
107  p = nullptr;
108  }
109  else if (mprotect (p, n, PROT_READ | PROT_WRITE | PROT_EXEC) == -1)
110  {
111  munmap (p, n);
112  p = nullptr;
113  }
114 #if defined MACOSX
115 done:
116 #endif
117 #elif defined _WIN32
118  p = VirtualAlloc(nullptr, n, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
119 #endif
120  if (p != nullptr) {
121  *size = n;
122  }
123  return p;
124 }
125 
126 extern "C" void freeExec(
127  SAL_UNUSED_PARAMETER rtl_arena_type *, void * address, sal_Size size)
128 {
129 #if defined SAL_UNX
130  munmap(address, size);
131 #elif defined _WIN32
132  (void) size; // unused
133  VirtualFree(address, 0, MEM_RELEASE);
134 #endif
135 }
136 
137 #if defined MACOSX && defined __aarch64__
138 struct JitMemoryProtectionGuard {
139  JitMemoryProtectionGuard() { pthread_jit_write_protect_np(0); }
140  ~JitMemoryProtectionGuard() { pthread_jit_write_protect_np(1); }
141 };
142 #endif
143 
144 }
145 
147  public std::vector<Block>
148 {
149 public:
150  GuardedBlocks(const GuardedBlocks&) = delete;
151  const GuardedBlocks& operator=(const GuardedBlocks&) = delete;
152 
153  explicit GuardedBlocks(VtableFactory const & factory):
154  m_factory(factory), m_guarded(true) {}
155 
156  ~GuardedBlocks();
157 
158  void unguard() { m_guarded = false; }
159 
160 private:
162  bool m_guarded;
163 };
164 
166  if (m_guarded) {
167  for (iterator i(begin()); i != end(); ++i) {
169  }
170  }
171 }
172 
174 public:
175  explicit BaseOffset(typelib_InterfaceTypeDescription * type) { calculate(type, 0); }
176 
177  sal_Int32 getFunctionOffset(OUString const & name) const
178  { return m_map.find(name)->second; }
179 
180 private:
181  sal_Int32 calculate(
182  typelib_InterfaceTypeDescription * type, sal_Int32 offset);
183 
184  std::unordered_map< OUString, sal_Int32 > m_map;
185 };
186 
188  typelib_InterfaceTypeDescription * type, sal_Int32 offset)
189 {
190  OUString name(type->aBase.pTypeName);
191  auto it = m_map.find(name);
192  if (it == m_map.end()) {
193  for (sal_Int32 i = 0; i < type->nBaseTypes; ++i) {
194  offset = calculate(type->ppBaseTypes[i], offset);
195  }
196  m_map.insert(it, {name, offset});
198  reinterpret_cast< typelib_TypeDescription ** >(&type));
200  }
201  return offset;
202 }
203 
204 VtableFactory::VtableFactory(): m_arena(
205  rtl_arena_create(
206  "bridges::cpp_uno::shared::VtableFactory",
207  sizeof (void *), // to satisfy alignment requirements
208  0, nullptr, allocExec, freeExec, 0))
209 {
210  if (m_arena == nullptr) {
211  throw std::bad_alloc();
212  }
213 }
214 
216  {
217  osl::MutexGuard guard(m_mutex);
218  for (const auto& rEntry : m_map) {
219  for (sal_Int32 j = 0; j < rEntry.second.count; ++j) {
220  freeBlock(rEntry.second.blocks[j]);
221  }
222  }
223  }
224  rtl_arena_destroy(m_arena);
225 }
226 
228  typelib_InterfaceTypeDescription * type)
229 {
230  OUString name(type->aBase.pTypeName);
231  osl::MutexGuard guard(m_mutex);
232  Map::iterator i(m_map.find(name));
233  if (i == m_map.end()) {
234  GuardedBlocks blocks(*this);
235  createVtables(blocks, BaseOffset(type), type, 0, type, true);
236  Vtables vtables;
237  assert(blocks.size() <= SAL_MAX_INT32);
238  vtables.count = static_cast< sal_Int32 >(blocks.size());
239  vtables.blocks.reset(new Block[vtables.count]);
240  for (sal_Int32 j = 0; j < vtables.count; ++j) {
241  vtables.blocks[j] = blocks[j];
242  }
243  i = m_map.emplace(name, std::move(vtables)).first;
244  blocks.unguard();
245  }
246  return i->second;
247 }
248 
249 #ifdef USE_DOUBLE_MMAP
250 bool VtableFactory::createBlock(Block &block, sal_Int32 slotCount) const
251 {
252  std::size_t size = getBlockSize(slotCount);
253  std::size_t pagesize = sysconf(_SC_PAGESIZE);
254  block.size = (size + (pagesize - 1)) & ~(pagesize - 1);
255  block.fd = -1;
256 
257  // Try non-doublemmaped allocation first:
258  block.start = block.exec = rtl_arena_alloc(m_arena, &block.size);
259  if (block.start != nullptr) {
260  return true;
261  }
262 
263  osl::Security aSecurity;
264  OUString strDirectory;
265  OUString strURLDirectory;
266  if (aSecurity.getHomeDir(strURLDirectory))
267  osl::File::getSystemPathFromFileURL(strURLDirectory, strDirectory);
268 
269  for (int i = strDirectory.isEmpty() ? 1 : 0; i < 2; ++i)
270  {
271  if (strDirectory.isEmpty())
272  strDirectory = "/tmp";
273 
274  strDirectory += "/.execoooXXXXXX";
275  OString aTmpName = OUStringToOString(strDirectory, osl_getThreadTextEncoding());
276  std::unique_ptr<char[]> tmpfname(new char[aTmpName.getLength()+1]);
277  strncpy(tmpfname.get(), aTmpName.getStr(), aTmpName.getLength()+1);
278  // coverity[secure_temp] - https://communities.coverity.com/thread/3179
279  if ((block.fd = mkstemp(tmpfname.get())) == -1)
280  fprintf(stderr, "mkstemp(\"%s\") failed: %s\n", tmpfname.get(), strerror(errno));
281  if (block.fd == -1)
282  {
283  break;
284  }
285  unlink(tmpfname.get());
286  tmpfname.reset();
287 #if defined(HAVE_POSIX_FALLOCATE)
288  int err = posix_fallocate(block.fd, 0, block.size);
289 #else
290  int err = ftruncate(block.fd, block.size);
291 #endif
292  if (err != 0)
293  {
294 #if defined(HAVE_POSIX_FALLOCATE)
295  SAL_WARN("bridges", "posix_fallocate failed with code " << err);
296 #else
297  SAL_WARN("bridges", "truncation of executable memory area failed with code " << err);
298 #endif
299  close(block.fd);
300  block.fd = -1;
301  break;
302  }
303  block.start = mmap(nullptr, block.size, PROT_READ | PROT_WRITE, MAP_SHARED, block.fd, 0);
304  if (block.start== MAP_FAILED) {
305  block.start = nullptr;
306  }
307  block.exec = mmap(nullptr, block.size, PROT_READ | PROT_EXEC, MAP_SHARED, block.fd, 0);
308  if (block.exec == MAP_FAILED) {
309  block.exec = nullptr;
310  }
311 
312  //All good
313  if (block.start && block.exec && block.fd != -1)
314  break;
315 
316  freeBlock(block);
317 
318  strDirectory.clear();
319  }
320  return (block.start != nullptr && block.exec != nullptr);
321 }
322 
323 void VtableFactory::freeBlock(Block const & block) const {
324  //if the double-map failed we were allocated on the arena
325  if (block.fd == -1 && block.start == block.exec && block.start != nullptr)
326  rtl_arena_free(m_arena, block.start, block.size);
327  else
328  {
329  if (block.start) munmap(block.start, block.size);
330  if (block.exec) munmap(block.exec, block.size);
331  if (block.fd != -1) close(block.fd);
332  }
333 }
334 #else
335 bool VtableFactory::createBlock(Block &block, sal_Int32 slotCount) const
336 {
337  block.size = getBlockSize(slotCount);
338  block.start = rtl_arena_alloc(m_arena, &block.size);
339  return block.start != nullptr;
340 }
341 
342 void VtableFactory::freeBlock(Block const & block) const {
343  rtl_arena_free(m_arena, block.start, block.size);
344 }
345 #endif
346 
348  GuardedBlocks & blocks, BaseOffset const & baseOffset,
349  typelib_InterfaceTypeDescription * type, sal_Int32 vtableNumber,
350  typelib_InterfaceTypeDescription * mostDerived, bool includePrimary) const
351 {
352  {
353 #if defined MACOSX && defined __aarch64__
354  JitMemoryProtectionGuard guard;
355 #endif
356  if (includePrimary) {
357  sal_Int32 slotCount
359  Block block;
360  if (!createBlock(block, slotCount)) {
361  throw std::bad_alloc();
362  }
363  try {
364  Slot * slots = initializeBlock(
365  block.start, slotCount, vtableNumber, mostDerived);
366  unsigned char * codeBegin =
367  reinterpret_cast< unsigned char * >(slots);
368  unsigned char * code = codeBegin;
369  sal_Int32 vtableOffset = blocks.size() * sizeof (Slot *);
370  for (typelib_InterfaceTypeDescription const * type2 = type;
371  type2 != nullptr; type2 = type2->pBaseTypeDescription)
372  {
373  code = addLocalFunctions(
374  &slots, code,
375 #ifdef USE_DOUBLE_MMAP
376  reinterpret_cast<sal_uIntPtr>(block.exec) - reinterpret_cast<sal_uIntPtr>(block.start),
377 #endif
378  type2,
379  baseOffset.getFunctionOffset(type2->aBase.pTypeName),
381  vtableOffset);
382  }
383  flushCode(codeBegin, code);
384 #ifdef USE_DOUBLE_MMAP
385  //Finished generating block, swap writable pointer with executable
386  //pointer
387  std::swap(block.start, block.exec);
388 #endif
389  blocks.push_back(block);
390  } catch (...) {
391  freeBlock(block);
392  throw;
393  }
394  }
395  }
396  for (sal_Int32 i = 0; i < type->nBaseTypes; ++i) {
397  vtableNumber = createVtables(
398  blocks, baseOffset, type->ppBaseTypes[i],
399  vtableNumber + (i == 0 ? 0 : 1), mostDerived, i != 0);
400  }
401  return vtableNumber;
402 }
403 
404 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
sal_Int32 createVtables(GuardedBlocks &blocks, BaseOffset const &baseOffset, typelib_InterfaceTypeDescription *type, sal_Int32 vtableNumber, typelib_InterfaceTypeDescription *mostDerived, bool includePrimary) const
sal_Int32 getPrimaryFunctions(typelib_InterfaceTypeDescription *type)
Calculate the number of primary functions of an interface type.
Definition: vtables.cxx:122
std::unique_ptr< Block[]> blocks
An array of blocks, representing the multiple vtables of a (multiple-inheritance) type...
std::unordered_map< OUString, sal_Int32 > m_map
sal_Int64 n
sal_Int32 calculate(typelib_InterfaceTypeDescription *type, sal_Int32 offset)
Hand out vtable structures for interface type descriptions.
static std::size_t getBlockSize(sal_Int32 slotCount)
Calculate the size of a raw vtable block.
#define USE_DOUBLE_MMAP
sal_Unicode code
t_map m_map
enumrange< T >::Iterator begin(enumrange< T >)
const BorderLinePrimitive2D *pCandidateB assert(pCandidateA)
sal_Bool SAL_CALL typelib_typedescription_complete(typelib_TypeDescription **ppTypeDescr) SAL_THROW_EXTERN_C()
static Slot * initializeBlock(void *block, sal_Int32 slotCount, sal_Int32 vtableNumber, typelib_InterfaceTypeDescription *type)
Initialize a raw vtable block.
OString OUStringToOString(std::u16string_view str, ConnectionSettings const *settings)
err
static unsigned char * addLocalFunctions(Slot **slots, unsigned char *code, sal_PtrDiff writetoexecdiff, typelib_InterfaceTypeDescription const *type, sal_Int32 functionOffset, sal_Int32 functionCount, sal_Int32 vtableOffset)
Fill the vtable slots corresponding to all local (i.e., not inherited) functions of a given interface...
sal_Int32 getFunctionOffset(OUString const &name) const
#define SAL_MAX_INT32
The vtable structure corresponding to an interface type.
int i
void * start
The start of the raw vtable block.
def pagesize
sal_Int32 getLocalFunctions(typelib_InterfaceTypeDescription const *type)
Calculate the number of local functions of an interface type.
Definition: vtables.cxx:114
bool createBlock(Block &block, sal_Int32 slotCount) const
enumrange< T >::Iterator end(enumrange< T >)
sal_Size size
The size of the raw vtable block, in bytes.
bool close
void * exec
When separately mmapping the block for writing and executing exec points to the same memory as start...
const GuardedBlocks & operator=(const GuardedBlocks &)=delete
#define SAL_INFO(area, stream)
static void flushCode(unsigned char const *begin, unsigned char const *end)
Flush all the generated code snippets of a vtable, on platforms that require it.
void * p
sal_Int32 count
The number of blocks/vtables.
#define SAL_WARN(area, stream)
void freeBlock(Block const &block) const
BaseOffset(typelib_InterfaceTypeDescription *type)
const Vtables & getVtables(typelib_InterfaceTypeDescription *type)
Given an interface type description, return its corresponding vtable structure.
char const * name
typedef void(CALLTYPE *GetFuncDataPtr)(sal_uInt16 &nNo