1 /**************************************************************
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 *************************************************************/
21
22
23
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_bridges.hxx"
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <hash_map>
30
31 #include <rtl/alloc.h>
32 #include <osl/mutex.hxx>
33
34 #include <com/sun/star/uno/genfunc.hxx>
35 #include "com/sun/star/uno/RuntimeException.hpp"
36 #include <uno/data.h>
37 #include <typelib/typedescription.hxx>
38
39 #include "bridges/cpp_uno/shared/bridge.hxx"
40 #include "bridges/cpp_uno/shared/cppinterfaceproxy.hxx"
41 #include "bridges/cpp_uno/shared/types.hxx"
42 #include "bridges/cpp_uno/shared/vtablefactory.hxx"
43
44 #include "abi.hxx"
45 #include "share.hxx"
46
47 using namespace ::osl;
48 using namespace ::rtl;
49 using namespace ::com::sun::star::uno;
50
51 //==================================================================================================
52
53 // Perform the UNO call
54 //
55 // We must convert the paramaters stored in gpreg, fpreg and ovrflw to UNO
56 // arguments and call pThis->getUnoI()->pDispatcher.
57 //
58 // gpreg: [ret *], this, [gpr params]
59 // fpreg: [fpr params]
60 // ovrflw: [gpr or fpr params (properly aligned)]
61 //
62 // [ret *] is present when we are returning a structure bigger than 16 bytes
63 // Simple types are returned in rax, rdx (int), or xmm0, xmm1 (fp).
64 // Similarly structures <= 16 bytes are in rax, rdx, xmm0, xmm1 as necessary.
cpp2uno_call(bridges::cpp_uno::shared::CppInterfaceProxy * pThis,const typelib_TypeDescription * pMemberTypeDescr,typelib_TypeDescriptionReference * pReturnTypeRef,sal_Int32 nParams,typelib_MethodParameter * pParams,void ** gpreg,void ** fpreg,void ** ovrflw,sal_uInt64 * pRegisterReturn)65 static typelib_TypeClass cpp2uno_call(
66 bridges::cpp_uno::shared::CppInterfaceProxy * pThis,
67 const typelib_TypeDescription * pMemberTypeDescr,
68 typelib_TypeDescriptionReference * pReturnTypeRef, // 0 indicates void return
69 sal_Int32 nParams, typelib_MethodParameter * pParams,
70 void ** gpreg, void ** fpreg, void ** ovrflw,
71 sal_uInt64 * pRegisterReturn /* space for register return */ )
72 {
73 unsigned int nr_gpr = 0; //number of gpr registers used
74 unsigned int nr_fpr = 0; //number of fpr registers used
75
76 // return
77 typelib_TypeDescription * pReturnTypeDescr = 0;
78 if (pReturnTypeRef)
79 TYPELIB_DANGER_GET( &pReturnTypeDescr, pReturnTypeRef );
80
81 void * pUnoReturn = 0;
82 void * pCppReturn = 0; // complex return ptr: if != 0 && != pUnoReturn, reconversion need
83
84 if ( pReturnTypeDescr )
85 {
86 if ( x86_64::return_in_hidden_param( pReturnTypeRef ) )
87 {
88 pCppReturn = *gpreg++;
89 nr_gpr++;
90
91 pUnoReturn = ( bridges::cpp_uno::shared::relatesToInterfaceType( pReturnTypeDescr )
92 ? alloca( pReturnTypeDescr->nSize )
93 : pCppReturn ); // direct way
94 }
95 else
96 pUnoReturn = pRegisterReturn; // direct way for simple types
97 }
98
99 // pop this
100 gpreg++;
101 nr_gpr++;
102
103 // stack space
104 // parameters
105 void ** pUnoArgs = (void **)alloca( 4 * sizeof(void *) * nParams );
106 void ** pCppArgs = pUnoArgs + nParams;
107 // indizes of values this have to be converted (interface conversion cpp<=>uno)
108 sal_Int32 * pTempIndizes = (sal_Int32 *)(pUnoArgs + (2 * nParams));
109 // type descriptions for reconversions
110 typelib_TypeDescription ** ppTempParamTypeDescr = (typelib_TypeDescription **)(pUnoArgs + (3 * nParams));
111
112 sal_Int32 nTempIndizes = 0;
113
114 for ( sal_Int32 nPos = 0; nPos < nParams; ++nPos )
115 {
116 const typelib_MethodParameter & rParam = pParams[nPos];
117
118 int nUsedGPR = 0;
119 int nUsedSSE = 0;
120 #if OSL_DEBUG_LEVEL > 0
121 bool bFitsRegisters =
122 #endif
123 x86_64::examine_argument( rParam.pTypeRef, false, nUsedGPR, nUsedSSE );
124 if ( !rParam.bOut && bridges::cpp_uno::shared::isSimpleType( rParam.pTypeRef ) ) // value
125 {
126 // Simple types must fit exactly one register on x86_64
127 OSL_ASSERT( bFitsRegisters && ( ( nUsedSSE == 1 && nUsedGPR == 0 ) || ( nUsedSSE == 0 && nUsedGPR == 1 ) ) );
128
129 if ( nUsedSSE == 1 )
130 {
131 if ( nr_fpr < x86_64::MAX_SSE_REGS )
132 {
133 pCppArgs[nPos] = pUnoArgs[nPos] = fpreg++;
134 nr_fpr++;
135 }
136 else
137 pCppArgs[nPos] = pUnoArgs[nPos] = ovrflw++;
138 }
139 else if ( nUsedGPR == 1 )
140 {
141 if ( nr_gpr < x86_64::MAX_GPR_REGS )
142 {
143 pCppArgs[nPos] = pUnoArgs[nPos] = gpreg++;
144 nr_gpr++;
145 }
146 else
147 pCppArgs[nPos] = pUnoArgs[nPos] = ovrflw++;
148 }
149 }
150 else // struct <= 16 bytes || ptr to complex value || ref
151 {
152 typelib_TypeDescription * pParamTypeDescr = 0;
153 TYPELIB_DANGER_GET( &pParamTypeDescr, rParam.pTypeRef );
154
155 void *pCppStack;
156 if ( nr_gpr < x86_64::MAX_GPR_REGS )
157 {
158 pCppArgs[nPos] = pCppStack = *gpreg++;
159 nr_gpr++;
160 }
161 else
162 pCppArgs[nPos] = pCppStack = *ovrflw++;
163
164 if (! rParam.bIn) // is pure out
165 {
166 // uno out is unconstructed mem!
167 pUnoArgs[nPos] = alloca( pParamTypeDescr->nSize );
168 pTempIndizes[nTempIndizes] = nPos;
169 // will be released at reconversion
170 ppTempParamTypeDescr[nTempIndizes++] = pParamTypeDescr;
171 }
172 else if ( bridges::cpp_uno::shared::relatesToInterfaceType( pParamTypeDescr ) ) // is in/inout
173 {
174 uno_copyAndConvertData( pUnoArgs[nPos] = alloca( pParamTypeDescr->nSize ),
175 pCppStack, pParamTypeDescr,
176 pThis->getBridge()->getCpp2Uno() );
177 pTempIndizes[nTempIndizes] = nPos; // has to be reconverted
178 // will be released at reconversion
179 ppTempParamTypeDescr[nTempIndizes++] = pParamTypeDescr;
180 }
181 else // direct way
182 {
183 pUnoArgs[nPos] = pCppStack;
184 // no longer needed
185 TYPELIB_DANGER_RELEASE( pParamTypeDescr );
186 }
187 }
188 }
189
190 // ExceptionHolder
191 uno_Any aUnoExc; // Any will be constructed by callee
192 uno_Any * pUnoExc = &aUnoExc;
193
194 // invoke uno dispatch call
195 (*pThis->getUnoI()->pDispatcher)( pThis->getUnoI(), pMemberTypeDescr, pUnoReturn, pUnoArgs, &pUnoExc );
196
197 // in case an exception occurred...
198 if ( pUnoExc )
199 {
200 // destruct temporary in/inout params
201 for ( ; nTempIndizes--; )
202 {
203 sal_Int32 nIndex = pTempIndizes[nTempIndizes];
204
205 if (pParams[nIndex].bIn) // is in/inout => was constructed
206 uno_destructData( pUnoArgs[nIndex], ppTempParamTypeDescr[nTempIndizes], 0 );
207 TYPELIB_DANGER_RELEASE( ppTempParamTypeDescr[nTempIndizes] );
208 }
209 if (pReturnTypeDescr)
210 TYPELIB_DANGER_RELEASE( pReturnTypeDescr );
211
212 CPPU_CURRENT_NAMESPACE::raiseException( &aUnoExc, pThis->getBridge()->getUno2Cpp() ); // has to destruct the any
213 // is here for dummy
214 return typelib_TypeClass_VOID;
215 }
216 else // else no exception occurred...
217 {
218 // temporary params
219 for ( ; nTempIndizes--; )
220 {
221 sal_Int32 nIndex = pTempIndizes[nTempIndizes];
222 typelib_TypeDescription * pParamTypeDescr = ppTempParamTypeDescr[nTempIndizes];
223
224 if ( pParams[nIndex].bOut ) // inout/out
225 {
226 // convert and assign
227 uno_destructData( pCppArgs[nIndex], pParamTypeDescr, cpp_release );
228 uno_copyAndConvertData( pCppArgs[nIndex], pUnoArgs[nIndex], pParamTypeDescr,
229 pThis->getBridge()->getUno2Cpp() );
230 }
231 // destroy temp uno param
232 uno_destructData( pUnoArgs[nIndex], pParamTypeDescr, 0 );
233
234 TYPELIB_DANGER_RELEASE( pParamTypeDescr );
235 }
236 // return
237 if ( pCppReturn ) // has complex return
238 {
239 if ( pUnoReturn != pCppReturn ) // needs reconversion
240 {
241 uno_copyAndConvertData( pCppReturn, pUnoReturn, pReturnTypeDescr,
242 pThis->getBridge()->getUno2Cpp() );
243 // destroy temp uno return
244 uno_destructData( pUnoReturn, pReturnTypeDescr, 0 );
245 }
246 // complex return ptr is set to return reg
247 *(void **)pRegisterReturn = pCppReturn;
248 }
249 if ( pReturnTypeDescr )
250 {
251 typelib_TypeClass eRet = (typelib_TypeClass)pReturnTypeDescr->eTypeClass;
252 TYPELIB_DANGER_RELEASE( pReturnTypeDescr );
253 return eRet;
254 }
255 else
256 return typelib_TypeClass_VOID;
257 }
258 }
259
260
261 //==================================================================================================
cpp_vtable_call(sal_Int32 nFunctionIndex,sal_Int32 nVtableOffset,void ** gpreg,void ** fpreg,void ** ovrflw,sal_uInt64 * pRegisterReturn)262 extern "C" typelib_TypeClass cpp_vtable_call(
263 sal_Int32 nFunctionIndex, sal_Int32 nVtableOffset,
264 void ** gpreg, void ** fpreg, void ** ovrflw,
265 sal_uInt64 * pRegisterReturn /* space for register return */ )
266 {
267 // gpreg: [ret *], this, [other gpr params]
268 // fpreg: [fpr params]
269 // ovrflw: [gpr or fpr params (properly aligned)]
270 void * pThis;
271 if ( nFunctionIndex & 0x80000000 )
272 {
273 nFunctionIndex &= 0x7fffffff;
274 pThis = gpreg[1];
275 }
276 else
277 {
278 pThis = gpreg[0];
279 }
280 pThis = static_cast<char *>( pThis ) - nVtableOffset;
281
282 bridges::cpp_uno::shared::CppInterfaceProxy * pCppI =
283 bridges::cpp_uno::shared::CppInterfaceProxy::castInterfaceToProxy( pThis );
284
285 typelib_InterfaceTypeDescription * pTypeDescr = pCppI->getTypeDescr();
286
287 OSL_ENSURE( nFunctionIndex < pTypeDescr->nMapFunctionIndexToMemberIndex, "### illegal vtable index!\n" );
288 if ( nFunctionIndex >= pTypeDescr->nMapFunctionIndexToMemberIndex )
289 {
290 throw RuntimeException( OUString::createFromAscii("illegal vtable index!"),
291 reinterpret_cast<XInterface *>( pCppI ) );
292 }
293
294 // determine called method
295 sal_Int32 nMemberPos = pTypeDescr->pMapFunctionIndexToMemberIndex[nFunctionIndex];
296 OSL_ENSURE( nMemberPos < pTypeDescr->nAllMembers, "### illegal member index!\n" );
297
298 TypeDescription aMemberDescr( pTypeDescr->ppAllMembers[nMemberPos] );
299
300 typelib_TypeClass eRet;
301 switch ( aMemberDescr.get()->eTypeClass )
302 {
303 case typelib_TypeClass_INTERFACE_ATTRIBUTE:
304 {
305 typelib_TypeDescriptionReference *pAttrTypeRef =
306 reinterpret_cast<typelib_InterfaceAttributeTypeDescription *>( aMemberDescr.get() )->pAttributeTypeRef;
307
308 if ( pTypeDescr->pMapMemberIndexToFunctionIndex[nMemberPos] == nFunctionIndex )
309 {
310 // is GET method
311 eRet = cpp2uno_call( pCppI, aMemberDescr.get(), pAttrTypeRef,
312 0, 0, // no params
313 gpreg, fpreg, ovrflw, pRegisterReturn );
314 }
315 else
316 {
317 // is SET method
318 typelib_MethodParameter aParam;
319 aParam.pTypeRef = pAttrTypeRef;
320 aParam.bIn = sal_True;
321 aParam.bOut = sal_False;
322
323 eRet = cpp2uno_call( pCppI, aMemberDescr.get(),
324 0, // indicates void return
325 1, &aParam,
326 gpreg, fpreg, ovrflw, pRegisterReturn );
327 }
328 break;
329 }
330 case typelib_TypeClass_INTERFACE_METHOD:
331 {
332 // is METHOD
333 switch ( nFunctionIndex )
334 {
335 case 1: // acquire()
336 pCppI->acquireProxy(); // non virtual call!
337 eRet = typelib_TypeClass_VOID;
338 break;
339 case 2: // release()
340 pCppI->releaseProxy(); // non virtual call!
341 eRet = typelib_TypeClass_VOID;
342 break;
343 case 0: // queryInterface() opt
344 {
345 typelib_TypeDescription * pTD = 0;
346 TYPELIB_DANGER_GET( &pTD, reinterpret_cast<Type *>( gpreg[2] )->getTypeLibType() );
347 if ( pTD )
348 {
349 XInterface * pInterface = 0;
350 (*pCppI->getBridge()->getCppEnv()->getRegisteredInterface)
351 ( pCppI->getBridge()->getCppEnv(),
352 (void **)&pInterface,
353 pCppI->getOid().pData,
354 reinterpret_cast<typelib_InterfaceTypeDescription *>( pTD ) );
355
356 if ( pInterface )
357 {
358 ::uno_any_construct( reinterpret_cast<uno_Any *>( gpreg[0] ),
359 &pInterface, pTD, cpp_acquire );
360
361 pInterface->release();
362 TYPELIB_DANGER_RELEASE( pTD );
363
364 reinterpret_cast<void **>( pRegisterReturn )[0] = gpreg[0];
365 eRet = typelib_TypeClass_ANY;
366 break;
367 }
368 TYPELIB_DANGER_RELEASE( pTD );
369 }
370 } // else perform queryInterface()
371 default:
372 {
373 typelib_InterfaceMethodTypeDescription *pMethodTD =
374 reinterpret_cast<typelib_InterfaceMethodTypeDescription *>( aMemberDescr.get() );
375
376 eRet = cpp2uno_call( pCppI, aMemberDescr.get(),
377 pMethodTD->pReturnTypeRef,
378 pMethodTD->nParams,
379 pMethodTD->pParams,
380 gpreg, fpreg, ovrflw, pRegisterReturn );
381 }
382 }
383 break;
384 }
385 default:
386 {
387 throw RuntimeException( OUString::createFromAscii("no member description found!"),
388 reinterpret_cast<XInterface *>( pCppI ) );
389 // is here for dummy
390 eRet = typelib_TypeClass_VOID;
391 }
392 }
393
394 return eRet;
395 }
396
397 //==================================================================================================
398 extern "C" void privateSnippetExecutor( ... );
399
400 const int codeSnippetSize = 24;
401
402 // Generate a trampoline that redirects method calls to
403 // privateSnippetExecutor().
404 //
405 // privateSnippetExecutor() saves all the registers that are used for
406 // parameter passing on x86_64, and calls the cpp_vtable_call().
407 // When it returns, privateSnippetExecutor() sets the return value.
408 //
409 // Note: The code snippet we build here must not create a stack frame,
410 // otherwise the UNO exceptions stop working thanks to non-existing
411 // unwinding info.
codeSnippet(unsigned char * code,sal_Int32 nFunctionIndex,sal_Int32 nVtableOffset,bool bHasHiddenParam)412 unsigned char * codeSnippet( unsigned char * code,
413 sal_Int32 nFunctionIndex, sal_Int32 nVtableOffset,
414 bool bHasHiddenParam ) SAL_THROW( () )
415 {
416 sal_uInt64 nOffsetAndIndex = ( ( (sal_uInt64) nVtableOffset ) << 32 ) | ( (sal_uInt64) nFunctionIndex );
417
418 if ( bHasHiddenParam )
419 nOffsetAndIndex |= 0x80000000;
420
421 // movq $<nOffsetAndIndex>, %r10
422 *reinterpret_cast<sal_uInt16 *>( code ) = 0xba49;
423 *reinterpret_cast<sal_uInt64 *>( code + 2 ) = nOffsetAndIndex;
424
425 // movq $<address of the privateSnippetExecutor>, %r11
426 *reinterpret_cast<sal_uInt16 *>( code + 10 ) = 0xbb49;
427 *reinterpret_cast<sal_uInt64 *>( code + 12 ) = reinterpret_cast<sal_uInt64>( privateSnippetExecutor );
428
429 // jmpq *%r11
430 *reinterpret_cast<sal_uInt32 *>( code + 20 ) = 0x00e3ff49;
431
432 return code + codeSnippetSize;
433 }
434
435 //==================================================================================================
436 struct bridges::cpp_uno::shared::VtableFactory::Slot { void * fn; };
437
438 bridges::cpp_uno::shared::VtableFactory::Slot *
mapBlockToVtable(void * block)439 bridges::cpp_uno::shared::VtableFactory::mapBlockToVtable(void * block)
440 {
441 return static_cast< Slot * >(block) + 2;
442 }
443
444 //==================================================================================================
getBlockSize(sal_Int32 slotCount)445 sal_Size bridges::cpp_uno::shared::VtableFactory::getBlockSize(
446 sal_Int32 slotCount)
447 {
448 return (slotCount + 2) * sizeof (Slot) + slotCount * codeSnippetSize;
449 }
450
451 //==================================================================================================
452 bridges::cpp_uno::shared::VtableFactory::Slot *
initializeBlock(void * block,sal_Int32 slotCount)453 bridges::cpp_uno::shared::VtableFactory::initializeBlock(
454 void * block, sal_Int32 slotCount)
455 {
456 Slot * slots = mapBlockToVtable(block);
457 slots[-2].fn = 0;
458 slots[-1].fn = 0;
459 return slots + slotCount;
460 }
461
462 //==================================================================================================
463
addLocalFunctions(Slot ** slots,unsigned char * code,sal_PtrDiff writetoexecdiff,typelib_InterfaceTypeDescription const * type,sal_Int32 nFunctionOffset,sal_Int32 functionCount,sal_Int32 nVtableOffset)464 unsigned char * bridges::cpp_uno::shared::VtableFactory::addLocalFunctions(
465 Slot ** slots, unsigned char * code, sal_PtrDiff writetoexecdiff,
466 typelib_InterfaceTypeDescription const * type, sal_Int32 nFunctionOffset,
467 sal_Int32 functionCount, sal_Int32 nVtableOffset )
468 {
469 (*slots) -= functionCount;
470 Slot * s = *slots;
471 for ( sal_Int32 nPos = 0; nPos < type->nMembers; ++nPos )
472 {
473 typelib_TypeDescription * pTD = 0;
474
475 TYPELIB_DANGER_GET( &pTD, type->ppMembers[ nPos ] );
476 OSL_ASSERT( pTD );
477
478 if ( typelib_TypeClass_INTERFACE_ATTRIBUTE == pTD->eTypeClass )
479 {
480 typelib_InterfaceAttributeTypeDescription *pAttrTD =
481 reinterpret_cast<typelib_InterfaceAttributeTypeDescription *>( pTD );
482
483 // get method
484 (s++)->fn = code + writetoexecdiff;
485 code = codeSnippet( code, nFunctionOffset++, nVtableOffset,
486 x86_64::return_in_hidden_param( pAttrTD->pAttributeTypeRef ) );
487
488 if ( ! pAttrTD->bReadOnly )
489 {
490 // set method
491 (s++)->fn = code + writetoexecdiff;
492 code = codeSnippet( code, nFunctionOffset++, nVtableOffset, false );
493 }
494 }
495 else if ( typelib_TypeClass_INTERFACE_METHOD == pTD->eTypeClass )
496 {
497 typelib_InterfaceMethodTypeDescription *pMethodTD =
498 reinterpret_cast<typelib_InterfaceMethodTypeDescription *>( pTD );
499
500 (s++)->fn = code + writetoexecdiff;
501 code = codeSnippet( code, nFunctionOffset++, nVtableOffset,
502 x86_64::return_in_hidden_param( pMethodTD->pReturnTypeRef ) );
503 }
504 else
505 OSL_ASSERT( false );
506
507 TYPELIB_DANGER_RELEASE( pTD );
508 }
509 return code;
510 }
511
512 //==================================================================================================
flushCode(unsigned char const *,unsigned char const *)513 void bridges::cpp_uno::shared::VtableFactory::flushCode(
514 unsigned char const *, unsigned char const * )
515 {
516 }
517