LibreOffice Module bridges (master)  1
gcc3_linux_aarch64/cpp2uno.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 <cstdarg>
24 #include <cstddef>
25 #include <cstdlib>
26 #include <cstring>
27 
28 #include <dlfcn.h>
29 
30 #include <com/sun/star/uno/XInterface.hpp>
31 #include <com/sun/star/uno/genfunc.hxx>
32 #include <sal/alloca.h>
33 #include <sal/types.h>
34 #include <typelib/typeclass.h>
35 #include <typelib/typedescription.h>
36 #include <typelib/typedescription.hxx>
37 
38 #include <bridge.hxx>
39 #include <cppinterfaceproxy.hxx>
40 #include <types.hxx>
41 #include <vtablefactory.hxx>
42 
43 #include "abi.hxx"
44 #include "vtablecall.hxx"
45 
46 namespace {
47 
48 void call(
50  css::uno::TypeDescription const & description,
51  typelib_TypeDescriptionReference * returnType, sal_Int32 count,
52  typelib_MethodParameter * parameters, unsigned long * gpr,
53  unsigned long * fpr, unsigned long * stack, void * indirectRet)
54 {
55  typelib_TypeDescription * rtd = nullptr;
56  if (returnType != nullptr) {
57  TYPELIB_DANGER_GET(&rtd, returnType);
58  }
59  abi_aarch64::ReturnKind retKind = rtd == nullptr
61  bool retConv = rtd != nullptr
63  void * retin = retKind == abi_aarch64::RETURN_KIND_INDIRECT && !retConv
64  ? indirectRet : rtd == nullptr ? nullptr : alloca(rtd->nSize);
65  void ** args = static_cast< void ** >(alloca(count * sizeof (void *)));
66  void ** cppArgs = static_cast< void ** >(alloca(count * sizeof (void *)));
67  typelib_TypeDescription ** argtds = static_cast<typelib_TypeDescription **>(
68  alloca(count * sizeof (typelib_TypeDescription *)));
69  sal_Int32 ngpr = 1;
70  sal_Int32 nfpr = 0;
71  sal_Int32 sp = 0;
72 #ifdef MACOSX
73  sal_Int32 subsp = 0;
74 #endif
75  for (sal_Int32 i = 0; i != count; ++i) {
76  if (!parameters[i].bOut
77  && bridges::cpp_uno::shared::isSimpleType(parameters[i].pTypeRef))
78  {
79  switch (parameters[i].pTypeRef->eTypeClass) {
80 #ifdef MACOSX
81  case typelib_TypeClass_BOOLEAN:
82  case typelib_TypeClass_BYTE:
83  if (ngpr < 8)
84  {
85  args[i] = gpr + ngpr;
86  ngpr++;
87  }
88  else
89  {
90  args[i] = reinterpret_cast<void *>(reinterpret_cast<uintptr_t>(stack + sp) + subsp);
91  subsp += 1;
92  if (subsp == 8)
93  {
94  sp++;
95  subsp = 0;
96  }
97  }
98  break;
99  case typelib_TypeClass_SHORT:
100  case typelib_TypeClass_UNSIGNED_SHORT:
101  case typelib_TypeClass_CHAR:
102  if (ngpr < 8)
103  {
104  args[i] = gpr + ngpr;
105  ngpr++;
106  }
107  else
108  {
109  subsp = (subsp + 1) & ~0x1;
110  if (subsp == 8)
111  {
112  sp++;
113  subsp = 0;
114  }
115  args[i] = reinterpret_cast<void *>(reinterpret_cast<uintptr_t>(stack + sp) + subsp);
116  subsp += 2;
117  if (subsp == 8)
118  {
119  sp++;
120  subsp = 0;
121  }
122  }
123  break;
124  case typelib_TypeClass_LONG:
125  case typelib_TypeClass_UNSIGNED_LONG:
126  case typelib_TypeClass_ENUM:
127  if (ngpr < 8)
128  {
129  args[i] = gpr + ngpr;
130  ngpr++;
131  }
132  else
133  {
134  subsp = (subsp + 3) & ~0x3;
135  if (subsp == 8)
136  {
137  sp++;
138  subsp = 0;
139  }
140  args[i] = reinterpret_cast<void *>(reinterpret_cast<uintptr_t>(stack + sp) + subsp);
141  subsp += 4;
142  if (subsp == 8)
143  {
144  sp++;
145  subsp = 0;
146  }
147  }
148  break;
149  case typelib_TypeClass_HYPER:
150  case typelib_TypeClass_UNSIGNED_HYPER:
151  if (ngpr < 8)
152  {
153  args[i] = gpr + ngpr;
154  ngpr++;
155  }
156  else
157  {
158  if (subsp > 0)
159  {
160  sp++;
161  subsp = 0;
162  }
163  args[i] = stack + sp;
164  sp++;
165  }
166  break;
167  case typelib_TypeClass_FLOAT:
168  if (nfpr < 8)
169  {
170  args[i] = fpr + nfpr;
171  nfpr++;
172  }
173  else
174  {
175  subsp = (subsp + 3) & ~0x3;
176  if (subsp == 8)
177  {
178  sp++;
179  subsp = 0;
180  }
181  args[i] = reinterpret_cast<void *>(reinterpret_cast<uintptr_t>(stack + sp) + subsp);
182  subsp += 4;
183  if (subsp == 8)
184  {
185  sp++;
186  subsp = 0;
187  }
188  }
189  break;
190  case typelib_TypeClass_DOUBLE:
191  if (nfpr < 8)
192  {
193  args[i] = fpr + nfpr;
194  nfpr++;
195  }
196  else
197  {
198  if (subsp > 0)
199  {
200  sp++;
201  subsp = 0;
202  }
203  args[i] = stack + sp;
204  sp++;
205  }
206  break;
207 #else
208  case typelib_TypeClass_BOOLEAN:
209  case typelib_TypeClass_BYTE:
210  case typelib_TypeClass_SHORT:
211  case typelib_TypeClass_UNSIGNED_SHORT:
212  case typelib_TypeClass_LONG:
213  case typelib_TypeClass_UNSIGNED_LONG:
214  case typelib_TypeClass_HYPER:
215  case typelib_TypeClass_UNSIGNED_HYPER:
216  case typelib_TypeClass_CHAR:
217  case typelib_TypeClass_ENUM:
218  args[i] = ngpr == 8 ? stack + sp++ : gpr + ngpr++;
219  break;
220  case typelib_TypeClass_FLOAT:
221  case typelib_TypeClass_DOUBLE:
222  args[i] = nfpr == 8 ? stack + sp++ : fpr + nfpr++;
223  break;
224 #endif
225  default:
226  assert(false);
227  }
228  argtds[i] = nullptr;
229  } else {
230 #ifdef MACOSX
231  if (subsp > 0)
232  {
233  sp++;
234  subsp = 0;
235  }
236 #endif
237  cppArgs[i] = reinterpret_cast<void *>(
238  ngpr == 8 ? stack[sp++] : gpr[ngpr++]);
239  typelib_TypeDescription * ptd = nullptr;
240  TYPELIB_DANGER_GET(&ptd, parameters[i].pTypeRef);
241  if (!parameters[i].bIn) {
242  args[i] = alloca(ptd->nSize);
243  argtds[i] = ptd;
245  args[i] = alloca(ptd->nSize);
247  args[i], cppArgs[i], ptd, proxy->getBridge()->getCpp2Uno());
248  argtds[i] = ptd;
249  } else {
250  args[i] = cppArgs[i];
251  argtds[i] = nullptr;
252  TYPELIB_DANGER_RELEASE(ptd);
253  }
254  }
255  }
256  uno_Any exc;
257  uno_Any * pexc = &exc;
258  proxy->getUnoI()->pDispatcher(
259  proxy->getUnoI(), description.get(), retin, args, &pexc);
260  if (pexc != nullptr) {
261  for (sal_Int32 i = 0; i != count; ++i) {
262  if (argtds[i] != nullptr) {
263  if (parameters[i].bIn) {
264  uno_destructData(args[i], argtds[i], nullptr);
265  }
266  TYPELIB_DANGER_RELEASE(argtds[i]);
267  }
268  }
269  if (rtd != nullptr) {
270  TYPELIB_DANGER_RELEASE(rtd);
271  }
273  }
274  for (sal_Int32 i = 0; i != count; ++i) {
275  if (argtds[i] != nullptr) {
276  if (parameters[i].bOut) {
278  cppArgs[i], argtds[i],
279  reinterpret_cast<uno_ReleaseFunc>(css::uno::cpp_release));
281  cppArgs[i], args[i], argtds[i],
282  proxy->getBridge()->getUno2Cpp());
283  }
284  uno_destructData(args[i], argtds[i], nullptr);
285  TYPELIB_DANGER_RELEASE(argtds[i]);
286  }
287  }
288  void * retout = nullptr; // avoid false -Werror=maybe-uninitialized
289  switch (retKind) {
291  switch (rtd == nullptr ? typelib_TypeClass_VOID : rtd->eTypeClass) {
292  case typelib_TypeClass_VOID:
293  break;
294  case typelib_TypeClass_BOOLEAN:
295  case typelib_TypeClass_BYTE:
296  case typelib_TypeClass_SHORT:
297  case typelib_TypeClass_UNSIGNED_SHORT:
298  case typelib_TypeClass_LONG:
299  case typelib_TypeClass_UNSIGNED_LONG:
300  case typelib_TypeClass_HYPER:
301  case typelib_TypeClass_UNSIGNED_HYPER:
302  case typelib_TypeClass_CHAR:
303  case typelib_TypeClass_ENUM:
304  std::memcpy(gpr, retin, rtd->nSize);
305  assert(!retConv);
306  break;
307  case typelib_TypeClass_FLOAT:
308  case typelib_TypeClass_DOUBLE:
309  std::memcpy(fpr, retin, rtd->nSize);
310  assert(!retConv);
311  break;
312  case typelib_TypeClass_STRUCT:
313  if (retConv) {
314  retout = gpr;
315  } else {
316  std::memcpy(gpr, retin, rtd->nSize);
317  }
318  break;
319  default:
320  assert(false);
321  }
322  break;
324  assert(rtd != nullptr);
325  switch (rtd->nSize) {
326  case 16:
327  std::memcpy(fpr + 3, static_cast<char *>(retin) + 12, 4);
328  [[fallthrough]];
329  case 12:
330  std::memcpy(fpr + 2, static_cast<char *>(retin) + 8, 4);
331  [[fallthrough]];
332  case 8:
333  std::memcpy(fpr + 1, static_cast<char *>(retin) + 4, 4);
334  [[fallthrough]];
335  case 4:
336  std::memcpy(fpr, retin, 4);
337  break;
338  default:
339  assert(false);
340  }
341  assert(!retConv);
342  break;
344  assert(rtd != nullptr);
345  std::memcpy(fpr, retin, rtd->nSize);
346  assert(!retConv);
347  break;
349  retout = indirectRet;
350  break;
351  }
352  if (retConv) {
354  retout, retin, rtd, proxy->getBridge()->getUno2Cpp());
355  uno_destructData(retin, rtd, nullptr);
356  }
357  if (rtd != nullptr) {
358  TYPELIB_DANGER_RELEASE(rtd);
359  }
360 }
361 
362 }
363 
365  sal_Int32 functionIndex, sal_Int32 vtableOffset,
366  unsigned long * gpr, unsigned long * fpr, unsigned long * stack,
367  void * indirectRet)
368 {
371  reinterpret_cast<char *>(gpr[0]) - vtableOffset);
372  typelib_InterfaceTypeDescription * type = proxy->getTypeDescr();
373  assert(functionIndex < type->nMapFunctionIndexToMemberIndex);
374  sal_Int32 pos = type->pMapFunctionIndexToMemberIndex[functionIndex];
375  css::uno::TypeDescription desc(type->ppAllMembers[pos]);
376  switch (desc.get()->eTypeClass) {
377  case typelib_TypeClass_INTERFACE_ATTRIBUTE:
378  if (type->pMapMemberIndexToFunctionIndex[pos] == functionIndex) {
379  // Getter:
380  call(
381  proxy, desc,
382  reinterpret_cast<typelib_InterfaceAttributeTypeDescription *>(
383  desc.get())->pAttributeTypeRef,
384  0, nullptr, gpr, fpr, stack, indirectRet);
385  } else {
386  // Setter:
387  typelib_MethodParameter param = {
388  nullptr,
389  reinterpret_cast<typelib_InterfaceAttributeTypeDescription *>(
390  desc.get())->pAttributeTypeRef,
391  true, false };
392  call(proxy, desc, nullptr, 1, &param, gpr, fpr, stack, indirectRet);
393  }
394  break;
395  case typelib_TypeClass_INTERFACE_METHOD:
396  switch (functionIndex) {
397  case 1:
398  proxy->acquireProxy();
399  break;
400  case 2:
401  proxy->releaseProxy();
402  break;
403  case 0:
404  {
405  typelib_TypeDescription * td = nullptr;
406  TYPELIB_DANGER_GET(
407  &td,
408  (reinterpret_cast<css::uno::Type *>(gpr[1])
409  ->getTypeLibType()));
410  if (td != nullptr && td->eTypeClass == typelib_TypeClass_INTERFACE) {
411  css::uno::XInterface * ifc = nullptr;
412  proxy->getBridge()->getCppEnv()->getRegisteredInterface(
413  proxy->getBridge()->getCppEnv(),
414  reinterpret_cast<void **>(&ifc), proxy->getOid().pData,
415  reinterpret_cast<typelib_InterfaceTypeDescription *>(
416  td));
417  if (ifc != nullptr) {
419  static_cast<uno_Any *>(indirectRet), &ifc, td,
420  reinterpret_cast<uno_AcquireFunc>(
421  css::uno::cpp_acquire));
422  ifc->release();
423  TYPELIB_DANGER_RELEASE(td);
424  break;
425  }
426  TYPELIB_DANGER_RELEASE(td);
427  }
428  }
429  [[fallthrough]];
430  default:
431  call(
432  proxy, desc,
433  reinterpret_cast<typelib_InterfaceMethodTypeDescription *>(
434  desc.get())->pReturnTypeRef,
435  reinterpret_cast<typelib_InterfaceMethodTypeDescription *>(
436  desc.get())->nParams,
437  reinterpret_cast<typelib_InterfaceMethodTypeDescription *>(
438  desc.get())->pParams,
439  gpr, fpr, stack, indirectRet);
440  }
441  break;
442  default:
443  assert(false);
444  }
445 }
446 
447 namespace {
448 
449 std::size_t const codeSnippetSize = 8 * 4;
450 
451 unsigned char * generateCodeSnippet(
452  unsigned char * code, sal_Int32 functionIndex, sal_Int32 vtableOffset)
453 {
454  // movz x9, <low functionIndex>
455  reinterpret_cast<unsigned int *>(code)[0] = 0xD2800009
456  | ((functionIndex & 0xFFFF) << 5);
457  // movk x9, <high functionIndex>, LSL #16
458  reinterpret_cast<unsigned int *>(code)[1] = 0xF2A00009
459  | ((functionIndex >> 16) << 5);
460  // movz x10, <low vtableOffset>
461  reinterpret_cast<unsigned int *>(code)[2] = 0xD280000A
462  | ((vtableOffset & 0xFFFF) << 5);
463  // movk x10, <high vtableOffset>, LSL #16
464  reinterpret_cast<unsigned int *>(code)[3] = 0xF2A0000A
465  | ((vtableOffset >> 16) << 5);
466  // ldr x11, +2*4
467  reinterpret_cast<unsigned int *>(code)[4] = 0x5800004B;
468  // br x11
469  reinterpret_cast<unsigned int *>(code)[5] = 0xD61F0160;
470  reinterpret_cast<unsigned long *>(code)[3]
471  = reinterpret_cast<unsigned long>(&vtableSlotCall);
472  return code + codeSnippetSize;
473 }
474 
475 }
476 
478 
481  return static_cast<Slot *>(block) + 2;
482 }
483 
485  sal_Int32 slotCount)
486 {
487  return (slotCount + 2) * sizeof (Slot) + slotCount * codeSnippetSize;
488 }
489 
492  void * block, sal_Int32 slotCount, sal_Int32,
493  typelib_InterfaceTypeDescription *)
494 {
495  Slot * slots = mapBlockToVtable(block);
496  slots[-2].fn = nullptr;
497  slots[-1].fn = nullptr;
498  return slots + slotCount;
499 }
500 
502  Slot ** slots, unsigned char * code,
503 #ifdef USE_DOUBLE_MMAP
504  sal_PtrDiff writetoexecdiff,
505 #endif
506  typelib_InterfaceTypeDescription const * type, sal_Int32 functionOffset,
507  sal_Int32 functionCount, sal_Int32 vtableOffset)
508 {
509 #ifndef USE_DOUBLE_MMAP
510  constexpr sal_PtrDiff writetoexecdiff = 0;
511 #endif
512  (*slots) -= functionCount;
513  Slot * s = *slots;
514  for (sal_Int32 i = 0; i != type->nMembers; ++i) {
515  typelib_TypeDescription * td = nullptr;
516  TYPELIB_DANGER_GET(&td, type->ppMembers[i]);
517  assert(td != nullptr);
518  switch (td->eTypeClass) {
519  case typelib_TypeClass_INTERFACE_ATTRIBUTE:
520  {
521  typelib_InterfaceAttributeTypeDescription * atd
522  = reinterpret_cast<
523  typelib_InterfaceAttributeTypeDescription *>(td);
524  // Getter:
525  (s++)->fn = code + writetoexecdiff;
526  code = generateCodeSnippet(
527  code, functionOffset++, vtableOffset);
528  // Setter:
529  if (!atd->bReadOnly) {
530  (s++)->fn = code + writetoexecdiff;
531  code = generateCodeSnippet(
532  code, functionOffset++, vtableOffset);
533  }
534  break;
535  }
536  case typelib_TypeClass_INTERFACE_METHOD:
537  (s++)->fn = code + writetoexecdiff;
538  code = generateCodeSnippet(code, functionOffset++, vtableOffset);
539  break;
540  default:
541  assert(false);
542  }
543  TYPELIB_DANGER_RELEASE(td);
544  }
545  return code;
546 }
547 
549  unsigned char const * begin, unsigned char const * end)
550 {
551 #if !defined ANDROID && !defined MACOSX
552  static void (*clear_cache)(unsigned char const *, unsigned char const *)
553  = (void (*)(unsigned char const *, unsigned char const *)) dlsym(
554  RTLD_DEFAULT, "__clear_cache");
555  (*clear_cache)(begin, end);
556 #else
557  // GCC clarified with
558  // <http://gcc.gnu.org/git/?p=gcc.git;a=commit;h=a90b0cdd444f6dde1084a439862cf507f6d3b2ae>
559  // "extend.texi (__clear_cache): Correct signature" that __builtin___clear_cache takes void*
560  // parameters, while Clang uses char* ever since
561  // <https://github.com/llvm/llvm-project/commit/c491a8d4577052bc6b3b4c72a7db6a7cfcbc2ed0> "Add
562  // support for __builtin___clear_cache in Clang" (TODO: see
563  // <https://bugs.llvm.org/show_bug.cgi?id=48489> "__builtin___clear_cache() has a different
564  // prototype than GCC"; once fixed for our Clang baseline, we can drop the reinterpret_casts):
565  __builtin___clear_cache(
566  reinterpret_cast<char *>(const_cast<unsigned char *>(begin)),
567  reinterpret_cast<char *>(const_cast<unsigned char *>(end)));
568 #endif
569 }
570 
571 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void vtableCall(sal_Int32 functionIndex, sal_Int32 vtableOffset, unsigned long *gpr, unsigned long *fpr, unsigned long *stack, void *indirectRet)
void SAL_CALL uno_destructData(void *pValue, typelib_TypeDescription *pTypeDescr, uno_ReleaseFunc release) SAL_THROW_EXTERN_C()
void raiseException(uno_Any *any, uno_Mapping *mapping)
tuple args
bool isSimpleType(typelib_TypeClass typeClass)
Determines whether a type is a "simple" type (VOID, BOOLEAN, BYTE, SHORT, UNSIGNED SHORT...
Definition: types.cxx:28
static std::size_t getBlockSize(sal_Int32 slotCount)
Calculate the size of a raw vtable block.
#define USE_DOUBLE_MMAP
sal_Unicode code
const int codeSnippetSize
void SAL_CALL uno_any_construct(uno_Any *pDest, void *pSource, typelib_TypeDescription *pTypeDescr, uno_AcquireFunc acquire) SAL_THROW_EXTERN_C()
bool relatesToInterfaceType(typelib_TypeDescription const *type)
Determines whether a type relates to an interface type (is itself an interface type, or might contain entities of interface type).
Definition: types.cxx:41
enumrange< T >::Iterator begin(enumrange< T >)
const BorderLinePrimitive2D *pCandidateB assert(pCandidateA)
size_t pos
static Slot * initializeBlock(void *block, sal_Int32 slotCount, sal_Int32 vtableNumber, typelib_InterfaceTypeDescription *type)
Initialize a raw vtable block.
static Slot * mapBlockToVtable(void *block)
Given a pointer to a block, turn it into a vtable pointer.
struct _uno_Any uno_Any
Definition: msvc/except.hxx:31
uno_Mapping * getUno2Cpp()
Definition: bridge.hxx:73
void vtableSlotCall()
uno_ExtEnvironment * getCppEnv()
Definition: bridge.hxx:69
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...
typelib_InterfaceTypeDescription * getTypeDescr()
A cpp proxy wrapping a uno interface.
int i
void SAL_CALL uno_copyAndConvertData(void *pDest, void *pSource, typelib_TypeDescription *pTypeDescr, uno_Mapping *mapping) SAL_THROW_EXTERN_C()
struct _typelib_TypeDescription typelib_TypeDescription
Definition: msvc/except.hxx:52
uno_Mapping * getCpp2Uno()
Definition: bridge.hxx:72
enumrange< T >::Iterator end(enumrange< T >)
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.
ResultType type
static CppInterfaceProxy * castInterfaceToProxy(void *pInterface)
ReturnKind getReturnKind(typelib_TypeDescription const *type)
typedef void(CALLTYPE *GetFuncDataPtr)(sal_uInt16 &nNo