Implement RFC 9669 MEMSX sign-extension load instructions (#701)
* Initial plan
* Implement RFC 9669 MEMSX sign-extension load instructions
- Added EBPF_MODE_MEMSX (0x80) constant
- Added LDXBSX, LDXHSX, LDXWSX opcodes for sign-extension loads
- Implemented ubpf_mem_load_sx() for VM interpreter
- Added JIT support for x86-64 with emit_load_sx()
- Added JIT support for ARM64 using existing LS_LDRSB/H/W opcodes
- Added instruction validation filters
Co-authored-by: Alan-Jowett <20480683+Alan-Jowett@users.noreply.github.com>
* Add assembler/disassembler support and comprehensive tests for MEMSX
- Added MEM_LOAD_SX_OPS to assembler.py for ldx{b,h,w}sx instructions
- Updated disassembler.py to recognize MEMSX mode (mode 4)
- Fixed x86-64 JIT emit_load_sx for proper 32-bit sign extension
- Added comprehensive test program validating VM and JIT for all MEMSX variants
- All tests pass for byte, halfword, and word sign extensions
Co-authored-by: Alan-Jowett <20480683+Alan-Jowett@users.noreply.github.com>
* Remove test binary from repository
* Fix pointer dereference spacing in ubpf_mem_load_sx
- Fixed spacing in pointer dereferences to avoid precedence issues
- All tests still pass after the fix
Co-authored-by: Alan-Jowett <20480683+Alan-Jowett@users.noreply.github.com>
* Update .gitignore to exclude test binaries and CodeQL artifacts
* Update .gitignore to exclude test binaries and CodeQL artifacts
Co-authored-by: Alan-Jowett <20480683+Alan-Jowett@users.noreply.github.com>
* Fix .gitignore: preserve .vs/ line and add test artifacts separately
- Keep original .vs/ line unchanged
- Add test_memsx and _codeql_detected_source_root on separate lines
- Add explanatory comment for test artifacts
Co-authored-by: Alan-Jowett <20480683+Alan-Jowett@users.noreply.github.com>
* Remove test_memsx.c development artifact
- test_memsx.c was a temporary test file used during development
- Proper tests are in tests/*.data files (ldxbsx.data, ldxhsx.data, ldxwsx.data)
- Not needed in the final PR
Co-authored-by: Alan-Jowett <20480683+Alan-Jowett@users.noreply.github.com>
* Add patch and documentation for bpf_conformance MEMSX support
- Created bpf_conformance_memsx.patch with changes needed for the assembler
- Added MEMSX_BPF_CONFORMANCE.md explaining the CI test failures
- The bpf_conformance submodule needs these changes to recognize MEMSX mnemonics
- Python assembler (ubpf/assembler.py) already has MEMSX support
Co-authored-by: Alan-Jowett <20480683+Alan-Jowett@users.noreply.github.com>
* Fix MEMSX mnemonic naming to match RFC 9669 standard
- Changed mnemonics from ldxbsx/ldxhsx/ldxwsx to ldxsb/ldxsh/ldxsw
- Updated test files to use correct naming (ldxsb.data, ldxsh.data, ldxsw.data)
- Updated Python assembler to generate ldxsb/ldxsh/ldxsw mnemonics
- Updated disassembler to show ldxsb/ldxsh/ldxsw (not ldxbsx/etc)
- Removed obsolete documentation files (now compatible with bpf_conformance PR #435)
- Naming now matches Linux kernel and RFC 9669 conventions
Co-authored-by: Alan-Jowett <20480683+Alan-Jowett@users.noreply.github.com>
---------
Signed-off-by: Alan Jowett <alanjo@microsoft.com>
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: Alan-Jowett <20480683+Alan-Jowett@users.noreply.github.com>
Co-authored-by: Alan Jowett <alanjo@microsoft.com>Userspace eBPF VM
This project aims to create an Apache-licensed library for executing eBPF programs. The primary implementation of eBPF lives in the Linux kernel, but due to its GPL license it can't be used in many projects.
BPF Instruction Set Architecture (ISA) - RFC 9669
Linux documentation for the eBPF instruction set
This project includes an eBPF assembler, disassembler, interpreter (for all platforms), and JIT compiler (for x86-64 and Arm64 targets).
Before following any of the instructions below for building, testing, contributing, etc, please be sure to properly check out the source code which requires properly initializing submodules:
git submodule update --init --recursive
In order to prepare your system to successfully generate the build system using CMake, follow the platform-specific instructions below.
Building, compiling and testing on Windows requires an installation of Visual Studio (not VS Code -- the MSVC compiler is necessary!).
Note: There are free-to-use versions of Visual Studio for individual developers. These versions are known as the community version.
You can build, compile and test uBPF using VS Code but Visual Studio is still required.
The other requirement is that you have nuget.exe in your PATH. You can determine if your host meets this criteria by testing whether
> nuget.exe
produces output about how to execute the program. With nuget.exe installed, the cmake configuration system will download all the required developer libraries as it configures the build system.
First, make sure that you have the XCode Command Line Tools installed:
$ xcode-select --install
Installing the XCode Command Linux Tools will install Apple's version of the Clang compiler and other developer-support tools.
uBpf requires that your host have several support libraries installed. The easiest way to configure your host to meet these requirements,
$ brew install boost
$ brew install llvm cmake $ brew install clang-format
Installing LLVM from Homebrew is optional for developing and using uBPF on macOS. It is required if you plan on compiling/creating eBPF programs by compiling LLVM and storing them in ELF files. If you do install LLVM from Homebrew, add -DUBPF_ALTERNATE_LLVM_PATH=/opt/homebrew/opt/llvm/bin to the cmake configuration command:
cmake -S . -B build -DUBPF_ENABLE_TESTS=true -DUBPF_ALTERNATE_LLVM_PATH=/opt/homebrew/opt/llvm/bin
./scripts/build-libbpf.sh
A build system for compiling and testing ubpf is generated for Windows, Linux and macOS platforms using cmake:
cmake -S . -B build -DUBPF_ENABLE_TESTS=true cmake --build build --config Debug
cmake --build build --target test --
ctest --test-dir build
We love contributions!
We aim to maintain code coverage with every code change. The CI/CD pipeline will verify this invariant as part of the contribution process. However, you can calculate code coverage locally by
coveralls --gcov-options '\-lp' -i $PWD/vm/ubpf_vm.c -i $PWD/vm/ubpf_jit_x86_64.c -i $PWD/vm/ubpf_loader.c
We also aim to maintain a consistent code format. The pre-commit git hooks configured for the uBPF repository will guarantee that code changes match the format we expect. In order for those hooks to work effectively, you must have clang-format installed and available on your system.
You'll need Clang 3.7.
clang-3.7 -O2 -target bpf -c prog.c -o prog.o
You can then pass the contents of prog.o to ubpf_load_elf, or to the stdin of the vm/test binary.
Copyright 2015, Big Switch Networks, Inc. Licensed under the Apache License, Version 2.0 <LICENSE.txt or http://www.apache.org/licenses/LICENSE-2.0>.