Skip to content

asm: allow negative constants for builtin function calls #1806

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

Merged
merged 1 commit into from
Jun 24, 2025
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
14 changes: 10 additions & 4 deletions asm/instruction.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,17 @@ func (ins *Instruction) Unmarshal(r io.Reader, bo binary.ByteOrder, platform str
ins.Constant = int64(int32(bo.Uint32(data[4:8])))

if ins.IsBuiltinCall() {
fn, err := BuiltinFuncForPlatform(platform, uint32(ins.Constant))
if err != nil {
return err
if ins.Constant >= 0 {
// Leave negative constants from the instruction stream
// unchanged. These are sometimes used as placeholders for later
// patching.
// This relies on not having a valid platform tag with a high bit set.
fn, err := BuiltinFuncForPlatform(platform, uint32(ins.Constant))
if err != nil {
return err
}
ins.Constant = int64(fn)
}
ins.Constant = int64(fn)
} else if ins.OpCode.Class().IsALU() {
switch ins.OpCode.ALUOp() {
case Div:
Expand Down
10 changes: 10 additions & 0 deletions asm/instruction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,16 @@ func TestInstructionWithMetadata(t *testing.T) {
}
}

func TestReadCallToNegativeOne(t *testing.T) {
raw := []byte{
0x85, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
}
var ins Instruction
err := ins.Unmarshal(bytes.NewReader(raw), binary.LittleEndian, platform.Linux)
qt.Assert(t, qt.IsNil(err))
qt.Assert(t, qt.Equals(ins.Constant, -1))
}

// You can use format flags to change the way an eBPF
// program is stringified.
func ExampleInstructions_Format() {
Expand Down
2 changes: 1 addition & 1 deletion internal/platform/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const (
)

const (
platformMax = 0xf
platformMax = 1<<3 - 1 // most not exceed 3 bits to avoid setting the high bit
platformShift = 28
platformMask = platformMax << platformShift
)
Expand Down
2 changes: 1 addition & 1 deletion internal/platform/constants_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

func TestConstant(t *testing.T) {
const maxConstant = ^uint32(platformMask)
const maxConstant = uint32(1<<platformShift - 1)
for _, plat := range []string{
Linux,
Windows,
Expand Down
Loading