Skip to content

Commit

Permalink
tdx-tdcall: add some unit test cases
Browse files Browse the repository at this point in the history
Add some unit test cases for tdx-tdcall crate.

Signed-off-by: Liu Jiang <[email protected]>
  • Loading branch information
jiangliu authored and jyao1 committed Feb 5, 2022
1 parent b758d33 commit 55f5bf3
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 14 deletions.
57 changes: 44 additions & 13 deletions tdx-tdcall/src/tdreport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,15 @@ impl TdxReport {
}
}

impl Default for TdxReport {
fn default() -> Self {
unsafe { zeroed() }
}
}

struct TdxReportBuf {
buf: [u8; TD_REPORT_BUFF_SIZE],
start: usize,
offset: usize,
end: usize,
additional: usize,
Expand All @@ -165,19 +172,25 @@ impl TdxReportBuf {
fn new() -> Self {
let mut buf = TdxReportBuf {
buf: [0u8; TD_REPORT_BUFF_SIZE],
start: 0,
offset: 0,
end: 0,
additional: 0,
};
let pos = buf.buf.as_ptr() as *const u8 as usize;

buf.offset = TD_REPORT_SIZE - (pos & (TD_REPORT_SIZE - 1));
buf.end = buf.offset + TD_REPORT_SIZE;
buf.additional = buf.end + TD_REPORT_ADDITIONAL_DATA_SIZE;

buf.adjust();
buf
}

fn adjust(&mut self) {
let pos = self.buf.as_ptr() as *const u8 as usize;
if pos != self.start {
self.start = pos;
self.offset = TD_REPORT_SIZE - (pos & (TD_REPORT_SIZE - 1));
self.end = self.offset + TD_REPORT_SIZE;
self.additional = self.end + TD_REPORT_ADDITIONAL_DATA_SIZE;
}
}

fn report_buf_start(&mut self) -> u64 {
&mut self.buf[self.offset] as *mut u8 as u64
}
Expand All @@ -195,22 +208,18 @@ impl TdxReportBuf {
}
}

impl Default for TdxReport {
fn default() -> Self {
unsafe { zeroed() }
}
}

lazy_static! {
static ref TD_REPORT: Mutex<TdxReportBuf> = Mutex::new(TdxReportBuf::new());
}

/// Query TDX report information.
pub fn tdcall_report(additional_data: &[u8; TD_REPORT_ADDITIONAL_DATA_SIZE]) -> TdxReport {
let mut buff = TD_REPORT.lock();
let addr = buff.report_buf_start();
buff.adjust();

let addr = buff.report_buf_start();
buff.additional_buf_mut().copy_from_slice(additional_data);

let ret = unsafe {
tdx::td_call(
tdx::TDCALL_TDREPORT,
Expand All @@ -233,3 +242,25 @@ pub fn tdreport_dump() {
let tdx_report = tdcall_report(&addtional_data);
log::info!("{}", tdx_report);
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_tdx_report_size() {
assert_eq!(size_of::<TdxReport>(), 0x400);
}

#[test]
fn test_tdx_report_buf() {
let mut buf = TdxReportBuf::new();
assert_eq!(buf.report_buf_start() & 0x3ff, 0);

let additional = buf.additional_buf_mut().as_ptr() as u64;
assert_eq!(buf.report_buf_start() + 0x400, additional);

TD_REPORT.lock().adjust();
assert_eq!(TD_REPORT.lock().report_buf_start() & 0x3ff, 0);
}
}
18 changes: 17 additions & 1 deletion tdx-tdcall/src/tdx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ lazy_static! {
static ref SHARED_MASK: u64 = td_shared_page_mask();
}

#[repr(align(64))]
// Both alignment and size are 64 bytes.
#[repr(C, align(64))]
pub struct TdxDigest {
pub data: [u8; 48],
}
Expand Down Expand Up @@ -283,3 +284,18 @@ pub fn td_shared_page_mask() -> u64 {
assert!((gpaw == 48 || gpaw == 52));
1u64 << (gpaw - 1)
}

#[cfg(test)]
mod tests {
use super::*;
use core::mem::{align_of, size_of};

#[test]
fn test_struct_size_alignment() {
assert_eq!(align_of::<TdxDigest>(), 64);
assert_eq!(size_of::<TdxDigest>(), 64);
assert_eq!(size_of::<TdCallGenericReturnData>(), 48);
assert_eq!(size_of::<TdInfoReturnData>(), 48);
assert_eq!(size_of::<TdVeInfoReturnData>(), 48);
}
}

0 comments on commit 55f5bf3

Please sign in to comment.