Skip to content

Fix instruction length calculation. - #311

Open
igankevich wants to merge 7 commits into
masterfrom
ivan/fix-instruction-length-calculation
Open

igankevich wants to merge 7 commits into
masterfrom
ivan/fix-instruction-length-calculation

Conversation

@igankevich

@igankevich igankevich commented Jun 26, 2025

Copy link
Copy Markdown

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.

@igankevich
igankevich requested a review from koute June 26, 2025 12:36
Comment thread crates/polkavm/src/compiler.rs Outdated
Comment on lines +467 to +472
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;

Copy link
Copy Markdown
Collaborator

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.

Copy link
Copy Markdown
Author

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.

@koute

koute commented Jun 30, 2025

Copy link
Copy Markdown
Collaborator

cc @aman4150 The fuzzer should have caught this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants