diff --git a/.github/workflows/build-aya.yml b/.github/workflows/build-aya.yml index 076e3ac5..e9dd47b0 100644 --- a/.github/workflows/build-aya.yml +++ b/.github/workflows/build-aya.yml @@ -31,6 +31,10 @@ jobs: toolchain: stable override: true + - uses: taiki-e/install-action@cargo-hack + - name: Check + run: cargo hack check --feature-powerset --ignore-private + - uses: Swatinem/rust-cache@v1 - name: Prereqs run: cargo install cross --git https://github.com/cross-rs/cross diff --git a/aya-obj/Cargo.toml b/aya-obj/Cargo.toml index 9ede2431..a6846771 100644 --- a/aya-obj/Cargo.toml +++ b/aya-obj/Cargo.toml @@ -14,14 +14,13 @@ edition = "2021" bytes = "1" log = "0.4" object = { version = "0.30", default-features = false, features = ["read_core", "elf"] } -hashbrown = { version = "0.13", optional = true } -thiserror-std = { package = "thiserror", version = "1" } -thiserror-core = { version = "1", default-features = false, features = [], optional = true } +hashbrown = { version = "0.13" } +thiserror = { version = "1", default-features = false } +core-error = { version = "0.0.0" } [dev-dependencies] matches = "0.1.8" rbpf = "0.1.0" [features] -default = [] -no_std = ["hashbrown", "thiserror-core"] +std = [] diff --git a/aya-obj/src/btf/btf.rs b/aya-obj/src/btf/btf.rs index 40494ef0..45c943b9 100644 --- a/aya-obj/src/btf/btf.rs +++ b/aya-obj/src/btf/btf.rs @@ -21,18 +21,20 @@ use crate::{ IntEncoding, LineInfo, Struct, Typedef, VarLinkage, }, generated::{btf_ext_header, btf_header}, - thiserror::{self, Error}, util::{bytes_of, HashMap}, Object, }; +#[cfg(not(feature = "std"))] +use crate::std; + pub(crate) const MAX_RESOLVE_DEPTH: u8 = 32; pub(crate) const MAX_SPEC_LEN: usize = 64; /// The error type returned when `BTF` operations fail. -#[derive(Error, Debug)] +#[derive(thiserror::Error, Debug)] pub enum BtfError { - #[cfg(not(feature = "no_std"))] + #[cfg(feature = "std")] /// Error parsing file #[error("error parsing {path}")] FileError { @@ -126,7 +128,7 @@ pub enum BtfError { type_id: u32, }, - #[cfg(not(feature = "no_std"))] + #[cfg(feature = "std")] /// Loading the btf failed #[error("the BPF_BTF_LOAD syscall failed. Verifier output: {verifier_log}")] LoadError { @@ -232,13 +234,13 @@ impl Btf { } /// Loads BTF metadata from `/sys/kernel/btf/vmlinux`. - #[cfg(not(feature = "no_std"))] + #[cfg(feature = "std")] pub fn from_sys_fs() -> Result { Btf::parse_file("/sys/kernel/btf/vmlinux", Endianness::default()) } /// Loads BTF metadata from the given `path`. - #[cfg(not(feature = "no_std"))] + #[cfg(feature = "std")] pub fn parse_file>( path: P, endianness: Endianness, @@ -1528,7 +1530,7 @@ mod tests { } #[test] - #[cfg(not(feature = "no_std"))] + #[cfg(feature = "std")] #[cfg_attr(miri, ignore)] fn test_read_btf_from_sys_fs() { let btf = Btf::parse_file("/sys/kernel/btf/vmlinux", Endianness::default()).unwrap(); diff --git a/aya-obj/src/btf/relocation.rs b/aya-obj/src/btf/relocation.rs index 7b8cf002..dbfd25ac 100644 --- a/aya-obj/src/btf/relocation.rs +++ b/aya-obj/src/btf/relocation.rs @@ -17,13 +17,15 @@ use crate::{ bpf_core_relo, bpf_core_relo_kind::*, bpf_insn, BPF_ALU, BPF_ALU64, BPF_B, BPF_DW, BPF_H, BPF_K, BPF_LD, BPF_LDX, BPF_ST, BPF_STX, BPF_W, BTF_INT_SIGNED, }, - thiserror::{self, Error}, util::HashMap, Object, Program, ProgramSection, }; +#[cfg(not(feature = "std"))] +use crate::std; + /// The error type returned by [`Object::relocate_btf`]. -#[derive(Error, Debug)] +#[derive(thiserror::Error, Debug)] #[error("error relocating `{section}`")] pub struct BtfRelocationError { /// The function name @@ -34,9 +36,9 @@ pub struct BtfRelocationError { } /// Relocation failures -#[derive(Error, Debug)] +#[derive(thiserror::Error, Debug)] enum RelocationError { - #[cfg(not(feature = "no_std"))] + #[cfg(feature = "std")] /// I/O error #[error(transparent)] IOError(#[from] std::io::Error), diff --git a/aya-obj/src/lib.rs b/aya-obj/src/lib.rs index 3775295e..a72a8eef 100644 --- a/aya-obj/src/lib.rs +++ b/aya-obj/src/lib.rs @@ -63,16 +63,17 @@ #![cfg_attr(docsrs, feature(doc_cfg))] #![deny(clippy::all, missing_docs)] #![allow(clippy::missing_safety_doc, clippy::len_without_is_empty)] -#![cfg_attr(feature = "no_std", feature(error_in_core))] - -#[cfg(feature = "no_std")] -pub(crate) use thiserror_core as thiserror; -#[cfg(not(feature = "no_std"))] -pub(crate) use thiserror_std as thiserror; extern crate alloc; -#[cfg(not(feature = "no_std"))] +#[cfg(feature = "std")] extern crate std; +#[cfg(not(feature = "std"))] +mod std { + pub mod error { + pub use core_error::Error; + } + pub use core::*; +} pub mod btf; pub mod generated; diff --git a/aya-obj/src/maps.rs b/aya-obj/src/maps.rs index 22a88da9..dbffed0c 100644 --- a/aya-obj/src/maps.rs +++ b/aya-obj/src/maps.rs @@ -2,12 +2,12 @@ use core::mem; -use crate::{ - thiserror::{self, Error}, - BpfSectionKind, -}; +use crate::BpfSectionKind; use alloc::vec::Vec; +#[cfg(not(feature = "std"))] +use crate::std; + /// Invalid map type encontered pub struct InvalidMapTypeError { /// The map type @@ -94,7 +94,7 @@ pub enum PinningType { } /// The error type returned when failing to parse a [PinningType] -#[derive(Debug, Error)] +#[derive(Debug, thiserror::Error)] pub enum PinningError { /// Unsupported pinning type #[error("unsupported pinning type `{pinning_type}`")] diff --git a/aya-obj/src/obj.rs b/aya-obj/src/obj.rs index 9d428dcb..e0450d95 100644 --- a/aya-obj/src/obj.rs +++ b/aya-obj/src/obj.rs @@ -19,10 +19,12 @@ use crate::{ generated::{BPF_CALL, BPF_JMP, BPF_K}, maps::{BtfMap, LegacyMap, Map, MINIMUM_MAP_SIZE}, relocation::*, - thiserror::{self, Error}, util::HashMap, }; +#[cfg(not(feature = "std"))] +use crate::std; + use crate::{ btf::{Btf, BtfError, BtfExt, BtfType}, generated::{bpf_insn, bpf_map_info, bpf_map_type::BPF_MAP_TYPE_ARRAY, BPF_F_RDONLY_PROG}, @@ -978,7 +980,7 @@ fn parse_maps_section<'a, I: Iterator>( } /// Errors caught during parsing the object file -#[derive(Debug, Error)] +#[derive(Debug, thiserror::Error)] #[allow(missing_docs)] pub enum ParseError { #[error("error parsing ELF data")] diff --git a/aya-obj/src/programs/cgroup_sock.rs b/aya-obj/src/programs/cgroup_sock.rs index b9194e24..227e26d5 100644 --- a/aya-obj/src/programs/cgroup_sock.rs +++ b/aya-obj/src/programs/cgroup_sock.rs @@ -1,10 +1,10 @@ //! Cgroup socket programs. use alloc::{borrow::ToOwned, string::String}; -use crate::{ - generated::bpf_attach_type, - thiserror::{self, Error}, -}; +use crate::generated::bpf_attach_type; + +#[cfg(not(feature = "std"))] +use crate::std; /// Defines where to attach a `CgroupSock` program. #[derive(Copy, Clone, Debug, Default)] @@ -31,7 +31,7 @@ impl From for bpf_attach_type { } } -#[derive(Debug, Error)] +#[derive(Debug, thiserror::Error)] #[error("{0} is not a valid attach type for a CGROUP_SOCK program")] pub(crate) struct InvalidAttachType(String); diff --git a/aya-obj/src/programs/cgroup_sock_addr.rs b/aya-obj/src/programs/cgroup_sock_addr.rs index 39db0ae3..6bd4070e 100644 --- a/aya-obj/src/programs/cgroup_sock_addr.rs +++ b/aya-obj/src/programs/cgroup_sock_addr.rs @@ -1,10 +1,10 @@ //! Cgroup socket address programs. use alloc::{borrow::ToOwned, string::String}; -use crate::{ - generated::bpf_attach_type, - thiserror::{self, Error}, -}; +use crate::generated::bpf_attach_type; + +#[cfg(not(feature = "std"))] +use crate::std; /// Defines where to attach a `CgroupSockAddr` program. #[derive(Copy, Clone, Debug)] @@ -54,7 +54,7 @@ impl From for bpf_attach_type { } } -#[derive(Debug, Error)] +#[derive(Debug, thiserror::Error)] #[error("{0} is not a valid attach type for a CGROUP_SOCK_ADDR program")] pub(crate) struct InvalidAttachType(String); diff --git a/aya-obj/src/programs/cgroup_sockopt.rs b/aya-obj/src/programs/cgroup_sockopt.rs index b48e984a..e3495728 100644 --- a/aya-obj/src/programs/cgroup_sockopt.rs +++ b/aya-obj/src/programs/cgroup_sockopt.rs @@ -1,10 +1,10 @@ //! Cgroup socket option programs. use alloc::{borrow::ToOwned, string::String}; -use crate::{ - generated::bpf_attach_type, - thiserror::{self, Error}, -}; +use crate::generated::bpf_attach_type; + +#[cfg(not(feature = "std"))] +use crate::std; /// Defines where to attach a `CgroupSockopt` program. #[derive(Copy, Clone, Debug)] @@ -24,7 +24,7 @@ impl From for bpf_attach_type { } } -#[derive(Debug, Error)] +#[derive(Debug, thiserror::Error)] #[error("{0} is not a valid attach type for a CGROUP_SOCKOPT program")] pub(crate) struct InvalidAttachType(String); diff --git a/aya-obj/src/relocation.rs b/aya-obj/src/relocation.rs index c2f2096d..407ab288 100644 --- a/aya-obj/src/relocation.rs +++ b/aya-obj/src/relocation.rs @@ -1,7 +1,6 @@ //! Program relocation handling. use core::mem; -use std::collections::HashSet; use alloc::{borrow::ToOwned, string::String}; use log::debug; @@ -14,15 +13,17 @@ use crate::{ }, maps::Map, obj::{Function, Object, Program}, - thiserror::{self, Error}, - util::HashMap, + util::{HashMap, HashSet}, BpfSectionKind, }; +#[cfg(not(feature = "std"))] +use crate::std; + pub(crate) const INS_SIZE: usize = mem::size_of::(); /// The error type returned by [`Object::relocate_maps`] and [`Object::relocate_calls`] -#[derive(Error, Debug)] +#[derive(thiserror::Error, Debug)] #[error("error relocating `{function}`")] pub struct BpfRelocationError { /// The function name @@ -33,7 +34,7 @@ pub struct BpfRelocationError { } /// Relocation failures -#[derive(Debug, Error)] +#[derive(Debug, thiserror::Error)] pub enum RelocationError { /// Unknown symbol #[error("unknown symbol, index `{index}`")] diff --git a/aya-obj/src/util.rs b/aya-obj/src/util.rs index 36355a18..53209444 100644 --- a/aya-obj/src/util.rs +++ b/aya-obj/src/util.rs @@ -1,10 +1,15 @@ use core::{mem, slice}; -#[cfg(feature = "no_std")] +#[cfg(not(feature = "std"))] pub(crate) use hashbrown::HashMap; -#[cfg(not(feature = "no_std"))] +#[cfg(feature = "std")] pub(crate) use std::collections::HashMap; +#[cfg(not(feature = "std"))] +pub(crate) use hashbrown::HashSet; +#[cfg(feature = "std")] +pub(crate) use std::collections::HashSet; + /// bytes_of converts a to a byte slice pub(crate) unsafe fn bytes_of(val: &T) -> &[u8] { let size = mem::size_of::(); diff --git a/aya/Cargo.toml b/aya/Cargo.toml index 1b00546e..9b68dd1f 100644 --- a/aya/Cargo.toml +++ b/aya/Cargo.toml @@ -12,7 +12,7 @@ edition = "2021" [dependencies] libc = { version = "0.2.105" } -aya-obj = { path = "../aya-obj", version = "0.1.0" } +aya-obj = { path = "../aya-obj", version = "0.1.0", features = ["std"] } thiserror = "1" object = { version = "0.30", default-features = false, features = ["std", "read_core", "elf"] } bitflags = "1.2.1"