|
1 | 1 | package datadog.trace.agent.tooling;
|
2 | 2 |
|
| 3 | +import static java.util.Arrays.asList; |
| 4 | +import static java.util.Collections.emptyList; |
| 5 | + |
3 | 6 | import datadog.trace.api.cache.DDCache;
|
4 | 7 | import datadog.trace.api.cache.DDCaches;
|
| 8 | +import java.util.HashMap; |
| 9 | +import java.util.List; |
5 | 10 | import java.util.Map;
|
6 | 11 | import net.bytebuddy.jar.asm.ClassReader;
|
7 | 12 | import net.bytebuddy.jar.asm.ClassVisitor;
|
|
10 | 15 | import net.bytebuddy.jar.asm.commons.Remapper;
|
11 | 16 |
|
12 | 17 | /** 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); |
| 18 | +public final class AdviceShader { |
| 19 | + private final Map<String, String> relocations; |
| 20 | + private final List<String> helperNames; |
15 | 21 |
|
16 |
| - /** Flattened sequence of old-prefix, new-prefix relocations. */ |
17 |
| - private final String[] prefixes; |
| 22 | + private volatile Remapper remapper; |
18 | 23 |
|
19 |
| - public static AdviceShader with(Map<String, String> relocations) { |
20 |
| - return relocations != null ? new AdviceShader(relocations) : null; |
| 24 | + /** |
| 25 | + * Used when installing {@link InstrumenterModule}s. Ensures any injected helpers have unique |
| 26 | + * names so the original and relocated modules can inject helpers into the same class-loader. |
| 27 | + */ |
| 28 | + public static AdviceShader with(InstrumenterModule module) { |
| 29 | + if (module.adviceShading() != null) { |
| 30 | + return new AdviceShader(module.adviceShading(), asList(module.helperClassNames())); |
| 31 | + } |
| 32 | + return null; |
21 | 33 | }
|
22 | 34 |
|
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 |
| - } |
| 35 | + /** Used to generate and check muzzle references. Only applies relocations declared in modules. */ |
| 36 | + public static AdviceShader with(Map<String, String> relocations) { |
| 37 | + if (relocations != null) { |
| 38 | + return new AdviceShader(relocations, emptyList()); |
38 | 39 | }
|
| 40 | + return null; |
| 41 | + } |
| 42 | + |
| 43 | + private AdviceShader(Map<String, String> relocations, List<String> helperNames) { |
| 44 | + this.relocations = relocations; |
| 45 | + this.helperNames = helperNames; |
39 | 46 | }
|
40 | 47 |
|
41 | 48 | /** Applies shading before calling the given {@link ClassVisitor}. */
|
42 |
| - public ClassVisitor shade(ClassVisitor cv) { |
43 |
| - return new ClassRemapper(cv, this); |
| 49 | + public ClassVisitor shadeClass(ClassVisitor cv) { |
| 50 | + if (null == remapper) { |
| 51 | + remapper = new AdviceMapper(); |
| 52 | + } |
| 53 | + return new ClassRemapper(cv, remapper); |
44 | 54 | }
|
45 | 55 |
|
46 | 56 | /** Returns the result of shading the given bytecode. */
|
47 |
| - public byte[] shade(byte[] bytecode) { |
| 57 | + public byte[] shadeClass(byte[] bytecode) { |
48 | 58 | ClassReader cr = new ClassReader(bytecode);
|
49 | 59 | ClassWriter cw = new ClassWriter(null, 0);
|
50 |
| - cr.accept(shade(cw), 0); |
| 60 | + cr.accept(shadeClass(cw), 0); |
51 | 61 | return cw.toByteArray();
|
52 | 62 | }
|
53 | 63 |
|
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 |
| 64 | + /** Generates a unique shaded name for the given helper. */ |
| 65 | + public String uniqueHelper(String dottedName) { |
| 66 | + int packageEnd = dottedName.lastIndexOf('.'); |
| 67 | + if (packageEnd > 0) { |
| 68 | + return dottedName.substring(0, packageEnd + 1) + "shaded" + dottedName.substring(packageEnd); |
60 | 69 | }
|
61 |
| - return cache.computeIfAbsent(internalName, this::shade); |
| 70 | + return dottedName; |
62 | 71 | }
|
63 | 72 |
|
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); |
| 73 | + final class AdviceMapper extends Remapper { |
| 74 | + private final DDCache<String, String> mappingCache = DDCaches.newFixedSizeCache(64); |
| 75 | + |
| 76 | + /** Flattened sequence of old-prefix, new-prefix relocations. */ |
| 77 | + private final String[] prefixes; |
| 78 | + |
| 79 | + private final Map<String, String> helperMapping; |
| 80 | + |
| 81 | + AdviceMapper() { |
| 82 | + // record the unique names that we've given to injected helpers |
| 83 | + this.helperMapping = new HashMap<>(helperNames.size() + 1, 1f); |
| 84 | + for (String h : helperNames) { |
| 85 | + this.helperMapping.put(h.replace('.', '/'), uniqueHelper(h).replace('.', '/')); |
| 86 | + } |
| 87 | + // convert relocations to a flattened sequence: old-prefix, new-prefix, etc. |
| 88 | + this.prefixes = new String[relocations.size() * 2]; |
| 89 | + int i = 0; |
| 90 | + for (Map.Entry<String, String> e : relocations.entrySet()) { |
| 91 | + String oldPrefix = e.getKey(); |
| 92 | + String newPrefix = e.getValue(); |
| 93 | + if (oldPrefix.indexOf('.') > 0) { |
| 94 | + // accept dotted prefixes, but store them in their internal form |
| 95 | + this.prefixes[i++] = oldPrefix.replace('.', '/'); |
| 96 | + this.prefixes[i++] = newPrefix.replace('.', '/'); |
| 97 | + } else { |
| 98 | + this.prefixes[i++] = oldPrefix; |
| 99 | + this.prefixes[i++] = newPrefix; |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public String map(String internalName) { |
| 106 | + String uniqueName = helperMapping.get(internalName); |
| 107 | + if (uniqueName != null) { |
| 108 | + return uniqueName; |
| 109 | + } |
| 110 | + if (internalName.startsWith("java/") |
| 111 | + || internalName.startsWith("datadog/") |
| 112 | + || internalName.startsWith("net/bytebuddy/")) { |
| 113 | + return internalName; // never shade these references |
| 114 | + } |
| 115 | + return mappingCache.computeIfAbsent(internalName, this::shadeInternalName); |
| 116 | + } |
| 117 | + |
| 118 | + @Override |
| 119 | + public Object mapValue(Object value) { |
| 120 | + if (value instanceof String) { |
| 121 | + String text = (String) value; |
| 122 | + if (text.isEmpty()) { |
| 123 | + return text; |
| 124 | + } else if (text.indexOf('.') > 0) { |
| 125 | + return shadeDottedName(text); |
| 126 | + } else { |
| 127 | + return shadeInternalName(text); |
| 128 | + } |
72 | 129 | } else {
|
73 |
| - return shade(text); |
| 130 | + return super.mapValue(value); |
74 | 131 | }
|
75 |
| - } else { |
76 |
| - return super.mapValue(value); |
77 | 132 | }
|
78 |
| - } |
79 | 133 |
|
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()); |
| 134 | + private String shadeInternalName(String internalName) { |
| 135 | + for (int i = 0; i < prefixes.length; i += 2) { |
| 136 | + if (internalName.startsWith(prefixes[i])) { |
| 137 | + return prefixes[i + 1] + internalName.substring(prefixes[i].length()); |
| 138 | + } |
84 | 139 | }
|
| 140 | + return internalName; |
85 | 141 | }
|
86 |
| - return internalName; |
87 |
| - } |
88 | 142 |
|
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()); |
| 143 | + private String shadeDottedName(String name) { |
| 144 | + String internalName = name.replace('.', '/'); |
| 145 | + for (int i = 0; i < prefixes.length; i += 2) { |
| 146 | + if (internalName.startsWith(prefixes[i])) { |
| 147 | + return prefixes[i + 1].replace('/', '.') + name.substring(prefixes[i].length()); |
| 148 | + } |
94 | 149 | }
|
| 150 | + return name; |
95 | 151 | }
|
96 |
| - return name; |
97 | 152 | }
|
98 | 153 | }
|
0 commit comments