-
Notifications
You must be signed in to change notification settings - Fork 333
Expand file tree
/
Copy pathAgentInstaller.java
More file actions
540 lines (484 loc) · 19.7 KB
/
AgentInstaller.java
File metadata and controls
540 lines (484 loc) · 19.7 KB
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
package datadog.trace.agent.tooling;
import static datadog.trace.agent.tooling.ExtensionFinder.findExtensions;
import static datadog.trace.agent.tooling.ExtensionLoader.loadExtensions;
import static datadog.trace.agent.tooling.bytebuddy.matcher.GlobalIgnoresMatcher.globalIgnoresMatcher;
import static net.bytebuddy.matcher.ElementMatchers.isDefaultFinalizer;
import datadog.environment.SystemProperties;
import datadog.trace.agent.tooling.bytebuddy.SharedTypePools;
import datadog.trace.agent.tooling.bytebuddy.iast.TaintableRedefinitionStrategyListener;
import datadog.trace.agent.tooling.bytebuddy.matcher.DDElementMatchers;
import datadog.trace.agent.tooling.bytebuddy.memoize.MemoizedMatchers;
import datadog.trace.agent.tooling.bytebuddy.outline.TypePoolFacade;
import datadog.trace.agent.tooling.usm.UsmExtractorImpl;
import datadog.trace.agent.tooling.usm.UsmMessageFactoryImpl;
import datadog.trace.api.InstrumenterConfig;
import datadog.trace.api.Platform;
import datadog.trace.api.ProductActivation;
import datadog.trace.api.telemetry.IntegrationsCollector;
import datadog.trace.bootstrap.FieldBackedContextAccessor;
import datadog.trace.bootstrap.instrumentation.java.concurrent.ExcludeFilter;
import datadog.trace.util.AgentTaskScheduler;
import de.thetaphi.forbiddenapis.SuppressForbidden;
import java.lang.instrument.ClassFileTransformer;
import java.lang.instrument.Instrumentation;
import java.util.Collections;
import java.util.Comparator;
import java.util.EnumSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.TimeUnit;
import java.util.function.BooleanSupplier;
import net.bytebuddy.ByteBuddy;
import net.bytebuddy.agent.builder.AgentBuilder;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.dynamic.DynamicType;
import net.bytebuddy.dynamic.NexusAccessor;
import net.bytebuddy.dynamic.VisibilityBridgeStrategy;
import net.bytebuddy.dynamic.scaffold.InstrumentedType;
import net.bytebuddy.dynamic.scaffold.MethodGraph;
import net.bytebuddy.matcher.LatentMatcher;
import net.bytebuddy.utility.JavaModule;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class AgentInstaller {
private static final Logger log = LoggerFactory.getLogger(AgentInstaller.class);
private static final boolean DEBUG = log.isDebugEnabled();
private static final List<Runnable> LOG_MANAGER_CALLBACKS = new CopyOnWriteArrayList<>();
private static final List<Runnable> MBEAN_SERVER_BUILDER_CALLBACKS = new CopyOnWriteArrayList<>();
static {
enableByteBuddyRawTypes();
disableByteBuddyNexus();
// register weak map supplier as early as possible
WeakMaps.registerAsSupplier();
circularityErrorWorkaround();
}
@SuppressForbidden
private static void circularityErrorWorkaround() {
// these classes have been involved in intermittent ClassCircularityErrors during startup
// they don't need context storage, so it's safe to load them before installing the agent
try {
Class.forName("java.util.concurrent.ThreadLocalRandom");
} catch (Throwable ignore) {
}
}
public static void installBytebuddyAgent(final Instrumentation inst) {
/*
* ByteBuddy agent is used by several systems which can be enabled independently;
* we need to install the agent whenever any of them is active.
*/
Set<InstrumenterModule.TargetSystem> enabledSystems = getEnabledSystems();
if (!enabledSystems.isEmpty()) {
installBytebuddyAgent(inst, false, enabledSystems);
if (DEBUG) {
log.debug("Instrumentation installed for {}", enabledSystems);
}
int poolCleaningInterval = InstrumenterConfig.get().getResolverResetInterval();
if (poolCleaningInterval > 0) {
AgentTaskScheduler.get()
.scheduleAtFixedRate(
SharedTypePools::clear,
poolCleaningInterval,
Math.max(poolCleaningInterval, 10),
TimeUnit.SECONDS);
}
} else if (DEBUG) {
log.debug("No target systems enabled, skipping instrumentation.");
}
}
/**
* Install the core bytebuddy agent along with all registered {@link InstrumenterModule}s.
*
* @return the agent's class transformer
*/
public static ClassFileTransformer installBytebuddyAgent(
final Instrumentation inst,
final boolean skipAdditionalLibraryMatcher,
final Set<InstrumenterModule.TargetSystem> enabledSystems,
final AgentBuilder.Listener... listeners) {
Utils.setInstrumentation(inst);
TypePoolFacade.registerAsSupplier();
if (InstrumenterConfig.get().isResolverMemoizingEnabled()) {
MemoizedMatchers.registerAsSupplier();
} else {
DDElementMatchers.registerAsSupplier();
}
if (enabledSystems.contains(InstrumenterModule.TargetSystem.USM)) {
UsmMessageFactoryImpl.registerAsSupplier();
UsmExtractorImpl.registerAsSupplier();
}
// By default ByteBuddy will skip all methods that are synthetic or default finalizer
// but we need to instrument some synthetic methods in Scala, so change the ignore matcher
ByteBuddy byteBuddy =
new ByteBuddy().ignore(new LatentMatcher.Resolved<>(isDefaultFinalizer()));
boolean simpleMethodGraph = InstrumenterConfig.get().isResolverSimpleMethodGraph();
if (simpleMethodGraph) {
// faster compiler that just considers visibility of locally declared methods
byteBuddy =
byteBuddy
.with(MethodGraph.Compiler.ForDeclaredMethods.INSTANCE)
.with(VisibilityBridgeStrategy.Default.NEVER)
.with(InstrumentedType.Factory.Default.FROZEN);
}
AgentBuilder agentBuilder = new AgentBuilder.Default(byteBuddy);
if (simpleMethodGraph) {
// faster strategy that assumes transformations use @Advice or AsmVisitorWrapper
agentBuilder = agentBuilder.with(AgentBuilder.TypeStrategy.Default.DECORATE);
}
agentBuilder =
agentBuilder
.disableClassFormatChanges()
.assureReadEdgeTo(inst, FieldBackedContextAccessor.class)
.with(AgentStrategies.transformerDecorator())
.with(AgentBuilder.RedefinitionStrategy.RETRANSFORMATION)
.with(AgentStrategies.rediscoveryStrategy())
.with(redefinitionStrategyListener(enabledSystems))
.with(AgentStrategies.locationStrategy())
.with(AgentStrategies.poolStrategy())
.with(AgentBuilder.DescriptionStrategy.Default.POOL_ONLY)
.with(AgentStrategies.bufferStrategy())
.with(AgentStrategies.typeStrategy())
.with(new ClassLoadListener())
// FIXME: we cannot enable it yet due to BB/JVM bug, see
// https://github.com/raphw/byte-buddy/issues/558
// .with(AgentBuilder.LambdaInstrumentationStrategy.ENABLED)
.ignore(globalIgnoresMatcher(skipAdditionalLibraryMatcher));
if (DEBUG) {
agentBuilder =
agentBuilder
.with(AgentBuilder.RedefinitionStrategy.RETRANSFORMATION)
.with(AgentStrategies.rediscoveryStrategy())
.with(redefinitionStrategyListener(enabledSystems))
.with(new RedefinitionLoggingListener())
.with(new TransformLoggingListener());
}
for (final AgentBuilder.Listener listener : listeners) {
agentBuilder = agentBuilder.with(listener);
}
InstrumenterIndex instrumenterIndex = InstrumenterIndex.readIndex();
// pre-size state before registering instrumentations to reduce number of allocations
InstrumenterState.initialize(instrumenterIndex.instrumentationCount());
// combine known modules indexed at build-time with extensions contributed at run-time
Iterable<InstrumenterModule> instrumenterModules =
withExtensions(
instrumenterIndex.modules(
InstrumenterModuleFilter.forTargetSystemsOrExcludeProvider(enabledSystems)));
// This needs to be a separate loop through all instrumentations before we start adding
// advice so that we can exclude field injection, since that will try to check exclusion
// immediately and we don't have the ability to express dependencies between different
// instrumentations to control the load order.
for (InstrumenterModule module : instrumenterModules) {
if (module instanceof ExcludeFilterProvider) {
ExcludeFilterProvider provider = (ExcludeFilterProvider) module;
ExcludeFilter.add(provider.excludedClasses());
if (DEBUG) {
log.debug(
"Adding filtered classes - instrumentation.class={}", module.getClass().getName());
}
}
}
CombiningTransformerBuilder transformerBuilder =
new CombiningTransformerBuilder(agentBuilder, instrumenterIndex, enabledSystems);
int installedCount = 0;
for (InstrumenterModule module : instrumenterModules) {
if (!module.isApplicable(enabledSystems)) {
if (DEBUG) {
log.debug("Not applicable - instrumentation.class={}", module.getClass().getName());
}
continue;
}
if (DEBUG) {
log.debug("Loading - instrumentation.class={}", module.getClass().getName());
}
try {
transformerBuilder.applyInstrumentation(module);
installedCount++;
} catch (Exception | LinkageError e) {
log.error("Failed to load - instrumentation.class={}", module.getClass().getName(), e);
}
}
if (DEBUG) {
log.debug("Installed {} instrumenter(s)", installedCount);
}
if (!Platform.isNativeImageBuilder()) {
InstrumenterFlare.register();
}
if (InstrumenterConfig.get().isTelemetryEnabled()) {
InstrumenterState.setObserver(
new InstrumenterState.Observer() {
@Override
public void applied(Iterable<String> instrumentationNames) {
IntegrationsCollector.get().update(instrumentationNames, true);
}
});
}
InstrumenterState.resetDefaultState();
try {
return transformerBuilder.installOn(inst);
} finally {
SharedTypePools.endInstall();
}
}
/** Returns an iterable that combines the original sequence with any discovered extensions. */
private static Iterable<InstrumenterModule> withExtensions(Iterable<InstrumenterModule> initial) {
String extensionsPath = InstrumenterConfig.get().getTraceExtensionsPath();
if (null != extensionsPath) {
if (findExtensions(extensionsPath, InstrumenterModule.class)) {
final List<InstrumenterModule> extensions = loadExtensions(InstrumenterModule.class);
extensions.sort(Comparator.comparingInt(InstrumenterModule::order));
return new Iterable<InstrumenterModule>() {
@Override
public Iterator<InstrumenterModule> iterator() {
return withExtensions(initial.iterator(), extensions);
}
};
}
}
return initial;
}
/** Returns an iterator that combines the original sequence with any discovered extensions. */
private static Iterator<InstrumenterModule> withExtensions(
final Iterator<InstrumenterModule> initial, final Iterable<InstrumenterModule> extensions) {
return new Iterator<InstrumenterModule>() {
private Iterator<InstrumenterModule> delegate = initial;
@Override
public boolean hasNext() {
if (delegate.hasNext()) {
return true;
} else if (delegate == initial) {
delegate = extensions.iterator();
return delegate.hasNext();
} else {
return false;
}
}
@Override
public InstrumenterModule next() {
if (hasNext()) {
return delegate.next();
} else {
throw new NoSuchElementException();
}
}
};
}
public static Set<InstrumenterModule.TargetSystem> getEnabledSystems() {
EnumSet<InstrumenterModule.TargetSystem> enabledSystems =
EnumSet.of(InstrumenterModule.TargetSystem.CONTEXT_TRACKING);
InstrumenterConfig cfg = InstrumenterConfig.get();
if (cfg.isTraceEnabled()) {
enabledSystems.add(InstrumenterModule.TargetSystem.TRACING);
}
if (cfg.isProfilingEnabled()) {
enabledSystems.add(InstrumenterModule.TargetSystem.PROFILING);
}
if (cfg.getAppSecActivation() != ProductActivation.FULLY_DISABLED) {
enabledSystems.add(InstrumenterModule.TargetSystem.APPSEC);
}
if (cfg.getIastActivation() != ProductActivation.FULLY_DISABLED) {
enabledSystems.add(InstrumenterModule.TargetSystem.IAST);
}
if (cfg.isRaspEnabled()) {
enabledSystems.add(InstrumenterModule.TargetSystem.RASP);
}
if (cfg.isCiVisibilityEnabled()) {
enabledSystems.add(InstrumenterModule.TargetSystem.CIVISIBILITY);
}
if (cfg.isUsmEnabled()) {
enabledSystems.add(InstrumenterModule.TargetSystem.USM);
}
if (cfg.isLlmObsEnabled()) {
enabledSystems.add(InstrumenterModule.TargetSystem.LLMOBS);
}
return enabledSystems;
}
private static void enableByteBuddyRawTypes() {
temporaryOverride("net.bytebuddy.raw", "true", AgentInstaller::rawTypesEnabled);
}
private static boolean rawTypesEnabled() {
return TypeDescription.AbstractBase.RAW_TYPES; // must avoid touching this before the override
}
private static void disableByteBuddyNexus() {
// disable byte-buddy's Nexus mechanism (we don't need it, and it triggers use of Unsafe)
temporaryOverride("net.bytebuddy.nexus.disabled", "true", AgentInstaller::nexusDisabled);
}
private static boolean nexusDisabled() {
return !NexusAccessor.isAlive(); // must avoid touching this before the override
}
/** Temporarily overrides a system property while checking it's had the intended side effect. */
private static void temporaryOverride(String key, String value, BooleanSupplier sideEffect) {
final String savedPropertyValue = SystemProperties.get(key);
final boolean overridden = SystemProperties.set(key, value);
if (!sideEffect.getAsBoolean() && DEBUG) {
log.debug("Too late to apply {}={}", key, value);
}
if (overridden) {
if (savedPropertyValue == null) {
SystemProperties.clear(key);
} else {
SystemProperties.set(key, savedPropertyValue);
}
}
}
private static AgentBuilder.RedefinitionStrategy.Listener redefinitionStrategyListener(
final Set<InstrumenterModule.TargetSystem> enabledSystems) {
if (enabledSystems.contains(InstrumenterModule.TargetSystem.IAST)) {
return TaintableRedefinitionStrategyListener.INSTANCE;
} else {
return AgentBuilder.RedefinitionStrategy.Listener.NoOp.INSTANCE;
}
}
static class RedefinitionLoggingListener implements AgentBuilder.RedefinitionStrategy.Listener {
private static final Logger log = LoggerFactory.getLogger(RedefinitionLoggingListener.class);
@Override
public void onBatch(final int index, final List<Class<?>> batch, final List<Class<?>> types) {}
@Override
public Iterable<? extends List<Class<?>>> onError(
final int index,
final List<Class<?>> batch,
final Throwable throwable,
final List<Class<?>> types) {
if (DEBUG) {
log.debug("Exception while retransforming {} classes: {}", batch.size(), batch, throwable);
}
return Collections.emptyList();
}
@Override
public void onComplete(
final int amount,
final List<Class<?>> types,
final Map<List<Class<?>>, Throwable> failures) {}
}
static class TransformLoggingListener implements AgentBuilder.Listener {
private static final Logger log = LoggerFactory.getLogger(TransformLoggingListener.class);
@Override
public void onError(
final String typeName,
final ClassLoader classLoader,
final JavaModule module,
final boolean loaded,
final Throwable throwable) {
if (DEBUG) {
log.debug(
"Transformation failed - instrumentation.target.class={} instrumentation.target.classloader={}",
typeName,
classLoader,
throwable);
}
}
@Override
public void onTransformation(
final TypeDescription typeDescription,
final ClassLoader classLoader,
final JavaModule module,
final boolean loaded,
final DynamicType dynamicType) {
if (DEBUG) {
log.debug(
"Transformed - instrumentation.target.class={} instrumentation.target.classloader={}",
typeDescription.getName(),
classLoader);
}
}
@Override
public void onIgnored(
final TypeDescription typeDescription,
final ClassLoader classLoader,
final JavaModule module,
final boolean loaded) {
// log.debug("onIgnored {}", typeDescription.getName());
}
@Override
public void onComplete(
final String typeName,
final ClassLoader classLoader,
final JavaModule module,
final boolean loaded) {
// log.debug("onComplete {}", typeName);
}
@Override
public void onDiscovery(
final String typeName,
final ClassLoader classLoader,
final JavaModule module,
final boolean loaded) {
// log.debug("onDiscovery {}", typeName);
}
}
/**
* Register a callback to run when a class is loading.
*
* <p>Caveats:
*
* <ul>
* <li>This callback will be invoked by a jvm class transformer.
* <li>Classes filtered out by {@link AgentInstaller}'s skip list will not be matched.
* </ul>
*
* @param className name of the class to match against
* @param callback runnable to invoke when class name matches
*/
public static void registerClassLoadCallback(final String className, final Runnable callback) {
if ("java.util.logging.LogManager".equals(className)) {
LOG_MANAGER_CALLBACKS.add(callback);
} else if ("javax.management.MBeanServerBuilder".equals(className)) {
MBEAN_SERVER_BUILDER_CALLBACKS.add(callback);
} else if (DEBUG) {
log.debug("Callback not registered for unexpected class {}", className);
}
}
private static class ClassLoadListener implements AgentBuilder.Listener {
@Override
public void onDiscovery(
final String typeName,
final ClassLoader classLoader,
final JavaModule javaModule,
final boolean b) {}
@Override
public void onTransformation(
final TypeDescription typeDescription,
final ClassLoader classLoader,
final JavaModule javaModule,
final boolean b,
final DynamicType dynamicType) {}
@Override
public void onIgnored(
final TypeDescription typeDescription,
final ClassLoader classLoader,
final JavaModule javaModule,
final boolean b) {}
@Override
public void onError(
final String s,
final ClassLoader classLoader,
final JavaModule javaModule,
final boolean b,
final Throwable throwable) {}
@Override
public void onComplete(
final String typeName,
final ClassLoader classLoader,
final JavaModule javaModule,
final boolean b) {
final List<Runnable> callbacks;
// On Java 17 java.util.logging.LogManager is loaded early even though it's not initialized,
// so wait for the LoggerContext to be initialized before the callbacks are processed.
if ("java.util.logging.LogManager$LoggerContext".equals(typeName)) {
callbacks = LOG_MANAGER_CALLBACKS;
} else if ("javax.management.MBeanServerBuilder".equals(typeName)) {
callbacks = MBEAN_SERVER_BUILDER_CALLBACKS;
} else {
callbacks = null;
}
if (callbacks != null) {
for (final Runnable callback : callbacks) {
callback.run();
}
}
}
}
private AgentInstaller() {}
}