Skip to content

Commit 06f646b

Browse files
committed
Increase code coverage
Increase code coverage by exercising a bunch of paths on recently added ELF relocation related types. Signed-off-by: Daniel Müller <deso@posteo.net>
1 parent b780a35 commit 06f646b

File tree

2 files changed

+118
-0
lines changed

2 files changed

+118
-0
lines changed

src/elf/relocations.rs

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,76 @@ impl SectionRelocations {
7777
self.maps.get(&target_idx).unwrap_or(&self.empty)
7878
}
7979
}
80+
81+
82+
#[cfg(test)]
83+
mod tests {
84+
use super::*;
85+
86+
87+
/// Exercise the `Debug` representation of relocation types.
88+
#[test]
89+
fn debug_repr() {
90+
let map = RelocationMap::default();
91+
assert_ne!(format!("{map:?}"), "");
92+
93+
let entry = RelocationEntry {
94+
addend: 42,
95+
implicit: false,
96+
};
97+
assert_ne!(format!("{entry:?}"), "");
98+
}
99+
100+
/// Test explicit (RELA) relocation.
101+
#[test]
102+
fn relocate_explicit() {
103+
let mut map = RelocationMap::default();
104+
let () = map.insert(0x100, 0xABCD, false);
105+
106+
assert_eq!(map.relocate(0x100, 999), 0xABCD);
107+
assert_eq!(map.relocate(0x200, 999), 999);
108+
}
109+
110+
/// Test implicit (REL) relocation.
111+
#[test]
112+
fn relocate_implicit() {
113+
let mut map = RelocationMap::default();
114+
let () = map.insert(0x100, 0x10, true);
115+
116+
assert_eq!(map.relocate(0x100, 0x20), 0x30);
117+
assert_eq!(map.relocate(0x200, 0x20), 0x20);
118+
}
119+
120+
/// Test wrapping behavior of implicit relocation.
121+
#[test]
122+
fn relocate_implicit_wrapping() {
123+
let mut map = RelocationMap::default();
124+
let () = map.insert(0x0, 1, true);
125+
126+
assert_eq!(map.relocate(0x0, u64::MAX), 0);
127+
}
128+
129+
/// Check that `SectionRelocations::empty()` returns identity maps
130+
/// for any index.
131+
#[test]
132+
fn section_relocations_empty() {
133+
let relocs = SectionRelocations::empty();
134+
assert_eq!(relocs.get(0).relocate(0x0, 42), 42);
135+
assert_eq!(relocs.get(99).relocate(0x0, 42), 42);
136+
}
137+
138+
/// Make sure that `SectionRelocations::new(maps)` returns populated
139+
/// map for known index and empty map for unknown.
140+
#[test]
141+
fn section_relocations_with_maps() {
142+
let mut map = RelocationMap::default();
143+
let () = map.insert(0x0, 0xFF, false);
144+
145+
let mut maps = HashMap::new();
146+
let _prev = maps.insert(3, map);
147+
148+
let relocs = SectionRelocations::new(maps);
149+
assert_eq!(relocs.get(3).relocate(0x0, 0), 0xFF);
150+
assert_eq!(relocs.get(7).relocate(0x0, 42), 42);
151+
}
152+
}

src/elf/types.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -850,6 +850,18 @@ mod tests {
850850

851851
let sym = Elf64_Sym::default();
852852
assert_ne!(format!("{sym:?}"), "");
853+
854+
let rela64 = Elf64_Rela::default();
855+
assert_ne!(format!("{rela64:?}"), "");
856+
857+
let rela32 = Elf32_Rela::default();
858+
assert_ne!(format!("{rela32:?}"), "");
859+
860+
let rel64 = Elf64_Rel::default();
861+
assert_ne!(format!("{rel64:?}"), "");
862+
863+
let rel32 = Elf32_Rel::default();
864+
assert_ne!(format!("{rel32:?}"), "");
853865
}
854866

855867
/// Exercise some trivial type conversion functions.
@@ -864,6 +876,12 @@ mod tests {
864876
let sym = Elf32_Sym::default();
865877
let _sym64 = Elf64_Sym::from(&sym);
866878

879+
let rela32 = Elf32_Rela::default();
880+
let _rela64 = Elf64_Rela::from(&rela32);
881+
882+
let rel32 = Elf32_Rel::default();
883+
let _rel64 = Elf64_Rel::from(&rel32);
884+
867885
let syms = [sym];
868886
let syms = ElfN_Syms::B32(Cow::Borrowed(&syms));
869887
let _syms = syms.to_owned();
@@ -898,5 +916,32 @@ mod tests {
898916
let _val = sym.size();
899917
let _val = sym.type_();
900918
let _val = sym.shndx();
919+
920+
// Test sym() accessor on Rela/Rel types with non-default r_info values.
921+
// 32-bit: sym index is r_info >> 8
922+
let rela32 = Elf32_Rela {
923+
r_info: 0x0000_0500,
924+
..Default::default()
925+
};
926+
assert_eq!(rela32.sym(), 5);
927+
928+
// 64-bit: sym index is r_info >> 32
929+
let rela64 = Elf64_Rela {
930+
r_info: 0x0000_0007_0000_0000,
931+
..Default::default()
932+
};
933+
assert_eq!(rela64.sym(), 7);
934+
935+
let rel32 = Elf32_Rel {
936+
r_info: 0x0000_0300,
937+
..Default::default()
938+
};
939+
assert_eq!(rel32.sym(), 3);
940+
941+
let rel64 = Elf64_Rel {
942+
r_info: 0x0000_000A_0000_0000,
943+
..Default::default()
944+
};
945+
assert_eq!(rel64.sym(), 10);
901946
}
902947
}

0 commit comments

Comments
 (0)