Skip to content
This repository was archived by the owner on Jan 22, 2025. It is now read-only.

Limited Deserialize isn't limiting anything#10952

Merged
CriesofCarrots merged 2 commits intosolana-labs:masterfrom
CriesofCarrots:bincode-limit
Jul 8, 2020
Merged

Limited Deserialize isn't limiting anything#10952
CriesofCarrots merged 2 commits intosolana-labs:masterfrom
CriesofCarrots:bincode-limit

Conversation

@CriesofCarrots
Copy link
Copy Markdown
Contributor

@CriesofCarrots CriesofCarrots commented Jul 8, 2020

Problem

We use this pattern several places.

bincode::config()
        .limit(u64)
        .deserialize()

However, it doesn't actually limit anything. See bincode issue: bincode-org/bincode#299
Also see failing test below; the 2nd assert fails because limited_deserialize does not return an error.

Summary of Changes

Use deserialize_from instead

Shred uses this same pattern, but using deserialize_from would require switching the trait bound to DeserializeOwned

T: Deserialize<'de>,

Doing this seemed to build fine, but I am not sure what deeper implications the change might have.

@CriesofCarrots CriesofCarrots requested a review from jackcmay July 8, 2020 01:56
@codecov
Copy link
Copy Markdown

codecov Bot commented Jul 8, 2020

Codecov Report

Merging #10952 into master will decrease coverage by 0.0%.
The diff coverage is 90.9%.

@@            Coverage Diff            @@
##           master   #10952     +/-   ##
=========================================
- Coverage    82.1%    82.1%   -0.1%     
=========================================
  Files         317      318      +1     
  Lines       72709    72724     +15     
=========================================
- Hits        59752    59750      -2     
- Misses      12957    12974     +17     

@CriesofCarrots CriesofCarrots marked this pull request as ready for review July 8, 2020 01:56
Comment thread core/src/rpc.rs
Comment on lines 1694 to +1696
bincode::config()
.limit(PACKET_DATA_SIZE as u64)
.deserialize(&wire_transaction)
.deserialize_from(&wire_transaction[..])
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm tempted to make this a plain-old bincode::deserialize, since we check the wire_transaction length and return a better error message immediately before this.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there isn't a performance impact I would say keep this consistent by using deserialize_from. Keeping it is also a good measure in case the other checks change or are removed for some reason.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good. That's where I landed also

@jackcmay
Copy link
Copy Markdown
Contributor

jackcmay commented Jul 8, 2020

Good find @CriesofCarrots the changes in this PR look good to me. I wonder if there is any performance impact of this change.

@CriesofCarrots
Copy link
Copy Markdown
Contributor Author

CriesofCarrots commented Jul 8, 2020

Good find @CriesofCarrots the changes in this PR look good to me. I wonder if there is any performance impact of this change.

@jackcmay , I benchmarked the bincode methods, and deserialize_from actually performs slightly better, with greater improvements the larger the slice. (3.5us vs 8us for a slice > PACKET_DATA_SIZE)

I took a glance through the CI benches, and nothing stood out as significantly different. Are there any specific benches I should pay attention to?

@jackcmay
Copy link
Copy Markdown
Contributor

jackcmay commented Jul 8, 2020

Not really, lgtm!

@CriesofCarrots CriesofCarrots merged commit 1a6bbd2 into solana-labs:master Jul 8, 2020
@CriesofCarrots CriesofCarrots deleted the bincode-limit branch July 24, 2020 22:44
CriesofCarrots added a commit that referenced this pull request Aug 28, 2020
* Add failing test

* Use deserialize_from to enable limit
mergify Bot added a commit that referenced this pull request Aug 29, 2020
* Update spl-token to v2.0 (#11884)

    * Update account-decoder to spl-token v2.0

    * Update transaction-status to spl-token v2.0

    * Update rpc to spl-token v2.0

    * Update getTokenSupply to pull from Mint directly

    * Fixup to spl-token v2.0.1

    (cherry picked from commit 76be36c)

    # Conflicts:
    #       Cargo.lock
    #       account-decoder/Cargo.toml
    #       core/Cargo.toml
    #       core/src/rpc.rs
    #       transaction-status/Cargo.toml

* Fix non-Cargo.lock conflicts

* Limited Deserialize isn't limiting anything (#10952)

* Add failing test

* Use deserialize_from to enable limit

* Cargo.lock

* chore(deps): bump bincode from 1.2.1 to 1.3.1 (#10867)

* chore(deps): bump bincode from 1.2.1 to 1.3.1

Bumps [bincode](https://github.com/servo/bincode) from 1.2.1 to 1.3.1.
- [Release notes](https://github.com/servo/bincode/releases)
- [Commits](https://github.com/servo/bincode/commits)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>

* [auto-commit] Update all Cargo lock files

* Switch from deprecated method

* Add options to maintain behavior with bincode::options()

Co-authored-by: dependabot-preview[bot] <27856297+dependabot-preview[bot]@users.noreply.github.com>
Co-authored-by: dependabot-buildkite <dependabot-buildkite@noreply.solana.com>
Co-authored-by: Tyera Eulberg <tyera@solana.com>

Co-authored-by: Tyera Eulberg <tyera@solana.com>
Co-authored-by: Tyera Eulberg <teulberg@gmail.com>
Co-authored-by: dependabot-preview[bot] <27856297+dependabot-preview[bot]@users.noreply.github.com>
Co-authored-by: dependabot-buildkite <dependabot-buildkite@noreply.solana.com>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants