1*7a46fbe4Spfg /************************************************************** 2*7a46fbe4Spfg * 3*7a46fbe4Spfg * Licensed to the Apache Software Foundation (ASF) under one 4*7a46fbe4Spfg * or more contributor license agreements. See the NOTICE file 5*7a46fbe4Spfg * distributed with this work for additional information 6*7a46fbe4Spfg * regarding copyright ownership. The ASF licenses this file 7*7a46fbe4Spfg * to you under the Apache License, Version 2.0 (the 8*7a46fbe4Spfg * "License"); you may not use this file except in compliance 9*7a46fbe4Spfg * with the License. You may obtain a copy of the License at 10*7a46fbe4Spfg * 11*7a46fbe4Spfg * http://www.apache.org/licenses/LICENSE-2.0 12*7a46fbe4Spfg * 13*7a46fbe4Spfg * Unless required by applicable law or agreed to in writing, 14*7a46fbe4Spfg * software distributed under the License is distributed on an 15*7a46fbe4Spfg * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*7a46fbe4Spfg * KIND, either express or implied. See the License for the 17*7a46fbe4Spfg * specific language governing permissions and limitations 18*7a46fbe4Spfg * under the License. 19*7a46fbe4Spfg * 20*7a46fbe4Spfg *************************************************************/ 21*7a46fbe4Spfg 22*7a46fbe4Spfg 23*7a46fbe4Spfg 24*7a46fbe4Spfg #include <stdio.h> 25*7a46fbe4Spfg #include <string.h> 26*7a46fbe4Spfg #include <dlfcn.h> 27*7a46fbe4Spfg #include <cxxabi.h> 28*7a46fbe4Spfg #include <hash_map> 29*7a46fbe4Spfg 30*7a46fbe4Spfg #include <rtl/strbuf.hxx> 31*7a46fbe4Spfg #include <rtl/ustrbuf.hxx> 32*7a46fbe4Spfg #include <osl/diagnose.h> 33*7a46fbe4Spfg #include <osl/mutex.hxx> 34*7a46fbe4Spfg 35*7a46fbe4Spfg #include <com/sun/star/uno/genfunc.hxx> 36*7a46fbe4Spfg #include "com/sun/star/uno/RuntimeException.hpp" 37*7a46fbe4Spfg #include <typelib/typedescription.hxx> 38*7a46fbe4Spfg #include <uno/any2.h> 39*7a46fbe4Spfg 40*7a46fbe4Spfg #include "share.hxx" 41*7a46fbe4Spfg 42*7a46fbe4Spfg 43*7a46fbe4Spfg using namespace ::std; 44*7a46fbe4Spfg using namespace ::osl; 45*7a46fbe4Spfg using namespace ::rtl; 46*7a46fbe4Spfg using namespace ::com::sun::star::uno; 47*7a46fbe4Spfg using namespace ::__cxxabiv1; 48*7a46fbe4Spfg 49*7a46fbe4Spfg extern sal_Int32 * pHack; 50*7a46fbe4Spfg extern sal_Int32 nHack; 51*7a46fbe4Spfg 52*7a46fbe4Spfg namespace CPPU_CURRENT_NAMESPACE 53*7a46fbe4Spfg { dummy_can_throw_anything(char const *)54*7a46fbe4Spfg void dummy_can_throw_anything( char const * ) 55*7a46fbe4Spfg { 56*7a46fbe4Spfg } 57*7a46fbe4Spfg 58*7a46fbe4Spfg //=================================================================== toUNOname(char const * p)59*7a46fbe4Spfg static OUString toUNOname( char const * p ) SAL_THROW( () ) 60*7a46fbe4Spfg { 61*7a46fbe4Spfg #if OSL_DEBUG_LEVEL > 1 62*7a46fbe4Spfg char const * start = p; 63*7a46fbe4Spfg #endif 64*7a46fbe4Spfg 65*7a46fbe4Spfg // example: N3com3sun4star4lang24IllegalArgumentExceptionE 66*7a46fbe4Spfg 67*7a46fbe4Spfg OUStringBuffer buf( 64 ); 68*7a46fbe4Spfg OSL_ASSERT( 'N' == *p ); 69*7a46fbe4Spfg ++p; // skip N 70*7a46fbe4Spfg 71*7a46fbe4Spfg while ('E' != *p) 72*7a46fbe4Spfg { 73*7a46fbe4Spfg // read chars count 74*7a46fbe4Spfg long n = (*p++ - '0'); 75*7a46fbe4Spfg while ('0' <= *p && '9' >= *p) 76*7a46fbe4Spfg { 77*7a46fbe4Spfg n *= 10; 78*7a46fbe4Spfg n += (*p++ - '0'); 79*7a46fbe4Spfg } 80*7a46fbe4Spfg buf.appendAscii( p, n ); 81*7a46fbe4Spfg p += n; 82*7a46fbe4Spfg if ('E' != *p) 83*7a46fbe4Spfg buf.append( (sal_Unicode)'.' ); 84*7a46fbe4Spfg } 85*7a46fbe4Spfg 86*7a46fbe4Spfg #if OSL_DEBUG_LEVEL > 1 87*7a46fbe4Spfg OUString ret( buf.makeStringAndClear() ); 88*7a46fbe4Spfg OString c_ret( OUStringToOString( ret, RTL_TEXTENCODING_ASCII_US ) ); 89*7a46fbe4Spfg fprintf( stderr, "> toUNOname(): %s => %s\n", start, c_ret.getStr() ); 90*7a46fbe4Spfg return ret; 91*7a46fbe4Spfg #else 92*7a46fbe4Spfg return buf.makeStringAndClear(); 93*7a46fbe4Spfg #endif 94*7a46fbe4Spfg } 95*7a46fbe4Spfg 96*7a46fbe4Spfg //===================================================================== 97*7a46fbe4Spfg class RTTI 98*7a46fbe4Spfg { 99*7a46fbe4Spfg typedef hash_map< OUString, type_info *, OUStringHash > t_rtti_map; 100*7a46fbe4Spfg 101*7a46fbe4Spfg Mutex m_mutex; 102*7a46fbe4Spfg t_rtti_map m_rttis; 103*7a46fbe4Spfg t_rtti_map m_generatedRttis; 104*7a46fbe4Spfg 105*7a46fbe4Spfg void * m_hApp; 106*7a46fbe4Spfg 107*7a46fbe4Spfg public: 108*7a46fbe4Spfg RTTI() SAL_THROW( () ); 109*7a46fbe4Spfg ~RTTI() SAL_THROW( () ); 110*7a46fbe4Spfg 111*7a46fbe4Spfg type_info * getRTTI(typelib_CompoundTypeDescription *) SAL_THROW( () ); 112*7a46fbe4Spfg }; 113*7a46fbe4Spfg //____________________________________________________________________ RTTI()114*7a46fbe4Spfg RTTI::RTTI() SAL_THROW( () ) 115*7a46fbe4Spfg : m_hApp( dlopen( 0, RTLD_LAZY ) ) 116*7a46fbe4Spfg { 117*7a46fbe4Spfg } 118*7a46fbe4Spfg //____________________________________________________________________ ~RTTI()119*7a46fbe4Spfg RTTI::~RTTI() SAL_THROW( () ) 120*7a46fbe4Spfg { 121*7a46fbe4Spfg dlclose( m_hApp ); 122*7a46fbe4Spfg } 123*7a46fbe4Spfg 124*7a46fbe4Spfg //____________________________________________________________________ getRTTI(typelib_CompoundTypeDescription * pTypeDescr)125*7a46fbe4Spfg type_info * RTTI::getRTTI( typelib_CompoundTypeDescription *pTypeDescr ) SAL_THROW( () ) 126*7a46fbe4Spfg { 127*7a46fbe4Spfg type_info * rtti; 128*7a46fbe4Spfg 129*7a46fbe4Spfg OUString const & unoName = *(OUString const *)&pTypeDescr->aBase.pTypeName; 130*7a46fbe4Spfg 131*7a46fbe4Spfg MutexGuard guard( m_mutex ); 132*7a46fbe4Spfg t_rtti_map::const_iterator iFind( m_rttis.find( unoName ) ); 133*7a46fbe4Spfg if (iFind == m_rttis.end()) 134*7a46fbe4Spfg { 135*7a46fbe4Spfg // RTTI symbol 136*7a46fbe4Spfg OStringBuffer buf( 64 ); 137*7a46fbe4Spfg buf.append( RTL_CONSTASCII_STRINGPARAM("_ZTIN") ); 138*7a46fbe4Spfg sal_Int32 index = 0; 139*7a46fbe4Spfg do 140*7a46fbe4Spfg { 141*7a46fbe4Spfg OUString token( unoName.getToken( 0, '.', index ) ); 142*7a46fbe4Spfg buf.append( token.getLength() ); 143*7a46fbe4Spfg OString c_token( OUStringToOString( token, RTL_TEXTENCODING_ASCII_US ) ); 144*7a46fbe4Spfg buf.append( c_token ); 145*7a46fbe4Spfg } 146*7a46fbe4Spfg while (index >= 0); 147*7a46fbe4Spfg buf.append( 'E' ); 148*7a46fbe4Spfg 149*7a46fbe4Spfg OString symName( buf.makeStringAndClear() ); 150*7a46fbe4Spfg rtti = (type_info *)dlsym( m_hApp, symName.getStr() ); 151*7a46fbe4Spfg 152*7a46fbe4Spfg if (rtti) 153*7a46fbe4Spfg { 154*7a46fbe4Spfg pair< t_rtti_map::iterator, bool > insertion( 155*7a46fbe4Spfg m_rttis.insert( t_rtti_map::value_type( unoName, rtti ) ) ); 156*7a46fbe4Spfg OSL_ENSURE( insertion.second, "### inserting new rtti failed?!" ); 157*7a46fbe4Spfg } 158*7a46fbe4Spfg else 159*7a46fbe4Spfg { 160*7a46fbe4Spfg // try to lookup the symbol in the generated rtti map 161*7a46fbe4Spfg t_rtti_map::const_iterator iFind2( m_generatedRttis.find( unoName ) ); 162*7a46fbe4Spfg if (iFind2 == m_generatedRttis.end()) 163*7a46fbe4Spfg { 164*7a46fbe4Spfg // we must generate it ! 165*7a46fbe4Spfg // symbol and rtti-name is nearly identical, 166*7a46fbe4Spfg // the symbol is prefixed with _ZTI 167*7a46fbe4Spfg char const * rttiName = symName.getStr() +4; 168*7a46fbe4Spfg #if OSL_DEBUG_LEVEL > 1 169*7a46fbe4Spfg fprintf( stderr,"generated rtti for %s\n", rttiName ); 170*7a46fbe4Spfg #endif 171*7a46fbe4Spfg if (pTypeDescr->pBaseTypeDescription) 172*7a46fbe4Spfg { 173*7a46fbe4Spfg // ensure availability of base 174*7a46fbe4Spfg type_info * base_rtti = getRTTI( 175*7a46fbe4Spfg (typelib_CompoundTypeDescription *)pTypeDescr->pBaseTypeDescription ); 176*7a46fbe4Spfg rtti = new __si_class_type_info( 177*7a46fbe4Spfg strdup( rttiName ), (__class_type_info *)base_rtti ); 178*7a46fbe4Spfg } 179*7a46fbe4Spfg else 180*7a46fbe4Spfg { 181*7a46fbe4Spfg // this class has no base class 182*7a46fbe4Spfg rtti = new __class_type_info( strdup( rttiName ) ); 183*7a46fbe4Spfg } 184*7a46fbe4Spfg 185*7a46fbe4Spfg pair< t_rtti_map::iterator, bool > insertion( 186*7a46fbe4Spfg m_generatedRttis.insert( t_rtti_map::value_type( unoName, rtti ) ) ); 187*7a46fbe4Spfg OSL_ENSURE( insertion.second, "### inserting new generated rtti failed?!" ); 188*7a46fbe4Spfg } 189*7a46fbe4Spfg else // taking already generated rtti 190*7a46fbe4Spfg { 191*7a46fbe4Spfg rtti = iFind2->second; 192*7a46fbe4Spfg } 193*7a46fbe4Spfg } 194*7a46fbe4Spfg } 195*7a46fbe4Spfg else 196*7a46fbe4Spfg { 197*7a46fbe4Spfg rtti = iFind->second; 198*7a46fbe4Spfg } 199*7a46fbe4Spfg 200*7a46fbe4Spfg return rtti; 201*7a46fbe4Spfg } 202*7a46fbe4Spfg 203*7a46fbe4Spfg //------------------------------------------------------------------ deleteException(void * pExc)204*7a46fbe4Spfg static void deleteException( void * pExc ) 205*7a46fbe4Spfg { 206*7a46fbe4Spfg __cxa_exception const * header = ((__cxa_exception const *)pExc - 1); 207*7a46fbe4Spfg typelib_TypeDescription * pTD = 0; 208*7a46fbe4Spfg OUString unoName( toUNOname( header->exceptionType->name() ) ); 209*7a46fbe4Spfg ::typelib_typedescription_getByName( &pTD, unoName.pData ); 210*7a46fbe4Spfg OSL_ENSURE( pTD, "### unknown exception type! leaving out destruction => leaking!!!" ); 211*7a46fbe4Spfg if (pTD) 212*7a46fbe4Spfg { 213*7a46fbe4Spfg ::uno_destructData( pExc, pTD, cpp_release ); 214*7a46fbe4Spfg ::typelib_typedescription_release( pTD ); 215*7a46fbe4Spfg } 216*7a46fbe4Spfg } 217*7a46fbe4Spfg 218*7a46fbe4Spfg //================================================================== raiseException(uno_Any * pUnoExc,uno_Mapping * pUno2Cpp)219*7a46fbe4Spfg void raiseException( uno_Any * pUnoExc, uno_Mapping * pUno2Cpp ) 220*7a46fbe4Spfg { 221*7a46fbe4Spfg #if OSL_DEBUG_LEVEL > 1 222*7a46fbe4Spfg OString cstr( 223*7a46fbe4Spfg OUStringToOString( 224*7a46fbe4Spfg *reinterpret_cast< OUString const * >( &pUnoExc->pType->pTypeName ), 225*7a46fbe4Spfg RTL_TEXTENCODING_ASCII_US ) ); 226*7a46fbe4Spfg fprintf( stderr, "> uno exception occurred: %s\n", cstr.getStr() ); 227*7a46fbe4Spfg #endif 228*7a46fbe4Spfg void * pCppExc; 229*7a46fbe4Spfg type_info * rtti; 230*7a46fbe4Spfg 231*7a46fbe4Spfg { 232*7a46fbe4Spfg // construct cpp exception object 233*7a46fbe4Spfg typelib_TypeDescription * pTypeDescr = 0; 234*7a46fbe4Spfg TYPELIB_DANGER_GET( &pTypeDescr, pUnoExc->pType ); 235*7a46fbe4Spfg OSL_ASSERT( pTypeDescr ); 236*7a46fbe4Spfg if (! pTypeDescr) 237*7a46fbe4Spfg { 238*7a46fbe4Spfg throw RuntimeException( 239*7a46fbe4Spfg OUString( RTL_CONSTASCII_USTRINGPARAM("cannot get typedescription for type ") ) + 240*7a46fbe4Spfg *reinterpret_cast< OUString const * >( &pUnoExc->pType->pTypeName ), 241*7a46fbe4Spfg Reference< XInterface >() ); 242*7a46fbe4Spfg } 243*7a46fbe4Spfg 244*7a46fbe4Spfg pCppExc = __cxa_allocate_exception( pTypeDescr->nSize ); 245*7a46fbe4Spfg ::uno_copyAndConvertData( pCppExc, pUnoExc->pData, pTypeDescr, pUno2Cpp ); 246*7a46fbe4Spfg 247*7a46fbe4Spfg // destruct uno exception 248*7a46fbe4Spfg ::uno_any_destruct( pUnoExc, 0 ); 249*7a46fbe4Spfg // avoiding locked counts 250*7a46fbe4Spfg static RTTI * s_rtti = 0; 251*7a46fbe4Spfg if (! s_rtti) 252*7a46fbe4Spfg { 253*7a46fbe4Spfg MutexGuard guard( Mutex::getGlobalMutex() ); 254*7a46fbe4Spfg if (! s_rtti) 255*7a46fbe4Spfg { 256*7a46fbe4Spfg #ifdef LEAK_STATIC_DATA 257*7a46fbe4Spfg s_rtti = new RTTI(); 258*7a46fbe4Spfg #else 259*7a46fbe4Spfg static RTTI rtti_data; 260*7a46fbe4Spfg s_rtti = &rtti_data; 261*7a46fbe4Spfg #endif 262*7a46fbe4Spfg } 263*7a46fbe4Spfg } 264*7a46fbe4Spfg rtti = (type_info *)s_rtti->getRTTI( (typelib_CompoundTypeDescription *) pTypeDescr ); 265*7a46fbe4Spfg TYPELIB_DANGER_RELEASE( pTypeDescr ); 266*7a46fbe4Spfg OSL_ENSURE( rtti, "### no rtti for throwing exception!" ); 267*7a46fbe4Spfg if (! rtti) 268*7a46fbe4Spfg { 269*7a46fbe4Spfg throw RuntimeException( 270*7a46fbe4Spfg OUString( RTL_CONSTASCII_USTRINGPARAM("no rtti for type ") ) + 271*7a46fbe4Spfg *reinterpret_cast< OUString const * >( &pUnoExc->pType->pTypeName ), 272*7a46fbe4Spfg Reference< XInterface >() ); 273*7a46fbe4Spfg } 274*7a46fbe4Spfg } 275*7a46fbe4Spfg 276*7a46fbe4Spfg 277*7a46fbe4Spfg __cxa_throw( pCppExc, rtti, deleteException ); 278*7a46fbe4Spfg } 279*7a46fbe4Spfg 280*7a46fbe4Spfg #ifdef __ARM_EABI__ getAdjustedPtr(__cxa_exception * header)281*7a46fbe4Spfg static void* getAdjustedPtr(__cxa_exception* header) 282*7a46fbe4Spfg { 283*7a46fbe4Spfg return (void*)header->unwindHeader.barrier_cache.bitpattern[0]; 284*7a46fbe4Spfg } 285*7a46fbe4Spfg #else getAdjustedPtr(__cxa_exception * header)286*7a46fbe4Spfg static void* getAdjustedPtr(__cxa_exception* header) 287*7a46fbe4Spfg { 288*7a46fbe4Spfg return header->adjustedPtr; 289*7a46fbe4Spfg } 290*7a46fbe4Spfg #endif 291*7a46fbe4Spfg 292*7a46fbe4Spfg //=================================================================== fillUnoException(__cxa_exception * header,uno_Any * pUnoExc,uno_Mapping * pCpp2Uno)293*7a46fbe4Spfg void fillUnoException( __cxa_exception * header, uno_Any * pUnoExc, uno_Mapping * pCpp2Uno ) 294*7a46fbe4Spfg { 295*7a46fbe4Spfg if (! header) 296*7a46fbe4Spfg { 297*7a46fbe4Spfg RuntimeException aRE( 298*7a46fbe4Spfg OUString( RTL_CONSTASCII_USTRINGPARAM("no exception header!") ), 299*7a46fbe4Spfg Reference< XInterface >() ); 300*7a46fbe4Spfg Type const & rType = ::getCppuType( &aRE ); 301*7a46fbe4Spfg uno_type_any_constructAndConvert( pUnoExc, &aRE, rType.getTypeLibType(), pCpp2Uno ); 302*7a46fbe4Spfg #if OSL_DEBUG_LEVEL > 0 303*7a46fbe4Spfg OString cstr( OUStringToOString( aRE.Message, RTL_TEXTENCODING_ASCII_US ) ); 304*7a46fbe4Spfg OSL_ENSURE( 0, cstr.getStr() ); 305*7a46fbe4Spfg #endif 306*7a46fbe4Spfg return; 307*7a46fbe4Spfg } 308*7a46fbe4Spfg 309*7a46fbe4Spfg typelib_TypeDescription * pExcTypeDescr = 0; 310*7a46fbe4Spfg OUString unoName( toUNOname( header->exceptionType->name() ) ); 311*7a46fbe4Spfg #if OSL_DEBUG_LEVEL > 1 312*7a46fbe4Spfg OString cstr_unoName( OUStringToOString( unoName, RTL_TEXTENCODING_ASCII_US ) ); 313*7a46fbe4Spfg fprintf( stderr, "> c++ exception occurred: %s\n", cstr_unoName.getStr() ); 314*7a46fbe4Spfg #endif 315*7a46fbe4Spfg typelib_typedescription_getByName( &pExcTypeDescr, unoName.pData ); 316*7a46fbe4Spfg if (0 == pExcTypeDescr) 317*7a46fbe4Spfg { 318*7a46fbe4Spfg RuntimeException aRE( 319*7a46fbe4Spfg OUString( RTL_CONSTASCII_USTRINGPARAM("exception type not found: ") ) + unoName, 320*7a46fbe4Spfg Reference< XInterface >() ); 321*7a46fbe4Spfg Type const & rType = ::getCppuType( &aRE ); 322*7a46fbe4Spfg uno_type_any_constructAndConvert( pUnoExc, &aRE, rType.getTypeLibType(), pCpp2Uno ); 323*7a46fbe4Spfg #if OSL_DEBUG_LEVEL > 0 324*7a46fbe4Spfg OString cstr( OUStringToOString( aRE.Message, RTL_TEXTENCODING_ASCII_US ) ); 325*7a46fbe4Spfg OSL_ENSURE( 0, cstr.getStr() ); 326*7a46fbe4Spfg #endif 327*7a46fbe4Spfg } 328*7a46fbe4Spfg else 329*7a46fbe4Spfg { 330*7a46fbe4Spfg // construct uno exception any 331*7a46fbe4Spfg uno_any_constructAndConvert( pUnoExc, getAdjustedPtr(header), pExcTypeDescr, pCpp2Uno ); 332*7a46fbe4Spfg typelib_typedescription_release( pExcTypeDescr ); 333*7a46fbe4Spfg } 334*7a46fbe4Spfg } 335*7a46fbe4Spfg } 336*7a46fbe4Spfg 337*7a46fbe4Spfg /* vi:set tabstop=4 shiftwidth=4 expandtab: */ 338