aya-rs / book

The Aya Book is an introductory book about using the Rust Programming Language and Aya library to build extended Berkley Packet Filter (eBPF) programs.
https://aya-rs.dev/book/
Apache License 2.0
81 stars 60 forks source link

Getting `target/bpfel... doesn't have a size known at compile-time` for example 2 #41

Closed Molter73 closed 2 years ago

Molter73 commented 2 years ago

With the latest changes added to the book I've stumbled with the following error (I created the project from the template and named it aya-test instead of myapp, but it is the same code):

   Compiling aya-test v0.1.0 (/workspaces/aya-test/aya-test)
error: couldn't read aya-test/src/../../target/bpfel-unknown-none/release/aya-test: No such file or directory (os error 2)
  --> aya-test/src/main.rs:22:17
   |
22 |     let bytes = include_bytes_aligned!("../../target/bpfel-unknown-none/release/aya-test");
   |                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = note: this error originates in the macro `include_bytes` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
  --> aya-test/src/main.rs:22:17
   |
22 |     let bytes = include_bytes_aligned!("../../target/bpfel-unknown-none/release/aya-test");
   |                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
   |
   = help: within `Aligned<[u8]>`, the trait `Sized` is not implemented for `[u8]`
note: required because it appears within the type `Aligned<[u8]>`
  --> aya-test/src/main.rs:22:17
   |
22 |     let bytes = include_bytes_aligned!("../../target/bpfel-unknown-none/release/aya-test");
   |                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   = note: structs must have a statically known size to be initialized
   = note: this error originates in the macro `include_bytes_aligned` (in Nightly builds, run with -Z macro-backtrace for more info)

For more information about this error, try `rustc --explain E0277`.
error: could not compile `aya-test` due to 2 previous errors
thread 'main' panicked at 'assertion failed: status.success()', xtask/src/run.rs:34:5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Turns out compilation up to that point is done in debug mode, so I think the "../../target/bpfel-unknown-none/release/myapp" string should actually be "../../target/bpfel-unknown-none/debug/myapp" here and here.

joshiste commented 2 years ago

Try to compile the ebpf program first using cargo xtask build-ebpf

Molter73 commented 2 years ago

Same result

[root@77f69e77483d aya-test]# cargo xtask build-ebpf
    Finished dev [unoptimized + debuginfo] target(s) in 2.49s
     Running `target/debug/xtask build-ebpf`
       Fresh core v0.0.0 (/root/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core)
       Fresh unicode-ident v1.0.1
       Fresh autocfg v1.1.0
       Fresh rustc-std-workspace-core v1.99.0 (/root/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/rustc-std-workspace-core)
       Fresh compiler_builtins v0.1.73
       Fresh proc-macro2 v1.0.40
       Fresh quote v1.0.20
       Fresh aya-bpf-cty v0.2.1 (https://github.com/aya-rs/aya?branch=main#150dc1b6)
       Fresh aya-log-common v0.1.11-dev.0 (https://github.com/aya-rs/aya-log?branch=main#1b0d3da1)
       Fresh aya-test-common v0.1.0 (/workspaces/aya-test/aya-test-common)
       Fresh syn v1.0.98
       Fresh aya-bpf-bindings v0.1.0 (https://github.com/aya-rs/aya?branch=main#150dc1b6)
       Fresh aya-bpf-macros v0.1.0 (https://github.com/aya-rs/aya?branch=main#150dc1b6)
       Fresh aya-log-ebpf-macros v0.1.0 (https://github.com/aya-rs/aya-log?branch=main#1b0d3da1)
       Fresh memoffset v0.6.5
       Fresh aya-bpf v0.1.0 (https://github.com/aya-rs/aya?branch=main#150dc1b6)
       Fresh aya-log-ebpf v0.1.0 (https://github.com/aya-rs/aya-log?branch=main#1b0d3da1)
       Fresh aya-test-ebpf v0.1.0 (/workspaces/aya-test/aya-test-ebpf)
    Finished dev [optimized] target(s) in 1.12s
[root@77f69e77483d aya-test]# find target/ -name 'aya-test' -type f
target/bpfel-unknown-none/debug/aya-test
target/debug/aya-test
[root@77f69e77483d aya-test]# 
joshiste commented 2 years ago

The first one looks like a release build (looking for target/bpfel-unknown-none/release/aya-test) and the second one was a debug build (producing target/bpfel-unknown-none/debug/aya-test)

Molter73 commented 2 years ago

They are both debug builds, the /release/ part of the string is hardcoded in the examples

[root@77f69e77483d aya-test]# cargo xtask run
    Finished dev [unoptimized + debuginfo] target(s) in 1.27s
     Running `target/debug/xtask run`
       Fresh core v0.0.0 (/root/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core)
       Fresh unicode-ident v1.0.1
       Fresh autocfg v1.1.0
       Fresh rustc-std-workspace-core v1.99.0 (/root/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/rustc-std-workspace-core)
       Fresh compiler_builtins v0.1.73
       Fresh proc-macro2 v1.0.40
       Fresh quote v1.0.20
       Fresh aya-bpf-cty v0.2.1 (https://github.com/aya-rs/aya?branch=main#150dc1b6)
       Fresh aya-log-common v0.1.11-dev.0 (https://github.com/aya-rs/aya-log?branch=main#1b0d3da1)
       Fresh aya-test-common v0.1.0 (/workspaces/aya-test/aya-test-common)
       Fresh syn v1.0.98
       Fresh aya-bpf-bindings v0.1.0 (https://github.com/aya-rs/aya?branch=main#150dc1b6)
       Fresh aya-bpf-macros v0.1.0 (https://github.com/aya-rs/aya?branch=main#150dc1b6)
       Fresh aya-log-ebpf-macros v0.1.0 (https://github.com/aya-rs/aya-log?branch=main#1b0d3da1)
       Fresh memoffset v0.6.5
       Fresh aya-bpf v0.1.0 (https://github.com/aya-rs/aya?branch=main#150dc1b6)
       Fresh aya-log-ebpf v0.1.0 (https://github.com/aya-rs/aya-log?branch=main#1b0d3da1)
       Fresh aya-test-ebpf v0.1.0 (/workspaces/aya-test/aya-test-ebpf)
    Finished dev [optimized] target(s) in 0.67s
   Compiling aya-test v0.1.0 (/workspaces/aya-test/aya-test)
error: couldn't read aya-test/src/../../target/bpfel-unknown-none/release/aya-test: No such file or directory (os error 2)
  --> aya-test/src/main.rs:22:17
   |
22 |     let bytes = include_bytes_aligned!("../../target/bpfel-unknown-none/release/aya-test");
   |                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = note: this error originates in the macro `include_bytes` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
  --> aya-test/src/main.rs:22:17
   |
22 |     let bytes = include_bytes_aligned!("../../target/bpfel-unknown-none/release/aya-test");
   |                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
   |
   = help: within `Aligned<[u8]>`, the trait `Sized` is not implemented for `[u8]`
note: required because it appears within the type `Aligned<[u8]>`
  --> aya-test/src/main.rs:22:17
   |
22 |     let bytes = include_bytes_aligned!("../../target/bpfel-unknown-none/release/aya-test");
   |                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   = note: structs must have a statically known size to be initialized
   = note: this error originates in the macro `include_bytes_aligned` (in Nightly builds, run with -Z macro-backtrace for more info)

For more information about this error, try `rustc --explain E0277`.
error: could not compile `aya-test` due to 2 previous errors
thread 'main' panicked at 'assertion failed: status.success()', xtask/src/run.rs:34:5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Billy99 commented 2 years ago

@Molter73 I have PR #40 pending. Once that merges, the documentation should be update with the generated code. The new template has something like:

    #[cfg(debug_assertions)]
    let mut bpf = Bpf::load(include_bytes_aligned!(
        "../../target/bpfel-unknown-none/debug/myapp"
    ))?;
    #[cfg(not(debug_assertions))]
    let mut bpf = Bpf::load(include_bytes_aligned!(
        "../../target/bpfel-unknown-none/release/myapp"
    ))?;

which hopefully will address your issue.

Here is what the new generated book will look like, to help you move forward (or check the PR for more updates): https://deploy-preview-40--marvelous-tartufo-718ab1.netlify.app/

Molter73 commented 2 years ago

Closing the issue since #40 fixed it. Thanks everyone!