-
-
Notifications
You must be signed in to change notification settings - Fork 82
Directly test a stream of opcodes #298
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
martin-hughes
wants to merge
3
commits into
rust-osdev:main
Choose a base branch
from
martin-hughes:raw-opcodes-test
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,148 @@ | ||
| //! pkglength encoding and decoding. | ||
|
|
||
| use alloc::{vec, vec::Vec}; | ||
| use bit_field::BitField; | ||
| use core::fmt::{Display, Formatter}; | ||
|
|
||
| /// Indicates an attempt to encode a pkglength that is too long (>= 2 ^ 28). | ||
| #[derive(Clone, Debug)] | ||
| pub struct PkglengthTooLongError {} | ||
|
|
||
| impl Display for PkglengthTooLongError { | ||
| fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result { | ||
| write!(f, "pkglength too long") | ||
| } | ||
| } | ||
|
|
||
| impl core::error::Error for PkglengthTooLongError {} | ||
|
|
||
| /// Encode a pkglength field. | ||
| /// | ||
| /// This is a variable length field used to define the length of other variable-length items in | ||
| /// ACPI - see [the spec](https://uefi.org/specs/ACPI/6.6/20_AML_Specification.html#package-length-encoding) | ||
| /// for details. | ||
| /// | ||
| /// This is less straightforward than it could be, because pkglength needs to include the length of | ||
| /// the pkglength field that gets output. This function adds that extra length. | ||
| /// | ||
| /// Returns an error if length >= 2^28. Otherwise, returns the encoded pkglength in a vec, LSB-first. | ||
| pub fn encode(data_length: u32) -> Result<Vec<u8>, PkglengthTooLongError> { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this probably belongs in |
||
| let extra_length = match data_length { | ||
| 0..63 => 1, | ||
| 63..0xFFE => 2, | ||
| 0xFFE..0xFFFFD => 3, | ||
| _ => 4, | ||
| }; | ||
| let result = encode_raw(data_length + extra_length); | ||
| result.inspect(|result| { | ||
| assert_eq!(result.len(), extra_length as usize); | ||
| }) | ||
| } | ||
|
|
||
| /// Encode a pkglength field, without taking into account the extra length of the pkglength field. | ||
| /// | ||
| /// Most callers should use [`encode`] instead. | ||
| /// | ||
| /// Returns an error if length >= 2^28. Otherwise, returns the encoded pkglength in a vec, LSB-first. | ||
| pub fn encode_raw(mut length: u32) -> Result<Vec<u8>, PkglengthTooLongError> { | ||
| if length & 0xF0000000 != 0 { | ||
| // Must be less than 2 ^ 28 | ||
| return Err(PkglengthTooLongError {}); | ||
| } | ||
|
|
||
| if length < 64 { | ||
| Ok(vec![length as u8]) | ||
| } else { | ||
| let mut result = vec![(length & 0xF) as u8]; | ||
| length >>= 4; | ||
|
|
||
| while length != 0 { | ||
| result.push((length & 0xff) as u8); | ||
| length >>= 8; | ||
| } | ||
|
|
||
| let num_bytes = result.len() as u8 - 1; | ||
| result[0] |= num_bytes << 6; | ||
|
|
||
| Ok(result) | ||
| } | ||
| } | ||
|
|
||
| /// Decode a pkglength field from a stream | ||
| /// | ||
| /// If the stream returns an error, that error is returned. Otherwise, the decoded length is | ||
| /// returned. | ||
| /// | ||
| /// `stream_next` must return the next byte in the stream when called (or an error, which is passed | ||
| /// through) | ||
| pub fn decode_stream<T>(mut stream_next: impl FnMut() -> Result<u8, T>) -> Result<usize, T> { | ||
| let lead_byte = stream_next()?; | ||
| let byte_count = lead_byte.get_bits(6..8); | ||
| assert!(byte_count < 4); | ||
|
|
||
| if byte_count == 0 { | ||
| Ok(lead_byte.get_bits(0..6) as usize) | ||
| } else { | ||
| let mut length = lead_byte.get_bits(0..4) as usize; | ||
| for i in 0..byte_count { | ||
| length |= (stream_next()? as usize) << (4 + i * 8); | ||
| } | ||
| Ok(length) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn basic_round_trip() { | ||
| let length: u32 = 0x12345; | ||
| let encoded = encode_raw(length).unwrap(); | ||
| let mut i = encoded.iter(); | ||
| let decoded = decode_stream(|| i.next().copied().ok_or(())).unwrap(); | ||
| assert_eq!(decoded, length as usize); | ||
| } | ||
|
|
||
| #[test] | ||
| fn less_than_64() { | ||
| let length: u32 = 0x12; | ||
| let encoded = encode_raw(length).unwrap(); | ||
| assert_eq!(encoded, vec![0x12]); | ||
|
|
||
| let mut i = encoded.iter(); | ||
| let decoded = decode_stream(|| i.next().copied().ok_or(())).unwrap(); | ||
| assert_eq!(decoded, 0x12); | ||
| } | ||
|
|
||
| #[test] | ||
| fn encodes_zero() { | ||
| let encoded = encode_raw(0).unwrap(); | ||
| assert_eq!(encoded, vec![0]); | ||
| } | ||
|
|
||
| fn round_trip_length(length: u32) -> usize { | ||
| let encoded = encode(length).unwrap(); | ||
| let mut i = encoded.iter(); | ||
| decode_stream(|| i.next().copied().ok_or(())).unwrap() | ||
| } | ||
|
|
||
| #[test] | ||
| fn extra_length_correct() { | ||
| assert_eq!(round_trip_length(62), 63); // An increase of a single byte | ||
| // 63 bytes of payload requires two bytes of pkglength | ||
| assert_eq!(round_trip_length(63), 65); | ||
|
|
||
| // A random test: | ||
| assert_eq!(round_trip_length(0x12345), 0x12348); | ||
|
|
||
| // Simply check that there's no errors around the boundaries - this tells us the maths to | ||
| // calculate `extra_length` is correct. | ||
| for i in 0xFF9..0x1004 { | ||
| encode(i).unwrap(); | ||
| } | ||
| for i in 0xFFFF9..0x100004 { | ||
| encode(i).unwrap(); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| mod test_infra; | ||
|
|
||
| use crate::test_infra::run_opcodes_test; | ||
| use aml_test_tools::handlers::null_handler::NullHandler; | ||
|
|
||
| #[test] | ||
| fn test_stray_opcode() { | ||
| // Make sure that a stray `One` opcode is ignored (see issue #289) | ||
| // The first opcode is `One`, the remainder are `Return (0)`. | ||
| let opcodes: Vec<u8> = vec![0x01, 0xA4, 0x0A, 0x00]; | ||
|
|
||
| let handler = NullHandler; | ||
| run_opcodes_test(&opcodes, handler); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is another tricky case re improving testability vs increasing cognitive overhead to the interpreter.
I don't think the payoff of the separate module + the closure adds enough value vs being able to see this instinctually as a loop munching a variable-size length specifier. Breaking pkglength decoding would so fundamentally break the AML tests that I don't think unit tests add much here.