Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include <sal/config.h>
21 :
22 : #include <cassert>
23 : #include <memory>
24 :
25 : #include "jni_bridge.h"
26 : #include "jniunoenvironmentdata.hxx"
27 :
28 : #include "jvmaccess/unovirtualmachine.hxx"
29 : #include "rtl/ref.hxx"
30 : #include "rtl/strbuf.hxx"
31 : #include "uno/lbnames.h"
32 :
33 : using namespace ::osl;
34 : using namespace ::jni_uno;
35 :
36 : namespace
37 : {
38 : extern "C"
39 : {
40 :
41 :
42 6 : void SAL_CALL Mapping_acquire( uno_Mapping * mapping )
43 : SAL_THROW_EXTERN_C()
44 : {
45 6 : Mapping const * that = static_cast< Mapping const * >( mapping );
46 6 : that->m_bridge->acquire();
47 6 : }
48 :
49 :
50 9 : void SAL_CALL Mapping_release( uno_Mapping * mapping )
51 : SAL_THROW_EXTERN_C()
52 : {
53 9 : Mapping const * that = static_cast< Mapping const * >( mapping );
54 9 : that->m_bridge->release();
55 9 : }
56 :
57 :
58 3 : void SAL_CALL Mapping_map_to_uno(
59 : uno_Mapping * mapping, void ** ppOut,
60 : void * pIn, typelib_InterfaceTypeDescription * td )
61 : SAL_THROW_EXTERN_C()
62 : {
63 3 : uno_Interface ** ppUnoI = reinterpret_cast<uno_Interface **>(ppOut);
64 3 : jobject javaI = static_cast<jobject>(pIn);
65 :
66 : static_assert(sizeof (void *) == sizeof (jobject), "must be the same size");
67 : assert(ppUnoI != 0);
68 : assert(td != 0);
69 :
70 3 : if (0 == javaI)
71 : {
72 0 : if (0 != *ppUnoI)
73 : {
74 0 : uno_Interface * p = *ppUnoI;
75 0 : (*p->release)( p );
76 0 : *ppUnoI = 0;
77 : }
78 : }
79 : else
80 : {
81 : try
82 : {
83 : Bridge const * bridge =
84 3 : static_cast< Mapping const * >( mapping )->m_bridge;
85 : JNI_guarded_context jni(
86 : bridge->getJniInfo(),
87 : (static_cast<jni_uno::JniUnoEnvironmentData *>(
88 : bridge->m_java_env->pContext)
89 3 : ->machine));
90 :
91 : JNI_interface_type_info const * info =
92 : static_cast< JNI_interface_type_info const * >(
93 : bridge->getJniInfo()->get_type_info(
94 3 : jni, &td->aBase ) );
95 3 : uno_Interface * pUnoI = bridge->map_to_uno( jni, javaI, info );
96 3 : if (0 != *ppUnoI)
97 : {
98 0 : uno_Interface * p = *ppUnoI;
99 0 : (*p->release)( p );
100 : }
101 3 : *ppUnoI = pUnoI;
102 : }
103 0 : catch (const BridgeRuntimeError & err)
104 : {
105 : SAL_WARN(
106 : "bridges",
107 : "ingoring BridgeRuntimeError \"" << err.m_message << "\"");
108 : }
109 0 : catch (const ::jvmaccess::VirtualMachine::AttachGuard::CreationException &)
110 : {
111 : SAL_WARN("bridges", "attaching current thread to java failed");
112 : }
113 : }
114 3 : }
115 :
116 :
117 0 : void SAL_CALL Mapping_map_to_java(
118 : uno_Mapping * mapping, void ** ppOut,
119 : void * pIn, typelib_InterfaceTypeDescription * td )
120 : SAL_THROW_EXTERN_C()
121 : {
122 0 : jobject * ppJavaI = reinterpret_cast<jobject *>(ppOut);
123 0 : uno_Interface * pUnoI = static_cast<uno_Interface *>(pIn);
124 :
125 : static_assert(sizeof (void *) == sizeof (jobject), "must be the same size");
126 : assert(ppJavaI != 0);
127 : assert(td != 0);
128 :
129 : try
130 : {
131 0 : if (0 == pUnoI)
132 : {
133 0 : if (0 != *ppJavaI)
134 : {
135 : Bridge const * bridge =
136 0 : static_cast< Mapping const * >( mapping )->m_bridge;
137 : JNI_guarded_context jni(
138 : bridge->getJniInfo(),
139 : (static_cast<jni_uno::JniUnoEnvironmentData *>(
140 : bridge->m_java_env->pContext)
141 0 : ->machine));
142 0 : jni->DeleteGlobalRef( *ppJavaI );
143 0 : *ppJavaI = 0;
144 : }
145 : }
146 : else
147 : {
148 : Bridge const * bridge =
149 0 : static_cast< Mapping const * >( mapping )->m_bridge;
150 : JNI_guarded_context jni(
151 : bridge->getJniInfo(),
152 : (static_cast<jni_uno::JniUnoEnvironmentData *>(
153 : bridge->m_java_env->pContext)
154 0 : ->machine));
155 :
156 : JNI_interface_type_info const * info =
157 : static_cast< JNI_interface_type_info const * >(
158 : bridge->getJniInfo()->get_type_info(
159 0 : jni, &td->aBase ) );
160 0 : jobject jlocal = bridge->map_to_java( jni, pUnoI, info );
161 0 : if (0 != *ppJavaI)
162 0 : jni->DeleteGlobalRef( *ppJavaI );
163 0 : *ppJavaI = jni->NewGlobalRef( jlocal );
164 0 : jni->DeleteLocalRef( jlocal );
165 : }
166 : }
167 0 : catch (const BridgeRuntimeError & err)
168 : {
169 : SAL_WARN(
170 : "bridges",
171 : "ingoring BridgeRuntimeError \"" << err.m_message << "\"");
172 : }
173 0 : catch (const ::jvmaccess::VirtualMachine::AttachGuard::CreationException &)
174 : {
175 : SAL_WARN("bridges", "attaching current thread to java failed");
176 : }
177 0 : }
178 :
179 :
180 0 : void SAL_CALL Bridge_free( uno_Mapping * mapping )
181 : SAL_THROW_EXTERN_C()
182 : {
183 0 : Mapping * that = static_cast< Mapping * >( mapping );
184 0 : delete that->m_bridge;
185 0 : }
186 :
187 : }
188 :
189 : }
190 :
191 : namespace jni_uno
192 : {
193 :
194 :
195 70 : void Bridge::acquire() const
196 : {
197 70 : if (1 == osl_atomic_increment( &m_ref ))
198 : {
199 0 : if (m_registered_java2uno)
200 : {
201 0 : uno_Mapping * mapping = const_cast< Mapping * >( &m_java2uno );
202 : uno_registerMapping(
203 : &mapping, Bridge_free,
204 0 : m_java_env, &m_uno_env->aBase, 0 );
205 : }
206 : else
207 : {
208 0 : uno_Mapping * mapping = const_cast< Mapping * >( &m_uno2java );
209 : uno_registerMapping(
210 : &mapping, Bridge_free,
211 0 : &m_uno_env->aBase, m_java_env, 0 );
212 : }
213 : }
214 70 : }
215 :
216 :
217 39 : void Bridge::release() const
218 : {
219 39 : if (! osl_atomic_decrement( &m_ref ))
220 : {
221 : uno_revokeMapping(
222 : m_registered_java2uno
223 : ? const_cast< Mapping * >( &m_java2uno )
224 0 : : const_cast< Mapping * >( &m_uno2java ) );
225 : }
226 39 : }
227 :
228 :
229 3 : Bridge::Bridge(
230 : uno_Environment * java_env, uno_ExtEnvironment * uno_env,
231 : bool registered_java2uno )
232 : : m_ref( 1 ),
233 : m_uno_env( uno_env ),
234 : m_java_env( java_env ),
235 3 : m_registered_java2uno( registered_java2uno )
236 : {
237 : assert(m_java_env != 0);
238 : assert(m_uno_env != 0);
239 :
240 : // uno_initEnvironment (below) cannot report errors directly, so it clears
241 : // its pContext upon error to indirectly report errors from here:
242 3 : if (static_cast<jni_uno::JniUnoEnvironmentData *>(m_java_env->pContext)
243 3 : == nullptr)
244 : {
245 0 : throw BridgeRuntimeError("error during JNI-UNO's uno_initEnvironment");
246 : }
247 :
248 3 : (*m_uno_env->aBase.acquire)( &m_uno_env->aBase );
249 3 : (*m_java_env->acquire)( m_java_env );
250 :
251 : // java2uno
252 3 : m_java2uno.acquire = Mapping_acquire;
253 3 : m_java2uno.release = Mapping_release;
254 3 : m_java2uno.mapInterface = Mapping_map_to_uno;
255 3 : m_java2uno.m_bridge = this;
256 : // uno2java
257 3 : m_uno2java.acquire = Mapping_acquire;
258 3 : m_uno2java.release = Mapping_release;
259 3 : m_uno2java.mapInterface = Mapping_map_to_java;
260 3 : m_uno2java.m_bridge = this;
261 3 : }
262 :
263 :
264 0 : Bridge::~Bridge()
265 : {
266 0 : (*m_java_env->release)( m_java_env );
267 0 : (*m_uno_env->aBase.release)( &m_uno_env->aBase );
268 0 : }
269 :
270 405 : JNI_info const * Bridge::getJniInfo() const {
271 : return static_cast<jni_uno::JniUnoEnvironmentData *>(m_java_env->pContext)
272 405 : ->info;
273 : }
274 :
275 0 : void JNI_context::java_exc_occurred() const
276 : {
277 : // !don't rely on JNI_info!
278 :
279 0 : JLocalAutoRef jo_exc( *this, m_env->ExceptionOccurred() );
280 0 : m_env->ExceptionClear();
281 : assert(jo_exc.is());
282 0 : if (! jo_exc.is())
283 : {
284 : throw BridgeRuntimeError(
285 0 : "java exception occurred, but not available!?" +
286 0 : get_stack_trace() );
287 : }
288 :
289 : // call toString(); don't rely on m_jni_info
290 0 : jclass jo_class = m_env->FindClass( "java/lang/Object" );
291 0 : if (m_env->ExceptionCheck())
292 : {
293 0 : m_env->ExceptionClear();
294 : throw BridgeRuntimeError(
295 0 : "cannot get class java.lang.Object!" + get_stack_trace() );
296 : }
297 0 : JLocalAutoRef jo_Object( *this, jo_class );
298 : // method Object.toString()
299 : jmethodID method_Object_toString = m_env->GetMethodID(
300 0 : static_cast<jclass>(jo_Object.get()), "toString", "()Ljava/lang/String;" );
301 0 : if (m_env->ExceptionCheck())
302 : {
303 0 : m_env->ExceptionClear();
304 : throw BridgeRuntimeError(
305 0 : "cannot get method id of java.lang.Object.toString()!" +
306 0 : get_stack_trace() );
307 : }
308 : assert(method_Object_toString != 0);
309 :
310 : JLocalAutoRef jo_descr(
311 : *this, m_env->CallObjectMethodA(
312 0 : jo_exc.get(), method_Object_toString, 0 ) );
313 0 : if (m_env->ExceptionCheck()) // no chance at all
314 : {
315 0 : m_env->ExceptionClear();
316 : throw BridgeRuntimeError(
317 0 : "error examining java exception object!" +
318 0 : get_stack_trace() );
319 : }
320 :
321 0 : jsize len = m_env->GetStringLength( static_cast<jstring>(jo_descr.get()) );
322 : std::unique_ptr< rtl_mem > ustr_mem(
323 : rtl_mem::allocate(
324 0 : sizeof (rtl_uString) + (len * sizeof (sal_Unicode)) ) );
325 0 : rtl_uString * ustr = reinterpret_cast<rtl_uString *>(ustr_mem.get());
326 0 : m_env->GetStringRegion( static_cast<jstring>(jo_descr.get()), 0, len, ustr->buffer );
327 0 : if (m_env->ExceptionCheck())
328 : {
329 0 : m_env->ExceptionClear();
330 : throw BridgeRuntimeError(
331 0 : "invalid java string object!" + get_stack_trace() );
332 : }
333 0 : ustr->refCount = 1;
334 0 : ustr->length = len;
335 0 : ustr->buffer[ len ] = '\0';
336 0 : OUString message( reinterpret_cast<rtl_uString *>(ustr_mem.release()), SAL_NO_ACQUIRE );
337 :
338 0 : throw BridgeRuntimeError( message + get_stack_trace( jo_exc.get() ) );
339 : }
340 :
341 :
342 3 : void JNI_context::getClassForName(
343 : jclass * classClass, jmethodID * methodForName) const
344 : {
345 3 : jclass c = m_env->FindClass("java/lang/Class");
346 3 : if (c != 0) {
347 : *methodForName = m_env->GetStaticMethodID(
348 : c, "forName",
349 3 : "(Ljava/lang/String;ZLjava/lang/ClassLoader;)Ljava/lang/Class;");
350 : }
351 3 : *classClass = c;
352 3 : }
353 :
354 :
355 109 : jclass JNI_context::findClass(
356 : char const * name, jclass classClass, jmethodID methodForName,
357 : bool inException) const
358 : {
359 109 : jclass c = 0;
360 109 : JLocalAutoRef s(*this, m_env->NewStringUTF(name));
361 109 : if (s.is()) {
362 : jvalue a[3];
363 109 : a[0].l = s.get();
364 109 : a[1].z = JNI_FALSE;
365 109 : a[2].l = m_class_loader;
366 : c = static_cast< jclass >(
367 109 : m_env->CallStaticObjectMethodA(classClass, methodForName, a));
368 : }
369 109 : if (!inException) {
370 109 : ensure_no_exception();
371 : }
372 109 : return c;
373 : }
374 :
375 :
376 0 : OUString JNI_context::get_stack_trace( jobject jo_exc ) const
377 : {
378 : JLocalAutoRef jo_JNI_proxy(
379 : *this,
380 0 : find_class( *this, "com.sun.star.bridges.jni_uno.JNI_proxy", true ) );
381 0 : if (assert_no_exception())
382 : {
383 : // static method JNI_proxy.get_stack_trace()
384 : jmethodID method = m_env->GetStaticMethodID(
385 0 : static_cast<jclass>(jo_JNI_proxy.get()), "get_stack_trace",
386 0 : "(Ljava/lang/Throwable;)Ljava/lang/String;" );
387 0 : if (assert_no_exception() && (0 != method))
388 : {
389 : jvalue arg;
390 0 : arg.l = jo_exc;
391 : JLocalAutoRef jo_stack_trace(
392 : *this, m_env->CallStaticObjectMethodA(
393 0 : static_cast<jclass>(jo_JNI_proxy.get()), method, &arg ) );
394 0 : if (assert_no_exception())
395 : {
396 : jsize len =
397 0 : m_env->GetStringLength( static_cast<jstring>(jo_stack_trace.get()) );
398 : std::unique_ptr< rtl_mem > ustr_mem(
399 : rtl_mem::allocate(
400 0 : sizeof (rtl_uString) + (len * sizeof (sal_Unicode)) ) );
401 0 : rtl_uString * ustr = reinterpret_cast<rtl_uString *>(ustr_mem.get());
402 : m_env->GetStringRegion(
403 0 : static_cast<jstring>(jo_stack_trace.get()), 0, len, ustr->buffer );
404 0 : if (assert_no_exception())
405 : {
406 0 : ustr->refCount = 1;
407 0 : ustr->length = len;
408 0 : ustr->buffer[ len ] = '\0';
409 : return OUString(
410 0 : reinterpret_cast<rtl_uString *>(ustr_mem.release()), SAL_NO_ACQUIRE );
411 0 : }
412 0 : }
413 : }
414 : }
415 0 : return OUString();
416 : }
417 :
418 : }
419 :
420 : using namespace ::jni_uno;
421 :
422 : extern "C" {
423 :
424 3 : void SAL_CALL java_env_dispose(uno_Environment * env) {
425 : auto * envData
426 3 : = static_cast<jni_uno::JniUnoEnvironmentData *>(env->pContext);
427 3 : if (envData != nullptr) {
428 : jobject async;
429 : {
430 3 : osl::MutexGuard g(envData->mutex);
431 3 : async = envData->asynchronousFinalizer;
432 3 : envData->asynchronousFinalizer = nullptr;
433 : }
434 3 : if (async != nullptr) {
435 : try {
436 3 : JNI_guarded_context jni(envData->info, envData->machine);
437 : jni->CallObjectMethodA(
438 : async, envData->info->m_method_AsynchronousFinalizer_drain,
439 3 : nullptr);
440 3 : jni.ensure_no_exception();
441 3 : jni->DeleteGlobalRef(async);
442 0 : } catch (const BridgeRuntimeError & e) {
443 : SAL_WARN(
444 : "bridges",
445 : "ignoring BridgeRuntimeError \"" << e.m_message << "\"");
446 : } catch (
447 0 : jvmaccess::VirtualMachine::AttachGuard::CreationException &)
448 : {
449 : SAL_WARN(
450 : "bridges",
451 : ("ignoring jvmaccess::VirtualMachine::AttachGuard"
452 : "::CreationException"));
453 : }
454 : }
455 : }
456 3 : }
457 :
458 0 : void SAL_CALL java_env_disposing(uno_Environment * env) {
459 0 : java_env_dispose(env);
460 0 : delete static_cast<jni_uno::JniUnoEnvironmentData *>(env->pContext);
461 0 : }
462 :
463 : #ifdef DISABLE_DYNLOADING
464 : #define uno_initEnvironment java_uno_initEnvironment
465 : #endif
466 :
467 :
468 3 : SAL_DLLPUBLIC_EXPORT void SAL_CALL uno_initEnvironment( uno_Environment * java_env )
469 : SAL_THROW_EXTERN_C()
470 : {
471 : try {
472 : // JavaComponentLoader::getJavaLoader (in
473 : // stoc/source/javaloader/javaloader.cxx) stores a
474 : // jvmaccess::UnoVirtualMachine pointer into java_env->pContext; replace
475 : // it here with either a pointer to a full JniUnoEnvironmentData upon
476 : // success, or with a null pointer upon failure (as this function cannot
477 : // directly report back failure, so it uses that way to indirectly
478 : // report failure later from within the Bridge ctor):
479 : rtl::Reference<jvmaccess::UnoVirtualMachine> vm(
480 3 : static_cast<jvmaccess::UnoVirtualMachine *>(java_env->pContext));
481 3 : java_env->pContext = nullptr;
482 3 : java_env->dispose = java_env_dispose;
483 3 : java_env->environmentDisposing = java_env_disposing;
484 3 : java_env->pExtEnv = 0; // no extended support
485 : std::unique_ptr<jni_uno::JniUnoEnvironmentData> envData(
486 6 : new jni_uno::JniUnoEnvironmentData(vm));
487 : {
488 3 : JNI_guarded_context jni(envData->info, envData->machine);
489 : JLocalAutoRef ref(
490 : jni,
491 : jni->NewObject(
492 3 : envData->info->m_class_AsynchronousFinalizer,
493 9 : envData->info->m_ctor_AsynchronousFinalizer));
494 3 : jni.ensure_no_exception();
495 3 : envData->asynchronousFinalizer = jni->NewGlobalRef(ref.get());
496 6 : jni.ensure_no_exception();
497 : }
498 6 : java_env->pContext = envData.release();
499 0 : } catch (const BridgeRuntimeError & e) {
500 : SAL_WARN("bridges", "BridgeRuntimeError \"" << e.m_message << "\"");
501 0 : } catch (jvmaccess::VirtualMachine::AttachGuard::CreationException &) {
502 : SAL_WARN(
503 : "bridges",
504 : "jvmaccess::VirtualMachine::AttachGuard::CreationException");
505 : }
506 3 : }
507 :
508 : #ifdef DISABLE_DYNLOADING
509 : #define uno_ext_getMapping java_uno_ext_getMapping
510 : #endif
511 :
512 :
513 3 : SAL_DLLPUBLIC_EXPORT void SAL_CALL uno_ext_getMapping(
514 : uno_Mapping ** ppMapping, uno_Environment * pFrom, uno_Environment * pTo )
515 : SAL_THROW_EXTERN_C()
516 : {
517 : assert(ppMapping != 0);
518 : assert(pFrom != 0);
519 : assert(pTo != 0);
520 3 : if (0 != *ppMapping)
521 : {
522 0 : (*(*ppMapping)->release)( *ppMapping );
523 0 : *ppMapping = 0;
524 : }
525 :
526 : static_assert(int(JNI_FALSE) == int(sal_False), "must be equal");
527 : static_assert(int(JNI_TRUE) == int(sal_True), "must be equal");
528 : static_assert(sizeof (jboolean) == sizeof (sal_Bool), "must be the same size");
529 : static_assert(sizeof (jchar) == sizeof (sal_Unicode), "must be the same size");
530 : static_assert(sizeof (jdouble) == sizeof (double), "must be the same size");
531 : static_assert(sizeof (jfloat) == sizeof (float), "must be the same size");
532 : static_assert(sizeof (jbyte) == sizeof (sal_Int8), "must be the same size");
533 : static_assert(sizeof (jshort) == sizeof (sal_Int16), "must be the same size");
534 : static_assert(sizeof (jint) == sizeof (sal_Int32), "must be the same size");
535 : static_assert(sizeof (jlong) == sizeof (sal_Int64), "must be the same size");
536 :
537 : OUString const & from_env_typename =
538 3 : OUString::unacquired( &pFrom->pTypeName );
539 : OUString const & to_env_typename =
540 3 : OUString::unacquired( &pTo->pTypeName );
541 :
542 3 : uno_Mapping * mapping = 0;
543 :
544 : try
545 : {
546 3 : if ( from_env_typename == UNO_LB_JAVA && to_env_typename == UNO_LB_UNO )
547 : {
548 : Bridge * bridge =
549 3 : new Bridge( pFrom, pTo->pExtEnv, true ); // ref count = 1
550 3 : mapping = &bridge->m_java2uno;
551 : uno_registerMapping(
552 : &mapping, Bridge_free,
553 3 : pFrom, &pTo->pExtEnv->aBase, 0 );
554 : // coverity[leaked_storage]
555 : }
556 0 : else if ( from_env_typename == UNO_LB_UNO && to_env_typename == UNO_LB_JAVA )
557 : {
558 : Bridge * bridge =
559 0 : new Bridge( pTo, pFrom->pExtEnv, false ); // ref count = 1
560 0 : mapping = &bridge->m_uno2java;
561 : uno_registerMapping(
562 : &mapping, Bridge_free,
563 0 : &pFrom->pExtEnv->aBase, pTo, 0 );
564 : // coverity[leaked_storage]
565 : }
566 : }
567 0 : catch (const BridgeRuntimeError & err)
568 : {
569 : SAL_WARN("bridges", "BridgeRuntimeError \"" << err.m_message << "\"");
570 : }
571 :
572 3 : *ppMapping = mapping;
573 3 : }
574 :
575 : }
576 :
577 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|