4
0

relocations.rs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. use std::{process::exit, time::Duration};
  2. use aya::{
  3. include_bytes_aligned,
  4. programs::{ProgramError, UProbe},
  5. Bpf,
  6. };
  7. #[test]
  8. fn relocations() {
  9. let bpf = load_and_attach(
  10. "test_64_32_call_relocs",
  11. include_bytes_aligned!("../../../target/bpfel-unknown-none/release/relocations"),
  12. );
  13. trigger_relocations_program();
  14. std::thread::sleep(Duration::from_millis(100));
  15. let m = aya::maps::Array::<_, u64>::try_from(bpf.map("RESULTS").unwrap()).unwrap();
  16. assert_eq!(m.get(&0, 0).unwrap(), 1);
  17. assert_eq!(m.get(&1, 0).unwrap(), 2);
  18. assert_eq!(m.get(&2, 0).unwrap(), 3);
  19. }
  20. #[test]
  21. fn text_64_64_reloc() {
  22. let mut bpf = load_and_attach(
  23. "test_text_64_64_reloc",
  24. include_bytes_aligned!("../../../target/bpfel-unknown-none/release/text_64_64_reloc.o"),
  25. );
  26. let mut m = aya::maps::Array::<_, u64>::try_from(bpf.map_mut("RESULTS").unwrap()).unwrap();
  27. m.set(0, 1, 0).unwrap();
  28. m.set(1, 2, 0).unwrap();
  29. trigger_relocations_program();
  30. std::thread::sleep(Duration::from_millis(100));
  31. assert_eq!(m.get(&0, 0).unwrap(), 2);
  32. assert_eq!(m.get(&1, 0).unwrap(), 3);
  33. }
  34. fn load_and_attach(name: &str, bytes: &[u8]) -> Bpf {
  35. let mut bpf = Bpf::load(bytes).unwrap();
  36. let prog: &mut UProbe = bpf.program_mut(name).unwrap().try_into().unwrap();
  37. if let Err(ProgramError::LoadError {
  38. io_error,
  39. verifier_log,
  40. }) = prog.load()
  41. {
  42. println!("Failed to load program `{name}`: {io_error}. Verifier log:\n{verifier_log:#}");
  43. exit(1);
  44. };
  45. prog.attach(
  46. Some("trigger_relocations_program"),
  47. 0,
  48. "/proc/self/exe",
  49. None,
  50. )
  51. .unwrap();
  52. bpf
  53. }
  54. #[no_mangle]
  55. #[inline(never)]
  56. pub extern "C" fn trigger_relocations_program() {}