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 <sal/alloca.h>
28 #include <rtl/alloc.h>
29
30 #include <uno/data.h>
31 #include <bridges/cpp_uno/bridge.hxx>
32 #include <bridges/cpp_uno/type_misc.hxx>
33
34 #include "share.hxx"
35
36
37 using namespace ::rtl;
38 using namespace ::com::sun::star::uno;
39
40 namespace CPPU_CURRENT_NAMESPACE
41 {
42
43 void dummy_can_throw_anything( char const * );
44
45 //==================================================================================================
46 // The call instruction within the asm section of callVirtualMethod may throw
47 // exceptions. So that the compiler handles this correctly, it is important
48 // that (a) callVirtualMethod might call dummy_can_throw_anything (although this
49 // never happens at runtime), which in turn can throw exceptions, and (b)
50 // callVirtualMethod is not inlined at its call site (so that any exceptions are
51 // caught which are thrown from the instruction calling callVirtualMethod):
52 void callVirtualMethod(
53 void * pThis,
54 sal_Int32 nVtableIndex,
55 void * pRegisterReturn,
56 typelib_TypeClass eReturnType,
57 sal_Int32 * pStackLongs,
58 sal_Int32 nStackLongs ) __attribute__((noinline));
59
callVirtualMethod(void * pThis,sal_Int32 nVtableIndex,void * pRegisterReturn,typelib_TypeClass eReturnType,sal_Int32 * pStackLongs,sal_Int32 nStackLongs)60 void callVirtualMethod(
61 void * pThis,
62 sal_Int32 nVtableIndex,
63 void * pRegisterReturn,
64 typelib_TypeClass eReturnType,
65 sal_Int32 * pStackLongs,
66 sal_Int32 nStackLongs )
67 {
68 // parameter list is mixed list of * and values
69 // reference parameters are pointers
70
71 OSL_ENSURE( pStackLongs && pThis, "### null ptr!" );
72 OSL_ENSURE( (sizeof(void *) == 4) && (sizeof(sal_Int32) == 4), "### unexpected size of int!" );
73 OSL_ENSURE( nStackLongs && pStackLongs, "### no stack in callVirtualMethod !" );
74
75 // never called
76 if (! pThis) dummy_can_throw_anything("xxx"); // address something
77
78 volatile long edx = 0, eax = 0; // for register returns
79 void * stackptr;
80 asm volatile (
81 "mov %%esp, %6\n\t"
82 // copy values
83 "mov %0, %%eax\n\t"
84 "mov %%eax, %%edx\n\t"
85 "dec %%edx\n\t"
86 "shl $2, %%edx\n\t"
87 "add %1, %%edx\n"
88 "Lcopy:\n\t"
89 "pushl 0(%%edx)\n\t"
90 "sub $4, %%edx\n\t"
91 "dec %%eax\n\t"
92 "jne Lcopy\n\t"
93 // do the actual call
94 "mov %2, %%edx\n\t"
95 "mov 0(%%edx), %%edx\n\t"
96 "mov %3, %%eax\n\t"
97 "shl $2, %%eax\n\t"
98 "add %%eax, %%edx\n\t"
99 "mov 0(%%edx), %%edx\n\t"
100 "call *%%edx\n\t"
101 // save return registers
102 "mov %%eax, %4\n\t"
103 "mov %%edx, %5\n\t"
104 // cleanup stack
105 "mov %6, %%esp\n\t"
106 :
107 : "m"(nStackLongs), "m"(pStackLongs), "m"(pThis), "m"(nVtableIndex),
108 "m"(eax), "m"(edx), "m"(stackptr)
109 : "eax", "edx" );
110 switch( eReturnType )
111 {
112 case typelib_TypeClass_HYPER:
113 case typelib_TypeClass_UNSIGNED_HYPER:
114 ((long*)pRegisterReturn)[1] = edx;
115 case typelib_TypeClass_LONG:
116 case typelib_TypeClass_UNSIGNED_LONG:
117 case typelib_TypeClass_CHAR:
118 case typelib_TypeClass_ENUM:
119 ((long*)pRegisterReturn)[0] = eax;
120 break;
121 case typelib_TypeClass_SHORT:
122 case typelib_TypeClass_UNSIGNED_SHORT:
123 *(unsigned short*)pRegisterReturn = eax;
124 break;
125 case typelib_TypeClass_BOOLEAN:
126 case typelib_TypeClass_BYTE:
127 *(unsigned char*)pRegisterReturn = eax;
128 break;
129 case typelib_TypeClass_FLOAT:
130 asm ( "fstps %0" : : "m"(*(char *)pRegisterReturn) );
131 break;
132 case typelib_TypeClass_DOUBLE:
133 asm ( "fstpl %0\n\t" : : "m"(*(char *)pRegisterReturn) );
134 break;
135 }
136 }
137
138 //==================================================================================================
cpp_call(cppu_unoInterfaceProxy * pThis,sal_Int32 nVtableCall,typelib_TypeDescriptionReference * pReturnTypeRef,sal_Int32 nParams,typelib_MethodParameter * pParams,void * pUnoReturn,void * pUnoArgs[],uno_Any ** ppUnoExc)139 static void cpp_call(
140 cppu_unoInterfaceProxy * pThis,
141 sal_Int32 nVtableCall,
142 typelib_TypeDescriptionReference * pReturnTypeRef,
143 sal_Int32 nParams, typelib_MethodParameter * pParams,
144 void * pUnoReturn, void * pUnoArgs[], uno_Any ** ppUnoExc )
145 {
146 // max space for: [complex ret ptr], values|ptr ...
147 char * pCppStack =
148 (char *)alloca( sizeof(sal_Int32) + ((nParams+2) * sizeof(sal_Int64)) );
149 char * pCppStackStart = pCppStack;
150
151 // return
152 typelib_TypeDescription * pReturnTypeDescr = 0;
153 TYPELIB_DANGER_GET( &pReturnTypeDescr, pReturnTypeRef );
154 OSL_ENSURE( pReturnTypeDescr, "### expected return type description!" );
155
156 void * pCppReturn = 0; // if != 0 && != pUnoReturn, needs reconversion
157
158 if (pReturnTypeDescr)
159 {
160 if (cppu_isSimpleType( pReturnTypeDescr ))
161 {
162 pCppReturn = pUnoReturn; // direct way for simple types
163 }
164 else
165 {
166 // complex return via ptr
167 pCppReturn = *(void **)pCppStack = (cppu_relatesToInterface( pReturnTypeDescr )
168 ? alloca( pReturnTypeDescr->nSize )
169 : pUnoReturn); // direct way
170 pCppStack += sizeof(void *);
171 }
172 }
173 // push this
174 *(void**)pCppStack = pThis->pCppI;
175 pCppStack += sizeof( void* );
176
177 // stack space
178 OSL_ENSURE( sizeof(void *) == sizeof(sal_Int32), "### unexpected size!" );
179 // args
180 void ** pCppArgs = (void **)alloca( 3 * sizeof(void *) * nParams );
181 // indizes of values this have to be converted (interface conversion cpp<=>uno)
182 sal_Int32 * pTempIndizes = (sal_Int32 *)(pCppArgs + nParams);
183 // type descriptions for reconversions
184 typelib_TypeDescription ** ppTempParamTypeDescr = (typelib_TypeDescription **)(pCppArgs + (2 * nParams));
185
186 sal_Int32 nTempIndizes = 0;
187
188 for ( sal_Int32 nPos = 0; nPos < nParams; ++nPos )
189 {
190 const typelib_MethodParameter & rParam = pParams[nPos];
191 typelib_TypeDescription * pParamTypeDescr = 0;
192 TYPELIB_DANGER_GET( &pParamTypeDescr, rParam.pTypeRef );
193
194 if (!rParam.bOut && cppu_isSimpleType( pParamTypeDescr ))
195 {
196 uno_copyAndConvertData( pCppArgs[nPos] = pCppStack, pUnoArgs[nPos], pParamTypeDescr,
197 &pThis->pBridge->aUno2Cpp );
198
199 switch (pParamTypeDescr->eTypeClass)
200 {
201 case typelib_TypeClass_HYPER:
202 case typelib_TypeClass_UNSIGNED_HYPER:
203 case typelib_TypeClass_DOUBLE:
204 pCppStack += sizeof(sal_Int32); // extra long
205 }
206 // no longer needed
207 TYPELIB_DANGER_RELEASE( pParamTypeDescr );
208 }
209 else // ptr to complex value | ref
210 {
211 if (! rParam.bIn) // is pure out
212 {
213 // cpp out is constructed mem, uno out is not!
214 uno_constructData(
215 *(void **)pCppStack = pCppArgs[nPos] = alloca( pParamTypeDescr->nSize ),
216 pParamTypeDescr );
217 pTempIndizes[nTempIndizes] = nPos; // default constructed for cpp call
218 // will be released at reconversion
219 ppTempParamTypeDescr[nTempIndizes++] = pParamTypeDescr;
220 }
221 // is in/inout
222 else if (cppu_relatesToInterface( pParamTypeDescr ))
223 {
224 uno_copyAndConvertData(
225 *(void **)pCppStack = pCppArgs[nPos] = alloca( pParamTypeDescr->nSize ),
226 pUnoArgs[nPos], pParamTypeDescr, &pThis->pBridge->aUno2Cpp );
227
228 pTempIndizes[nTempIndizes] = nPos; // has to be reconverted
229 // will be released at reconversion
230 ppTempParamTypeDescr[nTempIndizes++] = pParamTypeDescr;
231 }
232 else // direct way
233 {
234 *(void **)pCppStack = pCppArgs[nPos] = pUnoArgs[nPos];
235 // no longer needed
236 TYPELIB_DANGER_RELEASE( pParamTypeDescr );
237 }
238 }
239 pCppStack += sizeof(sal_Int32); // standard parameter length
240 }
241
242 try
243 {
244 OSL_ENSURE( !( (pCppStack - pCppStackStart ) & 3), "UNALIGNED STACK !!! (Please DO panic)" );
245 callVirtualMethod(
246 pThis->pCppI, nVtableCall,
247 pCppReturn, pReturnTypeDescr->eTypeClass,
248 (sal_Int32 *)pCppStackStart, (pCppStack - pCppStackStart) / sizeof(sal_Int32) );
249 // NO exception occured...
250 *ppUnoExc = 0;
251
252 // reconvert temporary params
253 for ( ; nTempIndizes--; )
254 {
255 sal_Int32 nIndex = pTempIndizes[nTempIndizes];
256 typelib_TypeDescription * pParamTypeDescr = ppTempParamTypeDescr[nTempIndizes];
257
258 if (pParams[nIndex].bIn)
259 {
260 if (pParams[nIndex].bOut) // inout
261 {
262 uno_destructData( pUnoArgs[nIndex], pParamTypeDescr, 0 ); // destroy uno value
263 uno_copyAndConvertData( pUnoArgs[nIndex], pCppArgs[nIndex], pParamTypeDescr,
264 &pThis->pBridge->aCpp2Uno );
265 }
266 }
267 else // pure out
268 {
269 uno_copyAndConvertData( pUnoArgs[nIndex], pCppArgs[nIndex], pParamTypeDescr,
270 &pThis->pBridge->aCpp2Uno );
271 }
272 // destroy temp cpp param => cpp: every param was constructed
273 uno_destructData( pCppArgs[nIndex], pParamTypeDescr, cpp_release );
274
275 TYPELIB_DANGER_RELEASE( pParamTypeDescr );
276 }
277 // return value
278 if (pCppReturn && pUnoReturn != pCppReturn)
279 {
280 uno_copyAndConvertData( pUnoReturn, pCppReturn, pReturnTypeDescr,
281 &pThis->pBridge->aCpp2Uno );
282 uno_destructData( pCppReturn, pReturnTypeDescr, cpp_release );
283 }
284 }
285 catch (...)
286 {
287 // fill uno exception
288 fillUnoException( __cxa_get_globals()->caughtExceptions, *ppUnoExc, &pThis->pBridge->aCpp2Uno );
289
290 // temporary params
291 for ( ; nTempIndizes--; )
292 {
293 sal_Int32 nIndex = pTempIndizes[nTempIndizes];
294 // destroy temp cpp param => cpp: every param was constructed
295 uno_destructData( pCppArgs[nIndex], ppTempParamTypeDescr[nTempIndizes], cpp_release );
296 TYPELIB_DANGER_RELEASE( ppTempParamTypeDescr[nTempIndizes] );
297 }
298 // return type
299 if (pReturnTypeDescr)
300 TYPELIB_DANGER_RELEASE( pReturnTypeDescr );
301 }
302 }
303
304
305 //==================================================================================================
cppu_unoInterfaceProxy_dispatch(uno_Interface * pUnoI,const typelib_TypeDescription * pMemberDescr,void * pReturn,void * pArgs[],uno_Any ** ppException)306 void SAL_CALL cppu_unoInterfaceProxy_dispatch(
307 uno_Interface * pUnoI, const typelib_TypeDescription * pMemberDescr,
308 void * pReturn, void * pArgs[], uno_Any ** ppException ) throw ()
309 {
310 // is my surrogate
311 cppu_unoInterfaceProxy * pThis = (cppu_unoInterfaceProxy *)pUnoI;
312 typelib_InterfaceTypeDescription * pTypeDescr = pThis->pTypeDescr;
313
314 switch (pMemberDescr->eTypeClass)
315 {
316 case typelib_TypeClass_INTERFACE_ATTRIBUTE:
317 {
318 // determine vtable call index
319 sal_Int32 nMemberPos = ((typelib_InterfaceMemberTypeDescription *)pMemberDescr)->nPosition;
320 OSL_ENSURE( nMemberPos < pTypeDescr->nAllMembers, "### member pos out of range!" );
321
322 sal_Int32 nVtableCall = pTypeDescr->pMapMemberIndexToFunctionIndex[nMemberPos];
323 OSL_ENSURE( nVtableCall < pTypeDescr->nMapFunctionIndexToMemberIndex, "### illegal vtable index!" );
324
325 if (pReturn)
326 {
327 // dependent dispatch
328 cpp_call(
329 pThis, nVtableCall,
330 ((typelib_InterfaceAttributeTypeDescription *)pMemberDescr)->pAttributeTypeRef,
331 0, 0, // no params
332 pReturn, pArgs, ppException );
333 }
334 else
335 {
336 // is SET
337 typelib_MethodParameter aParam;
338 aParam.pTypeRef =
339 ((typelib_InterfaceAttributeTypeDescription *)pMemberDescr)->pAttributeTypeRef;
340 aParam.bIn = sal_True;
341 aParam.bOut = sal_False;
342
343 typelib_TypeDescriptionReference * pReturnTypeRef = 0;
344 OUString aVoidName( RTL_CONSTASCII_USTRINGPARAM("void") );
345 typelib_typedescriptionreference_new(
346 &pReturnTypeRef, typelib_TypeClass_VOID, aVoidName.pData );
347
348 // dependent dispatch
349 cpp_call(
350 pThis, nVtableCall +1, // get, then set method
351 pReturnTypeRef,
352 1, &aParam,
353 pReturn, pArgs, ppException );
354
355 typelib_typedescriptionreference_release( pReturnTypeRef );
356 }
357
358 break;
359 }
360 case typelib_TypeClass_INTERFACE_METHOD:
361 {
362 // determine vtable call index
363 sal_Int32 nMemberPos = ((typelib_InterfaceMemberTypeDescription *)pMemberDescr)->nPosition;
364 OSL_ENSURE( nMemberPos < pTypeDescr->nAllMembers, "### member pos out of range!" );
365
366 sal_Int32 nVtableCall = pTypeDescr->pMapMemberIndexToFunctionIndex[nMemberPos];
367 OSL_ENSURE( nVtableCall < pTypeDescr->nMapFunctionIndexToMemberIndex, "### illegal vtable index!" );
368
369 switch (nVtableCall)
370 {
371 // standard calls
372 case 1: // acquire uno interface
373 (*pUnoI->acquire)( pUnoI );
374 *ppException = 0;
375 break;
376 case 2: // release uno interface
377 (*pUnoI->release)( pUnoI );
378 *ppException = 0;
379 break;
380 case 0: // queryInterface() opt
381 {
382 typelib_TypeDescription * pTD = 0;
383 TYPELIB_DANGER_GET( &pTD, reinterpret_cast< Type * >( pArgs[0] )->getTypeLibType() );
384 if (pTD)
385 {
386 uno_Interface * pInterface = 0;
387 (*pThis->pBridge->pUnoEnv->getRegisteredInterface)(
388 pThis->pBridge->pUnoEnv,
389 (void **)&pInterface, pThis->oid.pData, (typelib_InterfaceTypeDescription *)pTD );
390
391 if (pInterface)
392 {
393 ::uno_any_construct(
394 reinterpret_cast< uno_Any * >( pReturn ),
395 &pInterface, pTD, 0 );
396 (*pInterface->release)( pInterface );
397 TYPELIB_DANGER_RELEASE( pTD );
398 *ppException = 0;
399 break;
400 }
401 TYPELIB_DANGER_RELEASE( pTD );
402 }
403 } // else perform queryInterface()
404 default:
405 // dependent dispatch
406 cpp_call(
407 pThis, nVtableCall,
408 ((typelib_InterfaceMethodTypeDescription *)pMemberDescr)->pReturnTypeRef,
409 ((typelib_InterfaceMethodTypeDescription *)pMemberDescr)->nParams,
410 ((typelib_InterfaceMethodTypeDescription *)pMemberDescr)->pParams,
411 pReturn, pArgs, ppException );
412 }
413 break;
414 }
415 default:
416 {
417 ::com::sun::star::uno::RuntimeException aExc(
418 OUString( RTL_CONSTASCII_USTRINGPARAM("illegal member type description!") ),
419 ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >() );
420
421 Type const & rExcType = ::getCppuType( &aExc );
422 // binary identical null reference
423 ::uno_type_any_construct( *ppException, &aExc, rExcType.getTypeLibType(), 0 );
424 }
425 }
426 }
427
428 }
429
430