relocations.rs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. use std::time::Duration;
  2. use aya::{include_bytes_aligned, programs::UProbe, Bpf};
  3. #[test]
  4. fn relocations() {
  5. let bpf = load_and_attach(
  6. "test_64_32_call_relocs",
  7. include_bytes_aligned!("../../../target/bpfel-unknown-none/release/relocations"),
  8. );
  9. trigger_relocations_program();
  10. std::thread::sleep(Duration::from_millis(100));
  11. let m = aya::maps::Array::<_, u64>::try_from(bpf.map("RESULTS").unwrap()).unwrap();
  12. assert_eq!(m.get(&0, 0).unwrap(), 1);
  13. assert_eq!(m.get(&1, 0).unwrap(), 2);
  14. assert_eq!(m.get(&2, 0).unwrap(), 3);
  15. }
  16. #[test]
  17. fn text_64_64_reloc() {
  18. let mut bpf = load_and_attach("test_text_64_64_reloc", integration_test::TEXT_64_64_RELOC);
  19. let mut m = aya::maps::Array::<_, u64>::try_from(bpf.map_mut("RESULTS").unwrap()).unwrap();
  20. m.set(0, 1, 0).unwrap();
  21. m.set(1, 2, 0).unwrap();
  22. trigger_relocations_program();
  23. std::thread::sleep(Duration::from_millis(100));
  24. assert_eq!(m.get(&0, 0).unwrap(), 2);
  25. assert_eq!(m.get(&1, 0).unwrap(), 3);
  26. }
  27. fn load_and_attach(name: &str, bytes: &[u8]) -> Bpf {
  28. let mut bpf = Bpf::load(bytes).unwrap();
  29. let prog: &mut UProbe = bpf.program_mut(name).unwrap().try_into().unwrap();
  30. prog.load().unwrap();
  31. prog.attach(
  32. Some("trigger_relocations_program"),
  33. 0,
  34. "/proc/self/exe",
  35. None,
  36. )
  37. .unwrap();
  38. bpf
  39. }
  40. #[no_mangle]
  41. #[inline(never)]
  42. pub extern "C" fn trigger_relocations_program() {}