Skip to content

Commit

Permalink
pvh: Add tests for PVH note parsing
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
aljimenezb committed Mar 13, 2020
1 parent e4fdfa5 commit 7a3b22b
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 2 deletions.
2 changes: 1 addition & 1 deletion coverage_config_aarch64.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"coverage_score": 71,
"coverage_score": 70.9,
"exclude_path": "",
"crate_features": ""
}
2 changes: 1 addition & 1 deletion coverage_config_x86_64.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"coverage_score": 75.9,
"coverage_score": 76.3,
"exclude_path": "",
"crate_features": ""
}
54 changes: 54 additions & 0 deletions src/loader/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,24 @@ mod test {
v
}

#[cfg(feature = "elf")]
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
fn make_elfnote() -> Vec<u8> {
include_bytes!("test_elfnote.bin").to_vec()
}

#[cfg(feature = "elf")]
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
fn make_dummy_elfnote() -> Vec<u8> {
include_bytes!("test_dummynote.bin").to_vec()
}

#[cfg(feature = "elf")]
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
fn make_bad_elfnote() -> Vec<u8> {
include_bytes!("test_badnote.bin").to_vec()
}

#[allow(safe_packed_borrows)]
#[allow(non_snake_case)]
#[test]
Expand Down Expand Up @@ -678,6 +696,42 @@ mod test {
);
}

#[cfg(feature = "elf")]
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
#[test]
fn load_pvh() {
let gm = create_guest_mem();
let pvhnote_image = make_elfnote();
let loader_result = Elf::load(&gm, None, &mut Cursor::new(&pvhnote_image), None).unwrap();
println!(
"PVH entry point at address {:8x} \n",
loader_result.pvh_entry_addr.unwrap().raw_value()
);
assert_eq!(loader_result.pvh_entry_addr.unwrap().raw_value(), 0x1e1fe1f);
}

#[test]
#[cfg(feature = "elf")]
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
fn dumy_elfnote() {
let gm = create_guest_mem();
let dummynote_image = make_dummy_elfnote();
let loader_result = Elf::load(&gm, None, &mut Cursor::new(&dummynote_image), None).unwrap();
assert!(loader_result.pvh_entry_addr.is_none());
}

#[test]
#[cfg(feature = "elf")]
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
fn bad_elfnote() {
let gm = create_guest_mem();
let badnote_image = make_bad_elfnote();
assert_eq!(
Err(Error::InvalidPvhNote),
Elf::load(&gm, None, &mut Cursor::new(&badnote_image), None)
);
}

#[test]
fn cmdline_overflow() {
let gm = create_guest_mem();
Expand Down
Binary file added src/loader/test_badnote.bin
Binary file not shown.
Binary file added src/loader/test_dummynote.bin
Binary file not shown.
Binary file added src/loader/test_elfnote.bin
Binary file not shown.

0 comments on commit 7a3b22b

Please sign in to comment.