Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

memory allocation bugs #731

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions crates/codegen/src/yul/runtime/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub(super) fn make_create(
let func = function_definition! {
function [func_name.ident()]([value.ident()]) -> addr {
(let [size.ident()] := datasize([contract_symbol.clone()]))
(let mem_ptr := [provider.avail(db)])
(let mem_ptr := [provider.alloc(db, size.expr())])
(let contract_ptr := dataoffset([contract_symbol]))
(datacopy(mem_ptr, contract_ptr, [size.expr()]))
(addr := create([value.expr()], mem_ptr, [size.expr()]))
Expand All @@ -52,7 +52,7 @@ pub(super) fn make_create2(
let func = function_definition! {
function [func_name.ident()]([value.ident()], salt) -> addr {
(let [size.ident()] := datasize([contract_symbol.clone()]))
(let mem_ptr := [provider.avail(db)])
(let mem_ptr := [provider.alloc(db, size.expr())])
(let contract_ptr := dataoffset([contract_symbol]))
(datacopy(mem_ptr, contract_ptr, [size.expr()]))
(addr := create2([value.expr()], mem_ptr, [size.expr()], salt))
Expand Down
17 changes: 17 additions & 0 deletions crates/test-files/fixtures/regression/call_contract_mem.fe
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use std::context::Context

contract Foo {
pub fn add(_ x: u256, _ y: u256) -> u256 {
return x + y
}
}

contract FooFactory {
pub fn call_foo(ctx: Context) -> u256 {
let foo: Foo = Foo.create(ctx, 0)
let sum: u256 = foo.add(42, 26)
let my_array: Array<u8, 42>
assert my_array[0] == 0
return sum
}
}
17 changes: 17 additions & 0 deletions crates/test-files/fixtures/regression/create_contract_mem.fe
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use std::context::Context

contract Foo {
pub fn get_my_num() -> u256 {
return 42
}
}

contract FooFactory {
pub fn create_foo(ctx: Context) -> address {
# create was not allocating memory before copying code data
let foo: Foo = Foo.create(ctx, 0)
let my_array: Array<u8, 42>
assert my_array[0] == 0
return address(foo)
}
}
2 changes: 2 additions & 0 deletions crates/tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ mod features;
#[cfg(test)]
mod ingots;
#[cfg(test)]
mod regression;
#[cfg(test)]
mod solidity;
#[cfg(test)]
mod stress;
57 changes: 57 additions & 0 deletions crates/tests/src/regression.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
//! Regression tests for bugs that we find

#![cfg(feature = "solc-backend")]
use insta::assert_snapshot;
use primitive_types::H160;

use fe_compiler_test_utils::*;
use fe_compiler_test_utils::{self as test_utils};

pub fn deploy_contract(
executor: &mut Executor,
fixture: &str,
contract_name: &str,
init_params: &[ethabi::Token],
) -> ContractHarness {
test_utils::deploy_contract(
executor,
&format!("regression/{}", fixture),
contract_name,
init_params,
)
}

pub fn load_contract(address: H160, fixture: &str, contract_name: &str) -> ContractHarness {
test_utils::load_contract(address, &format!("regression/{}", fixture), contract_name)
}

#[test]
fn create_contract_mem() {
with_executor(&|mut executor| {
let factory_harness =
deploy_contract(&mut executor, "create_contract_mem.fe", "FooFactory", &[]);

let foo_address = factory_harness
.call_function(&mut executor, "create_foo", &[])
.expect("factory did not return an address")
.into_address()
.expect("not an address");

let foo_harness = load_contract(foo_address, "create_contract_mem.fe", "Foo");

foo_harness.test_function(&mut executor, "get_my_num", &[], Some(&uint_token(42)));

assert_harness_gas_report!(factory_harness);
})
}

#[test]
fn call_contract_mem() {
with_executor(&|mut executor| {
let factory_harness =
deploy_contract(&mut executor, "call_contract_mem.fe", "FooFactory", &[]);
factory_harness.test_function(&mut executor, "call_foo", &[], Some(&uint_token(68)));

assert_harness_gas_report!(factory_harness);
})
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
source: crates/tests/src/regression.rs
expression: "format!(\"{}\", factory_harness.gas_reporter)"

---
create_foo([]) used 40599 gas