Skip to content

Commit 165d4f4

Browse files
committed
pvh: Add test for PVH note parsing
Add test cases to verify the functionality that parses the ELF Note header to look for a PVH entry point address if one is encoded. Parse a minimal ELF binary that encodes a predefined address of 0x1e1fe1f, and verify that the same value is read. Also test the case in which a note header is present but no PVH entry point is encoded, as well as a case where the PVH entry address is encoded in the note header using a field of incorrect size. The minimal ELF source code (elfnote.S): #define ELFNOTE_START(name, type, flags) \ .pushsection .note.name, flags, @note ; \ .balign 4 ; \ .long 2f - 1f /* namesz */ ; \ .long 4484f - 3f /* descsz */ ; \ .long type ; \ 1:.asciz #name ; \ 2:.balign 4 ; \ 3: #define ELFNOTE_END \ 4484:.balign 4 ; \ .popsection ; #define ELFNOTE(name, type, desc) \ ELFNOTE_START(name, type, "a") \ desc ; \ ELFNOTE_END #define XEN_ELFNOTE_PHYS32_ENTRY 18 #define NT_VERSION 1 ELFNOTE(dummy, NT_VERSION, .quad 0xcafecafe) ELFNOTE(PVHNote, XEN_ELFNOTE_PHYS32_ENTRY, .quad 0x1e1fe1f) .section ".text","ax" .global _start _start: Built with: $ gcc elfnote.S -s -nostdlib -o test_elfnote.bin The elfnote.S source above is modified to generate the binaries for the rest of the test cases. Signed-off-by: Alejandro Jimenez <[email protected]>
1 parent 6b29b31 commit 165d4f4

File tree

4 files changed

+54
-0
lines changed

4 files changed

+54
-0
lines changed

src/loader/mod.rs

+54
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,24 @@ mod test {
543543
v
544544
}
545545

546+
#[cfg(feature = "elf")]
547+
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
548+
fn make_elfnote() -> Vec<u8> {
549+
include_bytes!("test_elfnote.bin").to_vec()
550+
}
551+
552+
#[cfg(feature = "elf")]
553+
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
554+
fn make_dummy_elfnote() -> Vec<u8> {
555+
include_bytes!("test_dummynote.bin").to_vec()
556+
}
557+
558+
#[cfg(feature = "elf")]
559+
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
560+
fn make_bad_elfnote() -> Vec<u8> {
561+
include_bytes!("test_badnote.bin").to_vec()
562+
}
563+
546564
#[allow(safe_packed_borrows)]
547565
#[allow(non_snake_case)]
548566
#[test]
@@ -665,6 +683,42 @@ mod test {
665683
);
666684
}
667685

686+
#[cfg(feature = "elf")]
687+
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
688+
#[test]
689+
fn load_pvh() {
690+
let gm = create_guest_mem();
691+
let pvhnote_image = make_elfnote();
692+
let loader_result = Elf::load(&gm, None, &mut Cursor::new(&pvhnote_image), None).unwrap();
693+
println!(
694+
"PVH entry point at address {:8x} \n",
695+
loader_result.pvh_entry_addr.unwrap().raw_value()
696+
);
697+
assert_eq!(loader_result.pvh_entry_addr.unwrap().raw_value(), 0x1e1fe1f);
698+
}
699+
700+
#[test]
701+
#[cfg(feature = "elf")]
702+
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
703+
fn dumy_elfnote() {
704+
let gm = create_guest_mem();
705+
let dummynote_image = make_dummy_elfnote();
706+
let loader_result = Elf::load(&gm, None, &mut Cursor::new(&dummynote_image), None).unwrap();
707+
assert!(loader_result.pvh_entry_addr.is_none());
708+
}
709+
710+
#[test]
711+
#[cfg(feature = "elf")]
712+
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
713+
fn bad_elfnote() {
714+
let gm = create_guest_mem();
715+
let badnote_image = make_bad_elfnote();
716+
assert_eq!(
717+
Err(Error::ReadNoteHeader),
718+
Elf::load(&gm, None, &mut Cursor::new(&badnote_image), None)
719+
);
720+
}
721+
668722
#[test]
669723
fn cmdline_overflow() {
670724
let gm = create_guest_mem();

src/loader/test_badnote.bin

640 Bytes
Binary file not shown.

src/loader/test_dummynote.bin

544 Bytes
Binary file not shown.

src/loader/test_elfnote.bin

648 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)