Skip to content

Commit 3ff63a5

Browse files
authored
EncodeBufferAppend: grow_if_needed should allocate sufficiently (#6686)
1 parent b6bbbf8 commit 3ff63a5

File tree

18 files changed

+60
-45
lines changed

18 files changed

+60
-45
lines changed

forc-plugins/forc-client/tests/deploy.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ async fn test_simple_deploy() {
373373
node.kill().unwrap();
374374
let expected = vec![DeployedPackage::Contract(DeployedContract {
375375
id: ContractId::from_str(
376-
"adcd0480deb735ffd8f5bd0683b66c55465e0841076efca4c28be25c767ede5b",
376+
"5c51b8904c539700852c646c6700fddab4b80477f66e56fb2515736facd84e69",
377377
)
378378
.unwrap(),
379379
proxy: None,
@@ -416,7 +416,7 @@ async fn test_deploy_submit_only() {
416416
node.kill().unwrap();
417417
let expected = vec![DeployedPackage::Contract(DeployedContract {
418418
id: ContractId::from_str(
419-
"adcd0480deb735ffd8f5bd0683b66c55465e0841076efca4c28be25c767ede5b",
419+
"5c51b8904c539700852c646c6700fddab4b80477f66e56fb2515736facd84e69",
420420
)
421421
.unwrap(),
422422
proxy: None,
@@ -462,12 +462,12 @@ async fn test_deploy_fresh_proxy() {
462462
node.kill().unwrap();
463463
let impl_contract = DeployedPackage::Contract(DeployedContract {
464464
id: ContractId::from_str(
465-
"adcd0480deb735ffd8f5bd0683b66c55465e0841076efca4c28be25c767ede5b",
465+
"5c51b8904c539700852c646c6700fddab4b80477f66e56fb2515736facd84e69",
466466
)
467467
.unwrap(),
468468
proxy: Some(
469469
ContractId::from_str(
470-
"5297238a1d867c9d7c8fa83c700e2d0d1c57e1874ec95ff4a67063e222ab1880",
470+
"7a78517c2c3322028db65e54893dc97958fa3d7c846a66f5675859e64f927540",
471471
)
472472
.unwrap(),
473473
),

forc/tests/cli_integration.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@ fn test_forc_test_raw_logs() -> Result<(), rexpect::error::Error> {
4949
// Assert that the output is correct
5050
process.exp_string(" test test_log_4")?;
5151
process.exp_string("Raw logs:")?;
52-
process.exp_string(r#"[{"LogData":{"data":"0000000000000004","digest":"8005f02d43fa06e7d0585fb64c961d57e318b27a145c857bcd3a6bdb413ff7fc","id":"0000000000000000000000000000000000000000000000000000000000000000","is":10368,"len":8,"pc":12660,"ptr":67107840,"ra":0,"rb":1515152261580153489}}]"#)?;
52+
process.exp_string(r#"[{"LogData":{"data":"0000000000000004","digest":"8005f02d43fa06e7d0585fb64c961d57e318b27a145c857bcd3a6bdb413ff7fc","id":"0000000000000000000000000000000000000000000000000000000000000000","is":10368,"len":8,"pc":12672,"ptr":67107840,"ra":0,"rb":1515152261580153489}}]"#)?;
5353
process.exp_string(" test test_log_2")?;
5454
process.exp_string("Raw logs:")?;
55-
process.exp_string(r#"[{"LogData":{"data":"0000000000000002","digest":"cd04a4754498e06db5a13c5f371f1f04ff6d2470f24aa9bd886540e5dce77f70","id":"0000000000000000000000000000000000000000000000000000000000000000","is":10368,"len":8,"pc":12660,"ptr":67107840,"ra":0,"rb":1515152261580153489}}]"#)?;
55+
process.exp_string(r#"[{"LogData":{"data":"0000000000000002","digest":"cd04a4754498e06db5a13c5f371f1f04ff6d2470f24aa9bd886540e5dce77f70","id":"0000000000000000000000000000000000000000000000000000000000000000","is":10368,"len":8,"pc":12672,"ptr":67107840,"ra":0,"rb":1515152261580153489}}]"#)?;
5656

5757
process.process.exit()?;
5858
Ok(())
@@ -74,11 +74,11 @@ fn test_forc_test_both_logs() -> Result<(), rexpect::error::Error> {
7474
process.exp_string(" test test_log_4")?;
7575
process.exp_string("Decoded log value: 4, log rb: 1515152261580153489")?;
7676
process.exp_string("Raw logs:")?;
77-
process.exp_string(r#"[{"LogData":{"data":"0000000000000004","digest":"8005f02d43fa06e7d0585fb64c961d57e318b27a145c857bcd3a6bdb413ff7fc","id":"0000000000000000000000000000000000000000000000000000000000000000","is":10368,"len":8,"pc":12660,"ptr":67107840,"ra":0,"rb":1515152261580153489}}]"#)?;
77+
process.exp_string(r#"[{"LogData":{"data":"0000000000000004","digest":"8005f02d43fa06e7d0585fb64c961d57e318b27a145c857bcd3a6bdb413ff7fc","id":"0000000000000000000000000000000000000000000000000000000000000000","is":10368,"len":8,"pc":12672,"ptr":67107840,"ra":0,"rb":1515152261580153489}}]"#)?;
7878
process.exp_string(" test test_log_2")?;
7979
process.exp_string("Decoded log value: 2, log rb: 1515152261580153489")?;
8080
process.exp_string("Raw logs:")?;
81-
process.exp_string(r#"[{"LogData":{"data":"0000000000000002","digest":"cd04a4754498e06db5a13c5f371f1f04ff6d2470f24aa9bd886540e5dce77f70","id":"0000000000000000000000000000000000000000000000000000000000000000","is":10368,"len":8,"pc":12660,"ptr":67107840,"ra":0,"rb":1515152261580153489}}]"#)?;
81+
process.exp_string(r#"[{"LogData":{"data":"0000000000000002","digest":"cd04a4754498e06db5a13c5f371f1f04ff6d2470f24aa9bd886540e5dce77f70","id":"0000000000000000000000000000000000000000000000000000000000000000","is":10368,"len":8,"pc":12672,"ptr":67107840,"ra":0,"rb":1515152261580153489}}]"#)?;
8282
process.process.exit()?;
8383
Ok(())
8484
}

sway-core/src/ir_generation/function.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1783,7 +1783,7 @@ impl<'eng> FnCompiler<'eng> {
17831783
);
17841784

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

17941794
let two = Constant::new_uint(context, 64, 2);
17951795
let two = Value::new_constant(context, two);
1796-
let new_cap =
1796+
let new_cap_part =
17971797
s.current_block
17981798
.append(context)
17991799
.binary_op(BinaryOpKind::Mul, cap, two);
1800-
1800+
let new_cap = s.current_block.append(context).binary_op(
1801+
BinaryOpKind::Add,
1802+
new_cap_part,
1803+
needed_size,
1804+
);
18011805
let new_ptr = s.current_block.append(context).asm_block(
18021806
vec![
18031807
AsmArg {

sway-lib-core/src/codec.sw

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5156,6 +5156,15 @@ fn to_slice<T>(array: T) -> raw_slice {
51565156
raw_slice::from_parts::<u8>(__addr_of(array), len)
51575157
}
51585158

5159+
fn assert_ge<T>(a: T, b: T, revert_code: u64)
5160+
where
5161+
T: Ord,
5162+
{
5163+
if a.lt(b) {
5164+
__revert(revert_code)
5165+
}
5166+
}
5167+
51595168
fn assert_eq<T>(a: T, b: T, revert_code: u64)
51605169
where
51615170
T: Eq,
@@ -5205,7 +5214,7 @@ where
52055214
// Append another item
52065215
let buffer = value_to_append.abi_encode(buffer);
52075216
assert_neq(ptr1, buffer.buffer.0, 4); // must have allocated new buffer
5208-
assert_eq(buffer.buffer.1, size_of_t * 2, 5); // capacity for two items
5217+
assert_ge(buffer.buffer.1, size_of_t * 2, 5); // capacity for at least two items
52095218
assert_eq(buffer.buffer.2, size_of_t * 2, 6); // buffer has two items
52105219

52115220
// Check that red zones were not overwritten

test/src/e2e_vm_tests/test_programs/should_pass/language/configurable_dedup_decode/stdout.snap

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
source: test/tests/tests.rs
3-
assertion_line: 99
3+
assertion_line: 115
44
---
55
> forc build --path test/src/e2e_vm_tests/test_programs/should_pass/language/configurable_dedup_decode --release --ir final
66
exit status: 0
@@ -207,24 +207,25 @@ script {
207207
encode_10_abi_encode_11_block1():
208208
v81 = const u64 2
209209
v82 = mul v33, v81, !116
210-
v83 = asm(new_cap: v82, old_ptr: v30, len: v36) -> ptr u8 hp, !117 {
210+
v83 = add v82, v37, !117
211+
v84 = asm(new_cap: v83, old_ptr: v30, len: v36) -> ptr u8 hp, !118 {
211212
aloc new_cap
212213
mcp hp old_ptr len
213214
}
214-
br encode_10_abi_encode_11_block0(v83, v82), !118
215+
br encode_10_abi_encode_11_block0(v84, v83), !119
215216
}
216217

217-
entry_orig fn main_8() -> u64, !121 {
218+
entry_orig fn main_8() -> u64, !122 {
218219
entry():
219-
v0 = get_config ptr { u64 }, WRAPPED, !122
220+
v0 = get_config ptr { u64 }, WRAPPED, !123
220221
v1 = const u64 0
221-
v2 = get_elem_ptr v0, ptr u64, v1, !123
222+
v2 = get_elem_ptr v0, ptr u64, v1, !124
222223
v3 = load v2
223-
v4 = get_config ptr { u64 }, TUPLE, !124
224+
v4 = get_config ptr { u64 }, TUPLE, !125
224225
v5 = const u64 0
225-
v6 = get_elem_ptr v4, ptr u64, v5, !125
226+
v6 = get_elem_ptr v4, ptr u64, v5, !126
226227
v7 = load v6
227-
v8 = add v3, v7, !128
228+
v8 = add v3, v7, !129
228229
ret u64 v8
229230
}
230231
}
@@ -348,15 +349,16 @@ script {
348349
!116 = (!49 !50 !66 !67)
349350
!117 = (!49 !50 !66 !67)
350351
!118 = (!49 !50 !66 !67)
351-
!119 = span !0 202 246
352-
!120 = fn_name_span !0 205 209
353-
!121 = (!119 !120)
354-
!122 = span !0 225 232
355-
!123 = span !0 30 36
356-
!124 = span !0 237 242
357-
!125 = span !0 243 244
358-
!126 = span !0 225 244
359-
!127 = fn_call_path_span !0 235 236
360-
!128 = (!126 !127)
352+
!119 = (!49 !50 !66 !67)
353+
!120 = span !0 202 246
354+
!121 = fn_name_span !0 205 209
355+
!122 = (!120 !121)
356+
!123 = span !0 225 232
357+
!124 = span !0 30 36
358+
!125 = span !0 237 242
359+
!126 = span !0 243 244
360+
!127 = span !0 225 244
361+
!128 = fn_call_path_span !0 235 236
362+
!129 = (!127 !128)
361363

362-
Finished release [optimized + fuel] target(s) [744 B] in ???
364+
Finished release [optimized + fuel] target(s) [752 B] in ???

test/src/e2e_vm_tests/test_programs/should_pass/language/u256/u256_abi/json_abi_oracle_new_encoding.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
{
1010
"concreteTypeId": "1b5759d94094368cfd443019e7ca5ec4074300e544e5ea993a979f5da627261e",
1111
"name": "SOME_U256",
12-
"offset": 864
12+
"offset": 872
1313
}
1414
],
1515
"encodingVersion": "1",

test/src/e2e_vm_tests/test_programs/should_pass/require_contract_deployment/array_of_structs_caller/src/main.sw

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::hash::*;
66
#[cfg(experimental_new_encoding = false)]
77
const CONTRACT_ID = 0x14ed3cd06c2947248f69d54bfa681fe40d26267be84df7e19e253622b7921bbe;
88
#[cfg(experimental_new_encoding = true)]
9-
const CONTRACT_ID = 0x91c3e72c3b3f4bf7bd5c548d8752c766d5fb8ebf15be3cf92ff34682da9bfb4d; // AUTO-CONTRACT-ID ../../test_contracts/array_of_structs_contract --release
9+
const CONTRACT_ID = 0x6b01f04c84ce955e8dfc6ed3611fd2518424ab757e5a540878f49fbd1bdec571; // AUTO-CONTRACT-ID ../../test_contracts/array_of_structs_contract --release
1010

1111
fn get_address() -> Option<std::address::Address> {
1212
Some(CONTRACT_ID.into())

test/src/e2e_vm_tests/test_programs/should_pass/require_contract_deployment/asset_ops_test/src/main.sw

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const FUEL_COIN_CONTRACT_ID = 0x19c0d374734bd8a92b776787e9dffa0f105a90e3c977626f
1414
#[cfg(experimental_new_encoding = false)]
1515
const BALANCE_CONTRACT_ID = 0xf6cd545152ac83225e8e7df2efb5c6fa6e37bc9b9e977b5ea8103d28668925df;
1616
#[cfg(experimental_new_encoding = true)]
17-
const BALANCE_CONTRACT_ID = 0xc0ae93ca2a6ec1c740a4869a3f5bfe061184e30e4e5c6185891d3ec2dba9a33d; // AUTO-CONTRACT-ID ../../test_contracts/balance_test_contract --release
17+
const BALANCE_CONTRACT_ID = 0xb770fa56f665d6fbdbdceecce21b7b61878f650981ac7f21c052613015937034; // AUTO-CONTRACT-ID ../../test_contracts/balance_test_contract --release
1818

1919
fn main() -> bool {
2020
let default_gas = 1_000_000_000_000;

test/src/e2e_vm_tests/test_programs/should_pass/require_contract_deployment/bal_opcode/src/main.sw

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use balance_test_abi::BalanceTest;
55
#[cfg(experimental_new_encoding = false)]
66
const CONTRACT_ID = 0xf6cd545152ac83225e8e7df2efb5c6fa6e37bc9b9e977b5ea8103d28668925df;
77
#[cfg(experimental_new_encoding = true)]
8-
const CONTRACT_ID = 0xc0ae93ca2a6ec1c740a4869a3f5bfe061184e30e4e5c6185891d3ec2dba9a33d; // AUTO-CONTRACT-ID ../../test_contracts/balance_test_contract --release
8+
const CONTRACT_ID = 0xb770fa56f665d6fbdbdceecce21b7b61878f650981ac7f21c052613015937034; // AUTO-CONTRACT-ID ../../test_contracts/balance_test_contract --release
99

1010
fn main() -> bool {
1111
let balance_test_contract = abi(BalanceTest, CONTRACT_ID);

test/src/e2e_vm_tests/test_programs/should_pass/require_contract_deployment/call_abi_with_tuples/src/main.sw

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use abi_with_tuples::{MyContract, Location, Person};
66
#[cfg(experimental_new_encoding = false)]
77
const CONTRACT_ID = 0xfdc14550c8aee742cd556d0ab7f378b7be0d3b1e6e086c097352e94590d4ed02;
88
#[cfg(experimental_new_encoding = true)]
9-
const CONTRACT_ID = 0xeb1d43bccc97620c80599c25722b1be3a22affd0570cfdcc57deac7c729e5389; // AUTO-CONTRACT-ID ../../test_contracts/abi_with_tuples_contract --release
9+
const CONTRACT_ID = 0xcc679c89d2950879d4f8fb3b99770f93dfde3335fff574cbf0588691fbcefde3; // AUTO-CONTRACT-ID ../../test_contracts/abi_with_tuples_contract --release
1010

1111
fn main() -> bool {
1212
let the_abi = abi(MyContract, CONTRACT_ID);

0 commit comments

Comments
 (0)