-
Notifications
You must be signed in to change notification settings - Fork 45
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
state.rs: Harden C enum casting #105
base: main
Are you sure you want to change the base?
Conversation
@@ -410,14 +424,14 @@ impl PriceAccount { | |||
} | |||
} | |||
|
|||
fn load<T: Pod>(data: &[u8]) -> Result<&T, PodCastError> { | |||
fn load<T: Pod>(data: &[u8]) -> Result<&T, CheckedCastError> { | |||
let size = size_of::<T>(); | |||
if data.len() >= size { | |||
Ok(from_bytes(cast_slice::<u8, u8>(try_cast_slice( |
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.
Unrelated : what is the point of all these cast_slice
and try_cast_slice
seems redundant.
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.
lgtm. Are we sure that pyth-agent and such don't crash if they can't deserialize an account (i.e., when this load function fails)? I think there's a potential failure mode here where we do a program upgrade that extends the serialization format, and then all of the data providers crash.
I think they may crash if some of the checked methods panic (the non-
As for format changes, I think it's a matter of sane versioning. I think we failed to indicate that there's a new price status (e.g. by bumping format version) and that's why we've seen the agent problem with the memory allocation. This change helps us make this more obvious in the enum case because Rust seems too confident about enum validity when transmuting from bytes. We probably should pay more attention to versioning the on-chain format, but this additional check is nice to have and prevent undefined behavior. |
This change makes it impossible to construct PriceStatus, PriceType and CorpAction enums from raw bytes that don't represent one of the valid values. All casting calls are replaced with their
checked
variants. Additionally, we explicitly define the first value of each enum and use the concrete int type corresponding with the oracle C code (u32 <=> uint32_t
).Motivation
in
pyth-agent
we've seen reports of extremely large allocation attempts which would mysteriously crash the service. It turned out that the most recentIgnored
PriceStatus field was not part of the SDK version used in agent, but the bytemuck cast would still happily assign the value. Rust would then trust this field and presumably misinterpret some out-of-bounds data as a size to allocate. Although bumping the dependency fixed the problem, with this change we will get a proper error when casting an invalid enum value, making outdated SDK dependencies easier to spot.Testing
PriceStatus
raw valuePriceStatus
raw values