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