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 :
21 : #ifdef WNT
22 : # include <stdio.h>
23 : # include <sys/stat.h>
24 : # include <windows.h>
25 : #endif
26 :
27 : #ifdef ANDROID
28 : # include <dlfcn.h>
29 : #endif
30 :
31 : #if OSL_DEBUG_LEVEL > 0
32 : #include <stdio.h>
33 : #endif
34 : #include <string.h>
35 :
36 : #include "boost/scoped_array.hpp"
37 : #include "osl/diagnose.h"
38 : #include "rtl/ustring.hxx"
39 : #include "rtl/ustrbuf.hxx"
40 : #include "osl/module.hxx"
41 : #include "osl/mutex.hxx"
42 : #include "osl/thread.hxx"
43 : #include "osl/file.hxx"
44 : #include "rtl/instance.hxx"
45 : #include "osl/getglobalmutex.hxx"
46 : #include <setjmp.h>
47 : #include <signal.h>
48 : #include <stack>
49 :
50 : #include "jni.h"
51 : #include "rtl/byteseq.hxx"
52 : #include "jvmfwk/vendorplugin.h"
53 : #include "util.hxx"
54 : #include "sunversion.hxx"
55 : #include "vendorlist.hxx"
56 : #include "diagnostics.h"
57 :
58 : #ifdef ANDROID
59 : #include <osl/detail/android-bootstrap.h>
60 : #else
61 : #if defined HAVE_VALGRIND_HEADERS
62 : #include <valgrind/valgrind.h>
63 : #else
64 : #define RUNNING_ON_VALGRIND 0
65 : #endif
66 : #endif
67 :
68 : using namespace osl;
69 : using namespace std;
70 : using namespace jfw_plugin;
71 :
72 :
73 : namespace {
74 :
75 : struct PluginMutex: public ::rtl::Static<osl::Mutex, PluginMutex> {};
76 :
77 : #if defined UNX
78 2 : OString getPluginJarPath(
79 : const OUString & sVendor,
80 : const OUString& sLocation,
81 : const OUString& sVersion)
82 : {
83 2 : OString ret;
84 4 : OUString sName1("javaplugin.jar");
85 4 : OUString sName2("plugin.jar");
86 4 : OUString sPath;
87 2 : if ( sVendor == "Sun Microsystems Inc." )
88 : {
89 0 : SunVersion ver142("1.4.2-ea");
90 0 : SunVersion ver150("1.5.0-ea");
91 0 : SunVersion ver(sVersion);
92 : OSL_ASSERT(ver142 && ver150 && ver);
93 :
94 0 : OUString sName;
95 0 : if (ver < ver142)
96 : {
97 0 : sName = sName1;
98 : }
99 0 : else if (ver < ver150)
100 : {//this will cause ea, beta etc. to have plugin.jar in path.
101 : //but this does not harm. 1.5.0-beta < 1.5.0
102 0 : sName = sName2;
103 : }
104 0 : if (!sName.isEmpty())
105 : {
106 0 : sName = sLocation + "/lib/" + sName;
107 0 : OSL_VERIFY(
108 : osl_getSystemPathFromFileURL(sName.pData, & sPath.pData)
109 : == osl_File_E_None);
110 0 : }
111 : }
112 : else
113 : {
114 2 : char sep[] = {SAL_PATHSEPARATOR, 0};
115 2 : OUString sName(sLocation + "/lib/" + sName1);
116 4 : OUString sPath1;
117 4 : OUString sPath2;
118 2 : if (osl_getSystemPathFromFileURL(sName.pData, & sPath1.pData)
119 : == osl_File_E_None)
120 : {
121 2 : sName = sLocation + "/lib/" + sName2;
122 2 : if (osl_getSystemPathFromFileURL(sName.pData, & sPath2.pData)
123 : == osl_File_E_None)
124 : {
125 2 : sPath = sPath1 + OUString::createFromAscii(sep) + sPath2;
126 : }
127 : }
128 2 : OSL_ASSERT(!sPath.isEmpty());
129 : }
130 2 : ret = OUStringToOString(sPath, osl_getThreadTextEncoding());
131 :
132 4 : return ret;
133 : }
134 : #endif // UNX
135 :
136 :
137 105 : JavaInfo* createJavaInfo(const rtl::Reference<VendorBase> & info)
138 : {
139 105 : JavaInfo* pInfo = (JavaInfo*) rtl_allocateMemory(sizeof(JavaInfo));
140 105 : if (pInfo == NULL)
141 0 : return NULL;
142 105 : OUString sVendor = info->getVendor();
143 105 : pInfo->sVendor = sVendor.pData;
144 105 : rtl_uString_acquire(sVendor.pData);
145 210 : OUString sHome = info->getHome();
146 105 : pInfo->sLocation = sHome.pData;
147 105 : rtl_uString_acquire(pInfo->sLocation);
148 210 : OUString sVersion = info->getVersion();
149 105 : pInfo->sVersion = sVersion.pData;
150 105 : rtl_uString_acquire(pInfo->sVersion);
151 105 : pInfo->nFeatures = info->supportsAccessibility() ? 1 : 0;
152 105 : pInfo->nRequirements = info->needsRestart() ? JFW_REQUIRE_NEEDRESTART : 0;
153 210 : OUStringBuffer buf(1024);
154 105 : buf.append(info->getRuntimeLibrary());
155 105 : if (!info->getLibraryPaths().isEmpty())
156 : {
157 105 : buf.appendAscii("\n");
158 105 : buf.append(info->getLibraryPaths());
159 105 : buf.appendAscii("\n");
160 : }
161 :
162 210 : OUString sVendorData = buf.makeStringAndClear();
163 : rtl::ByteSequence byteSeq( (sal_Int8*) sVendorData.pData->buffer,
164 210 : sVendorData.getLength() * sizeof(sal_Unicode));
165 105 : pInfo->arVendorData = byteSeq.get();
166 105 : rtl_byte_sequence_acquire(pInfo->arVendorData);
167 :
168 210 : return pInfo;
169 : }
170 :
171 2 : OUString getRuntimeLib(const rtl::ByteSequence & data)
172 : {
173 2 : const sal_Unicode* chars = (sal_Unicode*) data.getConstArray();
174 2 : sal_Int32 len = data.getLength();
175 2 : OUString sData(chars, len / 2);
176 : //the runtime lib is on the first line
177 2 : sal_Int32 index = 0;
178 2 : OUString aToken = sData.getToken( 0, '\n', index);
179 :
180 2 : return aToken;
181 : }
182 :
183 : jmp_buf jmp_jvm_abort;
184 : sig_atomic_t g_bInGetJavaVM = 0;
185 :
186 0 : extern "C" void JNICALL abort_handler()
187 : {
188 : // If we are within JNI_CreateJavaVM then we jump back into getJavaVM
189 0 : if( g_bInGetJavaVM != 0 )
190 : {
191 0 : fprintf( stderr, "JavaVM: JNI_CreateJavaVM called _exit, caught by abort_handler in javavm.cxx\n");
192 0 : longjmp( jmp_jvm_abort, 0);
193 : }
194 0 : }
195 :
196 : }
197 :
198 : extern "C"
199 102 : javaPluginError jfw_plugin_getAllJavaInfos(
200 : rtl_uString *sVendor,
201 : rtl_uString *sMinVersion,
202 : rtl_uString *sMaxVersion,
203 : rtl_uString * *arExcludeList,
204 : sal_Int32 nLenList,
205 : JavaInfo*** parJavaInfo,
206 : sal_Int32 *nLenInfoList)
207 : {
208 : OSL_ASSERT(sVendor);
209 : OSL_ASSERT(sMinVersion);
210 : OSL_ASSERT(sMaxVersion);
211 : OSL_ASSERT(parJavaInfo);
212 : OSL_ASSERT(parJavaInfo);
213 : OSL_ASSERT(nLenInfoList);
214 102 : if (!sVendor || !sMinVersion || !sMaxVersion || !parJavaInfo || !nLenInfoList)
215 0 : return JFW_PLUGIN_E_INVALID_ARG;
216 :
217 : //nLenlist contains the number of element in arExcludeList.
218 : //If no exclude list is provided then nLenList must be 0
219 : OSL_ASSERT( ! (arExcludeList == NULL && nLenList > 0));
220 102 : if (arExcludeList == NULL && nLenList > 0)
221 0 : return JFW_PLUGIN_E_INVALID_ARG;
222 :
223 102 : OUString ouVendor(sVendor);
224 204 : OUString ouMinVer(sMinVersion);
225 204 : OUString ouMaxVer(sMaxVersion);
226 :
227 : OSL_ASSERT(!ouVendor.isEmpty());
228 102 : if (ouVendor.isEmpty())
229 0 : return JFW_PLUGIN_E_INVALID_ARG;
230 :
231 102 : JavaInfo** arInfo = NULL;
232 :
233 : //Find all JREs
234 : vector<rtl::Reference<VendorBase> > vecInfos =
235 204 : getAllJREInfos();
236 204 : vector<rtl::Reference<VendorBase> > vecVerifiedInfos;
237 :
238 : typedef vector<rtl::Reference<VendorBase> >::iterator it;
239 204 : for (it i= vecInfos.begin(); i != vecInfos.end(); ++i)
240 : {
241 102 : const rtl::Reference<VendorBase>& cur = *i;
242 :
243 102 : if (ouVendor.equals(cur->getVendor()) == sal_False)
244 51 : continue;
245 :
246 51 : if (!ouMinVer.isEmpty())
247 : {
248 : try
249 : {
250 51 : if (cur->compareVersions(sMinVersion) == -1)
251 0 : continue;
252 : }
253 0 : catch (MalformedVersionException&)
254 : {
255 : //The minVersion was not recognized as valid for this vendor.
256 : JFW_ENSURE(
257 : 0,"[Java framework]sunjavaplugin does not know version: "
258 : + ouMinVer + " for vendor: " + cur->getVendor()
259 : + " .Check minimum Version." );
260 0 : return JFW_PLUGIN_E_WRONG_VERSION_FORMAT;
261 : }
262 : }
263 :
264 51 : if (!ouMaxVer.isEmpty())
265 : {
266 : try
267 : {
268 0 : if (cur->compareVersions(sMaxVersion) == 1)
269 0 : continue;
270 : }
271 0 : catch (MalformedVersionException&)
272 : {
273 : //The maxVersion was not recognized as valid for this vendor.
274 : JFW_ENSURE(
275 : 0,"[Java framework]sunjavaplugin does not know version: "
276 : + ouMaxVer + " for vendor: " + cur->getVendor()
277 : + " .Check maximum Version." );
278 0 : return JFW_PLUGIN_E_WRONG_VERSION_FORMAT;
279 : }
280 : }
281 :
282 51 : bool bExclude = false;
283 51 : for (int j = 0; j < nLenList; j++)
284 : {
285 0 : OUString sExVer(arExcludeList[j]);
286 : try
287 : {
288 0 : if (cur->compareVersions(sExVer) == 0)
289 : {
290 0 : bExclude = true;
291 0 : break;
292 : }
293 : }
294 0 : catch (MalformedVersionException&)
295 : {
296 : //The excluded version was not recognized as valid for this vendor.
297 : JFW_ENSURE(
298 : 0,"[Java framework]sunjavaplugin does not know version: "
299 : + sExVer + " for vendor: " + cur->getVendor()
300 : + " .Check excluded versions." );
301 0 : return JFW_PLUGIN_E_WRONG_VERSION_FORMAT;
302 : }
303 0 : }
304 51 : if (bExclude == true)
305 0 : continue;
306 :
307 51 : vecVerifiedInfos.push_back(*i);
308 : }
309 : //Now vecVerifiedInfos contains all those JREs which meet the version requirements
310 : //Transfer them into the array that is passed out.
311 102 : arInfo = (JavaInfo**) rtl_allocateMemory(vecVerifiedInfos.size() * sizeof (JavaInfo*));
312 102 : int j = 0;
313 : typedef vector<rtl::Reference<VendorBase> >::const_iterator cit;
314 153 : for (cit ii = vecVerifiedInfos.begin(); ii != vecVerifiedInfos.end(); ++ii, ++j)
315 : {
316 51 : arInfo[j] = createJavaInfo(*ii);
317 : }
318 102 : *nLenInfoList = vecVerifiedInfos.size();
319 :
320 :
321 102 : *parJavaInfo = arInfo;
322 204 : return JFW_PLUGIN_E_NONE;
323 : }
324 :
325 : extern "C"
326 108 : javaPluginError jfw_plugin_getJavaInfoByPath(
327 : rtl_uString *path,
328 : rtl_uString *sVendor,
329 : rtl_uString *sMinVersion,
330 : rtl_uString *sMaxVersion,
331 : rtl_uString * *arExcludeList,
332 : sal_Int32 nLenList,
333 : JavaInfo ** ppInfo)
334 : {
335 108 : javaPluginError errorcode = JFW_PLUGIN_E_NONE;
336 :
337 : OSL_ASSERT(path);
338 : OSL_ASSERT(sVendor);
339 : OSL_ASSERT(sMinVersion);
340 : OSL_ASSERT(sMaxVersion);
341 108 : if (!path || !sVendor || !sMinVersion || !sMaxVersion || !ppInfo)
342 0 : return JFW_PLUGIN_E_INVALID_ARG;
343 108 : OUString ouPath(path);
344 : OSL_ASSERT(!ouPath.isEmpty());
345 108 : if (ouPath.isEmpty())
346 0 : return JFW_PLUGIN_E_INVALID_ARG;
347 :
348 : //nLenlist contains the number of element in arExcludeList.
349 : //If no exclude list is provided then nLenList must be 0
350 : OSL_ASSERT( ! (arExcludeList == NULL && nLenList > 0));
351 108 : if (arExcludeList == NULL && nLenList > 0)
352 0 : return JFW_PLUGIN_E_INVALID_ARG;
353 :
354 216 : OUString ouVendor(sVendor);
355 216 : OUString ouMinVer(sMinVersion);
356 216 : OUString ouMaxVer(sMaxVersion);
357 :
358 : OSL_ASSERT(!ouVendor.isEmpty());
359 108 : if (ouVendor.isEmpty())
360 0 : return JFW_PLUGIN_E_INVALID_ARG;
361 :
362 216 : rtl::Reference<VendorBase> aVendorInfo = getJREInfoByPath(ouPath);
363 108 : if (aVendorInfo.is() == sal_False)
364 0 : return JFW_PLUGIN_E_NO_JRE;
365 :
366 : //Check if the detected JRE matches the version requirements
367 108 : if (ouVendor.equals(aVendorInfo->getVendor()) == sal_False)
368 54 : return JFW_PLUGIN_E_NO_JRE;
369 :
370 54 : if (!ouMinVer.isEmpty())
371 : {
372 54 : int nRes = 0;
373 : try
374 : {
375 54 : nRes = aVendorInfo->compareVersions(ouMinVer);
376 : }
377 0 : catch (MalformedVersionException&)
378 : {
379 : //The minVersion was not recognized as valid for this vendor.
380 : JFW_ENSURE(
381 : 0,"[Java framework]sunjavaplugin does not know version: "
382 : + ouMinVer + " for vendor: " + aVendorInfo->getVendor()
383 : + " .Check minimum Version." );
384 0 : return JFW_PLUGIN_E_WRONG_VERSION_FORMAT;
385 : }
386 54 : if (nRes < 0)
387 0 : return JFW_PLUGIN_E_FAILED_VERSION;
388 : }
389 :
390 54 : if (!ouMaxVer.isEmpty())
391 : {
392 0 : int nRes = 0;
393 : try
394 : {
395 0 : nRes = aVendorInfo->compareVersions(ouMaxVer);
396 : }
397 0 : catch (MalformedVersionException&)
398 : {
399 : //The maxVersion was not recognized as valid for this vendor.
400 : JFW_ENSURE(
401 : 0,"[Java framework]sunjavaplugin does not know version: "
402 : + ouMaxVer + " for vendor: " + aVendorInfo->getVendor()
403 : + " .Check maximum Version." );
404 0 : return JFW_PLUGIN_E_WRONG_VERSION_FORMAT;
405 : }
406 0 : if (nRes > 0)
407 0 : return JFW_PLUGIN_E_FAILED_VERSION;
408 : }
409 :
410 54 : for (int i = 0; i < nLenList; i++)
411 : {
412 0 : OUString sExVer(arExcludeList[i]);
413 0 : int nRes = 0;
414 : try
415 : {
416 0 : nRes = aVendorInfo->compareVersions(sExVer);
417 : }
418 0 : catch (MalformedVersionException&)
419 : {
420 : //The excluded version was not recognized as valid for this vendor.
421 : JFW_ENSURE(
422 : 0,"[Java framework]sunjavaplugin does not know version: "
423 : + sExVer + " for vendor: " + aVendorInfo->getVendor()
424 : + " .Check excluded versions." );
425 0 : return JFW_PLUGIN_E_WRONG_VERSION_FORMAT;
426 : }
427 0 : if (nRes == 0)
428 0 : return JFW_PLUGIN_E_FAILED_VERSION;
429 0 : }
430 54 : *ppInfo = createJavaInfo(aVendorInfo);
431 :
432 162 : return errorcode;
433 : }
434 :
435 : #if defined(WNT)
436 :
437 : // Load msvcr71.dll using an explicit full path from where it is
438 : // present as bundled with the JRE. In case it is not found where we
439 : // think it should be, do nothing, and just let the implicit loading
440 : // that happens when loading the JVM take care of it.
441 :
442 : static void load_msvcr71(LPCWSTR jvm_dll)
443 : {
444 : wchar_t msvcr71_dll[MAX_PATH];
445 : wchar_t *slash;
446 :
447 : if (wcslen(jvm_dll) > MAX_PATH - 15)
448 : return;
449 :
450 : wcscpy(msvcr71_dll, jvm_dll);
451 :
452 : // First check if msvcr71.dll is in the same folder as jvm.dll. It
453 : // normally isn't, at least up to 1.6.0_22, but who knows if it
454 : // might be in the future.
455 : slash = wcsrchr(msvcr71_dll, L'\\');
456 :
457 : if (!slash)
458 : {
459 : // Huh, weird path to jvm.dll. Oh well.
460 : return;
461 : }
462 :
463 : wcscpy(slash+1, L"msvcr71.dll");
464 : if (LoadLibraryW(msvcr71_dll))
465 : return;
466 :
467 : // Then check if msvcr71.dll is in the parent folder of where
468 : // jvm.dll is. That is currently (1.6.0_22) as far as I know the
469 : // normal case.
470 : *slash = 0;
471 : slash = wcsrchr(msvcr71_dll, L'\\');
472 :
473 : if (!slash)
474 : return;
475 :
476 : wcscpy(slash+1, L"msvcr71.dll");
477 : LoadLibraryW(msvcr71_dll);
478 : }
479 :
480 : // Check if the jvm DLL imports msvcr71.dll, and in that case try
481 : // loading it explicitly. In case something goes wrong, do nothing,
482 : // and just let the implicit loading try to take care of it.
483 : static void do_msvcr71_magic(rtl_uString *jvm_dll)
484 : {
485 : rtl_uString* Module(0);
486 : struct stat st;
487 :
488 : oslFileError nError = osl_getSystemPathFromFileURL(jvm_dll, &Module);
489 :
490 : if ( osl_File_E_None != nError )
491 : rtl_uString_assign(&Module, jvm_dll);
492 :
493 : FILE *f = _wfopen(reinterpret_cast<LPCWSTR>(Module->buffer), L"rb");
494 :
495 : if (fstat(fileno(f), &st) == -1)
496 : {
497 : fclose(f);
498 : return;
499 : }
500 :
501 : PIMAGE_DOS_HEADER dos_hdr = (PIMAGE_DOS_HEADER) malloc(st.st_size);
502 :
503 : if (fread(dos_hdr, st.st_size, 1, f) != 1 ||
504 : memcmp(dos_hdr, "MZ", 2) != 0 ||
505 : dos_hdr->e_lfanew < 0 ||
506 : dos_hdr->e_lfanew > (LONG) (st.st_size - sizeof(IMAGE_NT_HEADERS)))
507 : {
508 : free(dos_hdr);
509 : fclose(f);
510 : return;
511 : }
512 :
513 : fclose(f);
514 :
515 : IMAGE_NT_HEADERS *nt_hdr = (IMAGE_NT_HEADERS *) ((char *)dos_hdr + dos_hdr->e_lfanew);
516 :
517 : IMAGE_IMPORT_DESCRIPTOR *imports =
518 : (IMAGE_IMPORT_DESCRIPTOR *) ((char *) dos_hdr + nt_hdr->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress);
519 :
520 : while (imports <= (IMAGE_IMPORT_DESCRIPTOR *) ((char *) dos_hdr + st.st_size - sizeof (IMAGE_IMPORT_DESCRIPTOR)) &&
521 : imports->Name != 0 &&
522 : imports->Name < (DWORD) st.st_size)
523 : {
524 : // Intentional use of sizeof("msvcr71.dll") here to include the terminating zero byte
525 : if (strnicmp((char *) dos_hdr + imports->Name, "msvcr71.dll", sizeof("msvcr71.dll")) == 0)
526 : {
527 : load_msvcr71(reinterpret_cast<LPCWSTR>(Module->buffer));
528 : break;
529 : }
530 : imports++;
531 : }
532 :
533 : free(dos_hdr);
534 : }
535 :
536 : #endif
537 :
538 : /** starts a Java Virtual Machine.
539 : <p>
540 : The function shall ensure, that the VM does not abort the process
541 : during instantiation.
542 : </p>
543 : */
544 : extern "C"
545 2 : javaPluginError jfw_plugin_startJavaVirtualMachine(
546 : const JavaInfo *pInfo,
547 : const JavaVMOption* arOptions,
548 : sal_Int32 cOptions,
549 : JavaVM ** ppVm,
550 : JNIEnv ** ppEnv)
551 : {
552 : // unless guard is volatile the following warning occurs on gcc:
553 : // warning: variable 't' might be clobbered by `longjmp' or `vfork'
554 2 : volatile osl::MutexGuard guard(PluginMutex::get());
555 : // unless errorcode is volatile the following warning occurs on gcc:
556 : // warning: variable 'errorcode' might be clobbered by `longjmp' or `vfork'
557 2 : volatile javaPluginError errorcode = JFW_PLUGIN_E_NONE;
558 2 : if ( pInfo == NULL || ppVm == NULL || ppEnv == NULL)
559 0 : return JFW_PLUGIN_E_INVALID_ARG;
560 : //Check if the Vendor (pInfo->sVendor) is supported by this plugin
561 2 : if ( ! isVendorSupported(pInfo->sVendor))
562 0 : return JFW_PLUGIN_E_WRONG_VENDOR;
563 4 : OUString sRuntimeLib = getRuntimeLib(pInfo->arVendorData);
564 : JFW_TRACE2("[Java framework] Using Java runtime library: "
565 : + sRuntimeLib + ".\n");
566 :
567 : #ifndef ANDROID
568 : // On linux we load jvm with RTLD_GLOBAL. This is necessary for debugging, because
569 : // libjdwp.so need a symbol (fork1) from libjvm which it only gets if the jvm is loaded
570 : // witd RTLD_GLOBAL. On Solaris libjdwp.so is correctly linked with libjvm.so
571 2 : oslModule moduleRt = 0;
572 : #if defined(LINUX)
573 2 : if ((moduleRt = osl_loadModule(sRuntimeLib.pData,
574 2 : SAL_LOADMODULE_GLOBAL | SAL_LOADMODULE_NOW)) == 0 )
575 : #else
576 : #if defined(WNT)
577 : do_msvcr71_magic(sRuntimeLib.pData);
578 : #endif
579 : if ((moduleRt = osl_loadModule(sRuntimeLib.pData, SAL_LOADMODULE_DEFAULT)) == 0)
580 : #endif
581 : {
582 : JFW_ENSURE(0, "[Java framework]sunjavaplugin" SAL_DLLEXTENSION
583 : " could not load Java runtime library: \n"
584 : + sRuntimeLib + "\n");
585 : JFW_TRACE0("[Java framework]sunjavaplugin" SAL_DLLEXTENSION
586 : " could not load Java runtime library: \n"
587 : + sRuntimeLib + "\n");
588 0 : return JFW_PLUGIN_E_VM_CREATION_FAILED;
589 : }
590 :
591 : #ifdef UNX
592 : //Setting the JAVA_HOME is needed for awt
593 4 : OUString javaHome("JAVA_HOME=");
594 4 : OUString sPathLocation;
595 2 : osl_getSystemPathFromFileURL(pInfo->sLocation, & sPathLocation.pData);
596 2 : javaHome += sPathLocation;
597 : OString osJavaHome = OUStringToOString(
598 4 : javaHome, osl_getThreadTextEncoding());
599 2 : putenv(strdup(osJavaHome.getStr()));
600 : #endif
601 :
602 : typedef jint JNICALL JNI_CreateVM_Type(JavaVM **, JNIEnv **, void *);
603 4 : OUString sSymbolCreateJava("JNI_CreateJavaVM");
604 :
605 : JNI_CreateVM_Type * pCreateJavaVM = (JNI_CreateVM_Type *) osl_getFunctionSymbol(
606 2 : moduleRt, sSymbolCreateJava.pData);
607 2 : if (!pCreateJavaVM)
608 : {
609 : OSL_ASSERT(0);
610 : OString sLib = OUStringToOString(
611 0 : sRuntimeLib, osl_getThreadTextEncoding());
612 : OString sSymbol = OUStringToOString(
613 0 : sSymbolCreateJava, osl_getThreadTextEncoding());
614 : fprintf(stderr,"[Java framework]sunjavaplugin" SAL_DLLEXTENSION
615 : "Java runtime library: %s does not export symbol %s !\n",
616 0 : sLib.getStr(), sSymbol.getStr());
617 0 : return JFW_PLUGIN_E_VM_CREATION_FAILED;
618 : }
619 :
620 : // Valgrind typically emits many false errors when executing JIT'ed JVM
621 : // code, so force the JVM into interpreted mode:
622 2 : bool forceInterpreted = RUNNING_ON_VALGRIND > 0;
623 :
624 : // Some testing with Java 1.4 showed that JavaVMOption.optionString has to
625 : // be encoded with the system encoding (i.e., osl_getThreadTextEncoding):
626 : JavaVMInitArgs vm_args;
627 :
628 2 : sal_Int32 nOptions = 1 + cOptions + (forceInterpreted ? 1 : 0);
629 : //TODO: check for overflow
630 4 : boost::scoped_array<JavaVMOption> sarOptions(new JavaVMOption[nOptions]);
631 2 : JavaVMOption * options = sarOptions.get();
632 :
633 : // We set an abort handler which is called when the VM calls _exit during
634 : // JNI_CreateJavaVM. This happens when the LD_LIBRARY_PATH does not contain
635 : // all some directories of the Java installation. This is necessary for
636 : // all versions below 1.5.1
637 2 : int n = 0;
638 2 : options[n].optionString= (char *) "abort";
639 2 : options[n].extraInfo= (void* )(sal_IntPtr)abort_handler;
640 2 : ++n;
641 4 : OString sClassPathProp("-Djava.class.path=");
642 4 : OString sClassPathOption;
643 10 : for (int i = 0; i < cOptions; i++)
644 : {
645 : #ifdef UNX
646 : // Until java 1.5 we need to put a plugin.jar or javaplugin.jar (<1.4.2)
647 : // in the class path in order to have applet support.
648 8 : OString sClassPath = arOptions[i].optionString;
649 8 : if (sClassPath.match(sClassPathProp, 0) == sal_True)
650 : {
651 2 : char sep[] = {SAL_PATHSEPARATOR, 0};
652 2 : OString sAddPath = getPluginJarPath(pInfo->sVendor, pInfo->sLocation,pInfo->sVersion);
653 2 : if (!sAddPath.isEmpty())
654 2 : sClassPathOption = sClassPath + OString(sep) + sAddPath;
655 : else
656 0 : sClassPathOption = sClassPath;
657 2 : options[n].optionString = (char *) sClassPathOption.getStr();
658 2 : options[n].extraInfo = arOptions[i].extraInfo;
659 : }
660 : else
661 : {
662 : #endif
663 6 : options[n].optionString = arOptions[i].optionString;
664 6 : options[n].extraInfo = arOptions[i].extraInfo;
665 : #ifdef UNX
666 : }
667 : #endif
668 : #if OSL_DEBUG_LEVEL >= 2
669 : JFW_TRACE2("VM option: " << options[n].optionString << "\n");
670 : #endif
671 8 : ++n;
672 8 : }
673 2 : if (forceInterpreted) {
674 0 : options[n].optionString = const_cast<char *>("-Xint");
675 0 : options[n].extraInfo = 0;
676 0 : ++n;
677 : }
678 :
679 : #ifdef MACOSX
680 : vm_args.version= JNI_VERSION_1_4; // issue 88987
681 : #else
682 2 : vm_args.version= JNI_VERSION_1_2;
683 : #endif
684 2 : vm_args.options= options;
685 2 : vm_args.nOptions= nOptions;
686 2 : vm_args.ignoreUnrecognized= JNI_TRUE;
687 :
688 : /* We set a global flag which is used by the abort handler in order to
689 : determine whether it is should use longjmp to get back into this function.
690 : That is, the abort handler determines if it is on the same stack as this function
691 : and then jumps back into this function.
692 : */
693 2 : g_bInGetJavaVM = 1;
694 : jint err;
695 2 : JavaVM * pJavaVM = 0;
696 2 : memset( jmp_jvm_abort, 0, sizeof(jmp_jvm_abort));
697 2 : int jmpval= setjmp( jmp_jvm_abort );
698 : /* If jmpval is not "0" then this point was reached by a longjmp in the
699 : abort_handler, which was called indirectly by JNI_CreateVM.
700 : */
701 2 : if( jmpval == 0)
702 : {
703 : //returns negative number on failure
704 2 : err= pCreateJavaVM(&pJavaVM, ppEnv, &vm_args);
705 2 : g_bInGetJavaVM = 0;
706 : }
707 : else
708 : // set err to a positive number, so as or recognize that an abort (longjmp)
709 : //occurred
710 0 : err= 1;
711 :
712 2 : if(err != 0)
713 : {
714 0 : if( err < 0)
715 : {
716 : fprintf(stderr,"[Java framework] sunjavaplugin" SAL_DLLEXTENSION
717 0 : "Can not create Java Virtual Machine\n");
718 0 : errorcode = JFW_PLUGIN_E_VM_CREATION_FAILED;
719 : }
720 0 : else if( err > 0)
721 : {
722 : fprintf(stderr,"[Java framework] sunjavaplugin" SAL_DLLEXTENSION
723 0 : "Can not create JavaVirtualMachine, abort handler was called.\n");
724 0 : errorcode = JFW_PLUGIN_E_VM_CREATION_FAILED;
725 : }
726 : }
727 : else
728 : {
729 2 : *ppVm = pJavaVM;
730 : JFW_TRACE2("[Java framework] sunjavaplugin" SAL_DLLEXTENSION " has created a VM.\n");
731 : }
732 : #else
733 : (void) arOptions;
734 : (void) cOptions;
735 : // On Android we always have a Java VM as we only expect this code
736 : // to be run in an Android app anyway.
737 : *ppVm = lo_get_javavm();
738 : fprintf(stderr, "lo_get_javavm returns %p", *ppVm);
739 : #endif
740 :
741 4 : return errorcode;
742 : }
743 :
744 : extern "C"
745 52 : javaPluginError jfw_plugin_existJRE(const JavaInfo *pInfo, sal_Bool *exist)
746 : {
747 52 : javaPluginError ret = JFW_PLUGIN_E_NONE;
748 52 : if (!pInfo || !exist)
749 0 : return JFW_PLUGIN_E_INVALID_ARG;
750 52 : OUString sLocation(pInfo->sLocation);
751 :
752 52 : if (sLocation.isEmpty())
753 0 : return JFW_PLUGIN_E_INVALID_ARG;
754 104 : ::osl::DirectoryItem item;
755 52 : ::osl::File::RC rc_item = ::osl::DirectoryItem::get(sLocation, item);
756 52 : if (::osl::File::E_None == rc_item)
757 : {
758 52 : *exist = sal_True;
759 : }
760 0 : else if (::osl::File::E_NOENT == rc_item)
761 : {
762 0 : *exist = sal_False;
763 : }
764 : else
765 : {
766 0 : ret = JFW_PLUGIN_E_ERROR;
767 : }
768 : #ifdef MACOSX
769 : //We can have the situation that the JavaVM runtime library is not
770 : //contained within JAVA_HOME. Then the check for JAVA_HOME would return
771 : //true although the runtime library may not be loadable.
772 : if (ret == JFW_PLUGIN_E_NONE && *exist == sal_True)
773 : {
774 : OUString sRuntimeLib = getRuntimeLib(pInfo->arVendorData);
775 : JFW_TRACE2("[Java framework] Checking existence of Java runtime library.\n");
776 :
777 : ::osl::DirectoryItem itemRt;
778 : ::osl::File::RC rc_itemRt = ::osl::DirectoryItem::get(sRuntimeLib, itemRt);
779 : if (::osl::File::E_None == rc_itemRt)
780 : {
781 : *exist = sal_True;
782 : JFW_TRACE2("[Java framework] Java runtime library exist: "
783 : + sRuntimeLib + "\n");
784 :
785 : }
786 : else if (::osl::File::E_NOENT == rc_itemRt)
787 : {
788 : *exist = sal_False;
789 : JFW_TRACE2("[Java framework] Java runtime library does not exist: "
790 : + sRuntimeLib + "\n");
791 : }
792 : else
793 : {
794 : ret = JFW_PLUGIN_E_ERROR;
795 : JFW_TRACE2("[Java framework] Error while looking for Java runtime library: "
796 : + sRuntimeLib + " \n");
797 : }
798 : }
799 : #endif
800 104 : return ret;
801 : }
802 :
803 :
804 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|