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 #if ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 6)) 28 #include <exception> 29 #endif 30 31 #include <stdio.h> 32 #include <string.h> 33 #include <dlfcn.h> 34 #include <cxxabi.h> 35 #include <hash_map> 36 #include <sys/param.h> 37 38 #include <rtl/strbuf.hxx> 39 #include <rtl/ustrbuf.hxx> 40 #include <osl/diagnose.h> 41 #include <osl/mutex.hxx> 42 43 #include <com/sun/star/uno/genfunc.hxx> 44 #include "com/sun/star/uno/RuntimeException.hpp" 45 #include <typelib/typedescription.hxx> 46 #include <uno/any2.h> 47 48 #include "share.hxx" 49 50 51 using namespace ::std; 52 using namespace ::osl; 53 using namespace ::rtl; 54 using namespace ::com::sun::star::uno; 55 using namespace ::__cxxabiv1; 56 57 58 namespace CPPU_CURRENT_NAMESPACE 59 { 60 61 namespace { 62 63 typedef hash_map< OUString, type_info *, OUStringHash > ObservedRttiMap; 64 65 ObservedRttiMap & observedRttis() 66 { 67 static ObservedRttiMap map; 68 return map; 69 } 70 71 } 72 73 void dummy_can_throw_anything( char const * ) 74 { 75 } 76 77 //================================================================================================== 78 static OUString toUNOname( char const * p ) SAL_THROW( () ) 79 { 80 #if OSL_DEBUG_LEVEL > 1 81 char const * start = p; 82 #endif 83 84 // example: N3com3sun4star4lang24IllegalArgumentExceptionE 85 86 OUStringBuffer buf( 64 ); 87 OSL_ASSERT( 'N' == *p ); 88 ++p; // skip N 89 90 while ('E' != *p) 91 { 92 // read chars count 93 long n = (*p++ - '0'); 94 while ('0' <= *p && '9' >= *p) 95 { 96 n *= 10; 97 n += (*p++ - '0'); 98 } 99 buf.appendAscii( p, n ); 100 p += n; 101 if ('E' != *p) 102 buf.append( (sal_Unicode)'.' ); 103 } 104 105 #if OSL_DEBUG_LEVEL > 1 106 OUString ret( buf.makeStringAndClear() ); 107 OString c_ret( OUStringToOString( ret, RTL_TEXTENCODING_ASCII_US ) ); 108 fprintf( stderr, "> toUNOname(): %s => %s\n", start, c_ret.getStr() ); 109 return ret; 110 #else 111 return buf.makeStringAndClear(); 112 #endif 113 } 114 115 //================================================================================================== 116 class RTTI 117 { 118 typedef hash_map< OUString, type_info *, OUStringHash > t_rtti_map; 119 120 Mutex m_mutex; 121 t_rtti_map m_rttis; 122 t_rtti_map m_generatedRttis; 123 124 void * m_hApp; 125 126 public: 127 RTTI() SAL_THROW( () ); 128 ~RTTI() SAL_THROW( () ); 129 130 type_info * getRTTI( typelib_CompoundTypeDescription * ) SAL_THROW( () ); 131 }; 132 133 //__________________________________________________________________________________________________ 134 RTTI::RTTI() SAL_THROW( () ) 135 : m_hApp( dlopen( 0, RTLD_LAZY ) ) 136 { 137 } 138 139 //__________________________________________________________________________________________________ 140 RTTI::~RTTI() SAL_THROW( () ) 141 { 142 dlclose( m_hApp ); 143 } 144 145 //__________________________________________________________________________________________________ 146 type_info * RTTI::getRTTI( typelib_CompoundTypeDescription *pTypeDescr ) SAL_THROW( () ) 147 { 148 type_info * rtti; 149 150 OUString const & unoName = *(OUString const *)&pTypeDescr->aBase.pTypeName; 151 152 MutexGuard guard( m_mutex ); 153 154 ObservedRttiMap::const_iterator observed( observedRttis().find( unoName ) ); 155 if ( observed != observedRttis().end() ) 156 return observed->second; 157 158 t_rtti_map::const_iterator iFind( m_rttis.find( unoName ) ); 159 if (iFind == m_rttis.end()) 160 { 161 // RTTI symbol 162 OStringBuffer buf( 64 ); 163 buf.append( RTL_CONSTASCII_STRINGPARAM("_ZTIN") ); 164 sal_Int32 index = 0; 165 do 166 { 167 OUString token( unoName.getToken( 0, '.', index ) ); 168 buf.append( token.getLength() ); 169 OString c_token( OUStringToOString( token, RTL_TEXTENCODING_ASCII_US ) ); 170 buf.append( c_token ); 171 } 172 while (index >= 0); 173 buf.append( 'E' ); 174 175 OString symName( buf.makeStringAndClear() ); 176 rtti = static_cast<std::type_info *>(dlsym( RTLD_DEFAULT, symName.getStr() )); 177 178 if (rtti) 179 { 180 pair< t_rtti_map::iterator, bool > insertion( 181 m_rttis.insert( t_rtti_map::value_type( unoName, rtti ) ) ); 182 OSL_ENSURE( insertion.second, "### inserting new rtti failed?!" ); 183 } 184 else 185 { 186 // try to lookup the symbol in the generated rtti map 187 t_rtti_map::const_iterator iFind2( m_generatedRttis.find( unoName ) ); 188 if (iFind2 == m_generatedRttis.end()) 189 { 190 // we must generate it ! 191 // symbol and rtti-name is nearly identical, 192 // the symbol is prefixed with _ZTI 193 char const * rttiName = symName.getStr() +4; 194 #if OSL_DEBUG_LEVEL > 1 195 fprintf( stderr,"generated rtti for %s\n", rttiName ); 196 const OString aCUnoName = OUStringToOString( unoName, RTL_TEXTENCODING_UTF8); 197 OSL_TRACE( "TypeInfo for \"%s\" not found and cannot be generated.\n", aCUnoName.getStr()); 198 #endif 199 #ifndef AOO_BYPASS_RTTI 200 if (pTypeDescr->pBaseTypeDescription) 201 { 202 // ensure availability of base 203 type_info * base_rtti = getRTTI( 204 (typelib_CompoundTypeDescription *)pTypeDescr->pBaseTypeDescription ); 205 rtti = new __si_class_type_info( 206 strdup( rttiName ), (__class_type_info *)base_rtti ); 207 } 208 else 209 { 210 // this class has no base class 211 rtti = new __class_type_info( strdup( rttiName ) ); 212 } 213 #else 214 rtti = NULL; 215 #endif 216 bool bOK = m_generatedRttis.insert( t_rtti_map::value_type( unoName, rtti )).second; 217 OSL_ENSURE( bOK, "### inserting new generated rtti failed?!" ); 218 } 219 else // taking already generated rtti 220 { 221 rtti = iFind2->second; 222 } 223 } 224 } 225 else 226 { 227 rtti = iFind->second; 228 } 229 230 return rtti; 231 } 232 233 //-------------------------------------------------------------------------------------------------- 234 static void deleteException( void * pExc ) 235 { 236 __cxa_exception const * header = static_cast<__cxa_exception const *>(pExc) - 1; 237 /* More __cxa_exception mumbo-jumbo. See share.hxx and fillUnoException() below */ 238 if (header->exceptionDestructor != &deleteException) 239 { 240 header = reinterpret_cast<__cxa_exception const *>(reinterpret_cast<char const *>(header) - 8); 241 } 242 if( !header->exceptionType) 243 { 244 return; // NOTE: leak for now 245 } 246 typelib_TypeDescription * pTD = 0; 247 OUString unoName( toUNOname( header->exceptionType->name() ) ); 248 ::typelib_typedescription_getByName( &pTD, unoName.pData ); 249 OSL_ENSURE( pTD, "### unknown exception type! leaving out destruction => leaking!!!" ); 250 if (pTD) 251 { 252 ::uno_destructData( pExc, pTD, cpp_release ); 253 ::typelib_typedescription_release( pTD ); 254 } 255 } 256 257 //================================================================================================== 258 void raiseException( uno_Any * pUnoExc, uno_Mapping * pUno2Cpp ) 259 { 260 #if OSL_DEBUG_LEVEL > 1 261 OString cstr( 262 OUStringToOString( 263 *reinterpret_cast< OUString const * >( &pUnoExc->pType->pTypeName ), 264 RTL_TEXTENCODING_ASCII_US ) ); 265 fprintf( stderr, "> uno exception occurred: %s\n", cstr.getStr() ); 266 #endif 267 void * pCppExc; 268 type_info * rtti; 269 270 { 271 // construct cpp exception object 272 typelib_TypeDescription * pTypeDescr = 0; 273 TYPELIB_DANGER_GET( &pTypeDescr, pUnoExc->pType ); 274 OSL_ASSERT( pTypeDescr ); 275 if (! pTypeDescr) 276 { 277 throw RuntimeException( 278 OUString( RTL_CONSTASCII_USTRINGPARAM("cannot get typedescription for type ") ) + 279 *reinterpret_cast< OUString const * >( &pUnoExc->pType->pTypeName ), 280 Reference< XInterface >() ); 281 } 282 283 pCppExc = __cxa_allocate_exception( pTypeDescr->nSize ); 284 ::uno_copyAndConvertData( pCppExc, pUnoExc->pData, pTypeDescr, pUno2Cpp ); 285 286 // destruct uno exception 287 ::uno_any_destruct( pUnoExc, 0 ); 288 // avoiding locked counts 289 static RTTI * s_rtti = 0; 290 if (! s_rtti) 291 { 292 MutexGuard guard( Mutex::getGlobalMutex() ); 293 if (! s_rtti) 294 { 295 #ifdef LEAK_STATIC_DATA 296 s_rtti = new RTTI(); 297 #else 298 static RTTI rtti_data; 299 s_rtti = &rtti_data; 300 #endif 301 } 302 } 303 rtti = (type_info *)s_rtti->getRTTI( (typelib_CompoundTypeDescription *) pTypeDescr ); 304 TYPELIB_DANGER_RELEASE( pTypeDescr ); 305 OSL_ENSURE( rtti, "### no rtti for throwing exception!" ); 306 if (! rtti) 307 { 308 throw RuntimeException( 309 OUString( RTL_CONSTASCII_USTRINGPARAM("no rtti for type ") ) + 310 *reinterpret_cast< OUString const * >( &pUnoExc->pType->pTypeName ), 311 Reference< XInterface >() ); 312 } 313 } 314 315 __cxa_throw( pCppExc, rtti, deleteException ); 316 } 317 318 //================================================================================================== 319 void fillUnoException( __cxa_exception * header, uno_Any * pUnoExc, uno_Mapping * pCpp2Uno ) 320 { 321 if (! header) 322 { 323 RuntimeException aRE( 324 OUString( RTL_CONSTASCII_USTRINGPARAM("no exception header!") ), 325 Reference< XInterface >() ); 326 Type const & rType = ::getCppuType( &aRE ); 327 uno_type_any_constructAndConvert( pUnoExc, &aRE, rType.getTypeLibType(), pCpp2Uno ); 328 #if OSL_DEBUG_LEVEL > 0 329 OString cstr( OUStringToOString( aRE.Message, RTL_TEXTENCODING_ASCII_US ) ); 330 OSL_ENSURE( 0, cstr.getStr() ); 331 #endif 332 return; 333 } 334 335 /* 336 * Handle the case where we are built on llvm 10 (or later) but are running 337 * on an earlier version (eg, community builds). In this situation the 338 * reserved ptr doesn't exist in the struct returned and so the offsets 339 * that header uses are wrong. This assumes that reserved isn't used 340 * and that referenceCount is always >0 in the cases we handle. 341 * See share.hxx for the definition of __cxa_exception 342 */ 343 if (*reinterpret_cast<void **>(header) == 0) 344 { 345 header = reinterpret_cast<__cxa_exception *>(reinterpret_cast<char *>(header) + 8); 346 } 347 348 typelib_TypeDescription * pExcTypeDescr = 0; 349 OUString unoName( toUNOname( header->exceptionType->name() ) ); 350 #if OSL_DEBUG_LEVEL > 1 351 OString cstr_unoName( OUStringToOString( unoName, RTL_TEXTENCODING_ASCII_US ) ); 352 fprintf( stderr, "> c++ exception occurred: %s\n", cstr_unoName.getStr() ); 353 #endif 354 typelib_typedescription_getByName( &pExcTypeDescr, unoName.pData ); 355 if (0 == pExcTypeDescr) 356 { 357 RuntimeException aRE( 358 OUString( RTL_CONSTASCII_USTRINGPARAM("exception type not found: ") ) + unoName, 359 Reference< XInterface >() ); 360 Type const & rType = ::getCppuType( &aRE ); 361 uno_type_any_constructAndConvert( pUnoExc, &aRE, rType.getTypeLibType(), pCpp2Uno ); 362 #if OSL_DEBUG_LEVEL > 0 363 OString cstr( OUStringToOString( aRE.Message, RTL_TEXTENCODING_ASCII_US ) ); 364 OSL_ENSURE( 0, cstr.getStr() ); 365 #endif 366 } 367 else 368 { 369 // construct uno exception any 370 uno_any_constructAndConvert( pUnoExc, header->adjustedPtr, pExcTypeDescr, pCpp2Uno ); 371 typelib_typedescription_release( pExcTypeDescr ); 372 } 373 } 374 375 void fillUnoException( 376 std::type_info const & type, void * exception, uno_Any * pUnoExc, 377 uno_Mapping * pCpp2Uno ) 378 { 379 typelib_TypeDescription * pExcTypeDescr = 0; 380 OUString unoName( toUNOname( type.name() ) ); 381 { 382 MutexGuard guard( Mutex::getGlobalMutex() ); 383 observedRttis()[unoName] = const_cast<std::type_info *>( &type ); 384 } 385 typelib_typedescription_getByName( &pExcTypeDescr, unoName.pData ); 386 if ( pExcTypeDescr == 0 ) 387 { 388 RuntimeException aRE( 389 OUString( RTL_CONSTASCII_USTRINGPARAM("exception type not found: ") ) + unoName, 390 Reference< XInterface >() ); 391 Type const & rType = ::getCppuType( &aRE ); 392 uno_type_any_constructAndConvert( 393 pUnoExc, &aRE, rType.getTypeLibType(), pCpp2Uno ); 394 } 395 else 396 { 397 uno_any_constructAndConvert( 398 pUnoExc, exception, pExcTypeDescr, pCpp2Uno ); 399 typelib_typedescription_release( pExcTypeDescr ); 400 } 401 } 402 403 } 404