Skip to content

Commit

Permalink
Problem: re-assembling wasm code to get label offsets
Browse files Browse the repository at this point in the history
Solution: use new version's `assemble_options` to retrieve it

See icedland/iced#271 (comment)
  • Loading branch information
yrashk committed Feb 25, 2022
1 parent bc184e3 commit ed343c2
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 22 additions & 6 deletions parawasm/src/x86_64/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use core::ops::{Deref, DerefMut};
use iced_x86::code_asm::{
dword_ptr, qword_ptr, r8, r9, rax, rbp, rcx, rdi, rdx, rsi, rsp, AsmRegister64, CodeAssembler,
};
use iced_x86::IcedError;
use iced_x86::{BlockEncoderOptions, IcedError};
use wasmparser_nostd::*;

mod instructions;
Expand Down Expand Up @@ -207,7 +207,16 @@ impl Compiler for X86_64Compiler {
Payload::ImportSection(is) => {
for i in is {
let import = i?;
let offset = assembler.assemble(0)?.len();
let mut current_label = assembler.create_label();
assembler.set_label(&mut current_label)?;
assembler.zero_bytes()?;
let offset = assembler
.assemble_options(
0,
BlockEncoderOptions::RETURN_NEW_INSTRUCTION_OFFSETS,
)?
.label_ip(&current_label)?
as usize;
let reference = (
import.module.to_owned(),
import.field.map(str::to_owned),
Expand Down Expand Up @@ -365,15 +374,22 @@ impl Compiler for X86_64Compiler {
// Bind labels
for (idx, instruction) in assembler.take_instructions().into_iter().enumerate() {
if let Some((_, label)) = label_indices.iter_mut().find(|(i, _)| *i == idx) {
assembler.set_label(label)?;
assembler.zero_bytes()?;
// If this is a label pointing to a function, record function body entry point
if let Some((_label, index)) =
function_bodies.iter().find(|(label_, _)| label_ == label)
{
module
.function_bodies
.insert(*index, assembler.assemble(0)?.len());
module.function_bodies.insert(
*index,
assembler
.assemble_options(
0,
BlockEncoderOptions::RETURN_NEW_INSTRUCTION_OFFSETS,
)?
.label_ip(label)? as usize,
);
}
assembler.set_label(label)?;
}
assembler.add_instruction(instruction)?;
}
Expand Down
2 changes: 1 addition & 1 deletion parawasm_tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ wat = "1.0.41"
byteorder = { version = "1.4.3", default-features = false }

[dependencies.iced-x86]
version = "1.16.0"
version = "1.17.0"
default-features = false
features = ["no_std", "encoder", "decoder", "intel", "code_asm"]

0 comments on commit ed343c2

Please sign in to comment.