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