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