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