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