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

Use add!, sub!, mul! for overflow flag setting #491

Open
akiramenai opened this issue Apr 22, 2024 · 1 comment
Open

Use add!, sub!, mul! for overflow flag setting #491

akiramenai opened this issue Apr 22, 2024 · 1 comment

Comments

@akiramenai
Copy link
Collaborator

akiramenai commented Apr 22, 2024

The goal is to fix and restore the reverted patches:

The patches introduce selection DAG patterns that fold uaddo, umulo, usubo into add!, mul!, sub! correspondingly. However, as it explained in the bug report, and another one [change to public links] this selection is incorrect as the original nodes create flags that persist while EraVM instructions set flags that can be overwritten. The rest of the logic in original patches could be kept as it is modulo to porting to LLVM 17.

== Bug reproducers ==

  1. cargo run --verbose --release --bin compiler-tester -- --path tests/solidity/simple/modular/addmod_complex.sol --mode='Y+M*B* 0.8.21' --load-system-contracts system-contracts-stable-build
  2. Add the test below as compiler-tester/tests/solidity/simple/bug.sol:
//! { "cases": [ {
//!     "name": "default",
//!     "inputs": [
//!         {
//!             "method": "balanceAtTime",
//!             "calldata": [
//!                 "1702053600",
//!                 "1702053600",
//!                 "100",
//!                 "1",
//!                 "1",
//!                 "1702053600",
//!                 "1702053610"
//!             ]
//!         }
//!     ],
//!     "expected": [
//!         "10",
//!         "90",
//!         "1702053610"
//!     ]
//! } ] }

// SPDX-License-Identifier: BUSL-1.1

pragma solidity >=0.8.17;

contract Test {
    function min(uint256 a, uint256 b) public pure returns (uint256 _min) {
        _min = (a <= b) ? a : b;
    }

    function balanceAtTime(
        uint256 start,
        uint256 cliffDate,
        uint256 amount,
        uint256 rate,
        uint256 period,
        uint256 currentTime,
        uint256 redemptionTime
    )
        public
        pure
        returns (
            uint256 unlockedBalance,
            uint256 lockedBalance,
            uint256 unlockTime
        )
    {
        if (
            start > currentTime ||
            cliffDate > currentTime ||
            redemptionTime <= start
        ) {
            lockedBalance = amount;
            unlockTime = start;
        } else {
            uint256 periodsElapsed = (redemptionTime - start) / period;
            uint256 calculatedBalance = periodsElapsed * rate;
            unlockedBalance = min(calculatedBalance, amount);
            lockedBalance = amount - unlockedBalance;
            unlockTime = start + (period * periodsElapsed);
        }
    }
}

cargo run --verbose --release --bin compiler-tester -- --path tests/solidity/simple/bug.sol --mode='Y+M*B* 0.8.23'

@vladimirradosavljevic
Copy link
Contributor

The rearrangeOverflowHandlingBranches function implementation from the commit f31aa85 should be moved from the EraVMCodegenPrepare pass to the newly introduced EraVMPostCodegenPrepare pass (#493). This is because CodeGenPrepare can generate overflow intrinsics, and EraVMPostCodegenPrepare is executed immediately after that pass.

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

No branches or pull requests

3 participants