use crate::{Tag, TagIter, TagTrait, TagType, TagTypeId};
use core::fmt::{Debug, Formatter};
use core::mem::size_of;
use core::str::Utf8Error;
#[cfg(feature = "builder")]
use {crate::builder::traits::StructAsBytes, crate::builder::BoxedDst, alloc::vec::Vec};
const METADATA_SIZE: usize = size_of::<TagTypeId>() + 3 * size_of::<u32>();
#[derive(ptr_meta::Pointee, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(C)]
pub struct ModuleTag {
typ: TagTypeId,
size: u32,
mod_start: u32,
mod_end: u32,
cmdline: [u8],
}
impl ModuleTag {
#[cfg(feature = "builder")]
pub fn new(start: u32, end: u32, cmdline: &str) -> BoxedDst<Self> {
let mut cmdline_bytes: Vec<_> = cmdline.bytes().collect();
if !cmdline_bytes.ends_with(&[0]) {
cmdline_bytes.push(0);
}
let start_bytes = start.to_le_bytes();
let end_bytes = end.to_le_bytes();
let mut content_bytes = [start_bytes, end_bytes].concat();
content_bytes.extend_from_slice(&cmdline_bytes);
BoxedDst::new(TagType::Module, &content_bytes)
}
pub fn cmdline(&self) -> Result<&str, Utf8Error> {
Tag::get_dst_str_slice(&self.cmdline)
}
pub fn start_address(&self) -> u32 {
self.mod_start
}
pub fn end_address(&self) -> u32 {
self.mod_end
}
pub fn module_size(&self) -> u32 {
self.mod_end - self.mod_start
}
}
impl TagTrait for ModuleTag {
fn dst_size(base_tag: &Tag) -> usize {
assert!(base_tag.size as usize >= METADATA_SIZE);
base_tag.size as usize - METADATA_SIZE
}
}
#[cfg(feature = "builder")]
impl StructAsBytes for ModuleTag {
fn byte_size(&self) -> usize {
self.size.try_into().unwrap()
}
}
impl Debug for ModuleTag {
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
f.debug_struct("ModuleTag")
.field("type", &{ self.typ })
.field("size (tag)", &{ self.size })
.field("size (module)", &self.module_size())
.field("mod_start", &(self.mod_start as *const usize))
.field("mod_end", &(self.mod_end as *const usize))
.field("cmdline", &self.cmdline())
.finish()
}
}
pub fn module_iter(iter: TagIter) -> ModuleIter {
ModuleIter { iter }
}
#[derive(Clone)]
pub struct ModuleIter<'a> {
iter: TagIter<'a>,
}
impl<'a> Iterator for ModuleIter<'a> {
type Item = &'a ModuleTag;
fn next(&mut self) -> Option<&'a ModuleTag> {
self.iter
.find(|tag| tag.typ == TagType::Module)
.map(|tag| tag.cast_tag())
}
}
impl<'a> Debug for ModuleIter<'a> {
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
let mut list = f.debug_list();
self.clone().for_each(|tag| {
list.entry(&tag);
});
list.finish()
}
}
#[cfg(test)]
mod tests {
use crate::{ModuleTag, Tag, TagType};
const MSG: &str = "hello";
fn get_bytes() -> std::vec::Vec<u8> {
let size = (4 + 4 + 4 + 4 + MSG.as_bytes().len() + 1) as u32;
[
&((TagType::Module.val()).to_le_bytes()),
&size.to_le_bytes(),
&0_u32.to_le_bytes(),
&0_u32.to_le_bytes(),
MSG.as_bytes(),
&[0],
]
.iter()
.flat_map(|bytes| bytes.iter())
.copied()
.collect()
}
#[test]
#[cfg_attr(miri, ignore)]
fn test_parse_str() {
let tag = get_bytes();
let tag = unsafe { &*tag.as_ptr().cast::<Tag>() };
let tag = tag.cast_tag::<ModuleTag>();
assert_eq!({ tag.typ }, TagType::Module);
assert_eq!(tag.cmdline().expect("must be valid UTF-8"), MSG);
}
#[test]
#[cfg(feature = "builder")]
fn test_build_str() {
use crate::builder::traits::StructAsBytes;
let tag = ModuleTag::new(0, 0, MSG);
let bytes = tag.struct_as_bytes();
assert_eq!(bytes, get_bytes());
assert_eq!(tag.cmdline(), Ok(MSG));
let tag = ModuleTag::new(0, 0, "AbCdEfGhUjK YEAH");
assert_eq!(tag.cmdline(), Ok("AbCdEfGhUjK YEAH"));
let tag = ModuleTag::new(0, 0, "AbCdEfGhUjK YEAH".repeat(42).as_str());
assert_eq!(tag.cmdline(), Ok("AbCdEfGhUjK YEAH".repeat(42).as_str()));
}
}