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 ldc opcode in internal contexts #736

Merged
merged 12 commits into from
May 29, 2024
Merged

Conversation

Dentosal
Copy link
Member

@Dentosal Dentosal commented May 19, 2024

LDC opcode was behaving incorrectly in internal contexts, i.e. when called from within a contract.

FuelLabs/fuel-specs#584

Checklist

  • Breaking changes are clearly marked as such in the PR description and changelog
  • New behavior is reflected in tests

Before requesting review

  • I have reviewed the code myself

@Dentosal Dentosal added bug Something isn't working fuel-vm Related to the `fuel-vm` crate. labels May 19, 2024
@Dentosal Dentosal self-assigned this May 19, 2024
@Dentosal Dentosal marked this pull request as ready for review May 19, 2024 13:34
@Dentosal Dentosal requested a review from a team May 19, 2024 13:34
CHANGELOG.md Outdated Show resolved Hide resolved
xgreenx
xgreenx previously approved these changes May 27, 2024
Comment on lines +644 to +646
Word::from_be_bytes(self.memory.read_bytes(code_size_ptr)?);
let old_code_size = padded_len_word(old_code_size)
.expect("Code size cannot overflow with padding");
Copy link
Member Author

Choose a reason for hiding this comment

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

I would assume the size should already be padded when written in memory. Is it really correct to just compute padding here?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Our code uses unpadded value in the CallFrame

image

Copy link
Member Author

Choose a reason for hiding this comment

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

Yep. Shouldn't CallFrame use the padded value?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't know=) The question is to the compiler team: do they use it somehow or not? Or is it used by anyone=)

Our specification says "Code size in bytes.", that sounds like exact size of the contract without padding.

image

Copy link
Member Author

Choose a reason for hiding this comment

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

I'll ask the team, but a quick search from Sway codebase seems like they don't use this for anything. Time to remove? :D

Copy link
Member

Choose a reason for hiding this comment

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

Doesnt "word-aligned" mean the same thing as padding here?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yep.

Moved to use padded size in 7335f9f.

xgreenx
xgreenx previously approved these changes May 27, 2024
Comment on lines 600 to 601
let frame_end =
(*self.registers.system_registers.fp) + CallFrame::serialized_size() as Word;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
let frame_end =
(*self.registers.system_registers.fp) + CallFrame::serialized_size() as Word;
let frame_end =
(*self.registers.system_registers.fp) + total_size_in_stack as Word;

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 name was wrong but the math correct. I've fixed this in 27ef70c

@xgreenx
Copy link
Collaborator

xgreenx commented May 28, 2024

Could ou also update the Specification to specify this behaviour explicitly, please?=)

@Dentosal
Copy link
Member Author

Could ou also update the Specification to specify this behaviour explicitly, please?=)

FuelLabs/fuel-specs#584

@Dentosal Dentosal requested a review from xgreenx May 28, 2024 15:22
let code_start =
(*self.registers.system_registers.fp) + CallFrame::serialized_size() as Word;

*self.registers.system_registers.pc = code_start;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Before it was frame_end
image

Where frame_end is fp + total_size_in_stack

image

Copy link
Member Author

Choose a reason for hiding this comment

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

No? Essentially it was before like this:

content_size = len - frame_len_with_padding
frame_end = fp + content_size # return value

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ohh, there is saturating_sub, I missed that=D

@@ -2047,3 +2047,180 @@ fn coinbase_works() {

assert_eq!(data.as_ref().unwrap(), &*expected);
}

#[test]
fn various_ldc_issues_poc() {
Copy link
Member

Choose a reason for hiding this comment

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

Is there any way to break this down in to more granular && specific tests? Tests like this are hard to maintain as it's not clear what behavior they are defining.

Dentosal added a commit to FuelLabs/fuel-specs that referenced this pull request May 28, 2024
Introduced here: FuelLabs/fuel-vm#736

### Before requesting review
- [x] I have reviewed the code myself
@xgreenx xgreenx added this pull request to the merge queue May 29, 2024
Merged via the queue into master with commit f90f29b May 29, 2024
38 checks passed
@xgreenx xgreenx deleted the dento/fix-ldc-frame-size-update branch May 29, 2024 11:48
@xgreenx xgreenx mentioned this pull request May 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fuel-vm Related to the `fuel-vm` crate.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants