Skip to content

Commit

Permalink
EncodeBufferAppend: grow_if_needed should allocate sufficiently (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
vaivaswatha authored Nov 2, 2024
1 parent b6bbbf8 commit 3ff63a5
Show file tree
Hide file tree
Showing 18 changed files with 60 additions and 45 deletions.
8 changes: 4 additions & 4 deletions forc-plugins/forc-client/tests/deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ async fn test_simple_deploy() {
node.kill().unwrap();
let expected = vec![DeployedPackage::Contract(DeployedContract {
id: ContractId::from_str(
"adcd0480deb735ffd8f5bd0683b66c55465e0841076efca4c28be25c767ede5b",
"5c51b8904c539700852c646c6700fddab4b80477f66e56fb2515736facd84e69",
)
.unwrap(),
proxy: None,
Expand Down Expand Up @@ -416,7 +416,7 @@ async fn test_deploy_submit_only() {
node.kill().unwrap();
let expected = vec![DeployedPackage::Contract(DeployedContract {
id: ContractId::from_str(
"adcd0480deb735ffd8f5bd0683b66c55465e0841076efca4c28be25c767ede5b",
"5c51b8904c539700852c646c6700fddab4b80477f66e56fb2515736facd84e69",
)
.unwrap(),
proxy: None,
Expand Down Expand Up @@ -462,12 +462,12 @@ async fn test_deploy_fresh_proxy() {
node.kill().unwrap();
let impl_contract = DeployedPackage::Contract(DeployedContract {
id: ContractId::from_str(
"adcd0480deb735ffd8f5bd0683b66c55465e0841076efca4c28be25c767ede5b",
"5c51b8904c539700852c646c6700fddab4b80477f66e56fb2515736facd84e69",
)
.unwrap(),
proxy: Some(
ContractId::from_str(
"5297238a1d867c9d7c8fa83c700e2d0d1c57e1874ec95ff4a67063e222ab1880",
"7a78517c2c3322028db65e54893dc97958fa3d7c846a66f5675859e64f927540",
)
.unwrap(),
),
Expand Down
8 changes: 4 additions & 4 deletions forc/tests/cli_integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ fn test_forc_test_raw_logs() -> Result<(), rexpect::error::Error> {
// Assert that the output is correct
process.exp_string(" test test_log_4")?;
process.exp_string("Raw logs:")?;
process.exp_string(r#"[{"LogData":{"data":"0000000000000004","digest":"8005f02d43fa06e7d0585fb64c961d57e318b27a145c857bcd3a6bdb413ff7fc","id":"0000000000000000000000000000000000000000000000000000000000000000","is":10368,"len":8,"pc":12660,"ptr":67107840,"ra":0,"rb":1515152261580153489}}]"#)?;
process.exp_string(r#"[{"LogData":{"data":"0000000000000004","digest":"8005f02d43fa06e7d0585fb64c961d57e318b27a145c857bcd3a6bdb413ff7fc","id":"0000000000000000000000000000000000000000000000000000000000000000","is":10368,"len":8,"pc":12672,"ptr":67107840,"ra":0,"rb":1515152261580153489}}]"#)?;
process.exp_string(" test test_log_2")?;
process.exp_string("Raw logs:")?;
process.exp_string(r#"[{"LogData":{"data":"0000000000000002","digest":"cd04a4754498e06db5a13c5f371f1f04ff6d2470f24aa9bd886540e5dce77f70","id":"0000000000000000000000000000000000000000000000000000000000000000","is":10368,"len":8,"pc":12660,"ptr":67107840,"ra":0,"rb":1515152261580153489}}]"#)?;
process.exp_string(r#"[{"LogData":{"data":"0000000000000002","digest":"cd04a4754498e06db5a13c5f371f1f04ff6d2470f24aa9bd886540e5dce77f70","id":"0000000000000000000000000000000000000000000000000000000000000000","is":10368,"len":8,"pc":12672,"ptr":67107840,"ra":0,"rb":1515152261580153489}}]"#)?;

process.process.exit()?;
Ok(())
Expand All @@ -74,11 +74,11 @@ fn test_forc_test_both_logs() -> Result<(), rexpect::error::Error> {
process.exp_string(" test test_log_4")?;
process.exp_string("Decoded log value: 4, log rb: 1515152261580153489")?;
process.exp_string("Raw logs:")?;
process.exp_string(r#"[{"LogData":{"data":"0000000000000004","digest":"8005f02d43fa06e7d0585fb64c961d57e318b27a145c857bcd3a6bdb413ff7fc","id":"0000000000000000000000000000000000000000000000000000000000000000","is":10368,"len":8,"pc":12660,"ptr":67107840,"ra":0,"rb":1515152261580153489}}]"#)?;
process.exp_string(r#"[{"LogData":{"data":"0000000000000004","digest":"8005f02d43fa06e7d0585fb64c961d57e318b27a145c857bcd3a6bdb413ff7fc","id":"0000000000000000000000000000000000000000000000000000000000000000","is":10368,"len":8,"pc":12672,"ptr":67107840,"ra":0,"rb":1515152261580153489}}]"#)?;
process.exp_string(" test test_log_2")?;
process.exp_string("Decoded log value: 2, log rb: 1515152261580153489")?;
process.exp_string("Raw logs:")?;
process.exp_string(r#"[{"LogData":{"data":"0000000000000002","digest":"cd04a4754498e06db5a13c5f371f1f04ff6d2470f24aa9bd886540e5dce77f70","id":"0000000000000000000000000000000000000000000000000000000000000000","is":10368,"len":8,"pc":12660,"ptr":67107840,"ra":0,"rb":1515152261580153489}}]"#)?;
process.exp_string(r#"[{"LogData":{"data":"0000000000000002","digest":"cd04a4754498e06db5a13c5f371f1f04ff6d2470f24aa9bd886540e5dce77f70","id":"0000000000000000000000000000000000000000000000000000000000000000","is":10368,"len":8,"pc":12672,"ptr":67107840,"ra":0,"rb":1515152261580153489}}]"#)?;
process.process.exit()?;
Ok(())
}
10 changes: 7 additions & 3 deletions sway-core/src/ir_generation/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1783,7 +1783,7 @@ impl<'eng> FnCompiler<'eng> {
);

// needs realloc block
// new_cap = cap * 2
// new_cap = (cap * 2) + needed_size
// aloc new_cap
// mcp hp old_ptr len
// hp: ptr u8
Expand All @@ -1793,11 +1793,15 @@ impl<'eng> FnCompiler<'eng> {

let two = Constant::new_uint(context, 64, 2);
let two = Value::new_constant(context, two);
let new_cap =
let new_cap_part =
s.current_block
.append(context)
.binary_op(BinaryOpKind::Mul, cap, two);

let new_cap = s.current_block.append(context).binary_op(
BinaryOpKind::Add,
new_cap_part,
needed_size,
);
let new_ptr = s.current_block.append(context).asm_block(
vec![
AsmArg {
Expand Down
11 changes: 10 additions & 1 deletion sway-lib-core/src/codec.sw
Original file line number Diff line number Diff line change
Expand Up @@ -5156,6 +5156,15 @@ fn to_slice<T>(array: T) -> raw_slice {
raw_slice::from_parts::<u8>(__addr_of(array), len)
}

fn assert_ge<T>(a: T, b: T, revert_code: u64)
where
T: Ord,
{
if a.lt(b) {
__revert(revert_code)
}
}

fn assert_eq<T>(a: T, b: T, revert_code: u64)
where
T: Eq,
Expand Down Expand Up @@ -5205,7 +5214,7 @@ where
// Append another item
let buffer = value_to_append.abi_encode(buffer);
assert_neq(ptr1, buffer.buffer.0, 4); // must have allocated new buffer
assert_eq(buffer.buffer.1, size_of_t * 2, 5); // capacity for two items
assert_ge(buffer.buffer.1, size_of_t * 2, 5); // capacity for at least two items
assert_eq(buffer.buffer.2, size_of_t * 2, 6); // buffer has two items

// Check that red zones were not overwritten
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
source: test/tests/tests.rs
assertion_line: 99
assertion_line: 115
---
> forc build --path test/src/e2e_vm_tests/test_programs/should_pass/language/configurable_dedup_decode --release --ir final
exit status: 0
Expand Down Expand Up @@ -207,24 +207,25 @@ script {
encode_10_abi_encode_11_block1():
v81 = const u64 2
v82 = mul v33, v81, !116
v83 = asm(new_cap: v82, old_ptr: v30, len: v36) -> ptr u8 hp, !117 {
v83 = add v82, v37, !117
v84 = asm(new_cap: v83, old_ptr: v30, len: v36) -> ptr u8 hp, !118 {
aloc new_cap
mcp hp old_ptr len
}
br encode_10_abi_encode_11_block0(v83, v82), !118
br encode_10_abi_encode_11_block0(v84, v83), !119
}

entry_orig fn main_8() -> u64, !121 {
entry_orig fn main_8() -> u64, !122 {
entry():
v0 = get_config ptr { u64 }, WRAPPED, !122
v0 = get_config ptr { u64 }, WRAPPED, !123
v1 = const u64 0
v2 = get_elem_ptr v0, ptr u64, v1, !123
v2 = get_elem_ptr v0, ptr u64, v1, !124
v3 = load v2
v4 = get_config ptr { u64 }, TUPLE, !124
v4 = get_config ptr { u64 }, TUPLE, !125
v5 = const u64 0
v6 = get_elem_ptr v4, ptr u64, v5, !125
v6 = get_elem_ptr v4, ptr u64, v5, !126
v7 = load v6
v8 = add v3, v7, !128
v8 = add v3, v7, !129
ret u64 v8
}
}
Expand Down Expand Up @@ -348,15 +349,16 @@ script {
!116 = (!49 !50 !66 !67)
!117 = (!49 !50 !66 !67)
!118 = (!49 !50 !66 !67)
!119 = span !0 202 246
!120 = fn_name_span !0 205 209
!121 = (!119 !120)
!122 = span !0 225 232
!123 = span !0 30 36
!124 = span !0 237 242
!125 = span !0 243 244
!126 = span !0 225 244
!127 = fn_call_path_span !0 235 236
!128 = (!126 !127)
!119 = (!49 !50 !66 !67)
!120 = span !0 202 246
!121 = fn_name_span !0 205 209
!122 = (!120 !121)
!123 = span !0 225 232
!124 = span !0 30 36
!125 = span !0 237 242
!126 = span !0 243 244
!127 = span !0 225 244
!128 = fn_call_path_span !0 235 236
!129 = (!127 !128)

Finished release [optimized + fuel] target(s) [744 B] in ???
Finished release [optimized + fuel] target(s) [752 B] in ???
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
{
"concreteTypeId": "1b5759d94094368cfd443019e7ca5ec4074300e544e5ea993a979f5da627261e",
"name": "SOME_U256",
"offset": 864
"offset": 872
}
],
"encodingVersion": "1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::hash::*;
#[cfg(experimental_new_encoding = false)]
const CONTRACT_ID = 0x14ed3cd06c2947248f69d54bfa681fe40d26267be84df7e19e253622b7921bbe;
#[cfg(experimental_new_encoding = true)]
const CONTRACT_ID = 0x91c3e72c3b3f4bf7bd5c548d8752c766d5fb8ebf15be3cf92ff34682da9bfb4d; // AUTO-CONTRACT-ID ../../test_contracts/array_of_structs_contract --release
const CONTRACT_ID = 0x6b01f04c84ce955e8dfc6ed3611fd2518424ab757e5a540878f49fbd1bdec571; // AUTO-CONTRACT-ID ../../test_contracts/array_of_structs_contract --release

fn get_address() -> Option<std::address::Address> {
Some(CONTRACT_ID.into())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const FUEL_COIN_CONTRACT_ID = 0x19c0d374734bd8a92b776787e9dffa0f105a90e3c977626f
#[cfg(experimental_new_encoding = false)]
const BALANCE_CONTRACT_ID = 0xf6cd545152ac83225e8e7df2efb5c6fa6e37bc9b9e977b5ea8103d28668925df;
#[cfg(experimental_new_encoding = true)]
const BALANCE_CONTRACT_ID = 0xc0ae93ca2a6ec1c740a4869a3f5bfe061184e30e4e5c6185891d3ec2dba9a33d; // AUTO-CONTRACT-ID ../../test_contracts/balance_test_contract --release
const BALANCE_CONTRACT_ID = 0xb770fa56f665d6fbdbdceecce21b7b61878f650981ac7f21c052613015937034; // AUTO-CONTRACT-ID ../../test_contracts/balance_test_contract --release

fn main() -> bool {
let default_gas = 1_000_000_000_000;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use balance_test_abi::BalanceTest;
#[cfg(experimental_new_encoding = false)]
const CONTRACT_ID = 0xf6cd545152ac83225e8e7df2efb5c6fa6e37bc9b9e977b5ea8103d28668925df;
#[cfg(experimental_new_encoding = true)]
const CONTRACT_ID = 0xc0ae93ca2a6ec1c740a4869a3f5bfe061184e30e4e5c6185891d3ec2dba9a33d; // AUTO-CONTRACT-ID ../../test_contracts/balance_test_contract --release
const CONTRACT_ID = 0xb770fa56f665d6fbdbdceecce21b7b61878f650981ac7f21c052613015937034; // AUTO-CONTRACT-ID ../../test_contracts/balance_test_contract --release

fn main() -> bool {
let balance_test_contract = abi(BalanceTest, CONTRACT_ID);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use abi_with_tuples::{MyContract, Location, Person};
#[cfg(experimental_new_encoding = false)]
const CONTRACT_ID = 0xfdc14550c8aee742cd556d0ab7f378b7be0d3b1e6e086c097352e94590d4ed02;
#[cfg(experimental_new_encoding = true)]
const CONTRACT_ID = 0xeb1d43bccc97620c80599c25722b1be3a22affd0570cfdcc57deac7c729e5389; // AUTO-CONTRACT-ID ../../test_contracts/abi_with_tuples_contract --release
const CONTRACT_ID = 0xcc679c89d2950879d4f8fb3b99770f93dfde3335fff574cbf0588691fbcefde3; // AUTO-CONTRACT-ID ../../test_contracts/abi_with_tuples_contract --release

fn main() -> bool {
let the_abi = abi(MyContract, CONTRACT_ID);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use basic_storage_abi::{BasicStorage, Quad};
#[cfg(experimental_new_encoding = false)]
const CONTRACT_ID = 0x94db39f409a31b9f2ebcadeea44378e419208c20de90f5d8e1e33dc1523754cb;
#[cfg(experimental_new_encoding = true)]
const CONTRACT_ID = 0x770eb6e1b7de06098474e110cac9ed32c72c455ba7f34bea00ebb47a43986701; // AUTO-CONTRACT-ID ../../test_contracts/basic_storage --release
const CONTRACT_ID = 0x38ea1d4d087b96c1e0b4b70e031cf1acc02363f000e8059d02ffc07d61885452; // AUTO-CONTRACT-ID ../../test_contracts/basic_storage --release

fn main() -> u64 {
let addr = abi(BasicStorage, CONTRACT_ID);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use contract_with_type_aliases_abi::*;
#[cfg(experimental_new_encoding = false)]
const CONTRACT_ID = 0x0cbeb6efe3104b460be769bdc4ea101ebf16ccc16f2d7b667ec3e1c7f5ce35b5;
#[cfg(experimental_new_encoding = true)]
const CONTRACT_ID = 0xa3ebed6b4c9d17001bf759332daaa22c2e522cb1c61116ac395443f5ab392225; // AUTO-CONTRACT-ID ../../test_contracts/contract_with_type_aliases --release
const CONTRACT_ID = 0x2e8efc627379d037c6ac70249d0bf0726f228ea6bade786e11639b878241f333; // AUTO-CONTRACT-ID ../../test_contracts/contract_with_type_aliases --release

fn main() {
let caller = abi(MyContract, CONTRACT_ID);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use dynamic_contract_call::*;
#[cfg(experimental_new_encoding = false)]
const CONTRACT_ID = 0xd1b4047af7ef111c023ab71069e01dc2abfde487c0a0ce1268e4f447e6c6e4c2;
#[cfg(experimental_new_encoding = true)]
const CONTRACT_ID = 0x3a0ad1930a751f3849e10d55aedad4b14be604a1807b3016db959a311dbbd101; // AUTO-CONTRACT-ID ../../test_contracts/increment_contract --release
const CONTRACT_ID = 0xc2694e6a397883ae59ea2e807c0ba3714865d0c4bf16571893d632b3bd7ec6f7; // AUTO-CONTRACT-ID ../../test_contracts/increment_contract --release

fn main() -> bool {
let the_abi = abi(Incrementor, CONTRACT_ID);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use storage_enum_abi::*;
#[cfg(experimental_new_encoding = false)]
const CONTRACT_ID = 0xc601d11767195485a6654d566c67774134668863d8c797a8c69e8778fb1f89e9;
#[cfg(experimental_new_encoding = true)]
const CONTRACT_ID = 0x14df60f48d5874d2623ba7f5604987b29f6288a1ae35a130bfae57e64f74aa89; // AUTO-CONTRACT-ID ../../test_contracts/storage_enum_contract --release
const CONTRACT_ID = 0xd3da5ae426182db1a2a6c900ad374a9641dabfdebe610b39f0e9c5186cedf39b; // AUTO-CONTRACT-ID ../../test_contracts/storage_enum_contract --release

fn main() -> u64 {
let caller = abi(StorageEnum, CONTRACT_ID);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use auth_testing_abi::AuthTesting;
#[cfg(experimental_new_encoding = false)]
const CONTRACT_ID = 0xc2eec20491b53aab7232cbd27c31d15417b4e9daf0b89c74cc242ef1295f681f;
#[cfg(experimental_new_encoding = true)]
const CONTRACT_ID = 0xa3ee3b5d5469efa870ba468c36c3775ea5287da385d70fbcd4aadbf5acb5a772; // AUTO-CONTRACT-ID ../../test_contracts/auth_testing_contract --release
const CONTRACT_ID = 0xcdc67c314d13752e5c3e10aa1de6615e1ec6864e0461317b5f26eef3320a9a8e; // AUTO-CONTRACT-ID ../../test_contracts/auth_testing_contract --release

// should be false in the case of a script
fn main() -> bool {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use context_testing_abi::*;
#[cfg(experimental_new_encoding = false)]
const CONTRACT_ID = 0x6054c11cda000f5990373a4d61929396165be4dfdd61d5b7bd26da60ab0d8577;
#[cfg(experimental_new_encoding = true)]
const CONTRACT_ID = 0x16f15adbe1fbdee5d19349fc245429dad3265f8a0f7a8143ffec39de8c809ff4; // AUTO-CONTRACT-ID ../../test_contracts/context_testing_contract --release
const CONTRACT_ID = 0xecf511a0e2f4022c17413625d69c390d51d0740424d6886cbbe9ca60acd0606b; // AUTO-CONTRACT-ID ../../test_contracts/context_testing_contract --release

fn main() -> bool {
let gas: u64 = u64::max();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use nested_struct_args_abi::*;
#[cfg(experimental_new_encoding = false)]
const CONTRACT_ID = 0xe63d33a1b3a6903808b379f6a41a72fa8a370e8b76626775e7d9d2f9c4c5da40;
#[cfg(experimental_new_encoding = true)]
const CONTRACT_ID = 0xd9e836ce255122c0c243cb0bd6d10450d87206f995d127e60e90bf9ab4fc3532; // AUTO-CONTRACT-ID ../../test_contracts/nested_struct_args_contract --release
const CONTRACT_ID = 0x27e6a749a1773f65f850b36e4e61afdbfe72ba4189cd518c16e06cf0c04a1fa9; // AUTO-CONTRACT-ID ../../test_contracts/nested_struct_args_contract --release

fn main() -> bool {
let caller = abi(NestedStructArgs, CONTRACT_ID);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::hash::*;
#[cfg(experimental_new_encoding = false)]
const CONTRACT_ID = 0x3bc28acd66d327b8c1b9624c1fabfc07e9ffa1b5d71c2832c3bfaaf8f4b805e9;
#[cfg(experimental_new_encoding = true)]
const CONTRACT_ID = 0x9075162ba3ecabb57aae8ddcd785d8a1211d08281de71c34a1772a888e339ba1; // AUTO-CONTRACT-ID ../../test_contracts/storage_access_contract --release
const CONTRACT_ID = 0xb509b52a765f2a8e97b29e4699d0ec8fa056b2bb649f785c75e36f868c318b98; // AUTO-CONTRACT-ID ../../test_contracts/storage_access_contract --release

fn main() -> bool {
let caller = abi(StorageAccess, CONTRACT_ID);
Expand Down

0 comments on commit 3ff63a5

Please sign in to comment.