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