|
| 1 | +/* |
| 2 | + * ApplicationInsights-Java |
| 3 | + * Copyright (c) Microsoft Corporation |
| 4 | + * All rights reserved. |
| 5 | + * |
| 6 | + * MIT License |
| 7 | + * Permission is hereby granted, free of charge, to any person obtaining a copy of this |
| 8 | + * software and associated documentation files (the ""Software""), to deal in the Software |
| 9 | + * without restriction, including without limitation the rights to use, copy, modify, merge, |
| 10 | + * publish, distribute, sublicense, and/or sell copies of the Software, and to permit |
| 11 | + * persons to whom the Software is furnished to do so, subject to the following conditions: |
| 12 | + * The above copyright notice and this permission notice shall be included in all copies or |
| 13 | + * substantial portions of the Software. |
| 14 | + * THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, |
| 15 | + * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR |
| 16 | + * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE |
| 17 | + * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
| 18 | + * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 19 | + * DEALINGS IN THE SOFTWARE. |
| 20 | + */ |
| 21 | +package com.microsoft.applicationinsights.agent.internal; |
| 22 | + |
| 23 | +import java.lang.instrument.ClassFileTransformer; |
| 24 | +import java.security.ProtectionDomain; |
| 25 | +import java.util.HashSet; |
| 26 | +import java.util.Set; |
| 27 | + |
| 28 | +import net.bytebuddy.jar.asm.ClassReader; |
| 29 | +import net.bytebuddy.jar.asm.ClassVisitor; |
| 30 | +import net.bytebuddy.jar.asm.ClassWriter; |
| 31 | +import net.bytebuddy.jar.asm.MethodVisitor; |
| 32 | +import org.checkerframework.checker.nullness.qual.Nullable; |
| 33 | +import org.slf4j.Logger; |
| 34 | +import org.slf4j.LoggerFactory; |
| 35 | + |
| 36 | +import static net.bytebuddy.jar.asm.Opcodes.ASM7; |
| 37 | +import static net.bytebuddy.jar.asm.Opcodes.RETURN; |
| 38 | + |
| 39 | +public class DuplicateAgentClassFileTransformer implements ClassFileTransformer { |
| 40 | + |
| 41 | + private static final Logger logger = LoggerFactory.getLogger(DuplicateAgentClassFileTransformer.class); |
| 42 | + |
| 43 | + // using constant here so that it will NOT get shaded |
| 44 | + // IMPORTANT FOR THIS NOT TO BE FINAL (or private) |
| 45 | + // OTHERWISE COMPILER COULD THEORETICALLY INLINE IT BELOW AND APPLY .substring(1) |
| 46 | + // and then it WOULD be shaded |
| 47 | + static String[] UNSHADED_CLASS_NAMES = new String[] { |
| 48 | + "!io/opentelemetry/auto/bootstrap/AgentBootstrap", // early 3.0 previews |
| 49 | + "!com/microsoft/applicationinsights/agent/internal/Premain", // 2.5.0+ |
| 50 | + "!com/microsoft/applicationinsights/agent/internal/agent/AgentImplementation" // prior to 2.5.0 |
| 51 | + }; |
| 52 | + |
| 53 | + private final Set<String> unshadedClassNames; |
| 54 | + |
| 55 | + public DuplicateAgentClassFileTransformer() { |
| 56 | + Set<String> unshadedClassNames = new HashSet<>(); |
| 57 | + for (String unshadedClassName : UNSHADED_CLASS_NAMES) { |
| 58 | + unshadedClassNames.add(unshadedClassName.substring(1)); |
| 59 | + } |
| 60 | + this.unshadedClassNames = unshadedClassNames; |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public byte /*@Nullable*/[] transform(@Nullable ClassLoader loader, @Nullable String className, |
| 65 | + @Nullable Class<?> classBeingRedefined, |
| 66 | + @Nullable ProtectionDomain protectionDomain, |
| 67 | + byte[] classfileBuffer) { |
| 68 | + |
| 69 | + if (!unshadedClassNames.contains(className)) { |
| 70 | + return null; |
| 71 | + } |
| 72 | + try { |
| 73 | + ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS); |
| 74 | + ClassVisitor cv = new DuplicateAgentClassVisitor(cw); |
| 75 | + ClassReader cr = new ClassReader(classfileBuffer); |
| 76 | + cr.accept(cv, 0); |
| 77 | + return cw.toByteArray(); |
| 78 | + } catch (Throwable t) { |
| 79 | + logger.error(t.getMessage(), t); |
| 80 | + return null; |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + private static class DuplicateAgentClassVisitor extends ClassVisitor { |
| 85 | + |
| 86 | + private final ClassWriter cw; |
| 87 | + |
| 88 | + private DuplicateAgentClassVisitor(ClassWriter cw) { |
| 89 | + super(ASM7, cw); |
| 90 | + this.cw = cw; |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + public MethodVisitor visitMethod(int access, String name, String descriptor, @Nullable String signature, |
| 95 | + String /*@Nullable*/[] exceptions) { |
| 96 | + MethodVisitor mv = cw.visitMethod(access, name, descriptor, signature, exceptions); |
| 97 | + if (name.equals("premain") && descriptor.equals("(Ljava/lang/String;Ljava/lang/instrument/Instrumentation;)V")) { |
| 98 | + // no-op the initialize() method |
| 99 | + mv.visitCode(); |
| 100 | + mv.visitInsn(RETURN); |
| 101 | + mv.visitMaxs(0, 1); |
| 102 | + mv.visitEnd(); |
| 103 | + return null; |
| 104 | + } else { |
| 105 | + return mv; |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | +} |
0 commit comments