Skip to content

Commit

Permalink
8278753: Runtime crashes with access violation during JNI_CreateJavaV…
Browse files Browse the repository at this point in the history
…M call

Reviewed-by: dholmes, stuefe
  • Loading branch information
yminqi committed Feb 3, 2022
1 parent 86c24b3 commit cda9c30
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 31 deletions.
3 changes: 2 additions & 1 deletion make/data/hotspot-symbols/symbols-unix
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# Copyright (c) 2016, 2021, Oracle and/or its affiliates. All rights reserved.
# Copyright (c) 2016, 2022, Oracle and/or its affiliates. All rights reserved.
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
#
# This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -158,6 +158,7 @@ JVM_IsSupportedJNIVersion
JVM_IsThreadAlive
JVM_IsVMGeneratedMethodIx
JVM_LatestUserDefinedLoader
JVM_LoadZipLibrary
JVM_LoadLibrary
JVM_LookupDefineClass
JVM_LookupLambdaProxyClassFromArchive
Expand Down
20 changes: 10 additions & 10 deletions src/hotspot/share/classfile/classLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ static FindEntry_t FindEntry = NULL;
static ReadEntry_t ReadEntry = NULL;
static GetNextEntry_t GetNextEntry = NULL;
static Crc32_t Crc32 = NULL;
int ClassLoader::_libzip_loaded = 0;
int ClassLoader::_libzip_loaded = 0;
void* ClassLoader::_zip_handle = NULL;

// Entry points for jimage.dll for loading jimage file entries

Expand Down Expand Up @@ -942,20 +943,19 @@ void ClassLoader::load_zip_library() {
assert(ZipOpen == NULL, "should not load zip library twice");
char path[JVM_MAXPATHLEN];
char ebuf[1024];
void* handle = NULL;
if (os::dll_locate_lib(path, sizeof(path), Arguments::get_dll_dir(), "zip")) {
handle = os::dll_load(path, ebuf, sizeof ebuf);
_zip_handle = os::dll_load(path, ebuf, sizeof ebuf);
}
if (handle == NULL) {
if (_zip_handle == NULL) {
vm_exit_during_initialization("Unable to load zip library", path);
}

ZipOpen = CAST_TO_FN_PTR(ZipOpen_t, dll_lookup(handle, "ZIP_Open", path));
ZipClose = CAST_TO_FN_PTR(ZipClose_t, dll_lookup(handle, "ZIP_Close", path));
FindEntry = CAST_TO_FN_PTR(FindEntry_t, dll_lookup(handle, "ZIP_FindEntry", path));
ReadEntry = CAST_TO_FN_PTR(ReadEntry_t, dll_lookup(handle, "ZIP_ReadEntry", path));
GetNextEntry = CAST_TO_FN_PTR(GetNextEntry_t, dll_lookup(handle, "ZIP_GetNextEntry", path));
Crc32 = CAST_TO_FN_PTR(Crc32_t, dll_lookup(handle, "ZIP_CRC32", path));
ZipOpen = CAST_TO_FN_PTR(ZipOpen_t, dll_lookup(_zip_handle, "ZIP_Open", path));
ZipClose = CAST_TO_FN_PTR(ZipClose_t, dll_lookup(_zip_handle, "ZIP_Close", path));
FindEntry = CAST_TO_FN_PTR(FindEntry_t, dll_lookup(_zip_handle, "ZIP_FindEntry", path));
ReadEntry = CAST_TO_FN_PTR(ReadEntry_t, dll_lookup(_zip_handle, "ZIP_ReadEntry", path));
GetNextEntry = CAST_TO_FN_PTR(GetNextEntry_t, dll_lookup(_zip_handle, "ZIP_GetNextEntry", path));
Crc32 = CAST_TO_FN_PTR(Crc32_t, dll_lookup(_zip_handle, "ZIP_CRC32", path));
}

void ClassLoader::load_jimage_library() {
Expand Down
8 changes: 6 additions & 2 deletions src/hotspot/share/classfile/classLoader.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -229,6 +229,9 @@ class ClassLoader: AllStatic {
bool check_for_duplicates);
CDS_ONLY(static void add_to_module_path_entries(const char* path,
ClassPathEntry* entry);)

// cache the zip library handle
static void* _zip_handle;
public:
CDS_ONLY(static ClassPathEntry* app_classpath_entries() {return _app_classpath_entries;})
CDS_ONLY(static ClassPathEntry* module_path_entries() {return _module_path_entries;})
Expand All @@ -253,9 +256,10 @@ class ClassLoader: AllStatic {
private:
static int _libzip_loaded; // used to sync loading zip.
static void release_load_zip_library();
static inline void load_zip_library_if_needed();

public:
static inline void load_zip_library_if_needed();
static void* zip_library_handle() { return _zip_handle; }
static jzfile* open_zip_file(const char* canonical_path, char** error_msg, JavaThread* thread);
static ClassPathEntry* create_class_path_entry(JavaThread* current,
const char *path, const struct stat* st,
Expand Down
5 changes: 4 additions & 1 deletion src/hotspot/share/include/jvm.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -150,6 +150,9 @@ JVM_ActiveProcessorCount(void);
JNIEXPORT jboolean JNICALL
JVM_IsUseContainerSupport(void);

JNIEXPORT void * JNICALL
JVM_LoadZipLibrary();

JNIEXPORT void * JNICALL
JVM_LoadLibrary(const char *name, jboolean throwException);

Expand Down
7 changes: 6 additions & 1 deletion src/hotspot/share/prims/jvm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
#include "cds/heapShared.hpp"
#include "cds/lambdaFormInvokers.hpp"
#include "classfile/classFileStream.hpp"
#include "classfile/classLoader.hpp"
#include "classfile/classLoader.inline.hpp"
#include "classfile/classLoaderData.hpp"
#include "classfile/classLoaderData.inline.hpp"
#include "classfile/classLoadInfo.hpp"
Expand Down Expand Up @@ -3373,6 +3373,11 @@ JVM_END

// Library support ///////////////////////////////////////////////////////////////////////////

JVM_LEAF(void*, JVM_LoadZipLibrary())
ClassLoader::load_zip_library_if_needed();
return ClassLoader::zip_library_handle();
JVM_END

JVM_ENTRY_NO_ENV(void*, JVM_LoadLibrary(const char* name, jboolean throwException))
//%note jvm_ct
char ebuf[1024];
Expand Down
23 changes: 7 additions & 16 deletions src/java.base/share/native/libjimage/imageDecompressor.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2015, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2015, 2022, Oracle and/or its affiliates. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
Expand Down Expand Up @@ -30,6 +30,7 @@
*/

#include "jni.h"
#include "jvm.h"
#include "imageDecompressor.hpp"
#include "endian.hpp"
#ifdef WIN32
Expand Down Expand Up @@ -57,24 +58,14 @@ static ZipInflateFully_t ZipInflateFully = NULL;
* @return the address of the entry point or NULL
*/
static void* findEntry(const char* name) {
void *addr = NULL;
#ifdef WIN32
HMODULE handle = GetModuleHandle("zip.dll");
if (handle == NULL) {
handle = LoadLibrary("zip.dll");
}
if (handle == NULL) {
return NULL;
}
addr = (void*) GetProcAddress(handle, name);
return addr;
#else
addr = dlopen(JNI_LIB_PREFIX "zip" JNI_LIB_SUFFIX, RTLD_GLOBAL|RTLD_LAZY);
void *addr = JVM_LoadZipLibrary();
if (addr == NULL) {
return NULL;
}
addr = dlsym(addr, name);
return addr;
#ifdef WIN32
return (void*) GetProcAddress(static_cast<HMODULE>(addr), name);
#else
return dlsym(addr, name);
#endif
}

Expand Down

1 comment on commit cda9c30

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.