LibreOffice Module bridges (master) 1
msvc_win32_x86-64/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
21#include <sal/config.h>
22
23#include <cassert>
24#include <limits>
25#include <typeinfo>
26
27#include <malloc.h>
28
29#include <com/sun/star/uno/genfunc.hxx>
30#include <sal/log.hxx>
31#include <uno/data.h>
32#include <typelib/typedescription.hxx>
33
34#include <bridge.hxx>
35#include <cppinterfaceproxy.hxx>
36#include <types.hxx>
37#include <vtablefactory.hxx>
38
39#include "call.hxx"
40#include <msvc/cpp2uno.hxx>
41#include <msvc/amd64.hxx>
42
43extern "C" IMAGE_DOS_HEADER const __ImageBase;
44
45using namespace ::com::sun::star;
46
47extern "C" typelib_TypeClass cpp_vtable_call(sal_Int64 nOffsetAndIndex, void ** pCallStack)
48{
49 sal_Int32 nFunctionIndex = (nOffsetAndIndex & 0xFFFFFFFF);
50 sal_Int32 nVtableOffset = ((nOffsetAndIndex >> 32) & 0xFFFFFFFF);
51 return cpp_mediate(pCallStack, nFunctionIndex, nVtableOffset, nullptr);
52}
53
54int const codeSnippetSize = 48;
55
56namespace {
57
58typedef enum { REGPARAM_INT, REGPARAM_FLT } RegParamKind;
59
60}
61
62extern "C" char privateSnippetExecutor;
63
64// This function generates the code that acts as a proxy for the UNO function to be called.
65// The generated code does the following:
66// - Spills register parameters on stack
67// - Loads functionIndex and vtableOffset into scratch registers
68// - Jumps to privateSnippetExecutor
69
70static unsigned char * codeSnippet(
71 unsigned char * code,
72 RegParamKind param_kind[4],
73 sal_Int32 nFunctionIndex,
74 sal_Int32 nVtableOffset )
75{
76 sal_uInt64 nOffsetAndIndex = ( static_cast<sal_uInt64>(nVtableOffset) << 32 ) | static_cast<sal_uInt64>(nFunctionIndex);
77 unsigned char *p = code;
78
79 // Spill parameters
80 if (param_kind[0] == REGPARAM_INT)
81 {
82 // mov qword ptr 8[rsp], rcx
83 *p++ = 0x48; *p++ = 0x89; *p++ = 0x4C; *p++ = 0x24; *p++ = 0x08;
84 }
85 else
86 {
87 // movsd qword ptr 8[rsp], xmm0
88 *p++ = 0xF2; *p++ = 0x0F; *p++ = 0x11; *p++ = 0x44; *p++ = 0x24; *p++ = 0x08;
89 }
90 if ( param_kind[1] == REGPARAM_INT )
91 {
92 // mov qword ptr 16[rsp], rdx
93 *p++ = 0x48; *p++ = 0x89; *p++ = 0x54; *p++ = 0x24; *p++ = 0x10;
94 }
95 else
96 {
97 // movsd qword ptr 16[rsp], xmm1
98 *p++ = 0xF2; *p++ = 0x0F; *p++ = 0x11; *p++ = 0x4C; *p++ = 0x24; *p++ = 0x10;
99 }
100 if ( param_kind[2] == REGPARAM_INT )
101 {
102 // mov qword ptr 24[rsp], r8
103 *p++ = 0x4C; *p++ = 0x89; *p++ = 0x44; *p++ = 0x24; *p++ = 0x18;
104 }
105 else
106 {
107 // movsd qword ptr 24[rsp], xmm2
108 *p++ = 0xF2; *p++ = 0x0F; *p++ = 0x11; *p++ = 0x54; *p++ = 0x24; *p++ = 0x18;
109 }
110 if ( param_kind[3] == REGPARAM_INT )
111 {
112 // mov qword ptr 32[rsp], r9
113 *p++ = 0x4C;*p++ = 0x89; *p++ = 0x4C; *p++ = 0x24; *p++ = 0x20;
114 }
115 else
116 {
117 // movsd qword ptr 32[rsp], xmm3
118 *p++ = 0xF2; *p++ = 0x0F; *p++ = 0x11; *p++ = 0x5C; *p++ = 0x24; *p++ = 0x20;
119 }
120
121 // mov rcx, nOffsetAndIndex
122 *p++ = 0x48; *p++ = 0xB9;
123 *reinterpret_cast<sal_uInt64 *>(p) = nOffsetAndIndex; p += 8;
124
125 // mov r11, privateSnippetExecutor
126 *p++ = 0x49; *p++ = 0xBB;
127 *reinterpret_cast<void **>(p) = &privateSnippetExecutor; p += 8;
128
129 // jmp r11
130 *p++ = 0x41; *p++ = 0xFF; *p++ = 0xE3;
131
132 assert(p < code + codeSnippetSize);
133
134 return code + codeSnippetSize;
135}
136
138
141 void * block )
142{
143 return static_cast< Slot * >(block) + 1;
144}
145
147 sal_Int32 slotCount)
148{
149 return (slotCount + 1) * sizeof (Slot) + slotCount * codeSnippetSize;
150}
151
152static sal_uInt32 imageRelative(void const * p) {
153 assert(
154 reinterpret_cast<sal_uIntPtr>(p) >= reinterpret_cast<sal_uIntPtr>(&__ImageBase)
155 && reinterpret_cast<sal_uIntPtr>(p) - reinterpret_cast<sal_uIntPtr>(&__ImageBase)
156 <= std::numeric_limits<sal_uInt32>::max());
157 return reinterpret_cast<sal_uIntPtr>(p) - reinterpret_cast<sal_uIntPtr>(&__ImageBase);
158}
159
160namespace {
161
162// Some dummy type whose RTTI is used in the synthesized proxy vtables to make uses of dynamic_cast
163// on such proxy objects not crash:
164struct ProxyRtti {};
165
166// The following vtable RTTI data is based on how the code at
167// <https://github.com/llvm/llvm-project/blob/main/clang/lib/CodeGen/MicrosoftCXXABI.cpp> computes
168// such data, and on how <https://devblogs.microsoft.com/oldnewthing/20041025-00/?p=37483>
169// "Accessing the current module’s HINSTANCE from a static library" obtians __ImageBase:
170
171struct RttiClassHierarchyDescriptor;
172
173#pragma warning (push)
174#pragma warning (disable: 4324) // "structure was padded due to alignment specifier"
175
176struct alignas(16) RttiBaseClassDescriptor {
177 sal_uInt32 n0 = imageRelative(&typeid(ProxyRtti));
178 sal_uInt32 n1 = 0;
179 sal_uInt32 n2 = 0;
180 sal_uInt32 n3 = 0xFFFFFFFF;
181 sal_uInt32 n4 = 0;
182 sal_uInt32 n5 = 0x40;
183 sal_uInt32 n6;
184 RttiBaseClassDescriptor(RttiClassHierarchyDescriptor const * chd): n6(imageRelative(chd)) {}
185};
186
187struct alignas(4) RttiBaseClassArray {
188 sal_uInt32 n0;
189 sal_uInt32 n1 = 0;
190 RttiBaseClassArray(RttiBaseClassDescriptor const * bcd): n0(imageRelative(bcd)) {}
191};
192
193struct alignas(8) RttiClassHierarchyDescriptor {
194 sal_uInt32 n0 = 0;
195 sal_uInt32 n1 = 0;
196 sal_uInt32 n2 = 1;
197 sal_uInt32 n3;
198 RttiClassHierarchyDescriptor(RttiBaseClassArray const * bca): n3(imageRelative(bca)) {}
199};
200
201struct alignas(16) RttiCompleteObjectLocator {
202 sal_uInt32 n0 = 1;
203 sal_uInt32 n1 = 0;
204 sal_uInt32 n2 = 0;
205 sal_uInt32 n3 = imageRelative(&typeid(ProxyRtti));
206 sal_uInt32 n4;
207 sal_uInt32 n5 = imageRelative(this);
208 RttiCompleteObjectLocator(RttiClassHierarchyDescriptor const * chd): n4(imageRelative(chd)) {}
209};
210
211struct Rtti {
212 RttiBaseClassDescriptor bcd;
213 RttiBaseClassArray bca;
214 RttiClassHierarchyDescriptor chd;
215 RttiCompleteObjectLocator col;
216 Rtti(): bcd(&chd), bca(&bcd), chd(&bca), col(&chd) {}
217};
218
219#pragma warning (pop)
220
221}
222
225 void * block,
226 sal_Int32 slotCount,
227 sal_Int32, typelib_InterfaceTypeDescription *)
228{
229 static Rtti rtti;
230
231 Slot * slots = mapBlockToVtable(block);
232 slots[-1].fn = &rtti.col;
233 return slots + slotCount;
234}
235
237 Slot ** slots,
238 unsigned char * code,
239 typelib_InterfaceTypeDescription const * type,
240 sal_Int32 nFunctionOffset,
241 sal_Int32 functionCount,
242 sal_Int32 nVtableOffset )
243{
244 (*slots) -= functionCount;
245 Slot * s = *slots;
246
247 for (int member = 0; member < type->nMembers; ++member) {
248 typelib_TypeDescription * pTD = nullptr;
249
250 TYPELIB_DANGER_GET( &pTD, type->ppMembers[ member ] );
251 assert(pTD);
252
253 RegParamKind param_kind[4];
254 int nr = 0;
255
256 for (int i = 0; i < 4; ++i)
257 param_kind[i] = REGPARAM_INT;
258
259 // 'this'
260 ++nr;
261
262 if ( pTD->eTypeClass == typelib_TypeClass_INTERFACE_ATTRIBUTE )
263 {
264 typelib_InterfaceAttributeTypeDescription * pIfaceAttrTD =
265 reinterpret_cast<typelib_InterfaceAttributeTypeDescription *>( pTD );
266
267 // Getter
268
269 (s++)->fn = code;
270 code = codeSnippet( code, param_kind, nFunctionOffset++, nVtableOffset );
271 if ( ! pIfaceAttrTD->bReadOnly )
272 {
273 typelib_TypeDescription * pAttrTD = nullptr;
274 TYPELIB_DANGER_GET( &pAttrTD, pIfaceAttrTD->pAttributeTypeRef );
275 assert(pAttrTD);
276
277 // Setter
278 if ( pAttrTD->eTypeClass == typelib_TypeClass_FLOAT ||
279 pAttrTD->eTypeClass == typelib_TypeClass_DOUBLE )
280 param_kind[nr++] = REGPARAM_FLT;
281
282 TYPELIB_DANGER_RELEASE( pAttrTD );
283
284 (s++)->fn = code;
285 code = codeSnippet( code, param_kind, nFunctionOffset++, nVtableOffset );
286 }
287 }
288 else if ( pTD->eTypeClass == typelib_TypeClass_INTERFACE_METHOD )
289 {
290 typelib_InterfaceMethodTypeDescription * pMethodTD =
291 reinterpret_cast<typelib_InterfaceMethodTypeDescription *>( pTD );
292
293 typelib_TypeDescription * pReturnTD = nullptr;
294 TYPELIB_DANGER_GET( &pReturnTD, pMethodTD->pReturnTypeRef );
295 assert(pReturnTD);
296
297 if ( !bridges::cpp_uno::shared::isSimpleType( pReturnTD ) )
298 {
299 // Return value
300 ++nr;
301 }
302
303 for (int param = 0; nr < 4 && param < pMethodTD->nParams; ++param, ++nr)
304 {
305 typelib_TypeDescription * pParamTD = nullptr;
306
307 TYPELIB_DANGER_GET( &pParamTD, pMethodTD->pParams[param].pTypeRef );
308 assert(pParamTD);
309
310 if ( pParamTD->eTypeClass == typelib_TypeClass_FLOAT ||
311 pParamTD->eTypeClass == typelib_TypeClass_DOUBLE )
312 param_kind[nr] = REGPARAM_FLT;
313
314 TYPELIB_DANGER_RELEASE( pParamTD );
315 }
316 (s++)->fn = code;
317 code = codeSnippet( code, param_kind, nFunctionOffset++, nVtableOffset );
318
319 TYPELIB_DANGER_RELEASE( pReturnTD );
320 }
321 else
322 assert(false);
323
324 TYPELIB_DANGER_RELEASE( pTD );
325 }
326 return code;
327}
328
330 unsigned char const *,
331 unsigned char const *)
332{
333}
334
335/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static Slot * mapBlockToVtable(void *block)
Given a pointer to a block, turn it into a vtable pointer.
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.
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...
static Slot * initializeBlock(void *block, sal_Int32 slotCount, sal_Int32 vtableNumber, typelib_InterfaceTypeDescription *type)
Initialize a raw vtable block.
static std::size_t getBlockSize(sal_Int32 slotCount)
Calculate the size of a raw vtable block.
void * p
struct _typelib_TypeDescription typelib_TypeDescription
Definition: msvc/except.hxx:53
typelib_TypeClass __cdecl cpp_mediate(void **pCallStack, const sal_Int32 nFunctionIndex, const sal_Int32 nVtableOffset, sal_Int64 *const pRegisterReturn)
RttiClassHierarchyDescriptor chd
sal_uInt32 n0
sal_uInt32 n4
sal_uInt32 n2
sal_uInt32 n5
RttiCompleteObjectLocator col
RttiBaseClassDescriptor bcd
RttiBaseClassArray bca
sal_uInt32 n6
sal_uInt32 n3
sal_uInt32 n1
typelib_TypeClass cpp_vtable_call(sal_Int64 nOffsetAndIndex, void **pCallStack)
int const codeSnippetSize
IMAGE_DOS_HEADER const __ImageBase
static unsigned char * codeSnippet(unsigned char *code, RegParamKind param_kind[4], sal_Int32 nFunctionIndex, sal_Int32 nVtableOffset)
char privateSnippetExecutor
static sal_uInt32 imageRelative(void const *p)
bool isSimpleType(typelib_TypeClass typeClass)
Determines whether a type is a "simple" type (VOID, BOOLEAN, BYTE, SHORT, UNSIGNED SHORT,...
Definition: types.cxx:28
int i
sal_Unicode code
ResultType type