Skip to content

Commit 7a3b22b

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 e4fdfa5 commit 7a3b22b

6 files changed

+56
-2
lines changed

coverage_config_aarch64.json

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

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.3,
33
"exclude_path": "",
44
"crate_features": ""
55
}

src/loader/mod.rs

+54
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,24 @@ mod test {
556556
v
557557
}
558558

559+
#[cfg(feature = "elf")]
560+
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
561+
fn make_elfnote() -> Vec<u8> {
562+
include_bytes!("test_elfnote.bin").to_vec()
563+
}
564+
565+
#[cfg(feature = "elf")]
566+
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
567+
fn make_dummy_elfnote() -> Vec<u8> {
568+
include_bytes!("test_dummynote.bin").to_vec()
569+
}
570+
571+
#[cfg(feature = "elf")]
572+
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
573+
fn make_bad_elfnote() -> Vec<u8> {
574+
include_bytes!("test_badnote.bin").to_vec()
575+
}
576+
559577
#[allow(safe_packed_borrows)]
560578
#[allow(non_snake_case)]
561579
#[test]
@@ -678,6 +696,42 @@ mod test {
678696
);
679697
}
680698

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