From 65ccdebc675a366042cbf93c94631cbe29d110a5 Mon Sep 17 00:00:00 2001 From: kevinheavey Date: Sat, 28 Sep 2024 20:02:19 +0400 Subject: [PATCH 1/2] extract limited-deserialize crate --- Cargo.lock | 11 +++++++ Cargo.toml | 2 ++ programs/sbf/Cargo.lock | 10 ++++++ sdk/limited-deserialize/Cargo.toml | 26 ++++++++++++++++ sdk/limited-deserialize/src/lib.rs | 48 +++++++++++++++++++++++++++++ sdk/program/Cargo.toml | 1 + sdk/program/src/program_utils.rs | 49 +----------------------------- 7 files changed, 99 insertions(+), 48 deletions(-) create mode 100644 sdk/limited-deserialize/Cargo.toml create mode 100644 sdk/limited-deserialize/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index b257ddd582124e..f7cbf06831bc11 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6901,6 +6901,16 @@ dependencies = [ "trees", ] +[[package]] +name = "solana-limited-deserialize" +version = "2.1.0" +dependencies = [ + "bincode", + "serde", + "solana-instruction", + "solana-program", +] + [[package]] name = "solana-loader-v4-program" version = "2.1.0" @@ -7232,6 +7242,7 @@ dependencies = [ "solana-frozen-abi-macro", "solana-hash", "solana-instruction", + "solana-limited-deserialize", "solana-logger", "solana-msg", "solana-program-error", diff --git a/Cargo.toml b/Cargo.toml index c4bd10559a8c17..880640cef2e10b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -112,6 +112,7 @@ members = [ "sdk/gen-headers", "sdk/hash", "sdk/instruction", + "sdk/limited-deserialize", "sdk/macro", "sdk/msg", "sdk/package-metadata-macro", @@ -409,6 +410,7 @@ solana-inline-spl = { path = "inline-spl", version = "=2.1.0" } solana-instruction = { path = "sdk/instruction", version = "=2.1.0", default-features = false } solana-lattice-hash = { path = "lattice-hash", version = "=2.1.0" } solana-ledger = { path = "ledger", version = "=2.1.0" } +solana-limited-deserialize = { path = "sdk/limited-deserialize", version = "=2.1.0" } solana-loader-v4-program = { path = "programs/loader-v4", version = "=2.1.0" } solana-local-cluster = { path = "local-cluster", version = "=2.1.0" } solana-log-collector = { path = "log-collector", version = "=2.1.0" } diff --git a/programs/sbf/Cargo.lock b/programs/sbf/Cargo.lock index 626f2624711289..2655923d0bf540 100644 --- a/programs/sbf/Cargo.lock +++ b/programs/sbf/Cargo.lock @@ -5463,6 +5463,15 @@ dependencies = [ "trees", ] +[[package]] +name = "solana-limited-deserialize" +version = "2.1.0" +dependencies = [ + "bincode", + "serde", + "solana-instruction", +] + [[package]] name = "solana-loader-v4-program" version = "2.1.0" @@ -5644,6 +5653,7 @@ dependencies = [ "solana-define-syscall", "solana-hash", "solana-instruction", + "solana-limited-deserialize", "solana-msg", "solana-program-error", "solana-program-memory", diff --git a/sdk/limited-deserialize/Cargo.toml b/sdk/limited-deserialize/Cargo.toml new file mode 100644 index 00000000000000..d33663b48293cc --- /dev/null +++ b/sdk/limited-deserialize/Cargo.toml @@ -0,0 +1,26 @@ +[package] +name = "solana-limited-deserialize" +description = "Solana function for deserializing with a limit" +documentation = "https://docs.rs/solana-limited-deserialize" +version = { workspace = true } +authors = { workspace = true } +repository = { workspace = true } +homepage = { workspace = true } +license = { workspace = true } +edition = { workspace = true } + +[dependencies] +bincode = { workspace = true } +serde = { workspace = true } +solana-instruction = { workspace = true, default-features = false, features = [ + "std", +] } + +[dev-dependencies] +solana-program = { path = "../program" } + +[package.metadata.docs.rs] +targets = ["x86_64-unknown-linux-gnu"] + +[lints] +workspace = true diff --git a/sdk/limited-deserialize/src/lib.rs b/sdk/limited-deserialize/src/lib.rs new file mode 100644 index 00000000000000..28bd19dec7bdca --- /dev/null +++ b/sdk/limited-deserialize/src/lib.rs @@ -0,0 +1,48 @@ +//! Contains a single utility function for deserializing from [bincode]. +//! +//! [bincode]: https://docs.rs/bincode + +use {bincode::config::Options, solana_instruction::error::InstructionError}; + +/// Deserialize with a limit based the maximum amount of data a program can expect to get. +/// This function should be used in place of direct deserialization to help prevent OOM errors +pub fn limited_deserialize(instruction_data: &[u8], limit: u64) -> Result +where + T: serde::de::DeserializeOwned, +{ + bincode::options() + .with_limit(limit) + .with_fixint_encoding() // As per https://github.com/servo/bincode/issues/333, these two options are needed + .allow_trailing_bytes() // to retain the behavior of bincode::deserialize with the new `options()` method + .deserialize_from(instruction_data) + .map_err(|_| InstructionError::InvalidInstructionData) +} + +#[cfg(test)] +pub mod tests { + use {super::*, solana_program::system_instruction::SystemInstruction}; + + #[test] + fn test_limited_deserialize_advance_nonce_account() { + let item = SystemInstruction::AdvanceNonceAccount; + let mut serialized = bincode::serialize(&item).unwrap(); + + assert_eq!( + serialized.len(), + 4, + "`SanitizedMessage::get_durable_nonce()` may need a change" + ); + + assert_eq!( + limited_deserialize::(&serialized, 4).as_ref(), + Ok(&item) + ); + assert!(limited_deserialize::(&serialized, 3).is_err()); + + serialized.push(0); + assert_eq!( + limited_deserialize::(&serialized, 4).as_ref(), + Ok(&item) + ); + } +} diff --git a/sdk/program/Cargo.toml b/sdk/program/Cargo.toml index 6b0214ce1edcb3..7762c5a2e9f4e6 100644 --- a/sdk/program/Cargo.toml +++ b/sdk/program/Cargo.toml @@ -47,6 +47,7 @@ solana-instruction = { workspace = true, default-features = false, features = [ "serde", "std", ] } +solana-limited-deserialize = { workspace = true } solana-msg = { workspace = true } solana-program-error = { workspace = true, features = ["serde"] } solana-program-memory = { workspace = true } diff --git a/sdk/program/src/program_utils.rs b/sdk/program/src/program_utils.rs index f624a0f69b13fd..df185e23a238d5 100644 --- a/sdk/program/src/program_utils.rs +++ b/sdk/program/src/program_utils.rs @@ -1,48 +1 @@ -//! Contains a single utility function for deserializing from [bincode]. -//! -//! [bincode]: https://docs.rs/bincode - -use {crate::instruction::InstructionError, bincode::config::Options}; - -/// Deserialize with a limit based the maximum amount of data a program can expect to get. -/// This function should be used in place of direct deserialization to help prevent OOM errors -pub fn limited_deserialize(instruction_data: &[u8], limit: u64) -> Result -where - T: serde::de::DeserializeOwned, -{ - bincode::options() - .with_limit(limit) - .with_fixint_encoding() // As per https://github.com/servo/bincode/issues/333, these two options are needed - .allow_trailing_bytes() // to retain the behavior of bincode::deserialize with the new `options()` method - .deserialize_from(instruction_data) - .map_err(|_| InstructionError::InvalidInstructionData) -} - -#[cfg(test)] -pub mod tests { - use {super::*, solana_program::system_instruction::SystemInstruction}; - - #[test] - fn test_limited_deserialize_advance_nonce_account() { - let item = SystemInstruction::AdvanceNonceAccount; - let mut serialized = bincode::serialize(&item).unwrap(); - - assert_eq!( - serialized.len(), - 4, - "`SanitizedMessage::get_durable_nonce()` may need a change" - ); - - assert_eq!( - limited_deserialize::(&serialized, 4).as_ref(), - Ok(&item) - ); - assert!(limited_deserialize::(&serialized, 3).is_err()); - - serialized.push(0); - assert_eq!( - limited_deserialize::(&serialized, 4).as_ref(), - Ok(&item) - ); - } -} +pub use solana_limited_deserialize::limited_deserialize; From fd464e18d7fb1828da5608ef7a9bc17f2fd47776 Mon Sep 17 00:00:00 2001 From: kevinheavey Date: Tue, 8 Oct 2024 21:47:18 +0400 Subject: [PATCH 2/2] rename solana-limited-deserialize to solana-bincode --- Cargo.lock | 22 +++++++++---------- Cargo.toml | 4 ++-- programs/sbf/Cargo.lock | 20 ++++++++--------- .../Cargo.toml | 6 ++--- .../src/lib.rs | 0 sdk/program/Cargo.toml | 2 +- sdk/program/src/program_utils.rs | 2 +- 7 files changed, 28 insertions(+), 28 deletions(-) rename sdk/{limited-deserialize => bincode}/Cargo.toml (76%) rename sdk/{limited-deserialize => bincode}/src/lib.rs (100%) diff --git a/Cargo.lock b/Cargo.lock index f7cbf06831bc11..d5340e5974a6e7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5917,6 +5917,16 @@ dependencies = [ "thiserror", ] +[[package]] +name = "solana-bincode" +version = "2.1.0" +dependencies = [ + "bincode", + "serde", + "solana-instruction", + "solana-program", +] + [[package]] name = "solana-bloom" version = "2.1.0" @@ -6901,16 +6911,6 @@ dependencies = [ "trees", ] -[[package]] -name = "solana-limited-deserialize" -version = "2.1.0" -dependencies = [ - "bincode", - "serde", - "solana-instruction", - "solana-program", -] - [[package]] name = "solana-loader-v4-program" version = "2.1.0" @@ -7235,6 +7235,7 @@ dependencies = [ "sha3", "solana-account-info", "solana-atomic-u64", + "solana-bincode", "solana-clock", "solana-decode-error", "solana-define-syscall", @@ -7242,7 +7243,6 @@ dependencies = [ "solana-frozen-abi-macro", "solana-hash", "solana-instruction", - "solana-limited-deserialize", "solana-logger", "solana-msg", "solana-program-error", diff --git a/Cargo.toml b/Cargo.toml index 880640cef2e10b..1ccb6112901c40 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -103,6 +103,7 @@ members = [ "sdk", "sdk/account-info", "sdk/atomic-u64", + "sdk/bincode", "sdk/cargo-build-sbf", "sdk/cargo-test-sbf", "sdk/clock", @@ -112,7 +113,6 @@ members = [ "sdk/gen-headers", "sdk/hash", "sdk/instruction", - "sdk/limited-deserialize", "sdk/macro", "sdk/msg", "sdk/package-metadata-macro", @@ -369,6 +369,7 @@ solana-banks-client = { path = "banks-client", version = "=2.1.0" } solana-banks-interface = { path = "banks-interface", version = "=2.1.0" } solana-banks-server = { path = "banks-server", version = "=2.1.0" } solana-bench-tps = { path = "bench-tps", version = "=2.1.0" } +solana-bincode = { path = "sdk/bincode", version = "=2.1.0" } solana-bloom = { path = "bloom", version = "=2.1.0" } solana-bn254 = { path = "curves/bn254", version = "=2.1.0" } solana-bpf-loader-program = { path = "programs/bpf_loader", version = "=2.1.0" } @@ -410,7 +411,6 @@ solana-inline-spl = { path = "inline-spl", version = "=2.1.0" } solana-instruction = { path = "sdk/instruction", version = "=2.1.0", default-features = false } solana-lattice-hash = { path = "lattice-hash", version = "=2.1.0" } solana-ledger = { path = "ledger", version = "=2.1.0" } -solana-limited-deserialize = { path = "sdk/limited-deserialize", version = "=2.1.0" } solana-loader-v4-program = { path = "programs/loader-v4", version = "=2.1.0" } solana-local-cluster = { path = "local-cluster", version = "=2.1.0" } solana-log-collector = { path = "log-collector", version = "=2.1.0" } diff --git a/programs/sbf/Cargo.lock b/programs/sbf/Cargo.lock index 2655923d0bf540..ea866e80c9d1ac 100644 --- a/programs/sbf/Cargo.lock +++ b/programs/sbf/Cargo.lock @@ -4828,6 +4828,15 @@ dependencies = [ "tokio-serde", ] +[[package]] +name = "solana-bincode" +version = "2.1.0" +dependencies = [ + "bincode", + "serde", + "solana-instruction", +] + [[package]] name = "solana-bloom" version = "2.1.0" @@ -5463,15 +5472,6 @@ dependencies = [ "trees", ] -[[package]] -name = "solana-limited-deserialize" -version = "2.1.0" -dependencies = [ - "bincode", - "serde", - "solana-instruction", -] - [[package]] name = "solana-loader-v4-program" version = "2.1.0" @@ -5648,12 +5648,12 @@ dependencies = [ "sha3", "solana-account-info", "solana-atomic-u64", + "solana-bincode", "solana-clock", "solana-decode-error", "solana-define-syscall", "solana-hash", "solana-instruction", - "solana-limited-deserialize", "solana-msg", "solana-program-error", "solana-program-memory", diff --git a/sdk/limited-deserialize/Cargo.toml b/sdk/bincode/Cargo.toml similarity index 76% rename from sdk/limited-deserialize/Cargo.toml rename to sdk/bincode/Cargo.toml index d33663b48293cc..576ee0bf46a8b8 100644 --- a/sdk/limited-deserialize/Cargo.toml +++ b/sdk/bincode/Cargo.toml @@ -1,7 +1,7 @@ [package] -name = "solana-limited-deserialize" -description = "Solana function for deserializing with a limit" -documentation = "https://docs.rs/solana-limited-deserialize" +name = "solana-bincode" +description = "Solana bincode utilities" +documentation = "https://docs.rs/solana-bincode" version = { workspace = true } authors = { workspace = true } repository = { workspace = true } diff --git a/sdk/limited-deserialize/src/lib.rs b/sdk/bincode/src/lib.rs similarity index 100% rename from sdk/limited-deserialize/src/lib.rs rename to sdk/bincode/src/lib.rs diff --git a/sdk/program/Cargo.toml b/sdk/program/Cargo.toml index 7762c5a2e9f4e6..5ed6aa72f9c6ed 100644 --- a/sdk/program/Cargo.toml +++ b/sdk/program/Cargo.toml @@ -33,6 +33,7 @@ sha2 = { workspace = true } sha3 = { workspace = true } solana-account-info = { workspace = true, features = ["bincode"] } solana-atomic-u64 = { workspace = true } +solana-bincode = { workspace = true } solana-clock = { workspace = true, features = ["serde"] } solana-decode-error = { workspace = true } solana-frozen-abi = { workspace = true, optional = true, features = ["frozen-abi"] } @@ -47,7 +48,6 @@ solana-instruction = { workspace = true, default-features = false, features = [ "serde", "std", ] } -solana-limited-deserialize = { workspace = true } solana-msg = { workspace = true } solana-program-error = { workspace = true, features = ["serde"] } solana-program-memory = { workspace = true } diff --git a/sdk/program/src/program_utils.rs b/sdk/program/src/program_utils.rs index df185e23a238d5..823cd9881a377d 100644 --- a/sdk/program/src/program_utils.rs +++ b/sdk/program/src/program_utils.rs @@ -1 +1 @@ -pub use solana_limited_deserialize::limited_deserialize; +pub use solana_bincode::limited_deserialize;