|
| 1 | +package datadog.trace.agent.tooling; |
| 2 | + |
| 3 | +import datadog.trace.api.cache.DDCache; |
| 4 | +import datadog.trace.api.cache.DDCaches; |
| 5 | +import java.util.Map; |
| 6 | +import net.bytebuddy.jar.asm.ClassReader; |
| 7 | +import net.bytebuddy.jar.asm.ClassVisitor; |
| 8 | +import net.bytebuddy.jar.asm.ClassWriter; |
| 9 | +import net.bytebuddy.jar.asm.commons.ClassRemapper; |
| 10 | +import net.bytebuddy.jar.asm.commons.Remapper; |
| 11 | + |
| 12 | +/** Shades advice bytecode by applying relocations to all references. */ |
| 13 | +public final class AdviceShader extends Remapper { |
| 14 | + private final DDCache<String, String> cache = DDCaches.newFixedSizeCache(64); |
| 15 | + |
| 16 | + /** Flattened sequence of old-prefix, new-prefix relocations. */ |
| 17 | + private final String[] prefixes; |
| 18 | + |
| 19 | + public static AdviceShader with(Map<String, String> relocations) { |
| 20 | + return relocations != null ? new AdviceShader(relocations) : null; |
| 21 | + } |
| 22 | + |
| 23 | + AdviceShader(Map<String, String> relocations) { |
| 24 | + // convert relocations to a flattened sequence: old-prefix, new-prefix, etc. |
| 25 | + this.prefixes = new String[relocations.size() * 2]; |
| 26 | + int i = 0; |
| 27 | + for (Map.Entry<String, String> e : relocations.entrySet()) { |
| 28 | + String oldPrefix = e.getKey(); |
| 29 | + String newPrefix = e.getValue(); |
| 30 | + if (oldPrefix.indexOf('.') > 0) { |
| 31 | + // accept dotted prefixes, but store them in their internal form |
| 32 | + this.prefixes[i++] = oldPrefix.replace('.', '/'); |
| 33 | + this.prefixes[i++] = newPrefix.replace('.', '/'); |
| 34 | + } else { |
| 35 | + this.prefixes[i++] = oldPrefix; |
| 36 | + this.prefixes[i++] = newPrefix; |
| 37 | + } |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + /** Applies shading before calling the given {@link ClassVisitor}. */ |
| 42 | + public ClassVisitor shade(ClassVisitor cv) { |
| 43 | + return new ClassRemapper(cv, this); |
| 44 | + } |
| 45 | + |
| 46 | + /** Returns the result of shading the given bytecode. */ |
| 47 | + public byte[] shade(byte[] bytecode) { |
| 48 | + ClassReader cr = new ClassReader(bytecode); |
| 49 | + ClassWriter cw = new ClassWriter(null, 0); |
| 50 | + cr.accept(shade(cw), 0); |
| 51 | + return cw.toByteArray(); |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public String map(String internalName) { |
| 56 | + if (internalName.startsWith("java/") |
| 57 | + || internalName.startsWith("datadog/") |
| 58 | + || internalName.startsWith("net/bytebuddy/")) { |
| 59 | + return internalName; // never shade these references |
| 60 | + } |
| 61 | + return cache.computeIfAbsent(internalName, this::shade); |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public Object mapValue(Object value) { |
| 66 | + if (value instanceof String) { |
| 67 | + String text = (String) value; |
| 68 | + if (text.isEmpty()) { |
| 69 | + return text; |
| 70 | + } else if (text.indexOf('.') > 0) { |
| 71 | + return shadeDottedName(text); |
| 72 | + } else { |
| 73 | + return shade(text); |
| 74 | + } |
| 75 | + } else { |
| 76 | + return super.mapValue(value); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + private String shade(String internalName) { |
| 81 | + for (int i = 0; i < prefixes.length; i += 2) { |
| 82 | + if (internalName.startsWith(prefixes[i])) { |
| 83 | + return prefixes[i + 1] + internalName.substring(prefixes[i].length()); |
| 84 | + } |
| 85 | + } |
| 86 | + return internalName; |
| 87 | + } |
| 88 | + |
| 89 | + private String shadeDottedName(String name) { |
| 90 | + String internalName = name.replace('.', '/'); |
| 91 | + for (int i = 0; i < prefixes.length; i += 2) { |
| 92 | + if (internalName.startsWith(prefixes[i])) { |
| 93 | + return prefixes[i + 1].replace('/', '.') + name.substring(prefixes[i].length()); |
| 94 | + } |
| 95 | + } |
| 96 | + return name; |
| 97 | + } |
| 98 | +} |
0 commit comments