Compare commits

..

1 Commits

Author SHA1 Message Date
Cyrill Leutwiler c04c2560ac wip
Signed-off-by: Cyrill Leutwiler <bigcyrill@hotmail.com>
2024-09-06 09:55:43 +02:00
100 changed files with 2682 additions and 4221 deletions
-38
View File
@@ -1,38 +0,0 @@
name: Build revive-debian
on:
workflow_dispatch:
env:
REVIVE_DEBIAN_PACKAGE: revive-debian-x86
DEBIAN_CONTAINER: revive-builder-debian-x86
DEBIAN_CONTAINER_BUILDER: build-debian-builder.sh
DEBIAN_CONTAINER_RUNNER: run-debian-builder.sh
REVIVE_DEBIAN_INSTALL: ${{ github.workspace }}/target/release
REVIVE_DEBIAN_BINARY: resolc
RUST_VERSION: "1.80"
jobs:
build-revive-debian-x86:
name: debian-container-x86
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: build-container
run: |
(cd utils && ./${{ env.DEBIAN_CONTAINER_BUILDER}} --build-arg RUST_VERSION=${{ env.RUST_VERSION}} . )
- name: build-revive-debian
run: |
rustup show
cargo --version
rustup +nightly show
cargo +nightly --version
bash --version
utils/${{ env.DEBIAN_CONTAINER_RUNNER }} utils/build-revive.sh -o ${{ env.REVIVE_DEBIAN_INSTALL}}
- uses: actions/upload-artifact@v4
with:
name: ${{ env.REVIVE_DEBIAN_PACKAGE }}
path: ${{ env.REVIVE_DEBIAN_INSTALL }}/${{ env.REVIVE_DEBIAN_BINARY }}
retention-days: 1
+1 -1
View File
@@ -19,7 +19,7 @@ jobs:
- name: Install solc - name: Install solc
run: | run: |
mkdir -p solc mkdir -p solc
curl -sSL --output solc/solc https://github.com/ethereum/solidity/releases/download/v0.8.28/solc-static-linux curl -sSL --output solc/solc https://github.com/ethereum/solidity/releases/download/v0.8.26/solc-static-linux
chmod +x solc/solc chmod +x solc/solc
echo "$(pwd)/solc/" >> $GITHUB_PATH echo "$(pwd)/solc/" >> $GITHUB_PATH
-28
View File
@@ -1,28 +0,0 @@
# Changelog
## Unreleased
## v0.1.0-dev.5
This is development pre-release.
# Added
- Implement the `CODESIZE` and `EXTCODESIZE` opcodes.
# Changed
- Include the full revive version in the contract metadata.
# Fixed
## v0.1.0-dev-4
This is development pre-release.
# Added
- Support the `ORIGIN` opcode.
# Changed
- Update polkavm to `v0.14.0`.
- Enable the `a`, `fast-unaligned-access` and `xtheadcondmov` LLVM target features, decreasing the code size for some contracts.
# Fixed
Generated
+926 -2098
View File
File diff suppressed because it is too large Load Diff
+20 -22
View File
@@ -3,29 +3,28 @@ resolver = "2"
members = ["crates/*"] members = ["crates/*"]
[workspace.package] [workspace.package]
version = "0.1.0-dev.5" version = "0.1.0"
authors = [ authors = [
"Cyrill Leutwiler <cyrill@parity.io>", "Cyrill Leutwiler <cyrill@parity.io>",
"Parity Technologies <admin@parity.io>", "Parity Technologies <admin@parity.io>",
] ]
license = "MIT/Apache-2.0" license = "MIT/Apache-2.0"
edition = "2021" edition = "2021"
repository = "https://github.com/paritytech/revive" repository = "https://github.com/xermicus/revive"
rust-version = "1.80.0"
[workspace.dependencies] [workspace.dependencies]
revive-benchmarks = { version = "0.1.0-dev.5", path = "crates/benchmarks" } revive-benchmarks = { version = "0.1.0", path = "crates/benchmarks" }
revive-builtins = { version = "0.1.0-dev.5", path = "crates/builtins" } revive-builtins = { version = "0.1.0", path = "crates/builtins" }
revive-common = { version = "0.1.0-dev.5", path = "crates/common" } revive-common = { version = "0.1.0", path = "crates/common" }
revive-differential = { version = "0.1.0-dev.5", path = "crates/differential" } revive-differential = { version = "0.1.0", path = "crates/differential" }
revive-integration = { version = "0.1.0-dev.5", path = "crates/integration" } revive-integration = { version = "0.1.0", path = "crates/integration" }
revive-linker = { version = "0.1.0-dev.5", path = "crates/linker" } revive-linker = { version = "0.1.0", path = "crates/linker" }
lld-sys = { version = "0.1.0-dev.5", path = "crates/lld-sys" } lld-sys = { version = "0.1.0", path = "crates/lld-sys" }
revive-llvm-context = { version = "0.1.0-dev.5", path = "crates/llvm-context" } revive-llvm-context = { version = "0.1.0", path = "crates/llvm-context" }
revive-runtime-api = { version = "0.1.0-dev.5", path = "crates/runtime-api" } revive-runtime-api = { version = "0.1.0", path = "crates/runtime-api" }
revive-runner = { version = "0.1.0-dev.5", path = "crates/runner" } revive-runner = { version = "0.1.0", path = "crates/runner" }
revive-solidity = { version = "0.1.0-dev.5", path = "crates/solidity" } revive-solidity = { version = "0.1.0", path = "crates/solidity" }
revive-stdlib = { version = "0.1.0-dev.5", path = "crates/stdlib" } revive-stdlib = { version = "0.1.0", path = "crates/stdlib" }
hex = "0.4" hex = "0.4"
petgraph = "0.6" petgraph = "0.6"
@@ -51,10 +50,10 @@ path-slash = "0.2"
rayon = "1.8" rayon = "1.8"
clap = { version = "4", default-features = false, features = ["derive"] } clap = { version = "4", default-features = false, features = ["derive"] }
rand = "0.8" rand = "0.8"
polkavm-common = "0.14" polkavm-common = "0.10"
polkavm-linker = "0.14" polkavm-linker = "0.10"
polkavm-disassembler = "0.14" polkavm-disassembler = "0.10"
polkavm = "0.14" polkavm = "0.10"
alloy-primitives = { version = "0.8", features = ["serde"] } alloy-primitives = { version = "0.8", features = ["serde"] }
alloy-sol-types = "0.8" alloy-sol-types = "0.8"
alloy-genesis = "0.3" alloy-genesis = "0.3"
@@ -67,12 +66,11 @@ log = { version = "0.4" }
# polkadot-sdk and friends # polkadot-sdk and friends
codec = { version = "3.6.12", default-features = false, package = "parity-scale-codec" } codec = { version = "3.6.12", default-features = false, package = "parity-scale-codec" }
scale-info = { version = "2.11.1", default-features = false } scale-info = { version = "2.11.1", default-features = false }
polkadot-sdk = { git = "https://github.com/paritytech/polkadot-sdk", rev = "2b6b69641ccff4d7aa9c32051bbb2f1e775ef8cc" } polkadot-sdk = { git = "https://github.com/paritytech/polkadot-sdk", rev = "d7b575338b2c647e04fc48bfbe00ea8f492fb580" }
# llvm # llvm
[workspace.dependencies.inkwell] [workspace.dependencies.inkwell]
git = "https://github.com/TheDan64/inkwell.git" version = "0.5"
rev = "7b410298b6a93450adaa90b1841d5805a3038f12"
default-features = false default-features = false
features = ["serde", "llvm18-0", "no-libffi-linking", "target-riscv"] features = ["serde", "llvm18-0", "no-libffi-linking", "target-riscv"]
+4 -10
View File
@@ -8,20 +8,11 @@ install-bin:
install-npm: install-npm:
npm install && npm fund npm install && npm fund
# install-revive: Build and install to the directory specified in REVIVE_INSTALL_DIR
ifeq ($(origin REVIVE_INSTALL_DIR), undefined)
REVIVE_INSTALL_DIR=`pwd`/release/revive-debian
endif
install-revive:
cargo install --path crates/solidity --root $(REVIVE_INSTALL_DIR)
format: format:
cargo fmt --all --check cargo fmt --all --check
clippy:
cargo clippy --all-features --workspace --tests --benches -- --deny warnings --allow dead_code
test: format clippy test-cli test-workspace test: format clippy test-cli test-workspace
cargo test --workspace
test-integration: install-bin test-integration: install-bin
cargo test --package revive-integration cargo test --package revive-integration
@@ -47,6 +38,9 @@ bench: install-bin
cargo criterion --all --all-features --message-format=json \ cargo criterion --all --all-features --message-format=json \
| criterion-table > crates/benchmarks/BENCHMARKS.md | criterion-table > crates/benchmarks/BENCHMARKS.md
clippy:
cargo clippy --all-features --workspace --tests --benches
docs: docs-build docs: docs-build
mdbook serve --open docs/ mdbook serve --open docs/
+1 -1
View File
@@ -1,4 +1,4 @@
![CI](https://github.com/paritytech/revive/actions/workflows/rust.yml/badge.svg) ![CI](https://github.com/xermicus/revive/actions/workflows/rust.yml/badge.svg)
# revive # revive
-11
View File
@@ -1,11 +0,0 @@
# Release checklist
Prior to the first stable release we neither have formal release processes nor do we follow a fixed release schedule.
To create a new pre-release:
1. Merge a release PR which updates the `-dev.X` versions in the workspace `Cargo.toml` and updates the `CHANGELOG.md` accordingly
2. Push a release tag to `main`
3. Manually trigger the `Build revive-debian` action
4. Create a __pre-release__ from the tag and manually upload the build artifact generated by the action
5. Update the [contract-docs](https://github.com/paritytech/contract-docs/) accordingly
+1 -1
View File
@@ -20,7 +20,7 @@ pub const BIT_LENGTH_ETH_ADDRESS: usize =
pub const BIT_LENGTH_WORD: usize = crate::byte_length::BYTE_LENGTH_WORD * BIT_LENGTH_BYTE; pub const BIT_LENGTH_WORD: usize = crate::byte_length::BYTE_LENGTH_WORD * BIT_LENGTH_BYTE;
/// Bit length of the runtime value type. /// Bit length of the runtime value type.
pub const BIT_LENGTH_VALUE: usize = BIT_LENGTH_WORD; pub const BIT_LENGTH_VALUE: usize = crate::byte_length::BYTE_LENGTH_VALUE * BIT_LENGTH_BYTE;
/// Bit length of thre runimte block number type. /// Bit length of thre runimte block number type.
pub const BIT_LENGTH_BLOCK_NUMBER: usize = pub const BIT_LENGTH_BLOCK_NUMBER: usize =
+6 -3
View File
@@ -6,12 +6,15 @@ pub const BYTE_LENGTH_BYTE: usize = 1;
/// The x86 word byte-length. /// The x86 word byte-length.
pub const BYTE_LENGTH_X32: usize = 4; pub const BYTE_LENGTH_X32: usize = 4;
/// Native stack alignment size in bytes
#[cfg(not(feature = "riscv-64"))]
pub const BYTE_LENGTH_STACK_ALIGN: usize = 4;
#[cfg(feature = "riscv-64")]
pub const BYTE_LENGTH_STACK_ALIGN: usize = 8;
/// The x86_64 word byte-length. /// The x86_64 word byte-length.
pub const BYTE_LENGTH_X64: usize = 8; pub const BYTE_LENGTH_X64: usize = 8;
/// EVM native stack alignment size in bytes
pub const BYTE_LENGTH_STACK_ALIGN: usize = 32;
/// The ETH address byte-length. /// The ETH address byte-length.
pub const BYTE_LENGTH_ETH_ADDRESS: usize = 20; pub const BYTE_LENGTH_ETH_ADDRESS: usize = 20;
+2 -12
View File
@@ -1,6 +1,6 @@
{ {
"config": { "config": {
"chainId": 420420420, "chainId": 1,
"homesteadBlock": 0, "homesteadBlock": 0,
"eip150Block": 0, "eip150Block": 0,
"eip155Block": 0, "eip155Block": 0,
@@ -26,15 +26,5 @@
"mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000", "mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000", "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"timestamp": "0x00", "timestamp": "0x00",
"alloc": { "alloc": {}
"0101010101010101010101010101010101010101": {
"balance": "1000000000"
},
"0202020202020202020202020202020202020202": {
"balance": "1000000000"
},
"0303030303030303030303030303030303030303": {
"balance": "1000000000"
}
}
} }
+4 -3
View File
@@ -40,7 +40,7 @@ const EXECUTABLE_ARGS_BENCH: [&str; 6] = [
"-", "-",
]; ];
const GAS_USED_MARKER: &str = "EVM gas used:"; const GAS_USED_MARKER: &str = "EVM gas used:";
const REVERT_MARKER: &str = " error: "; const REVERT_MARKER: &str = "error: execution reverted";
/// The geth EVM state dump structure /// The geth EVM state dump structure
#[derive(Clone, Debug, Default, Serialize, Deserialize)] #[derive(Clone, Debug, Default, Serialize, Deserialize)]
@@ -100,9 +100,10 @@ pub struct EvmOutput {
impl EvmOutput { impl EvmOutput {
/// Return if there was no error found. /// Return if there was no error found.
/// ///
/// Panics if the gas used is zero as this indicates nothing was run. /// Panics if the gas used is zero as this indicates nothing was run, i.e.
/// there was no receiving account but still no error is reported.
pub fn run_success(&self) -> bool { pub fn run_success(&self) -> bool {
assert_ne!(self.gas_used, U256::ZERO, "nothing was executed: {self:?}"); assert_ne!(self.gas_used, U256::ZERO, "nothing was executed");
self.error.is_none() self.error.is_none()
} }
} }
+8 -8
View File
@@ -1,10 +1,10 @@
{ {
"Baseline": 967, "Baseline": 878,
"Computation": 4022, "Computation": 4305,
"DivisionArithmetics": 31787, "DivisionArithmetics": 39774,
"ERC20": 44233, "ERC20": 53405,
"Events": 1743, "Events": 1693,
"FibonacciIterative": 2927, "FibonacciIterative": 2917,
"Flipper": 3408, "Flipper": 3570,
"SHA1": 26009 "SHA1": 32557
} }
+16 -20
View File
@@ -7,10 +7,10 @@ pragma solidity ^0.8;
"differential": true, "differential": true,
"actions": [ "actions": [
{ {
"Upload": { "Instantiate": {
"code": { "code": {
"Solidity": { "Solidity": {
"contract": "Callee" "contract": "Call"
} }
} }
} }
@@ -19,40 +19,36 @@ pragma solidity ^0.8;
"Instantiate": { "Instantiate": {
"code": { "code": {
"Solidity": { "Solidity": {
"contract": "Caller" "contract": "Call"
} }
}, }
"value": 123
} }
}, },
{ {
"Call": { "Call": {
"dest": { "dest": {
"Instantiated": 0 "Instantiated": 1
}, },
"data": "5a6535fc00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000004cafebabe00000000000000000000000000000000000000000000000000000000" "data": "1b8b921d0000000000000000000000001c81a61a407017c58397a47d2ab28191b9b8ec9b000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000050102030405000000000000000000000000000000000000000000000000000000"
} }
} }
] ]
} }
*/ */
contract Callee { contract Call {
function value_transfer(address payable destination) public payable {
destination.transfer(msg.value);
}
function echo(bytes memory payload) public pure returns (bytes memory) { function echo(bytes memory payload) public pure returns (bytes memory) {
return payload; return payload;
} }
receive() external payable {} function call(
} address callee,
bytes memory payload
contract Caller { ) public pure returns (bytes memory) {
constructor() payable { return Call(callee).echo(payload);
Callee callee = new Callee();
payable(address(callee)).transfer(msg.value);
}
function call(bytes memory payload) public returns (bytes memory) {
Callee callee = new Callee();
return callee.echo(payload);
} }
} }
-16
View File
@@ -30,14 +30,6 @@ pragma solidity ^0.8;
}, },
"data": "fc9c8d39" "data": "fc9c8d39"
} }
},
{
"Call": {
"dest": {
"Instantiated": 0
},
"data": "3af973b1"
}
} }
] ]
} }
@@ -51,12 +43,4 @@ contract Context {
function caller() public view returns (address ret) { function caller() public view returns (address ret) {
ret = msg.sender; ret = msg.sender;
} }
function chain_id() public view returns (uint) {
uint256 id;
assembly {
id := chainid()
}
return id;
}
} }
+6 -57
View File
@@ -1,64 +1,13 @@
// SPDX-License-Identifier: MIT // SPDX-License-Identifier: MIT
pragma solidity ^0.8; pragma solidity ^0.8.24;
/* runner.json
{
"differential": true,
"actions": [
{
"Upload": {
"code": {
"Solidity": {
"contract": "CreateA"
}
}
}
},
{
"Instantiate": {
"code": {
"Solidity": {
"contract": "CreateB"
}
}
}
},
{
"Call": {
"dest": {
"Instantiated": 0
},
"value": 10000
}
},
{
"Call": {
"dest": {
"Instantiated": 0
}
}
},
{
"Call": {
"dest": {
"Instantiated": 0
}
}
},
{
"Call": {
"dest": {
"Instantiated": 0
}
}
}
]
}
*/
contract CreateA { contract CreateA {
constructor() payable {} address creator;
constructor() payable {
creator = msg.sender;
}
} }
contract CreateB { contract CreateB {
+11 -13
View File
@@ -4,32 +4,30 @@ pragma solidity ^0.8.24;
/* runner.json /* runner.json
{ {
"differential": true, "actions": [
"actions": [
{ {
"Instantiate": { "Instantiate": {}
"code": {
"Solidity": {
"contract": "TestSha3"
}
}
}
}, },
{ {
"Call": { "Call": {
"dest": { "dest": {
"Instantiated": 0 "Instantiated": 0
}, },
"data": "f9fbd5540000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000c68656c6c6f20776f726c64210000000000000000000000000000000000000000" "data": "f9fbd5540000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000c68656c6c6f20776f726c64210000000000000000000000000000000000000000"
} }
},
{
"VerifyCall": {
"success": true,
"output": "57caa176af1ac0433c5df30e8dabcd2ec1af1e92a26eced5f719b88458777cd6"
}
} }
] ]
} }
*/ */
contract TestSha3 { contract TestSha3 {
function test(string memory _pre) external payable returns (bytes32) { function test(string memory _pre) external payable returns (bytes32 hash) {
bytes32 hash = keccak256(bytes(_pre)); hash = keccak256(bytes(_pre));
return bytes32(uint(hash) + 1);
} }
} }
+4 -43
View File
@@ -4,27 +4,11 @@ pragma solidity ^0.8;
/* runner.json /* runner.json
{ {
"differential": true,
"actions": [ "actions": [
{ {
"Upload": { "Instantiate": {}
"code": { }
"Solidity": { ]
"contract": "ERC20"
}
}
}
},
{
"Instantiate": {
"code": {
"Solidity": {
"contract": "ERC20Tester"
}
}
}
}
]
} }
*/ */
@@ -98,26 +82,3 @@ contract ERC20 is IERC20 {
emit Transfer(msg.sender, address(0), amount); emit Transfer(msg.sender, address(0), amount);
} }
} }
contract ERC20Tester {
constructor() {
address BOB = address(0xffffffffffffffffffffffffffffffffffffff);
ERC20 token = new ERC20();
assert(token.decimals() == 18);
token.mint(300);
assert(token.balanceOf(address(this)) == 300);
token.transfer(BOB, 100);
assert(token.balanceOf(address(this)) == 200);
assert(token.balanceOf(BOB) == 100);
token.approve(address(this), 100);
token.transferFrom(address(this), BOB, 100);
assert(token.balanceOf(BOB) == 200);
assert(token.balanceOf(address(this)) == 100);
token.burn(100);
assert(token.balanceOf(address(this)) == 0);
}
}
+7 -3
View File
@@ -22,7 +22,12 @@ pragma solidity ^0.8;
}, },
"data": "4d43bec90000000000000000000000000000000000000000000000000000000000000000" "data": "4d43bec90000000000000000000000000000000000000000000000000000000000000000"
} }
}, }
]
}
*/
/* TODO when pallet_revive accepts Solidity event topics
{ {
"Call": { "Call": {
"dest": { "dest": {
@@ -31,8 +36,7 @@ pragma solidity ^0.8;
"data": "4d43bec9000000000000000000000000000000000000000000000000000000000000007b" "data": "4d43bec9000000000000000000000000000000000000000000000000000000000000007b"
} }
} }
]
}
*/ */
contract Events { contract Events {
-12
View File
@@ -14,16 +14,4 @@ contract ExtCode {
ret := codesize() ret := codesize()
} }
} }
function ExtCodeHash(address who) public view returns (bytes32 ret) {
assembly {
ret := extcodehash(who)
}
}
function CodeHash() public view returns (bytes32 ret) {
assembly {
ret := extcodehash(address())
}
}
} }
@@ -1,63 +0,0 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8;
/* runner.json
{
"differential": true,
"actions": [
{
"Upload": {
"code": {
"Solidity": {
"contract": "ImmutablesTester"
}
}
}
},
{
"Instantiate": {
"code": {
"Solidity": {
"contract": "Immutables"
}
}
}
},
{
"Call": {
"dest": {
"Instantiated": 0
}
}
}
]
}
*/
contract ImmutablesTester {
// Read should work in the runtime code
uint public immutable foo;
// Read should work in the runtime code
uint public immutable bar;
// Read should work in the runtime code
uint public immutable zoo;
// Assign and read should work in the constructor
constructor(uint _foo) payable {
foo = _foo;
bar = foo + 1;
zoo = bar + 2;
assert(zoo == _foo + 3);
}
}
contract Immutables {
fallback() external {
ImmutablesTester tester = new ImmutablesTester(127);
assert(tester.foo() == 127);
assert(tester.bar() == tester.foo() + 1);
assert(tester.zoo() == tester.bar() + 2);
}
}
+12 -1
View File
@@ -4,7 +4,6 @@ pragma solidity ^0.8;
/* runner.json /* runner.json
{ {
"differential": true,
"actions": [ "actions": [
{ {
"Instantiate": { "Instantiate": {
@@ -24,6 +23,12 @@ pragma solidity ^0.8;
"data": "f016832c" "data": "f016832c"
} }
}, },
{
"VerifyCall": {
"success": true,
"output": "0000000000000000000000000000000000000000000000000000000000000060"
}
},
{ {
"Call": { "Call": {
"dest": { "dest": {
@@ -31,6 +36,12 @@ pragma solidity ^0.8;
}, },
"data": "f4a63aa5" "data": "f4a63aa5"
} }
},
{
"VerifyCall": {
"success": true,
"output": "0000000000000000000000000000000000000000000000000000000000000084"
}
} }
] ]
} }
@@ -1,53 +0,0 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8;
/* runner.json
{
"differential": true,
"actions": [
{
"Upload": {
"code": {
"Solidity": {
"contract": "Callee"
}
}
}
},
{
"Instantiate": {
"code": {
"Solidity": {
"contract": "ReturnDataOob"
}
}
}
},
{
"Call": {
"dest": {
"Instantiated": 0
}
}
}
]
}
*/
contract Callee {
function echo(bytes memory payload) public pure returns (bytes memory) {
return payload;
}
}
contract ReturnDataOob {
fallback() external {
new Callee().echo(hex"1234");
assembly {
let pos := mload(64)
let size := add(returndatasize(), 1)
returndatacopy(pos, 0, size)
}
}
}
+1 -17
View File
@@ -22,14 +22,6 @@ pragma solidity ^0.8;
}, },
"data": "fabc9efaffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" "data": "fabc9efaffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
} }
},
{
"Call": {
"dest": {
"Instantiated": 0
},
"data": "558b9f9bffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
}
} }
] ]
} }
@@ -38,18 +30,10 @@ pragma solidity ^0.8;
contract Storage { contract Storage {
function transient(uint value) public returns (uint ret) { function transient(uint value) public returns (uint ret) {
assembly { assembly {
let slot := 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff00 let slot := 123
tstore(slot, value) tstore(slot, value)
let success := call(0, 0, 0, 0, 0, 0, 0) let success := call(0, 0, 0, 0, 0, 0, 0)
ret := tload(slot) ret := tload(slot)
} }
} }
function persistent(uint value) public returns (uint ret) {
assembly {
let slot := 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff00
sstore(slot, value)
ret := sload(slot)
}
}
} }
@@ -1,54 +0,0 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
/* runner.json
{
"differential": true,
"actions": [
{
"Upload": {
"code": {
"Solidity": {
"contract": "TransactionOrigin"
}
}
}
},
{
"Instantiate": {
"code": {
"Solidity": {
"contract": "TransactionTester"
}
}
}
},
{
"Call": {
"dest": {
"Instantiated": 0
},
"data": "f8a8fd6d"
}
}
]
}
*/
contract TransactionTester {
constructor() payable {
assert(tx.origin == new TransactionOrigin().test());
}
function test() public payable returns (address ret) {
ret = tx.origin;
}
}
contract TransactionOrigin {
function test() public payable returns (address ret) {
assert(msg.sender != tx.origin);
ret = tx.origin;
}
}
-54
View File
@@ -1,54 +0,0 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8;
/* runner.json
{
"differential": true,
"actions": [
{
"Instantiate": {
"code": {
"Solidity": {
"contract": "Transfer"
}
},
"value": 11
}
},
{
"Call": {
"dest": {
"Instantiated": 0
},
"data": "1c8d16b30000000000000000000000000303030303030303030303030303030303030303000000000000000000000000000000000000000000000000000000000000000a"
}
},
{
"Call": {
"dest": {
"Instantiated": 0
},
"data": "fb9e8d0500000000000000000000000003030303030303030303030303030303030303030000000000000000000000000000000000000000000000000000000000000001"
}
}
]
}
*/
contract Transfer {
constructor() payable {
transfer_self(msg.value);
}
function address_self() internal view returns (address payable) {
return payable(address(this));
}
function transfer_self(uint _amount) public payable {
transfer_to(address_self(), _amount);
}
function transfer_to(address payable _dest, uint _amount) public payable {
_dest.transfer(_amount);
}
}
+4 -33
View File
@@ -5,18 +5,8 @@ pragma solidity ^0.8;
{ {
"differential": true, "differential": true,
"actions": [ "actions": [
{
"Upload": {
"code": {
"Solidity": {
"contract": "ValueTester"
}
}
}
},
{ {
"Instantiate": { "Instantiate": {
"value": 1024,
"code": { "code": {
"Solidity": { "Solidity": {
"contract": "Value" "contract": "Value"
@@ -29,7 +19,6 @@ pragma solidity ^0.8;
"dest": { "dest": {
"Instantiated": 0 "Instantiated": 0
}, },
"value": 123,
"data": "3fa4f245" "data": "3fa4f245"
} }
} }
@@ -37,30 +26,12 @@ pragma solidity ^0.8;
} }
*/ */
contract ValueTester {
constructor() payable {}
function balance_self() public view returns (uint ret) {
ret = address(this).balance;
}
}
contract Value { contract Value {
constructor() payable {
ValueTester tester = new ValueTester{value: msg.value}();
// own account
assert(address(this).balance == 0);
// tester account
assert(address(tester).balance == msg.value);
assert(tester.balance_self() == msg.value);
// non-existant account
assert(address(0xdeadbeef).balance == 0);
}
function value() public payable returns (uint ret) { function value() public payable returns (uint ret) {
ret = msg.value; ret = msg.value;
} }
function balance_of(address _address) public view returns (uint ret) {
ret = _address.balance;
}
} }
-8
View File
@@ -178,16 +178,10 @@ sol!(
function ExtCodeSize(address who) public view returns (uint ret); function ExtCodeSize(address who) public view returns (uint ret);
function CodeSize() public pure returns (uint ret); function CodeSize() public pure returns (uint ret);
function ExtCodeHash(address who) public view returns (bytes32 ret);
function CodeHash() public view returns (bytes32 ret);
} }
); );
case!("ExtCode.sol", ExtCode, ExtCodeSizeCall, ext_code_size, address: Address); case!("ExtCode.sol", ExtCode, ExtCodeSizeCall, ext_code_size, address: Address);
case!("ExtCode.sol", ExtCode, CodeSizeCall, code_size,); case!("ExtCode.sol", ExtCode, CodeSizeCall, code_size,);
case!("ExtCode.sol", ExtCode, ExtCodeHashCall, ext_code_hash, address: Address);
case!("ExtCode.sol", ExtCode, CodeHashCall, code_hash,);
sol!( sol!(
contract MCopy { contract MCopy {
@@ -215,11 +209,9 @@ case!("Call.sol", "Call", vec![], call_constructor);
sol!( sol!(
contract Value { contract Value {
function balance_of(address _address) public view returns (uint ret); function balance_of(address _address) public view returns (uint ret);
function balance_self() public view returns (uint ret);
} }
); );
case!("Value.sol", Value, balance_ofCall, value_balance_of, address: Address); case!("Value.sol", Value, balance_ofCall, value_balance_of, address: Address);
case!("Value.sol", Value, balance_selfCall, value_balance_self,);
sol!( sol!(
contract Bitwise { contract Bitwise {
+137 -208
View File
@@ -6,10 +6,6 @@ use SpecsAction::*;
use crate::cases::Contract; use crate::cases::Contract;
/// Parameters:
/// - The function name of the test
/// - The contract name to fill in empty code based on the file path
/// - The contract source file
macro_rules! test_spec { macro_rules! test_spec {
($test_name:ident, $contract_name:literal, $source_file:literal) => { ($test_name:ident, $contract_name:literal, $source_file:literal) => {
#[test] #[test]
@@ -30,6 +26,7 @@ test_spec!(hash_keccak_256, "TestSha3", "Crypto.sol");
test_spec!(erc20, "ERC20", "ERC20.sol"); test_spec!(erc20, "ERC20", "ERC20.sol");
test_spec!(computation, "Computation", "Computation.sol"); test_spec!(computation, "Computation", "Computation.sol");
test_spec!(msize, "MSize", "MSize.sol"); test_spec!(msize, "MSize", "MSize.sol");
test_spec!(transferred_value, "Value", "Value.sol");
test_spec!(sha1, "SHA1", "SHA1.sol"); test_spec!(sha1, "SHA1", "SHA1.sol");
test_spec!(block, "Block", "Block.sol"); test_spec!(block, "Block", "Block.sol");
test_spec!(mcopy, "MCopy", "MCopy.sol"); test_spec!(mcopy, "MCopy", "MCopy.sol");
@@ -37,13 +34,6 @@ test_spec!(events, "Events", "Events.sol");
test_spec!(storage, "Storage", "Storage.sol"); test_spec!(storage, "Storage", "Storage.sol");
test_spec!(mstore8, "MStore8", "MStore8.sol"); test_spec!(mstore8, "MStore8", "MStore8.sol");
test_spec!(address, "Context", "Context.sol"); test_spec!(address, "Context", "Context.sol");
test_spec!(balance, "Value", "Value.sol");
test_spec!(create, "CreateB", "Create.sol");
test_spec!(call, "Caller", "Call.sol");
test_spec!(transfer, "Transfer", "Transfer.sol");
test_spec!(return_data_oob, "ReturnDataOob", "ReturnDataOob.sol");
test_spec!(immutables, "Immutables", "Immutables.sol");
test_spec!(transaction, "Transaction", "Transaction.sol");
fn instantiate(path: &str, contract: &str) -> Vec<SpecsAction> { fn instantiate(path: &str, contract: &str) -> Vec<SpecsAction> {
vec![Instantiate { vec![Instantiate {
@@ -231,206 +221,66 @@ fn signed_remainder() {
data: Contract::division_arithmetics_smod(n, d).calldata, data: Contract::division_arithmetics_smod(n, d).calldata,
}) })
} }
run_differential(actions);
}
#[test]
fn ext_code_hash() {
let mut actions = instantiate("contracts/ExtCode.sol", "ExtCode");
// First do contract instantiation to figure out address and code hash
let results = Specs {
actions: actions.clone(),
..Default::default()
}
.run();
let (addr, code_hash) = match results.first().cloned() {
Some(CallResult::Instantiate {
result, code_hash, ..
}) => (result.result.unwrap().addr, code_hash),
_ => panic!("instantiate contract failed"),
};
// code hash of itself
actions.push(Call {
origin: TestAddress::Alice,
dest: TestAddress::Instantiated(0),
value: 0,
gas_limit: None,
storage_deposit_limit: None,
data: Contract::code_hash().calldata,
});
actions.push(VerifyCall(VerifyCallExpectation {
success: true,
output: OptionalHex::from(code_hash.as_bytes().to_vec()),
gas_consumed: None,
}));
// code hash for a given contract address
actions.push(Call {
origin: TestAddress::Alice,
dest: TestAddress::Instantiated(0),
value: 0,
gas_limit: None,
storage_deposit_limit: None,
data: Contract::ext_code_hash(Address::from(addr.to_fixed_bytes())).calldata,
});
actions.push(VerifyCall(VerifyCallExpectation {
success: true,
output: OptionalHex::from(code_hash.as_bytes().to_vec()),
gas_consumed: None,
}));
// EOA returns fixed hash
actions.push(Call {
origin: TestAddress::Alice,
dest: TestAddress::Instantiated(0),
value: 0,
gas_limit: None,
storage_deposit_limit: None,
data: Contract::ext_code_hash(Address::from(CHARLIE.to_fixed_bytes())).calldata,
});
actions.push(VerifyCall(VerifyCallExpectation {
success: true,
output: OptionalHex::from(
hex!("c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470").to_vec(),
),
gas_consumed: None,
}));
// non-existing account
actions.push(Call {
origin: TestAddress::Alice,
dest: TestAddress::Instantiated(0),
value: 0,
gas_limit: None,
storage_deposit_limit: None,
data: Contract::ext_code_hash(Address::from([8u8; 20])).calldata,
});
actions.push(VerifyCall(VerifyCallExpectation {
success: true,
output: OptionalHex::from([0u8; 32].to_vec()),
gas_consumed: None,
}));
Specs {
actions,
..Default::default()
}
.run();
}
#[test]
fn ext_code_size() {
let alice = Address::from(ALICE.0);
let own_address = alice.create(0);
let baseline_address = alice.create2([0u8; 32], keccak256(Contract::baseline().pvm_runtime));
let own_code_size = U256::from(
Contract::ext_code_size(Default::default())
.pvm_runtime
.len(),
);
let baseline_code_size = U256::from(Contract::baseline().pvm_runtime.len());
Specs {
actions: vec![
// Instantiate the test contract
instantiate("contracts/ExtCode.sol", "ExtCode").remove(0),
// Instantiate the baseline contract
Instantiate {
origin: TestAddress::Alice,
value: 0,
gas_limit: Some(GAS_LIMIT),
storage_deposit_limit: None,
code: Code::Solidity {
path: Some("contracts/Baseline.sol".into()),
contract: "Baseline".to_string(),
solc_optimizer: None,
pipeline: None,
},
data: vec![],
salt: OptionalHex::from([0; 32]),
},
// Alice is not a contract and returns a code size of 0
Call {
origin: TestAddress::Alice,
dest: TestAddress::Instantiated(0),
value: 0,
gas_limit: None,
storage_deposit_limit: None,
data: Contract::ext_code_size(alice).calldata,
},
VerifyCall(VerifyCallExpectation {
success: true,
output: OptionalHex::from([0u8; 32].to_vec()),
gas_consumed: None,
}),
// Unknown address returns a code size of 0
Call {
origin: TestAddress::Alice,
dest: TestAddress::Instantiated(0),
value: 0,
gas_limit: None,
storage_deposit_limit: None,
data: Contract::ext_code_size(Address::from([0xff; 20])).calldata,
},
VerifyCall(VerifyCallExpectation {
success: true,
output: OptionalHex::from([0u8; 32].to_vec()),
gas_consumed: None,
}),
// Own address via extcodesize returns own code size
Call {
origin: TestAddress::Alice,
dest: TestAddress::Instantiated(0),
value: 0,
gas_limit: None,
storage_deposit_limit: None,
data: Contract::ext_code_size(own_address).calldata,
},
VerifyCall(VerifyCallExpectation {
success: true,
output: OptionalHex::from(own_code_size.to_be_bytes::<32>().to_vec()),
gas_consumed: None,
}),
// Own address via codesize returns own code size
Call {
origin: TestAddress::Alice,
dest: TestAddress::Instantiated(0),
value: 0,
gas_limit: None,
storage_deposit_limit: None,
data: Contract::code_size().calldata,
},
VerifyCall(VerifyCallExpectation {
success: true,
output: OptionalHex::from(own_code_size.to_be_bytes::<32>().to_vec()),
gas_consumed: None,
}),
// Baseline address returns the baseline code size
Call {
origin: TestAddress::Alice,
dest: TestAddress::Instantiated(0),
value: 0,
gas_limit: None,
storage_deposit_limit: None,
data: Contract::ext_code_size(baseline_address).calldata,
},
VerifyCall(VerifyCallExpectation {
success: true,
output: OptionalHex::from(baseline_code_size.to_be_bytes::<32>().to_vec()),
gas_consumed: None,
}),
],
..Default::default()
}
.run();
} }
/* /*
// These test were implement for the mock-runtime and need to be ported yet. #[test]
fn events() {
assert_success(&Contract::event(U256::ZERO), true);
assert_success(&Contract::event(U256::from(123)), true);
}
#[test]
fn balance() {
let (_, output) = assert_success(&Contract::value_balance_of(Default::default()), false);
let expected = U256::ZERO;
let received = U256::from_be_slice(&output.data);
assert_eq!(expected, received);
let expected = U256::from(54589);
let (mut state, address) = State::new_deployed(Contract::value_balance_of(Default::default()));
state.accounts_mut().get_mut(&address).unwrap().value = expected;
let contract = Contract::value_balance_of(address);
let (_, output) = state
.transaction()
.with_default_account(&contract.pvm_runtime)
.calldata(contract.calldata)
.call();
assert_eq!(ReturnFlags::Success, output.flags);
let received = U256::from_be_slice(&output.data);
assert_eq!(expected, received)
}
#[test]
fn create2() {
let mut state = State::default();
let contract_a = Contract::create_a();
state.upload_code(&contract_a.pvm_runtime);
let contract = Contract::create_b();
let (state, output) = state
.transaction()
.with_default_account(&contract.pvm_runtime)
.calldata(contract.calldata)
.call();
assert_eq!(output.flags, ReturnFlags::Success);
assert_eq!(state.accounts().len(), 2);
for address in state.accounts().keys() {
if *address != Transaction::default_address() {
let derived_address = Transaction::default_address().create2(
B256::from(U256::from(1)),
keccak256(&contract_a.pvm_runtime).0,
);
assert_eq!(*address, derived_address);
}
}
}
#[test] #[test]
fn create2_failure() { fn create2_failure() {
@@ -457,4 +307,83 @@ fn create2_failure() {
assert_eq!(output.flags, ReturnFlags::Revert); assert_eq!(output.flags, ReturnFlags::Revert);
} }
#[test]
fn create_with_value() {
let mut state = State::default();
state.upload_code(&Contract::create_a().pvm_runtime);
let amount = U256::from(123);
let contract = Contract::create_b();
let (state, output) = state
.transaction()
.with_default_account(&contract.pvm_runtime)
.callvalue(amount)
.call();
assert_eq!(output.flags, ReturnFlags::Success);
assert_eq!(state.accounts().len(), 2);
for (address, account) in state.accounts() {
if *address == Transaction::default_address() {
assert_eq!(account.value, U256::ZERO);
} else {
assert_eq!(account.value, amount);
}
}
}
#[test]
fn ext_code_size() {
let contract = Contract::ext_code_size(Transaction::default_address());
let (_, output) = assert_success(&contract, false);
let received = U256::from_be_slice(&output.data);
let expected = U256::from(contract.pvm_runtime.len());
assert_eq!(received, expected);
let contract = Contract::ext_code_size(Default::default());
let (_, output) = assert_success(&contract, false);
let received = U256::from_be_slice(&output.data);
let expected = U256::ZERO;
assert_eq!(received, expected);
}
#[test]
fn code_size() {
let contract = Contract::code_size();
let (_, output) = assert_success(&contract, false);
let expected = U256::from(contract.pvm_runtime.len());
let received = U256::from_be_slice(&output.data);
assert_eq!(expected, received);
}
#[test]
fn value_transfer() {
// Succeeds in remix (shanghai) but traps the interpreter
let (state, _) = assert_success(&Contract::call_value_transfer(Default::default()), false);
assert_eq!(state.accounts().len(), 2);
assert!(state.accounts().get(&Address::default()).is_some());
}
#[test]
fn echo() {
let (state, address) = State::new_deployed(Contract::call_constructor());
let expected = vec![1, 2, 3, 4, 5];
let contract = Contract::call_call(address, expected.clone());
let (_, output) = state
.transaction()
.with_default_account(&contract.pvm_runtime)
.calldata(contract.calldata)
.call();
assert_eq!(output.flags, ReturnFlags::Success);
let received = alloy_primitives::Bytes::abi_decode(&output.data, true)
.unwrap()
.to_vec();
assert_eq!(expected, received);
}
*/ */
-1
View File
@@ -32,7 +32,6 @@ fn invoke_lld(cmd_args: &[&str]) -> bool {
fn polkavm_linker<T: AsRef<[u8]>>(code: T) -> anyhow::Result<Vec<u8>> { fn polkavm_linker<T: AsRef<[u8]>>(code: T) -> anyhow::Result<Vec<u8>> {
let mut config = polkavm_linker::Config::default(); let mut config = polkavm_linker::Config::default();
config.set_strip(true); config.set_strip(true);
config.set_optimize(true);
polkavm_linker::program_from_elf(config, code.as_ref()) polkavm_linker::program_from_elf(config, code.as_ref())
.map_err(|reason| anyhow::anyhow!("polkavm linker failed: {}", reason)) .map_err(|reason| anyhow::anyhow!("polkavm linker failed: {}", reason))
-2
View File
@@ -27,11 +27,9 @@ pub use self::polkavm::context::function::llvm_runtime::LLVMRuntime as PolkaVMLL
pub use self::polkavm::context::function::r#return::Return as PolkaVMFunctionReturn; pub use self::polkavm::context::function::r#return::Return as PolkaVMFunctionReturn;
pub use self::polkavm::context::function::runtime::deploy_code::DeployCode as PolkaVMDeployCodeFunction; pub use self::polkavm::context::function::runtime::deploy_code::DeployCode as PolkaVMDeployCodeFunction;
pub use self::polkavm::context::function::runtime::entry::Entry as PolkaVMEntryFunction; pub use self::polkavm::context::function::runtime::entry::Entry as PolkaVMEntryFunction;
pub use self::polkavm::context::function::runtime::immutable_data_load::ImmutableDataLoad as PolkaVMImmutableDataLoadFunction;
pub use self::polkavm::context::function::runtime::runtime_code::RuntimeCode as PolkaVMRuntimeCodeFunction; pub use self::polkavm::context::function::runtime::runtime_code::RuntimeCode as PolkaVMRuntimeCodeFunction;
pub use self::polkavm::context::function::runtime::FUNCTION_DEPLOY_CODE as PolkaVMFunctionDeployCode; pub use self::polkavm::context::function::runtime::FUNCTION_DEPLOY_CODE as PolkaVMFunctionDeployCode;
pub use self::polkavm::context::function::runtime::FUNCTION_ENTRY as PolkaVMFunctionEntry; pub use self::polkavm::context::function::runtime::FUNCTION_ENTRY as PolkaVMFunctionEntry;
pub use self::polkavm::context::function::runtime::FUNCTION_LOAD_IMMUTABLE_DATA as PolkaVMFunctionImmutableDataLoad;
pub use self::polkavm::context::function::runtime::FUNCTION_RUNTIME_CODE as PolkaVMFunctionRuntimeCode; pub use self::polkavm::context::function::runtime::FUNCTION_RUNTIME_CODE as PolkaVMFunctionRuntimeCode;
pub use self::polkavm::context::function::yul_data::YulData as PolkaVMFunctionYulData; pub use self::polkavm::context::function::yul_data::YulData as PolkaVMFunctionYulData;
pub use self::polkavm::context::function::Function as PolkaVMFunction; pub use self::polkavm::context::function::Function as PolkaVMFunction;
@@ -21,6 +21,8 @@ pub struct Settings {
/// Fallback to optimizing for size if the bytecode is too large. /// Fallback to optimizing for size if the bytecode is too large.
pub is_fallback_to_size_enabled: bool, pub is_fallback_to_size_enabled: bool,
/// Whether the system request memoization is disabled.
pub is_system_request_memoization_disabled: bool,
/// Whether the LLVM `verify each` option is enabled. /// Whether the LLVM `verify each` option is enabled.
pub is_verify_each_enabled: bool, pub is_verify_each_enabled: bool,
@@ -41,6 +43,7 @@ impl Settings {
level_back_end, level_back_end,
is_fallback_to_size_enabled: false, is_fallback_to_size_enabled: false,
is_system_request_memoization_disabled: false,
is_verify_each_enabled: false, is_verify_each_enabled: false,
is_debug_logging_enabled: false, is_debug_logging_enabled: false,
@@ -62,6 +65,7 @@ impl Settings {
level_back_end, level_back_end,
is_fallback_to_size_enabled: false, is_fallback_to_size_enabled: false,
is_system_request_memoization_disabled: false,
is_verify_each_enabled, is_verify_each_enabled,
is_debug_logging_enabled, is_debug_logging_enabled,
@@ -202,10 +206,20 @@ impl Settings {
self.is_fallback_to_size_enabled = true; self.is_fallback_to_size_enabled = true;
} }
/// Disables the system request memoization.
pub fn disable_system_request_memoization(&mut self) {
self.is_system_request_memoization_disabled = true;
}
/// Whether the fallback to optimizing for size is enabled. /// Whether the fallback to optimizing for size is enabled.
pub fn is_fallback_to_size_enabled(&self) -> bool { pub fn is_fallback_to_size_enabled(&self) -> bool {
self.is_fallback_to_size_enabled self.is_fallback_to_size_enabled
} }
/// Whether the system request memoization is disabled.
pub fn is_system_request_memoization_disabled(&self) -> bool {
self.is_system_request_memoization_disabled
}
} }
impl PartialEq for Settings { impl PartialEq for Settings {
@@ -1,8 +1,14 @@
//! The LLVM context constants. //! The LLVM context constants.
/// Runtime API methods.
pub mod runtime_api;
/// The LLVM framework version. /// The LLVM framework version.
pub const LLVM_VERSION: semver::Version = semver::Version::new(18, 1, 4); pub const LLVM_VERSION: semver::Version = semver::Version::new(18, 1, 4);
/// The PolkaVM version.
pub const ZKEVM_VERSION: semver::Version = semver::Version::new(1, 3, 2);
/// The register width sized type /// The register width sized type
pub static XLEN: usize = revive_common::BIT_LENGTH_X32; pub static XLEN: usize = revive_common::BIT_LENGTH_X32;
@@ -15,9 +21,49 @@ pub static GLOBAL_CALLDATA_POINTER: &str = "ptr_calldata";
/// The calldata size global variable name. /// The calldata size global variable name.
pub static GLOBAL_CALLDATA_SIZE: &str = "calldatasize"; pub static GLOBAL_CALLDATA_SIZE: &str = "calldatasize";
/// The return data pointer global variable name.
pub static GLOBAL_RETURN_DATA_POINTER: &str = "ptr_return_data";
/// The return data size pointer global variable name.
pub static GLOBAL_RETURN_DATA_SIZE: &str = "returndatasize";
/// The call flags global variable name. /// The call flags global variable name.
pub static GLOBAL_CALL_FLAGS: &str = "call_flags"; pub static GLOBAL_CALL_FLAGS: &str = "call_flags";
/// The constant array global variable name prefix.
pub static GLOBAL_CONST_ARRAY_PREFIX: &str = "const_array_";
/// The global verbatim getter identifier prefix.
pub static GLOBAL_VERBATIM_GETTER_PREFIX: &str = "get_global::";
/// The static word size.
pub static GLOBAL_I256_SIZE: &str = "i256_size";
/// The static value size.
pub static GLOBAL_I160_SIZE: &str = "i160_size";
/// The static i64 size.
pub static GLOBAL_I64_SIZE: &str = "i64_size";
/// The external call data offset in the auxiliary heap.
pub const HEAP_AUX_OFFSET_EXTERNAL_CALL: u64 = 0;
/// The constructor return data offset in the auxiliary heap.
pub const HEAP_AUX_OFFSET_CONSTRUCTOR_RETURN_DATA: u64 =
8 * (revive_common::BYTE_LENGTH_WORD as u64);
/// The `create` method deployer signature.
pub static DEPLOYER_SIGNATURE_CREATE: &str = "create(bytes32,bytes32,bytes)";
/// The `create2` method deployer signature.
pub static DEPLOYER_SIGNATURE_CREATE2: &str = "create2(bytes32,bytes32,bytes)";
/// The absence of system call bit.
pub const NO_SYSTEM_CALL_BIT: bool = false;
/// The system call bit.
pub const SYSTEM_CALL_BIT: bool = true;
/// The deployer call header size that consists of: /// The deployer call header size that consists of:
/// - bytecode hash (32 bytes) /// - bytecode hash (32 bytes)
pub const DEPLOYER_CALL_HEADER_SIZE: usize = revive_common::BYTE_LENGTH_WORD; pub const DEPLOYER_CALL_HEADER_SIZE: usize = revive_common::BYTE_LENGTH_WORD;
@@ -0,0 +1,71 @@
//! Runtime API import and export symbols.
pub mod exports {
/// The contract deploy export.
pub static CALL: &str = "call";
/// The contract call export.
pub static DEPLOY: &str = "deploy";
/// All exported symbols.
/// Useful for configuring common attributes and linkage.
pub static EXPORTS: [&str; 2] = [CALL, DEPLOY];
}
pub mod imports {
pub static ADDRESS: &str = "address";
pub static BALANCE: &str = "balance";
pub static BLOCK_NUMBER: &str = "block_number";
pub static CALL: &str = "call";
pub static CALLER: &str = "caller";
pub static CODE_SIZE: &str = "code_size";
pub static DEPOSIT_EVENT: &str = "deposit_event";
pub static GET_STORAGE: &str = "get_storage";
pub static HASH_KECCAK_256: &str = "hash_keccak_256";
pub static INPUT: &str = "input";
pub static INSTANTIATE: &str = "instantiate";
pub static NOW: &str = "now";
pub static RETURN: &str = "seal_return";
pub static RETURNDATACOPY: &str = "returndatacopy";
pub static SET_STORAGE: &str = "set_storage";
pub static VALUE_TRANSFERRED: &str = "value_transferred";
/// All imported runtime API symbols.
/// Useful for configuring common attributes and linkage.
pub static IMPORTS: [&str; 16] = [
ADDRESS,
BALANCE,
BLOCK_NUMBER,
CALL,
CALLER,
CODE_SIZE,
DEPOSIT_EVENT,
GET_STORAGE,
HASH_KECCAK_256,
INPUT,
INSTANTIATE,
NOW,
RETURN,
RETURNDATACOPY,
SET_STORAGE,
VALUE_TRANSFERRED,
];
}
/// PolkaVM __sbrk API symbol to extend the heap memory.
pub static SBRK: &str = "__sbrk";
@@ -11,6 +11,15 @@ use crate::polkavm::context::function::Function;
/// The functions are automatically linked to the LLVM implementations if the signatures match. /// The functions are automatically linked to the LLVM implementations if the signatures match.
#[derive(Debug)] #[derive(Debug)]
pub struct LLVMRuntime<'ctx> { pub struct LLVMRuntime<'ctx> {
/// The corresponding LLVM runtime function.
pub shl: FunctionDeclaration<'ctx>,
/// The corresponding LLVM runtime function.
pub shr: FunctionDeclaration<'ctx>,
/// The corresponding LLVM runtime function.
pub sar: FunctionDeclaration<'ctx>,
/// The corresponding LLVM runtime function.
pub byte: FunctionDeclaration<'ctx>,
/// The corresponding LLVM runtime function. /// The corresponding LLVM runtime function.
pub add_mod: FunctionDeclaration<'ctx>, pub add_mod: FunctionDeclaration<'ctx>,
/// The corresponding LLVM runtime function. /// The corresponding LLVM runtime function.
@@ -22,9 +31,32 @@ pub struct LLVMRuntime<'ctx> {
/// The corresponding LLVM runtime function. /// The corresponding LLVM runtime function.
pub sha3: FunctionDeclaration<'ctx>, pub sha3: FunctionDeclaration<'ctx>,
/// The corresponding LLVM runtime function.
pub r#return: FunctionDeclaration<'ctx>,
/// The corresponding LLVM runtime function.
pub revert: FunctionDeclaration<'ctx>,
} }
impl<'ctx> LLVMRuntime<'ctx> { impl<'ctx> LLVMRuntime<'ctx> {
/// The LLVM personality function name.
pub const FUNCTION_PERSONALITY: &'static str = "__personality";
/// The LLVM exception throwing function name.
pub const FUNCTION_CXA_THROW: &'static str = "__cxa_throw";
/// The corresponding runtime function name.
pub const FUNCTION_SHL: &'static str = "__shl";
/// The corresponding runtime function name.
pub const FUNCTION_SHR: &'static str = "__shr";
/// The corresponding runtime function name.
pub const FUNCTION_SAR: &'static str = "__sar";
/// The corresponding runtime function name.
pub const FUNCTION_BYTE: &'static str = "__byte";
/// The corresponding runtime function name. /// The corresponding runtime function name.
pub const FUNCTION_ADDMOD: &'static str = "__addmod"; pub const FUNCTION_ADDMOD: &'static str = "__addmod";
@@ -40,12 +72,121 @@ impl<'ctx> LLVMRuntime<'ctx> {
/// The corresponding runtime function name. /// The corresponding runtime function name.
pub const FUNCTION_SHA3: &'static str = "__sha3"; pub const FUNCTION_SHA3: &'static str = "__sha3";
/// The corresponding runtime function name.
pub const FUNCTION_SYSTEM_REQUEST: &'static str = "__system_request";
/// The corresponding runtime function name.
pub const FUNCTION_FARCALL: &'static str = "__farcall";
/// The corresponding runtime function name.
pub const FUNCTION_STATICCALL: &'static str = "__staticcall";
/// The corresponding runtime function name.
pub const FUNCTION_DELEGATECALL: &'static str = "__delegatecall";
/// The corresponding runtime function name.
pub const FUNCTION_MIMICCALL: &'static str = "__mimiccall";
/// The corresponding runtime function name.
pub const FUNCTION_FARCALL_BYREF: &'static str = "__farcall_byref";
/// The corresponding runtime function name.
pub const FUNCTION_STATICCALL_BYREF: &'static str = "__staticcall_byref";
/// The corresponding runtime function name.
pub const FUNCTION_DELEGATECALL_BYREF: &'static str = "__delegatecall_byref";
/// The corresponding runtime function name.
pub const FUNCTION_MIMICCALL_BYREF: &'static str = "__mimiccall_byref";
/// The corresponding runtime function name.
pub const FUNCTION_RETURN: &'static str = "__return";
/// The corresponding runtime function name.
pub const FUNCTION_REVERT: &'static str = "__revert";
/// A shortcut constructor. /// A shortcut constructor.
pub fn new( pub fn new(
llvm: &'ctx inkwell::context::Context, llvm: &'ctx inkwell::context::Context,
module: &inkwell::module::Module<'ctx>, module: &inkwell::module::Module<'ctx>,
optimizer: &Optimizer, optimizer: &Optimizer,
) -> Self { ) -> Self {
let shl = Self::declare(
module,
Self::FUNCTION_SHL,
llvm.custom_width_int_type(revive_common::BIT_LENGTH_WORD as u32)
.fn_type(
vec![
llvm.custom_width_int_type(revive_common::BIT_LENGTH_WORD as u32)
.as_basic_type_enum()
.into();
2
]
.as_slice(),
false,
),
Some(inkwell::module::Linkage::External),
);
Function::set_default_attributes(llvm, shl, optimizer);
Function::set_pure_function_attributes(llvm, shl);
let shr = Self::declare(
module,
Self::FUNCTION_SHR,
llvm.custom_width_int_type(revive_common::BIT_LENGTH_WORD as u32)
.fn_type(
vec![
llvm.custom_width_int_type(revive_common::BIT_LENGTH_WORD as u32)
.as_basic_type_enum()
.into();
2
]
.as_slice(),
false,
),
Some(inkwell::module::Linkage::External),
);
Function::set_default_attributes(llvm, shr, optimizer);
Function::set_pure_function_attributes(llvm, shr);
let sar = Self::declare(
module,
Self::FUNCTION_SAR,
llvm.custom_width_int_type(revive_common::BIT_LENGTH_WORD as u32)
.fn_type(
vec![
llvm.custom_width_int_type(revive_common::BIT_LENGTH_WORD as u32)
.as_basic_type_enum()
.into();
2
]
.as_slice(),
false,
),
Some(inkwell::module::Linkage::External),
);
Function::set_default_attributes(llvm, sar, optimizer);
Function::set_pure_function_attributes(llvm, sar);
let byte = Self::declare(
module,
Self::FUNCTION_BYTE,
llvm.custom_width_int_type(revive_common::BIT_LENGTH_WORD as u32)
.fn_type(
vec![
llvm.custom_width_int_type(revive_common::BIT_LENGTH_WORD as u32)
.as_basic_type_enum()
.into();
2
]
.as_slice(),
false,
),
Some(inkwell::module::Linkage::External),
);
Function::set_default_attributes(llvm, byte, optimizer);
Function::set_pure_function_attributes(llvm, byte);
let add_mod = let add_mod =
Self::define(module, Self::FUNCTION_ADDMOD).expect("should be declared in stdlib"); Self::define(module, Self::FUNCTION_ADDMOD).expect("should be declared in stdlib");
Function::set_default_attributes(llvm, add_mod, optimizer); Function::set_default_attributes(llvm, add_mod, optimizer);
@@ -95,13 +236,54 @@ impl<'ctx> LLVMRuntime<'ctx> {
false, false,
); );
let r#return = Self::declare(
module,
Self::FUNCTION_RETURN,
llvm.void_type().fn_type(
vec![
llvm.custom_width_int_type(revive_common::BIT_LENGTH_WORD as u32)
.as_basic_type_enum()
.into();
3
]
.as_slice(),
false,
),
Some(inkwell::module::Linkage::External),
);
Function::set_default_attributes(llvm, r#return, optimizer);
let revert = Self::declare(
module,
Self::FUNCTION_REVERT,
llvm.void_type().fn_type(
vec![
llvm.custom_width_int_type(revive_common::BIT_LENGTH_WORD as u32)
.as_basic_type_enum()
.into();
3
]
.as_slice(),
false,
),
Some(inkwell::module::Linkage::External),
);
Function::set_default_attributes(llvm, revert, optimizer);
Self { Self {
shl,
shr,
sar,
byte,
add_mod, add_mod,
mul_mod, mul_mod,
exp, exp,
sign_extend, sign_extend,
sha3, sha3,
r#return,
revert,
} }
} }
@@ -48,6 +48,12 @@ pub struct Function<'ctx> {
} }
impl<'ctx> Function<'ctx> { impl<'ctx> Function<'ctx> {
/// The near call ABI function prefix.
pub const ZKSYNC_NEAR_CALL_ABI_PREFIX: &'static str = "ZKSYNC_NEAR_CALL";
/// The near call ABI exception handler name.
pub const ZKSYNC_NEAR_CALL_ABI_EXCEPTION_HANDLER: &'static str = "ZKSYNC_CATCH_NEAR_CALL";
/// The stack hashmap default capacity. /// The stack hashmap default capacity.
const STACK_HASHMAP_INITIAL_CAPACITY: usize = 64; const STACK_HASHMAP_INITIAL_CAPACITY: usize = 64;
@@ -85,8 +91,13 @@ impl<'ctx> Function<'ctx> {
|| (name.starts_with("__") || (name.starts_with("__")
&& name != self::runtime::FUNCTION_ENTRY && name != self::runtime::FUNCTION_ENTRY
&& name != self::runtime::FUNCTION_DEPLOY_CODE && name != self::runtime::FUNCTION_DEPLOY_CODE
&& name != self::runtime::FUNCTION_RUNTIME_CODE && name != self::runtime::FUNCTION_RUNTIME_CODE)
&& name != self::runtime::FUNCTION_LOAD_IMMUTABLE_DATA) }
/// Checks whether the function is related to the near call ABI.
pub fn is_near_call_abi(name: &str) -> bool {
name.starts_with(Self::ZKSYNC_NEAR_CALL_ABI_PREFIX)
|| name == Self::ZKSYNC_NEAR_CALL_ABI_EXCEPTION_HANDLER
} }
/// Returns the LLVM function declaration. /// Returns the LLVM function declaration.
@@ -42,7 +42,8 @@ where
D: Dependency + Clone, D: Dependency + Clone,
{ {
fn declare(&mut self, context: &mut Context<D>) -> anyhow::Result<()> { fn declare(&mut self, context: &mut Context<D>) -> anyhow::Result<()> {
let function_type = context.function_type::<inkwell::types::BasicTypeEnum>(vec![], 0); let function_type =
context.function_type::<inkwell::types::BasicTypeEnum>(vec![], 0, false);
context.add_function( context.add_function(
runtime::FUNCTION_DEPLOY_CODE, runtime::FUNCTION_DEPLOY_CODE,
function_type, function_type,
@@ -1,10 +1,12 @@
//! The entry function. //! The entry function.
use inkwell::types::BasicType; use inkwell::types::BasicType;
use inkwell::values::BasicValue;
use crate::polkavm::context::address_space::AddressSpace; use crate::polkavm::context::address_space::AddressSpace;
use crate::polkavm::context::function::runtime; use crate::polkavm::context::function::runtime;
use crate::polkavm::context::Context; use crate::polkavm::context::Context;
use crate::polkavm::r#const::*;
use crate::polkavm::Dependency; use crate::polkavm::Dependency;
use crate::polkavm::WriteLLVM; use crate::polkavm::WriteLLVM;
@@ -18,8 +20,14 @@ impl Entry {
/// The call flags argument index. /// The call flags argument index.
pub const ARGUMENT_INDEX_CALL_FLAGS: usize = 0; pub const ARGUMENT_INDEX_CALL_FLAGS: usize = 0;
/// Reserve 1kb for calldata. /// The number of mandatory arguments.
pub const MAX_CALLDATA_SIZE: usize = 1024; pub const MANDATORY_ARGUMENTS_COUNT: usize = 2;
/// Reserve 1mb for calldata.
pub const MAX_CALLDATA_SIZE: usize = 1024 * 1024;
/// Reserve 1mb for returndata.
pub const MAX_RETURNDATA_SIZE: usize = 1024 * 1024;
/// Initializes the global variables. /// Initializes the global variables.
/// The pointers are not initialized, because it's not possible to create a null pointer. /// The pointers are not initialized, because it's not possible to create a null pointer.
@@ -35,6 +43,14 @@ impl Entry {
calldata_type.get_undef(), calldata_type.get_undef(),
); );
let returndata_type = context.array_type(context.byte_type(), Self::MAX_RETURNDATA_SIZE);
context.set_global(
crate::polkavm::GLOBAL_RETURN_DATA_POINTER,
returndata_type,
AddressSpace::Stack,
returndata_type.get_undef(),
);
context.set_global( context.set_global(
crate::polkavm::GLOBAL_HEAP_MEMORY_POINTER, crate::polkavm::GLOBAL_HEAP_MEMORY_POINTER,
context.llvm().ptr_type(AddressSpace::Heap.into()), context.llvm().ptr_type(AddressSpace::Heap.into()),
@@ -45,10 +61,7 @@ impl Entry {
context context
.get_global(crate::polkavm::GLOBAL_HEAP_MEMORY_POINTER)? .get_global(crate::polkavm::GLOBAL_HEAP_MEMORY_POINTER)?
.into(), .into(),
context.build_sbrk( context.build_sbrk(context.integer_const(crate::polkavm::XLEN, 0))?,
context.xlen_type().const_zero(),
context.xlen_type().const_zero(),
)?,
)?; )?;
context.set_global( context.set_global(
@@ -57,6 +70,12 @@ impl Entry {
AddressSpace::Stack, AddressSpace::Stack,
context.word_undef(), context.word_undef(),
); );
context.set_global(
crate::polkavm::GLOBAL_RETURN_DATA_SIZE,
context.xlen_type(),
AddressSpace::Stack,
context.xlen_type().const_zero().as_basic_value_enum(),
);
context.set_global( context.set_global(
crate::polkavm::GLOBAL_CALL_FLAGS, crate::polkavm::GLOBAL_CALL_FLAGS,
@@ -65,6 +84,33 @@ impl Entry {
context.word_const(0), context.word_const(0),
); );
context.set_global(
crate::polkavm::GLOBAL_I256_SIZE,
context.xlen_type(),
AddressSpace::Stack,
context.integer_const(
crate::polkavm::XLEN,
revive_common::BYTE_LENGTH_X64 as u64 * 4,
),
);
context.set_global(
crate::polkavm::GLOBAL_I160_SIZE,
context.xlen_type(),
AddressSpace::Stack,
context.integer_const(
crate::polkavm::XLEN,
revive_common::BYTE_LENGTH_X64 as u64 * 2 + revive_common::BYTE_LENGTH_X32 as u64,
),
);
context.set_global(
crate::polkavm::GLOBAL_I64_SIZE,
context.xlen_type(),
AddressSpace::Stack,
context.integer_const(crate::polkavm::XLEN, revive_common::BYTE_LENGTH_X64 as u64),
);
Ok(()) Ok(())
} }
@@ -84,7 +130,7 @@ impl Entry {
"input_pointer_casted", "input_pointer_casted",
)?; )?;
let length_pointer = context.build_alloca_at_entry(context.xlen_type(), "len_ptr"); let length_pointer = context.build_alloca(context.xlen_type(), "len_ptr");
let length_pointer_casted = context.builder.build_ptr_to_int( let length_pointer_casted = context.builder.build_ptr_to_int(
length_pointer.value, length_pointer.value,
context.xlen_type(), context.xlen_type(),
@@ -96,7 +142,7 @@ impl Entry {
context.integer_const(crate::polkavm::XLEN, Self::MAX_CALLDATA_SIZE as u64), context.integer_const(crate::polkavm::XLEN, Self::MAX_CALLDATA_SIZE as u64),
)?; )?;
context.build_runtime_call( context.build_runtime_call(
revive_runtime_api::polkavm_imports::INPUT, runtime_api::imports::INPUT,
&[input_pointer_casted.into(), length_pointer_casted.into()], &[input_pointer_casted.into(), length_pointer_casted.into()],
); );
@@ -175,7 +221,7 @@ where
{ {
fn declare(&mut self, context: &mut Context<D>) -> anyhow::Result<()> { fn declare(&mut self, context: &mut Context<D>) -> anyhow::Result<()> {
let entry_arguments = vec![context.bool_type().as_basic_type_enum()]; let entry_arguments = vec![context.bool_type().as_basic_type_enum()];
let entry_function_type = context.function_type(entry_arguments, 0); let entry_function_type = context.function_type(entry_arguments, 0, false);
context.add_function( context.add_function(
runtime::FUNCTION_ENTRY, runtime::FUNCTION_ENTRY,
entry_function_type, entry_function_type,
@@ -183,18 +229,6 @@ where
Some(inkwell::module::Linkage::External), Some(inkwell::module::Linkage::External),
)?; )?;
context.declare_global(
revive_runtime_api::immutable_data::GLOBAL_IMMUTABLE_DATA_POINTER,
context.word_type().array_type(0),
AddressSpace::Stack,
);
context.declare_global(
revive_runtime_api::immutable_data::GLOBAL_IMMUTABLE_DATA_SIZE,
context.xlen_type(),
AddressSpace::Stack,
);
Ok(()) Ok(())
} }
@@ -1,116 +0,0 @@
//! The immutable data runtime function.
use crate::polkavm::context::address_space::AddressSpace;
use crate::polkavm::context::function::runtime;
use crate::polkavm::context::pointer::Pointer;
use crate::polkavm::context::Context;
use crate::polkavm::Dependency;
use crate::polkavm::WriteLLVM;
/// A function for requesting the immutable data from the runtime.
/// This is a special function that is only used by the front-end generated code.
///
/// The runtime API is called lazily and subsequent calls are no-ops.
///
/// The bytes written is asserted to match the expected length.
/// This should never fail; the length is known.
/// However, this is a one time assertion, hence worth it.
#[derive(Debug)]
pub struct ImmutableDataLoad;
impl<D> WriteLLVM<D> for ImmutableDataLoad
where
D: Dependency + Clone,
{
fn declare(&mut self, context: &mut Context<D>) -> anyhow::Result<()> {
context.add_function(
runtime::FUNCTION_LOAD_IMMUTABLE_DATA,
context.void_type().fn_type(Default::default(), false),
0,
Some(inkwell::module::Linkage::Private),
)?;
Ok(())
}
fn into_llvm(self, context: &mut Context<D>) -> anyhow::Result<()> {
context.set_current_function(runtime::FUNCTION_LOAD_IMMUTABLE_DATA)?;
context.set_basic_block(context.current_function().borrow().entry_block());
let immutable_data_size_pointer = context
.get_global(revive_runtime_api::immutable_data::GLOBAL_IMMUTABLE_DATA_SIZE)?
.value
.as_pointer_value();
let immutable_data_size = context.build_load(
Pointer::new(
context.xlen_type(),
AddressSpace::Stack,
immutable_data_size_pointer,
),
"immutable_data_size_load",
)?;
let load_immutable_data_block = context.append_basic_block("load_immutables_block");
let return_block = context.current_function().borrow().return_block();
let immutable_data_size_is_zero = context.builder().build_int_compare(
inkwell::IntPredicate::EQ,
context.xlen_type().const_zero(),
immutable_data_size.into_int_value(),
"immutable_data_size_is_zero",
)?;
context.build_conditional_branch(
immutable_data_size_is_zero,
return_block,
load_immutable_data_block,
)?;
context.set_basic_block(load_immutable_data_block);
let output_pointer = context
.get_global(revive_runtime_api::immutable_data::GLOBAL_IMMUTABLE_DATA_POINTER)?
.value
.as_pointer_value();
context.build_runtime_call(
revive_runtime_api::polkavm_imports::GET_IMMUTABLE_DATA,
&[
context
.builder()
.build_ptr_to_int(output_pointer, context.xlen_type(), "ptr_to_xlen")?
.into(),
context
.builder()
.build_ptr_to_int(
immutable_data_size_pointer,
context.xlen_type(),
"ptr_to_xlen",
)?
.into(),
],
);
let bytes_written = context.builder().build_load(
context.xlen_type(),
immutable_data_size_pointer,
"bytes_written",
)?;
context.builder().build_store(
immutable_data_size_pointer,
context.xlen_type().const_zero(),
)?;
let overflow_block = context.append_basic_block("immutable_data_overflow");
let is_overflow = context.builder().build_int_compare(
inkwell::IntPredicate::UGT,
immutable_data_size.into_int_value(),
bytes_written.into_int_value(),
"is_overflow",
)?;
context.build_conditional_branch(is_overflow, overflow_block, return_block)?;
context.set_basic_block(overflow_block);
context.build_call(context.intrinsics().trap, &[], "invalid_trap");
context.build_unreachable();
context.set_basic_block(return_block);
context.build_return(None);
Ok(())
}
}
@@ -2,7 +2,6 @@
pub mod deploy_code; pub mod deploy_code;
pub mod entry; pub mod entry;
pub mod immutable_data_load;
pub mod runtime_code; pub mod runtime_code;
/// The main entry function name. /// The main entry function name.
@@ -13,6 +12,3 @@ pub const FUNCTION_DEPLOY_CODE: &str = "__deploy";
/// The runtime code function name. /// The runtime code function name.
pub const FUNCTION_RUNTIME_CODE: &str = "__runtime"; pub const FUNCTION_RUNTIME_CODE: &str = "__runtime";
/// The immutable data load function name.
pub const FUNCTION_LOAD_IMMUTABLE_DATA: &str = "__immutable_data_load";
@@ -42,7 +42,8 @@ where
D: Dependency + Clone, D: Dependency + Clone,
{ {
fn declare(&mut self, context: &mut Context<D>) -> anyhow::Result<()> { fn declare(&mut self, context: &mut Context<D>) -> anyhow::Result<()> {
let function_type = context.function_type::<inkwell::types::BasicTypeEnum>(vec![], 0); let function_type =
context.function_type::<inkwell::types::BasicTypeEnum>(vec![], 0, false);
context.add_function( context.add_function(
runtime::FUNCTION_RUNTIME_CODE, runtime::FUNCTION_RUNTIME_CODE,
function_type, function_type,
@@ -51,31 +51,4 @@ impl<'ctx> Global<'ctx> {
global global
} }
/// Construct an external global.
pub fn declare<D, T>(
context: &mut Context<'ctx, D>,
r#type: T,
address_space: AddressSpace,
name: &str,
) -> Self
where
D: PolkaVMDependency + Clone,
T: BasicType<'ctx>,
{
let r#type = r#type.as_basic_type_enum();
let value = context
.module()
.add_global(r#type, Some(address_space.into()), name);
let global = Self { r#type, value };
global.value.set_linkage(inkwell::module::Linkage::External);
global
.value
.set_visibility(inkwell::GlobalVisibility::Default);
global.value.set_externally_initialized(true);
global
}
} }
+173 -125
View File
@@ -26,6 +26,7 @@ use inkwell::values::BasicValue;
use crate::optimizer::settings::Settings as OptimizerSettings; use crate::optimizer::settings::Settings as OptimizerSettings;
use crate::optimizer::Optimizer; use crate::optimizer::Optimizer;
use crate::polkavm::r#const::*;
use crate::polkavm::DebugConfig; use crate::polkavm::DebugConfig;
use crate::polkavm::Dependency; use crate::polkavm::Dependency;
use crate::target_machine::target::Target; use crate::target_machine::target::Target;
@@ -135,7 +136,7 @@ where
) )
.expect("the PolkaVM imports module should be linkable"); .expect("the PolkaVM imports module should be linkable");
for import in revive_runtime_api::polkavm_imports::IMPORTS { for import in runtime_api::imports::IMPORTS {
module module
.get_function(import) .get_function(import)
.expect("should be declared") .expect("should be declared")
@@ -161,18 +162,6 @@ where
}) })
} }
fn link_immutable_data(&self, contract_path: &str) -> anyhow::Result<()> {
let size = self.solidity().immutables_size() as u32;
let exports = revive_runtime_api::immutable_data::module(self.llvm(), size);
self.module.link_in_module(exports).map_err(|error| {
anyhow::anyhow!(
"The contract `{}` immutable data module linking error: {}",
contract_path,
error
)
})
}
/// Configure the PolkaVM minimum stack size. /// Configure the PolkaVM minimum stack size.
fn set_polkavm_stack_size( fn set_polkavm_stack_size(
llvm: &'ctx inkwell::context::Context, llvm: &'ctx inkwell::context::Context,
@@ -250,7 +239,6 @@ where
let module_clone = self.module.clone(); let module_clone = self.module.clone();
self.link_polkavm_exports(contract_path)?; self.link_polkavm_exports(contract_path)?;
self.link_immutable_data(contract_path)?;
let target_machine = TargetMachine::new(Target::PVM, self.optimizer.settings())?; let target_machine = TargetMachine::new(Target::PVM, self.optimizer.settings())?;
target_machine.set_target_data(self.module()); target_machine.set_target_data(self.module());
@@ -393,15 +381,6 @@ where
} }
} }
/// Declare an external global.
pub fn declare_global<T>(&mut self, name: &str, r#type: T, address_space: AddressSpace)
where
T: BasicType<'ctx> + Clone + Copy,
{
let global = Global::declare(self, r#type, address_space, name);
self.globals.insert(name.to_owned(), global);
}
/// Returns the LLVM intrinsics collection reference. /// Returns the LLVM intrinsics collection reference.
pub fn intrinsics(&self) -> &Intrinsics<'ctx> { pub fn intrinsics(&self) -> &Intrinsics<'ctx> {
&self.intrinsics &self.intrinsics
@@ -412,14 +391,47 @@ where
&self.llvm_runtime &self.llvm_runtime
} }
/// Declare a function already existing in the module.
pub fn declare_extern_function(
&mut self,
name: &str,
) -> anyhow::Result<Rc<RefCell<Function<'ctx>>>> {
let function = self.module().get_function(name).ok_or_else(|| {
anyhow::anyhow!("Failed to activate an undeclared function `{}`", name)
})?;
let basic_block = self.llvm.append_basic_block(function, name);
let declaration = FunctionDeclaration::new(
self.function_type::<inkwell::types::BasicTypeEnum>(vec![], 0, false),
function,
);
let function = Function::new(
name.to_owned(),
declaration,
FunctionReturn::None,
basic_block,
basic_block,
);
Function::set_default_attributes(self.llvm, function.declaration(), &self.optimizer);
let function = Rc::new(RefCell::new(function));
self.functions.insert(name.to_string(), function.clone());
Ok(function)
}
/// Appends a function to the current module. /// Appends a function to the current module.
pub fn add_function( pub fn add_function(
&mut self, &mut self,
name: &str, name: &str,
r#type: inkwell::types::FunctionType<'ctx>, r#type: inkwell::types::FunctionType<'ctx>,
return_values_length: usize, return_values_length: usize,
linkage: Option<inkwell::module::Linkage>, mut linkage: Option<inkwell::module::Linkage>,
) -> anyhow::Result<Rc<RefCell<Function<'ctx>>>> { ) -> anyhow::Result<Rc<RefCell<Function<'ctx>>>> {
if Function::is_near_call_abi(name) && self.is_system_mode() {
linkage = Some(inkwell::module::Linkage::External);
}
let value = self.module().add_function(name, r#type, linkage); let value = self.module().add_function(name, r#type, linkage);
let entry_block = self.llvm.append_basic_block(value, "entry"); let entry_block = self.llvm.append_basic_block(value, "entry");
@@ -432,6 +444,12 @@ where
let pointer = self.build_alloca(self.word_type(), "return_pointer"); let pointer = self.build_alloca(self.word_type(), "return_pointer");
FunctionReturn::primitive(pointer) FunctionReturn::primitive(pointer)
} }
size if name.starts_with(Function::ZKSYNC_NEAR_CALL_ABI_PREFIX) => {
let first_argument = value.get_first_param().expect("Always exists");
let r#type = self.structure_type(vec![self.word_type(); size].as_slice());
let pointer = first_argument.into_pointer_value();
FunctionReturn::compound(Pointer::new(r#type, AddressSpace::Stack, pointer), size)
}
size => { size => {
self.set_basic_block(entry_block); self.set_basic_block(entry_block);
let pointer = self.build_alloca( let pointer = self.build_alloca(
@@ -452,6 +470,10 @@ where
return_block, return_block,
); );
Function::set_default_attributes(self.llvm, function.declaration(), &self.optimizer); Function::set_default_attributes(self.llvm, function.declaration(), &self.optimizer);
if Function::is_near_call_abi(function.name()) && self.is_system_mode() {
Function::set_exception_handler_attributes(self.llvm, function.declaration());
}
let function = Rc::new(RefCell::new(function)); let function = Rc::new(RefCell::new(function));
self.functions.insert(name.to_string(), function.clone()); self.functions.insert(name.to_string(), function.clone());
@@ -512,6 +534,10 @@ where
manager, manager,
name, name,
self.optimizer.settings().to_owned(), self.optimizer.settings().to_owned(),
self.yul_data
.as_ref()
.map(|data| data.is_system_mode())
.unwrap_or_default(),
self.include_metadata_hash, self.include_metadata_hash,
self.debug_config.clone(), self.debug_config.clone(),
) )
@@ -569,70 +595,59 @@ where
self.builder.get_insert_block().expect("Always exists") self.builder.get_insert_block().expect("Always exists")
} }
/// Builds an aligned stack allocation at the function entry. /// Builds a stack allocation instruction.
pub fn build_alloca_at_entry<T: BasicType<'ctx> + Clone + Copy>( /// Sets the alignment to 128 bits.
&self,
r#type: T,
name: &str,
) -> Pointer<'ctx> {
let current_block = self.basic_block();
let entry_block = self.current_function().borrow().entry_block();
match entry_block.get_first_instruction() {
Some(instruction) => self.builder().position_before(&instruction),
None => self.builder().position_at_end(entry_block),
}
let pointer = self.build_alloca(r#type, name);
self.set_basic_block(current_block);
pointer
}
/// Builds an aligned stack allocation at the current position.
/// Use this if [`build_alloca_at_entry`] might change program semantics.
/// Otherwise, alloca should always be built at the function prelude!
pub fn build_alloca<T: BasicType<'ctx> + Clone + Copy>( pub fn build_alloca<T: BasicType<'ctx> + Clone + Copy>(
&self, &self,
r#type: T, r#type: T,
name: &str, name: &str,
) -> Pointer<'ctx> { ) -> Pointer<'ctx> {
let pointer = self.builder.build_alloca(r#type, name).unwrap(); let pointer = self.builder.build_alloca(r#type, name).unwrap();
pointer self.basic_block()
.as_instruction() .get_last_instruction()
.unwrap() .expect("Always exists")
.set_alignment(revive_common::BYTE_LENGTH_STACK_ALIGN as u32) .set_alignment(revive_common::BYTE_LENGTH_STACK_ALIGN as u32)
.expect("Alignment is valid"); .expect("Alignment is valid");
Pointer::new(r#type, AddressSpace::Stack, pointer) Pointer::new(r#type, AddressSpace::Stack, pointer)
} }
/// Truncate `address` to the ethereum address length and store it as bytes on the stack. /// Allocate an int of size `bit_length` on the stack.
/// The stack allocation will be at the function entry. Returns the stack pointer. /// Returns the allocation pointer and the length pointer.
/// This helper should be used when passing address arguments to the runtime, ensuring correct size and endianness. ///
pub fn build_address_argument_store( /// Useful helper for passing runtime API parameters on the stack.
pub fn build_stack_parameter(
&self, &self,
address: inkwell::values::IntValue<'ctx>, bit_length: usize,
) -> anyhow::Result<Pointer<'ctx>> { name: &str,
let address_type = self.integer_type(revive_common::BIT_LENGTH_ETH_ADDRESS); ) -> (Pointer<'ctx>, Pointer<'ctx>) {
let address_pointer = self.build_alloca_at_entry(address_type, "address_pointer"); let buffer_pointer = self.build_alloca(self.integer_type(bit_length), name);
let address_truncated = let symbol = match bit_length {
self.builder() revive_common::BIT_LENGTH_WORD => GLOBAL_I256_SIZE,
.build_int_truncate(address, address_type, "address_truncated")?; revive_common::BIT_LENGTH_ETH_ADDRESS => GLOBAL_I160_SIZE,
let address_swapped = self.build_byte_swap(address_truncated.into())?; revive_common::BIT_LENGTH_BLOCK_NUMBER => GLOBAL_I64_SIZE,
self.build_store(address_pointer, address_swapped)?; _ => panic!("invalid stack parameter bit width: {bit_length}"),
Ok(address_pointer) };
let length_pointer = self.get_global(symbol).expect("should be declared");
(buffer_pointer, length_pointer.into())
} }
/// Load the address at given pointer and zero extend it to the VM word size. /// Load the integer at given pointer and zero extend it to the VM word size.
pub fn build_load_address( pub fn build_load_word(
&self, &self,
pointer: Pointer<'ctx>, pointer: Pointer<'ctx>,
bit_length: usize,
name: &str,
) -> anyhow::Result<inkwell::values::BasicValueEnum<'ctx>> { ) -> anyhow::Result<inkwell::values::BasicValueEnum<'ctx>> {
let address = self.build_byte_swap(self.build_load(pointer, "address_pointer")?)?; let value = self.build_load(
Ok(self pointer.cast(self.integer_type(bit_length)),
.builder() &format!("load_{name}"),
.build_int_z_extend(address.into_int_value(), self.word_type(), "address_zext")? )?;
.into()) let value_extended = self.builder().build_int_z_extend(
value.into_int_value(),
self.word_type(),
&format!("zext_{name}"),
)?;
Ok(value_extended.as_basic_value_enum())
} }
/// Builds a stack load instruction. /// Builds a stack load instruction.
@@ -669,10 +684,22 @@ where
self.build_byte_swap(value) self.build_byte_swap(value)
} }
AddressSpace::Storage | AddressSpace::TransientStorage => { AddressSpace::Storage | AddressSpace::TransientStorage => {
let storage_key_value = self.builder().build_ptr_to_int(
pointer.value,
self.word_type(),
"storage_ptr_to_int",
)?;
let storage_key_pointer = self.build_alloca(self.word_type(), "storage_key");
let storage_key_pointer_casted = self.builder().build_ptr_to_int(
storage_key_pointer.value,
self.xlen_type(),
"storage_key_pointer_casted",
)?;
self.builder()
.build_store(storage_key_pointer.value, storage_key_value)?;
let storage_value_pointer = let storage_value_pointer =
self.build_alloca(self.word_type(), "storage_value_pointer"); self.build_alloca(self.word_type(), "storage_value_pointer");
self.build_store(storage_value_pointer, self.word_const(0))?;
let storage_value_length_pointer = let storage_value_length_pointer =
self.build_alloca(self.xlen_type(), "storage_value_length_pointer"); self.build_alloca(self.xlen_type(), "storage_value_length_pointer");
self.build_store( self.build_store(
@@ -683,10 +710,10 @@ where
let transient = pointer.address_space == AddressSpace::TransientStorage; let transient = pointer.address_space == AddressSpace::TransientStorage;
self.build_runtime_call( self.build_runtime_call(
revive_runtime_api::polkavm_imports::GET_STORAGE, runtime_api::imports::GET_STORAGE,
&[ &[
self.xlen_type().const_int(transient as u64, false).into(), self.xlen_type().const_int(transient as u64, false).into(),
pointer.to_int(self).into(), storage_key_pointer_casted.into(),
self.xlen_type().const_all_ones().into(), self.xlen_type().const_all_ones().into(),
storage_value_pointer.to_int(self).into(), storage_value_pointer.to_int(self).into(),
storage_value_length_pointer.to_int(self).into(), storage_value_length_pointer.to_int(self).into(),
@@ -755,6 +782,18 @@ where
self.word_type().as_basic_type_enum() self.word_type().as_basic_type_enum()
); );
let storage_key_value = self.builder().build_ptr_to_int(
pointer.value,
self.word_type(),
"storage_ptr_to_int",
)?;
let storage_key_pointer = self.build_alloca(self.word_type(), "storage_key");
let storage_key_pointer_casted = self.builder().build_ptr_to_int(
storage_key_pointer.value,
self.xlen_type(),
"storage_key_pointer_casted",
)?;
let storage_value_pointer = self.build_alloca(self.word_type(), "storage_value"); let storage_value_pointer = self.build_alloca(self.word_type(), "storage_value");
let storage_value_pointer_casted = self.builder().build_ptr_to_int( let storage_value_pointer_casted = self.builder().build_ptr_to_int(
storage_value_pointer.value, storage_value_pointer.value,
@@ -762,16 +801,18 @@ where
"storage_value_pointer_casted", "storage_value_pointer_casted",
)?; )?;
self.builder()
.build_store(storage_key_pointer.value, storage_key_value)?;
self.builder() self.builder()
.build_store(storage_value_pointer.value, value)?; .build_store(storage_value_pointer.value, value)?;
let transient = pointer.address_space == AddressSpace::TransientStorage; let transient = pointer.address_space == AddressSpace::TransientStorage;
self.build_runtime_call( self.build_runtime_call(
revive_runtime_api::polkavm_imports::SET_STORAGE, runtime_api::imports::SET_STORAGE,
&[ &[
self.xlen_type().const_int(transient as u64, false).into(), self.xlen_type().const_int(transient as u64, false).into(),
pointer.to_int(self).into(), storage_key_pointer_casted.into(),
self.xlen_type().const_all_ones().into(), self.xlen_type().const_all_ones().into(),
storage_value_pointer_casted.into(), storage_value_pointer_casted.into(),
self.integer_const(crate::polkavm::XLEN, 32).into(), self.integer_const(crate::polkavm::XLEN, 32).into(),
@@ -880,29 +921,13 @@ where
.copied() .copied()
.map(inkwell::values::BasicMetadataValueEnum::from) .map(inkwell::values::BasicMetadataValueEnum::from)
.collect::<Vec<_>>(), .collect::<Vec<_>>(),
&format!("runtime_api_{name}_return_value"), &format!("runtime API call {name}"),
) )
.unwrap() .unwrap()
.try_as_basic_value() .try_as_basic_value()
.left() .left()
} }
/// Builds a call to the runtime API `import`, where `import` is a "getter" API.
/// This means that the supplied API method just writes back a single word.
/// `import` is thus expect to have a single parameter, the 32 bytes output buffer,
/// and no return value.
pub fn build_runtime_call_to_getter(
&self,
import: &'static str,
) -> anyhow::Result<inkwell::values::BasicValueEnum<'ctx>>
where
D: Dependency + Clone,
{
let pointer = self.build_alloca_at_entry(self.word_type(), &format!("{import}_output"));
self.build_runtime_call(import, &[pointer.to_int(self).into()]);
self.build_load(pointer, import)
}
/// Builds a call. /// Builds a call.
pub fn build_call( pub fn build_call(
&self, &self,
@@ -1003,7 +1028,7 @@ where
)?; )?;
self.build_runtime_call( self.build_runtime_call(
revive_runtime_api::polkavm_imports::RETURN, runtime_api::imports::RETURN,
&[flags.into(), offset_pointer.into(), length_pointer.into()], &[flags.into(), offset_pointer.into(), length_pointer.into()],
); );
self.build_unreachable(); self.build_unreachable();
@@ -1061,20 +1086,16 @@ where
Ok(truncated) Ok(truncated)
} }
/// Build a call to PolkaVM `sbrk` for extending the heap from offset by `size`. /// Build a call to PolkaVM `sbrk` for extending the heap by `size`.
/// The allocation is aligned to 32 bytes.
///
/// This emulates the EVM linear memory until the runtime supports metered memory.
pub fn build_sbrk( pub fn build_sbrk(
&self, &self,
offset: inkwell::values::IntValue<'ctx>,
size: inkwell::values::IntValue<'ctx>, size: inkwell::values::IntValue<'ctx>,
) -> anyhow::Result<inkwell::values::PointerValue<'ctx>> { ) -> anyhow::Result<inkwell::values::PointerValue<'ctx>> {
Ok(self Ok(self
.builder() .builder()
.build_call( .build_call(
self.runtime_api_method(revive_runtime_api::polkavm_imports::SBRK), self.runtime_api_method(runtime_api::SBRK),
&[offset.into(), size.into()], &[size.into()],
"call_sbrk", "call_sbrk",
)? )?
.try_as_basic_value() .try_as_basic_value()
@@ -1083,29 +1104,14 @@ where
.into_pointer_value()) .into_pointer_value())
} }
/// Build a call to PolkaVM `msize` for querying the linear memory size. /// Call PolkaVM `sbrk` for extending the heap by `size`,
pub fn build_msize(&self) -> anyhow::Result<inkwell::values::IntValue<'ctx>> {
Ok(self
.builder()
.build_call(
self.runtime_api_method(revive_runtime_api::polkavm_imports::MEMORY_SIZE),
&[],
"call_msize",
)?
.try_as_basic_value()
.left()
.expect("sbrk returns an int")
.into_int_value())
}
/// Call PolkaVM `sbrk` for extending the heap by `offset` + `size`,
/// trapping the contract if the call failed. /// trapping the contract if the call failed.
/// Returns the end of memory pointer.
pub fn build_heap_alloc( pub fn build_heap_alloc(
&self, &self,
offset: inkwell::values::IntValue<'ctx>,
size: inkwell::values::IntValue<'ctx>, size: inkwell::values::IntValue<'ctx>,
) -> anyhow::Result<()> { ) -> anyhow::Result<inkwell::values::PointerValue<'ctx>> {
let end_of_memory = self.build_sbrk(offset, size)?; let end_of_memory = self.build_sbrk(size)?;
let return_is_nil = self.builder().build_int_compare( let return_is_nil = self.builder().build_int_compare(
inkwell::IntPredicate::EQ, inkwell::IntPredicate::EQ,
end_of_memory, end_of_memory,
@@ -1123,7 +1129,7 @@ where
self.set_basic_block(continue_block); self.set_basic_block(continue_block);
Ok(()) Ok(end_of_memory)
} }
/// Returns a pointer to `offset` into the heap, allocating /// Returns a pointer to `offset` into the heap, allocating
@@ -1138,12 +1144,40 @@ where
assert_eq!(offset.get_type(), self.xlen_type()); assert_eq!(offset.get_type(), self.xlen_type());
assert_eq!(length.get_type(), self.xlen_type()); assert_eq!(length.get_type(), self.xlen_type());
self.build_heap_alloc(offset, length)?;
let heap_start = self let heap_start = self
.get_global(crate::polkavm::GLOBAL_HEAP_MEMORY_POINTER)? .get_global(crate::polkavm::GLOBAL_HEAP_MEMORY_POINTER)?
.value .value
.as_pointer_value(); .as_pointer_value();
let heap_end = self.build_sbrk(self.integer_const(crate::polkavm::XLEN, 0))?;
let value_end = self.build_gep(
Pointer::new(self.byte_type(), AddressSpace::Stack, heap_start),
&[self.builder().build_int_nuw_add(offset, length, "end")?],
self.byte_type(),
"heap_end_gep",
);
let is_out_of_bounds = self.builder().build_int_compare(
inkwell::IntPredicate::UGT,
value_end.value,
heap_end,
"is_value_overflowing_heap",
)?;
let out_of_bounds_block = self.append_basic_block("heap_offset_out_of_bounds");
let heap_offset_block = self.append_basic_block("build_heap_pointer");
self.build_conditional_branch(is_out_of_bounds, out_of_bounds_block, heap_offset_block)?;
self.set_basic_block(out_of_bounds_block);
let size = self.builder().build_int_nuw_sub(
self.builder()
.build_ptr_to_int(value_end.value, self.xlen_type(), "value_end")?,
self.builder()
.build_ptr_to_int(heap_end, self.xlen_type(), "heap_end")?,
"heap_alloc_size",
)?;
self.build_heap_alloc(size)?;
self.build_unconditional_branch(heap_offset_block);
self.set_basic_block(heap_offset_block);
Ok(self.build_gep( Ok(self.build_gep(
Pointer::new(self.byte_type(), AddressSpace::Stack, heap_start), Pointer::new(self.byte_type(), AddressSpace::Stack, heap_start),
&[offset], &[offset],
@@ -1264,11 +1298,12 @@ where
&self, &self,
argument_types: Vec<T>, argument_types: Vec<T>,
return_values_size: usize, return_values_size: usize,
is_near_call_abi: bool,
) -> inkwell::types::FunctionType<'ctx> ) -> inkwell::types::FunctionType<'ctx>
where where
T: BasicType<'ctx>, T: BasicType<'ctx>,
{ {
let argument_types: Vec<inkwell::types::BasicMetadataTypeEnum> = argument_types let mut argument_types: Vec<inkwell::types::BasicMetadataTypeEnum> = argument_types
.as_slice() .as_slice()
.iter() .iter()
.map(T::as_basic_type_enum) .map(T::as_basic_type_enum)
@@ -1280,6 +1315,11 @@ where
.void_type() .void_type()
.fn_type(argument_types.as_slice(), false), .fn_type(argument_types.as_slice(), false),
1 => self.word_type().fn_type(argument_types.as_slice(), false), 1 => self.word_type().fn_type(argument_types.as_slice(), false),
_size if is_near_call_abi && self.is_system_mode() => {
let return_type = self.llvm().ptr_type(AddressSpace::Stack.into());
argument_types.insert(0, return_type.as_basic_type_enum().into());
return_type.fn_type(argument_types.as_slice(), false)
}
size => self size => self
.structure_type(vec![self.word_type().as_basic_type_enum(); size].as_slice()) .structure_type(vec![self.word_type().as_basic_type_enum(); size].as_slice())
.fn_type(argument_types.as_slice(), false), .fn_type(argument_types.as_slice(), false),
@@ -1472,4 +1512,12 @@ where
anyhow::bail!("The immutable size data is not available"); anyhow::bail!("The immutable size data is not available");
} }
} }
/// Whether the system mode is enabled.
pub fn is_system_mode(&self) -> bool {
self.yul_data
.as_ref()
.map(|data| data.is_system_mode())
.unwrap_or_default()
}
} }
@@ -17,7 +17,7 @@ impl SolidityData {
Self::default() Self::default()
} }
/// Returns the current size of immutable values in the contract. /// Returns the current number of immutables values in the contract.
pub fn immutables_size(&self) -> usize { pub fn immutables_size(&self) -> usize {
self.immutables.len() * revive_common::BYTE_LENGTH_WORD self.immutables.len() * revive_common::BYTE_LENGTH_WORD
} }
@@ -8,12 +8,28 @@ use num::Zero;
/// Describes some data that is only relevant to Yul. /// Describes some data that is only relevant to Yul.
#[derive(Debug, Default)] #[derive(Debug, Default)]
pub struct YulData { pub struct YulData {
/// The system mode flag.
/// The call simulations only work if this mode is enabled.
is_system_mode: bool,
/// The list of constant arrays in the code section. /// The list of constant arrays in the code section.
/// It is a temporary storage used until the finalization method is called. /// It is a temporary storage used until the finalization method is called.
const_arrays: BTreeMap<u8, Vec<num::BigUint>>, const_arrays: BTreeMap<u8, Vec<num::BigUint>>,
} }
impl YulData { impl YulData {
/// A shortcut constructor.
pub fn new(is_system_mode: bool) -> Self {
Self {
is_system_mode,
const_arrays: BTreeMap::new(),
}
}
/// Whether the system mode is enabled.
pub fn is_system_mode(&self) -> bool {
self.is_system_mode
}
/// Declares a temporary constant array representation. /// Declares a temporary constant array representation.
pub fn const_array_declare(&mut self, index: u8, size: u16) -> anyhow::Result<()> { pub fn const_array_declare(&mut self, index: u8, size: u16) -> anyhow::Result<()> {
if self.const_arrays.contains_key(&index) { if self.const_arrays.contains_key(&index) {
+41 -43
View File
@@ -5,15 +5,18 @@ use inkwell::values::BasicValue;
use crate::polkavm::context::argument::Argument; use crate::polkavm::context::argument::Argument;
use crate::polkavm::context::Context; use crate::polkavm::context::Context;
use crate::polkavm::Dependency; use crate::polkavm::Dependency;
use crate::polkavm_const::runtime_api;
const STATIC_CALL_FLAG: u32 = 0b0001_0000; static STATIC_CALL_FLAG: u32 = 0b0001_0000;
const REENTRANT_CALL_FLAG: u32 = 0b0000_1000;
/// Translates a contract call. /// Translates a contract call.
///
/// If the `simulation_address` is specified, the call is
/// substituted with another instruction according to the specification.
#[allow(clippy::too_many_arguments)] #[allow(clippy::too_many_arguments)]
pub fn call<'ctx, D>( pub fn call<'ctx, D>(
context: &mut Context<'ctx, D>, context: &mut Context<'ctx, D>,
_gas: inkwell::values::IntValue<'ctx>, gas: inkwell::values::IntValue<'ctx>,
address: inkwell::values::IntValue<'ctx>, address: inkwell::values::IntValue<'ctx>,
value: Option<inkwell::values::IntValue<'ctx>>, value: Option<inkwell::values::IntValue<'ctx>>,
input_offset: inkwell::values::IntValue<'ctx>, input_offset: inkwell::values::IntValue<'ctx>,
@@ -26,64 +29,59 @@ pub fn call<'ctx, D>(
where where
D: Dependency + Clone, D: Dependency + Clone,
{ {
let address_pointer = context.build_address_argument_store(address)?; let address_pointer = context.build_alloca(context.word_type(), "address_ptr");
context.build_store(address_pointer, address)?;
let value = value.unwrap_or_else(|| context.word_const(0)); let value_pointer = if let Some(value) = value {
let value_pointer = context.build_alloca_at_entry(context.word_type(), "value_pointer"); let value_pointer = context.build_alloca(context.value_type(), "value");
context.build_store(value_pointer, value)?; context.build_store(value_pointer, value)?;
value_pointer
} else {
context.sentinel_pointer()
};
let input_offset = context.safe_truncate_int_to_xlen(input_offset)?; let input_offset = context.safe_truncate_int_to_xlen(input_offset)?;
let input_length = context.safe_truncate_int_to_xlen(input_length)?; let input_length = context.safe_truncate_int_to_xlen(input_length)?;
let output_offset = context.safe_truncate_int_to_xlen(output_offset)?; let output_offset = context.safe_truncate_int_to_xlen(output_offset)?;
let output_length = context.safe_truncate_int_to_xlen(output_length)?; let output_length = context.safe_truncate_int_to_xlen(output_length)?;
// TODO: What to supply here? Is there a weight to gas? let gas = context
let _gas = context
.builder() .builder()
.build_int_truncate(_gas, context.integer_type(64), "gas")?; .build_int_truncate(gas, context.integer_type(64), "gas")?;
let flags = if static_call { STATIC_CALL_FLAG } else { 0 };
let input_pointer = context.build_heap_gep(input_offset, input_length)?; let input_pointer = context.build_heap_gep(input_offset, input_length)?;
let output_pointer = context.build_heap_gep(output_offset, output_length)?; let output_pointer = context.build_heap_gep(output_offset, output_length)?;
let output_length_pointer = context.build_alloca_at_entry(context.xlen_type(), "output_length"); let output_length_pointer = context.get_global(crate::polkavm::GLOBAL_RETURN_DATA_SIZE)?;
context.build_store(output_length_pointer, output_length)?; context.build_store(output_length_pointer.into(), output_length)?;
let flags = if static_call { let argument_pointer = revive_runtime_api::calling_convention::Spill::new(
REENTRANT_CALL_FLAG | STATIC_CALL_FLAG
} else {
REENTRANT_CALL_FLAG
};
let flags = context.xlen_type().const_int(flags as u64, false);
let argument_type = revive_runtime_api::calling_convention::call(context.llvm());
let argument_pointer = context.build_alloca_at_entry(argument_type, "call_arguments");
let arguments = &[
flags.as_basic_value_enum(),
address_pointer.value.as_basic_value_enum(),
context.integer_const(64, 0).as_basic_value_enum(),
context.integer_const(64, 0).as_basic_value_enum(),
context.sentinel_pointer().value.as_basic_value_enum(),
value_pointer.value.as_basic_value_enum(),
input_pointer.value.as_basic_value_enum(),
input_length.as_basic_value_enum(),
output_pointer.value.as_basic_value_enum(),
output_length_pointer.value.as_basic_value_enum(),
];
revive_runtime_api::calling_convention::spill(
context.builder(), context.builder(),
argument_pointer.value, revive_runtime_api::calling_convention::call(context.llvm()),
argument_type, "call_arguments",
arguments, )?
)?; .next(context.xlen_type().const_int(flags as u64, false))?
.next(address_pointer.value)?
.next(gas)?
.skip()
.next(context.sentinel_pointer().value)?
.next(value_pointer.value)?
.next(input_pointer.value)?
.next(input_length)?
.next(output_pointer.value)?
.next(output_length_pointer.value)?
.done();
let name = revive_runtime_api::polkavm_imports::CALL; let name = runtime_api::imports::CALL;
let argument_pointer = context.builder().build_ptr_to_int( let arguments = context.builder().build_ptr_to_int(
argument_pointer.value, argument_pointer,
context.xlen_type(), context.xlen_type(),
"call_argument_pointer", "argument_pointer",
)?; )?;
let success = context let success = context
.build_runtime_call(name, &[argument_pointer.into()]) .build_runtime_call(name, &[arguments.into()])
.unwrap_or_else(|| panic!("{name} should return a value")) .unwrap_or_else(|| panic!("{name} should return a value"))
.into_int_value(); .into_int_value();
+87 -26
View File
@@ -4,6 +4,7 @@ use inkwell::values::BasicValue;
use crate::polkavm::context::Context; use crate::polkavm::context::Context;
use crate::polkavm::Dependency; use crate::polkavm::Dependency;
use crate::polkavm_const::runtime_api;
/// Translates the `gas_limit` instruction. /// Translates the `gas_limit` instruction.
pub fn gas_limit<'ctx, D>( pub fn gas_limit<'ctx, D>(
@@ -27,19 +28,12 @@ where
/// Translates the `tx.origin` instruction. /// Translates the `tx.origin` instruction.
pub fn origin<'ctx, D>( pub fn origin<'ctx, D>(
context: &mut Context<'ctx, D>, _context: &mut Context<'ctx, D>,
) -> anyhow::Result<inkwell::values::BasicValueEnum<'ctx>> ) -> anyhow::Result<inkwell::values::BasicValueEnum<'ctx>>
where where
D: Dependency + Clone, D: Dependency + Clone,
{ {
let address_type = context.integer_type(revive_common::BIT_LENGTH_ETH_ADDRESS); todo!()
let address_pointer = context.build_alloca_at_entry(address_type, "origin_address");
context.build_store(address_pointer, address_type.const_zero())?;
context.build_runtime_call(
revive_runtime_api::polkavm_imports::ORIGIN,
&[address_pointer.to_int(context).into()],
);
context.build_load_address(address_pointer)
} }
/// Translates the `chain_id` instruction. /// Translates the `chain_id` instruction.
@@ -49,7 +43,7 @@ pub fn chain_id<'ctx, D>(
where where
D: Dependency + Clone, D: Dependency + Clone,
{ {
context.build_runtime_call_to_getter(revive_runtime_api::polkavm_imports::CHAIN_ID) Ok(context.word_const(0).as_basic_value_enum())
} }
/// Translates the `block_number` instruction. /// Translates the `block_number` instruction.
@@ -59,7 +53,22 @@ pub fn block_number<'ctx, D>(
where where
D: Dependency + Clone, D: Dependency + Clone,
{ {
context.build_runtime_call_to_getter(revive_runtime_api::polkavm_imports::BLOCK_NUMBER) let (output_pointer, output_length_pointer) = context.build_stack_parameter(
revive_common::BIT_LENGTH_BLOCK_NUMBER,
"block_timestamp_output",
);
context.build_runtime_call(
runtime_api::imports::BLOCK_NUMBER,
&[
output_pointer.to_int(context).into(),
output_length_pointer.to_int(context).into(),
],
);
context.build_load_word(
output_pointer,
revive_common::BIT_LENGTH_BLOCK_NUMBER,
"block_number",
)
} }
/// Translates the `block_timestamp` instruction. /// Translates the `block_timestamp` instruction.
@@ -69,7 +78,22 @@ pub fn block_timestamp<'ctx, D>(
where where
D: Dependency + Clone, D: Dependency + Clone,
{ {
context.build_runtime_call_to_getter(revive_runtime_api::polkavm_imports::NOW) let (output_pointer, output_length_pointer) = context.build_stack_parameter(
revive_common::BIT_LENGTH_BLOCK_TIMESTAMP,
"block_timestamp_output",
);
context.build_runtime_call(
runtime_api::imports::NOW,
&[
output_pointer.to_int(context).into(),
output_length_pointer.to_int(context).into(),
],
);
context.build_load_word(
output_pointer,
revive_common::BIT_LENGTH_BLOCK_TIMESTAMP,
"block_timestamp",
)
} }
/// Translates the `block_hash` instruction. /// Translates the `block_hash` instruction.
@@ -113,6 +137,33 @@ where
Ok(context.word_const(0).as_basic_value_enum()) Ok(context.word_const(0).as_basic_value_enum())
} }
/// Translates the `msize` instruction.
pub fn msize<'ctx, D>(
context: &mut Context<'ctx, D>,
) -> anyhow::Result<inkwell::values::BasicValueEnum<'ctx>>
where
D: Dependency + Clone,
{
let heap_end = context.build_sbrk(context.xlen_type().const_zero())?;
let heap_start = context
.get_global(crate::polkavm::GLOBAL_HEAP_MEMORY_POINTER)?
.value
.as_pointer_value();
let heap_size = context.builder().build_int_nuw_sub(
context
.builder()
.build_ptr_to_int(heap_end, context.xlen_type(), "heap_end")?,
context
.builder()
.build_ptr_to_int(heap_start, context.xlen_type(), "heap_start")?,
"heap_size",
)?;
Ok(context
.builder()
.build_int_z_extend(heap_size, context.word_type(), "heap_size_extended")?
.as_basic_value_enum())
}
/// Translates the `address` instruction. /// Translates the `address` instruction.
pub fn address<'ctx, D>( pub fn address<'ctx, D>(
context: &mut Context<'ctx, D>, context: &mut Context<'ctx, D>,
@@ -120,15 +171,20 @@ pub fn address<'ctx, D>(
where where
D: Dependency + Clone, D: Dependency + Clone,
{ {
let pointer = context.build_alloca_at_entry( let (output_pointer, output_length_pointer) =
context.integer_type(revive_common::BIT_LENGTH_ETH_ADDRESS), context.build_stack_parameter(revive_common::BIT_LENGTH_ETH_ADDRESS, "address_output");
"address_output",
);
context.build_runtime_call( context.build_runtime_call(
revive_runtime_api::polkavm_imports::ADDRESS, runtime_api::imports::ADDRESS,
&[pointer.to_int(context).into()], &[
output_pointer.to_int(context).into(),
output_length_pointer.to_int(context).into(),
],
); );
context.build_load_address(pointer) let value = context.build_byte_swap(context.build_load(output_pointer, "address")?)?;
Ok(context
.builder()
.build_int_z_extend(value.into_int_value(), context.word_type(), "address_zext")?
.into())
} }
/// Translates the `caller` instruction. /// Translates the `caller` instruction.
@@ -138,13 +194,18 @@ pub fn caller<'ctx, D>(
where where
D: Dependency + Clone, D: Dependency + Clone,
{ {
let pointer = context.build_alloca_at_entry( let (output_pointer, output_length_pointer) =
context.integer_type(revive_common::BIT_LENGTH_ETH_ADDRESS), context.build_stack_parameter(revive_common::BIT_LENGTH_ETH_ADDRESS, "caller_output");
"address_output",
);
context.build_runtime_call( context.build_runtime_call(
revive_runtime_api::polkavm_imports::CALLER, runtime_api::imports::CALLER,
&[pointer.to_int(context).into()], &[
output_pointer.to_int(context).into(),
output_length_pointer.to_int(context).into(),
],
); );
context.build_load_address(pointer) let value = context.build_byte_swap(context.build_load(output_pointer, "caller")?)?;
Ok(context
.builder()
.build_int_z_extend(value.into_int_value(), context.word_type(), "caller_zext")?
.into())
} }
+56 -55
View File
@@ -7,15 +7,28 @@ use crate::polkavm::context::argument::Argument;
use crate::polkavm::context::code_type::CodeType; use crate::polkavm::context::code_type::CodeType;
use crate::polkavm::context::Context; use crate::polkavm::context::Context;
use crate::polkavm::Dependency; use crate::polkavm::Dependency;
use crate::polkavm_const::runtime_api;
/// Translates the contract `create` and `create2` instruction. /// Translates the contract `create` instruction.
/// /// The instruction is simulated by a call to a system contract.
/// A `salt` value of `None` is equivalent to `create1`.
pub fn create<'ctx, D>( pub fn create<'ctx, D>(
context: &mut Context<'ctx, D>, context: &mut Context<'ctx, D>,
value: inkwell::values::IntValue<'ctx>, value: inkwell::values::IntValue<'ctx>,
input_offset: inkwell::values::IntValue<'ctx>, input_offset: inkwell::values::IntValue<'ctx>,
input_length: inkwell::values::IntValue<'ctx>, input_length: inkwell::values::IntValue<'ctx>,
) -> anyhow::Result<inkwell::values::BasicValueEnum<'ctx>>
where
D: Dependency + Clone,
{
self::create2(context, value, input_offset, input_length, None)
}
/// Translates the contract `create2` instruction.
pub fn create2<'ctx, D>(
context: &mut Context<'ctx, D>,
value: inkwell::values::IntValue<'ctx>,
input_offset: inkwell::values::IntValue<'ctx>,
input_length: inkwell::values::IntValue<'ctx>,
salt: Option<inkwell::values::IntValue<'ctx>>, salt: Option<inkwell::values::IntValue<'ctx>>,
) -> anyhow::Result<inkwell::values::BasicValueEnum<'ctx>> ) -> anyhow::Result<inkwell::values::BasicValueEnum<'ctx>>
where where
@@ -24,6 +37,9 @@ where
let input_offset = context.safe_truncate_int_to_xlen(input_offset)?; let input_offset = context.safe_truncate_int_to_xlen(input_offset)?;
let input_length = context.safe_truncate_int_to_xlen(input_length)?; let input_length = context.safe_truncate_int_to_xlen(input_length)?;
let value_pointer = context.build_alloca(context.value_type(), "value");
context.build_store(value_pointer, value)?;
let code_hash_pointer = context.build_heap_gep(input_offset, input_length)?; let code_hash_pointer = context.build_heap_gep(input_offset, input_length)?;
let input_data_pointer = context.build_gep( let input_data_pointer = context.build_gep(
@@ -32,68 +48,53 @@ where
.xlen_type() .xlen_type()
.const_int(revive_common::BYTE_LENGTH_WORD as u64, false)], .const_int(revive_common::BYTE_LENGTH_WORD as u64, false)],
context.byte_type(), context.byte_type(),
"input_ptr_parameter_offset", "value_ptr_parameter_offset",
); );
let value_pointer = context.build_alloca_at_entry(context.value_type(), "transferred_value"); let salt_pointer = context.build_alloca(context.word_type(), "salt");
context.build_store(value_pointer, value)?; context.build_store(salt_pointer, salt.unwrap_or_else(|| context.word_const(0)))?;
let salt_pointer = match salt { let (address_pointer, address_length_pointer) =
Some(salt) => { context.build_stack_parameter(revive_common::BIT_LENGTH_ETH_ADDRESS, "address_pointer");
let salt_pointer = context.build_alloca_at_entry(context.word_type(), "salt_pointer");
context.build_store(salt_pointer, salt)?;
salt_pointer
}
None => context.sentinel_pointer(),
};
let address_pointer = context.build_alloca_at_entry(
context.integer_type(revive_common::BIT_LENGTH_ETH_ADDRESS),
"address_pointer",
);
context.build_store(address_pointer, context.word_const(0))?; context.build_store(address_pointer, context.word_const(0))?;
let argument_type = revive_runtime_api::calling_convention::instantiate(context.llvm()); let argument_pointer = revive_runtime_api::calling_convention::Spill::new(
let argument_pointer = context.build_alloca_at_entry(argument_type, "instantiate_arguments");
let arguments = &[
code_hash_pointer.value.as_basic_value_enum(),
context.integer_const(64, 0).as_basic_value_enum(),
context.integer_const(64, 0).as_basic_value_enum(),
context.sentinel_pointer().value.as_basic_value_enum(),
value_pointer.value.as_basic_value_enum(),
input_data_pointer.value.as_basic_value_enum(),
input_length.as_basic_value_enum(),
address_pointer.value.as_basic_value_enum(),
context.sentinel_pointer().value.as_basic_value_enum(),
context.sentinel_pointer().value.as_basic_value_enum(),
salt_pointer.value.as_basic_value_enum(),
];
revive_runtime_api::calling_convention::spill(
context.builder(), context.builder(),
argument_pointer.value, revive_runtime_api::calling_convention::instantiate(context.llvm()),
argument_type, "create2_arguments",
arguments, )?
)?; .next(code_hash_pointer.value)?
.skip()
.skip()
.next(context.sentinel_pointer().value)?
.next(value_pointer.value)?
.next(input_data_pointer.value)?
.next(input_length)?
.next(address_pointer.value)?
.next(address_length_pointer.value)?
.next(context.sentinel_pointer().value)?
.next(context.sentinel_pointer().value)?
.next(salt_pointer.value)?
.next(
context
.xlen_type()
.const_int(revive_common::BYTE_LENGTH_WORD as u64, false),
)?
.done();
let argument_pointer = context.builder().build_ptr_to_int(
argument_pointer.value,
context.xlen_type(),
"instantiate_argument_pointer",
)?;
context.build_runtime_call( context.build_runtime_call(
revive_runtime_api::polkavm_imports::INSTANTIATE, runtime_api::imports::INSTANTIATE,
&[argument_pointer.into()], &[context
.builder()
.build_ptr_to_int(argument_pointer, context.xlen_type(), "argument_pointer")?
.into()],
); );
let address = context.build_byte_swap(context.build_load(address_pointer, "address")?)?; context.build_load_word(
Ok(context address_pointer,
.builder() revive_common::BIT_LENGTH_ETH_ADDRESS,
.build_int_z_extend( "address",
address.into_int_value(), )
context.word_type(),
"address_zext",
)?
.into())
} }
/// Translates the contract hash instruction, which is actually used to set the hash of the contract /// Translates the contract hash instruction, which is actually used to set the hash of the contract
@@ -2,6 +2,7 @@
use crate::polkavm::context::Context; use crate::polkavm::context::Context;
use crate::polkavm::Dependency; use crate::polkavm::Dependency;
use crate::polkavm_const::runtime_api;
/// Translates the `sha3` instruction. /// Translates the `sha3` instruction.
pub fn sha3<'ctx, D>( pub fn sha3<'ctx, D>(
@@ -18,7 +19,7 @@ where
let output_pointer = context.build_alloca(context.word_type(), "output_pointer"); let output_pointer = context.build_alloca(context.word_type(), "output_pointer");
context.build_runtime_call( context.build_runtime_call(
revive_runtime_api::polkavm_imports::HASH_KECCAK_256, runtime_api::imports::HASH_KECCAK_256,
&[ &[
input_pointer.to_int(context).into(), input_pointer.to_int(context).into(),
length_casted.into(), length_casted.into(),
@@ -4,6 +4,7 @@ use inkwell::values::BasicValue;
use crate::polkavm::context::Context; use crate::polkavm::context::Context;
use crate::polkavm::Dependency; use crate::polkavm::Dependency;
use crate::polkavm_const::runtime_api;
/// Translates the `gas` instruction. /// Translates the `gas` instruction.
pub fn gas<'ctx, D>( pub fn gas<'ctx, D>(
@@ -22,13 +23,20 @@ pub fn value<'ctx, D>(
where where
D: Dependency + Clone, D: Dependency + Clone,
{ {
let output_pointer = context.build_alloca(context.value_type(), "value_transferred"); let (output_pointer, output_length_pointer) =
context.build_store(output_pointer, context.word_const(0))?; context.build_stack_parameter(revive_common::BIT_LENGTH_VALUE, "value_transferred_output");
context.build_runtime_call( context.build_runtime_call(
revive_runtime_api::polkavm_imports::VALUE_TRANSFERRED, runtime_api::imports::VALUE_TRANSFERRED,
&[output_pointer.to_int(context).into()], &[
output_pointer.to_int(context).into(),
output_length_pointer.to_int(context).into(),
],
); );
context.build_load(output_pointer, "value_transferred") context.build_load_word(
output_pointer,
revive_common::BIT_LENGTH_VALUE,
"value_transferred",
)
} }
/// Translates the `balance` instructions. /// Translates the `balance` instructions.
@@ -39,40 +47,24 @@ pub fn balance<'ctx, D>(
where where
D: Dependency + Clone, D: Dependency + Clone,
{ {
let address_pointer = context.build_address_argument_store(address)?;
let balance_pointer = context.build_alloca(context.word_type(), "balance_pointer"); let balance_pointer = context.build_alloca(context.word_type(), "balance_pointer");
let address_pointer = context.build_alloca(context.word_type(), "address_pointer");
context.build_store(address_pointer, address)?;
let balance = context.builder().build_ptr_to_int( let balance = context.builder().build_ptr_to_int(
balance_pointer.value, balance_pointer.value,
context.xlen_type(), context.xlen_type(),
"balance", "balance",
)?; )?;
let address = context.builder().build_ptr_to_int(
context.build_runtime_call( address_pointer.value,
revive_runtime_api::polkavm_imports::BALANCE_OF,
&[address_pointer.to_int(context).into(), balance.into()],
);
context.build_load(balance_pointer, "balance")
}
/// Translates the `selfbalance` instructions.
pub fn self_balance<'ctx, D>(
context: &mut Context<'ctx, D>,
) -> anyhow::Result<inkwell::values::BasicValueEnum<'ctx>>
where
D: Dependency + Clone,
{
let balance_pointer = context.build_alloca(context.word_type(), "balance_pointer");
let balance = context.builder().build_ptr_to_int(
balance_pointer.value,
context.xlen_type(), context.xlen_type(),
"balance", "address",
)?; )?;
context.build_runtime_call( context.build_runtime_call(
revive_runtime_api::polkavm_imports::BALANCE, runtime_api::imports::BALANCE,
&[balance.into()], &[address.into(), balance.into()],
); );
context.build_load(balance_pointer, "balance") context.build_load(balance_pointer, "balance")
+3 -7
View File
@@ -4,6 +4,7 @@ use inkwell::values::BasicValue;
use crate::polkavm::context::Context; use crate::polkavm::context::Context;
use crate::polkavm::Dependency; use crate::polkavm::Dependency;
use crate::polkavm_const::runtime_api;
/// Translates a log or event call. /// Translates a log or event call.
/// ///
@@ -42,7 +43,6 @@ where
context.byte_type().array_type(topics_buffer_size as u32), context.byte_type().array_type(topics_buffer_size as u32),
"topics_buffer", "topics_buffer",
); );
for (n, topic) in topics.iter().enumerate() { for (n, topic) in topics.iter().enumerate() {
let topic_buffer_offset = context let topic_buffer_offset = context
.xlen_type() .xlen_type()
@@ -57,7 +57,6 @@ where
context.build_byte_swap(topic.as_basic_value_enum())?, context.build_byte_swap(topic.as_basic_value_enum())?,
)?; )?;
} }
[ [
context context
.builder() .builder()
@@ -69,17 +68,14 @@ where
.as_basic_value_enum(), .as_basic_value_enum(),
context context
.xlen_type() .xlen_type()
.const_int(topics.len() as u64, false) .const_int(topics_buffer_size as u64, false)
.as_basic_value_enum(), .as_basic_value_enum(),
input_pointer.as_basic_value_enum(), input_pointer.as_basic_value_enum(),
input_length.as_basic_value_enum(), input_length.as_basic_value_enum(),
] ]
}; };
let _ = context.build_runtime_call( let _ = context.build_runtime_call(runtime_api::imports::DEPOSIT_EVENT, &arguments);
revive_runtime_api::polkavm_imports::DEPOSIT_EVENT,
&arguments,
);
Ok(()) Ok(())
} }
+29 -37
View File
@@ -1,7 +1,10 @@
//! Translates the external code operations. //! Translates the external code operations.
use inkwell::values::BasicValue;
use crate::polkavm::context::Context; use crate::polkavm::context::Context;
use crate::polkavm::Dependency; use crate::polkavm::Dependency;
use crate::polkavm_const::runtime_api;
/// Translates the `extcodesize` instruction if `address` is `Some`. /// Translates the `extcodesize` instruction if `address` is `Some`.
/// Otherwise, translates the `codesize` instruction. /// Otherwise, translates the `codesize` instruction.
@@ -12,52 +15,41 @@ pub fn size<'ctx, D>(
where where
D: Dependency + Clone, D: Dependency + Clone,
{ {
let address = match address { let address_pointer = match address {
Some(address) => address, Some(address) => {
None => super::context::address(context)?.into_int_value(), let address_pointer = context.build_alloca(context.word_type(), "value");
context.build_store(address_pointer, address)?;
address_pointer
}
None => context.sentinel_pointer(),
}; };
let address_pointer = context.build_address_argument_store(address)?; let address_pointer_casted = context.builder().build_ptr_to_int(
let output_pointer = context.build_alloca_at_entry(context.word_type(), "output_pointer"); address_pointer.value,
context.xlen_type(),
"address_pointer",
)?;
let value = context
.build_runtime_call(
runtime_api::imports::CODE_SIZE,
&[address_pointer_casted.into()],
)
.unwrap_or_else(|| panic!("{} should return a value", runtime_api::imports::CODE_SIZE))
.into_int_value();
context.build_runtime_call( Ok(context
revive_runtime_api::polkavm_imports::CODE_SIZE, .builder()
&[ .build_int_z_extend(value, context.word_type(), "extcodesize")?
address_pointer.to_int(context).into(), .as_basic_value_enum())
output_pointer.to_int(context).into(),
],
);
context.build_load(output_pointer, "code_size")
} }
/// Translates the `extcodehash` instruction. /// Translates the `extcodehash` instruction.
pub fn hash<'ctx, D>( pub fn hash<'ctx, D>(
context: &mut Context<'ctx, D>, _context: &mut Context<'ctx, D>,
address: inkwell::values::IntValue<'ctx>, _address: inkwell::values::IntValue<'ctx>,
) -> anyhow::Result<inkwell::values::BasicValueEnum<'ctx>> ) -> anyhow::Result<inkwell::values::BasicValueEnum<'ctx>>
where where
D: Dependency + Clone, D: Dependency + Clone,
{ {
let address_type = context.integer_type(revive_common::BIT_LENGTH_ETH_ADDRESS); todo!()
let address_pointer = context.build_alloca_at_entry(address_type, "address_pointer");
let address_truncated =
context
.builder()
.build_int_truncate(address, address_type, "address_truncated")?;
let address_swapped = context.build_byte_swap(address_truncated.into())?;
context.build_store(address_pointer, address_swapped)?;
let extcodehash_pointer =
context.build_alloca_at_entry(context.word_type(), "extcodehash_pointer");
context.build_runtime_call(
revive_runtime_api::polkavm_imports::CODE_HASH,
&[
address_pointer.to_int(context).into(),
extcodehash_pointer.to_int(context).into(),
],
);
context.build_byte_swap(context.build_load(extcodehash_pointer, "extcodehash_value")?)
} }
@@ -1,19 +1,14 @@
//! Translates the contract immutable operations. //! Translates the contract immutable operations.
use inkwell::types::BasicType;
use crate::polkavm::context::address_space::AddressSpace; use crate::polkavm::context::address_space::AddressSpace;
use crate::polkavm::context::code_type::CodeType; use crate::polkavm::context::code_type::CodeType;
use crate::polkavm::context::function::runtime;
use crate::polkavm::context::pointer::Pointer; use crate::polkavm::context::pointer::Pointer;
use crate::polkavm::context::Context; use crate::polkavm::context::Context;
use crate::polkavm::Dependency; use crate::polkavm::Dependency;
/// Translates the contract immutable load. /// Translates the contract immutable load.
/// /// In the deploy code the values are read from the auxiliary heap.
/// In deploy code the values are read from the stack. /// In the runtime code they are requested from the system contract.
///
/// In runtime code they are loaded lazily with the `get_immutable_data` syscall.
pub fn load<'ctx, D>( pub fn load<'ctx, D>(
context: &mut Context<'ctx, D>, context: &mut Context<'ctx, D>,
index: inkwell::values::IntValue<'ctx>, index: inkwell::values::IntValue<'ctx>,
@@ -25,27 +20,38 @@ where
None => { None => {
anyhow::bail!("Immutables are not available if the contract part is undefined"); anyhow::bail!("Immutables are not available if the contract part is undefined");
} }
Some(CodeType::Deploy) => load_from_memory(context, index), Some(CodeType::Deploy) => {
Some(CodeType::Runtime) => { let index_double = context.builder().build_int_mul(
context.build_call( index,
context context.word_const(2),
.get_function(runtime::FUNCTION_LOAD_IMMUTABLE_DATA) "immutable_load_index_double",
.expect("is always declared for runtime code") )?;
.borrow() let offset_absolute = context.builder().build_int_add(
.declaration(), index_double,
&[], context.word_const(
runtime::FUNCTION_LOAD_IMMUTABLE_DATA, crate::polkavm::HEAP_AUX_OFFSET_CONSTRUCTOR_RETURN_DATA
+ (3 * revive_common::BYTE_LENGTH_WORD) as u64,
),
"immutable_offset_absolute",
)?;
let immutable_pointer = Pointer::new_with_offset(
context,
AddressSpace::default(),
context.word_type(),
offset_absolute,
"immutable_pointer",
); );
load_from_memory(context, index) context.build_load(immutable_pointer, "immutable_value")
}
Some(CodeType::Runtime) => {
todo!()
} }
} }
} }
/// Translates the contract immutable store. /// Translates the contract immutable store.
/// /// In the deploy code the values are written to the auxiliary heap at the predefined offset,
/// In deploy code the values are written to the stack at the predefined offset, /// being prepared for returning to the system contract for saving.
/// being prepared for storing them using the `set_immutable_data` syscall.
///
/// Ignored in the runtime code. /// Ignored in the runtime code.
pub fn store<'ctx, D>( pub fn store<'ctx, D>(
context: &mut Context<'ctx, D>, context: &mut Context<'ctx, D>,
@@ -60,48 +66,46 @@ where
anyhow::bail!("Immutables are not available if the contract part is undefined"); anyhow::bail!("Immutables are not available if the contract part is undefined");
} }
Some(CodeType::Deploy) => { Some(CodeType::Deploy) => {
let immutable_data_pointer = context let index_double = context.builder().build_int_mul(
.get_global(revive_runtime_api::immutable_data::GLOBAL_IMMUTABLE_DATA_POINTER)? index,
.value context.word_const(2),
.as_pointer_value(); "immutable_load_index_double",
let immutable_pointer = context.build_gep( )?;
Pointer::new( let index_offset_absolute = context.builder().build_int_add(
context.word_type(), index_double,
AddressSpace::Stack, context.word_const(
immutable_data_pointer, crate::polkavm::HEAP_AUX_OFFSET_CONSTRUCTOR_RETURN_DATA
+ (2 * revive_common::BYTE_LENGTH_WORD) as u64,
), ),
&[index], "index_offset_absolute",
context.word_type().as_basic_type_enum(), )?;
"immutable_variable_pointer", let index_offset_pointer = Pointer::new_with_offset(
context,
AddressSpace::default(),
context.word_type(),
index_offset_absolute,
"immutable_index_pointer",
); );
context.build_store(immutable_pointer, value) context.build_store(index_offset_pointer, index)?;
let value_offset_absolute = context.builder().build_int_add(
index_offset_absolute,
context.word_const(revive_common::BYTE_LENGTH_WORD as u64),
"value_offset_absolute",
)?;
let value_offset_pointer = Pointer::new_with_offset(
context,
AddressSpace::default(),
context.word_type(),
value_offset_absolute,
"immutable_value_pointer",
);
context.build_store(value_offset_pointer, value)?;
Ok(())
} }
Some(CodeType::Runtime) => { Some(CodeType::Runtime) => {
anyhow::bail!("Immutable writes are not available in the runtime code"); anyhow::bail!("Immutable writes are not available in the runtime code");
} }
} }
} }
pub fn load_from_memory<'ctx, D>(
context: &mut Context<'ctx, D>,
index: inkwell::values::IntValue<'ctx>,
) -> anyhow::Result<inkwell::values::BasicValueEnum<'ctx>>
where
D: Dependency + Clone,
{
let immutable_data_pointer = context
.get_global(revive_runtime_api::immutable_data::GLOBAL_IMMUTABLE_DATA_POINTER)?
.value
.as_pointer_value();
let immutable_pointer = context.build_gep(
Pointer::new(
context.word_type(),
AddressSpace::Stack,
immutable_data_pointer,
),
&[index],
context.word_type().as_basic_type_enum(),
"immutable_variable_pointer",
);
context.build_load(immutable_pointer, "immutable_value")
}
@@ -1,29 +1,10 @@
//! Translates the heap memory operations. //! Translates the heap memory operations.
use inkwell::values::BasicValue;
use crate::polkavm::context::address_space::AddressSpace; use crate::polkavm::context::address_space::AddressSpace;
use crate::polkavm::context::pointer::Pointer; use crate::polkavm::context::pointer::Pointer;
use crate::polkavm::context::Context; use crate::polkavm::context::Context;
use crate::polkavm::Dependency; use crate::polkavm::Dependency;
/// Translates the `msize` instruction.
pub fn msize<'ctx, D>(
context: &mut Context<'ctx, D>,
) -> anyhow::Result<inkwell::values::BasicValueEnum<'ctx>>
where
D: Dependency + Clone,
{
Ok(context
.builder()
.build_int_z_extend(
context.build_msize()?,
context.word_type(),
"heap_size_extended",
)?
.as_basic_value_enum())
}
/// Translates the `mload` instruction. /// Translates the `mload` instruction.
/// Uses the main heap. /// Uses the main heap.
pub fn load<'ctx, D>( pub fn load<'ctx, D>(
+2 -57
View File
@@ -1,8 +1,5 @@
//! Translates the transaction return operations. //! Translates the transaction return operations.
use crate::polkavm::context::address_space::AddressSpace;
use crate::polkavm::context::code_type::CodeType;
use crate::polkavm::context::pointer::Pointer;
use crate::polkavm::context::Context; use crate::polkavm::context::Context;
use crate::polkavm::Dependency; use crate::polkavm::Dependency;
@@ -15,60 +12,8 @@ pub fn r#return<'ctx, D>(
where where
D: Dependency + Clone, D: Dependency + Clone,
{ {
match context.code_type() { if context.code_type().is_none() {
None => anyhow::bail!("Return is not available if the contract part is undefined"), anyhow::bail!("Return is not available if the contract part is undefined");
Some(CodeType::Deploy) => {
let immutable_data_size_pointer = context
.get_global(revive_runtime_api::immutable_data::GLOBAL_IMMUTABLE_DATA_SIZE)?
.value
.as_pointer_value();
let immutable_data_size = context.build_load(
Pointer::new(
context.xlen_type(),
AddressSpace::Stack,
immutable_data_size_pointer,
),
"immutable_data_size_load",
)?;
let write_immutable_data_block = context.append_basic_block("write_immutables_block");
let join_return_block = context.append_basic_block("join_return_block");
let immutable_data_size_is_zero = context.builder().build_int_compare(
inkwell::IntPredicate::EQ,
context.xlen_type().const_zero(),
immutable_data_size.into_int_value(),
"immutable_data_size_is_zero",
)?;
context.build_conditional_branch(
immutable_data_size_is_zero,
join_return_block,
write_immutable_data_block,
)?;
context.set_basic_block(write_immutable_data_block);
let immutable_data_pointer = context
.get_global(revive_runtime_api::immutable_data::GLOBAL_IMMUTABLE_DATA_POINTER)?
.value
.as_pointer_value();
context.build_runtime_call(
revive_runtime_api::polkavm_imports::SET_IMMUTABLE_DATA,
&[
context
.builder()
.build_ptr_to_int(
immutable_data_pointer,
context.xlen_type(),
"immutable_data_pointer_to_xlen",
)?
.into(),
immutable_data_size,
],
);
context.build_unconditional_branch(join_return_block);
context.set_basic_block(join_return_block);
}
Some(CodeType::Runtime) => {}
} }
context.build_exit( context.build_exit(
@@ -1,7 +1,10 @@
//! Translates the return data instructions. //! Translates the return data instructions.
use inkwell::values::BasicValue;
use crate::polkavm::context::Context; use crate::polkavm::context::Context;
use crate::polkavm::Dependency; use crate::polkavm::Dependency;
use crate::polkavm_const::runtime_api;
/// Translates the return data size. /// Translates the return data size.
pub fn size<'ctx, D>( pub fn size<'ctx, D>(
@@ -10,17 +13,13 @@ pub fn size<'ctx, D>(
where where
D: Dependency + Clone, D: Dependency + Clone,
{ {
let output_pointer = context.build_alloca_at_entry(context.word_type(), "return_data_size"); let value = context
let output_pointer_parameter = context.builder().build_ptr_to_int( .get_global_value(crate::polkavm::GLOBAL_RETURN_DATA_SIZE)?
output_pointer.value, .into_int_value();
context.xlen_type(), Ok(context
"return_data_copy_output_pointer", .builder()
)?; .build_int_z_extend(value, context.word_type(), "calldatasize_extended")?
context.build_runtime_call( .as_basic_value_enum())
revive_runtime_api::polkavm_imports::RETURNDATASIZE,
&[output_pointer_parameter.into()],
);
context.build_load(output_pointer, "return_data_size_load")
} }
/// Translates the return data copy, trapping if /// Translates the return data copy, trapping if
@@ -40,49 +39,16 @@ where
let destination_offset = context.safe_truncate_int_to_xlen(destination_offset)?; let destination_offset = context.safe_truncate_int_to_xlen(destination_offset)?;
let size = context.safe_truncate_int_to_xlen(size)?; let size = context.safe_truncate_int_to_xlen(size)?;
let output_pointer = context.builder().build_ptr_to_int( let destination_offset = context.builder().build_ptr_to_int(
context.build_heap_gep(destination_offset, size)?.value, context.build_heap_gep(destination_offset, size)?.value,
context.xlen_type(), context.xlen_type(),
"return_data_copy_output_pointer", "destination_offset",
)?;
let output_length_pointer = context.build_alloca_at_entry(
context.xlen_type(),
"return_data_copy_output_length_pointer",
);
context.build_store(output_length_pointer, size)?;
let output_length_pointer_int = context.builder().build_ptr_to_int(
output_length_pointer.value,
context.xlen_type(),
"return_data_copy_output_length_pointer_int",
)?; )?;
context.build_runtime_call( context.build_runtime_call(
revive_runtime_api::polkavm_imports::RETURNDATACOPY, runtime_api::imports::RETURNDATACOPY,
&[ &[destination_offset.into(), source_offset.into(), size.into()],
output_pointer.into(),
output_length_pointer_int.into(),
source_offset.into(),
],
); );
// Trap on OOB (will be different in EOF code)
let overflow_block = context.append_basic_block("return_data_overflow");
let non_overflow_block = context.append_basic_block("return_data_non_overflow");
let is_overflow = context.builder().build_int_compare(
inkwell::IntPredicate::UGT,
size,
context
.build_load(output_length_pointer, "bytes_written")?
.into_int_value(),
"is_overflow",
)?;
context.build_conditional_branch(is_overflow, overflow_block, non_overflow_block)?;
context.set_basic_block(overflow_block);
context.build_call(context.intrinsics().trap, &[], "invalid_trap");
context.build_unreachable();
context.set_basic_block(non_overflow_block);
Ok(()) Ok(())
} }
+33 -16
View File
@@ -1,6 +1,7 @@
//! Translates the storage operations. //! Translates the storage operations.
use crate::polkavm::context::address_space::AddressSpace; use crate::polkavm::context::address_space::AddressSpace;
use crate::polkavm::context::pointer::Pointer;
use crate::polkavm::context::Context; use crate::polkavm::context::Context;
use crate::polkavm::Dependency; use crate::polkavm::Dependency;
@@ -12,10 +13,14 @@ pub fn load<'ctx, D>(
where where
D: Dependency + Clone, D: Dependency + Clone,
{ {
let mut slot_ptr = context.build_alloca_at_entry(context.word_type(), "slot_pointer"); let position_pointer = Pointer::new_with_offset(
slot_ptr.address_space = AddressSpace::Storage; context,
context.builder().build_store(slot_ptr.value, position)?; AddressSpace::Storage,
context.build_load(slot_ptr, "storage_load_value") context.word_type(),
position,
"storage_load_position_pointer",
);
context.build_load(position_pointer, "storage_load_value")
} }
/// Translates the storage store. /// Translates the storage store.
@@ -27,10 +32,14 @@ pub fn store<'ctx, D>(
where where
D: Dependency + Clone, D: Dependency + Clone,
{ {
let mut slot_ptr = context.build_alloca_at_entry(context.word_type(), "slot_pointer"); let position_pointer = Pointer::new_with_offset(
slot_ptr.address_space = AddressSpace::Storage; context,
context.builder().build_store(slot_ptr.value, position)?; AddressSpace::Storage,
context.build_store(slot_ptr, value)?; context.word_type(),
position,
"storage_store_position_pointer",
);
context.build_store(position_pointer, value)?;
Ok(()) Ok(())
} }
@@ -42,10 +51,14 @@ pub fn transient_load<'ctx, D>(
where where
D: Dependency + Clone, D: Dependency + Clone,
{ {
let mut slot_ptr = context.build_alloca_at_entry(context.word_type(), "slot_pointer"); let position_pointer = Pointer::new_with_offset(
slot_ptr.address_space = AddressSpace::TransientStorage; context,
context.builder().build_store(slot_ptr.value, position)?; AddressSpace::TransientStorage,
context.build_load(slot_ptr, "transient_storage_load_value") context.word_type(),
position,
"transient_storage_load_position_pointer",
);
context.build_load(position_pointer, "transient_storage_load_value")
} }
/// Translates the transient storage store. /// Translates the transient storage store.
@@ -57,9 +70,13 @@ pub fn transient_store<'ctx, D>(
where where
D: Dependency + Clone, D: Dependency + Clone,
{ {
let mut slot_ptr = context.build_alloca_at_entry(context.word_type(), "slot_pointer"); let position_pointer = Pointer::new_with_offset(
slot_ptr.address_space = AddressSpace::TransientStorage; context,
context.builder().build_store(slot_ptr.value, position)?; AddressSpace::TransientStorage,
context.build_store(slot_ptr, value)?; context.word_type(),
position,
"transient_storage_store_position_pointer",
);
context.build_store(position_pointer, value)?;
Ok(()) Ok(())
} }
+2
View File
@@ -97,6 +97,7 @@ pub trait Dependency {
dependency: Self, dependency: Self,
path: &str, path: &str,
optimizer_settings: OptimizerSettings, optimizer_settings: OptimizerSettings,
is_system_mode: bool,
include_metadata_hash: bool, include_metadata_hash: bool,
debug_config: Option<DebugConfig>, debug_config: Option<DebugConfig>,
) -> anyhow::Result<String>; ) -> anyhow::Result<String>;
@@ -117,6 +118,7 @@ impl Dependency for DummyDependency {
_dependency: Self, _dependency: Self,
_path: &str, _path: &str,
_optimizer_settings: OptimizerSettings, _optimizer_settings: OptimizerSettings,
_is_system_mode: bool,
_include_metadata_hash: bool, _include_metadata_hash: bool,
_debug_config: Option<DebugConfig>, _debug_config: Option<DebugConfig>,
) -> anyhow::Result<String> { ) -> anyhow::Result<String> {
@@ -39,9 +39,9 @@ impl TargetMachine {
/// LLVM target features. /// LLVM target features.
#[cfg(feature = "riscv-zbb")] #[cfg(feature = "riscv-zbb")]
pub const VM_FEATURES: &'static str = "+zbb,+a,+e,+m,+c,+fast-unaligned-access,+xtheadcondmov"; pub const VM_FEATURES: &'static str = "+zbb,+e,+m";
#[cfg(not(feature = "riscv-zbb"))] #[cfg(not(feature = "riscv-zbb"))]
pub const VM_FEATURES: &'static str = "+a,+e,+m,+c,+fast-unaligned-access,+xtheadcondmov"; pub const VM_FEATURES: &'static str = "+e,+m";
/// A shortcut constructor. /// A shortcut constructor.
/// A separate instance for every optimization level is created. /// A separate instance for every optimization level is created.
Binary file not shown.
+11 -22
View File
@@ -26,13 +26,13 @@
use std::time::Duration; use std::time::Duration;
use hex::{FromHex, ToHex}; use hex::{FromHex, ToHex};
use pallet_revive::{AddressMapper, ExecReturnValue, InstantiateReturnValue}; use pallet_revive::AddressMapper;
use polkadot_sdk::*; use polkadot_sdk::*;
use polkadot_sdk::{ use polkadot_sdk::{
pallet_revive::{CollectEvents, ContractResult, DebugInfo}, pallet_revive::{CollectEvents, ContractExecResult, ContractInstantiateResult, DebugInfo},
polkadot_runtime_common::BuildStorage, polkadot_runtime_common::BuildStorage,
polkadot_sdk_frame::testing_prelude::*, polkadot_sdk_frame::testing_prelude::*,
sp_core::{H160, H256}, sp_core::H160,
sp_keystore::{testing::MemoryKeystore, KeystoreExt}, sp_keystore::{testing::MemoryKeystore, KeystoreExt},
sp_runtime::AccountId32, sp_runtime::AccountId32,
}; };
@@ -72,7 +72,7 @@ impl ExtBuilder {
Self { Self {
balance_genesis_config: value balance_genesis_config: value
.iter() .iter()
.map(|(address, balance)| (AccountId::to_fallback_account_id(address), *balance)) .map(|(address, balance)| (AccountId::to_account_id(address), *balance))
.collect(), .collect(),
} }
} }
@@ -161,7 +161,7 @@ impl VerifyCallExpectation {
fn verify(self, result: &CallResult) { fn verify(self, result: &CallResult) {
assert_eq!( assert_eq!(
self.success, self.success,
!result.did_revert(), result.is_ok(),
"contract execution result mismatch: {result:?}" "contract execution result mismatch: {result:?}"
); );
@@ -179,33 +179,23 @@ impl VerifyCallExpectation {
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub enum CallResult { pub enum CallResult {
Exec { Exec {
result: ContractResult<ExecReturnValue, Balance, EventRecord>, result: ContractExecResult<Balance, EventRecord>,
wall_time: Duration, wall_time: Duration,
}, },
Instantiate { Instantiate {
result: ContractResult<InstantiateReturnValue, Balance, EventRecord>, result: ContractInstantiateResult<Balance, EventRecord>,
wall_time: Duration, wall_time: Duration,
code_hash: H256,
}, },
} }
impl CallResult { impl CallResult {
/// Check if the call was successful /// Check if the call was successful
fn did_revert(&self) -> bool { fn is_ok(&self) -> bool {
match self { match self {
Self::Exec { result, .. } => result Self::Exec { result, .. } => result.result.is_ok(),
.result Self::Instantiate { result, .. } => result.result.is_ok(),
.as_ref()
.map(|r| r.did_revert())
.unwrap_or(true),
Self::Instantiate { result, .. } => result
.result
.as_ref()
.map(|r| r.result.did_revert())
.unwrap_or(true),
} }
} }
/// Get the output of the call /// Get the output of the call
fn output(&self) -> Vec<u8> { fn output(&self) -> Vec<u8> {
match self { match self {
@@ -221,7 +211,6 @@ impl CallResult {
.unwrap_or_default(), .unwrap_or_default(),
} }
} }
/// Get the gas consumed by the call /// Get the gas consumed by the call
fn gas_consumed(&self) -> Weight { fn gas_consumed(&self) -> Weight {
match self { match self {
@@ -246,7 +235,7 @@ pub enum Code {
/// A contract blob /// A contract blob
Bytes(Vec<u8>), Bytes(Vec<u8>),
/// Pre-existing contract hash /// Pre-existing contract hash
Hash(crate::runtime::Hash), Hash(Hash),
} }
impl Default for Code { impl Default for Code {
+3 -6
View File
@@ -1,6 +1,5 @@
use frame_support::runtime; use frame_support::runtime;
use pallet_revive::AccountId32Mapper;
use polkadot_sdk::*; use polkadot_sdk::*;
use polkadot_sdk::{ use polkadot_sdk::{
polkadot_sdk_frame::{log, runtime::prelude::*}, polkadot_sdk_frame::{log, runtime::prelude::*},
@@ -8,7 +7,7 @@ use polkadot_sdk::{
}; };
pub type Balance = u128; pub type Balance = u128;
pub type AccountId = pallet_revive::AccountId32Mapper<Runtime>; pub type AccountId = pallet_revive::DefaultAddressMapper;
pub type Block = frame_system::mocking::MockBlock<Runtime>; pub type Block = frame_system::mocking::MockBlock<Runtime>;
pub type Hash = <Runtime as frame_system::Config>::Hash; pub type Hash = <Runtime as frame_system::Config>::Hash;
pub type EventRecord = pub type EventRecord =
@@ -75,13 +74,11 @@ impl pallet_revive::Config for Runtime {
type ChainExtension = (); type ChainExtension = ();
type DepositPerByte = DepositPerByte; type DepositPerByte = DepositPerByte;
type DepositPerItem = DepositPerItem; type DepositPerItem = DepositPerItem;
type AddressMapper = AccountId32Mapper<Self>; type AddressMapper = AccountId;
type RuntimeMemory = ConstU32<{ 512 * 1024 * 1024 }>;
type PVFMemory = ConstU32<{ 1024 * 1024 * 1024 }>;
type UnsafeUnstableInterface = UnstableInterface; type UnsafeUnstableInterface = UnstableInterface;
type UploadOrigin = EnsureSigned<AccountId32>; type UploadOrigin = EnsureSigned<AccountId32>;
type InstantiateOrigin = EnsureSigned<AccountId32>; type InstantiateOrigin = EnsureSigned<AccountId32>;
type Migrations = ();
type CodeHashLockupDepositPercent = CodeHashLockupDepositPercent; type CodeHashLockupDepositPercent = CodeHashLockupDepositPercent;
type Debug = (); type Debug = ();
type ChainId = ConstU64<420_420_420>;
} }
+24 -65
View File
@@ -1,9 +1,10 @@
use std::time::Instant; use std::time::Instant;
use pallet_revive::AddressMapper;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::*; use crate::*;
use alloy_primitives::{keccak256, Address}; use alloy_primitives::Address;
#[cfg(feature = "revive-solidity")] #[cfg(feature = "revive-solidity")]
use revive_differential::{Evm, EvmLog}; use revive_differential::{Evm, EvmLog};
#[cfg(feature = "revive-solidity")] #[cfg(feature = "revive-solidity")]
@@ -57,6 +58,7 @@ pub enum SpecsAction {
}, },
/// Verify the result of the last call, omitting this will simply ensure the last call was successful /// Verify the result of the last call, omitting this will simply ensure the last call was successful
VerifyCall(VerifyCallExpectation), VerifyCall(VerifyCallExpectation),
/// Verify the balance of an account /// Verify the balance of an account
VerifyBalance { VerifyBalance {
origin: TestAddress, origin: TestAddress,
@@ -177,11 +179,7 @@ impl Default for Specs {
fn default() -> Self { fn default() -> Self {
Self { Self {
differential: false, differential: false,
balances: vec![ balances: vec![(ALICE, 1_000_000_000)],
(ALICE, 1_000_000_000),
(BOB, 1_000_000_000),
(CHARLIE, 1_000_000_000),
],
actions: Default::default(), actions: Default::default(),
} }
} }
@@ -274,10 +272,10 @@ impl Specs {
origin, origin,
value, value,
gas_limit, gas_limit,
storage_deposit_limit,
code, code,
data, data,
salt, salt,
..
} => { } => {
let Code::Solidity { let Code::Solidity {
path: Some(path), path: Some(path),
@@ -288,37 +286,21 @@ impl Specs {
else { else {
panic!("the differential runner requires Code::Solidity source"); panic!("the differential runner requires Code::Solidity source");
}; };
assert_ne!( assert_ne!(solc_optimizer, Some(false), "solc_optimizer must be enabled in differntial mode");
pipeline, assert_ne!(pipeline, Some(revive_solidity::SolcPipeline::EVMLA), "yul pipeline must be enabled in differntial mode");
Some(revive_solidity::SolcPipeline::EVMLA), assert!(storage_deposit_limit.is_none(), "storage deposit limit is not supported in differential mode");
"yul pipeline must be enabled in differential mode" assert!(salt.0.is_none(), "salt is not supported in differential mode");
); assert_eq!(origin, TestAddress::default(), "configuring the origin is not supported in differential mode");
assert!(
salt.0.is_none(),
"salt is not supported in differential mode"
);
assert_eq!(
origin,
TestAddress::default(),
"configuring the origin is not supported in differential mode"
);
let deploy_code = match std::fs::read_to_string(&path) { let deploy_code = match std::fs::read_to_string(&path) {
Ok(solidity_source) => hex::encode(compile_evm_deploy_code( Ok(solidity_source) => compile_evm_deploy_code(&contract, &solidity_source),
&contract,
&solidity_source,
solc_optimizer.unwrap_or(true),
)),
Err(err) => panic!( Err(err) => panic!(
"failed to read solidity source\n . path: '{}'\n . error: {:?}", "failed to read solidity source\n . path: '{}'\n . error: {:?}",
path.display(), path.display(),
err err
), ),
}; };
let mut vm = evm let deploy_code = hex::encode(deploy_code);
.code_blob(deploy_code.as_bytes().to_vec()) let mut vm = evm.code_blob(deploy_code.as_bytes().to_vec()).sender(origin.to_eth_addr(&[]).0.into()).deploy(true);
.sender(origin.to_eth_addr(&[]).0.into())
.deploy(true);
if !data.is_empty() { if !data.is_empty() {
vm = vm.input(data.into()); vm = vm.input(data.into());
} }
@@ -333,13 +315,7 @@ impl Specs {
let deployed_account = log.account_deployed.expect("no account was created"); let deployed_account = log.account_deployed.expect("no account was created");
let account_pvm = TestAddress::Instantiated(deployed_accounts.len()); let account_pvm = TestAddress::Instantiated(deployed_accounts.len());
deployed_accounts.push(deployed_account); deployed_accounts.push(deployed_account);
derived_specs derived_specs.actions.append(&mut SpecsAction::derive_verification(&log, deployed_account, account_pvm));
.actions
.append(&mut SpecsAction::derive_verification(
&log,
deployed_account,
account_pvm,
));
evm = Evm::from_genesis(log.state_dump.into()); evm = Evm::from_genesis(log.state_dump.into());
} }
Call { Call {
@@ -347,23 +323,16 @@ impl Specs {
dest, dest,
value, value,
gas_limit, gas_limit,
storage_deposit_limit,
data, data,
..
} => { } => {
assert_eq!( assert_eq!(origin, TestAddress::default(), "configuring the origin is not supported in differential mode");
origin, assert!(storage_deposit_limit.is_none(), "storage deposit limit is not supported in differential mode");
TestAddress::default(),
"configuring the origin is not supported in differential mode"
);
let TestAddress::Instantiated(n) = dest else { let TestAddress::Instantiated(n) = dest else {
panic!("the differential runner requires TestAccountId::Instantiated(n) as dest"); panic!("the differential runner requires TestAccountId::Instantiated(n) as dest");
}; };
let address = deployed_accounts let address = deployed_accounts.get(n).unwrap_or_else(|| panic!("no account at index {n} "));
.get(n) let mut vm = evm.receiver(*address).sender(origin.to_eth_addr(&[]).0.into());
.unwrap_or_else(|| panic!("no account at index {n} "));
let mut vm = evm
.receiver(*address)
.sender(origin.to_eth_addr(&[]).0.into());
if !data.is_empty() { if !data.is_empty() {
vm = vm.input(data.into()); vm = vm.input(data.into());
} }
@@ -375,13 +344,10 @@ impl Specs {
} }
let log = vm.run(); let log = vm.run();
derived_specs derived_specs.actions.append(&mut SpecsAction::derive_verification(&log, *address, dest));
.actions
.append(&mut SpecsAction::derive_verification(&log, *address, dest));
evm = Evm::from_genesis(log.state_dump.into()); evm = Evm::from_genesis(log.state_dump.into());
} }
Upload { .. } => continue, _ => panic!("only instantiate and call action allowed in differential mode, got: {action:?}"),
other => derived_specs.actions.push(other),
} }
} }
@@ -408,13 +374,6 @@ impl Specs {
data, data,
salt, salt,
} => { } => {
let code: pallet_revive::Code = code.into();
let code_hash = match code.clone() {
pallet_revive::Code::Existing(code_hash) => code_hash,
pallet_revive::Code::Upload(bytes) => {
H256::from_slice(keccak256(&bytes).as_slice())
}
};
let origin = RuntimeOrigin::signed(origin.to_account_id(&results)); let origin = RuntimeOrigin::signed(origin.to_account_id(&results));
let time_start = Instant::now(); let time_start = Instant::now();
let result = Contracts::bare_instantiate( let result = Contracts::bare_instantiate(
@@ -422,7 +381,7 @@ impl Specs {
value, value,
gas_limit.unwrap_or(GAS_LIMIT), gas_limit.unwrap_or(GAS_LIMIT),
storage_deposit_limit.unwrap_or(DEPOSIT_LIMIT), storage_deposit_limit.unwrap_or(DEPOSIT_LIMIT),
code, code.into(),
data, data,
salt.0, salt.0,
DebugInfo::Skip, DebugInfo::Skip,
@@ -431,7 +390,6 @@ impl Specs {
results.push(CallResult::Instantiate { results.push(CallResult::Instantiate {
result, result,
wall_time: time_start.elapsed(), wall_time: time_start.elapsed(),
code_hash,
}) })
} }
Upload { Upload {
@@ -444,7 +402,7 @@ impl Specs {
pallet_revive::Code::Existing(_) => continue, pallet_revive::Code::Existing(_) => continue,
pallet_revive::Code::Upload(bytes) => bytes, pallet_revive::Code::Upload(bytes) => bytes,
}, },
storage_deposit_limit.unwrap_or(DEPOSIT_LIMIT), storage_deposit_limit.unwrap_or_default(),
) )
.unwrap_or_else(|error| panic!("code upload failed: {error:?}")), .unwrap_or_else(|error| panic!("code upload failed: {error:?}")),
Call { Call {
@@ -484,6 +442,7 @@ impl Specs {
expected, expected,
} => { } => {
let address = contract.to_eth_addr(&results); let address = contract.to_eth_addr(&results);
dbg!(contract.to_account_id(&results));
let Ok(value) = Contracts::get_storage(address, key) else { let Ok(value) = Contracts::get_storage(address, key) else {
panic!("error reading storage for address {address}"); panic!("error reading storage for address {address}");
}; };
+1 -3
View File
@@ -12,6 +12,4 @@ riscv-64 = []
[dependencies] [dependencies]
anyhow = { workspace = true } anyhow = { workspace = true }
inkwell = { workspace = true, features = ["target-riscv", "no-libffi-linking", "llvm18-0"] } inkwell = { workspace = true, features = ["target-riscv", "no-libffi-linking", "llvm18-0"] }
revive-common = { workspace = true }
+51 -25
View File
@@ -3,7 +3,7 @@ use inkwell::{
context::Context, context::Context,
module::Module, module::Module,
types::{BasicType, StructType}, types::{BasicType, StructType},
values::{BasicValueEnum, PointerValue}, values::{BasicValue, PointerValue},
}; };
/// Creates a module that sets the PolkaVM minimum stack size to [`size`] if linked in. /// Creates a module that sets the PolkaVM minimum stack size to [`size`] if linked in.
@@ -21,33 +21,56 @@ pub fn min_stack_size<'context>(
module module
} }
/// Helper for building function calls with stack spilled arguments. pub struct Spill<'a, 'ctx> {
/// - `pointer`: points to a struct of the packed argument struct type
/// - `type`: the packed argument struct type
/// - `arguments`: a correctly ordered list of the struct field values
pub fn spill<'ctx>(
builder: &Builder<'ctx>,
pointer: PointerValue<'ctx>, pointer: PointerValue<'ctx>,
builder: &'a Builder<'ctx>,
r#type: StructType<'ctx>, r#type: StructType<'ctx>,
arguments: &[BasicValueEnum<'ctx>], current_field: u32,
) -> anyhow::Result<()> { }
for index in 0..r#type.get_field_types().len() {
let field_pointer = builder.build_struct_gep( impl<'a, 'ctx> Spill<'a, 'ctx> {
r#type, pub fn new(
pointer, builder: &'a Builder<'ctx>,
index as u32, r#type: StructType<'ctx>,
&format!("spill_parameter_{}", index), name: &str,
)?; ) -> anyhow::Result<Self> {
let field_value = arguments Ok(Self {
.get(index) pointer: builder.build_alloca(r#type, name)?,
.ok_or_else(|| anyhow::anyhow!("invalid index {index} for struct type {}", r#type))?; builder,
builder.build_store(field_pointer, *field_value)?; r#type,
} current_field: 0,
})
Ok(()) }
pub fn next<V: BasicValue<'ctx>>(mut self, value: V) -> anyhow::Result<Self> {
let field_pointer = self.builder.build_struct_gep(
self.r#type,
self.pointer,
self.current_field,
&format!("spill_parameter_{}", self.current_field),
)?;
self.builder.build_store(field_pointer, value)?;
self.current_field += 1;
Ok(self)
}
pub fn skip(mut self) -> Self {
self.current_field += 1;
self
}
pub fn done(self) -> PointerValue<'ctx> {
assert!(
self.r#type
.get_field_type_at_index(self.current_field)
.is_none(),
"there must not be any missing parameters"
);
self.pointer
}
} }
/// Returns a packed struct argument type for the `instantiate` API.
pub fn instantiate(context: &Context) -> StructType { pub fn instantiate(context: &Context) -> StructType {
context.struct_type( context.struct_type(
&[ &[
@@ -67,18 +90,21 @@ pub fn instantiate(context: &Context) -> StructType {
context.i32_type().as_basic_type_enum(), context.i32_type().as_basic_type_enum(),
// address_ptr: u32, // address_ptr: u32,
context.ptr_type(Default::default()).as_basic_type_enum(), context.ptr_type(Default::default()).as_basic_type_enum(),
// address_len_ptr: u32,
context.ptr_type(Default::default()).as_basic_type_enum(),
// output_ptr: u32, // output_ptr: u32,
context.ptr_type(Default::default()).as_basic_type_enum(), context.ptr_type(Default::default()).as_basic_type_enum(),
// output_len_ptr: u32, // output_len_ptr: u32,
context.ptr_type(Default::default()).as_basic_type_enum(), context.ptr_type(Default::default()).as_basic_type_enum(),
// salt_ptr: u32, // salt_ptr: u32,
context.ptr_type(Default::default()).as_basic_type_enum(), context.ptr_type(Default::default()).as_basic_type_enum(),
// salt_len: u32
context.i32_type().as_basic_type_enum(),
], ],
true, true,
) )
} }
/// Returns a packed struct argument type for the `call` API.
pub fn call(context: &Context) -> StructType { pub fn call(context: &Context) -> StructType {
context.struct_type( context.struct_type(
&[ &[
-89
View File
@@ -1,89 +0,0 @@
//! Allocates memory for the immutable data in a separate module.
//!
//! Because we only know how many immutable variables were set after
//! translating the whole contract code, we want to set the size at
//! last. However, array types need a size upon declaration.
//!
//! A simple work around is to replace it during link time.
//! To quote the [LLVM docs][0]:
//!
//! > For global variable declarations [..] the allocation size and
//! > alignment of the definition it resolves to must be greater than
//! > or equal to that of the declaration [..]
//!
//! To adhere to this we initially declare a length of 0 in
//! `revive-llvm-context`.
//!
//! [0]: https://llvm.org/docs/LangRef.html#global-variables
/// The immutable data module name.
pub static MODULE_NAME: &str = "__evm_immutables";
/// The immutable data global pointer.
pub static GLOBAL_IMMUTABLE_DATA_POINTER: &str = "__immutable_data_ptr";
/// The immutable data global size.
pub static GLOBAL_IMMUTABLE_DATA_SIZE: &str = "__immutable_data_size";
/// The immutable data maximum size in bytes.
pub static IMMUTABLE_DATA_MAX_SIZE: u32 = 4 * 1024;
/// Returns the immutable data global type.
pub fn data_type(context: &inkwell::context::Context, size: u32) -> inkwell::types::ArrayType {
context
.custom_width_int_type(revive_common::BIT_LENGTH_WORD as u32)
.array_type(size)
}
/// Returns the immutable data size global type.
pub fn size_type(context: &inkwell::context::Context) -> inkwell::types::IntType {
context.custom_width_int_type(revive_common::BIT_LENGTH_X32 as u32)
}
/// Creates a LLVM module with the immutable data and its `size` in bytes.
pub fn module(context: &inkwell::context::Context, size: u32) -> inkwell::module::Module {
let module = context.create_module(MODULE_NAME);
let length = size / revive_common::BYTE_LENGTH_WORD as u32;
let immutable_data = module.add_global(
data_type(context, length),
Default::default(),
GLOBAL_IMMUTABLE_DATA_POINTER,
);
immutable_data.set_linkage(inkwell::module::Linkage::External);
immutable_data.set_visibility(inkwell::GlobalVisibility::Default);
immutable_data.set_initializer(&data_type(context, length).get_undef());
let immutable_data_size = module.add_global(
size_type(context),
Default::default(),
GLOBAL_IMMUTABLE_DATA_SIZE,
);
immutable_data_size.set_linkage(inkwell::module::Linkage::External);
immutable_data_size.set_visibility(inkwell::GlobalVisibility::Default);
immutable_data_size.set_initializer(&size_type(context).const_int(size as u64, false));
module
}
#[cfg(test)]
mod tests {
use crate::immutable_data::*;
#[test]
fn it_works() {
inkwell::targets::Target::initialize_riscv(&Default::default());
let context = inkwell::context::Context::create();
let size = 512;
let module = crate::immutable_data::module(&context, size);
let immutable_data_pointer = module.get_global(GLOBAL_IMMUTABLE_DATA_POINTER).unwrap();
assert_eq!(
immutable_data_pointer.get_initializer().unwrap(),
data_type(&context, size / 32).get_undef()
);
let immutable_data_size = module.get_global(GLOBAL_IMMUTABLE_DATA_SIZE).unwrap();
assert_eq!(
immutable_data_size.get_initializer().unwrap(),
size_type(&context).const_int(size as u64, false)
);
}
}
-1
View File
@@ -7,6 +7,5 @@
//! [1]: [https://docs.rs/pallet-contracts/26.0.0/pallet_contracts/api_doc/index.html] //! [1]: [https://docs.rs/pallet-contracts/26.0.0/pallet_contracts/api_doc/index.html]
pub mod calling_convention; pub mod calling_convention;
pub mod immutable_data;
pub mod polkavm_exports; pub mod polkavm_exports;
pub mod polkavm_imports; pub mod polkavm_imports;
+2 -12
View File
@@ -2,16 +2,6 @@ use inkwell::{context::Context, memory_buffer::MemoryBuffer, module::Module, sup
include!(concat!(env!("OUT_DIR"), "/polkavm_exports.rs")); include!(concat!(env!("OUT_DIR"), "/polkavm_exports.rs"));
/// The contract deploy export.
pub static CALL: &str = "call";
/// The contract call export.
pub static DEPLOY: &str = "deploy";
/// All exported symbols.
/// Useful for configuring common attributes and linkage.
pub static EXPORTS: [&str; 2] = [CALL, DEPLOY];
/// Creates a LLVM module from the [BITCODE]. /// Creates a LLVM module from the [BITCODE].
/// The module exports `call` and `deploy` functions (which are named thereafter). /// The module exports `call` and `deploy` functions (which are named thereafter).
/// Returns `Error` if the bitcode fails to parse, which should never happen. /// Returns `Error` if the bitcode fails to parse, which should never happen.
@@ -33,7 +23,7 @@ mod tests {
let context = inkwell::context::Context::create(); let context = inkwell::context::Context::create();
let module = polkavm_exports::module(&context, "polkavm_exports").unwrap(); let module = polkavm_exports::module(&context, "polkavm_exports").unwrap();
assert!(module.get_function(polkavm_exports::CALL).is_some()); assert!(module.get_function("call").is_some());
assert!(module.get_function(polkavm_exports::DEPLOY).is_some()); assert!(module.get_function("deploy").is_some());
} }
} }
+77 -62
View File
@@ -3,34 +3,9 @@
#include "polkavm_guest.h" #include "polkavm_guest.h"
// Missing builtins // Missing builtins
#define EVM_WORD_SIZE 32
#define ALIGN(size) ((size + EVM_WORD_SIZE - 1) & ~(EVM_WORD_SIZE - 1))
#define MAX_MEMORY_SIZE (64 * 1024)
static char __memory[MAX_MEMORY_SIZE];
static uint32_t __memory_size = 0;
void * __sbrk_internal(uint32_t offset, uint32_t size) {
if (offset >= MAX_MEMORY_SIZE || size > MAX_MEMORY_SIZE) {
return NULL;
}
uint32_t new_size = ALIGN(offset + size);
if (new_size > MAX_MEMORY_SIZE) {
return NULL;
}
if (new_size > __memory_size) {
__memory_size = new_size;
}
return (void *)&__memory[__memory_size];
}
uint32_t __msize() {
return __memory_size;
}
void * memset(void *b, int c, size_t len) { void * memset(void *b, int c, size_t len) {
uint8_t *dest = b; uint8_t *dest = b;
while (len-- > 0) *dest++ = c; while (len-- > 0) *dest++ = c;
@@ -62,50 +37,90 @@ void * memmove(void *dst, const void *src, size_t n) {
return dst; return dst;
} }
void * __sbrk(uint32_t size) {
uint32_t address;
__asm__ __volatile__(
".insn r 0xb, 1, 0, %[dst], %[sz], zero"
: [dst] "=r" (address)
: [sz] "ir" (size)
:
);
return (void *)address;
}
// Imports // Imports
POLKAVM_IMPORT(void, address, uint32_t)
POLKAVM_IMPORT(void, balance, uint32_t)
POLKAVM_IMPORT(void, balance_of, uint32_t, uint32_t)
POLKAVM_IMPORT(void, block_number, uint32_t)
POLKAVM_IMPORT(uint32_t, call, uint32_t)
POLKAVM_IMPORT(void, caller, uint32_t)
POLKAVM_IMPORT(void, chain_id, uint32_t)
POLKAVM_IMPORT(uint32_t, code_size, uint32_t, uint32_t)
POLKAVM_IMPORT(void, code_hash, uint32_t, uint32_t)
POLKAVM_IMPORT(void, deposit_event, uint32_t, uint32_t, uint32_t, uint32_t)
POLKAVM_IMPORT(void, get_immutable_data, uint32_t, uint32_t);
POLKAVM_IMPORT(uint32_t, get_storage, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t)
POLKAVM_IMPORT(void, hash_keccak_256, uint32_t, uint32_t, uint32_t)
POLKAVM_IMPORT(void, input, uint32_t, uint32_t) POLKAVM_IMPORT(void, input, uint32_t, uint32_t)
POLKAVM_IMPORT(uint32_t, instantiate, uint32_t)
POLKAVM_IMPORT(void, now, uint32_t)
POLKAVM_IMPORT(void, origin, uint32_t)
POLKAVM_IMPORT(void, seal_return, uint32_t, uint32_t, uint32_t) POLKAVM_IMPORT(void, seal_return, uint32_t, uint32_t, uint32_t)
POLKAVM_IMPORT(void, returndatacopy, uint32_t, uint32_t, uint32_t)
POLKAVM_IMPORT(void, value_transferred, uint32_t, uint32_t)
POLKAVM_IMPORT(uint32_t, set_storage, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t) POLKAVM_IMPORT(uint32_t, set_storage, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t)
POLKAVM_IMPORT(void, return_data_copy, uint32_t, uint32_t, uint32_t) POLKAVM_IMPORT(uint32_t, get_storage, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t)
POLKAVM_IMPORT(void, return_data_size, uint32_t) POLKAVM_IMPORT(uint32_t, clear_storage, uint32_t, uint32_t)
POLKAVM_IMPORT(void, set_immutable_data, uint32_t, uint32_t); POLKAVM_IMPORT(uint32_t, contains_storage, uint32_t, uint32_t)
POLKAVM_IMPORT(void, value_transferred, uint32_t) POLKAVM_IMPORT(uint32_t, take_storage, uint32_t, uint32_t, uint32_t, uint32_t)
POLKAVM_IMPORT(uint32_t, call, uint32_t)
POLKAVM_IMPORT(uint32_t, delegate_call, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t)
POLKAVM_IMPORT(uint32_t, instantiate, uint32_t)
POLKAVM_IMPORT(void, terminate, uint32_t)
POLKAVM_IMPORT(void, caller, uint32_t, uint32_t)
POLKAVM_IMPORT(uint32_t, is_contract, uint32_t)
POLKAVM_IMPORT(uint32_t, code_hash, uint32_t, uint32_t, uint32_t)
POLKAVM_IMPORT(uint32_t, code_size, uint32_t)
POLKAVM_IMPORT(void, own_code_hash, uint32_t, uint32_t)
POLKAVM_IMPORT(uint32_t, caller_is_origin)
POLKAVM_IMPORT(uint32_t, caller_is_root)
POLKAVM_IMPORT(void, address, uint32_t, uint32_t)
POLKAVM_IMPORT(void, weight_to_fee, uint64_t, uint32_t, uint32_t)
POLKAVM_IMPORT(void, gas_left, uint32_t, uint32_t)
POLKAVM_IMPORT(void, balance, uint32_t, uint32_t)
POLKAVM_IMPORT(void, now, uint32_t, uint32_t)
POLKAVM_IMPORT(void, minimum_balance, uint32_t, uint32_t)
POLKAVM_IMPORT(void, deposit_event, uint32_t, uint32_t, uint32_t, uint32_t)
POLKAVM_IMPORT(void, block_number, uint32_t, uint32_t)
POLKAVM_IMPORT(void, hash_sha2_256, uint32_t, uint32_t, uint32_t)
POLKAVM_IMPORT(void, hash_keccak_256, uint32_t, uint32_t, uint32_t)
POLKAVM_IMPORT(void, hash_blake2_256, uint32_t, uint32_t, uint32_t)
POLKAVM_IMPORT(void, hash_blake2_128, uint32_t, uint32_t, uint32_t)
POLKAVM_IMPORT(uint32_t, call_chain_extension, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t)
POLKAVM_IMPORT(uint32_t, debug_message, uint32_t, uint32_t)
POLKAVM_IMPORT(uint32_t, set_code_hash, uint32_t)
POLKAVM_IMPORT(uint64_t, instantiation_nonce,)
POLKAVM_IMPORT(uint32_t, transfer, uint32_t, uint32_t, uint32_t, uint32_t)
-80
View File
@@ -10,86 +10,6 @@ use inkwell::{context::Context, memory_buffer::MemoryBuffer, module::Module, sup
include!(concat!(env!("OUT_DIR"), "/polkavm_imports.rs")); include!(concat!(env!("OUT_DIR"), "/polkavm_imports.rs"));
pub static SBRK: &str = "__sbrk_internal";
pub static MEMORY_SIZE: &str = "__msize";
pub static ADDRESS: &str = "address";
pub static BALANCE: &str = "balance";
pub static BALANCE_OF: &str = "balance_of";
pub static BLOCK_NUMBER: &str = "block_number";
pub static CALL: &str = "call";
pub static CALLER: &str = "caller";
pub static CHAIN_ID: &str = "chain_id";
pub static CODE_SIZE: &str = "code_size";
pub static CODE_HASH: &str = "code_hash";
pub static DEPOSIT_EVENT: &str = "deposit_event";
pub static GET_IMMUTABLE_DATA: &str = "get_immutable_data";
pub static GET_STORAGE: &str = "get_storage";
pub static HASH_KECCAK_256: &str = "hash_keccak_256";
pub static INPUT: &str = "input";
pub static INSTANTIATE: &str = "instantiate";
pub static NOW: &str = "now";
pub static ORIGIN: &str = "origin";
pub static RETURN: &str = "seal_return";
pub static SET_STORAGE: &str = "set_storage";
pub static RETURNDATACOPY: &str = "return_data_copy";
pub static RETURNDATASIZE: &str = "return_data_size";
pub static SET_IMMUTABLE_DATA: &str = "set_immutable_data";
pub static VALUE_TRANSFERRED: &str = "value_transferred";
/// All imported runtime API symbols.
/// Useful for configuring common attributes and linkage.
pub static IMPORTS: [&str; 25] = [
SBRK,
MEMORY_SIZE,
ADDRESS,
BALANCE,
BALANCE_OF,
BLOCK_NUMBER,
CALL,
CALLER,
CHAIN_ID,
CODE_SIZE,
CODE_HASH,
DEPOSIT_EVENT,
GET_IMMUTABLE_DATA,
GET_STORAGE,
HASH_KECCAK_256,
INPUT,
INSTANTIATE,
NOW,
ORIGIN,
RETURN,
RETURNDATACOPY,
RETURNDATASIZE,
SET_IMMUTABLE_DATA,
SET_STORAGE,
VALUE_TRANSFERRED,
];
/// Creates a LLVM module from the [BITCODE]. /// Creates a LLVM module from the [BITCODE].
/// The module imports `pallet-revive` runtime API functions. /// The module imports `pallet-revive` runtime API functions.
/// Returns `Error` if the bitcode fails to parse, which should never happen. /// Returns `Error` if the bitcode fails to parse, which should never happen.
-9
View File
@@ -1,9 +0,0 @@
fn main() {
let git_rev = std::process::Command::new("git")
.args(["rev-parse", "--short", "HEAD"])
.output()
.map(|out| String::from_utf8(out.stdout).unwrap_or_default())
.unwrap_or("unknown".to_owned());
println!("cargo:rustc-env=GIT_COMMIT_HASH={}", git_rev.trim());
}
+8 -4
View File
@@ -8,7 +8,6 @@ use std::path::Path;
use crate::solc::combined_json::CombinedJson; use crate::solc::combined_json::CombinedJson;
use crate::solc::standard_json::output::Output as StandardJsonOutput; use crate::solc::standard_json::output::Output as StandardJsonOutput;
use crate::solc::version::Version as SolcVersion; use crate::solc::version::Version as SolcVersion;
use crate::ResolcVersion;
use self::contract::Contract; use self::contract::Contract;
@@ -41,7 +40,11 @@ impl Build {
} }
/// Writes all contracts assembly and bytecode to the combined JSON. /// Writes all contracts assembly and bytecode to the combined JSON.
pub fn write_to_combined_json(self, combined_json: &mut CombinedJson) -> anyhow::Result<()> { pub fn write_to_combined_json(
self,
combined_json: &mut CombinedJson,
resolc_version: &semver::Version,
) -> anyhow::Result<()> {
for (path, contract) in self.contracts.into_iter() { for (path, contract) in self.contracts.into_iter() {
let combined_json_contract = combined_json let combined_json_contract = combined_json
.contracts .contracts
@@ -58,7 +61,7 @@ impl Build {
contract.write_to_combined_json(combined_json_contract)?; contract.write_to_combined_json(combined_json_contract)?;
} }
combined_json.revive_version = Some(ResolcVersion::default().long); combined_json.zk_version = Some(resolc_version.to_string());
Ok(()) Ok(())
} }
@@ -68,6 +71,7 @@ impl Build {
mut self, mut self,
standard_json: &mut StandardJsonOutput, standard_json: &mut StandardJsonOutput,
solc_version: &SolcVersion, solc_version: &SolcVersion,
resolc_version: &semver::Version,
) -> anyhow::Result<()> { ) -> anyhow::Result<()> {
let contracts = match standard_json.contracts.as_mut() { let contracts = match standard_json.contracts.as_mut() {
Some(contracts) => contracts, Some(contracts) => contracts,
@@ -86,7 +90,7 @@ impl Build {
standard_json.version = Some(solc_version.default.to_string()); standard_json.version = Some(solc_version.default.to_string());
standard_json.long_version = Some(solc_version.long.to_owned()); standard_json.long_version = Some(solc_version.long.to_owned());
standard_json.revive_version = Some(ResolcVersion::default().long); standard_json.zk_version = Some(resolc_version.to_string());
Ok(()) Ok(())
} }
@@ -199,7 +199,6 @@ where
revive_llvm_context::PolkaVMDummyLLVMWritable::default(), revive_llvm_context::PolkaVMDummyLLVMWritable::default(),
) )
.declare(context)?; .declare(context)?;
revive_llvm_context::PolkaVMImmutableDataLoadFunction.declare(context)?;
entry.into_llvm(context)?; entry.into_llvm(context)?;
@@ -267,7 +266,6 @@ where
revive_llvm_context::PolkaVMCodeType::Runtime, revive_llvm_context::PolkaVMCodeType::Runtime,
)) ))
.into_llvm(context)?; .into_llvm(context)?;
revive_llvm_context::PolkaVMImmutableDataLoadFunction.into_llvm(context)?;
Ok(()) Ok(())
} }
@@ -45,7 +45,7 @@ impl Element {
fn pop_arguments_llvm<'ctx, D>( fn pop_arguments_llvm<'ctx, D>(
&mut self, &mut self,
context: &mut revive_llvm_context::PolkaVMContext<'ctx, D>, context: &mut revive_llvm_context::PolkaVMContext<'ctx, D>,
) -> anyhow::Result<Vec<inkwell::values::BasicValueEnum<'ctx>>> ) -> Vec<inkwell::values::BasicValueEnum<'ctx>>
where where
D: revive_llvm_context::PolkaVMDependency + Clone, D: revive_llvm_context::PolkaVMDependency + Clone,
{ {
@@ -57,13 +57,15 @@ impl Element {
[self.stack.elements.len() + input_size - output_size - 1 - index] [self.stack.elements.len() + input_size - output_size - 1 - index]
.to_llvm() .to_llvm()
.into_pointer_value(); .into_pointer_value();
let value = context.build_load( let value = context
revive_llvm_context::PolkaVMPointer::new_stack_field(context, pointer), .build_load(
format!("argument_{index}").as_str(), revive_llvm_context::PolkaVMPointer::new_stack_field(context, pointer),
)?; format!("argument_{index}").as_str(),
)
.unwrap();
arguments.push(value); arguments.push(value);
} }
Ok(arguments) arguments
} }
} }
@@ -424,7 +426,7 @@ where
InstructionName::JUMPDEST => Ok(None), InstructionName::JUMPDEST => Ok(None),
InstructionName::ADD => { InstructionName::ADD => {
let arguments = self.pop_arguments_llvm(context)?; let arguments = self.pop_arguments_llvm(context);
revive_llvm_context::polkavm_evm_arithmetic::addition( revive_llvm_context::polkavm_evm_arithmetic::addition(
context, context,
arguments[0].into_int_value(), arguments[0].into_int_value(),
@@ -433,7 +435,7 @@ where
.map(Some) .map(Some)
} }
InstructionName::SUB => { InstructionName::SUB => {
let arguments = self.pop_arguments_llvm(context)?; let arguments = self.pop_arguments_llvm(context);
revive_llvm_context::polkavm_evm_arithmetic::subtraction( revive_llvm_context::polkavm_evm_arithmetic::subtraction(
context, context,
arguments[0].into_int_value(), arguments[0].into_int_value(),
@@ -442,7 +444,7 @@ where
.map(Some) .map(Some)
} }
InstructionName::MUL => { InstructionName::MUL => {
let arguments = self.pop_arguments_llvm(context)?; let arguments = self.pop_arguments_llvm(context);
revive_llvm_context::polkavm_evm_arithmetic::multiplication( revive_llvm_context::polkavm_evm_arithmetic::multiplication(
context, context,
arguments[0].into_int_value(), arguments[0].into_int_value(),
@@ -451,7 +453,7 @@ where
.map(Some) .map(Some)
} }
InstructionName::DIV => { InstructionName::DIV => {
let arguments = self.pop_arguments_llvm(context)?; let arguments = self.pop_arguments_llvm(context);
revive_llvm_context::polkavm_evm_arithmetic::division( revive_llvm_context::polkavm_evm_arithmetic::division(
context, context,
arguments[0].into_int_value(), arguments[0].into_int_value(),
@@ -460,7 +462,7 @@ where
.map(Some) .map(Some)
} }
InstructionName::MOD => { InstructionName::MOD => {
let arguments = self.pop_arguments_llvm(context)?; let arguments = self.pop_arguments_llvm(context);
revive_llvm_context::polkavm_evm_arithmetic::remainder( revive_llvm_context::polkavm_evm_arithmetic::remainder(
context, context,
arguments[0].into_int_value(), arguments[0].into_int_value(),
@@ -469,7 +471,7 @@ where
.map(Some) .map(Some)
} }
InstructionName::SDIV => { InstructionName::SDIV => {
let arguments = self.pop_arguments_llvm(context)?; let arguments = self.pop_arguments_llvm(context);
revive_llvm_context::polkavm_evm_arithmetic::division_signed( revive_llvm_context::polkavm_evm_arithmetic::division_signed(
context, context,
arguments[0].into_int_value(), arguments[0].into_int_value(),
@@ -478,7 +480,7 @@ where
.map(Some) .map(Some)
} }
InstructionName::SMOD => { InstructionName::SMOD => {
let arguments = self.pop_arguments_llvm(context)?; let arguments = self.pop_arguments_llvm(context);
revive_llvm_context::polkavm_evm_arithmetic::remainder_signed( revive_llvm_context::polkavm_evm_arithmetic::remainder_signed(
context, context,
arguments[0].into_int_value(), arguments[0].into_int_value(),
@@ -488,7 +490,7 @@ where
} }
InstructionName::LT => { InstructionName::LT => {
let arguments = self.pop_arguments_llvm(context)?; let arguments = self.pop_arguments_llvm(context);
revive_llvm_context::polkavm_evm_comparison::compare( revive_llvm_context::polkavm_evm_comparison::compare(
context, context,
arguments[0].into_int_value(), arguments[0].into_int_value(),
@@ -498,7 +500,7 @@ where
.map(Some) .map(Some)
} }
InstructionName::GT => { InstructionName::GT => {
let arguments = self.pop_arguments_llvm(context)?; let arguments = self.pop_arguments_llvm(context);
revive_llvm_context::polkavm_evm_comparison::compare( revive_llvm_context::polkavm_evm_comparison::compare(
context, context,
arguments[0].into_int_value(), arguments[0].into_int_value(),
@@ -508,7 +510,7 @@ where
.map(Some) .map(Some)
} }
InstructionName::EQ => { InstructionName::EQ => {
let arguments = self.pop_arguments_llvm(context)?; let arguments = self.pop_arguments_llvm(context);
revive_llvm_context::polkavm_evm_comparison::compare( revive_llvm_context::polkavm_evm_comparison::compare(
context, context,
arguments[0].into_int_value(), arguments[0].into_int_value(),
@@ -518,7 +520,7 @@ where
.map(Some) .map(Some)
} }
InstructionName::ISZERO => { InstructionName::ISZERO => {
let arguments = self.pop_arguments_llvm(context)?; let arguments = self.pop_arguments_llvm(context);
revive_llvm_context::polkavm_evm_comparison::compare( revive_llvm_context::polkavm_evm_comparison::compare(
context, context,
arguments[0].into_int_value(), arguments[0].into_int_value(),
@@ -528,7 +530,7 @@ where
.map(Some) .map(Some)
} }
InstructionName::SLT => { InstructionName::SLT => {
let arguments = self.pop_arguments_llvm(context)?; let arguments = self.pop_arguments_llvm(context);
revive_llvm_context::polkavm_evm_comparison::compare( revive_llvm_context::polkavm_evm_comparison::compare(
context, context,
arguments[0].into_int_value(), arguments[0].into_int_value(),
@@ -538,7 +540,7 @@ where
.map(Some) .map(Some)
} }
InstructionName::SGT => { InstructionName::SGT => {
let arguments = self.pop_arguments_llvm(context)?; let arguments = self.pop_arguments_llvm(context);
revive_llvm_context::polkavm_evm_comparison::compare( revive_llvm_context::polkavm_evm_comparison::compare(
context, context,
arguments[0].into_int_value(), arguments[0].into_int_value(),
@@ -549,7 +551,7 @@ where
} }
InstructionName::OR => { InstructionName::OR => {
let arguments = self.pop_arguments_llvm(context)?; let arguments = self.pop_arguments_llvm(context);
revive_llvm_context::polkavm_evm_bitwise::or( revive_llvm_context::polkavm_evm_bitwise::or(
context, context,
arguments[0].into_int_value(), arguments[0].into_int_value(),
@@ -558,7 +560,7 @@ where
.map(Some) .map(Some)
} }
InstructionName::XOR => { InstructionName::XOR => {
let arguments = self.pop_arguments_llvm(context)?; let arguments = self.pop_arguments_llvm(context);
revive_llvm_context::polkavm_evm_bitwise::xor( revive_llvm_context::polkavm_evm_bitwise::xor(
context, context,
arguments[0].into_int_value(), arguments[0].into_int_value(),
@@ -567,7 +569,7 @@ where
.map(Some) .map(Some)
} }
InstructionName::NOT => { InstructionName::NOT => {
let arguments = self.pop_arguments_llvm(context)?; let arguments = self.pop_arguments_llvm(context);
revive_llvm_context::polkavm_evm_bitwise::xor( revive_llvm_context::polkavm_evm_bitwise::xor(
context, context,
arguments[0].into_int_value(), arguments[0].into_int_value(),
@@ -576,7 +578,7 @@ where
.map(Some) .map(Some)
} }
InstructionName::AND => { InstructionName::AND => {
let arguments = self.pop_arguments_llvm(context)?; let arguments = self.pop_arguments_llvm(context);
revive_llvm_context::polkavm_evm_bitwise::and( revive_llvm_context::polkavm_evm_bitwise::and(
context, context,
arguments[0].into_int_value(), arguments[0].into_int_value(),
@@ -585,7 +587,7 @@ where
.map(Some) .map(Some)
} }
InstructionName::SHL => { InstructionName::SHL => {
let arguments = self.pop_arguments_llvm(context)?; let arguments = self.pop_arguments_llvm(context);
revive_llvm_context::polkavm_evm_bitwise::shift_left( revive_llvm_context::polkavm_evm_bitwise::shift_left(
context, context,
arguments[0].into_int_value(), arguments[0].into_int_value(),
@@ -594,7 +596,7 @@ where
.map(Some) .map(Some)
} }
InstructionName::SHR => { InstructionName::SHR => {
let arguments = self.pop_arguments_llvm(context)?; let arguments = self.pop_arguments_llvm(context);
revive_llvm_context::polkavm_evm_bitwise::shift_right( revive_llvm_context::polkavm_evm_bitwise::shift_right(
context, context,
arguments[0].into_int_value(), arguments[0].into_int_value(),
@@ -603,7 +605,7 @@ where
.map(Some) .map(Some)
} }
InstructionName::SAR => { InstructionName::SAR => {
let arguments = self.pop_arguments_llvm(context)?; let arguments = self.pop_arguments_llvm(context);
revive_llvm_context::polkavm_evm_bitwise::shift_right_arithmetic( revive_llvm_context::polkavm_evm_bitwise::shift_right_arithmetic(
context, context,
arguments[0].into_int_value(), arguments[0].into_int_value(),
@@ -612,7 +614,7 @@ where
.map(Some) .map(Some)
} }
InstructionName::BYTE => { InstructionName::BYTE => {
let arguments = self.pop_arguments_llvm(context)?; let arguments = self.pop_arguments_llvm(context);
revive_llvm_context::polkavm_evm_bitwise::byte( revive_llvm_context::polkavm_evm_bitwise::byte(
context, context,
arguments[0].into_int_value(), arguments[0].into_int_value(),
@@ -622,7 +624,7 @@ where
} }
InstructionName::ADDMOD => { InstructionName::ADDMOD => {
let arguments = self.pop_arguments_llvm(context)?; let arguments = self.pop_arguments_llvm(context);
revive_llvm_context::polkavm_evm_math::add_mod( revive_llvm_context::polkavm_evm_math::add_mod(
context, context,
arguments[0].into_int_value(), arguments[0].into_int_value(),
@@ -632,7 +634,7 @@ where
.map(Some) .map(Some)
} }
InstructionName::MULMOD => { InstructionName::MULMOD => {
let arguments = self.pop_arguments_llvm(context)?; let arguments = self.pop_arguments_llvm(context);
revive_llvm_context::polkavm_evm_math::mul_mod( revive_llvm_context::polkavm_evm_math::mul_mod(
context, context,
arguments[0].into_int_value(), arguments[0].into_int_value(),
@@ -642,7 +644,7 @@ where
.map(Some) .map(Some)
} }
InstructionName::EXP => { InstructionName::EXP => {
let arguments = self.pop_arguments_llvm(context)?; let arguments = self.pop_arguments_llvm(context);
revive_llvm_context::polkavm_evm_math::exponent( revive_llvm_context::polkavm_evm_math::exponent(
context, context,
arguments[0].into_int_value(), arguments[0].into_int_value(),
@@ -651,7 +653,7 @@ where
.map(Some) .map(Some)
} }
InstructionName::SIGNEXTEND => { InstructionName::SIGNEXTEND => {
let arguments = self.pop_arguments_llvm(context)?; let arguments = self.pop_arguments_llvm(context);
revive_llvm_context::polkavm_evm_math::sign_extend( revive_llvm_context::polkavm_evm_math::sign_extend(
context, context,
arguments[0].into_int_value(), arguments[0].into_int_value(),
@@ -661,7 +663,7 @@ where
} }
InstructionName::SHA3 | InstructionName::KECCAK256 => { InstructionName::SHA3 | InstructionName::KECCAK256 => {
let arguments = self.pop_arguments_llvm(context)?; let arguments = self.pop_arguments_llvm(context);
revive_llvm_context::polkavm_evm_crypto::sha3( revive_llvm_context::polkavm_evm_crypto::sha3(
context, context,
arguments[0].into_int_value(), arguments[0].into_int_value(),
@@ -671,7 +673,7 @@ where
} }
InstructionName::MLOAD => { InstructionName::MLOAD => {
let arguments = self.pop_arguments_llvm(context)?; let arguments = self.pop_arguments_llvm(context);
revive_llvm_context::polkavm_evm_memory::load( revive_llvm_context::polkavm_evm_memory::load(
context, context,
arguments[0].into_int_value(), arguments[0].into_int_value(),
@@ -679,7 +681,7 @@ where
.map(Some) .map(Some)
} }
InstructionName::MSTORE => { InstructionName::MSTORE => {
let arguments = self.pop_arguments_llvm(context)?; let arguments = self.pop_arguments_llvm(context);
revive_llvm_context::polkavm_evm_memory::store( revive_llvm_context::polkavm_evm_memory::store(
context, context,
arguments[0].into_int_value(), arguments[0].into_int_value(),
@@ -688,7 +690,7 @@ where
.map(|_| None) .map(|_| None)
} }
InstructionName::MSTORE8 => { InstructionName::MSTORE8 => {
let arguments = self.pop_arguments_llvm(context)?; let arguments = self.pop_arguments_llvm(context);
revive_llvm_context::polkavm_evm_memory::store_byte( revive_llvm_context::polkavm_evm_memory::store_byte(
context, context,
arguments[0].into_int_value(), arguments[0].into_int_value(),
@@ -697,7 +699,7 @@ where
.map(|_| None) .map(|_| None)
} }
InstructionName::MCOPY => { InstructionName::MCOPY => {
let arguments = self.pop_arguments_llvm(context)?; let arguments = self.pop_arguments_llvm(context);
let destination = revive_llvm_context::PolkaVMPointer::new_with_offset( let destination = revive_llvm_context::PolkaVMPointer::new_with_offset(
context, context,
revive_llvm_context::PolkaVMAddressSpace::Heap, revive_llvm_context::PolkaVMAddressSpace::Heap,
@@ -723,7 +725,7 @@ where
} }
InstructionName::SLOAD => { InstructionName::SLOAD => {
let arguments = self.pop_arguments_llvm(context)?; let arguments = self.pop_arguments_llvm(context);
revive_llvm_context::polkavm_evm_storage::load( revive_llvm_context::polkavm_evm_storage::load(
context, context,
arguments[0].into_int_value(), arguments[0].into_int_value(),
@@ -731,7 +733,7 @@ where
.map(Some) .map(Some)
} }
InstructionName::SSTORE => { InstructionName::SSTORE => {
let arguments = self.pop_arguments_llvm(context)?; let arguments = self.pop_arguments_llvm(context);
revive_llvm_context::polkavm_evm_storage::store( revive_llvm_context::polkavm_evm_storage::store(
context, context,
arguments[0].into_int_value(), arguments[0].into_int_value(),
@@ -740,7 +742,7 @@ where
.map(|_| None) .map(|_| None)
} }
InstructionName::TLOAD => { InstructionName::TLOAD => {
let arguments = self.pop_arguments_llvm(context)?; let arguments = self.pop_arguments_llvm(context);
revive_llvm_context::polkavm_evm_storage::transient_load( revive_llvm_context::polkavm_evm_storage::transient_load(
context, context,
arguments[0].into_int_value(), arguments[0].into_int_value(),
@@ -748,7 +750,7 @@ where
.map(Some) .map(Some)
} }
InstructionName::TSTORE => { InstructionName::TSTORE => {
let arguments = self.pop_arguments_llvm(context)?; let arguments = self.pop_arguments_llvm(context);
revive_llvm_context::polkavm_evm_storage::transient_store( revive_llvm_context::polkavm_evm_storage::transient_store(
context, context,
arguments[0].into_int_value(), arguments[0].into_int_value(),
@@ -764,28 +766,27 @@ where
let offset = context let offset = context
.solidity_mut() .solidity_mut()
.get_or_allocate_immutable(key.as_str()) .get_or_allocate_immutable(key.as_str());
/ revive_common::BYTE_LENGTH_WORD;
let index = context.xlen_type().const_int(offset as u64, false); let index = context.word_const(offset as u64);
revive_llvm_context::polkavm_evm_immutable::load(context, index).map(Some) revive_llvm_context::polkavm_evm_immutable::load(context, index).map(Some)
} }
InstructionName::ASSIGNIMMUTABLE => { InstructionName::ASSIGNIMMUTABLE => {
let mut arguments = self.pop_arguments_llvm(context)?; let mut arguments = self.pop_arguments_llvm(context);
let key = self let key = self
.instruction .instruction
.value .value
.ok_or_else(|| anyhow::anyhow!("Instruction value missing"))?; .ok_or_else(|| anyhow::anyhow!("Instruction value missing"))?;
let offset = context.solidity_mut().allocate_immutable(key.as_str()) let offset = context.solidity_mut().allocate_immutable(key.as_str());
/ revive_common::BYTE_LENGTH_WORD;
let index = context.xlen_type().const_int(offset as u64, false); let index = context.word_const(offset as u64);
let value = arguments.pop().expect("Always exists").into_int_value(); let value = arguments.pop().expect("Always exists").into_int_value();
revive_llvm_context::polkavm_evm_immutable::store(context, index, value) revive_llvm_context::polkavm_evm_immutable::store(context, index, value)
.map(|_| None) .map(|_| None)
} }
InstructionName::CALLDATALOAD => { InstructionName::CALLDATALOAD => {
match context match context
.code_type() .code_type()
@@ -795,7 +796,7 @@ where
Ok(Some(context.word_const(0).as_basic_value_enum())) Ok(Some(context.word_const(0).as_basic_value_enum()))
} }
revive_llvm_context::PolkaVMCodeType::Runtime => { revive_llvm_context::PolkaVMCodeType::Runtime => {
let arguments = self.pop_arguments_llvm(context)?; let arguments = self.pop_arguments_llvm(context);
revive_llvm_context::polkavm_evm_calldata::load( revive_llvm_context::polkavm_evm_calldata::load(
context, context,
arguments[0].into_int_value(), arguments[0].into_int_value(),
@@ -818,7 +819,7 @@ where
} }
} }
InstructionName::CALLDATACOPY => { InstructionName::CALLDATACOPY => {
let arguments = self.pop_arguments_llvm(context)?; let arguments = self.pop_arguments_llvm(context);
match context match context
.code_type() .code_type()
@@ -861,7 +862,7 @@ where
} }
} }
InstructionName::CODECOPY => { InstructionName::CODECOPY => {
let arguments = self.pop_arguments_llvm(context)?; let arguments = self.pop_arguments_llvm(context);
let parent = context.module().get_name().to_str().expect("Always valid"); let parent = context.module().get_name().to_str().expect("Always valid");
let source = &self.stack_input.elements[1]; let source = &self.stack_input.elements[1];
@@ -916,7 +917,7 @@ where
revive_llvm_context::polkavm_evm_return_data::size(context).map(Some) revive_llvm_context::polkavm_evm_return_data::size(context).map(Some)
} }
InstructionName::RETURNDATACOPY => { InstructionName::RETURNDATACOPY => {
let arguments = self.pop_arguments_llvm(context)?; let arguments = self.pop_arguments_llvm(context);
revive_llvm_context::polkavm_evm_return_data::copy( revive_llvm_context::polkavm_evm_return_data::copy(
context, context,
arguments[0].into_int_value(), arguments[0].into_int_value(),
@@ -926,7 +927,7 @@ where
.map(|_| None) .map(|_| None)
} }
InstructionName::EXTCODESIZE => { InstructionName::EXTCODESIZE => {
let arguments = self.pop_arguments_llvm(context)?; let arguments = self.pop_arguments_llvm(context);
revive_llvm_context::polkavm_evm_ext_code::size( revive_llvm_context::polkavm_evm_ext_code::size(
context, context,
Some(arguments[0].into_int_value()), Some(arguments[0].into_int_value()),
@@ -934,7 +935,7 @@ where
.map(Some) .map(Some)
} }
InstructionName::EXTCODEHASH => { InstructionName::EXTCODEHASH => {
let arguments = self.pop_arguments_llvm(context)?; let arguments = self.pop_arguments_llvm(context);
revive_llvm_context::polkavm_evm_ext_code::hash( revive_llvm_context::polkavm_evm_ext_code::hash(
context, context,
arguments[0].into_int_value(), arguments[0].into_int_value(),
@@ -943,7 +944,7 @@ where
} }
InstructionName::RETURN => { InstructionName::RETURN => {
let arguments = self.pop_arguments_llvm(context)?; let arguments = self.pop_arguments_llvm(context);
revive_llvm_context::polkavm_evm_return::r#return( revive_llvm_context::polkavm_evm_return::r#return(
context, context,
arguments[0].into_int_value(), arguments[0].into_int_value(),
@@ -952,7 +953,7 @@ where
.map(|_| None) .map(|_| None)
} }
InstructionName::REVERT => { InstructionName::REVERT => {
let arguments = self.pop_arguments_llvm(context)?; let arguments = self.pop_arguments_llvm(context);
revive_llvm_context::polkavm_evm_return::revert( revive_llvm_context::polkavm_evm_return::revert(
context, context,
arguments[0].into_int_value(), arguments[0].into_int_value(),
@@ -968,7 +969,7 @@ where
} }
InstructionName::LOG0 => { InstructionName::LOG0 => {
let mut arguments = self.pop_arguments_llvm(context)?; let mut arguments = self.pop_arguments_llvm(context);
revive_llvm_context::polkavm_evm_event::log( revive_llvm_context::polkavm_evm_event::log(
context, context,
arguments.remove(0).into_int_value(), arguments.remove(0).into_int_value(),
@@ -981,7 +982,7 @@ where
.map(|_| None) .map(|_| None)
} }
InstructionName::LOG1 => { InstructionName::LOG1 => {
let mut arguments = self.pop_arguments_llvm(context)?; let mut arguments = self.pop_arguments_llvm(context);
revive_llvm_context::polkavm_evm_event::log( revive_llvm_context::polkavm_evm_event::log(
context, context,
arguments.remove(0).into_int_value(), arguments.remove(0).into_int_value(),
@@ -994,7 +995,7 @@ where
.map(|_| None) .map(|_| None)
} }
InstructionName::LOG2 => { InstructionName::LOG2 => {
let mut arguments = self.pop_arguments_llvm(context)?; let mut arguments = self.pop_arguments_llvm(context);
revive_llvm_context::polkavm_evm_event::log( revive_llvm_context::polkavm_evm_event::log(
context, context,
arguments.remove(0).into_int_value(), arguments.remove(0).into_int_value(),
@@ -1007,7 +1008,7 @@ where
.map(|_| None) .map(|_| None)
} }
InstructionName::LOG3 => { InstructionName::LOG3 => {
let mut arguments = self.pop_arguments_llvm(context)?; let mut arguments = self.pop_arguments_llvm(context);
revive_llvm_context::polkavm_evm_event::log( revive_llvm_context::polkavm_evm_event::log(
context, context,
arguments.remove(0).into_int_value(), arguments.remove(0).into_int_value(),
@@ -1020,7 +1021,7 @@ where
.map(|_| None) .map(|_| None)
} }
InstructionName::LOG4 => { InstructionName::LOG4 => {
let mut arguments = self.pop_arguments_llvm(context)?; let mut arguments = self.pop_arguments_llvm(context);
revive_llvm_context::polkavm_evm_event::log( revive_llvm_context::polkavm_evm_event::log(
context, context,
arguments.remove(0).into_int_value(), arguments.remove(0).into_int_value(),
@@ -1034,7 +1035,7 @@ where
} }
InstructionName::CALL => { InstructionName::CALL => {
let mut arguments = self.pop_arguments_llvm(context)?; let mut arguments = self.pop_arguments_llvm(context);
let gas = arguments.remove(0).into_int_value(); let gas = arguments.remove(0).into_int_value();
let address = arguments.remove(0).into_int_value(); let address = arguments.remove(0).into_int_value();
@@ -1059,7 +1060,7 @@ where
.map(Some) .map(Some)
} }
InstructionName::STATICCALL => { InstructionName::STATICCALL => {
let mut arguments = self.pop_arguments_llvm(context)?; let mut arguments = self.pop_arguments_llvm(context);
let gas = arguments.remove(0).into_int_value(); let gas = arguments.remove(0).into_int_value();
let address = arguments.remove(0).into_int_value(); let address = arguments.remove(0).into_int_value();
@@ -1083,7 +1084,7 @@ where
.map(Some) .map(Some)
} }
InstructionName::DELEGATECALL => { InstructionName::DELEGATECALL => {
let mut arguments = self.pop_arguments_llvm(context)?; let mut arguments = self.pop_arguments_llvm(context);
let gas = arguments.remove(0).into_int_value(); let gas = arguments.remove(0).into_int_value();
let address = arguments.remove(0).into_int_value(); let address = arguments.remove(0).into_int_value();
@@ -1107,7 +1108,7 @@ where
} }
InstructionName::CREATE | InstructionName::ZK_CREATE => { InstructionName::CREATE | InstructionName::ZK_CREATE => {
let arguments = self.pop_arguments_llvm(context)?; let arguments = self.pop_arguments_llvm(context);
let value = arguments[0].into_int_value(); let value = arguments[0].into_int_value();
let input_offset = arguments[1].into_int_value(); let input_offset = arguments[1].into_int_value();
@@ -1118,19 +1119,18 @@ where
value, value,
input_offset, input_offset,
input_length, input_length,
None,
) )
.map(Some) .map(Some)
} }
InstructionName::CREATE2 | InstructionName::ZK_CREATE2 => { InstructionName::CREATE2 | InstructionName::ZK_CREATE2 => {
let arguments = self.pop_arguments_llvm(context)?; let arguments = self.pop_arguments_llvm(context);
let value = arguments[0].into_int_value(); let value = arguments[0].into_int_value();
let input_offset = arguments[1].into_int_value(); let input_offset = arguments[1].into_int_value();
let input_length = arguments[2].into_int_value(); let input_length = arguments[2].into_int_value();
let salt = arguments[3].into_int_value(); let salt = arguments[3].into_int_value();
revive_llvm_context::polkavm_evm_create::create( revive_llvm_context::polkavm_evm_create::create2(
context, context,
value, value,
input_offset, input_offset,
@@ -1154,14 +1154,12 @@ where
revive_llvm_context::polkavm_evm_ether_gas::gas(context).map(Some) revive_llvm_context::polkavm_evm_ether_gas::gas(context).map(Some)
} }
InstructionName::BALANCE => { InstructionName::BALANCE => {
let arguments = self.pop_arguments_llvm(context)?; let arguments = self.pop_arguments_llvm(context);
let address = arguments[0].into_int_value(); let address = arguments[0].into_int_value();
revive_llvm_context::polkavm_evm_ether_gas::balance(context, address).map(Some) revive_llvm_context::polkavm_evm_ether_gas::balance(context, address).map(Some)
} }
InstructionName::SELFBALANCE => { InstructionName::SELFBALANCE => todo!(),
revive_llvm_context::polkavm_evm_ether_gas::self_balance(context).map(Some)
}
InstructionName::GASLIMIT => { InstructionName::GASLIMIT => {
revive_llvm_context::polkavm_evm_contract_context::gas_limit(context).map(Some) revive_llvm_context::polkavm_evm_contract_context::gas_limit(context).map(Some)
@@ -1183,7 +1181,7 @@ where
revive_llvm_context::polkavm_evm_contract_context::block_number(context).map(Some) revive_llvm_context::polkavm_evm_contract_context::block_number(context).map(Some)
} }
InstructionName::BLOCKHASH => { InstructionName::BLOCKHASH => {
let arguments = self.pop_arguments_llvm(context)?; let arguments = self.pop_arguments_llvm(context);
let index = arguments[0].into_int_value(); let index = arguments[0].into_int_value();
revive_llvm_context::polkavm_evm_contract_context::block_hash(context, index) revive_llvm_context::polkavm_evm_contract_context::block_hash(context, index)
@@ -1206,7 +1204,7 @@ where
anyhow::bail!("The `BLOBBASEFEE` instruction is not supported until zkVM v1.5.0"); anyhow::bail!("The `BLOBBASEFEE` instruction is not supported until zkVM v1.5.0");
} }
InstructionName::MSIZE => { InstructionName::MSIZE => {
revive_llvm_context::polkavm_evm_memory::msize(context).map(Some) revive_llvm_context::polkavm_evm_contract_context::msize(context).map(Some)
} }
InstructionName::CALLCODE => { InstructionName::CALLCODE => {
@@ -1221,7 +1219,7 @@ where
anyhow::bail!("The `EXTCODECOPY` instruction is not supported"); anyhow::bail!("The `EXTCODECOPY` instruction is not supported");
} }
InstructionName::SELFDESTRUCT => { InstructionName::SELFDESTRUCT => {
let _arguments = self.pop_arguments_llvm(context)?; let _arguments = self.pop_arguments_llvm(context);
anyhow::bail!("The `SELFDESTRUCT` instruction is not supported"); anyhow::bail!("The `SELFDESTRUCT` instruction is not supported");
} }
@@ -1233,7 +1231,7 @@ where
return_address, return_address,
.. ..
} => { } => {
let mut arguments = self.pop_arguments_llvm(context)?; let mut arguments = self.pop_arguments_llvm(context);
arguments.pop(); arguments.pop();
arguments.reverse(); arguments.reverse();
arguments.pop(); arguments.pop();
@@ -1296,7 +1294,7 @@ where
return Ok(()); return Ok(());
} }
InstructionName::RecursiveReturn { .. } => { InstructionName::RecursiveReturn { .. } => {
let mut arguments = self.pop_arguments_llvm(context)?; let mut arguments = self.pop_arguments_llvm(context);
arguments.reverse(); arguments.reverse();
arguments.pop(); arguments.pop();
@@ -1139,6 +1139,7 @@ where
.integer_type(revive_common::BIT_LENGTH_BOOLEAN) .integer_type(revive_common::BIT_LENGTH_BOOLEAN)
.as_basic_type_enum()], .as_basic_type_enum()],
output_size, output_size,
false,
); );
(r#type, output_size) (r#type, output_size)
} }
@@ -1155,6 +1156,7 @@ where
input_size input_size
], ],
output_size, output_size,
false,
); );
(r#type, output_size) (r#type, output_size)
} }
+54 -13
View File
@@ -7,7 +7,6 @@ pub(crate) mod missing_libraries;
pub(crate) mod process; pub(crate) mod process;
pub(crate) mod project; pub(crate) mod project;
pub(crate) mod solc; pub(crate) mod solc;
pub(crate) mod version;
pub(crate) mod warning; pub(crate) mod warning;
pub(crate) mod yul; pub(crate) mod yul;
@@ -39,7 +38,6 @@ pub use self::solc::standard_json::output::contract::Contract as SolcStandardJso
pub use self::solc::standard_json::output::Output as SolcStandardJsonOutput; pub use self::solc::standard_json::output::Output as SolcStandardJsonOutput;
pub use self::solc::version::Version as SolcVersion; pub use self::solc::version::Version as SolcVersion;
pub use self::solc::Compiler as SolcCompiler; pub use self::solc::Compiler as SolcCompiler;
pub use self::version::Version as ResolcVersion;
pub use self::warning::Warning; pub use self::warning::Warning;
pub mod test_utils; pub mod test_utils;
@@ -53,6 +51,7 @@ pub fn yul(
input_files: &[PathBuf], input_files: &[PathBuf],
solc: &mut SolcCompiler, solc: &mut SolcCompiler,
optimizer_settings: revive_llvm_context::OptimizerSettings, optimizer_settings: revive_llvm_context::OptimizerSettings,
is_system_mode: bool,
include_metadata_hash: bool, include_metadata_hash: bool,
debug_config: Option<revive_llvm_context::DebugConfig>, debug_config: Option<revive_llvm_context::DebugConfig>,
) -> anyhow::Result<Build> { ) -> anyhow::Result<Build> {
@@ -65,17 +64,28 @@ pub fn yul(
), ),
}; };
if solc.version()?.default != SolcCompiler::LAST_SUPPORTED_VERSION { let solc_validator = if is_system_mode {
anyhow::bail!( None
} else {
if solc.version()?.default != SolcCompiler::LAST_SUPPORTED_VERSION {
anyhow::bail!(
"The Yul mode is only supported with the most recent version of the Solidity compiler: {}", "The Yul mode is only supported with the most recent version of the Solidity compiler: {}",
SolcCompiler::LAST_SUPPORTED_VERSION, SolcCompiler::LAST_SUPPORTED_VERSION,
); );
} }
Some(&*solc)
};
let solc_validator = Some(&*solc);
let project = Project::try_from_yul_path(path, solc_validator)?; let project = Project::try_from_yul_path(path, solc_validator)?;
let build = project.compile(optimizer_settings, include_metadata_hash, debug_config)?; let build = project.compile(
optimizer_settings,
is_system_mode,
include_metadata_hash,
false,
debug_config,
)?;
Ok(build) Ok(build)
} }
@@ -84,6 +94,7 @@ pub fn yul(
pub fn llvm_ir( pub fn llvm_ir(
input_files: &[PathBuf], input_files: &[PathBuf],
optimizer_settings: revive_llvm_context::OptimizerSettings, optimizer_settings: revive_llvm_context::OptimizerSettings,
is_system_mode: bool,
include_metadata_hash: bool, include_metadata_hash: bool,
debug_config: Option<revive_llvm_context::DebugConfig>, debug_config: Option<revive_llvm_context::DebugConfig>,
) -> anyhow::Result<Build> { ) -> anyhow::Result<Build> {
@@ -98,7 +109,13 @@ pub fn llvm_ir(
let project = Project::try_from_llvm_ir_path(path)?; let project = Project::try_from_llvm_ir_path(path)?;
let build = project.compile(optimizer_settings, include_metadata_hash, debug_config)?; let build = project.compile(
optimizer_settings,
is_system_mode,
include_metadata_hash,
false,
debug_config,
)?;
Ok(build) Ok(build)
} }
@@ -113,6 +130,7 @@ pub fn standard_output(
solc_optimizer_enabled: bool, solc_optimizer_enabled: bool,
optimizer_settings: revive_llvm_context::OptimizerSettings, optimizer_settings: revive_llvm_context::OptimizerSettings,
force_evmla: bool, force_evmla: bool,
is_system_mode: bool,
include_metadata_hash: bool, include_metadata_hash: bool,
base_path: Option<String>, base_path: Option<String>,
include_paths: Vec<String>, include_paths: Vec<String>,
@@ -136,6 +154,7 @@ pub fn standard_output(
None, None,
&solc_version.default, &solc_version.default,
optimizer_settings.is_fallback_to_size_enabled(), optimizer_settings.is_fallback_to_size_enabled(),
optimizer_settings.is_system_request_memoization_disabled(),
), ),
None, None,
solc_pipeline == SolcPipeline::Yul, solc_pipeline == SolcPipeline::Yul,
@@ -181,7 +200,13 @@ pub fn standard_output(
debug_config.as_ref(), debug_config.as_ref(),
)?; )?;
let build = project.compile(optimizer_settings, include_metadata_hash, debug_config)?; let build = project.compile(
optimizer_settings,
is_system_mode,
include_metadata_hash,
false,
debug_config,
)?;
Ok(build) Ok(build)
} }
@@ -192,6 +217,7 @@ pub fn standard_json(
solc: &mut SolcCompiler, solc: &mut SolcCompiler,
detect_missing_libraries: bool, detect_missing_libraries: bool,
force_evmla: bool, force_evmla: bool,
is_system_mode: bool,
base_path: Option<String>, base_path: Option<String>,
include_paths: Vec<String>, include_paths: Vec<String>,
allow_paths: Option<String>, allow_paths: Option<String>,
@@ -199,6 +225,7 @@ pub fn standard_json(
) -> anyhow::Result<()> { ) -> anyhow::Result<()> {
let solc_version = solc.version()?; let solc_version = solc.version()?;
let solc_pipeline = SolcPipeline::new(&solc_version, force_evmla); let solc_pipeline = SolcPipeline::new(&solc_version, force_evmla);
let resolc_version = semver::Version::parse(env!("CARGO_PKG_VERSION")).expect("Always valid");
let solc_input = SolcStandardJsonInput::try_from_stdin(solc_pipeline)?; let solc_input = SolcStandardJsonInput::try_from_stdin(solc_pipeline)?;
let source_code_files = solc_input let source_code_files = solc_input
@@ -245,10 +272,20 @@ pub fn standard_json(
if detect_missing_libraries { if detect_missing_libraries {
let missing_libraries = project.get_missing_libraries(); let missing_libraries = project.get_missing_libraries();
missing_libraries.write_to_standard_json(&mut solc_output, &solc_version)?; missing_libraries.write_to_standard_json(
&mut solc_output,
&solc_version,
&resolc_version,
)?;
} else { } else {
let build = project.compile(optimizer_settings, include_metadata_hash, debug_config)?; let build = project.compile(
build.write_to_standard_json(&mut solc_output, &solc_version)?; optimizer_settings,
is_system_mode,
include_metadata_hash,
false,
debug_config,
)?;
build.write_to_standard_json(&mut solc_output, &solc_version, &resolc_version)?;
} }
serde_json::to_writer(std::io::stdout(), &solc_output)?; serde_json::to_writer(std::io::stdout(), &solc_output)?;
std::process::exit(0); std::process::exit(0);
@@ -265,6 +302,7 @@ pub fn combined_json(
solc_optimizer_enabled: bool, solc_optimizer_enabled: bool,
optimizer_settings: revive_llvm_context::OptimizerSettings, optimizer_settings: revive_llvm_context::OptimizerSettings,
force_evmla: bool, force_evmla: bool,
is_system_mode: bool,
include_metadata_hash: bool, include_metadata_hash: bool,
base_path: Option<String>, base_path: Option<String>,
include_paths: Vec<String>, include_paths: Vec<String>,
@@ -275,6 +313,8 @@ pub fn combined_json(
output_directory: Option<PathBuf>, output_directory: Option<PathBuf>,
overwrite: bool, overwrite: bool,
) -> anyhow::Result<()> { ) -> anyhow::Result<()> {
let resolc_version = semver::Version::parse(env!("CARGO_PKG_VERSION")).expect("Always valid");
let build = standard_output( let build = standard_output(
input_files, input_files,
libraries, libraries,
@@ -283,6 +323,7 @@ pub fn combined_json(
solc_optimizer_enabled, solc_optimizer_enabled,
optimizer_settings, optimizer_settings,
force_evmla, force_evmla,
is_system_mode,
include_metadata_hash, include_metadata_hash,
base_path, base_path,
include_paths, include_paths,
@@ -293,7 +334,7 @@ pub fn combined_json(
)?; )?;
let mut combined_json = solc.combined_json(input_files, format.as_str())?; let mut combined_json = solc.combined_json(input_files, format.as_str())?;
build.write_to_combined_json(&mut combined_json)?; build.write_to_combined_json(&mut combined_json, &resolc_version)?;
match output_directory { match output_directory {
Some(output_directory) => { Some(output_directory) => {
+2 -2
View File
@@ -5,7 +5,6 @@ use std::collections::HashSet;
use crate::solc::standard_json::output::Output as StandardJsonOutput; use crate::solc::standard_json::output::Output as StandardJsonOutput;
use crate::solc::version::Version as SolcVersion; use crate::solc::version::Version as SolcVersion;
use crate::ResolcVersion;
/// The missing Solidity libraries. /// The missing Solidity libraries.
pub struct MissingLibraries { pub struct MissingLibraries {
@@ -24,6 +23,7 @@ impl MissingLibraries {
mut self, mut self,
standard_json: &mut StandardJsonOutput, standard_json: &mut StandardJsonOutput,
solc_version: &SolcVersion, solc_version: &SolcVersion,
resolc_version: &semver::Version,
) -> anyhow::Result<()> { ) -> anyhow::Result<()> {
let contracts = match standard_json.contracts.as_mut() { let contracts = match standard_json.contracts.as_mut() {
Some(contracts) => contracts, Some(contracts) => contracts,
@@ -43,7 +43,7 @@ impl MissingLibraries {
standard_json.version = Some(solc_version.default.to_string()); standard_json.version = Some(solc_version.default.to_string());
standard_json.long_version = Some(solc_version.long.to_owned()); standard_json.long_version = Some(solc_version.long.to_owned());
standard_json.revive_version = Some(ResolcVersion::default().long); standard_json.zk_version = Some(resolc_version.to_string());
Ok(()) Ok(())
} }
+8
View File
@@ -14,8 +14,12 @@ pub struct Input {
pub contract: Contract, pub contract: Contract,
/// The project representation. /// The project representation.
pub project: Project, pub project: Project,
/// The system mode flag.
pub is_system_mode: bool,
/// Whether to append the metadata hash. /// Whether to append the metadata hash.
pub include_metadata_hash: bool, pub include_metadata_hash: bool,
/// Enables the test bytecode encoding.
pub enable_test_encoding: bool,
/// The optimizer settings. /// The optimizer settings.
pub optimizer_settings: revive_llvm_context::OptimizerSettings, pub optimizer_settings: revive_llvm_context::OptimizerSettings,
/// The debug output config. /// The debug output config.
@@ -27,14 +31,18 @@ impl Input {
pub fn new( pub fn new(
contract: Contract, contract: Contract,
project: Project, project: Project,
is_system_mode: bool,
include_metadata_hash: bool, include_metadata_hash: bool,
enable_test_encoding: bool,
optimizer_settings: revive_llvm_context::OptimizerSettings, optimizer_settings: revive_llvm_context::OptimizerSettings,
debug_config: Option<revive_llvm_context::DebugConfig>, debug_config: Option<revive_llvm_context::DebugConfig>,
) -> Self { ) -> Self {
Self { Self {
contract, contract,
project, project,
is_system_mode,
include_metadata_hash, include_metadata_hash,
enable_test_encoding,
optimizer_settings, optimizer_settings,
debug_config, debug_config,
} }
+4
View File
@@ -40,9 +40,13 @@ pub fn run(input_file: Option<&mut std::fs::File>) -> anyhow::Result<()> {
} }
let input: Input = revive_common::deserialize_from_slice(buffer.as_slice())?; let input: Input = revive_common::deserialize_from_slice(buffer.as_slice())?;
if input.enable_test_encoding {
todo!()
}
let result = input.contract.compile( let result = input.contract.compile(
input.project, input.project,
input.optimizer_settings, input.optimizer_settings,
input.is_system_mode,
input.include_metadata_hash, input.include_metadata_hash,
input.debug_config, input.debug_config,
); );
@@ -2,8 +2,6 @@
use serde::Serialize; use serde::Serialize;
use crate::ResolcVersion;
/// The Solidity contract metadata. /// The Solidity contract metadata.
/// Is used to append the metadata hash to the contract bytecode. /// Is used to append the metadata hash to the contract bytecode.
#[derive(Debug, Serialize)] #[derive(Debug, Serialize)]
@@ -11,11 +9,11 @@ pub struct Metadata {
/// The `solc` metadata. /// The `solc` metadata.
pub solc_metadata: serde_json::Value, pub solc_metadata: serde_json::Value,
/// The `solc` version. /// The `solc` version.
pub solc_version: String, pub solc_version: semver::Version,
/// The pallet revive edition. /// The zkVM `solc` edition.
pub revive_pallet_version: Option<semver::Version>, pub solc_zkvm_edition: Option<semver::Version>,
/// The PolkaVM compiler version. /// The PolkaVM compiler version.
pub revive_version: String, pub zk_version: semver::Version,
/// The PolkaVM compiler optimizer settings. /// The PolkaVM compiler optimizer settings.
pub optimizer_settings: revive_llvm_context::OptimizerSettings, pub optimizer_settings: revive_llvm_context::OptimizerSettings,
} }
@@ -24,15 +22,16 @@ impl Metadata {
/// A shortcut constructor. /// A shortcut constructor.
pub fn new( pub fn new(
solc_metadata: serde_json::Value, solc_metadata: serde_json::Value,
solc_version: String, solc_version: semver::Version,
revive_pallet_version: Option<semver::Version>, solc_zkvm_edition: Option<semver::Version>,
zk_version: semver::Version,
optimizer_settings: revive_llvm_context::OptimizerSettings, optimizer_settings: revive_llvm_context::OptimizerSettings,
) -> Self { ) -> Self {
Self { Self {
solc_metadata, solc_metadata,
solc_version, solc_version,
revive_pallet_version, solc_zkvm_edition,
revive_version: ResolcVersion::default().long, zk_version,
optimizer_settings, optimizer_settings,
} }
} }
+5 -2
View File
@@ -78,6 +78,7 @@ impl Contract {
mut self, mut self,
project: Project, project: Project,
optimizer_settings: revive_llvm_context::OptimizerSettings, optimizer_settings: revive_llvm_context::OptimizerSettings,
is_system_mode: bool,
include_metadata_hash: bool, include_metadata_hash: bool,
debug_config: Option<revive_llvm_context::DebugConfig>, debug_config: Option<revive_llvm_context::DebugConfig>,
) -> anyhow::Result<ContractBuild> { ) -> anyhow::Result<ContractBuild> {
@@ -89,8 +90,9 @@ impl Contract {
let metadata = Metadata::new( let metadata = Metadata::new(
self.metadata_json.take(), self.metadata_json.take(),
version.long.clone(), version.default.clone(),
version.l2_revision.clone(), version.l2_revision.clone(),
semver::Version::parse(env!("CARGO_PKG_VERSION")).expect("Always valid"),
optimizer.settings().to_owned(), optimizer.settings().to_owned(),
); );
let metadata_json = serde_json::to_value(&metadata).expect("Always valid"); let metadata_json = serde_json::to_value(&metadata).expect("Always valid");
@@ -125,7 +127,8 @@ impl Contract {
context.set_solidity_data(revive_llvm_context::PolkaVMContextSolidityData::default()); context.set_solidity_data(revive_llvm_context::PolkaVMContextSolidityData::default());
match self.ir { match self.ir {
IR::Yul(_) => { IR::Yul(_) => {
context.set_yul_data(Default::default()); let yul_data = revive_llvm_context::PolkaVMContextYulData::new(is_system_mode);
context.set_yul_data(yul_data);
} }
IR::EVMLA(_) => { IR::EVMLA(_) => {
let evmla_data = revive_llvm_context::PolkaVMContextEVMLAData::new(version.default); let evmla_data = revive_llvm_context::PolkaVMContextEVMLAData::new(version.default);
+6
View File
@@ -62,7 +62,9 @@ impl Project {
pub fn compile( pub fn compile(
self, self,
optimizer_settings: revive_llvm_context::OptimizerSettings, optimizer_settings: revive_llvm_context::OptimizerSettings,
is_system_mode: bool,
include_metadata_hash: bool, include_metadata_hash: bool,
bytecode_encoding_testing: bool,
debug_config: Option<revive_llvm_context::DebugConfig>, debug_config: Option<revive_llvm_context::DebugConfig>,
) -> anyhow::Result<Build> { ) -> anyhow::Result<Build> {
let project = self.clone(); let project = self.clone();
@@ -73,7 +75,9 @@ impl Project {
let process_output = crate::process::call(ProcessInput::new( let process_output = crate::process::call(ProcessInput::new(
contract, contract,
project.clone(), project.clone(),
is_system_mode,
include_metadata_hash, include_metadata_hash,
bytecode_encoding_testing,
optimizer_settings.clone(), optimizer_settings.clone(),
debug_config.clone(), debug_config.clone(),
)); ));
@@ -237,6 +241,7 @@ impl revive_llvm_context::PolkaVMDependency for Project {
project: Self, project: Self,
identifier: &str, identifier: &str,
optimizer_settings: revive_llvm_context::OptimizerSettings, optimizer_settings: revive_llvm_context::OptimizerSettings,
is_system_mode: bool,
include_metadata_hash: bool, include_metadata_hash: bool,
debug_config: Option<revive_llvm_context::DebugConfig>, debug_config: Option<revive_llvm_context::DebugConfig>,
) -> anyhow::Result<String> { ) -> anyhow::Result<String> {
@@ -256,6 +261,7 @@ impl revive_llvm_context::PolkaVMDependency for Project {
.compile( .compile(
project, project,
optimizer_settings, optimizer_settings,
is_system_mode,
include_metadata_hash, include_metadata_hash,
debug_config, debug_config,
) )
+25 -3
View File
@@ -62,6 +62,10 @@ pub struct Arguments {
#[structopt(long = "fallback-Oz")] #[structopt(long = "fallback-Oz")]
pub fallback_to_optimizing_for_size: bool, pub fallback_to_optimizing_for_size: bool,
/// Disable the system request memoization.
#[structopt(long = "disable-system-request-memoization")]
pub disable_system_request_memoization: bool,
/// Disable the `solc` optimizer. /// Disable the `solc` optimizer.
/// Use it if your project uses the `MSIZE` instruction, or in other cases. /// Use it if your project uses the `MSIZE` instruction, or in other cases.
/// Beware that it will prevent libraries from being inlined. /// Beware that it will prevent libraries from being inlined.
@@ -75,7 +79,7 @@ pub struct Arguments {
pub solc: Option<String>, pub solc: Option<String>,
/// The EVM target version to generate IR for. /// The EVM target version to generate IR for.
/// See https://github.com/paritytech/revive/blob/main/crates/common/src/evm_version.rs for reference. /// See https://github.com/xermicus/revive/blob/main/crates/common/src/evm_version.rs for reference.
#[structopt(long = "evm-version")] #[structopt(long = "evm-version")]
pub evm_version: Option<String>, pub evm_version: Option<String>,
@@ -119,6 +123,13 @@ pub struct Arguments {
#[structopt(long = "force-evmla")] #[structopt(long = "force-evmla")]
pub force_evmla: bool, pub force_evmla: bool,
/// Enable system contract compilation mode.
/// In this mode PolkaVM extensions are enabled. For example, calls to addresses `0xFFFF` and below
/// are substituted by special PolkaVM instructions.
/// In the Yul mode, the `verbatim_*` instruction family is available.
#[structopt(long = "system-mode")]
pub is_system_mode: bool,
/// Set metadata hash mode. /// Set metadata hash mode.
/// The only supported value is `none` that disables appending the metadata hash. /// The only supported value is `none` that disables appending the metadata hash.
/// Is enabled by default. /// Is enabled by default.
@@ -185,14 +196,14 @@ impl Arguments {
} }
#[cfg(debug_assertions)] #[cfg(debug_assertions)]
if self.recursive_process_input.is_some() && !self.recursive_process { if self.recursive_process_input.is_none() && !self.recursive_process {
anyhow::bail!("--process-input can be only used when --recursive-process is given"); anyhow::bail!("--process-input can be only used when --recursive-process is given");
} }
#[cfg(debug_assertions)] #[cfg(debug_assertions)]
if self.recursive_process if self.recursive_process
&& ((self.recursive_process_input.is_none() && std::env::args().count() > 2) && ((self.recursive_process_input.is_none() && std::env::args().count() > 2)
|| (self.recursive_process_input.is_some() && std::env::args().count() > 4)) || (self.recursive_process_input.is_none() && std::env::args().count() > 4))
{ {
anyhow::bail!("No other options are allowed in recursive mode."); anyhow::bail!("No other options are allowed in recursive mode.");
} }
@@ -256,6 +267,12 @@ impl Arguments {
if self.solc.is_some() { if self.solc.is_some() {
anyhow::bail!("`solc` is not used in LLVM IR and PolkaVM assembly modes."); anyhow::bail!("`solc` is not used in LLVM IR and PolkaVM assembly modes.");
} }
if self.is_system_mode {
anyhow::bail!(
"System contract mode is not supported in LLVM IR and PolkaVM assembly modes."
);
}
} }
if self.combined_json.is_some() { if self.combined_json.is_some() {
@@ -302,6 +319,11 @@ impl Arguments {
"Falling back to -Oz must specified in standard JSON input settings." "Falling back to -Oz must specified in standard JSON input settings."
); );
} }
if self.disable_system_request_memoization {
anyhow::bail!(
"Disabling the system request memoization must specified in standard JSON input settings."
);
}
if self.metadata_hash.is_some() { if self.metadata_hash.is_some() {
anyhow::bail!("Metadata hash mode must specified in standard JSON input settings."); anyhow::bail!("Metadata hash mode must specified in standard JSON input settings.");
} }
+11 -2
View File
@@ -31,9 +31,10 @@ fn main_inner() -> anyhow::Result<()> {
if arguments.version { if arguments.version {
println!( println!(
"{} version {}", "{} v{} (LLVM build {:?})",
env!("CARGO_PKG_DESCRIPTION"), env!("CARGO_PKG_DESCRIPTION"),
revive_solidity::ResolcVersion::default().long env!("CARGO_PKG_VERSION"),
inkwell::support::get_llvm_version()
); );
return Ok(()); return Ok(());
} }
@@ -100,6 +101,9 @@ fn main_inner() -> anyhow::Result<()> {
if arguments.fallback_to_optimizing_for_size { if arguments.fallback_to_optimizing_for_size {
optimizer_settings.enable_fallback_to_size(); optimizer_settings.enable_fallback_to_size();
} }
if arguments.disable_system_request_memoization {
optimizer_settings.disable_system_request_memoization();
}
optimizer_settings.is_verify_each_enabled = arguments.llvm_verify_each; optimizer_settings.is_verify_each_enabled = arguments.llvm_verify_each;
optimizer_settings.is_debug_logging_enabled = arguments.llvm_debug_logging; optimizer_settings.is_debug_logging_enabled = arguments.llvm_debug_logging;
@@ -117,6 +121,7 @@ fn main_inner() -> anyhow::Result<()> {
input_files.as_slice(), input_files.as_slice(),
&mut solc, &mut solc,
optimizer_settings, optimizer_settings,
arguments.is_system_mode,
include_metadata_hash, include_metadata_hash,
debug_config, debug_config,
) )
@@ -124,6 +129,7 @@ fn main_inner() -> anyhow::Result<()> {
revive_solidity::llvm_ir( revive_solidity::llvm_ir(
input_files.as_slice(), input_files.as_slice(),
optimizer_settings, optimizer_settings,
arguments.is_system_mode,
include_metadata_hash, include_metadata_hash,
debug_config, debug_config,
) )
@@ -132,6 +138,7 @@ fn main_inner() -> anyhow::Result<()> {
&mut solc, &mut solc,
arguments.detect_missing_libraries, arguments.detect_missing_libraries,
arguments.force_evmla, arguments.force_evmla,
arguments.is_system_mode,
arguments.base_path, arguments.base_path,
arguments.include_paths, arguments.include_paths,
arguments.allow_paths, arguments.allow_paths,
@@ -148,6 +155,7 @@ fn main_inner() -> anyhow::Result<()> {
!arguments.disable_solc_optimizer, !arguments.disable_solc_optimizer,
optimizer_settings, optimizer_settings,
arguments.force_evmla, arguments.force_evmla,
arguments.is_system_mode,
include_metadata_hash, include_metadata_hash,
arguments.base_path, arguments.base_path,
arguments.include_paths, arguments.include_paths,
@@ -168,6 +176,7 @@ fn main_inner() -> anyhow::Result<()> {
!arguments.disable_solc_optimizer, !arguments.disable_solc_optimizer,
optimizer_settings, optimizer_settings,
arguments.force_evmla, arguments.force_evmla,
arguments.is_system_mode,
include_metadata_hash, include_metadata_hash,
arguments.base_path, arguments.base_path,
arguments.include_paths, arguments.include_paths,
@@ -28,7 +28,7 @@ pub struct CombinedJson {
pub version: String, pub version: String,
/// The `resolc` compiler version. /// The `resolc` compiler version.
#[serde(skip_serializing_if = "Option::is_none")] #[serde(skip_serializing_if = "Option::is_none")]
pub revive_version: Option<String>, pub zk_version: Option<String>,
} }
impl CombinedJson { impl CombinedJson {
+1 -1
View File
@@ -37,7 +37,7 @@ impl Compiler {
pub const FIRST_VIA_IR_VERSION: semver::Version = semver::Version::new(0, 8, 13); pub const FIRST_VIA_IR_VERSION: semver::Version = semver::Version::new(0, 8, 13);
/// The last supported version of `solc`. /// The last supported version of `solc`.
pub const LAST_SUPPORTED_VERSION: semver::Version = semver::Version::new(0, 8, 28); pub const LAST_SUPPORTED_VERSION: semver::Version = semver::Version::new(0, 8, 26);
/// A shortcut constructor. /// A shortcut constructor.
/// Different tools may use different `executable` names. For example, the integration tester /// Different tools may use different `executable` names. For example, the integration tester
@@ -22,6 +22,9 @@ pub struct Optimizer {
/// Whether to try to recompile with -Oz if the bytecode is too large. /// Whether to try to recompile with -Oz if the bytecode is too large.
#[serde(skip_serializing)] #[serde(skip_serializing)]
pub fallback_to_optimizing_for_size: Option<bool>, pub fallback_to_optimizing_for_size: Option<bool>,
/// Whether to disable the system request memoization.
#[serde(skip_serializing)]
pub disable_system_request_memoization: Option<bool>,
} }
impl Optimizer { impl Optimizer {
@@ -31,12 +34,14 @@ impl Optimizer {
mode: Option<char>, mode: Option<char>,
version: &semver::Version, version: &semver::Version,
fallback_to_optimizing_for_size: bool, fallback_to_optimizing_for_size: bool,
disable_system_request_memoization: bool,
) -> Self { ) -> Self {
Self { Self {
enabled, enabled,
mode, mode,
details: Some(Details::disabled(version)), details: Some(Details::disabled(version)),
fallback_to_optimizing_for_size: Some(fallback_to_optimizing_for_size), fallback_to_optimizing_for_size: Some(fallback_to_optimizing_for_size),
disable_system_request_memoization: Some(disable_system_request_memoization),
} }
} }
@@ -61,6 +66,9 @@ impl TryFrom<&Optimizer> for revive_llvm_context::OptimizerSettings {
if value.fallback_to_optimizing_for_size.unwrap_or_default() { if value.fallback_to_optimizing_for_size.unwrap_or_default() {
result.enable_fallback_to_size(); result.enable_fallback_to_size();
} }
if value.disable_system_request_memoization.unwrap_or_default() {
result.disable_system_request_memoization();
}
Ok(result) Ok(result)
} }
} }
@@ -41,9 +41,6 @@ pub enum Flag {
EVMBC, EVMBC,
#[serde(rename = "evm.deployedBytecode")] #[serde(rename = "evm.deployedBytecode")]
EVMDBC, EVMDBC,
/// The assembly code
#[serde(rename = "evm.assembly")]
Assembly,
} }
impl From<SolcPipeline> for Flag { impl From<SolcPipeline> for Flag {
@@ -69,7 +66,6 @@ impl std::fmt::Display for Flag {
Self::EVMLA => write!(f, "evm.legacyAssembly"), Self::EVMLA => write!(f, "evm.legacyAssembly"),
Self::EVMBC => write!(f, "evm.bytecode"), Self::EVMBC => write!(f, "evm.bytecode"),
Self::EVMDBC => write!(f, "evm.deployedBytecode"), Self::EVMDBC => write!(f, "evm.deployedBytecode"),
Self::Assembly => write!(f, "evm.assembly"),
} }
} }
} }
@@ -20,19 +20,15 @@ use self::extra_metadata::ExtraMetadata;
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct EVM { pub struct EVM {
/// The contract EVM legacy assembly code. /// The contract EVM legacy assembly code.
#[serde(rename = "legacyAssembly", skip_serializing_if = "Option::is_none")] #[serde(rename = "legacyAssembly")]
pub assembly: Option<Assembly>, pub assembly: Option<Assembly>,
/// The contract PolkaVM assembly code. /// The contract PolkaVM assembly code.
#[serde(rename = "assembly", skip_serializing_if = "Option::is_none")] #[serde(rename = "assembly")]
pub assembly_text: Option<String>, pub assembly_text: Option<String>,
/// The contract bytecode. /// The contract bytecode.
/// Is reset by that of PolkaVM before yielding the compiled project artifacts. /// Is reset by that of PolkaVM before yielding the compiled project artifacts.
#[serde(skip_serializing_if = "Option::is_none")]
pub bytecode: Option<Bytecode>, pub bytecode: Option<Bytecode>,
/// The deployed bytecode of the contract. /// The contract deployed bytecode.
/// It is overwritten with the PolkaVM blob before yielding the compiled project artifacts.
/// Hence it will be the same as the runtime code but we keep both for compatibility reasons.
#[serde(skip_serializing_if = "Option::is_none")]
pub deployed_bytecode: Option<DeployedBytecode>, pub deployed_bytecode: Option<DeployedBytecode>,
/// The contract function signatures. /// The contract function signatures.
#[serde(default, skip_serializing_if = "Option::is_none")] #[serde(default, skip_serializing_if = "Option::is_none")]
@@ -46,7 +42,6 @@ impl EVM {
/// Sets the PolkaVM assembly and bytecode. /// Sets the PolkaVM assembly and bytecode.
pub fn modify(&mut self, assembly_text: String, bytecode: String) { pub fn modify(&mut self, assembly_text: String, bytecode: String) {
self.assembly_text = Some(assembly_text); self.assembly_text = Some(assembly_text);
self.bytecode = Some(Bytecode::new(bytecode.clone())); self.bytecode = Some(Bytecode::new(bytecode));
self.deployed_bytecode = Some(DeployedBytecode::new(bytecode));
} }
} }
@@ -45,7 +45,7 @@ pub struct Output {
pub long_version: Option<String>, pub long_version: Option<String>,
/// The `resolc` compiler version. /// The `resolc` compiler version.
#[serde(skip_serializing_if = "Option::is_none")] #[serde(skip_serializing_if = "Option::is_none")]
pub revive_version: Option<String>, pub zk_version: Option<String>,
} }
impl Output { impl Output {
+36 -21
View File
@@ -3,6 +3,7 @@ use std::collections::BTreeMap;
use std::collections::BTreeSet; use std::collections::BTreeSet;
use std::collections::HashMap; use std::collections::HashMap;
use std::path::PathBuf; use std::path::PathBuf;
use std::str::FromStr;
use std::sync::Mutex; use std::sync::Mutex;
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
@@ -94,6 +95,7 @@ pub fn build_solidity_with_options(
None, None,
&solc_version.default, &solc_version.default,
false, false,
false,
), ),
None, None,
pipeline == SolcPipeline::Yul, pipeline == SolcPipeline::Yul,
@@ -104,8 +106,12 @@ pub fn build_solidity_with_options(
let project = output.try_to_project(sources, libraries, pipeline, &solc_version, None)?; let project = output.try_to_project(sources, libraries, pipeline, &solc_version, None)?;
let build: crate::Build = project.compile(optimizer_settings, false, None)?; let build: crate::Build = project.compile(optimizer_settings, false, false, false, None)?;
build.write_to_standard_json(&mut output, &solc_version)?; build.write_to_standard_json(
&mut output,
&solc_version,
&semver::Version::from_str(env!("CARGO_PKG_VERSION"))?,
)?;
Ok(output) Ok(output)
} }
@@ -138,6 +144,7 @@ pub fn build_solidity_with_options_evm(
None, None,
&solc_version.default, &solc_version.default,
false, false,
false,
), ),
None, None,
pipeline == SolcPipeline::Yul, pipeline == SolcPipeline::Yul,
@@ -186,7 +193,13 @@ pub fn build_solidity_and_detect_missing_libraries(
libraries.clone(), libraries.clone(),
None, None,
SolcStandardJsonInputSettingsSelection::new_required(pipeline), SolcStandardJsonInputSettingsSelection::new_required(pipeline),
SolcStandardJsonInputSettingsOptimizer::new(true, None, &solc_version.default, false), SolcStandardJsonInputSettingsOptimizer::new(
true,
None,
&solc_version.default,
false,
false,
),
None, None,
pipeline == SolcPipeline::Yul, pipeline == SolcPipeline::Yul,
None, None,
@@ -197,7 +210,11 @@ pub fn build_solidity_and_detect_missing_libraries(
let project = output.try_to_project(sources, libraries, pipeline, &solc_version, None)?; let project = output.try_to_project(sources, libraries, pipeline, &solc_version, None)?;
let missing_libraries = project.get_missing_libraries(); let missing_libraries = project.get_missing_libraries();
missing_libraries.write_to_standard_json(&mut output, &solc.version()?)?; missing_libraries.write_to_standard_json(
&mut output,
&solc.version()?,
&semver::Version::from_str(env!("CARGO_PKG_VERSION"))?,
)?;
Ok(output) Ok(output)
} }
@@ -212,7 +229,7 @@ pub fn build_yul(source_code: &str) -> anyhow::Result<()> {
let project = let project =
Project::try_from_yul_string(PathBuf::from("test.yul").as_path(), source_code, None)?; Project::try_from_yul_string(PathBuf::from("test.yul").as_path(), source_code, None)?;
let _build = project.compile(optimizer_settings, false, None)?; let _build = project.compile(optimizer_settings, false, false, false, None)?;
Ok(()) Ok(())
} }
@@ -223,14 +240,14 @@ pub fn check_solidity_warning(
warning_substring: &str, warning_substring: &str,
libraries: BTreeMap<String, BTreeMap<String, String>>, libraries: BTreeMap<String, BTreeMap<String, String>>,
pipeline: SolcPipeline, pipeline: SolcPipeline,
skip_for_revive_edition: bool, skip_for_zkvm_edition: bool,
suppressed_warnings: Option<Vec<Warning>>, suppressed_warnings: Option<Vec<Warning>>,
) -> anyhow::Result<bool> { ) -> anyhow::Result<bool> {
check_dependencies(); check_dependencies();
let mut solc = SolcCompiler::new(SolcCompiler::DEFAULT_EXECUTABLE_NAME.to_owned())?; let mut solc = SolcCompiler::new(SolcCompiler::DEFAULT_EXECUTABLE_NAME.to_owned())?;
let solc_version = solc.version()?; let solc_version = solc.version()?;
if skip_for_revive_edition && solc_version.l2_revision.is_some() { if skip_for_zkvm_edition && solc_version.l2_revision.is_some() {
return Ok(true); return Ok(true);
} }
@@ -242,7 +259,13 @@ pub fn check_solidity_warning(
libraries, libraries,
None, None,
SolcStandardJsonInputSettingsSelection::new_required(pipeline), SolcStandardJsonInputSettingsSelection::new_required(pipeline),
SolcStandardJsonInputSettingsOptimizer::new(true, None, &solc_version.default, false), SolcStandardJsonInputSettingsOptimizer::new(
true,
None,
&solc_version.default,
false,
false,
),
None, None,
pipeline == SolcPipeline::Yul, pipeline == SolcPipeline::Yul,
suppressed_warnings, suppressed_warnings,
@@ -267,26 +290,18 @@ pub fn compile_blob(contract_name: &str, source_code: &str) -> Vec<u8> {
/// Compile the EVM bin-runtime of `contract_name` found in given `source_code`. /// Compile the EVM bin-runtime of `contract_name` found in given `source_code`.
/// The `solc` optimizer will be enabled /// The `solc` optimizer will be enabled
pub fn compile_evm_bin_runtime(contract_name: &str, source_code: &str) -> Vec<u8> { pub fn compile_evm_bin_runtime(contract_name: &str, source_code: &str) -> Vec<u8> {
compile_evm(contract_name, source_code, true, true) compile_evm(contract_name, source_code, true)
} }
/// Compile the EVM bin of `contract_name` found in given `source_code`. /// Compile the EVM bin of `contract_name` found in given `source_code`.
/// The `solc` optimizer will be enabled /// The `solc` optimizer will be enabled
pub fn compile_evm_deploy_code( pub fn compile_evm_deploy_code(contract_name: &str, source_code: &str) -> Vec<u8> {
contract_name: &str, compile_evm(contract_name, source_code, false)
source_code: &str,
solc_optimizer_enabled: bool,
) -> Vec<u8> {
compile_evm(contract_name, source_code, solc_optimizer_enabled, false)
} }
fn compile_evm( fn compile_evm(contract_name: &str, source_code: &str, runtime: bool) -> Vec<u8> {
contract_name: &str,
source_code: &str,
solc_optimizer_enabled: bool,
runtime: bool,
) -> Vec<u8> {
let pipeline = SolcPipeline::Yul; let pipeline = SolcPipeline::Yul;
let solc_optimizer_enabled = true;
let id = CachedBlob { let id = CachedBlob {
contract_name: contract_name.to_owned(), contract_name: contract_name.to_owned(),
pipeline, pipeline,
@@ -3,7 +3,7 @@
"version": "1.0.0", "version": "1.0.0",
"title": "resolc CLI Tests", "title": "resolc CLI Tests",
"description": "Auto tests for verifying resolc CLI", "description": "Auto tests for verifying resolc CLI",
"repository": "https://github.com/paritytech/revive", "repository": "https://github.com/xermicus/revive",
"main": "index.js", "main": "index.js",
"private": true, "private": true,
"scripts": { "scripts": {
-30
View File
@@ -1,30 +0,0 @@
//! The resolc compiler version.
use serde::Deserialize;
use serde::Serialize;
/// The resolc compiler version.
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Version {
/// The long version string.
pub long: String,
/// The short `semver`.
pub default: semver::Version,
/// The LLVM version string.
pub llvm: semver::Version,
}
impl Default for Version {
fn default() -> Self {
let default = semver::Version::parse(env!("CARGO_PKG_VERSION")).expect("Always valid");
let commit = env!("GIT_COMMIT_HASH");
let (llvm_major, llvm_minor, llvm_patch) = inkwell::support::get_llvm_version();
let llvm = semver::Version::new(llvm_major as u64, llvm_minor as u64, llvm_patch as u64);
Self {
long: format!("{default}+commit.{commit}.llvm-{llvm}"),
default,
llvm,
}
}
}
@@ -125,6 +125,13 @@ impl FunctionCall {
let location = self.location; let location = self.location;
match self.name { match self.name {
Name::UserDefined(name)
if name.starts_with(
revive_llvm_context::PolkaVMFunction::ZKSYNC_NEAR_CALL_ABI_PREFIX,
) && context.is_system_mode() =>
{
unimplemented!();
}
Name::UserDefined(name) => { Name::UserDefined(name) => {
let mut values = Vec::with_capacity(self.arguments.len()); let mut values = Vec::with_capacity(self.arguments.len());
for argument in self.arguments.into_iter().rev() { for argument in self.arguments.into_iter().rev() {
@@ -494,30 +501,25 @@ impl FunctionCall {
) )
.map(|_| None) .map(|_| None)
} }
Name::LoadImmutable => { Name::LoadImmutable => todo!(),
let mut arguments = self.pop_arguments::<D, 1>(context)?;
let key = arguments[0].original.take().ok_or_else(|| {
anyhow::anyhow!("{} `load_immutable` literal is missing", location)
})?;
let offset = context
.solidity_mut()
.get_or_allocate_immutable(key.as_str())
/ revive_common::BYTE_LENGTH_WORD;
let index = context.xlen_type().const_int(offset as u64, false);
revive_llvm_context::polkavm_evm_immutable::load(context, index).map(Some)
}
Name::SetImmutable => { Name::SetImmutable => {
let mut arguments = self.pop_arguments::<D, 3>(context)?; let mut arguments = self.pop_arguments::<D, 3>(context)?;
let key = arguments[1].original.take().ok_or_else(|| { let key = arguments[1].original.take().ok_or_else(|| {
anyhow::anyhow!("{} `load_immutable` literal is missing", location) anyhow::anyhow!("{} `load_immutable` literal is missing", location)
})?; })?;
let offset = context.solidity_mut().allocate_immutable(key.as_str())
/ revive_common::BYTE_LENGTH_WORD; if key.as_str() == "library_deploy_address" {
let index = context.xlen_type().const_int(offset as u64, false); return Ok(None);
}
let offset = context.solidity_mut().allocate_immutable(key.as_str());
let index = context.word_const(offset as u64);
let value = arguments[2].value.into_int_value(); let value = arguments[2].value.into_int_value();
revive_llvm_context::polkavm_evm_immutable::store(context, index, value) revive_llvm_context::polkavm_evm_immutable::store(context, index, value)
.map(|_| None) .map(|_| None)
} }
Name::CallDataLoad => { Name::CallDataLoad => {
let arguments = self.pop_arguments_llvm::<D, 1>(context)?; let arguments = self.pop_arguments_llvm::<D, 1>(context)?;
@@ -829,7 +831,6 @@ impl FunctionCall {
value, value,
input_offset, input_offset,
input_length, input_length,
None,
) )
.map(Some) .map(Some)
} }
@@ -841,7 +842,7 @@ impl FunctionCall {
let input_length = arguments[2].into_int_value(); let input_length = arguments[2].into_int_value();
let salt = arguments[3].into_int_value(); let salt = arguments[3].into_int_value();
revive_llvm_context::polkavm_evm_create::create( revive_llvm_context::polkavm_evm_create::create2(
context, context,
value, value,
input_offset, input_offset,
@@ -911,9 +912,7 @@ impl FunctionCall {
let address = arguments[0].into_int_value(); let address = arguments[0].into_int_value();
revive_llvm_context::polkavm_evm_ether_gas::balance(context, address).map(Some) revive_llvm_context::polkavm_evm_ether_gas::balance(context, address).map(Some)
} }
Name::SelfBalance => { Name::SelfBalance => todo!(),
revive_llvm_context::polkavm_evm_ether_gas::self_balance(context).map(Some)
}
Name::GasLimit => { Name::GasLimit => {
revive_llvm_context::polkavm_evm_contract_context::gas_limit(context).map(Some) revive_llvm_context::polkavm_evm_contract_context::gas_limit(context).map(Some)
@@ -963,7 +962,9 @@ impl FunctionCall {
location location
); );
} }
Name::MSize => revive_llvm_context::polkavm_evm_memory::msize(context).map(Some), Name::MSize => {
revive_llvm_context::polkavm_evm_contract_context::msize(context).map(Some)
}
Name::Verbatim { Name::Verbatim {
input_size, input_size,
@@ -92,7 +92,37 @@ impl FunctionDefinition {
} }
} }
let (arguments, next) = Identifier::parse_typed_list(lexer, None)?; let (mut arguments, next) = Identifier::parse_typed_list(lexer, None)?;
if identifier
.inner
.contains(revive_llvm_context::PolkaVMFunction::ZKSYNC_NEAR_CALL_ABI_PREFIX)
{
if arguments.is_empty() {
return Err(ParserError::InvalidNumberOfArguments {
location,
identifier: identifier.inner,
expected: 1,
found: arguments.len(),
}
.into());
}
arguments.remove(0);
}
if identifier
.inner
.contains(revive_llvm_context::PolkaVMFunction::ZKSYNC_NEAR_CALL_ABI_EXCEPTION_HANDLER)
&& !arguments.is_empty()
{
return Err(ParserError::InvalidNumberOfArguments {
location,
identifier: identifier.inner,
expected: 0,
found: arguments.len(),
}
.into());
}
match crate::yul::parser::take_or_next(next, lexer)? { match crate::yul::parser::take_or_next(next, lexer)? {
Token { Token {
lexeme: Lexeme::Symbol(Symbol::ParenthesisRight), lexeme: Lexeme::Symbol(Symbol::ParenthesisRight),
@@ -204,7 +234,12 @@ where
}) })
.collect(); .collect();
let function_type = context.function_type(argument_types, self.result.len()); let function_type = context.function_type(
argument_types,
self.result.len(),
self.identifier
.starts_with(revive_llvm_context::PolkaVMFunction::ZKSYNC_NEAR_CALL_ABI_PREFIX),
);
let function = context.add_function( let function = context.add_function(
self.identifier.as_str(), self.identifier.as_str(),
@@ -275,12 +310,23 @@ where
yul_type.into_llvm(context) yul_type.into_llvm(context)
}) })
.collect(); .collect();
for (index, argument) in self.arguments.iter().enumerate() { for (mut index, argument) in self.arguments.iter().enumerate() {
let pointer = context.build_alloca(argument_types[index], argument.inner.as_str()); let pointer = context.build_alloca(argument_types[index], argument.inner.as_str());
context context
.current_function() .current_function()
.borrow_mut() .borrow_mut()
.insert_stack_pointer(argument.inner.clone(), pointer); .insert_stack_pointer(argument.inner.clone(), pointer);
if self
.identifier
.starts_with(revive_llvm_context::PolkaVMFunction::ZKSYNC_NEAR_CALL_ABI_PREFIX)
&& matches!(
context.current_function().borrow().r#return(),
revive_llvm_context::PolkaVMFunctionReturn::Compound { .. }
)
&& context.is_system_mode()
{
index += 1;
}
context.build_store( context.build_store(
pointer, pointer,
context.current_function().borrow().get_nth_param(index), context.current_function().borrow().get_nth_param(index),
@@ -308,6 +354,13 @@ where
let return_value = context.build_load(pointer, "return_value")?; let return_value = context.build_load(pointer, "return_value")?;
context.build_return(Some(&return_value)); context.build_return(Some(&return_value));
} }
revive_llvm_context::PolkaVMFunctionReturn::Compound { pointer, .. }
if context.current_function().borrow().name().starts_with(
revive_llvm_context::PolkaVMFunction::ZKSYNC_NEAR_CALL_ABI_PREFIX,
) =>
{
context.build_return(Some(&pointer.value));
}
revive_llvm_context::PolkaVMFunctionReturn::Compound { pointer, .. } => { revive_llvm_context::PolkaVMFunctionReturn::Compound { pointer, .. } => {
let return_value = context.build_load(pointer, "return_value")?; let return_value = context.build_load(pointer, "return_value")?;
context.build_return(Some(&return_value)); context.build_return(Some(&return_value));
@@ -471,6 +524,80 @@ object "Test" {
); );
} }
#[test]
fn error_invalid_number_of_arguments_near_call_abi() {
let input = r#"
object "Test" {
code {
{
return(0, 0)
}
}
object "Test_deployed" {
code {
{
return(0, 0)
}
function ZKSYNC_NEAR_CALL_test() -> result {
result := 42
}
}
}
}
"#;
let mut lexer = Lexer::new(input.to_owned());
let result = Object::parse(&mut lexer, None);
assert_eq!(
result,
Err(Error::InvalidNumberOfArguments {
location: Location::new(14, 22),
identifier: "ZKSYNC_NEAR_CALL_test".to_owned(),
expected: 1,
found: 0,
}
.into())
);
}
#[test]
fn error_invalid_number_of_arguments_near_call_abi_catch() {
let input = r#"
object "Test" {
code {
{
return(0, 0)
}
}
object "Test_deployed" {
code {
{
return(0, 0)
}
function ZKSYNC_CATCH_NEAR_CALL(length) {
revert(0, length)
}
}
}
}
"#;
let mut lexer = Lexer::new(input.to_owned());
let result = Object::parse(&mut lexer, None);
assert_eq!(
result,
Err(Error::InvalidNumberOfArguments {
location: Location::new(14, 22),
identifier: "ZKSYNC_CATCH_NEAR_CALL".to_owned(),
expected: 0,
found: 1,
}
.into())
);
}
#[test] #[test]
fn error_reserved_identifier() { fn error_reserved_identifier() {
let input = r#" let input = r#"
@@ -183,7 +183,6 @@ where
&mut self, &mut self,
context: &mut revive_llvm_context::PolkaVMContext<D>, context: &mut revive_llvm_context::PolkaVMContext<D>,
) -> anyhow::Result<()> { ) -> anyhow::Result<()> {
revive_llvm_context::PolkaVMImmutableDataLoadFunction.declare(context)?;
let mut entry = revive_llvm_context::PolkaVMEntryFunction::default(); let mut entry = revive_llvm_context::PolkaVMEntryFunction::default();
entry.declare(context)?; entry.declare(context)?;
@@ -200,7 +199,6 @@ where
revive_llvm_context::PolkaVMFunctionDeployCode, revive_llvm_context::PolkaVMFunctionDeployCode,
revive_llvm_context::PolkaVMFunctionRuntimeCode, revive_llvm_context::PolkaVMFunctionRuntimeCode,
revive_llvm_context::PolkaVMFunctionEntry, revive_llvm_context::PolkaVMFunctionEntry,
revive_llvm_context::PolkaVMFunctionImmutableDataLoad,
] ]
.into_iter() .into_iter()
{ {
@@ -218,7 +216,6 @@ where
fn into_llvm(self, context: &mut revive_llvm_context::PolkaVMContext<D>) -> anyhow::Result<()> { fn into_llvm(self, context: &mut revive_llvm_context::PolkaVMContext<D>) -> anyhow::Result<()> {
if self.identifier.ends_with("_deployed") { if self.identifier.ends_with("_deployed") {
revive_llvm_context::PolkaVMImmutableDataLoadFunction.into_llvm(context)?;
revive_llvm_context::PolkaVMRuntimeCodeFunction::new(self.code).into_llvm(context)?; revive_llvm_context::PolkaVMRuntimeCodeFunction::new(self.code).into_llvm(context)?;
} else { } else {
revive_llvm_context::PolkaVMDeployCodeFunction::new(self.code).into_llvm(context)?; revive_llvm_context::PolkaVMDeployCodeFunction::new(self.code).into_llvm(context)?;
-7
View File
@@ -1,7 +0,0 @@
#! /usr/bin/env bash
CONTAINER=revive-builder-debian-x86
VERSION=latest
DOCKERFILE=revive-builder-debian.dockerfile
docker build --rm -t ${CONTAINER}:${VERSION} -f ${DOCKERFILE} $@
-20
View File
@@ -1,20 +0,0 @@
#! /usr/bin/env bash
set -euo pipefail
REVIVE_INSTALL_DIR=$(pwd)/target/release
while getopts "o:" option ; do
case $option in
o) # Output directory
REVIVE_INSTALL_DIR=$OPTARG
;;
\?) echo "Error: Invalid option"
exit 1;;
esac
done
echo "Installing to ${REVIVE_INSTALL_DIR}"
$(pwd)/build-llvm.sh
export PATH=$(pwd)/llvm18.0/bin:$PATH
make install-revive REVIVE_INSTALL_DIR=${REVIVE_INSTALL_DIR}
-14
View File
@@ -1,14 +0,0 @@
# syntax=docker/dockerfile:1
# Dockerfile for building revive in a Debian container.
FROM debian:12
RUN <<EOF
apt-get update
apt-get install -q -y build-essential cmake make ninja-build python3 \
libmpfr-dev libgmp-dev libmpc-dev ncurses-dev \
git curl
EOF
ARG RUST_VERSION=stable
RUN <<EOF
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --default-toolchain ${RUST_VERSION}
EOF
ENV PATH=/root/.cargo/bin:${PATH}
-6
View File
@@ -1,6 +0,0 @@
#! /usr/bin/env bash
CONTAINER=revive-builder-debian-x86
VERSION=latest
docker run --rm -v $(pwd):$(pwd) -w $(pwd) ${CONTAINER}:${VERSION} $@