Skip to content

Commit da1a7ce

Browse files
committed
pvh: Add tests 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 82859bb commit da1a7ce

File tree

5 files changed

+55
-1
lines changed

5 files changed

+55
-1
lines changed

coverage_config_x86_64.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"coverage_score": 75.9,
2+
"coverage_score": 76.0,
33
"exclude_path": "",
44
"crate_features": ""
55
}

src/loader/mod.rs

+54
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,24 @@ mod test {
552552
v
553553
}
554554

555+
#[cfg(feature = "elf")]
556+
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
557+
fn make_elfnote() -> Vec<u8> {
558+
include_bytes!("test_elfnote.bin").to_vec()
559+
}
560+
561+
#[cfg(feature = "elf")]
562+
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
563+
fn make_dummy_elfnote() -> Vec<u8> {
564+
include_bytes!("test_dummynote.bin").to_vec()
565+
}
566+
567+
#[cfg(feature = "elf")]
568+
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
569+
fn make_bad_elfnote() -> Vec<u8> {
570+
include_bytes!("test_badnote.bin").to_vec()
571+
}
572+
555573
#[allow(safe_packed_borrows)]
556574
#[allow(non_snake_case)]
557575
#[test]
@@ -674,6 +692,42 @@ mod test {
674692
);
675693
}
676694

695+
#[cfg(feature = "elf")]
696+
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
697+
#[test]
698+
fn load_pvh() {
699+
let gm = create_guest_mem();
700+
let pvhnote_image = make_elfnote();
701+
let loader_result = Elf::load(&gm, None, &mut Cursor::new(&pvhnote_image), None).unwrap();
702+
println!(
703+
"PVH entry point at address {:8x} \n",
704+
loader_result.pvh_entry_addr.unwrap().raw_value()
705+
);
706+
assert_eq!(loader_result.pvh_entry_addr.unwrap().raw_value(), 0x1e1fe1f);
707+
}
708+
709+
#[test]
710+
#[cfg(feature = "elf")]
711+
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
712+
fn dumy_elfnote() {
713+
let gm = create_guest_mem();
714+
let dummynote_image = make_dummy_elfnote();
715+
let loader_result = Elf::load(&gm, None, &mut Cursor::new(&dummynote_image), None).unwrap();
716+
assert!(loader_result.pvh_entry_addr.is_none());
717+
}
718+
719+
#[test]
720+
#[cfg(feature = "elf")]
721+
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
722+
fn bad_elfnote() {
723+
let gm = create_guest_mem();
724+
let badnote_image = make_bad_elfnote();
725+
assert_eq!(
726+
Err(Error::InvalidPvhNote),
727+
Elf::load(&gm, None, &mut Cursor::new(&badnote_image), None)
728+
);
729+
}
730+
677731
#[test]
678732
fn cmdline_overflow() {
679733
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)