Skip to content

Commit 944c892

Browse files
fix(rmi): Use primitive types for RMI context serialization (#10937)
(cherry picked from commit a0089d4)
1 parent 69635d2 commit 944c892

File tree

2 files changed

+18
-11
lines changed

2 files changed

+18
-11
lines changed

dd-java-agent/agent-bootstrap/src/main/java/datadog/trace/bootstrap/instrumentation/rmi/ContextPayload.java

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
public class ContextPayload {
1818

1919
private static final Logger log = LoggerFactory.getLogger(ContextPayload.class);
20+
private static final int MAX_CONTEXT_SIZE = 1000;
2021
private final Map<String, String> context;
2122
public static final InjectAdapter SETTER = new InjectAdapter();
2223

@@ -39,20 +40,26 @@ public static ContextPayload from(final AgentSpan span) {
3940
}
4041

4142
public static ContextPayload read(final ObjectInput oi) throws IOException {
42-
try {
43-
final Object object = oi.readObject();
44-
if (object instanceof Map) {
45-
return new ContextPayload((Map<String, String>) object);
46-
}
47-
} catch (final ClassCastException | ClassNotFoundException ex) {
48-
log.debug("Error reading object", ex);
43+
final int size = oi.readInt();
44+
if (size < 0 || size > MAX_CONTEXT_SIZE) {
45+
log.debug("Dropping RMI context payload: size {} exceeds maximum {}", size, MAX_CONTEXT_SIZE);
46+
return null;
4947
}
50-
51-
return null;
48+
final Map<String, String> context = new HashMap<>(size * 2);
49+
for (int i = 0; i < size; i++) {
50+
final String key = oi.readUTF();
51+
final String value = oi.readUTF();
52+
context.put(key, value);
53+
}
54+
return new ContextPayload(context);
5255
}
5356

5457
public void write(final ObjectOutput out) throws IOException {
55-
out.writeObject(context);
58+
out.writeInt(context.size());
59+
for (final Map.Entry<String, String> entry : context.entrySet()) {
60+
out.writeUTF(entry.getKey());
61+
out.writeUTF(entry.getValue());
62+
}
5663
}
5764

5865
@ParametersAreNonnullByDefault

dd-java-agent/agent-bootstrap/src/main/java/datadog/trace/bootstrap/instrumentation/rmi/ContextPropagator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public class ContextPropagator {
2020
private static final ObjID REGISTRY_ID = new ObjID(ObjID.REGISTRY_ID);
2121

2222
// RMI object id used to identify DataDog instrumentation
23-
public static final ObjID DD_CONTEXT_CALL_ID = new ObjID("Datadog.v1.context_call".hashCode());
23+
public static final ObjID DD_CONTEXT_CALL_ID = new ObjID("Datadog.v2.context_call".hashCode());
2424

2525
// Operation id used for checking context propagation is possible
2626
// RMI expects these operations to have negative identifier, as positive ones mean legacy

0 commit comments

Comments
 (0)