hyperledger / solang

Solidity Compiler for Solana and Polkadot
https://solang.readthedocs.io/
Apache License 2.0
1.22k stars 207 forks source link

Bug: Invalid codegen for structs #1618

Closed xermicus closed 4 months ago

xermicus commented 5 months ago

Found a case where Solang (latest commit on main) generates invalid code, failing an LLVM assertion. Code is from here but I extracted a minimal reproducer below which is enough to trigger the bug.

$ solang compile --target polkadot --release reproducer.sol

thread 'main' panicked at /home/glow/.cargo/registry/src/index.crates.io-6f17d22bba15001f/inkwell-0.2.0/src/values/enums.rs:286:13:
Found PointerValue(PointerValue { ptr_value: Value { name: "struct member14", address: 0x5c58cccf2980, is_const: false, is_null: false, is_undef: false, llvm_value: "  %\"struct member14\" = getelementptr inbounds { ptr, i256, i256, i256 }, ptr %0, i32 0, i32 3", llvm_type: "ptr" } }) but expected the IntValue variant

Minimal reproducer

contract Bug {
    struct State {
        bytes input;
        uint256 incnt;
        uint256 bitbuf;
        uint256 bitcnt;
    }

    function bits(State memory s) public pure {
        uint256 val = s.bitbuf;
        while (s.bitcnt < 1) {
            val |= uint256(uint8(s.input[s.incnt++])) << s.bitcnt;
        }

        s.bitbuf = val >> 1;
    }
}
yp945 commented 4 months ago

I submitted a PR to fix this issue, looking forward to your review. 😄

xermicus commented 4 months ago

@yp945 thanks for the fix