Skip to content

Commit 1a07d54

Browse files
author
David Holmes
committed
8343703: Symbol name cleanups after JEP 479
Reviewed-by: kbarrett, amenkov
1 parent a0df0a5 commit 1a07d54

File tree

5 files changed

+46
-88
lines changed

5 files changed

+46
-88
lines changed

src/hotspot/share/include/jvm.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1147,13 +1147,6 @@ JVM_GetClassFileVersion(JNIEnv *env, jclass current);
11471147
JNIEXPORT jboolean JNICALL
11481148
JVM_PrintWarningAtDynamicAgentLoad(void);
11491149

1150-
#define JNI_ONLOAD_SYMBOLS {"JNI_OnLoad"}
1151-
#define JNI_ONUNLOAD_SYMBOLS {"JNI_OnUnload"}
1152-
#define JVM_ONLOAD_SYMBOLS {"JVM_OnLoad"}
1153-
#define AGENT_ONLOAD_SYMBOLS {"Agent_OnLoad"}
1154-
#define AGENT_ONUNLOAD_SYMBOLS {"Agent_OnUnload"}
1155-
#define AGENT_ONATTACH_SYMBOLS {"Agent_OnAttach"}
1156-
11571150
/*
11581151
* This structure is used by the launcher to get the default thread
11591152
* stack size from the VM using JNI_GetDefaultJavaVMInitArgs() with a

src/hotspot/share/prims/jvmtiAgent.cpp

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
#include "cds/cds_globals.hpp"
2828
#include "cds/cdsConfig.hpp"
2929
#include "jni.h"
30-
#include "jvm.h"
3130
#include "jvm_io.h"
3231
#include "jvmtifiles/jvmtiEnv.hpp"
3332
#include "prims/jvmtiEnvBase.hpp"
@@ -268,10 +267,10 @@ static void assert_preload(const JvmtiAgent* agent) {
268267
// For statically linked agents we can't rely on os_lib == nullptr because
269268
// statically linked agents could have a handle of RTLD_DEFAULT which == 0 on some platforms.
270269
// If this function returns true, then agent->is_static_lib() && agent->is_loaded().
271-
static bool load_agent_from_executable(JvmtiAgent* agent, const char* on_load_symbols[], size_t num_symbol_entries) {
270+
static bool load_agent_from_executable(JvmtiAgent* agent, const char* on_load_symbol) {
272271
DEBUG_ONLY(assert_preload(agent);)
273-
assert(on_load_symbols != nullptr, "invariant");
274-
return os::find_builtin_agent(agent, &on_load_symbols[0], num_symbol_entries);
272+
assert(on_load_symbol != nullptr, "invariant");
273+
return os::find_builtin_agent(agent, on_load_symbol);
275274
}
276275

277276
// Load the library from the absolute path of the agent, if available.
@@ -310,7 +309,7 @@ static void* load_agent_from_relative_path(JvmtiAgent* agent, bool vm_exit_on_er
310309
}
311310

312311
// For absolute and relative paths.
313-
static void* load_library(JvmtiAgent* agent, const char* on_symbols[], size_t num_symbol_entries, bool vm_exit_on_error) {
312+
static void* load_library(JvmtiAgent* agent, bool vm_exit_on_error) {
314313
return agent->is_absolute_path() ? load_agent_from_absolute_path(agent, vm_exit_on_error) :
315314
load_agent_from_relative_path(agent, vm_exit_on_error);
316315
}
@@ -321,30 +320,27 @@ extern "C" {
321320
}
322321

323322
// Find the OnLoad entry point for -agentlib: -agentpath: -Xrun agents.
324-
// num_symbol_entries must be passed-in since only the caller knows the number of symbols in the array.
325-
static OnLoadEntry_t lookup_On_Load_entry_point(JvmtiAgent* agent, const char* on_load_symbols[], size_t num_symbol_entries) {
323+
static OnLoadEntry_t lookup_On_Load_entry_point(JvmtiAgent* agent, const char* on_load_symbol) {
326324
assert(agent != nullptr, "invariant");
327325
if (!agent->is_loaded()) {
328-
if (!load_agent_from_executable(agent, on_load_symbols, num_symbol_entries)) {
329-
void* const library = load_library(agent, on_load_symbols, num_symbol_entries, /* vm exit on error */ true);
326+
if (!load_agent_from_executable(agent, on_load_symbol)) {
327+
void* const library = load_library(agent, /* vm exit on error */ true);
330328
assert(library != nullptr, "invariant");
331329
agent->set_os_lib(library);
332330
agent->set_loaded();
333331
}
334332
}
335333
assert(agent->is_loaded(), "invariant");
336334
// Find the OnLoad function.
337-
return CAST_TO_FN_PTR(OnLoadEntry_t, os::find_agent_function(agent, false, on_load_symbols, num_symbol_entries));
335+
return CAST_TO_FN_PTR(OnLoadEntry_t, os::find_agent_function(agent, false, on_load_symbol));
338336
}
339337

340338
static OnLoadEntry_t lookup_JVM_OnLoad_entry_point(JvmtiAgent* lib) {
341-
const char* on_load_symbols[] = JVM_ONLOAD_SYMBOLS;
342-
return lookup_On_Load_entry_point(lib, on_load_symbols, sizeof(on_load_symbols) / sizeof(char*));
339+
return lookup_On_Load_entry_point(lib, "JVM_OnLoad");
343340
}
344341

345342
static OnLoadEntry_t lookup_Agent_OnLoad_entry_point(JvmtiAgent* agent) {
346-
const char* on_load_symbols[] = AGENT_ONLOAD_SYMBOLS;
347-
return lookup_On_Load_entry_point(agent, on_load_symbols, sizeof(on_load_symbols) / sizeof(char*));
343+
return lookup_On_Load_entry_point(agent, "Agent_OnLoad");
348344
}
349345

350346
void JvmtiAgent::convert_xrun_agent() {
@@ -499,14 +495,13 @@ static bool invoke_Agent_OnAttach(JvmtiAgent* agent, outputStream* st) {
499495
assert(agent->is_dynamic(), "invariant");
500496
assert(st != nullptr, "invariant");
501497
assert(JvmtiEnvBase::get_phase() == JVMTI_PHASE_LIVE, "not in live phase!");
502-
const char* on_attach_symbols[] = AGENT_ONATTACH_SYMBOLS;
503-
const size_t num_symbol_entries = ARRAY_SIZE(on_attach_symbols);
498+
const char* on_attach_symbol = "Agent_OnAttach";
504499
void* library = nullptr;
505500
bool previously_loaded;
506-
if (load_agent_from_executable(agent, &on_attach_symbols[0], num_symbol_entries)) {
501+
if (load_agent_from_executable(agent, on_attach_symbol)) {
507502
previously_loaded = JvmtiAgentList::is_static_lib_loaded(agent->name());
508503
} else {
509-
library = load_library(agent, &on_attach_symbols[0], num_symbol_entries, /* vm_exit_on_error */ false);
504+
library = load_library(agent, /* vm_exit_on_error */ false);
510505
if (library == nullptr) {
511506
st->print_cr("%s was not loaded.", agent->name());
512507
if (*ebuf != '\0') {
@@ -531,10 +526,10 @@ static bool invoke_Agent_OnAttach(JvmtiAgent* agent, outputStream* st) {
531526
assert(agent->is_loaded(), "invariant");
532527
// The library was loaded so we attempt to lookup and invoke the Agent_OnAttach function.
533528
OnAttachEntry_t on_attach_entry = CAST_TO_FN_PTR(OnAttachEntry_t,
534-
os::find_agent_function(agent, false, &on_attach_symbols[0], num_symbol_entries));
529+
os::find_agent_function(agent, false, on_attach_symbol));
535530

536531
if (on_attach_entry == nullptr) {
537-
st->print_cr("%s is not available in %s", on_attach_symbols[0], agent->name());
532+
st->print_cr("%s is not available in %s", on_attach_symbol, agent->name());
538533
unload_library(agent, library);
539534
return false;
540535
}
@@ -629,10 +624,10 @@ extern "C" {
629624
}
630625

631626
void JvmtiAgent::unload() {
632-
const char* on_unload_symbols[] = AGENT_ONUNLOAD_SYMBOLS;
627+
const char* on_unload_symbol = "Agent_OnUnload";
633628
// Find the Agent_OnUnload function.
634629
Agent_OnUnload_t unload_entry = CAST_TO_FN_PTR(Agent_OnUnload_t,
635-
os::find_agent_function(this, false, &on_unload_symbols[0], ARRAY_SIZE(on_unload_symbols)));
630+
os::find_agent_function(this, false, on_unload_symbol));
636631
if (unload_entry != nullptr) {
637632
// Invoke the Agent_OnUnload function
638633
JavaThread* thread = JavaThread::current();

src/hotspot/share/runtime/os.cpp

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -546,49 +546,35 @@ void* os::native_java_library() {
546546
* executable if agent_lib->is_static_lib() == true or in the shared library
547547
* referenced by 'handle'.
548548
*/
549-
void* os::find_agent_function(JvmtiAgent *agent_lib, bool check_lib,
550-
const char *syms[], size_t syms_len) {
549+
void* os::find_agent_function(JvmtiAgent *agent_lib, bool check_lib, const char *sym) {
551550
assert(agent_lib != nullptr, "sanity check");
552-
const char *lib_name;
553551
void *handle = agent_lib->os_lib();
554552
void *entryName = nullptr;
555-
char *agent_function_name;
556-
size_t i;
557553

558554
// If checking then use the agent name otherwise test is_static_lib() to
559555
// see how to process this lookup
560-
lib_name = ((check_lib || agent_lib->is_static_lib()) ? agent_lib->name() : nullptr);
561-
for (i = 0; i < syms_len; i++) {
562-
agent_function_name = build_agent_function_name(syms[i], lib_name, agent_lib->is_absolute_path());
563-
if (agent_function_name == nullptr) {
564-
break;
565-
}
556+
const char *lib_name = ((check_lib || agent_lib->is_static_lib()) ? agent_lib->name() : nullptr);
557+
558+
char* agent_function_name = build_agent_function_name(sym, lib_name, agent_lib->is_absolute_path());
559+
if (agent_function_name != nullptr) {
566560
entryName = dll_lookup(handle, agent_function_name);
567561
FREE_C_HEAP_ARRAY(char, agent_function_name);
568-
if (entryName != nullptr) {
569-
break;
570-
}
571562
}
572563
return entryName;
573564
}
574565

575566
// See if the passed in agent is statically linked into the VM image.
576-
bool os::find_builtin_agent(JvmtiAgent* agent, const char *syms[],
577-
size_t syms_len) {
578-
void *ret;
579-
void *proc_handle;
580-
void *save_handle;
581-
567+
bool os::find_builtin_agent(JvmtiAgent* agent, const char* sym) {
582568
assert(agent != nullptr, "sanity check");
583569
if (agent->name() == nullptr) {
584570
return false;
585571
}
586-
proc_handle = get_default_process_handle();
572+
void* proc_handle = get_default_process_handle();
587573
// Check for Agent_OnLoad/Attach_lib_name function
588-
save_handle = agent->os_lib();
574+
void* save_handle = agent->os_lib();
589575
// We want to look in this process' symbol table.
590576
agent->set_os_lib(proc_handle);
591-
ret = find_agent_function(agent, true, syms, syms_len);
577+
void* ret = find_agent_function(agent, true, sym);
592578
if (ret != nullptr) {
593579
// Found an entry point like Agent_OnLoad_lib_name so we have a static agent
594580
agent->set_static_lib();

src/hotspot/share/runtime/os.hpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -777,12 +777,10 @@ class os: AllStatic {
777777
static void* get_default_process_handle();
778778

779779
// Check for static linked agent library
780-
static bool find_builtin_agent(JvmtiAgent *agent_lib, const char *syms[],
781-
size_t syms_len);
780+
static bool find_builtin_agent(JvmtiAgent* agent_lib, const char* sym);
782781

783782
// Find agent entry point
784-
static void *find_agent_function(JvmtiAgent *agent_lib, bool check_lib,
785-
const char *syms[], size_t syms_len);
783+
static void* find_agent_function(JvmtiAgent* agent_lib, bool check_lib, const char* sym);
786784

787785
// Provide wrapper versions of these functions to guarantee NUL-termination
788786
// in all cases.

src/java.base/share/native/libjava/NativeLibraries.c

Lines changed: 18 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -64,45 +64,31 @@ static jboolean initIDs(JNIEnv *env)
6464
*/
6565
static void *findJniFunction(JNIEnv *env, void *handle,
6666
const char *cname, jboolean isLoad) {
67-
const char *onLoadSymbols[] = JNI_ONLOAD_SYMBOLS;
68-
const char *onUnloadSymbols[] = JNI_ONUNLOAD_SYMBOLS;
69-
const char **syms;
70-
int symsLen;
67+
const char *sym;
7168
void *entryName = NULL;
7269
char *jniFunctionName;
73-
int i;
7470
size_t len;
7571

7672
// Check for JNI_On(Un)Load<_libname> function
77-
if (isLoad) {
78-
syms = onLoadSymbols;
79-
symsLen = sizeof(onLoadSymbols) / sizeof(char *);
80-
} else {
81-
syms = onUnloadSymbols;
82-
symsLen = sizeof(onUnloadSymbols) / sizeof(char *);
73+
sym = isLoad ? "JNI_OnLoad" : "JNI_OnUnload";
74+
75+
// sym + '_' + cname + '\0'
76+
if ((len = strlen(sym) + (cname != NULL ? (strlen(cname) + 1) : 0) + 1) >
77+
FILENAME_MAX) {
78+
goto done;
8379
}
84-
for (i = 0; i < symsLen; i++) {
85-
// cname + sym + '_' + '\0'
86-
if ((len = (cname != NULL ? strlen(cname) : 0) + strlen(syms[i]) + 2) >
87-
FILENAME_MAX) {
88-
goto done;
89-
}
90-
jniFunctionName = malloc(len);
91-
if (jniFunctionName == NULL) {
92-
JNU_ThrowOutOfMemoryError(env, NULL);
93-
goto done;
94-
}
95-
strcpy(jniFunctionName, syms[i]);
96-
if (cname != NULL) {
97-
strcat(jniFunctionName, "_");
98-
strcat(jniFunctionName, cname);
99-
}
100-
entryName = JVM_FindLibraryEntry(handle, jniFunctionName);
101-
free(jniFunctionName);
102-
if(entryName) {
103-
break;
104-
}
80+
jniFunctionName = malloc(len);
81+
if (jniFunctionName == NULL) {
82+
JNU_ThrowOutOfMemoryError(env, NULL);
83+
goto done;
84+
}
85+
strcpy(jniFunctionName, sym);
86+
if (cname != NULL) {
87+
strcat(jniFunctionName, "_");
88+
strcat(jniFunctionName, cname);
10589
}
90+
entryName = JVM_FindLibraryEntry(handle, jniFunctionName);
91+
free(jniFunctionName);
10692

10793
done:
10894
return entryName;

0 commit comments

Comments
 (0)