-
Notifications
You must be signed in to change notification settings - Fork 73
Fix instruction length calculation. #311
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
base: master
Are you sure you want to change the base?
Conversation
crates/polkavm/src/compiler.rs
Outdated
let offset = self.program_counter_to_machine_code_offset_list.last().unwrap().1 as usize; | ||
let instruction_length = self.asm.len() - offset; | ||
let instruction_length = self.asm.len() - self.asm_len_before; |
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.
Doing this isn't necessarily correct.
In general the point of this check is as follows: we want to estimate how many bytes of native assembly each byte of PVM bytecode can emit at maximum, and then allocate the appropriate amount of address space so that it will all fit even in the worst case. So we need to include the size of the gas metering stub in these calculations somehow. If we apply this change then the size of the gas metering stub will effectively be ignored.
However, I think it should be fine if we move this whole if
to the end of this function so that it will include the size of the gas metering stub.
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.
Thanks. I reverted my changes and instead increased max. instruction length by 10 (the size of the stub). I hope this is correct. The former if
already included gas metering stub length.
cc @aman4150 The fuzzer should have caught this. |
Previously the length of the gas metering stub was incorrectly added to the length of the instruction that comes after. For long instructions such as 66-byte
div
this resulted in exceeding max. instruction length and assertion failure. This PR fixes the issue.