Skip to content

Commit d964fbc

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 2ca1049 commit d964fbc

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

src/loader/mod.rs

+54
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,24 @@ mod test {
545545
v
546546
}
547547

548+
#[cfg(feature = "elf")]
549+
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
550+
fn make_elfnote() -> Vec<u8> {
551+
include_bytes!("test_elfnote.bin").to_vec()
552+
}
553+
554+
#[cfg(feature = "elf")]
555+
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
556+
fn make_dummy_elfnote() -> Vec<u8> {
557+
include_bytes!("test_dummynote.bin").to_vec()
558+
}
559+
560+
#[cfg(feature = "elf")]
561+
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
562+
fn make_bad_elfnote() -> Vec<u8> {
563+
include_bytes!("test_badnote.bin").to_vec()
564+
}
565+
548566
#[allow(safe_packed_borrows)]
549567
#[allow(non_snake_case)]
550568
#[test]
@@ -667,6 +685,42 @@ mod test {
667685
);
668686
}
669687

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