Skip to content

Commit

Permalink
Restore some uses of #![feature(associated_type_bounds)]
Browse files Browse the repository at this point in the history
Signed-off-by: Nick Spinale <[email protected]>
  • Loading branch information
nspin committed May 2, 2024
1 parent 5cc2522 commit b83d7da
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 75 deletions.
4 changes: 2 additions & 2 deletions crates/sel4-capdl-initializer/add-spec/src/render_elf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// SPDX-License-Identifier: BSD-2-Clause
//

use num::{NumCast, One, Zero};
use num::{NumCast, One, PrimInt, Zero};

use sel4_render_elf_with_data::{FileHeaderExt, Input, SymbolicInjection, SymbolicValue};

Expand All @@ -16,7 +16,7 @@ pub(crate) struct RenderElfArgs<'a> {
}

impl<'a> RenderElfArgs<'a> {
pub(crate) fn call_with<T: FileHeaderExt>(&self) -> Vec<u8> {
pub(crate) fn call_with<T: FileHeaderExt<Word: PrimInt, Sword: PrimInt>>(&self) -> Vec<u8> {
let data_len: T::Word = NumCast::from(self.data.len()).unwrap();
let heap_size: T::Word = NumCast::from(self.heap_size).unwrap();
let align_modulus = T::Word::one() << self.granule_size_bits;
Expand Down
9 changes: 6 additions & 3 deletions crates/sel4-kernel-loader/add-payload/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,12 @@ fn main() -> Result<()> {

fn continue_with_word_size<T>(args: &Args) -> Result<()>
where
T: FileHeaderExt + FileHeader<Endian = Endianness>,
T::Word: PrimInt + WrappingSub + Integer + Serialize,
T::Sword: PrimInt,
T: FileHeaderExt
+ FileHeader<
Word: PrimInt + WrappingSub + Integer + Serialize,
Sword: PrimInt,
Endian = Endianness,
>,
{
let loader_bytes = fs::read(&args.loader_path)?;

Expand Down
5 changes: 3 additions & 2 deletions crates/sel4-kernel-loader/add-payload/src/render_elf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
// SPDX-License-Identifier: BSD-2-Clause
//

use num::{NumCast, One, Zero};
use num::{NumCast, One, PrimInt, Zero};
use object::{read::elf::FileHeader, Endianness};

use sel4_render_elf_with_data::{FileHeaderExt, Input, SymbolicInjection, SymbolicValue};

pub fn render_elf<T>(orig_elf: &[u8], serialized_payload: &[u8]) -> Vec<u8>
where
T: FileHeaderExt,
T: FileHeaderExt + FileHeader<Word: PrimInt, Sword: PrimInt, Endian = Endianness>,
{
let align_modulus = T::Word::one();
let align_residue = T::Word::one();
Expand Down
34 changes: 11 additions & 23 deletions crates/sel4-kernel-loader/add-payload/src/serialize_payload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,14 @@ struct PlatformInfoForBuildSystem {
devices: Ranges,
}

pub fn serialize_payload<T>(
pub fn serialize_payload<
T: FileHeader<Endian = Endianness, Word: PrimInt + WrappingSub + Integer + Serialize>,
>(
kernel_path: impl AsRef<Path>,
app_path: impl AsRef<Path>,
dtb_path: impl AsRef<Path>,
platform_info_path: impl AsRef<Path>,
) -> Vec<u8>
where
T: FileHeader<Endian = Endianness>,
T::Word: PrimInt + WrappingSub + Integer + Serialize,
{
) -> Vec<u8> {
let platform_info: PlatformInfoForBuildSystem =
serde_yaml::from_reader(fs::File::open(&platform_info_path).unwrap()).unwrap();

Expand Down Expand Up @@ -92,11 +90,7 @@ struct Builder<T: FileHeader> {
actual_content: Vec<u8>,
}

impl<T> Builder<T>
where
T: FileHeader<Endian = Endianness>,
T::Word: PrimInt + WrappingSub + Integer,
{
impl<T: FileHeader<Endian = Endianness, Word: PrimInt + WrappingSub + Integer>> Builder<T> {
fn new() -> Self {
Self {
regions: HeaplessVec::new(),
Expand Down Expand Up @@ -191,12 +185,9 @@ where
f(&elf)
}

fn elf_virt_addr_range<'a, T, R>(elf: &ElfFile<'a, T, R>) -> Range<T::Word>
where
T: FileHeader<Endian = Endianness>,
T::Word: PrimInt,
R: ReadRef<'a>,
{
fn elf_virt_addr_range<'a, T: FileHeader<Endian = Endianness, Word: PrimInt>, R: ReadRef<'a>>(
elf: &ElfFile<'a, T, R>,
) -> Range<T::Word> {
let endian = elf.endian();
let virt_min = elf
.raw_segments()
Expand All @@ -219,12 +210,9 @@ where
virt_min..virt_max
}

fn elf_phys_to_vaddr_offset<'a, T, R>(elf: &ElfFile<'a, T, R>) -> T::Word
where
T: FileHeader,
T::Word: PrimInt + WrappingSub,
R: ReadRef<'a>,
{
fn elf_phys_to_vaddr_offset<'a, T: FileHeader<Word: PrimInt + WrappingSub>, R: ReadRef<'a>>(
elf: &ElfFile<'a, T, R>,
) -> T::Word {
let endian = elf.endian();
unified(
elf.raw_segments()
Expand Down
63 changes: 21 additions & 42 deletions crates/sel4-render-elf-with-data/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,46 +33,29 @@ impl ElfBitWidth {
pub type ConcreteFileHeader32 = FileHeader32<Endianness>;
pub type ConcreteFileHeader64 = FileHeader64<Endianness>;

// NOTE(rustc_wishlist)
//
// This is much simpler with #![feature(associated_type_bounds)]:
// ```
// pub trait FileHeaderExt:
// FileHeader<Word: PrimInt, Sword: PrimInt, Endian = Endianness>
// ```
//
pub trait FileHeaderExt:
FileHeader<Word = Self::ExtWord, Sword = Self::ExtSword, Endian = Endianness>
FileHeader + FileHeader<Word: PrimInt, Sword: PrimInt, Endian = Endianness>
{
type ExtWord: PrimInt + Into<u64>;
type ExtSword: PrimInt + Into<i64>;

fn checked_add_signed(x: Self::ExtWord, y: Self::ExtSword) -> Option<Self::ExtWord>;
fn write_word_bytes(endian: impl Endian, n: Self::ExtWord) -> Vec<u8>;
fn checked_add_signed(x: Self::Word, y: Self::Sword) -> Option<Self::Word>;
fn write_word_bytes(endian: impl Endian, n: Self::Word) -> Vec<u8>;
}

impl FileHeaderExt for ConcreteFileHeader32 {
type ExtWord = u32;
type ExtSword = i32;

fn checked_add_signed(x: Self::ExtWord, y: Self::ExtSword) -> Option<Self::ExtWord> {
fn checked_add_signed(x: Self::Word, y: Self::Sword) -> Option<Self::Word> {
x.checked_add_signed(y)
}

fn write_word_bytes(endian: impl Endian, n: Self::ExtWord) -> Vec<u8> {
fn write_word_bytes(endian: impl Endian, n: Self::Word) -> Vec<u8> {
endian.write_u32_bytes(n).to_vec()
}
}

impl FileHeaderExt for ConcreteFileHeader64 {
type ExtWord = u64;
type ExtSword = i64;

fn checked_add_signed(x: Self::ExtWord, y: Self::ExtSword) -> Option<Self::ExtWord> {
fn checked_add_signed(x: Self::Word, y: Self::Sword) -> Option<Self::Word> {
x.checked_add_signed(y)
}

fn write_word_bytes(endian: impl Endian, n: Self::ExtWord) -> Vec<u8> {
fn write_word_bytes(endian: impl Endian, n: Self::Word) -> Vec<u8> {
endian.write_u64_bytes(n).to_vec()
}
}
Expand Down Expand Up @@ -101,31 +84,31 @@ impl<'a, T: FileHeaderExt> Default for Input<'a, T> {

type Symbol = String;

type ConcreteValue<T> = <T as FileHeaderExt>::ExtWord;
type ConcreteValue<T> = <T as FileHeader>::Word;

pub struct SymbolicInjection<'a, T: FileHeaderExt> {
pub align_modulus: T::ExtWord,
pub align_residue: T::ExtWord,
pub align_modulus: T::Word,
pub align_residue: T::Word,
pub content: &'a [u8],
pub memsz: T::ExtWord,
pub memsz: T::Word,
pub patches: Vec<(Symbol, SymbolicValue<T>)>,
}

#[derive(Debug)]
pub struct SymbolicValue<T: FileHeaderExt> {
pub addend: T::ExtSword,
pub addend: T::Sword,
}

impl<'a, T: FileHeaderExt> SymbolicInjection<'a, T> {
fn filesz(&self) -> T::ExtWord {
fn filesz(&self) -> T::Word {
NumCast::from(self.content.len()).unwrap()
}

fn align_from(&self, addr: T::ExtWord) -> T::ExtWord {
fn align_from(&self, addr: T::Word) -> T::Word {
align_from::<T>(addr, self.align_modulus, self.align_residue)
}

fn locate(&self, vaddr: T::ExtWord) -> Result<Injection<'a, T>> {
fn locate(&self, vaddr: T::Word) -> Result<Injection<'a, T>> {
Ok(Injection {
vaddr,
content: self.content,
Expand All @@ -145,22 +128,22 @@ impl<'a, T: FileHeaderExt> SymbolicInjection<'a, T> {
}

pub struct Injection<'a, T: FileHeaderExt> {
pub vaddr: T::ExtWord,
pub vaddr: T::Word,
pub content: &'a [u8],
pub memsz: T::ExtWord,
pub memsz: T::Word,
pub patches: Vec<(Symbol, ConcreteValue<T>)>,
}

impl<'a, T: FileHeaderExt> Injection<'a, T> {
fn vaddr(&self) -> T::ExtWord {
fn vaddr(&self) -> T::Word {
self.vaddr
}

fn filesz(&self) -> T::ExtWord {
fn filesz(&self) -> T::Word {
NumCast::from(self.content.len()).unwrap()
}

fn memsz(&self) -> T::ExtWord {
fn memsz(&self) -> T::Word {
self.memsz
}

Expand All @@ -173,10 +156,6 @@ impl<'a, T: FileHeaderExt> Injection<'a, T> {
}
}

fn align_from<T: FileHeaderExt>(
addr: T::ExtWord,
modulus: T::ExtWord,
residue: T::ExtWord,
) -> T::ExtWord {
fn align_from<T: FileHeaderExt>(addr: T::Word, modulus: T::Word, residue: T::Word) -> T::Word {
addr + (modulus + residue - addr % modulus) % modulus
}
3 changes: 0 additions & 3 deletions hacking/unstable-feature-monitoring/wishlist/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@
// For sel4_microkit::Handler::Error = !
#![feature(associated_type_defaults)]

// Would greatly simplify sel4_render_elf_with_data::FileHeaderExt
#![feature(associated_type_bounds)]

// Would enable sel4_bounce_buffer_allocator::Basic without a global heap
#![feature(allocator_api)]
#![feature(btreemap_alloc)]
Expand Down

0 comments on commit b83d7da

Please sign in to comment.