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