diff --git a/src/dwarf/debug_link.rs b/src/dwarf/debug_link.rs index c979f94b..8de41ee6 100644 --- a/src/dwarf/debug_link.rs +++ b/src/dwarf/debug_link.rs @@ -20,6 +20,7 @@ use std::path::PathBuf; use crate::elf::ElfParser; use crate::error::IntoError as _; +use crate::util::align_up; use crate::util::bytes_to_os_str; use crate::util::ReadRaw as _; use crate::Result; @@ -161,7 +162,7 @@ fn parse_debug_link_section_data(mut data: &[u8]) -> Result usize { + debug_assert_eq!( + align_to.next_power_of_two(), + align_to, + "alignment ({align_to}) is no power of two" + ); + (value + (align_to - 1)) & !(align_to - 1) +} + + /// Split a byte slice at the first byte for which `check` returns /// `true`. /// @@ -718,6 +729,28 @@ mod tests { ); } + /// Check that we can properly align values to a power of two. + #[tag(miri)] + #[test] + fn upwards_alignment() { + assert_eq!(align_up(0, 4), 0); + assert_eq!(align_up(1, 4), 4); + assert_eq!(align_up(2, 4), 4); + assert_eq!(align_up(3, 4), 4); + assert_eq!(align_up(4, 4), 4); + assert_eq!(align_up(5, 4), 8); + } + + /// Make sure that `align_up` panics when attempting to align to + /// something other than a power of two. + #[cfg(debug_assertions)] + #[tag(miri)] + #[test] + #[should_panic = "alignment (3) is no power of two"] + fn upwards_alignment_no_power_of_two() { + assert_eq!(align_up(0, 3), 0); + } + /// Check that we can reorder elements in an array as expected. #[tag(miri)] #[test]