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

aya: convert bpf programs to assembly #1000

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ jobs:
--exclude aya-log-ebpf \
--exclude integration-ebpf \
--exclude integration-test \
--workspace
--workspace -- --nocapture

- name: Doctests
env:
Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ rbpf = { version = "0.2.0", default-features = false }
rustdoc-json = { version = "0.9.0", default-features = false }
rustup-toolchain = { version = "0.1.5", default-features = false }
rustversion = { version = "1.0.0", default-features = false }
solana_rbpf = { version = "0.8.2", default-features = false }
syn = { version = "2", default-features = false }
tempfile = { version = "3", default-features = false }
test-case = { version = "3.1.0", default-features = false }
Expand Down
1 change: 1 addition & 0 deletions aya/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ lazy_static = { workspace = true }
libc = { workspace = true }
log = { workspace = true }
object = { workspace = true, features = ["elf", "read_core", "std", "write"] }
solana_rbpf = { workspace = true }
thiserror = { workspace = true }
tokio = { workspace = true, features = ["rt"], optional = true }

Expand Down
143 changes: 134 additions & 9 deletions aya/src/sys/bpf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use obj::{
maps::{bpf_map_def, LegacyMap},
EbpfSectionKind, VerifierLog,
};
use solana_rbpf::{assembler::assemble, program::BuiltinProgram, vm::TestContextObject};

use crate::{
generated::{
Expand Down Expand Up @@ -672,10 +673,25 @@ pub(crate) fn is_prog_name_supported() -> bool {
});
u.prog_name = name;

// The fields conforming an encoded basic instruction are stored in the following order:
// opcode:8 src_reg:4 dst_reg:4 offset:16 imm:32 - In little-endian BPF.
// opcode:8 dst_reg:4 src_reg:4 offset:16 imm:32 - In big-endian BPF.
// Multi-byte fields ('imm' and 'offset') are stored using endian order.
// https://www.kernel.org/doc/html/v6.4-rc7/bpf/instruction-set.html#instruction-encoding
/*
let prog: &[u8] = &[
0xb7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // mov64 r0 = 0
0xb7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // mov64 r0, 0
0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // exit
];
*/
let executable = assemble::<TestContextObject>(
"mov64 r0, 0
exit",
std::sync::Arc::new(BuiltinProgram::new_mock()),
)
.unwrap();
let prog = executable.get_text_bytes().1;
println!("BILLY: name_supported {:x?}", prog);

let gpl = b"GPL\0";
u.license = gpl.as_ptr() as u64;
Expand All @@ -692,14 +708,44 @@ pub(crate) fn is_probe_read_kernel_supported() -> bool {
let mut attr = unsafe { mem::zeroed::<bpf_attr>() };
let u = unsafe { &mut attr.__bindgen_anon_3 };

// The fields conforming an encoded basic instruction are stored in the following order:
// opcode:8 src_reg:4 dst_reg:4 offset:16 imm:32 - In little-endian BPF.
// opcode:8 dst_reg:4 src_reg:4 offset:16 imm:32 - In big-endian BPF.
// Multi-byte fields ('imm' and 'offset') are stored using endian order.
// https://www.kernel.org/doc/html/v6.4-rc7/bpf/instruction-set.html#instruction-encoding
/*
#[cfg(target_endian = "little")]
let prog: &[u8] = &[
0xbf, 0xa1, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // mov64 r1, r10
0x07, 0x01, 0x00, 0x00, 0xf8, 0xff, 0xff, 0xff, // add64 r1, -8
0xb7, 0x02, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, // mov64 r2, 8
0xb7, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // mov64 r3, 0
0x85, 0x00, 0x00, 0x00, 0x71, 0x00, 0x00, 0x00, // call 113 <-- BILLY: TODO
0x85, 0x10, 0x00, 0x00, 0x76, 0x00, 0x00, 0x00, // <-- BILLY: ERROR - SEEING
0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // exit
];
#[cfg(target_endian = "big")]
let prog: &[u8] = &[
0xbf, 0xa1, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // r1 = r10
0x07, 0x01, 0x00, 0x00, 0xf8, 0xff, 0xff, 0xff, // r1 -= 8
0xb7, 0x02, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, // r2 = 8
0xb7, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // r3 = 0
0x85, 0x00, 0x00, 0x00, 0x71, 0x00, 0x00, 0x00, // call 113
0xbf, 0x1a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // r1 = r10
0x07, 0x10, 0x00, 0x00, 0xff, 0xff, 0xff, 0xf8, // r1 -= 8
0xb7, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, // r2 = 8
0xb7, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // r3 = 0
0x85, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x71, // call 113
0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // exit
];
*/
let executable = assemble::<TestContextObject>(
"mov64 r1, r10
add64 r1, -8
mov64 r2, 8
mov64 r3, 0
call 113
exit",
std::sync::Arc::new(BuiltinProgram::new_mock()),
)
.unwrap();
let prog = executable.get_text_bytes().1;
println!("BILLY: read_kernel_supported {:x?}", prog);

let gpl = b"GPL\0";
u.license = gpl.as_ptr() as u64;
Expand All @@ -716,10 +762,25 @@ pub(crate) fn is_perf_link_supported() -> bool {
let mut attr = unsafe { mem::zeroed::<bpf_attr>() };
let u = unsafe { &mut attr.__bindgen_anon_3 };

// The fields conforming an encoded basic instruction are stored in the following order:
// opcode:8 src_reg:4 dst_reg:4 offset:16 imm:32 - In little-endian BPF.
// opcode:8 dst_reg:4 src_reg:4 offset:16 imm:32 - In big-endian BPF.
// Multi-byte fields ('imm' and 'offset') are stored using endian order.
// https://www.kernel.org/doc/html/v6.4-rc7/bpf/instruction-set.html#instruction-encoding
/*
let prog: &[u8] = &[
0xb7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // mov64 r0 = 0
0xb7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // mov64 r0, 0
0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // exit
];
*/
let executable = assemble::<TestContextObject>(
"mov64 r0, 0
exit",
std::sync::Arc::new(BuiltinProgram::new_mock()),
)
.unwrap();
let prog = executable.get_text_bytes().1;
println!("BILLY: perf_link_supported {:x?}", prog);

let gpl = b"GPL\0";
u.license = gpl.as_ptr() as u64;
Expand Down Expand Up @@ -747,13 +808,41 @@ pub(crate) fn is_bpf_global_data_supported() -> bool {
let mut attr = unsafe { mem::zeroed::<bpf_attr>() };
let u = unsafe { &mut attr.__bindgen_anon_3 };

// The fields conforming an encoded basic instruction are stored in the following order:
// opcode:8 src_reg:4 dst_reg:4 offset:16 imm:32 - In little-endian BPF.
// opcode:8 dst_reg:4 src_reg:4 offset:16 imm:32 - In big-endian BPF.
// Multi-byte fields ('imm' and 'offset') are stored using endian order.
// https://www.kernel.org/doc/html/v6.4-rc7/bpf/instruction-set.html#instruction-encoding
/*
#[cfg(target_endian = "little")]
let prog: &[u8] = &[
0x18, 0x21, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ld_pseudo r1, 0x2, 0x0
0x18, 0x21, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // lddw r1, 0x2, 0x0 <-- BILLY: TODO
0x18, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // lddw r1, 0x0000001000000000 <-- ERROR - SEEING AND COMMAND DOESN'T SEEM CORRECT
0x79, 0x21, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ldxdw r1, [r2 + 0x0] <-- ERROR - SEEING AND COMMAND DOESN'T SEEM CORRECT
0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, //
0x7a, 0x01, 0x00, 0x00, 0x2a, 0x00, 0x00, 0x00, // stdw [r1 + 0x0], 0x2a
0xb7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // mov64 r0 = 0
0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // exit
];
#[cfg(target_endian = "big")]
let prog: &[u8] = &[
0x18, 0x12, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, // ld_pseudo r1, 0x2, 0x0
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, //
0x7a, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2a, // stdw [r1 + 0x0], 0x2a
0xb7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // mov64 r0, 0
0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // exit
];
*/
let executable = assemble::<TestContextObject>(
"lddw r1, 0x0000001000000000
stdw [r1 + 0x0], 0x2a
mov64 r0, 0
exit",
std::sync::Arc::new(BuiltinProgram::new_mock()),
)
.unwrap();
let prog = executable.get_text_bytes().1;
println!("BILLY: global_data_supported {:x?}", prog);

let mut insns = copy_instructions(prog).unwrap();

Expand Down Expand Up @@ -794,10 +883,32 @@ pub(crate) fn is_bpf_cookie_supported() -> bool {
let mut attr = unsafe { mem::zeroed::<bpf_attr>() };
let u = unsafe { &mut attr.__bindgen_anon_3 };

// The fields conforming an encoded basic instruction are stored in the following order:
// opcode:8 src_reg:4 dst_reg:4 offset:16 imm:32 - In little-endian BPF.
// opcode:8 dst_reg:4 src_reg:4 offset:16 imm:32 - In big-endian BPF.
// Multi-byte fields ('imm' and 'offset') are stored using endian order.
// https://www.kernel.org/doc/html/v6.4-rc7/bpf/instruction-set.html#instruction-encoding
/*
#[cfg(target_endian = "little")]
let prog: &[u8] = &[
0x85, 0x00, 0x00, 0x00, 0xae, 0x00, 0x00, 0x00, // call bpf_get_attach_cookie <-- BILLY: TODO
0x85, 0x00, 0x00, 0x00, 0x1f, 0x03, 0x66, 0xe0, // <-- BILLY: ERROR - SEEING
0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // exit
];
#[cfg(target_endian = "big")]
let prog: &[u8] = &[
0x85, 0x00, 0x00, 0x00, 0xae, 0x00, 0x00, 0x00, // call bpf_get_attach_cookie
0x85, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xae, // call bpf_get_attach_cookie
0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // exit
];
*/
let executable = assemble::<TestContextObject>(
"syscall bpf_get_attach_cookie
exit",
std::sync::Arc::new(BuiltinProgram::new_mock()),
)
.unwrap();
let prog = executable.get_text_bytes().1;
println!("BILLY: cookie_supported {:x?}", prog);

let gpl = b"GPL\0";
u.license = gpl.as_ptr() as u64;
Expand Down Expand Up @@ -1136,6 +1247,20 @@ mod tests {
assert!(!supported);
}

#[test]
fn test_prog_name_supported() {
let name_supported = is_prog_name_supported();
assert!(!name_supported);
let read_kernel_supported = is_probe_read_kernel_supported();
assert!(!read_kernel_supported);
let perf_link_supported = is_perf_link_supported();
assert!(!perf_link_supported);
let global_data_supported = is_bpf_global_data_supported();
assert!(!global_data_supported);
let cookie_supported = is_bpf_cookie_supported();
assert!(!cookie_supported);
}

#[test]
#[should_panic = "assertion failed: `BPF_MAP_TYPE_HASH` does not match `bpf_map_type::BPF_MAP_TYPE_CPUMAP | bpf_map_type::BPF_MAP_TYPE_DEVMAP |
bpf_map_type::BPF_MAP_TYPE_DEVMAP_HASH`"]
Expand Down
Loading