Skip to content
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

Fix a crash in CCP and zero-filling issues in CCP and LDC #524

Merged
merged 18 commits into from
Aug 11, 2023

Conversation

Dentosal
Copy link
Member

@Dentosal Dentosal commented Jul 25, 2023

Fixes #523. Partial overlap with #511, but the order doesn't matter much.

Addresses the following issues:

  • CCP instruction could crash when given out-of-contract-bounds input.
  • CCP instruction would only zero memory when asked to copy more bytes than the contract has. This is a bug.
  • LDC instruction would revert when asked more to load more bytes than the contract has. The spec requires zero-filling here.

As a side not, the logic around $fp->code_size seems quite complicated and it looks like it's not used for anything. Maybe we could remove it?

@Dentosal Dentosal added bug Something isn't working breaking A breaking api change tech-debt fuel-vm Related to the `fuel-vm` crate. labels Jul 25, 2023
@Dentosal Dentosal requested a review from a team July 25, 2023 13:39
@Dentosal Dentosal self-assigned this Jul 25, 2023
@MitchTurner
Copy link
Member

Could you give me the expected behavior for each of the bugs this PR is addressing? For example "out-of-contract-bounds input" sounds like it should result in unhappy behavior, if not "crash" what should it do?

@Dentosal
Copy link
Member Author

Dentosal commented Aug 2, 2023

Could you give me the expected behavior for each of the bugs this PR is addressing? For example "out-of-contract-bounds input" sounds like it should result in unhappy behavior, if not "crash" what should it do?

Sure: The expected behavior is to follow the spec for each of these. Specifically:

CCP instruction could crash when given out-of-contract-bounds input.

If $rD is greater than the code size, zero bytes are filled in.

That's a bit inexact, as here $rC..$rC+$rD is the range in contract, but I think that the correct interpretation here is that all bytes read from outside contract are treated as zeroes when copying them over.

CCP instruction would only zero memory when asked to copy more bytes than the contract has.

Copy $rD bytes of code starting at $rC for contract with ID equal to the 32 bytes in memory starting at $rB into memory starting at $rA.

The expected behavior is that $rC..$rC+$rD range, or whatever of it fits inside the contract bounds, is copied into memory starting from $rA. The issue was that the memory was only zeroed, and the contract contents were not copied at all.

LDC instruction would revert when asked more to load more bytes than the contract has.

If $rC is greater than the code size, zero bytes are filled in.

Spec requires zero-extending the part outside the contract bounds, but we were reverting instead of doing that.

|| contract_id_end as Word > VM_MAX_RAM
|| length > MEM_MAX_ACCESS_SIZE as usize
|| length > self.contract_max_size as usize
if memory_offset.saturating_add(length) > *self.hp
Copy link
Member

@Voxelot Voxelot Aug 5, 2023

Choose a reason for hiding this comment

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

Why did this change from >= hp to > hp? Since this variable is only used in a method that says noownershipchecks, wouldn't that mean this could allow a stack overflow?

Copy link
Member Author

Choose a reason for hiding this comment

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

This was fixed and cleaned up in #511. I merged master to this branch just now.

@@ -140,6 +140,13 @@ impl MemoryRange {
Self(start..end)
}

/// Splits range at given relative offset. Panics if offset > range length.
pub fn split_at_offset(self, at: usize) -> (Self, Self) {
Copy link
Member

@Voxelot Voxelot Aug 5, 2023

Choose a reason for hiding this comment

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

Maybe we should return an optional return type here instead of using a panicking assert? Since this is a public api it may be hard to guarantee that it's used correctly in the future.

Copy link
Collaborator

Choose a reason for hiding this comment

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

It seems you use this function only in one place, so maybe it is better to move this code there instead.

Copy link
Member Author

Choose a reason for hiding this comment

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

The API here mirrors slice::split_at from core, including the panic behavior. The code is written here instead of the call site since it's a useful abstraction, and IMO makes the code more readable.

fuel-vm/src/interpreter/blockchain.rs Outdated Show resolved Hide resolved
@@ -140,6 +140,13 @@ impl MemoryRange {
Self(start..end)
}

/// Splits range at given relative offset. Panics if offset > range length.
pub fn split_at_offset(self, at: usize) -> (Self, Self) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

It seems you use this function only in one place, so maybe it is better to move this code there instead.

fuel-vm/src/interpreter/memory.rs Outdated Show resolved Hide resolved
Copy link
Collaborator

@xgreenx xgreenx left a comment

Choose a reason for hiding this comment

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

Looks good to me=) Thank you for fixing that! @Voxelot Could you have a look please one more time?

dst_addr: Word,
contract_id_addr: Word,
contract_offset: Word,
length: Word,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do we need to worry about padding here? Or is it the responsibility of the CCP user?

Copy link
Member

Choose a reason for hiding this comment

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

hmm it's not in the specs. I guess since this isn't meant to be executable code it's not so relevant here.

@Dentosal Dentosal added this pull request to the merge queue Aug 11, 2023
Merged via the queue into master with commit 731a187 Aug 11, 2023
33 checks passed
@Dentosal Dentosal deleted the dento/ccp-crash-fix branch August 11, 2023 18:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking A breaking api change bug Something isn't working fuel-vm Related to the `fuel-vm` crate. tech-debt
Projects
None yet
Development

Successfully merging this pull request may close these issues.

TOB-FUEL-18: Crash when executing CCP (Copy Code) instruction
4 participants