Skip to content

Commit

Permalink
Update all crates for recent fuel-asm refactor. fuel-vm -> 0.26
Browse files Browse the repository at this point in the history
…, `fuel-core` -> `0.17`. (#4004)

NOTE: This PR is against @Voxelot's PR branch here:
#3961.

This updates all crates for the recent upstream `fuel-asm` refactor.

To learn more about the refactor, see the original PR at the fuel-vm
repo: FuelLabs/fuel-vm#283.

The fuel-core update PR can be found here:
FuelLabs/fuel-core#973.

## TODO

- [x] Update `fuels` for `fuel-asm` change. `fuels-core` errors in the
`offsets` module. Will address this now and then update this PR.

## Follow-up

- [Rename `AllocatedOpcode` to `AllocatedInstruction` to follow
fuel-spec/fuel-asm
terminology.](#4006)

## Checklist

- [x] I have linked to any relevant issues.
- [x] I have commented my code, particularly in hard-to-understand
areas.
- [x] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [x] I have added tests that prove my fix is effective or that my
feature works.
- [x] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [x] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [x] I have requested a review from the relevant team or maintainers.

---------

Co-authored-by: bingcicle <bingcicle@proton.me>
Co-authored-by: Brandon Kite <brandonkite92@gmail.com>
  • Loading branch information
3 people committed Feb 8, 2023
1 parent 0740175 commit 8f4d6ca
Show file tree
Hide file tree
Showing 9 changed files with 247 additions and 261 deletions.
20 changes: 6 additions & 14 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,9 @@ jobs:
uses: peaceiris/actions-mdbook@v1
with:
mdbook-version: "0.4.15"
- name: Emit book logs to tmp.txt, fail if build logs contain 'ERROR'
- name: Build the Sway Book
run: MDBOOK_preprocessor__FORC_documenter__STRICT="true" mdbook build docs/book
- name: Emit Sway Book logs to tmp.txt, fail if build logs contain 'ERROR'
run: |
MDBOOK_preprocessor__FORC_documenter__STRICT="true" mdbook build docs/book &> tmp.txt
if cat tmp.txt | grep 'ERROR'
Expand All @@ -141,18 +143,8 @@ jobs:
else
rm tmp.txt && exit 0
fi
- name: Emit reference logs to tmp.txt, fail if build logs contain 'ERROR'
run: |
mdbook build docs/reference
mdbook build docs/reference &> tmp.txt
if cat tmp.txt | grep 'ERROR'
then
cat tmp.txt
rm tmp.txt && exit 1
else
rm tmp.txt && exit 0
fi
- name: Build the Sway Reference
run: mdbook build docs/reference

build-forc-test-project:
runs-on: ubuntu-latest
Expand Down Expand Up @@ -255,7 +247,7 @@ jobs:
runs-on: ubuntu-latest
services:
fuel-core:
image: ghcr.io/fuellabs/fuel-core:v0.16.1
image: ghcr.io/fuellabs/fuel-core:v0.17.0
ports:
- 4000:4000
steps:
Expand Down
83 changes: 53 additions & 30 deletions Cargo.lock

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

17 changes: 8 additions & 9 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,11 @@ exclude = [
debug = 2

[workspace.dependencies]
fuel-asm = "0.25"
fuel-crypto = "0.25"
fuel-tx = "0.25"
fuel-core-client = "0.16"
fuel-vm = "0.25"
fuels-core = { git = "https://github.com/FuelLabs/fuels-rs.git", branch = "Voxelot/fuel-core-0.16" }
fuels-signers = { git = "https://github.com/FuelLabs/fuels-rs.git", branch = "Voxelot/fuel-core-0.16" }
fuels-types = { git = "https://github.com/FuelLabs/fuels-rs.git", branch = "Voxelot/fuel-core-0.16" }
fuels = { git = "https://github.com/FuelLabs/fuels-rs.git", branch = "Voxelot/fuel-core-0.16" }
fuel-asm = "0.26"
fuel-crypto = "0.26"
fuel-tx = "0.26"
fuel-core-client = "0.17"
fuel-vm = "0.26"
fuels-core = "0.36.0"
fuels-signers = "0.36.0"
fuels-types = "0.36.0"
1 change: 1 addition & 0 deletions docs/book/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
- [forc client](./forc/plugins/forc_client/index.md)
- [forc deploy](./forc/plugins/forc_client/forc_deploy.md)
- [forc run](./forc/plugins/forc_client/forc_run.md)
- [forc doc](./forc/plugins/forc_doc.md)
- [forc explore](./forc/plugins/forc_explore.md)
- [forc fmt](./forc/plugins/forc_fmt.md)
- [forc lsp](./forc/plugins/forc_lsp.md)
6 changes: 3 additions & 3 deletions forc-test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use forc_pkg as pkg;
use fuel_tx as tx;
use fuel_vm::checked_transaction::builder::TransactionBuilderExt;
use fuel_vm::gas::GasCosts;
use fuel_vm::{self as vm, prelude::Opcode};
use fuel_vm::{self as vm, fuel_asm, prelude::Instruction};
use pkg::TestPassCondition;
use pkg::{Built, BuiltPackage, CONTRACT_ID_CONSTANT_NAME};
use rand::{distributions::Standard, prelude::Distribution, Rng, SeedableRng};
Expand Down Expand Up @@ -501,15 +501,15 @@ fn run_tests(built: BuiltTests) -> anyhow::Result<Tested> {
fn patch_test_bytecode(bytecode: &[u8], test_offset: u32) -> std::borrow::Cow<[u8]> {
// TODO: Standardize this or add metadata to bytecode.
const PROGRAM_START_INST_OFFSET: u32 = 6;
const PROGRAM_START_BYTE_OFFSET: usize = PROGRAM_START_INST_OFFSET as usize * Opcode::LEN;
const PROGRAM_START_BYTE_OFFSET: usize = PROGRAM_START_INST_OFFSET as usize * Instruction::SIZE;

// If our desired entry point is the program start, no need to jump.
if test_offset == PROGRAM_START_INST_OFFSET {
return std::borrow::Cow::Borrowed(bytecode);
}

// Create the jump instruction and splice it into the bytecode.
let ji = Opcode::JI(test_offset);
let ji = fuel_asm::op::ji(test_offset);
let ji_bytes = ji.to_bytes();
let start = PROGRAM_START_BYTE_OFFSET;
let end = start + ji_bytes.len();
Expand Down
45 changes: 21 additions & 24 deletions forc/src/cli/commands/parse_bytecode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,10 @@ pub(crate) fn exec(command: Command) -> Result<()> {
.map_err(|_| anyhow!("{}: file not found", command.file_path))?;
let mut buffer = vec![0; metadata.len() as usize];
f.read_exact(&mut buffer).expect("buffer overflow");
let mut instructions = vec![];

for i in (0..buffer.len()).step_by(4) {
let raw = &buffer[i..i + 4];
unsafe {
let op = fuel_asm::Opcode::from_bytes_unchecked(raw);
instructions.push((raw, op));
};
}
let instructions = fuel_asm::from_bytes(buffer.iter().cloned())
.zip(buffer.chunks(fuel_asm::Instruction::SIZE));

let mut table = term_table::Table::new();
table.separate_rows = false;
table.add_row(Row::new(vec![
Expand All @@ -38,34 +33,36 @@ pub(crate) fn exec(command: Command) -> Result<()> {
TableCell::new("notes"),
]));
table.style = term_table::TableStyle::empty();
for (word_ix, instruction) in instructions.iter().enumerate() {
use fuel_asm::Opcode::*;
let notes = match instruction.1 {
JI(num) => format!("jump to byte {}", num * 4),
JNEI(_, _, num) => format!("conditionally jump to byte {}", num * 4),
JNZI(_, num) => format!("conditionally jump to byte {}", num * 4),
Undefined if word_ix == 2 || word_ix == 3 => {
let parsed_raw = u32::from_be_bytes([
instruction.0[0],
instruction.0[1],
instruction.0[2],
instruction.0[3],
]);
for (word_ix, (result, raw)) in instructions.enumerate() {
use fuel_asm::Instruction;
let notes = match result {
Ok(Instruction::JI(ji)) => format!("jump to byte {}", u32::from(ji.imm24()) * 4),
Ok(Instruction::JNEI(jnei)) => {
format!("conditionally jump to byte {}", u32::from(jnei.imm12()) * 4)
}
Ok(Instruction::JNZI(jnzi)) => {
format!("conditionally jump to byte {}", u32::from(jnzi.imm18()) * 4)
}
Err(fuel_asm::InvalidOpcode) if word_ix == 2 || word_ix == 3 => {
let parsed_raw = u32::from_be_bytes([raw[0], raw[1], raw[2], raw[3]]);
format!(
"data section offset {} ({})",
if word_ix == 2 { "lo" } else { "hi" },
parsed_raw
)
}
_ => "".into(),
Ok(_) | Err(fuel_asm::InvalidOpcode) => "".into(),
};
table.add_row(Row::new(vec![
TableCell::new_with_alignment(word_ix, 1, Alignment::Right),
TableCell::new(word_ix * 4),
TableCell::new(format!("{:?}", instruction.1)),
TableCell::new(match result {
Ok(inst) => format!("{inst:?}"),
Err(err) => format!("{err:?}"),
}),
TableCell::new(format!(
"{:02x} {:02x} {:02x} {:02x}",
instruction.0[0], instruction.0[1], instruction.0[2], instruction.0[3],
raw[0], raw[1], raw[2], raw[3],
)),
TableCell::new(notes),
]));
Expand Down
7 changes: 3 additions & 4 deletions sway-core/src/asm_generation/finalized_asm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use sway_error::error::CompileError;
use sway_types::span::Span;

use either::Either;
use std::{collections::BTreeMap, fmt, io::Read};
use std::{collections::BTreeMap, fmt};

/// Represents an ASM set which has had register allocation, jump elimination, and optimization
/// applied to it
Expand Down Expand Up @@ -143,14 +143,13 @@ fn to_bytecode_mut(
if ops.len() > 1 {
buf.resize(buf.len() + ((ops.len() - 1) * 4), 0);
}
for mut op in ops {
for op in ops {
if let Some(span) = &span {
source_map.insert(half_word_ix, span);
}
let read_range_upper_bound =
core::cmp::min(half_word_ix * 4 + std::mem::size_of_val(&op), buf.len());
op.read_exact(&mut buf[half_word_ix * 4..read_range_upper_bound])
.expect("Failed to write to in-memory buffer.");
buf[half_word_ix * 4..read_range_upper_bound].copy_from_slice(&op.to_bytes());
half_word_ix += 1;
}
}
Expand Down
Loading

0 comments on commit 8f4d6ca

Please sign in to comment.