relocations.rs 1.3 KB

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