Skip to content
This repository has been archived by the owner on Jul 11, 2023. It is now read-only.

Segmentation fault in simple ebpf program #213

Closed
sebastiaoamaro opened this issue Nov 5, 2021 · 14 comments
Closed

Segmentation fault in simple ebpf program #213

sebastiaoamaro opened this issue Nov 5, 2021 · 14 comments

Comments

@sebastiaoamaro
Copy link
Contributor

Running a program based on https://github.com/foniod/redbpf/blob/main/examples/example-probes/src/tcp_lifetime/main.rs with code:

// This program can be executed by
// # cargo run --example tcp-lifetime [interface]
#![no_std]
#![no_main]
use core::mem::{self, MaybeUninit};
use memoffset::offset_of;

use redbpf_probes::socket_filter::prelude::*;

use monitor::usage::{SocketAddr,message};

#[map(link_section = "maps/perf_events")]
static mut perf_events: PerfMap<message> = PerfMap::with_max_entries(1024);
#[map(link_section = "maps/usage")]
static mut usage: HashMap<u32, u32> = HashMap::with_max_entries(4096);

#[map(link_section = "maps/time")]
static mut time: HashMap<u32, u64> = HashMap::with_max_entries(4096);

program!(0xFFFFFFFE, "GPL");
#[socket_filter]
fn measure_tcp_lifetime(skb: SkBuff) -> SkBuffResult {
    let eth_len = mem::size_of::<ethhdr>();
    let eth_proto = skb.load::<__be16>(offset_of!(ethhdr, h_proto))? as u32;
    if eth_proto != ETH_P_IP {
        return Ok(SkBuffAction::Ignore);
    }

    let ip_proto = skb.load::<__u8>(eth_len + offset_of!(iphdr, protocol))? as u32;
    if ip_proto != IPPROTO_TCP {
        return Ok(SkBuffAction::Ignore);
    }

    Ok(SkBuffAction::Ignore)
}
´´´

Gives a segmentation fault, however, used to work about a month ago don't know if it started to happen due to rust updates or redBPF updates.
Thanks in Advance.
@rhdxmr
Copy link
Collaborator

rhdxmr commented Nov 6, 2021

@sebastiaoamaro Do you experience Segfault while compiling BPF programs?
Or it happens while running an executable?

@rhdxmr
Copy link
Collaborator

rhdxmr commented Nov 6, 2021

@sebastiaoamaro I used to experience segfault while compiling BPF programs if versions mismatch between rust and LLVM.

The latest stable rust depends on LLVM 13. But compiling BPF programs will fail if your system has LLVM 12 libraries.

@rhdxmr
Copy link
Collaborator

rhdxmr commented Nov 6, 2021

You can check your rustc uses which version of LLVM by executing this:

$ rustc -vV
rustc 1.55.0 (c8dfcfe04 2021-09-06)
binary: rustc
commit-hash: c8dfcfe046a7680554bf4eb612bad840e7631c4b
commit-date: 2021-09-06
host: x86_64-unknown-linux-gnu
release: 1.55.0
LLVM version: 12.0.1

And you can check the version of LLVM in your system by running this command:

$ llvm-config --version
12.0.1

@rhdxmr
Copy link
Collaborator

rhdxmr commented Nov 8, 2021

Hello @sebastiaoamaro
How's it going with the problem?
Did you find some clue?

@sebastiaoamaro
Copy link
Contributor Author

sebastiaoamaro commented Nov 8, 2021

Thanks for the responses!
This is my rustc -vV command result:

rustc 1.56.1 (59eed8a2a 2021-11-01)
binary: rustc
commit-hash: 59eed8a2aac0230a8b53e89d4e99d55912ba6b35
commit-date: 2021-11-01
host: x86_64-unknown-linux-gnu
release: 1.56.1
LLVM version: 13.0.0

And my llvm-config --version:
13.0.1

I get the error:
error: Can not use LLVM12 with Rust >= 1.56
--> /home/sebas/.cargo/registry/src/git.luolix.top-1ecc6299db9ec823/cargo-bpf-2.1.0/src/llvm.rs:26:9
|
26 | compile_error!("Can not use LLVM12 with Rust >= 1.56");

When running cargo install cargo-bpf.
I changed versions using these commands: https://askubuntu.com/a/1310730

@sebastiaoamaro
Copy link
Contributor Author

I created a new ubuntu (21.10) with only llvm13, and this time I get:
error:
No suitable version of LLVM was found system-wide or pointed
to by LLVM_SYS_120_PREFIX.

   Consider using `llvmenv` to compile an appropriate copy of LLVM, and
   refer to the llvm-sys documentation for more information.

   llvm-sys: https://crates.io/crates/llvm-sys
   llvmenv: https://crates.io/crates/llvmenv

--> /home/sebasamaro/.cargo/registry/src/git.luolix.top-1ecc6299db9ec823/llvm-sys-120.2.1/src/lib.rs:486:1
|
486 | / std::compile_error!(concat!(
487 | | "No suitable version of LLVM was found system-wide or pointed
488 | | to by LLVM_SYS_",
489 | | env!("CARGO_PKG_VERSION_MAJOR"),
... |
496 | | llvmenv: https://crates.io/crates/llvmenv"
497 | | ));

When running llvm-13-config:
I have 13.0.1

I also tried installing with llvmenv, however their build is not working.

@rhdxmr
Copy link
Collaborator

rhdxmr commented Nov 9, 2021

@sebastiaoamaro

There were two options you could choose.

  1. Downgrade rust to 1.55 that depends on LLVM 12 and LLVM 12 installed in system
  2. Use rust 1.56 that depends on LLVM 13, and LLVM 13 installed in system

It seems that you chose option no. 2
In this situation you can pass llvm13 feature, to tell redbpf to use it instead of llvm12.

$ cargo build --no-default-features --features=llvm13

Currently, RedBPF uses LLVM12 as a default LLVM version. But you can change the version by the command above. Or you can specify the feature through Cargo.toml if you use redbpf as a dependency.

Please refer to valid combination of rust and llvm versions for more information.

If you have any problem please let me know.

@sebastiaoamaro
Copy link
Contributor Author

I understand that step when building bpf programs, but I am still stuck on installing (cargo install cargo-bpf).

@sebastiaoamaro
Copy link
Contributor Author

If i install running cargo install cargo-bpf --no-default-features --features=llvm13 then I get:
error[E0433]: failed to resolve: use of undeclared crate or module bpf_sys
--> /home/sebasamaro/.cargo/registry/src/git.luolix.top-1ecc6299db9ec823/cargo-bpf-2.1.0/src/build_constants.rs:1:5
|
1 | use bpf_sys::headers::prefix_kernel_headers;
| ^^^^^^^ use of undeclared crate or module bpf_sys

error[E0425]: cannot find function prefix_kernel_headers in this scope
--> /home/sebasamaro/.cargo/registry/src/git.luolix.top-1ecc6299db9ec823/cargo-bpf-2.1.0/src/build_constants.rs:64:5
|
64 | prefix_kernel_headers(&KERNEL_HEADERS).ok_or(())
| ^^^^^^^^^^^^^^^^^^^^^ not found in this scope

Because some default features need to be installed.

@rhdxmr
Copy link
Collaborator

rhdxmr commented Nov 9, 2021

@sebastiaoamaro
Thank you for filing your issue.
You can build cargo-bpf cargo sub-command with LLVM13 like this:

$ cargo install cargo-bpf --no-default-features --features=llvm13,command-line

Please, see cargo-bpf/Cargo.toml to understand why I passed the --features=llvm13,command-line.

Thank you

@sebastiaoamaro
Copy link
Contributor Author

It makes sense yes, maybe changing the tutorial so that other users don't get the same error would be good.
It also fixed the segmentation fault. Thanks so much for the help.

@rhdxmr
Copy link
Collaborator

rhdxmr commented Nov 9, 2021

@sebastiaoamaro
Good idea. It would be nice to state rust and LLVM version issue in the tutorial.
And I didn't realize that installing cargo-bpf may fail because of LLVM version mismatch before you mentioned. So it would be also helpful to mention how to build cargo-bpf properly.

Thank you

rhdxmr added a commit that referenced this issue Nov 11, 2021
A few users had struggled with problems originated from RedBPF. And they
asked how to solve the tricky problems via github issues.

The updated content consists of answers to issues as below:
- #213
- #194
- #212
- #205

Thanks to users effort and their report, this content can be written.

Signed-off-by: Junyeong Jeong <rhdxmr@gmail.com>
@rhdxmr rhdxmr mentioned this issue Nov 11, 2021
@rhdxmr
Copy link
Collaborator

rhdxmr commented Nov 11, 2021

@sebastiaoamaro
As you suggested, I wrote about LLVM version issue into the tutorial.
See #217 for more information.
Thanks

@rhdxmr rhdxmr closed this as completed Nov 11, 2021
rhdxmr added a commit that referenced this issue Nov 12, 2021
A few users had struggled with problems originated from RedBPF. And they
asked how to solve the tricky problems via github issues.

The updated content consists of answers to issues as below:
- #213
- #194
- #212
- #205

Thanks to users effort and their report, this content can be written.

Signed-off-by: Junyeong Jeong <rhdxmr@gmail.com>
@yasin-cs-ko-ak
Copy link

@rhdxmr Thank you so much for your comments. I've been trying to install this for a while. Did not find anyway around. I was stuck with this error.
Now it's installed successfully!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants