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

Unsafe type conversions with the IR codegen #15142

Open
hedgar2017 opened this issue May 23, 2024 · 1 comment
Open

Unsafe type conversions with the IR codegen #15142

hedgar2017 opened this issue May 23, 2024 · 1 comment
Labels

Comments

@hedgar2017
Copy link

Description

With legacy codegen, when uint40 is returned from a function, the value is truncated. In the code below, if a value larger than uint40 is passed to getNumber, it is truncated to 40 bits. This means that the results of getNumber(0) and getNumber(2**40) are the same.

With viaIR codegen, the value is not truncated and some garbage remains in memory. Thus, the actual index accessed in the numbers array is not correct, so executing getNumber(2**40) results in an 'Array index out of bound' panic error.

Environment

  • Compiler version: 0.8.25
  • Target EVM version (as per compiler settings): default
  • Operating system: MacOS 14 Sonoma

Steps to Reproduce

contract Foo {
    uint256[] internal numbers;

    function addNumber(uint256 number) public {
        numbers.push(number);
    }

    function getNumber(uint256 index) public view returns (uint256) {
        return numbers[_convertToUint40(index)];
    }

    function _convertToUint40(uint256 n) internal pure returns (uint40 result) {
        assembly {
            result := n
        }
    }
}
@sallywang147
Copy link

Glad someone brought this up too. I identified similar issues. On Mac processors, the recasting is assigning uint40(38) to uint256(2**40). This does seem like a coedGenerator bug to me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants