From e02bf88c37a03dfd2af35a8a56c412a3a938c377 Mon Sep 17 00:00:00 2001 From: Marcus Wichelmann Date: Wed, 3 Apr 2024 17:53:53 +0200 Subject: [PATCH 1/2] btf: synthesise instruction comments into line info When an asm.Comment is added to an instruction, existing source information like btf.Line will be replaced. But in some cases, instructions without line information won't pass the verifier. The expected behavior would be, that the comment gets synthesized into a line info where everything but the line string is zero. This commit implements this. Fixes #1413 Signed-off-by: Marcus Wichelmann --- btf/ext_info.go | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/btf/ext_info.go b/btf/ext_info.go index d5652bad5..eb9044bad 100644 --- a/btf/ext_info.go +++ b/btf/ext_info.go @@ -142,12 +142,13 @@ func AssignMetadataToInstructions( // MarshalExtInfos encodes function and line info embedded in insns into kernel // wire format. +// +// If an instruction has an [asm.Comment], it will be synthesized into a mostly +// empty line info. func MarshalExtInfos(insns asm.Instructions, b *Builder) (funcInfos, lineInfos []byte, _ error) { iter := insns.Iterate() for iter.Next() { - _, ok := iter.Ins.Source().(*Line) - fn := FuncMetadata(iter.Ins) - if ok || fn != nil { + if iter.Ins.Source() != nil || FuncMetadata(iter.Ins) != nil { goto marshal } } @@ -167,7 +168,16 @@ marshal: } } - if line, ok := iter.Ins.Source().(*Line); ok { + if source := iter.Ins.Source(); source != nil { + var line *Line + if l, ok := source.(*Line); ok { + line = l + } else { + line = &Line{ + line: source.String(), + } + } + li := &lineInfo{ line: line, offset: iter.Offset, From 3ccdc4fe5471a35ea7c50472469ae0de3ba77b2b Mon Sep 17 00:00:00 2001 From: Marcus Wichelmann Date: Wed, 3 Apr 2024 18:00:17 +0200 Subject: [PATCH 2/2] btf: use asm.Comment to annotate poisoned CO-RE relocations instead Signed-off-by: Marcus Wichelmann --- btf/core.go | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/btf/core.go b/btf/core.go index d53cf9168..f3b7e039e 100644 --- a/btf/core.go +++ b/btf/core.go @@ -57,13 +57,9 @@ func (f *COREFixup) Apply(ins *asm.Instruction) error { // Add context to the kernel verifier output. if source := ins.Source(); source != nil { - *ins = ins.WithSource(&Line{ - line: fmt.Sprintf("instruction poisoned by CO-RE: %s", source), - }) + *ins = ins.WithSource(asm.Comment(fmt.Sprintf("instruction poisoned by CO-RE: %s", source))) } else { - *ins = ins.WithSource(&Line{ - line: "instruction poisoned by CO-RE", - }) + *ins = ins.WithSource(asm.Comment("instruction poisoned by CO-RE")) } return nil