Skip to content

Commit

Permalink
test(e2e): fix memory corruption test (#2378)
Browse files Browse the repository at this point in the history
  • Loading branch information
yonadaa authored Mar 11, 2024
1 parent 5debcca commit 06ac603
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 37 deletions.
2 changes: 1 addition & 1 deletion e2e/packages/contracts/mud.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export default mudConfig({
},
StaticArray: {
keySchema: {},
valueSchema: "uint256[2]",
valueSchema: "uint256[3]",
},
},
});
58 changes: 29 additions & 29 deletions e2e/packages/contracts/src/codegen/tables/StaticArray.sol

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

59 changes: 52 additions & 7 deletions e2e/packages/contracts/test/StaticArray.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,69 @@ pragma solidity >=0.8.24;

import { MudTest } from "@latticexyz/world/test/MudTest.t.sol";

import { toStaticArray_uint256_2 } from "../src/codegen/tables/StaticArray.sol";
import { toStaticArray_uint256_3 } from "../src/codegen/tables/StaticArray.sol";

contract StaticArrayTest is MudTest {
uint256 internal memoryCorruptionCheck = uint256(keccak256("memoryCorruptionCheck"));
uint256 constant memoryCorruptionCheck = uint256(keccak256("memoryCorruptionCheck"));

function testMemoryCorruption() public {
contract StaticArrayTest is MudTest {
/*
* Test that the data is correctly copied when the dynamic and static arrays are the same length
*/
function testMemoryCorruptionSameLength() public {
uint256[] memory data = new uint256[](3);
data[0] = 1;
data[1] = 2;
data[2] = memoryCorruptionCheck;

uint256[2] memory result = toStaticArray_uint256_2(data);
assertEq(result[0], 0);
assertEq(result[1], 0);
uint256[3] memory result = toStaticArray_uint256_3(data);
assertEq(result[0], 1);
assertEq(result[1], 2);

uint256 memoryAfterResult;
assembly {
memoryAfterResult := mload(add(result, 0x40))
mstore(0x40, add(mload(0x40), 0x40))
}
assertEq(memoryAfterResult, memoryCorruptionCheck);
}

/*
* Test that the data is correctly copied when the dynamic array is longer
*/
function testMemoryCorruptionLongerDynamic() public {
uint256[] memory data = new uint256[](4);
data[0] = 1;
data[1] = 2;
data[2] = 3;
data[3] = memoryCorruptionCheck;

uint256[3] memory result = toStaticArray_uint256_3(data);
assertEq(result[0], 1);
assertEq(result[1], 2);
assertEq(result[2], 3);

uint256 memoryAfterResult;
assembly {
memoryAfterResult := mload(add(result, 0x60))
mstore(0x40, add(mload(0x40), 0x30))
}
assertEq(memoryAfterResult, memoryCorruptionCheck);
}

/*
* Test that an uninitialized array is returned when the dynamic array is shorter
*/
function testMemoryCorruptionShorterDynamic() public {
uint256[] memory data = new uint256[](2);
data[0] = 1;
data[1] = memoryCorruptionCheck;

uint256[3] memory result = toStaticArray_uint256_3(data);
assertEq(result[0], 0);

uint256 memoryAfterResult;
assembly {
memoryAfterResult := mload(add(result, 0x20))
mstore(0x40, add(mload(0x40), 0x20))
}
assertNotEq(memoryAfterResult, memoryCorruptionCheck);
Expand Down

0 comments on commit 06ac603

Please sign in to comment.