Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix duplicated locals with arguments #7683

Merged
merged 1 commit into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,7 @@ private Collection<LocalVariableNode> initAndHoistLocalVars(InsnList insnList) {
}
checkHoistableLocalVar(localVar, localVarsByName, localVarsBySlot, hoistableVarByName);
}
removeDuplicatesFromArgs(hoistableVarByName, localVarsBySlotArray);
// hoist vars
List<LocalVariableNode> results = new ArrayList<>();
for (Map.Entry<String, List<LocalVariableNode>> entry : hoistableVarByName.entrySet()) {
Expand Down Expand Up @@ -512,6 +513,19 @@ private Collection<LocalVariableNode> initAndHoistLocalVars(InsnList insnList) {
return results;
}

private void removeDuplicatesFromArgs(
Map<String, List<LocalVariableNode>> hoistableVarByName,
LocalVariableNode[] localVarsBySlotArray) {
for (int idx = 0; idx < argOffset; idx++) {
LocalVariableNode localVar = localVarsBySlotArray[idx];
if (localVar == null) {
continue;
}
// we are removing local variables that are arguments
hoistableVarByName.remove(localVar.name);
}
}

private LabelNode findLatestLabel(InsnList instructions, Set<LabelNode> endLabels) {
for (AbstractInsnNode insn = instructions.getLast(); insn != null; insn = insn.getPrevious()) {
if (insn instanceof LabelNode && endLabels.contains(insn)) {
Expand Down Expand Up @@ -762,8 +776,8 @@ private void collectArguments(InsnList insnList, Snapshot.Kind kind) {
int slot = isStatic ? 0 : 1;
for (Type argType : argTypes) {
String currentArgName = null;
if (slot < localVarsBySlot.length) {
LocalVariableNode localVarNode = localVarsBySlot[slot];
if (slot < localVarsBySlotArray.length) {
LocalVariableNode localVarNode = localVarsBySlotArray[slot];
currentArgName = localVarNode != null ? localVarNode.name : null;
}
if (currentArgName == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public abstract class Instrumentor {
protected final LabelNode methodEnterLabel;
protected int localVarBaseOffset;
protected int argOffset;
protected final LocalVariableNode[] localVarsBySlot;
protected final LocalVariableNode[] localVarsBySlotArray;
protected LabelNode returnHandlerLabel;
protected final List<CapturedContextInstrumentor.FinallyBlock> finallyBlocks = new ArrayList<>();

Expand All @@ -68,7 +68,7 @@ public Instrumentor(
for (Type t : argTypes) {
argOffset += t.getSize();
}
localVarsBySlot = extractLocalVariables(argTypes);
localVarsBySlotArray = extractLocalVariables(argTypes);
}

public abstract InstrumentationResult.Status instrument();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -697,8 +697,8 @@ private ASMHelper.Type tryRetrieveArgument(String head, InsnList insnList) {
int slot = instrumentor.isStatic ? 0 : 1;
for (Type argType : argTypes) {
String currentArgName = null;
if (instrumentor.localVarsBySlot.length > 0) {
LocalVariableNode localVarNode = instrumentor.localVarsBySlot[slot];
if (instrumentor.localVarsBySlotArray.length > 0) {
LocalVariableNode localVarNode = instrumentor.localVarsBySlotArray[slot];
currentArgName = localVarNode != null ? localVarNode.name : null;
}
if (currentArgName == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import com.datadog.debugger.agent.JsonSnapshotSerializer;
Expand Down Expand Up @@ -99,6 +100,8 @@ void testTracerSameMethod() throws Exception {
Snapshot snapshot = request.getDebugger().getSnapshot();
assertEquals(PROBE_ID.getId(), snapshot.getProbe().getId());
assertEquals(42, snapshot.getCaptures().getEntry().getArguments().get("argInt").getValue());
// no locals captured
assertNull(snapshot.getCaptures().getEntry().getLocals());
assertTrue(Pattern.matches("[0-9a-f]+", request.getTraceId()));
assertTrue(Pattern.matches("\\d+", request.getSpanId()));
}
Expand Down