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