Skip to content

Commit

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

Signed-off-by: Yang, Longlong <[email protected]>
  • Loading branch information
longlongyang committed Dec 24, 2023
1 parent 05c41c6 commit 4d32890
Show file tree
Hide file tree
Showing 21 changed files with 976 additions and 894 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 vendor_defined_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 vendor_defined_rsp_payload_struct,
)
.await
.is_ok();
}
Expand Down
7 changes: 5 additions & 2 deletions idekm/src/pci_ide_km_requester/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
//
// SPDX-License-Identifier: Apache-2.0 or MIT

#[derive(Debug, Default, Copy, Clone)]
pub struct IdekmReqContext;
#[derive(Debug, Default, Clone)]
pub struct IdekmReqContext {
pub vendor_defined_req_payload_struct: spdmlib::message::VendorDefinedReqPayloadStruct,
pub vendor_defined_rsp_payload_struct: spdmlib::message::VendorDefinedRspPayloadStruct,
}

pub mod pci_ide_km_req_query;
pub use pci_ide_km_req_query::*;
Expand Down
30 changes: 14 additions & 16 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 @@ -7,10 +7,7 @@ use codec::Writer;
use spdmlib::error::SpdmResult;
use spdmlib::error::SPDM_STATUS_BUFFER_FULL;
use spdmlib::error::SPDM_STATUS_INVALID_MSG_FIELD;
use spdmlib::{
message::{VendorDefinedReqPayloadStruct, MAX_SPDM_VENDOR_DEFINED_PAYLOAD_SIZE},
requester::RequesterContext,
};
use spdmlib::requester::RequesterContext;

use crate::pci_idekm::vendor_id;
use crate::pci_idekm::KpAckDataObject;
Expand All @@ -36,15 +33,13 @@ impl IdekmReqContext {
// OUT
kp_ack_status: &mut KpAckStatus,
) -> SpdmResult {
let mut vendor_defined_req_payload_struct = VendorDefinedReqPayloadStruct {
req_length: 0,
vendor_defined_req_payload: [0u8; MAX_SPDM_VENDOR_DEFINED_PAYLOAD_SIZE],
};
let mut writer = Writer::init(
&mut self
.vendor_defined_req_payload_struct
.vendor_defined_req_payload,
);

let mut writer =
Writer::init(&mut vendor_defined_req_payload_struct.vendor_defined_req_payload);

vendor_defined_req_payload_struct.req_length = KeyProgDataObject {
self.vendor_defined_req_payload_struct.req_length = KeyProgDataObject {
stream_id,
key_set,
key_direction,
Expand All @@ -56,18 +51,21 @@ 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,
&self.vendor_defined_req_payload_struct,
&mut self.vendor_defined_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],
&self
.vendor_defined_rsp_payload_struct
.vendor_defined_rsp_payload
[..self.vendor_defined_rsp_payload_struct.rsp_length as usize],
)
.ok_or(SPDM_STATUS_INVALID_MSG_FIELD)?;

Expand Down
31 changes: 14 additions & 17 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 @@ -6,11 +6,7 @@ use codec::Codec;
use codec::Writer;
use spdmlib::error::SPDM_STATUS_BUFFER_FULL;
use spdmlib::error::SPDM_STATUS_INVALID_MSG_FIELD;
use spdmlib::{
error::SpdmResult,
message::{VendorDefinedReqPayloadStruct, MAX_SPDM_VENDOR_DEFINED_PAYLOAD_SIZE},
requester::RequesterContext,
};
use spdmlib::{error::SpdmResult, requester::RequesterContext};

use crate::pci_idekm::vendor_id;
use crate::pci_idekm::KGoStopAckDataObject;
Expand All @@ -33,15 +29,13 @@ impl IdekmReqContext {
key_sub_stream: u8,
port_index: u8,
) -> SpdmResult {
let mut vendor_defined_req_payload_struct = VendorDefinedReqPayloadStruct {
req_length: 0,
vendor_defined_req_payload: [0u8; MAX_SPDM_VENDOR_DEFINED_PAYLOAD_SIZE],
};
let mut writer = Writer::init(
&mut self
.vendor_defined_req_payload_struct
.vendor_defined_req_payload,
);

let mut writer =
Writer::init(&mut vendor_defined_req_payload_struct.vendor_defined_req_payload);

vendor_defined_req_payload_struct.req_length = KSetGoDataObject {
self.vendor_defined_req_payload_struct.req_length = KSetGoDataObject {
stream_id,
key_set,
key_direction,
Expand All @@ -52,18 +46,21 @@ 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,
&self.vendor_defined_req_payload_struct,
&mut self.vendor_defined_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],
&self
.vendor_defined_rsp_payload_struct
.vendor_defined_rsp_payload
[..self.vendor_defined_rsp_payload_struct.rsp_length as usize],
)
.ok_or(SPDM_STATUS_INVALID_MSG_FIELD)?;

Expand Down
31 changes: 14 additions & 17 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 @@ -6,11 +6,7 @@ use codec::Codec;
use codec::Writer;
use spdmlib::error::SPDM_STATUS_BUFFER_FULL;
use spdmlib::error::SPDM_STATUS_INVALID_MSG_FIELD;
use spdmlib::{
error::SpdmResult,
message::{VendorDefinedReqPayloadStruct, MAX_SPDM_VENDOR_DEFINED_PAYLOAD_SIZE},
requester::RequesterContext,
};
use spdmlib::{error::SpdmResult, requester::RequesterContext};

use crate::pci_idekm::vendor_id;
use crate::pci_idekm::KGoStopAckDataObject;
Expand All @@ -33,15 +29,13 @@ impl IdekmReqContext {
key_sub_stream: u8,
port_index: u8,
) -> SpdmResult {
let mut vendor_defined_req_payload_struct = VendorDefinedReqPayloadStruct {
req_length: 0,
vendor_defined_req_payload: [0u8; MAX_SPDM_VENDOR_DEFINED_PAYLOAD_SIZE],
};
let mut writer = Writer::init(
&mut self
.vendor_defined_req_payload_struct
.vendor_defined_req_payload,
);

let mut writer =
Writer::init(&mut vendor_defined_req_payload_struct.vendor_defined_req_payload);

vendor_defined_req_payload_struct.req_length = KSetStopDataObject {
self.vendor_defined_req_payload_struct.req_length = KSetStopDataObject {
stream_id,
key_set,
key_direction,
Expand All @@ -52,18 +46,21 @@ 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,
&self.vendor_defined_req_payload_struct,
&mut self.vendor_defined_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],
&self
.vendor_defined_rsp_payload_struct
.vendor_defined_rsp_payload
[..self.vendor_defined_rsp_payload_struct.rsp_length as usize],
)
.ok_or(SPDM_STATUS_INVALID_MSG_FIELD)?;

Expand Down
26 changes: 13 additions & 13 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 @@ -5,7 +5,6 @@
use codec::{Codec, Writer};
use spdmlib::{
error::{SpdmResult, SPDM_STATUS_BUFFER_FULL, SPDM_STATUS_INVALID_MSG_FIELD},
message::{VendorDefinedReqPayloadStruct, MAX_SPDM_VENDOR_DEFINED_PAYLOAD_SIZE},
requester::RequesterContext,
};

Expand Down Expand Up @@ -33,31 +32,32 @@ impl IdekmReqContext {
ide_reg_block: &mut [u32; PCI_IDE_KM_IDE_REG_BLOCK_MAX_COUNT],
ide_reg_block_cnt: &mut usize,
) -> SpdmResult {
let mut vendor_defined_req_payload_struct = VendorDefinedReqPayloadStruct {
req_length: 0,
vendor_defined_req_payload: [0u8; MAX_SPDM_VENDOR_DEFINED_PAYLOAD_SIZE],
};
let mut writer = Writer::init(
&mut self
.vendor_defined_req_payload_struct
.vendor_defined_req_payload,
);

let mut writer =
Writer::init(&mut vendor_defined_req_payload_struct.vendor_defined_req_payload);

vendor_defined_req_payload_struct.req_length = QueryDataObject { port_index }
self.vendor_defined_req_payload_struct.req_length = QueryDataObject { port_index }
.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,
&self.vendor_defined_req_payload_struct,
&mut self.vendor_defined_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],
&self
.vendor_defined_rsp_payload_struct
.vendor_defined_rsp_payload
[..self.vendor_defined_rsp_payload_struct.rsp_length as usize],
)
.ok_or(SPDM_STATUS_INVALID_MSG_FIELD)?;

Expand Down
18 changes: 18 additions & 0 deletions spdmlib/src/message/vendor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,15 @@ impl Codec for VendorDefinedReqPayloadStruct {
}
}

impl Default for VendorDefinedReqPayloadStruct {
fn default() -> Self {
Self {
req_length: 0,
vendor_defined_req_payload: [0u8; MAX_SPDM_VENDOR_DEFINED_PAYLOAD_SIZE],
}
}
}

#[derive(Debug, Clone, ZeroizeOnDrop)]
pub struct VendorDefinedRspPayloadStruct {
pub rsp_length: u16,
Expand Down Expand Up @@ -174,6 +183,15 @@ impl Codec for VendorDefinedRspPayloadStruct {
}
}

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

#[derive(Debug, Clone)]
pub struct SpdmVendorDefinedRequestPayload {
pub standard_id: RegistryOrStandardsBodyID,
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
6 changes: 6 additions & 0 deletions tdisp/src/pci_tdisp_requester/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@

use crate::pci_tdisp::{InterfaceId, TdispVersion};

#[derive(Debug, Default, Clone)]
pub struct TdispReqContext {
pub vendor_defined_req_payload_struct: spdmlib::message::VendorDefinedReqPayloadStruct,
pub vendor_defined_rsp_payload_struct: spdmlib::message::VendorDefinedRspPayloadStruct,
}

pub mod pci_tdisp_req_get_tdisp_version;
pub use pci_tdisp_req_get_tdisp_version::*;

Expand Down
Loading

0 comments on commit 4d32890

Please sign in to comment.