Skip to content
This repository has been archived by the owner on Jul 29, 2024. It is now read-only.

Commit

Permalink
optimize stack usage on vendor request.
Browse files Browse the repository at this point in the history
fix #186

Signed-off-by: Yang, Longlong <[email protected]>
  • Loading branch information
longlongyang committed Dec 26, 2023
1 parent 3d3d79e commit a60170a
Show file tree
Hide file tree
Showing 20 changed files with 269 additions and 186 deletions.
10 changes: 9 additions & 1 deletion fuzz-target/requester/vendor_req/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,16 @@ async fn fuzz_send_spdm_vendor_defined_request(fuzzdata: Arc<Vec<u8>>) {
vendor_defined_req_payload: [0u8; config::MAX_SPDM_MSG_SIZE - 7 - 2],
};

let mut rsp_payload_struct = spdmlib::message::VendorDefinedRspPayloadStruct::default();

let _ = requester
.send_spdm_vendor_defined_request(None, standard_id, vendor_idstruct, req_payload_struct)
.send_spdm_vendor_defined_request(
None,
standard_id,
vendor_idstruct,
&req_payload_struct,
&mut rsp_payload_struct,
)
.await
.is_ok();
}
Expand Down
10 changes: 6 additions & 4 deletions idekm/src/pci_ide_km_requester/pci_ide_km_req_key_prog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ impl IdekmReqContext {
key_iv: Aes256GcmKeyBuffer,
// OUT
kp_ack_status: &mut KpAckStatus,
rsp_payload_struct: &mut spdmlib::message::VendorDefinedRspPayloadStruct,
) -> SpdmResult {
let mut vendor_defined_req_payload_struct = VendorDefinedReqPayloadStruct {
req_length: 0,
Expand All @@ -56,18 +57,19 @@ impl IdekmReqContext {
.map_err(|_| SPDM_STATUS_BUFFER_FULL)?
as u16;

let vendor_defined_rsp_payload_struct = spdm_requester
spdm_requester
.send_spdm_vendor_defined_request(
Some(session_id),
STANDARD_ID,
vendor_id(),
vendor_defined_req_payload_struct,
&vendor_defined_req_payload_struct,
rsp_payload_struct,
)
.await?;

let kp_ack_data_object = KpAckDataObject::read_bytes(
&vendor_defined_rsp_payload_struct.vendor_defined_rsp_payload
[..vendor_defined_rsp_payload_struct.rsp_length as usize],
&rsp_payload_struct.vendor_defined_rsp_payload
[..rsp_payload_struct.rsp_length as usize],
)
.ok_or(SPDM_STATUS_INVALID_MSG_FIELD)?;

Expand Down
10 changes: 6 additions & 4 deletions idekm/src/pci_ide_km_requester/pci_ide_km_req_key_set_go.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ impl IdekmReqContext {
key_direction: u8,
key_sub_stream: u8,
port_index: u8,
rsp_payload_struct: &mut spdmlib::message::VendorDefinedRspPayloadStruct,
) -> SpdmResult {
let mut vendor_defined_req_payload_struct = VendorDefinedReqPayloadStruct {
req_length: 0,
Expand All @@ -52,18 +53,19 @@ impl IdekmReqContext {
.map_err(|_| SPDM_STATUS_BUFFER_FULL)?
as u16;

let vendor_defined_rsp_payload_struct = spdm_requester
spdm_requester
.send_spdm_vendor_defined_request(
Some(session_id),
STANDARD_ID,
vendor_id(),
vendor_defined_req_payload_struct,
&vendor_defined_req_payload_struct,
rsp_payload_struct,
)
.await?;

let kgo_stop_ack_data_object = KGoStopAckDataObject::read_bytes(
&vendor_defined_rsp_payload_struct.vendor_defined_rsp_payload
[..vendor_defined_rsp_payload_struct.rsp_length as usize],
&rsp_payload_struct.vendor_defined_rsp_payload
[..rsp_payload_struct.rsp_length as usize],
)
.ok_or(SPDM_STATUS_INVALID_MSG_FIELD)?;

Expand Down
10 changes: 6 additions & 4 deletions idekm/src/pci_ide_km_requester/pci_ide_km_req_key_set_stop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ impl IdekmReqContext {
key_direction: u8,
key_sub_stream: u8,
port_index: u8,
rsp_payload_struct: &mut spdmlib::message::VendorDefinedRspPayloadStruct,
) -> SpdmResult {
let mut vendor_defined_req_payload_struct = VendorDefinedReqPayloadStruct {
req_length: 0,
Expand All @@ -52,18 +53,19 @@ impl IdekmReqContext {
.map_err(|_| SPDM_STATUS_BUFFER_FULL)?
as u16;

let vendor_defined_rsp_payload_struct = spdm_requester
spdm_requester
.send_spdm_vendor_defined_request(
Some(session_id),
STANDARD_ID,
vendor_id(),
vendor_defined_req_payload_struct,
&vendor_defined_req_payload_struct,
rsp_payload_struct,
)
.await?;

let kgo_stop_ack_data_object = KGoStopAckDataObject::read_bytes(
&vendor_defined_rsp_payload_struct.vendor_defined_rsp_payload
[..vendor_defined_rsp_payload_struct.rsp_length as usize],
&rsp_payload_struct.vendor_defined_rsp_payload
[..rsp_payload_struct.rsp_length as usize],
)
.ok_or(SPDM_STATUS_INVALID_MSG_FIELD)?;

Expand Down
10 changes: 6 additions & 4 deletions idekm/src/pci_ide_km_requester/pci_ide_km_req_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ impl IdekmReqContext {
max_port_index: &mut u8,
ide_reg_block: &mut [u32; PCI_IDE_KM_IDE_REG_BLOCK_MAX_COUNT],
ide_reg_block_cnt: &mut usize,
rsp_payload_struct: &mut spdmlib::message::VendorDefinedRspPayloadStruct,
) -> SpdmResult {
let mut vendor_defined_req_payload_struct = VendorDefinedReqPayloadStruct {
req_length: 0,
Expand All @@ -46,18 +47,19 @@ impl IdekmReqContext {
.map_err(|_| SPDM_STATUS_BUFFER_FULL)?
as u16;

let vendor_defined_rsp_payload_struct = spdm_requester
spdm_requester
.send_spdm_vendor_defined_request(
Some(session_id),
STANDARD_ID,
vendor_id(),
vendor_defined_req_payload_struct,
&vendor_defined_req_payload_struct,
rsp_payload_struct,
)
.await?;

let query_resp_data_object = QueryRespDataObject::read_bytes(
&vendor_defined_rsp_payload_struct.vendor_defined_rsp_payload
[..vendor_defined_rsp_payload_struct.rsp_length as usize],
&rsp_payload_struct.vendor_defined_rsp_payload
[..rsp_payload_struct.rsp_length as usize],
)
.ok_or(SPDM_STATUS_INVALID_MSG_FIELD)?;

Expand Down
9 changes: 9 additions & 0 deletions spdmlib/src/message/vendor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,15 @@ pub struct VendorDefinedRspPayloadStruct {
pub vendor_defined_rsp_payload: [u8; MAX_SPDM_VENDOR_DEFINED_PAYLOAD_SIZE],
}

impl Default for VendorDefinedRspPayloadStruct {
fn default() -> Self {
Self {
rsp_length: 0,
vendor_defined_rsp_payload: [0u8; MAX_SPDM_VENDOR_DEFINED_PAYLOAD_SIZE],
}
}
}

impl Codec for VendorDefinedRspPayloadStruct {
fn encode(&self, bytes: &mut Writer) -> Result<usize, codec::EncodeErr> {
let mut cnt = 0usize;
Expand Down
20 changes: 14 additions & 6 deletions spdmlib/src/requester/vendor_req.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ impl RequesterContext {
session_id: Option<u32>,
standard_id: RegistryOrStandardsBodyID,
vendor_id_struct: VendorIDStruct,
req_payload_struct: VendorDefinedReqPayloadStruct,
) -> SpdmResult<VendorDefinedRspPayloadStruct> {
req_payload_struct: &VendorDefinedReqPayloadStruct,
rsp_payload_struct: &mut VendorDefinedRspPayloadStruct,
) -> SpdmResult {
info!("send vendor defined request\n");

self.common.reset_buffer_via_request_code(
Expand All @@ -33,7 +34,7 @@ impl RequesterContext {
SpdmVendorDefinedRequestPayload {
standard_id,
vendor_id: vendor_id_struct,
req_payload: req_payload_struct,
req_payload: req_payload_struct.clone(),
},
),
};
Expand All @@ -48,14 +49,19 @@ impl RequesterContext {
.receive_message(session_id, &mut receive_buffer, false)
.await?;

self.handle_spdm_vendor_defined_respond(session_id, &receive_buffer[..receive_used])
self.handle_spdm_vendor_defined_respond(
session_id,
&receive_buffer[..receive_used],
rsp_payload_struct,
)
}

pub fn handle_spdm_vendor_defined_respond(
&mut self,
session_id: Option<u32>,
receive_buffer: &[u8],
) -> SpdmResult<VendorDefinedRspPayloadStruct> {
rsp_payload_struct: &mut VendorDefinedRspPayloadStruct,
) -> SpdmResult {
let mut reader = Reader::init(receive_buffer);
match SpdmMessageHeader::read(&mut reader) {
Some(message_header) => {
Expand All @@ -69,7 +75,9 @@ impl RequesterContext {
&mut reader,
) {
Some(spdm_vendor_defined_response_payload) => {
Ok(spdm_vendor_defined_response_payload.rsp_payload)
*rsp_payload_struct =
spdm_vendor_defined_response_payload.rsp_payload;
Ok(())
}
None => Err(SPDM_STATUS_INVALID_MSG_FIELD),
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ pub async fn pci_tdisp_req_bind_p2p_stream_request(
p2p_stream_id: u8,
// OUT
tdisp_error_code: &mut Option<TdispErrorCode>,
rsp_payload_struct: &mut spdmlib::message::VendorDefinedRspPayloadStruct,
) -> SpdmResult {
let mut vendor_defined_req_payload_struct = VendorDefinedReqPayloadStruct {
req_length: 0,
Expand All @@ -56,18 +57,18 @@ pub async fn pci_tdisp_req_bind_p2p_stream_request(
.map_err(|_| SPDM_STATUS_BUFFER_FULL)?
as u16;

let vendor_defined_rsp_payload_struct = spdm_requester
spdm_requester
.send_spdm_vendor_defined_request(
Some(session_id),
STANDARD_ID,
vendor_id(),
vendor_defined_req_payload_struct,
&vendor_defined_req_payload_struct,
rsp_payload_struct,
)
.await?;

if let Ok(tdisp_error) = RspTdispError::read_bytes(
&vendor_defined_rsp_payload_struct.vendor_defined_rsp_payload
[..vendor_defined_rsp_payload_struct.rsp_length as usize],
&rsp_payload_struct.vendor_defined_rsp_payload[..rsp_payload_struct.rsp_length as usize],
)
.ok_or(SPDM_STATUS_INVALID_MSG_FIELD)
{
Expand All @@ -76,8 +77,7 @@ pub async fn pci_tdisp_req_bind_p2p_stream_request(
}

let rsp_bind_p2_pstream_response = RspBindP2PStreamResponse::read_bytes(
&vendor_defined_rsp_payload_struct.vendor_defined_rsp_payload
[..vendor_defined_rsp_payload_struct.rsp_length as usize],
&rsp_payload_struct.vendor_defined_rsp_payload[..rsp_payload_struct.rsp_length as usize],
)
.ok_or(SPDM_STATUS_INVALID_MSG_FIELD)?;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ pub async fn pci_tdisp_req_get_device_interface_report(
report: &mut [u8; MAX_DEVICE_REPORT_BUFFER],
report_size: &mut usize,
tdisp_error_code: &mut Option<TdispErrorCode>,
rsp_payload_struct: &mut spdmlib::message::VendorDefinedRspPayloadStruct,
) -> SpdmResult {
let mut offset = 0u16;
let length = MAX_PORTION_LENGTH as u16;
Expand Down Expand Up @@ -67,18 +68,19 @@ pub async fn pci_tdisp_req_get_device_interface_report(
.map_err(|_| SPDM_STATUS_BUFFER_FULL)?
as u16;

let vendor_defined_rsp_payload_struct = spdm_requester
spdm_requester
.send_spdm_vendor_defined_request(
Some(session_id),
STANDARD_ID,
vendor_id(),
vendor_defined_req_payload_struct,
&vendor_defined_req_payload_struct,
rsp_payload_struct,
)
.await?;

if let Ok(tdisp_error) = RspTdispError::read_bytes(
&vendor_defined_rsp_payload_struct.vendor_defined_rsp_payload
[..vendor_defined_rsp_payload_struct.rsp_length as usize],
&rsp_payload_struct.vendor_defined_rsp_payload
[..rsp_payload_struct.rsp_length as usize],
)
.ok_or(SPDM_STATUS_INVALID_MSG_FIELD)
{
Expand All @@ -87,8 +89,8 @@ pub async fn pci_tdisp_req_get_device_interface_report(
}

let rsp_device_interface_report = RspDeviceInterfaceReport::read_bytes(
&vendor_defined_rsp_payload_struct.vendor_defined_rsp_payload
[..vendor_defined_rsp_payload_struct.rsp_length as usize],
&rsp_payload_struct.vendor_defined_rsp_payload
[..rsp_payload_struct.rsp_length as usize],
)
.ok_or(SPDM_STATUS_INVALID_MSG_FIELD)?;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ pub async fn pci_tdisp_req_get_device_interface_state(
interface_id: InterfaceId,
// OUT
tdi_state: &mut TdiState,
rsp_payload_struct: &mut spdmlib::message::VendorDefinedRspPayloadStruct,
) -> SpdmResult {
let mut vendor_defined_req_payload_struct = VendorDefinedReqPayloadStruct {
req_length: 0,
Expand All @@ -53,18 +54,18 @@ pub async fn pci_tdisp_req_get_device_interface_state(
.map_err(|_| SPDM_STATUS_BUFFER_FULL)?
as u16;

let vendor_defined_rsp_payload_struct = spdm_requester
spdm_requester
.send_spdm_vendor_defined_request(
Some(session_id),
STANDARD_ID,
vendor_id(),
vendor_defined_req_payload_struct,
&vendor_defined_req_payload_struct,
rsp_payload_struct,
)
.await?;

let rsp_device_interface_state = RspDeviceInterfaceState::read_bytes(
&vendor_defined_rsp_payload_struct.vendor_defined_rsp_payload
[..vendor_defined_rsp_payload_struct.rsp_length as usize],
&rsp_payload_struct.vendor_defined_rsp_payload[..rsp_payload_struct.rsp_length as usize],
)
.ok_or(SPDM_STATUS_INVALID_MSG_FIELD)?;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ pub async fn pci_tdisp_req_get_tdisp_capabilities(
num_req_this: &mut u8,
num_req_all: &mut u8,
req_msgs_supported: &mut [u8; 16],
rsp_payload_struct: &mut spdmlib::message::VendorDefinedRspPayloadStruct,
) -> SpdmResult {
let mut vendor_defined_req_payload_struct = VendorDefinedReqPayloadStruct {
req_length: 0,
Expand All @@ -60,18 +61,18 @@ pub async fn pci_tdisp_req_get_tdisp_capabilities(
.encode(&mut writer)
.map_err(|_| SPDM_STATUS_BUFFER_FULL)?
as u16;
let vendor_defined_rsp_payload_struct = spdm_requester
spdm_requester
.send_spdm_vendor_defined_request(
Some(session_id),
STANDARD_ID,
vendor_id(),
vendor_defined_req_payload_struct,
&vendor_defined_req_payload_struct,
rsp_payload_struct,
)
.await?;

let rsp_tdisp_capabilities = RspTdispCapabilities::read_bytes(
&vendor_defined_rsp_payload_struct.vendor_defined_rsp_payload
[..vendor_defined_rsp_payload_struct.rsp_length as usize],
&rsp_payload_struct.vendor_defined_rsp_payload[..rsp_payload_struct.rsp_length as usize],
)
.ok_or(SPDM_STATUS_INVALID_MSG_FIELD)?;

Expand Down
Loading

0 comments on commit a60170a

Please sign in to comment.