From 2e27b2d0f7e162e60c98f131044c96aa18ad992e Mon Sep 17 00:00:00 2001 From: Marcos Henrich Date: Tue, 6 Aug 2024 10:43:13 +0100 Subject: [PATCH] Fixes crash on encode_buffer_append with wrong args. When encode_buffer_append was called with wrong number of args an array OOB panick was thrown. With this fix we throw a CompileError::IntrinsicIncorrectNumArgs in case the intrinsic encode_buffer_append is called with more or less than the expected 2 arguments. Fixes #6337 --- .../ast_node/expression/intrinsic_function.rs | 8 ++++++++ .../encode_append_wrong_args/Forc.lock | 3 +++ .../encode_append_wrong_args/Forc.toml | 6 ++++++ .../json_abi_oracle.json | 1 + .../encode_append_wrong_args/src/main.sw | 17 +++++++++++++++++ .../encode_append_wrong_args/test.toml | 7 +++++++ 6 files changed, 42 insertions(+) create mode 100644 test/src/e2e_vm_tests/test_programs/should_fail/encode_append_wrong_args/Forc.lock create mode 100644 test/src/e2e_vm_tests/test_programs/should_fail/encode_append_wrong_args/Forc.toml create mode 100644 test/src/e2e_vm_tests/test_programs/should_fail/encode_append_wrong_args/json_abi_oracle.json create mode 100644 test/src/e2e_vm_tests/test_programs/should_fail/encode_append_wrong_args/src/main.sw create mode 100644 test/src/e2e_vm_tests/test_programs/should_fail/encode_append_wrong_args/test.toml diff --git a/sway-core/src/semantic_analysis/ast_node/expression/intrinsic_function.rs b/sway-core/src/semantic_analysis/ast_node/expression/intrinsic_function.rs index 9c750722f07..b1bfc77bbd2 100644 --- a/sway-core/src/semantic_analysis/ast_node/expression/intrinsic_function.rs +++ b/sway-core/src/semantic_analysis/ast_node/expression/intrinsic_function.rs @@ -224,6 +224,14 @@ fn type_check_encode_append( _type_arguments: &[TypeArgument], span: Span, ) -> Result<(ty::TyIntrinsicFunctionKind, TypeId), ErrorEmitted> { + if arguments.len() != 2 { + return Err(handler.emit_err(CompileError::IntrinsicIncorrectNumArgs { + name: kind.to_string(), + expected: 2, + span, + })); + } + let type_engine = ctx.engines.te(); let engines = ctx.engines(); diff --git a/test/src/e2e_vm_tests/test_programs/should_fail/encode_append_wrong_args/Forc.lock b/test/src/e2e_vm_tests/test_programs/should_fail/encode_append_wrong_args/Forc.lock new file mode 100644 index 00000000000..976b79b97d6 --- /dev/null +++ b/test/src/e2e_vm_tests/test_programs/should_fail/encode_append_wrong_args/Forc.lock @@ -0,0 +1,3 @@ +[[package]] +name = "encode_append_wrong_args" +source = "member" diff --git a/test/src/e2e_vm_tests/test_programs/should_fail/encode_append_wrong_args/Forc.toml b/test/src/e2e_vm_tests/test_programs/should_fail/encode_append_wrong_args/Forc.toml new file mode 100644 index 00000000000..080346ed9b0 --- /dev/null +++ b/test/src/e2e_vm_tests/test_programs/should_fail/encode_append_wrong_args/Forc.toml @@ -0,0 +1,6 @@ +[project] +authors = ["Fuel Labs "] +entry = "main.sw" +license = "Apache-2.0" +name = "encode_append_wrong_args" +implicit-std = false diff --git a/test/src/e2e_vm_tests/test_programs/should_fail/encode_append_wrong_args/json_abi_oracle.json b/test/src/e2e_vm_tests/test_programs/should_fail/encode_append_wrong_args/json_abi_oracle.json new file mode 100644 index 00000000000..0637a088a01 --- /dev/null +++ b/test/src/e2e_vm_tests/test_programs/should_fail/encode_append_wrong_args/json_abi_oracle.json @@ -0,0 +1 @@ +[] \ No newline at end of file diff --git a/test/src/e2e_vm_tests/test_programs/should_fail/encode_append_wrong_args/src/main.sw b/test/src/e2e_vm_tests/test_programs/should_fail/encode_append_wrong_args/src/main.sw new file mode 100644 index 00000000000..b5813db61d8 --- /dev/null +++ b/test/src/e2e_vm_tests/test_programs/should_fail/encode_append_wrong_args/src/main.sw @@ -0,0 +1,17 @@ +library; + +pub struct Buffer { + buffer: u64 +} + +pub trait T { + fn ar(buffer: Buffer) -> Buffer; +} + +impl T for str[10] { + fn ar(buffer: Buffer) -> Buffer { + Buffer { + buffer: __encode_buffer_append(buffer.buffer) + } + } +} \ No newline at end of file diff --git a/test/src/e2e_vm_tests/test_programs/should_fail/encode_append_wrong_args/test.toml b/test/src/e2e_vm_tests/test_programs/should_fail/encode_append_wrong_args/test.toml new file mode 100644 index 00000000000..7c6ea4d2bde --- /dev/null +++ b/test/src/e2e_vm_tests/test_programs/should_fail/encode_append_wrong_args/test.toml @@ -0,0 +1,7 @@ +category = "fail" + +# check: $()warning +# check: $()buffer: __encode_buffer_append(buffer.buffer) + +# check: $()buffer: __encode_buffer_append(buffer.buffer) +# nextln: $()Call to "encode_buffer_append" expects 2 arguments