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

Behavior around signed imm values and 64bit ALU operations #10

Open
Alan-Jowett opened this issue Oct 4, 2022 · 2 comments
Open

Behavior around signed imm values and 64bit ALU operations #10

Alan-Jowett opened this issue Oct 4, 2022 · 2 comments

Comments

@Alan-Jowett
Copy link
Collaborator

Assume:

mov r0, -10

Byte sequence:

b7 00 00 00 f6 f6 ff ff

What value should r0 contain?
0x00000000FFFFFFF6 or 0xFFFFFFFFFFFFFFF6

Should there be a sign extend operation when loading the 32bit immediate value into the 64bit unsigned register?

@yesh0
Copy link

yesh0 commented Dec 1, 2022

According to the Linux interpreter, yes, it should be sign-extended. (And so do all the other ALU64 operations except shifts.)

https://github.com/torvalds/linux/blob/ef4d3ea40565a781c25847e9cb96c1bd9f462bc6/kernel/bpf/core.c#L1734-L1736

ALU64_MOV_K:
    DST = IMM;
//  // or rather
//  regs[insn->dst_reg] = insn->imm;
//  // assigning imm (i32) to regs[...] (u64) does sign-bit extension in c

@Alan-Jowett
Copy link
Collaborator Author

Thanks @yesh0, appreciate the feedback.

We now have a test to measure this behavior on both Linux and other BPF runtimes.

This issue was to add clarity to the doc as I felt it wasn't super clear.

dthaler added a commit that referenced this issue Jul 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants