Skip to content

Commit 81a3d70

Browse files
committed
parser: fix a bug in state expr parsing
When a PC is printed before state expressions, we were not dropping a space correctly, leading to invalid ids like " r0". Fix it by correctly reassigning `rest`.
1 parent fb70474 commit 81a3d70

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

src/parser.test.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import {
1313
BpfAddressSpaceCastInstruction,
1414
InstructionLine,
1515
KnownMessageInfoType,
16+
BpfInstructionClass,
17+
OpcodeSource,
1618
} from "./parser";
1719

1820
const AluInstructionSample = "0: (b7) r2 = 1 ; R2_w=1";
@@ -206,4 +208,50 @@ describe("parser", () => {
206208
expect(parsed.type).toBe(ParsedLineType.UNRECOGNIZED);
207209
});
208210
});
211+
212+
describe("parses exprs with pc", () => {
213+
const str =
214+
"64: (55) if r0 != 0x0 goto pc+6 71: R0=ptr_node_data(non_own_ref,off=16) R7=2 R10=fp0";
215+
it("parses global function valid messages", () => {
216+
const parsed = parseLine(str, 10);
217+
expect(parsed).toMatchObject({
218+
type: ParsedLineType.INSTRUCTION,
219+
idx: 10,
220+
raw: str,
221+
bpfIns: {
222+
pc: 64,
223+
opcode: {
224+
code: BpfJmpCode.JSET,
225+
iclass: BpfInstructionClass.JMP,
226+
source: OpcodeSource.K,
227+
},
228+
cond: { left: { id: "r0" }, right: { id: "IMM" } },
229+
},
230+
});
231+
const insLine = <InstructionLine>parsed;
232+
233+
expect(insLine.bpfStateExprs.length).toBe(3);
234+
expect(insLine.bpfStateExprs[0]).toEqual({
235+
id: "r0",
236+
value: "ptr_node_data(non_own_ref,off=16)",
237+
rawKey: "R0",
238+
frame: 0,
239+
pc: 71,
240+
});
241+
expect(insLine.bpfStateExprs[1]).toEqual({
242+
id: "r7",
243+
value: "2",
244+
rawKey: "R7",
245+
frame: 0,
246+
pc: 71,
247+
});
248+
expect(insLine.bpfStateExprs[2]).toEqual({
249+
id: "r10",
250+
value: "fp-0",
251+
rawKey: "R10",
252+
frame: 0,
253+
pc: 71,
254+
});
255+
});
256+
});
209257
});

src/parser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,8 +354,8 @@ export const parseBpfStateExprs = (
354354
let frameId = 0;
355355
if (frame.match) {
356356
frameId = parseInt(frame.match[1], 10);
357-
rest = frame.rest;
358357
}
358+
rest = frame.rest;
359359

360360
let exprs = [];
361361
while (rest.length > 0) {

0 commit comments

Comments
 (0)