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