From 1b99c89492cf1a587e492a75a92dadf09228521a Mon Sep 17 00:00:00 2001 From: Vaivaswatha Nagaraj Date: Wed, 11 Sep 2024 11:47:48 +0530 Subject: [PATCH] update patch_test_bytecode for forc test --- forc-test/src/execute.rs | 24 ++++++++++++------- .../asm_generation/fuel/programs/abstract.rs | 2 +- sway-core/src/asm_lang/mod.rs | 1 + sway-core/src/asm_lang/virtual_ops.rs | 8 +++++++ 4 files changed, 26 insertions(+), 9 deletions(-) diff --git a/forc-test/src/execute.rs b/forc-test/src/execute.rs index 3436261af94..78aa5fd2d7c 100644 --- a/forc-test/src/execute.rs +++ b/forc-test/src/execute.rs @@ -246,17 +246,25 @@ impl TestExecutor { /// The following is how the beginning of the bytecode is laid out: /// /// ```ignore -/// [0] ji i4 ; Jumps to the data section setup. -/// [1] noop -/// [2] DATA_SECTION_OFFSET[0..32] -/// [3] DATA_SECTION_OFFSET[32..64] -/// [4] lw $ds $is 1 ; The data section setup, i.e. where the first ji lands. -/// [5] add $$ds $$ds $is -/// [6] ; This is where we want to jump from to our test code! +/// [ 0] ji i4 ; Jumps to the data section setup. +/// [ 1] noop +/// [ 2] DATA_SECTION_OFFSET[0..32] +/// [ 3] DATA_SECTION_OFFSET[32..64] +/// [ 4] METADATA (0-32) +/// [ 5] METADATA (32-64) +/// [ 6] METADATA (64-96) +/// [ 7] METADATA (96-128) +/// [ 8] METADATA (128-160) +/// [ 9] METADATA (160-192) +/// [10] METADATA (192-224) +/// [11] METADATA (224-256) +/// [12] lw $ds $is 1 ; The data section setup, i.e. where the first ji lands. +/// [13] add $$ds $$ds $is +/// [14] ; This is where we want to jump from to our test code! /// ``` 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_INST_OFFSET: u32 = 14; 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. diff --git a/sway-core/src/asm_generation/fuel/programs/abstract.rs b/sway-core/src/asm_generation/fuel/programs/abstract.rs index 2d88afea684..4f8e2e93fe3 100644 --- a/sway-core/src/asm_generation/fuel/programs/abstract.rs +++ b/sway-core/src/asm_generation/fuel/programs/abstract.rs @@ -160,7 +160,7 @@ impl AbstractProgram { /// /// WORD OP /// 1 MOV $scratch $pc - /// - JMPF $zero i4 + /// - JMPF $zero i10 /// 2 DATA_START (0-32) (in bytes, offset from $is) /// - DATA_START (32-64) /// 3 METADATA (0-32) diff --git a/sway-core/src/asm_lang/mod.rs b/sway-core/src/asm_lang/mod.rs index 310107877b1..7c69aa423cc 100644 --- a/sway-core/src/asm_lang/mod.rs +++ b/sway-core/src/asm_lang/mod.rs @@ -1248,6 +1248,7 @@ impl fmt::Display for VirtualOp { /* Non-VM Instructions */ BLOB(a) => write!(fmtr, "blob {a}"), DataSectionOffsetPlaceholder => write!(fmtr, "data section offset placeholder"), + Metadata => write!(fmtr, "Metadata"), LoadDataId(a, b) => write!(fmtr, "load {a} {b}"), AddrDataId(a, b) => write!(fmtr, "addr {a} {b}"), Undefined => write!(fmtr, "undefined op"), diff --git a/sway-core/src/asm_lang/virtual_ops.rs b/sway-core/src/asm_lang/virtual_ops.rs index ffe3509b7cd..17d3e281c0c 100644 --- a/sway-core/src/asm_lang/virtual_ops.rs +++ b/sway-core/src/asm_lang/virtual_ops.rs @@ -226,6 +226,7 @@ pub(crate) enum VirtualOp { /* Non-VM Instructions */ BLOB(VirtualImmediate24), + Metadata, DataSectionOffsetPlaceholder, // LoadDataId takes a virtual register and a DataId, which points to a labeled piece // of data in the data section. Note that the ASM op corresponding to a LW is @@ -347,6 +348,7 @@ impl VirtualOp { /* Non-VM Instructions */ BLOB(_imm) => vec![], DataSectionOffsetPlaceholder => vec![], + Metadata => vec![], LoadDataId(r1, _i) => vec![r1], AddrDataId(r1, _) => vec![r1], @@ -465,6 +467,7 @@ impl VirtualOp { // Virtual OPs | BLOB(_) | DataSectionOffsetPlaceholder + | Metadata | Undefined => true } } @@ -571,6 +574,7 @@ impl VirtualOp { | GTF(_, _, _) | BLOB(_) | DataSectionOffsetPlaceholder + | Metadata | LoadDataId(_, _) | AddrDataId(_, _) | Undefined => vec![], @@ -692,6 +696,7 @@ impl VirtualOp { /* Non-VM Instructions */ BLOB(_imm) => vec![], DataSectionOffsetPlaceholder => vec![], + Metadata => vec![], LoadDataId(_r1, _i) => vec![], AddrDataId(_r1, _i) => vec![], @@ -815,6 +820,7 @@ impl VirtualOp { LoadDataId(r1, _i) => vec![r1], AddrDataId(r1, _i) => vec![r1], DataSectionOffsetPlaceholder => vec![], + Metadata => vec![], Undefined => vec![], }) .into_iter() @@ -1263,6 +1269,7 @@ impl VirtualOp { /* Non-VM Instructions */ BLOB(i) => Self::BLOB(i.clone()), DataSectionOffsetPlaceholder => Self::DataSectionOffsetPlaceholder, + Metadata => Self::Metadata, LoadDataId(r1, i) => Self::LoadDataId(update_reg(reg_to_reg_map, r1), i.clone()), AddrDataId(r1, i) => Self::AddrDataId(update_reg(reg_to_reg_map, r1), i.clone()), Undefined => Self::Undefined, @@ -1743,6 +1750,7 @@ impl VirtualOp { /* Non-VM Instructions */ BLOB(imm) => AllocatedOpcode::BLOB(imm.clone()), DataSectionOffsetPlaceholder => AllocatedOpcode::DataSectionOffsetPlaceholder, + Metadata => AllocatedOpcode::Metadata, LoadDataId(reg1, label) => { AllocatedOpcode::LoadDataId(map_reg(&mapping, reg1), label.clone()) }