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