From a7b698e96aed0f3d6f6a35249a6e5ca733b23488 Mon Sep 17 00:00:00 2001 From: xunilrj Date: Fri, 19 Jul 2024 15:43:15 +0100 Subject: [PATCH] better dbg --- test/src/e2e_vm_tests/mod.rs | 2 +- .../language/slice/slice_intrinsics/Forc.lock | 8 + .../language/slice/slice_intrinsics/Forc.toml | 1 + .../slice/slice_intrinsics/src/main.sw | 170 ++++++------------ test/src/e2e_vm_tests/utils/.gitignore | 2 + test/src/e2e_vm_tests/utils/Forc.toml | 8 + test/src/e2e_vm_tests/utils/src/main.sw | 146 +++++++++++++++ 7 files changed, 216 insertions(+), 121 deletions(-) create mode 100644 test/src/e2e_vm_tests/utils/.gitignore create mode 100644 test/src/e2e_vm_tests/utils/Forc.toml create mode 100644 test/src/e2e_vm_tests/utils/src/main.sw diff --git a/test/src/e2e_vm_tests/mod.rs b/test/src/e2e_vm_tests/mod.rs index b163d64d892..4c8664fbe40 100644 --- a/test/src/e2e_vm_tests/mod.rs +++ b/test/src/e2e_vm_tests/mod.rs @@ -146,7 +146,7 @@ fn print_receipts(output: &mut String, receipts: &[Receipt]) { let data = data.as_deref().unwrap(); let s = u64::from_be_bytes(data.try_into().unwrap()); - text_log.push_str(&format!("[{}]", s)); + text_log.push_str(&format!("{}", s)); } 2 => { text_log.push('\n'); diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/language/slice/slice_intrinsics/Forc.lock b/test/src/e2e_vm_tests/test_programs/should_pass/language/slice/slice_intrinsics/Forc.lock index c5bc5415952..a7de4b3565f 100644 --- a/test/src/e2e_vm_tests/test_programs/should_pass/language/slice/slice_intrinsics/Forc.lock +++ b/test/src/e2e_vm_tests/test_programs/should_pass/language/slice/slice_intrinsics/Forc.lock @@ -5,4 +5,12 @@ source = "path+from-root-53D6DC514F06A250" [[package]] name = "slice_intrinsics" source = "member" +dependencies = [ + "core", + "utils", +] + +[[package]] +name = "utils" +source = "path+from-root-53D6DC514F06A250" dependencies = ["core"] diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/language/slice/slice_intrinsics/Forc.toml b/test/src/e2e_vm_tests/test_programs/should_pass/language/slice/slice_intrinsics/Forc.toml index a7ed10b10d3..fe47736bbcf 100644 --- a/test/src/e2e_vm_tests/test_programs/should_pass/language/slice/slice_intrinsics/Forc.toml +++ b/test/src/e2e_vm_tests/test_programs/should_pass/language/slice/slice_intrinsics/Forc.toml @@ -6,3 +6,4 @@ name = "slice_intrinsics" [dependencies] core = { path = "../../../../../../../../sway-lib-core" } +utils = { path = "../../../../../../../../test/src/e2e_vm_tests/utils" } diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/language/slice/slice_intrinsics/src/main.sw b/test/src/e2e_vm_tests/test_programs/should_pass/language/slice/slice_intrinsics/src/main.sw index 7c1ba559a4d..cf66404e5b4 100644 --- a/test/src/e2e_vm_tests/test_programs/should_pass/language/slice/slice_intrinsics/src/main.sw +++ b/test/src/e2e_vm_tests/test_programs/should_pass/language/slice/slice_intrinsics/src/main.sw @@ -1,132 +1,65 @@ script; -trait Log { - fn log(self); -} - -impl Log for str { - fn log(self) { - let encoded = encode(self); - asm(ra: u64::max(), ptr: encoded.ptr(), len: encoded.len::()) { - logd ra zero ptr len; - } - } -} - - -impl Log for u64 { - fn log(self) { - let encoded = encode(self); - asm(ra: u64::max(), ptr: encoded.ptr(), len: encoded.len::()) { - logd ra one ptr len; - } +use utils::*; + +fn alloc_slice(len: u64) -> &__slice[T] { + let size_in_bytes = len * __size_of::(); + let ptr = asm(size_in_bytes: size_in_bytes) { + aloc size_in_bytes; + hp: raw_ptr + }; + asm(buf: (ptr, len)) { + buf: &__slice[T] } } -struct NewLine {} - -fn new_line() -> NewLine { - NewLine { } -} - -impl Log for NewLine { - fn log(self) { - asm(ra: u64::max(), rb: 2) { - logd ra rb zero zero; - } - } -} +fn realloc_slice(old: &__slice[T], len: u64) -> &__slice[T] { + let old_ptr = old.ptr(); + let old_len_in_bytes = old.len() * __size_of::(); -impl Log for (A, B) -where - A: Log, - B: Log, -{ - fn log(self) { - self.0.log(); - self.1.log(); - } -} + let new_len_in_bytes = len * __size_of::(); + let new_ptr = asm(new_len_in_bytes: new_len_in_bytes, old_ptr: old_ptr, old_len_in_bytes: old_len_in_bytes) { + aloc new_len_in_bytes; + mcp hp old_ptr old_len_in_bytes; + hp: raw_ptr + }; -impl Log for (A, B, C) -where - A: Log, - B: Log, - C: Log, -{ - #[allow(dead_code)] - fn log(self) { - self.0.log(); - self.1.log(); - self.2.log(); + asm(buf: (new_ptr, len)) { + buf: &__slice[T] } } -impl Log for (A, B, C, D) -where - A: Log, - B: Log, - C: Log, - D: Log -{ - fn log(self) { - self.0.log(); - self.1.log(); - self.2.log(); - self.3.log(); - } -} - - -impl Log for (A, B, C, D, E) -where - A: Log, - B: Log, - C: Log, - D: Log, - E: Log, -{ - fn log(self) { - self.0.log(); - self.1.log(); - self.2.log(); - self.3.log(); - self.4.log(); - } +pub struct Vec { + buf: &__slice[T], + len: u64, } -impl Log for Vec { - fn log(self) { +impl Dbg for Vec { + fn dbg(self) { ( - "Vec", + "Vec { buf: (ptr: ", asm(v: self.buf.ptr()) { v: u64 }, + ", len: ", self.buf.len(), + "), len: ", self.len, - ).log(); + ")" + ).dbg(); } } -pub struct Vec { - buf: &__slice[T], - len: u64, -} impl Vec { pub fn new() -> Self { - let ptr = asm() { - hp: raw_ptr - }; Self { - buf: asm(buf: (ptr, 0)) { - buf: &__slice[T] - }, + buf: alloc_slice::(0), len: 0 } } pub fn push(ref mut self, item: T) { - ("Vec::push", new_line()).log(); - (self, new_line()).log(); + "Vec::push(...)".dbgln(); + (" ", self).dbgln(); let new_item_idx = self.len; let current_cap = self.buf.len(); @@ -136,35 +69,32 @@ impl Vec { } else { current_cap * 2 }; - let new_cap_in_bytes = new_cap * __size_of::(); - - let old_buf_ptr = self.buf.ptr(); - let old_cap_in_bytes = current_cap * __size_of::(); - - let ptr = asm(new_cap_in_bytes: new_cap_in_bytes, old_buf_ptr: old_buf_ptr, old_cap_in_bytes: old_cap_in_bytes) { - aloc new_cap_in_bytes; - mcp hp old_buf_ptr old_cap_in_bytes; - hp: raw_ptr - }; - - self.buf = asm(buf: (ptr, new_cap)) { - buf: &__slice[T] - }; + self.buf = realloc_slice(self.buf, new_cap); + (" After realloc: ", self).dbgln(); } - (self, new_line()).log(); - let v: &mut T = __slice_elem(self.buf, new_item_idx); - ("elem", new_item_idx, " at ", asm(v: v) { v: u64 }, NewLine{}).log(); + + let buffer_addr = asm(v: self.buf.ptr()) { v: u64 }; + let elem_addr = asm(v: v) { v: u64 }; + (" elem ", new_item_idx, " at ", elem_addr, " buffer offset (in bytes): ", elem_addr - buffer_addr).dbgln(); *v = item; self.len += 1; + + (" ", self).dbgln(); } pub fn get(self, index: u64) -> T { - ("Vec::get ", index).log(); + ("Vec::get(", index, ")").dbgln(); + (" ", self).dbgln(); + let item: &mut T = __slice_elem(self.buf, index); - (asm(v: item) { v: u64 }, NewLine{}).log(); + + let buffer_addr = asm(v: self.buf.ptr()) { v: u64 }; + let elem_addr = asm(v: item) { v: u64 }; + (" element ", index, " at ", elem_addr, " buffer offset (in bytes): ", elem_addr - buffer_addr).dbgln(); + *item } } diff --git a/test/src/e2e_vm_tests/utils/.gitignore b/test/src/e2e_vm_tests/utils/.gitignore new file mode 100644 index 00000000000..77d3844f58c --- /dev/null +++ b/test/src/e2e_vm_tests/utils/.gitignore @@ -0,0 +1,2 @@ +out +target diff --git a/test/src/e2e_vm_tests/utils/Forc.toml b/test/src/e2e_vm_tests/utils/Forc.toml new file mode 100644 index 00000000000..10a6cda5587 --- /dev/null +++ b/test/src/e2e_vm_tests/utils/Forc.toml @@ -0,0 +1,8 @@ +[project] +authors = ["Fuel Labs "] +entry = "main.sw" +license = "Apache-2.0" +name = "utils" + +[dependencies] +core = { path = "../../../../sway-lib-core" } diff --git a/test/src/e2e_vm_tests/utils/src/main.sw b/test/src/e2e_vm_tests/utils/src/main.sw new file mode 100644 index 00000000000..4dcfbf52f28 --- /dev/null +++ b/test/src/e2e_vm_tests/utils/src/main.sw @@ -0,0 +1,146 @@ +library; + +/// This is only useful for the e2e harness setup, because +/// no one else knows how to "decode" this into meaningful +// textual logs. +pub trait Dbg { + fn dbg(self); +} { + fn dbgln(self) { + self.dbg(); + asm(ra: u64::max(), rb: 2) { + logd ra rb zero zero; + } + } +} + +impl Dbg for str { + fn dbg(self) { + let encoded = encode(self); + asm(ra: u64::max(), ptr: encoded.ptr(), len: encoded.len::()) { + logd ra zero ptr len; + } + } +} + + +impl Dbg for u64 { + fn dbg(self) { + let encoded = encode(self); + asm(ra: u64::max(), ptr: encoded.ptr(), len: encoded.len::()) { + logd ra one ptr len; + } + } +} + +pub struct NewLine {} + +pub fn new_line() -> NewLine { + NewLine { } +} + +impl Dbg for NewLine { + fn dbg(self) { + asm(ra: u64::max(), rb: 2) { + logd ra rb zero zero; + } + } +} + +impl Dbg for (A, B) +where + A: Dbg, + B: Dbg, +{ + fn dbg(self) { + self.0.dbg(); + self.1.dbg(); + } +} + +impl Dbg for (A, B, C) +where + A: Dbg, + B: Dbg, + C: Dbg, +{ + #[allow(dead_code)] + fn dbg(self) { + self.0.dbg(); + self.1.dbg(); + self.2.dbg(); + } +} + +impl Dbg for (A, B, C, D) +where + A: Dbg, + B: Dbg, + C: Dbg, + D: Dbg +{ + fn dbg(self) { + self.0.dbg(); + self.1.dbg(); + self.2.dbg(); + self.3.dbg(); + } +} + + +impl Dbg for (A, B, C, D, E) +where + A: Dbg, + B: Dbg, + C: Dbg, + D: Dbg, + E: Dbg, +{ + fn dbg(self) { + self.0.dbg(); + self.1.dbg(); + self.2.dbg(); + self.3.dbg(); + self.4.dbg(); + } +} + +impl Dbg for (A, B, C, D, E, F) +where + A: Dbg, + B: Dbg, + C: Dbg, + D: Dbg, + E: Dbg, + F: Dbg, +{ + fn dbg(self) { + self.0.dbg(); + self.1.dbg(); + self.2.dbg(); + self.3.dbg(); + self.4.dbg(); + self.5.dbg(); + } +} + +impl Dbg for (A, B, C, D, E, F, G) +where + A: Dbg, + B: Dbg, + C: Dbg, + D: Dbg, + E: Dbg, + F: Dbg, + G: Dbg +{ + fn dbg(self) { + self.0.dbg(); + self.1.dbg(); + self.2.dbg(); + self.3.dbg(); + self.4.dbg(); + self.5.dbg(); + self.6.dbg(); + } +}