-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAgentInitializer.java
182 lines (160 loc) · 6.29 KB
/
AgentInitializer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
package io.opentelemetry.javaagent.bootstrap;
import io.opentelemetry.instrumentation.api.internal.ConfigPropertiesUtil;
import java.io.File;
import java.lang.instrument.Instrumentation;
import java.lang.reflect.Constructor;
import java.security.PrivilegedAction;
import java.security.PrivilegedExceptionAction;
import javax.annotation.Nullable;
/**
* Agent start up logic.
*
* <p>This class is loaded and called by {@code io.opentelemetry.javaagent.OpenTelemetryAgent}
*
* <p>The intention is for this class to be loaded by bootstrap class loader to make sure we have
* unimpeded access to the rest of agent parts.
*/
public final class AgentInitializer {
@Nullable private static ClassLoader agentClassLoader = null;
@Nullable private static AgentStarter agentStarter = null;
private static boolean isSecurityManagerSupportEnabled = false;
public static void initialize(Instrumentation inst, File javaagentFile, boolean fromPremain)
throws Exception {
if (agentClassLoader != null) {
return;
}
// we expect that at this point agent jar has been appended to boot class path and all agent
// classes are loaded in boot loader
if (AgentInitializer.class.getClassLoader() != null) {
throw new IllegalStateException("agent initializer should be loaded in boot loader");
}
isSecurityManagerSupportEnabled = isSecurityManagerSupportEnabled();
// this call deliberately uses anonymous class instead of lambda because using lambdas too
// early on early jdk8 (see isEarlyOracle18 method) causes jvm to crash. See CrashEarlyJdk8Test.
execute(
new PrivilegedExceptionAction<Void>() {
@Override
public Void run() throws Exception {
agentClassLoader = createAgentClassLoader("inst", javaagentFile);
agentStarter = createAgentStarter(agentClassLoader, inst, javaagentFile);
if (!fromPremain || !delayAgentStart()) {
agentStarter.start();
}
return null;
}
});
}
private static void execute(PrivilegedExceptionAction<Void> action) throws Exception {
if (isSecurityManagerSupportEnabled && System.getSecurityManager() != null) {
doPrivilegedExceptionAction(action);
} else {
action.run();
}
}
private static boolean isSecurityManagerSupportEnabled() {
return getBoolean("otel.javaagent.experimental.security-manager-support.enabled", false);
}
private static boolean getBoolean(String property, boolean defaultValue) {
// this call deliberately uses anonymous class instead of lambda because using lambdas too
// early on early jdk8 (see isEarlyOracle18 method) causes jvm to crash. See CrashEarlyJdk8Test.
return doPrivileged(
new PrivilegedAction<Boolean>() {
@Override
public Boolean run() {
return ConfigPropertiesUtil.getBoolean(property, defaultValue);
}
});
}
@SuppressWarnings({"deprecation", "removal"}) // AccessController is deprecated
private static <T> T doPrivilegedExceptionAction(PrivilegedExceptionAction<T> action)
throws Exception {
return java.security.AccessController.doPrivileged(action);
}
@SuppressWarnings({"deprecation", "removal"}) // AccessController is deprecated
private static <T> T doPrivileged(PrivilegedAction<T> action) {
return java.security.AccessController.doPrivileged(action);
}
/**
* Test whether we are running on oracle 1.8 before 1.8.0_40.
*
* @return true for oracle 1.8 before 1.8.0_40
*/
private static boolean isEarlyOracle18() {
// Java HotSpot(TM) 64-Bit Server VM or OpenJDK 64-Bit Server VM
String vmName = System.getProperty("java.vm.name");
if (!vmName.contains("HotSpot") && !vmName.contains("OpenJDK")) {
return false;
}
// 1.8.0_31
String javaVersion = System.getProperty("java.version");
if (!javaVersion.startsWith("1.8")) {
return false;
}
int index = javaVersion.indexOf('_');
if (index == -1) {
return false;
}
String minorVersion = javaVersion.substring(index + 1);
try {
int version = Integer.parseInt(minorVersion);
if (version >= 40) {
return false;
}
} catch (NumberFormatException exception) {
return false;
}
return true;
}
private static boolean delayAgentStart() {
if (!isEarlyOracle18()) {
return false;
}
return agentStarter.delayStart();
}
/**
* Call to this method is inserted into {@code sun.launcher.LauncherHelper.checkAndLoadMain()}.
*/
@SuppressWarnings("unused")
public static void delayedStartHook() throws Exception {
// this call deliberately uses anonymous class instead of lambda because using lambdas too
// early on early jdk8 (see isEarlyOracle18 method) causes jvm to crash. See CrashEarlyJdk8Test.
execute(
new PrivilegedExceptionAction<Void>() {
@Override
public Void run() {
agentStarter.start();
return null;
}
});
}
public static ClassLoader getExtensionsClassLoader() {
// agentStarter can be null when running tests
return agentStarter != null ? agentStarter.getExtensionClassLoader() : null;
}
/**
* Create the agent class loader. This must be called after the bootstrap jar has been appended to
* the bootstrap classpath.
*
* @param innerJarFilename Filename of internal jar to use for the classpath of the agent class
* loader
* @return Agent Classloader
*/
private static ClassLoader createAgentClassLoader(String innerJarFilename, File javaagentFile) {
return new AgentClassLoader(javaagentFile, innerJarFilename, isSecurityManagerSupportEnabled);
}
private static AgentStarter createAgentStarter(
ClassLoader agentClassLoader, Instrumentation instrumentation, File javaagentFile)
throws Exception {
Class<?> starterClass =
agentClassLoader.loadClass("io.opentelemetry.javaagent.tooling.AgentStarterImpl");
Constructor<?> constructor =
starterClass.getDeclaredConstructor(Instrumentation.class, File.class, boolean.class);
return (AgentStarter)
constructor.newInstance(instrumentation, javaagentFile, isSecurityManagerSupportEnabled);
}
private AgentInitializer() {}
}