Compare commits

...

154 Commits

Author SHA1 Message Date
Cyrill Leutwiler 952c5cc894 resolc-0.1.0-dev.7 (#143)
Signed-off-by: Cyrill Leutwiler <bigcyrill@hotmail.com>
2024-12-20 14:33:08 +01:00
Cyrill Leutwiler d8752ec6b5 the basefee opcode (#142)
Signed-off-by: xermicus <cyrill@parity.io>
2024-12-19 18:59:10 +01:00
Cyrill Leutwiler 6ad846a285 the base fee opcode (#141)
Signed-off-by: xermicus <cyrill@parity.io>
2024-12-19 12:44:15 +01:00
Cyrill Leutwiler 3f9771f838 update and fix the linker (#140) 2024-12-18 23:11:06 +01:00
Cyrill Leutwiler 22070a824d the gas limit opcode (#139)
Signed-off-by: xermicus <cyrill@parity.io>
2024-12-18 20:31:42 +01:00
Cyrill Leutwiler d299dd1a19 change getters to register version (#138) 2024-12-18 17:56:37 +01:00
Cyrill Leutwiler 14a598e840 implement the gas opcode (#136) 2024-12-18 17:19:32 +01:00
Cyrill Leutwiler 55ec0988e8 fix the CI runner version (#137)
Signed-off-by: Cyrill Leutwiler <bigcyrill@hotmail.com>
2024-12-18 17:03:29 +01:00
Cyrill Leutwiler 909de515c4 Allow arbitrary call data size (#135)
Signed-off-by: Cyrill Leutwiler <bigcyrill@hotmail.com>
2024-12-18 14:04:46 +01:00
Sebastian Miasojed afe44ad21b JS: Fix encoding conversion from utf16 to utf8 (#131) 2024-12-11 09:23:02 +01:00
Cyrill Leutwiler 2cb8f82266 calls: supply max ref_time and proof_size limits (#133)
Signed-off-by: Cyrill Leutwiler <bigcyrill@hotmail.com>
2024-12-10 16:31:44 +01:00
Cyrill Leutwiler 6f2f158ef1 fix new clippy lint (#132)
Signed-off-by: Cyrill Leutwiler <bigcyrill@hotmail.com>
2024-12-10 15:24:25 +01:00
Cyrill Leutwiler 4e5482bad2 fix the commit sha in the version (#129)
Signed-off-by: xermicus <cyrill@parity.io>
2024-12-03 12:34:17 +01:00
Cyrill Leutwiler 0110258d81 release 0.1.0 dev.6 (#128) 2024-11-29 17:45:47 +01:00
Cyrill Leutwiler f0d9d44dce Implement the gasprice opcode (#127) 2024-11-29 17:14:27 +01:00
Cyrill Leutwiler 3f6cd115ee update warnings (#126)
Signed-off-by: xermicus <cyrill@parity.io>
2024-11-29 16:23:36 +01:00
Cyrill Leutwiler 7c00bbb0fc remove unneeded utils (#125)
Signed-off-by: xermicus <cyrill@parity.io>
2024-11-29 16:21:24 +01:00
Cyrill Leutwiler 423a494621 Switch target to 64bit and enable the zbb feature (#120) 2024-11-29 15:56:10 +01:00
Cyrill Leutwiler 08112e3449 Fix broken link in README.md 2024-11-29 10:39:42 +01:00
Jeeyong Um db6ca1fcfa Fix broken link to introduction in README.md (#124) 2024-11-29 10:38:09 +01:00
Sebastian Miasojed 54d154a73c Merge pull request #123 from smiasojed/web
Add web worker compatibility
2024-11-29 10:22:59 +01:00
dependabot[bot] 93f1deb6a5 Bump rustls from 0.23.17 to 0.23.18 (#121)
Bumps [rustls](https://github.com/rustls/rustls) from 0.23.17 to 0.23.18.
- [Release notes](https://github.com/rustls/rustls/releases)
- [Changelog](https://github.com/rustls/rustls/blob/main/CHANGELOG.md)
- [Commits](https://github.com/rustls/rustls/compare/v/0.23.17...v/0.23.18)

---
updated-dependencies:
- dependency-name: rustls
  dependency-type: indirect
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2024-11-29 09:50:29 +01:00
Sebastian Miasojed 9a150b13f3 Remove hardcoded soljson 2024-11-28 16:54:27 +01:00
Sebastian Miasojed 677cef9c0f Remove comment 2024-11-27 15:30:49 +01:00
Sebastian Miasojed d25be523c1 Update deps 2024-11-27 15:28:04 +01:00
Sebastian Miasojed 6a4fd1e991 Add web worker compatibility 2024-11-27 15:17:26 +01:00
Sebastian Miasojed 81915ddbcb Merge pull request #122 from smiasojed/sync
Remove async JS calls
2024-11-27 14:03:26 +01:00
Sebastian Miasojed 0f25bac4bd Fix CI 2024-11-27 08:49:07 +01:00
Sebastian Miasojed 7d41495587 Fmt 2024-11-27 08:30:07 +01:00
Sebastian Miasojed d6d5acfcce Remove deps on solc 2024-11-26 22:35:18 +01:00
Sebastian Miasojed 130ac48bf0 Fix revive wasm CI 2024-11-26 22:11:26 +01:00
Sebastian Miasojed b65fa2e42c Remove async calls from revive 2024-11-26 22:03:57 +01:00
Sebastian Miasojed 1eb1083d40 Merge pull request #111 from smiasojed/resolc.js
Add compilation to NodeJS module
2024-11-26 14:44:44 +01:00
Sebastian Miasojed 4c0c74f7f4 Fix npm build 2024-11-25 15:36:36 +01:00
Sebastian Miasojed 229c0d452d Merge remote-tracking branch 'origin/main' into resolc.js 2024-11-25 10:54:44 +01:00
Sebastian Miasojed 4e024655a1 Add support for esm and cjs modules 2024-11-25 10:26:22 +01:00
Ermal Kaleci 01b5ed5ba3 Implement delegate_call (#80) 2024-11-23 18:05:21 +01:00
Sebastian Miasojed 892c9b5fbe Update clone-llvm.sh
Co-authored-by: Cyrill Leutwiler <bigcyrill@hotmail.com>
2024-11-22 19:52:06 +01:00
xermicus 84d47fa738 fix missing semicolon
Signed-off-by: xermicus <cyrill@parity.io>
2024-11-22 16:14:07 +01:00
Cyrill Leutwiler dbb47fd13e experimental: support for debug info (#118)
Signed-off-by: wpt967 <matt.aw@parity.io>
Signed-off-by: xermicus <cyrill@parity.io>
2024-11-22 08:56:09 +01:00
Cyrill Leutwiler 87f2bcefb3 dump the elf shared object into the debug output directory (#119)
Signed-off-by: Cyrill Leutwiler <bigcyrill@hotmail.com>
2024-11-21 21:48:42 +01:00
Sebastian Miasojed 05925f25f1 Update comment 2024-11-21 13:57:00 +01:00
Sebastian Miasojed 8990b2a486 Update emscripten-build-llvm.sh
Co-authored-by: Cyrill Leutwiler <bigcyrill@hotmail.com>
2024-11-21 12:49:23 +01:00
Cyrill Leutwiler 89ddfb28c8 CI: ignore the broken test (#116)
Signed-off-by: Cyrill Leutwiler <bigcyrill@hotmail.com>
2024-11-21 12:22:15 +01:00
Sebastian Miasojed 32d4b2309c Fix build script 2024-11-21 10:36:12 +01:00
Sebastian Miasojed a772ff1354 Fix build path 2024-11-21 10:29:21 +01:00
Sebastian Miasojed 0a82addf02 Add directory arg support to clone-llvm 2024-11-20 17:15:55 +01:00
Sebastian Miasojed 491850760f Update readme file 2024-11-20 12:57:41 +01:00
Sebastian Miasojed fcbe00f4a9 Merge branch 'main' into resolc.js 2024-11-20 12:51:10 +01:00
Sebastian Miasojed 77406264ea Improve description 2024-11-20 12:42:29 +01:00
Sebastian Miasojed cdebf69fc0 Fix stdin support 2024-11-20 10:54:15 +01:00
Sebastian Miasojed f36d62ca73 Add stdin support 2024-11-20 10:27:57 +01:00
Sebastian Miasojed 39e504703e Add test with standard json args 2024-11-18 16:52:56 +01:00
Sebastian Miasojed 63da7212a1 Add worker code to resolc.js 2024-11-18 14:49:35 +01:00
Sebastian Miasojed 3072c03600 Fix parallel feature 2024-11-18 13:59:48 +01:00
Sebastian Miasojed d88ddb25bd Refactor soljson compiler 2024-11-18 12:19:36 +01:00
Sebastian Miasojed 87dd77b784 Remove libsolc crate 2024-11-18 11:07:45 +01:00
Sebastian Miasojed cece20deb1 Cleanup 2024-11-18 10:18:21 +01:00
Sebastian Miasojed f57ab96eed Revert changes in llvm build 2024-11-15 16:08:36 +01:00
Cyrill Leutwiler 3232382d96 update dependencies (#115)
Signed-off-by: xermicus <cyrill@parity.io>
2024-11-15 13:50:55 +01:00
Cyrill Leutwiler 6a120463c2 implement the blockhash opcode (#114) 2024-11-15 13:11:04 +01:00
Sebastian Miasojed 881b88354c Rename compiler to solc 2024-11-14 17:09:03 +01:00
Cyrill Leutwiler c9dd347755 Add documentation portal (#99) 2024-11-14 14:21:48 +01:00
Sebastian Miasojed 140545ea15 Fix CI 2024-11-14 11:49:20 +01:00
Sebastian Miasojed 010a2ed223 Fmt 2024-11-14 11:44:48 +01:00
Sebastian Miasojed 563864dd25 Fix CI 2024-11-14 11:26:20 +01:00
Sebastian Miasojed ce8bf3d9ef Apply suggestions from previous review 2024-11-14 11:20:33 +01:00
Cyrill Leutwiler f947984671 update 64bit target flags (#113) 2024-11-14 10:29:21 +01:00
Sebastian Miasojed 14991f40ac Fix CI for wasm path 2024-11-13 15:34:41 +01:00
Sebastian Miasojed 6d16790f83 Update GHA 2024-11-12 17:22:15 +01:00
Sebastian Miasojed 64fefe76b5 Removed not needed libs from linking process 2024-11-12 16:07:21 +01:00
Sebastian Miasojed f59b47df7b Add ltinfo dep to GHA 2024-11-12 12:52:11 +01:00
Sebastian Miasojed 6e6fe20c71 Add Missing dep to GHA 2024-11-12 09:52:06 +01:00
Sebastian Miasojed 93d2f3b9d9 Log LLVM version in GHA 2024-11-12 09:47:43 +01:00
Sebastian Miasojed 8a225871ee Fix deps in GHA 2024-11-08 15:53:27 +01:00
Sebastian Miasojed 677aedc6f3 Switch GHA to ubuntu 2024-11-08 15:45:52 +01:00
Sebastian Miasojed 90423ffb6b Remove llvm-15 env from GHA 2024-11-08 15:36:00 +01:00
Sebastian Miasojed 255176978a Remove old llvm-15 from GHA 2024-11-08 15:27:11 +01:00
Sebastian Miasojed 9a8003afbf Install llvm in GHA 2024-11-08 15:10:42 +01:00
Sebastian Miasojed 7f3d0cecb2 Update cmake versions in GHA 2024-11-08 15:00:14 +01:00
Sebastian Miasojed 18376432f1 Fix GHA emsdk path 2024-11-08 14:01:18 +01:00
Sebastian Miasojed 007b79ee62 Use bash in GHA 2024-11-08 13:51:51 +01:00
Sebastian Miasojed 8c7d18aec7 Update GHA for wasm build 2024-11-08 13:46:00 +01:00
Sebastian Miasojed c0a82ce6d2 Update wasm GHA 2024-11-08 13:38:26 +01:00
Sebastian Miasojed c51d50bc88 Add GHA for wasm target 2024-11-08 13:28:45 +01:00
Sebastian Miasojed 94445bab93 Fix compilation for wasm target 2024-11-08 12:06:11 +01:00
Sebastian Miasojed a934ec204e Add temoprary wasm compilation output 2024-11-08 10:55:26 +01:00
Sebastian Miasojed b6baf6cfd9 Make wasm version to compile 2024-11-08 09:54:10 +01:00
Sebastian Miasojed b7b28efded Update solc compiler version check 2024-11-07 15:30:00 +01:00
Sebastian Miasojed d260472330 Fmt 2024-11-07 15:15:52 +01:00
Sebastian Miasojed 4f6debcbe3 Make native version to compaile again 2024-11-07 15:04:35 +01:00
Sebastian Miasojed 9b23e19479 Fix Cargo.toml 2024-11-07 09:43:44 +01:00
Sebastian Miasojed 88a888d138 Merge remote-tracking branch 'origin/main' into resolc.js 2024-11-06 15:04:34 +01:00
Cyrill Leutwiler 4cce4a729e pre-release v0.1.0-dev.5 (#110) 2024-10-31 15:22:34 +01:00
Cyrill Leutwiler 9267a2af02 use the long revive version string in the contract metadata (#109) 2024-10-31 14:52:06 +01:00
Cyrill Leutwiler 173ace72cb solidity: rename the revive metadata (#106)
Rename the revive metadata fields and includes the commit hash and LLVM version in the revive version (similar to what solc does).

Signed-off-by: xermicus <cyrill@parity.io>
2024-10-31 13:00:35 +01:00
Cyrill Leutwiler 37ab2b6782 declare immutable globals during in declare (#108)
Signed-off-by: xermicus <cyrill@parity.io>
2024-10-31 12:07:39 +01:00
Cyrill Leutwiler 43d2ef3ce9 implement the code size opcodes (#107) 2024-10-31 11:46:47 +01:00
Cyrill Leutwiler 68564f9866 display the git revision version information (#105)
Signed-off-by: Cyrill Leutwiler <bigcyrill@hotmail.com>
2024-10-30 14:28:00 +01:00
Cyrill Leutwiler 36d9317831 update the polkadot-sdk and polkavm dependencies (#104)
Signed-off-by: Cyrill Leutwiler <bigcyrill@hotmail.com>
2024-10-30 09:28:24 +01:00
Cyrill Leutwiler 5b3b90db83 support the origin opcode (#103)
Signed-off-by: Cyrill Leutwiler <bigcyrill@hotmail.com>
2024-10-29 18:40:25 +01:00
Cyrill Leutwiler a4043ecde7 update LLVM target features (#102)
Signed-off-by: xermicus <cyrill@parity.io>
2024-10-29 18:17:29 +01:00
Cyrill Leutwiler f985f42370 update polkadot-sdk and inkwell dependencies (#101)
Signed-off-by: Cyrill Leutwiler <bigcyrill@hotmail.com>
2024-10-29 11:27:44 +01:00
Ermal Kaleci aae25107a2 support full storage key space (#100)
- The storage pointer values will no longer be truncated to the register size, allowing for the use of arbitrary storage keys
- Failed storage value reads will now guarantee to return the zero value
2024-10-28 10:18:11 +01:00
Sebastian Miasojed cc38c37481 Set evm.deployedBytecode to the value of evm.bytecode (#95) 2024-10-24 14:59:50 +02:00
Cyrill Leutwiler e56feb95be standard json output: skip serializing keys instead of emitting a null object (#52)
Signed-off-by: Cyrill Leutwiler <bigcyrill@hotmail.com>
2024-10-23 15:28:28 +02:00
Cyrill Leutwiler 995b115c5f bump polkadot-sdk and polkavm (#94)
Signed-off-by: xermicus <cyrill@parity.io>
2024-10-22 19:50:58 +02:00
Ermal Kaleci ee83d28a51 Implement extcodehash (#77) 2024-10-21 10:13:50 +02:00
Cyrill Leutwiler 82ae22c163 tidy up the runtime API crate (#85)
- remove unused runtime API imports and constants
- move runtime api symbols into the revive-runtime-api crate

Signed-off-by: Cyrill Leutwiler <bigcyrill@hotmail.com>
2024-10-17 10:55:27 +02:00
Cyrill Leutwiler 20e3560398 Makefile: remove duplicate workspace tests from the test target (#90)
Signed-off-by: Cyrill Leutwiler <bigcyrill@hotmail.com>
2024-10-17 10:22:04 +02:00
Cyrill Leutwiler 5bf31db660 bump polkavm and polkadot-sdk (#87)
Signed-off-by: Cyrill Leutwiler <bigcyrill@hotmail.com>
2024-10-15 10:46:20 +02:00
Cyrill Leutwiler f0f344a139 disable sbrk and emulate EVM linear memory internally (#76)
Signed-off-by: Cyrill Leutwiler <bigcyrill@hotmail.com>
Signed-off-by: xermicus <cyrill@parity.io>
2024-10-14 15:20:00 +02:00
Cyrill Leutwiler 92a18460e4 support solc 0.8.28 (#84)
Signed-off-by: Cyrill Leutwiler <bigcyrill@hotmail.com>
2024-10-14 13:25:18 +02:00
Cyrill Leutwiler e5233fc46e revive-runner: consider non-reverted transactions as success (#82)
Signed-off-by: Cyrill Leutwiler <bigcyrill@hotmail.com>
2024-10-14 13:24:59 +02:00
Ermal Kaleci adda5a0f12 improve runtime api return value names (#81) 2024-10-14 08:56:26 +02:00
Ermal Kaleci d9842b5427 Fix implementation for balance_of (#79)
The balance_of syscall is now available in pallet-revive.
- Fix balance_of implementation to use correct runtime api
- Add build_address_argument_store helper to be used for address arguments
2024-10-12 12:35:10 +02:00
Cyrill Leutwiler 6335c34a2b remove obsolete runtime builtins
Signed-off-by: Cyrill Leutwiler <bigcyrill@hotmail.com>
2024-10-11 15:58:28 +02:00
Cyrill Leutwiler 49d2157a84 remove near calls
Signed-off-by: Cyrill Leutwiler <bigcyrill@hotmail.com>
2024-10-11 15:58:28 +02:00
Cyrill Leutwiler e131eebf50 remove system mode and request memoization
Signed-off-by: Cyrill Leutwiler <bigcyrill@hotmail.com>
2024-10-11 15:58:28 +02:00
Cyrill Leutwiler 705d1d7866 run signed_remainder test in differential mode (#75) 2024-10-10 19:26:04 +02:00
Cyrill Leutwiler d5d419cefc implement immutable variables codegen (#70) 2024-10-10 13:33:00 +02:00
Cyrill Leutwiler 8b7fe8e3d7 deny clippy warnings by default (#73) 2024-10-09 20:19:12 +02:00
xermicus b344e0cff5 Revert "implement immutable variables codegen"
This reverts commit 0e6a6d12c3.
2024-10-09 03:43:05 +02:00
xermicus 0e6a6d12c3 implement immutable variables codegen
Signed-off-by: xermicus <cyrill@parity.io>
2024-10-09 03:37:56 +02:00
Cyrill Leutwiler 77fe683f18 remove the test encoding option (#65) 2024-10-04 09:59:27 +02:00
wpt967 e8fcd6118e Merge pull request #55 from wpt967/revive-debian-x86-build
[workflows] Add workflow for building revive in a debian container.
2024-09-30 17:25:08 +01:00
wpt967 ad46e94ebd [workflows] Add workflow for building revive in a debian container.
Makefile: Add target 'install-revive' to build revive with the
installation path specified by variable REVIVE_INSTALL_DIR.

Add utils directory with scripts for building revive in a container.

Add utils/build-revive.sh taking option argument '-o <install-dir>' to
build revive with the specified install directory.

Add utils/revive-builder-debian.dockerfile to make a docker container
for building revive in a Debian environment.
2024-09-30 12:02:57 +01:00
Cyrill Leutwiler 6585973e99 Updated call semantics (#56)
- Update pallet-revive dependency
- Implement calls according to pallet-revive call semantics
- Switch to the new return data API in pallet revive and get rid of return data buffer
- Remove a bunch of resulting dead code
2024-09-28 20:03:03 +02:00
Cyrill Leutwiler 066acc4663 set chain_id in test runtime
Signed-off-by: Cyrill Leutwiler <bigcyrill@hotmail.com>
2024-09-28 19:17:41 +02:00
Cyrill Leutwiler 287272b789 sync runtime with the pallet (#54) 2024-09-24 09:14:36 +02:00
Cyrill Leutwiler f621971a13 move the repository to paritytech org (#49) 2024-09-17 14:53:17 +02:00
Cyrill Leutwiler b8bd44b62a install solc 0.8.27 on CI (#47)
Signed-off-by: xermicus <cyrill@parity.io>
2024-09-16 13:17:30 +02:00
Cyrill Leutwiler d8c708eba5 support solc 0.8.27
Signed-off-by: Cyrill Leutwiler <bigcyrill@hotmail.com>
2024-09-16 12:43:49 +02:00
Cyrill Leutwiler 72d958392b revise alloca at function entry later
Signed-off-by: Cyrill Leutwiler <bigcyrill@hotmail.com>
2024-09-16 12:42:50 +02:00
Cyrill Leutwiler 616f044633 always build alloca in the entry block (#46)
Signed-off-by: xermicus <cyrill@parity.io>
2024-09-13 21:54:42 +02:00
xermicus 7dc8e6051e enable compressed instructions extension
Signed-off-by: xermicus <cyrill@parity.io>
2024-09-13 14:55:16 +02:00
xermicus 783fa640e4 temporarily disable pvm linker optimizations
Signed-off-by: xermicus <cyrill@parity.io>
2024-09-13 14:52:47 +02:00
xermicus f086c57442 bump up runner runtime limits
Signed-off-by: xermicus <cyrill@parity.io>
2024-09-13 14:52:09 +02:00
xermicus 0f2b55f6ff fixed output size for transferred value
Signed-off-by: xermicus <cyrill@parity.io>
2024-09-13 08:41:23 +02:00
wpt967 ac9215329c [solidity] Fix wrong check on recursive-process-input CLI option. (#42) 2024-09-10 19:11:36 +02:00
Cyrill Leutwiler 6635a0b337 fixed size event topics
Signed-off-by: Cyrill Leutwiler <bigcyrill@hotmail.com>
2024-09-10 09:09:52 +02:00
Cyrill Leutwiler 77df88b3cb update dependencies
Signed-off-by: Cyrill Leutwiler <bigcyrill@hotmail.com>
2024-09-10 09:04:55 +02:00
Cyrill Leutwiler 2955f77772 implement self balance
Signed-off-by: Cyrill Leutwiler <bigcyrill@hotmail.com>
2024-09-09 09:40:24 +02:00
Cyrill Leutwiler d77ee1e0d4 runner: endow test accounts on genesis
Signed-off-by: Cyrill Leutwiler <bigcyrill@hotmail.com>
2024-09-06 13:50:01 +02:00
xermicus c111bcbc4d common: remove unused constants
Signed-off-by: xermicus <cyrill@parity.io>
2024-09-05 20:02:33 +02:00
xermicus 9e73c48150 remove the extra abi data
Signed-off-by: xermicus <cyrill@parity.io>
2024-09-05 18:10:24 +02:00
Cyrill Leutwiler afd9f26aed runner: gate the solidity frontend behind a feature
Signed-off-by: Cyrill Leutwiler <bigcyrill@hotmail.com>
2024-09-04 20:12:27 +02:00
Cyrill Leutwiler a413238464 runner: CodeUpload specs action
Signed-off-by: Cyrill Leutwiler <bigcyrill@hotmail.com>
2024-09-04 18:03:30 +02:00
Cyrill Leutwiler d47539159b enable call context address tests
Signed-off-by: Cyrill Leutwiler <bigcyrill@hotmail.com>
2024-09-04 12:55:44 +02:00
Cyrill Leutwiler 393d90165e pallet_revive: Account20 (#41)
Signed-off-by: xermicus <cyrill@parity.io>
2024-09-04 12:42:41 +02:00
xermicus 7c934e5ca1 fix some clippies
Signed-off-by: xermicus <cyrill@parity.io>
2024-09-04 11:31:18 +02:00
Cyrill Leutwiler d2f76b645f switch to pallet_revive runtime (#40)
Signed-off-by: xermicus <cyrill@parity.io>
2024-09-03 17:18:22 +02:00
Sebastian Miasojed 5ac67bdc0d Initial wasm support 2024-08-29 17:28:31 +02:00
Sebastian Miasojed 41c8d4e955 Add support for --license arg (#38) 2024-08-29 16:26:06 +02:00
221 changed files with 9518 additions and 6650 deletions
+38
View File
@@ -0,0 +1,38 @@
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-22.04
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
+79
View File
@@ -0,0 +1,79 @@
name: Build revive-wasm
on:
push:
branches: ["main"]
pull_request:
branches: ["main"]
workflow_dispatch:
env:
CARGO_TERM_COLOR: always
REVIVE_WASM_INSTALL_DIR: ${{ github.workspace }}/target/wasm32-unknown-emscripten/release
EMSCRIPTEN_VERSION: 3.1.64
jobs:
build-revive-wasm:
runs-on: ubuntu-22.04
defaults:
run:
shell: bash
steps:
- uses: actions/checkout@v4
- name: Cache LLVM build
id: cache-llvm
uses: actions/cache@v3
with:
path: |
llvm18.0-emscripten
# Use a unique key based on LLVM version or configuration files to avoid cache invalidation
key: llvm-build-${{ runner.os }}-${{ hashFiles('clone-llvm.sh', 'emscripten-build-llvm.sh') }}
- name: Install Dependencies
run: |
sudo apt-get update && sudo apt-get install -y cmake ninja-build libncurses5
rustup target add wasm32-unknown-emscripten
# Install LLVM required for the compiler runtime, runtime-api and stdlib
curl -sSL --output llvm.tar.xz https://github.com/llvm/llvm-project/releases/download/llvmorg-18.1.4/clang+llvm-18.1.4-x86_64-linux-gnu-ubuntu-18.04.tar.xz
tar Jxf llvm.tar.xz
mv clang+llvm-18.1.4-x86_64-linux-gnu-ubuntu-18.04 llvm18/
echo "$(pwd)/llvm18/bin" >> $GITHUB_PATH
# Install Emscripten
git clone https://github.com/emscripten-core/emsdk.git
cd emsdk
./emsdk install ${{ env.EMSCRIPTEN_VERSION }}
./emsdk activate ${{ env.EMSCRIPTEN_VERSION }}
- run: |
rustup show
cargo --version
rustup +nightly show
cargo +nightly --version
cmake --version
bash --version
llvm-config --version
- name: Build LLVM
if: steps.cache-llvm.outputs.cache-hit != 'true'
run: |
export EMSDK_ROOT=${PWD}/emsdk
./emscripten-build-llvm.sh
- name: Use Cached LLVM
if: steps.cache-llvm.outputs.cache-hit == 'true'
run: |
echo "Using cached LLVM"
- name: Build revive
run: |
export LLVM_LINK_PREFIX=${PWD}/llvm18.0-emscripten
source ./emsdk/emsdk_env.sh
make install-wasm
- uses: actions/upload-artifact@v4
with:
name: revive-wasm
path: |
${{ env.REVIVE_WASM_INSTALL_DIR }}/resolc.js
${{ env.REVIVE_WASM_INSTALL_DIR }}/resolc.wasm
retention-days: 1
+12 -3
View File
@@ -11,7 +11,7 @@ env:
jobs: jobs:
build-ubuntu-x86: build-ubuntu-x86:
runs-on: ubuntu-latest runs-on: ubuntu-22.04
steps: steps:
- uses: actions/checkout@v4 - uses: actions/checkout@v4
@@ -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.26/solc-static-linux curl -sSL --output solc/solc https://github.com/ethereum/solidity/releases/download/v0.8.28/solc-static-linux
chmod +x solc/solc chmod +x solc/solc
echo "$(pwd)/solc/" >> $GITHUB_PATH echo "$(pwd)/solc/" >> $GITHUB_PATH
@@ -31,7 +31,10 @@ jobs:
echo "$(pwd)/llvm18/bin" >> $GITHUB_PATH echo "$(pwd)/llvm18/bin" >> $GITHUB_PATH
- name: Install apt dependencies - name: Install apt dependencies
run: sudo apt update && sudo apt install -y libtinfo5 run: |
sudo add-apt-repository -y ppa:ethereum/ethereum
sudo apt update
sudo apt install -y libtinfo5 ethereum
- name: Format - name: Format
run: make format run: make format
@@ -44,3 +47,9 @@ jobs:
- name: Test CLI - name: Test CLI
run: make test-cli run: make test-cli
- uses: actions/upload-artifact@v4
with:
name: ${{ github.job }}-resolc
path: ./target/release/resolc
retention-days: 1
+2
View File
@@ -8,9 +8,11 @@
/*.s /*.s
/llvm-project /llvm-project
/llvm18.0 /llvm18.0
/llvm18.0-emscripten
node_modules node_modules
artifacts artifacts
tmp tmp
package-lock.json package-lock.json
/*.html /*.html
/build /build
soljson.js
+67
View File
@@ -0,0 +1,67 @@
# Changelog
## Unreleased
## v0.1.0-dev.7
This is a development pre-release.
### Added
- Implement the `GASPRICE` opcode.
- Implement the `BASEFEE` opcode.
- Implement the `GASLIMIT` opcode.
### Changed
- The `GAS` opcode now returns the remaining `ref_time`.
- Contracts can now be supplied call data input of arbitrary size.
- Some syscalls now return the value in a register, slightly improving emitted contract code.
- Calls forward maximum weight limits instead of 0, anticipating a change in polkadot-sdk where weight limits of 0 no longer interprets as uncapped limit.
### Fixed
- A linker bug which was preventing certain contracts from linking with the PVM linker.
- JS: Fix encoding conversion from JS string (UTF-16) to UTF-8.
- The git commit hash slug is always displayed in the version string.
## v0.1.0-dev.6
This is a development pre-release.
# Added
- Implement the `BLOCKHASH` opcode.
- Implement delegate calls.
- Implement the `GASPRICE` opcode. Currently hard-coded to return `1`.
- The ELF shared object contract artifact is dumped into the debug output directory.
- Initial support for emitting debug info (opt in via the `-g` flag)
# Changed
- resolc now emits 64bit PolkaVM blobs, reducing contract code size and execution time.
- The RISC-V bit-manipulation target feature (`zbb`) is enabled.
# Fixed
- Compilation to Wasm (for usage in node and web browsers)
## 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
+2199 -1321
View File
File diff suppressed because it is too large Load Diff
+31 -27
View File
@@ -3,28 +3,29 @@ resolver = "2"
members = ["crates/*"] members = ["crates/*"]
[workspace.package] [workspace.package]
version = "0.1.0" version = "0.1.0-dev.7"
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/xermicus/revive" repository = "https://github.com/paritytech/revive"
rust-version = "1.80.0"
[workspace.dependencies] [workspace.dependencies]
revive-benchmarks = { version = "0.1.0", path = "crates/benchmarks" } revive-benchmarks = { version = "0.1.0-dev.7", path = "crates/benchmarks" }
revive-builtins = { version = "0.1.0", path = "crates/builtins" } revive-builtins = { version = "0.1.0-dev.7", path = "crates/builtins" }
revive-common = { version = "0.1.0", path = "crates/common" } revive-common = { version = "0.1.0-dev.7", path = "crates/common" }
revive-differential = { version = "0.1.0", path = "crates/differential" } revive-differential = { version = "0.1.0-dev.7", path = "crates/differential" }
revive-integration = { version = "0.1.0", path = "crates/integration" } revive-integration = { version = "0.1.0-dev.7", path = "crates/integration" }
revive-linker = { version = "0.1.0", path = "crates/linker" } revive-linker = { version = "0.1.0-dev.7", path = "crates/linker" }
lld-sys = { version = "0.1.0", path = "crates/lld-sys" } lld-sys = { version = "0.1.0-dev.7", path = "crates/lld-sys" }
revive-llvm-context = { version = "0.1.0", path = "crates/llvm-context" } revive-llvm-context = { version = "0.1.0-dev.7", path = "crates/llvm-context" }
pallet-contracts-pvm-llapi = { version = "0.1.0", path = "crates/pallet-contracts-pvm-llapi" } revive-runtime-api = { version = "0.1.0-dev.7", path = "crates/runtime-api" }
revive-runner = { version = "0.1.0", path = "crates/runner" } revive-runner = { version = "0.1.0-dev.7", path = "crates/runner" }
revive-solidity = { version = "0.1.0", path = "crates/solidity" } revive-solidity = { version = "0.1.0-dev.7", path = "crates/solidity" }
revive-stdlib = { version = "0.1.0", path = "crates/stdlib" } revive-stdlib = { version = "0.1.0-dev.7", path = "crates/stdlib" }
hex = "0.4" hex = "0.4"
petgraph = "0.6" petgraph = "0.6"
@@ -50,30 +51,33 @@ 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 = { git = "https://github.com/koute/polkavm.git", rev = "360029e" } polkavm-common = "0.18"
polkavm-linker = { git = "https://github.com/koute/polkavm.git", rev = "360029e" } polkavm-linker = "0.18"
polkavm-disassembler = { git = "https://github.com/koute/polkavm.git", rev = "360029e" } polkavm-disassembler = "0.18"
polkavm = { git = "https://github.com/koute/polkavm.git", rev = "360029e" } polkavm = "0.18"
alloy-primitives = "0.6" alloy-primitives = { version = "0.8", features = ["serde"] }
alloy-sol-types = "0.6" alloy-sol-types = "0.8"
alloy-genesis = "0.3"
alloy-serde = "0.3"
env_logger = { version = "0.10.0", default-features = false } env_logger = { version = "0.10.0", default-features = false }
serde_stacker = "0.1" serde_stacker = "0.1"
criterion = { version = "0.5", features = ["html_reports"] } criterion = { version = "0.5", features = ["html_reports"] }
log = { version = "0.4" } log = { version = "0.4" }
git2 = "0.19.0"
# 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 = "559fa1db0594a81d5dbf343613ba2f3fc16708da" } polkadot-sdk = { git = "https://github.com/paritytech/polkadot-sdk", rev = "243b751abbb94369bbd92c83d8ab159ddfc3c556" }
# Benchmarking against EVM
primitive-types = { version = "0.12", features = ["codec"] }
evm-interpreter = { git = "https://github.com/xermicus/evm.git", branch = "separate-compilation" }
# llvm
[workspace.dependencies.inkwell] [workspace.dependencies.inkwell]
version = "0.5" git = "https://github.com/TheDan64/inkwell.git"
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"]
[profile.benchmark] [profile.bench]
inherits = "release" inherits = "release"
lto = true lto = true
codegen-units = 1 codegen-units = 1
+41 -19
View File
@@ -1,5 +1,22 @@
.PHONY: install format test test-solidity test-cli test-integration test-workspace clean docs docs-build .PHONY: install format test test-solidity test-cli test-integration test-workspace clean docs docs-build
RUSTFLAGS_EMSCRIPTEN := \
-C link-arg=-sEXPORTED_FUNCTIONS=_main,_free,_malloc \
-C link-arg=-sNO_INVOKE_RUN=1 \
-C link-arg=-sEXIT_RUNTIME=1 \
-C link-arg=-sALLOW_MEMORY_GROWTH=1 \
-C link-arg=-sEXPORTED_RUNTIME_METHODS=FS,callMain,stringToNewUTF8 \
-C link-arg=-sMODULARIZE=1 \
-C link-arg=-sEXPORT_NAME=createRevive \
-C link-arg=-sWASM_ASYNC_COMPILATION=0 \
-C link-arg=-sDYNAMIC_EXECUTION=0 \
-C link-arg=-sALLOW_TABLE_GROWTH=1 \
-C link-arg=--js-library=js/embed/soljson_interface.js \
-C link-arg=--pre-js=js/embed/pre.js \
-C link-arg=-sNODEJS_CATCH_EXIT=0 \
-C link-arg=-sDISABLE_EXCEPTION_CATCHING=0 \
-C opt-level=3
install: install-bin install-npm install: install-bin install-npm
install-bin: install-bin:
@@ -8,11 +25,24 @@ install-bin:
install-npm: install-npm:
npm install && npm fund npm install && npm fund
install-wasm:
RUSTFLAGS='$(RUSTFLAGS_EMSCRIPTEN)' cargo build --target wasm32-unknown-emscripten -p revive-solidity --release --no-default-features
npm install
# 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
@@ -26,28 +56,18 @@ test-workspace: install
test-cli: install test-cli: install
npm run test:cli npm run test:cli
bench-prepare: install-bin bench-pvm: install-bin
cargo criterion --bench prepare --features bench-evm,bench-pvm --message-format=json \ cargo criterion --bench execute --features bench-pvm-interpreter --message-format=json \
| criterion-table > crates/benchmarks/PREPARE.md | criterion-table > crates/benchmarks/PVM.md
bench-execute: install-bin bench-evm: install-bin
cargo criterion --bench execute --features bench-evm,bench-pvm --message-format=json \ cargo criterion --bench execute --features bench-evm --message-format=json \
| criterion-table > crates/benchmarks/EXECUTE.md | criterion-table > crates/benchmarks/EVM.md
bench-extensive: install-bin
cargo criterion --all --all-features --message-format=json \
| criterion-table > crates/benchmarks/BENCHMARKS.md
bench-quick: install-bin
cargo criterion --all --features bench-evm
bench: install-bin bench: install-bin
cargo criterion --all --features bench-evm,bench-pvm --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/
@@ -59,4 +79,6 @@ clean:
rm -rf node_modules ; \ rm -rf node_modules ; \
rm -rf crates/solidity/src/tests/cli-tests/artifacts ; \ rm -rf crates/solidity/src/tests/cli-tests/artifacts ; \
cargo uninstall revive-solidity ; \ cargo uninstall revive-solidity ; \
rm -f package-lock.json rm -f package-lock.json ; \
rm -rf js/dist ; \
rm -f js/src/resolc.{wasm,js}
+39 -6
View File
@@ -1,14 +1,15 @@
![CI](https://github.com/xermicus/revive/actions/workflows/rust.yml/badge.svg) ![CI](https://github.com/paritytech/revive/actions/workflows/rust.yml/badge.svg)
[![Docs](https://img.shields.io/badge/Docs-contracts.polkadot.io-brightgreen.svg)](https://contracts.polkadot.io)
# revive # revive
YUL and EVM assembly recompiler to LLVM, targetting RISC-V on [PolkaVM](https://github.com/koute/polkavm). YUL and EVM assembly recompiler to LLVM, targetting RISC-V on [PolkaVM](https://github.com/koute/polkavm).
[Frontend](https://github.com/matter-labs/era-compiler-solidity) and [code generator](https://github.com/matter-labs/era-compiler-llvm-context) are based of ZKSync `zksolc`. Visit [contracts.polkadot.io](https://contracts.polkadot.io) to learn more about contracts on Polkadot!
## Status ## Status
This is experimental software in active development and not ready just yet for production usage. This is experimental software in active development and not ready just yet for production usage. Please do report any compiler related issues or missing features that are [not yet known to us](https://contracts.polkadot.io/known_issues/) here.
Discussion around the development is hosted on the [Polkadot Forum](https://forum.polkadot.network/t/contracts-update-solidity-on-polkavm/6949#a-new-solidity-compiler-1). Discussion around the development is hosted on the [Polkadot Forum](https://forum.polkadot.network/t/contracts-update-solidity-on-polkavm/6949#a-new-solidity-compiler-1).
@@ -16,6 +17,14 @@ Discussion around the development is hosted on the [Polkadot Forum](https://foru
`resolc` depends on the [solc](https://github.com/ethereum/solidity) binary installed on your system. `resolc` depends on the [solc](https://github.com/ethereum/solidity) binary installed on your system.
Building from source requires a compatible LLVM build.
### LLVM
`revive` requires a build of LLVM 18.1.4 or later with the RISC-V _embedded_ target, including `compiler-rt`. Use the provided [build-llvm.sh](build-llvm.sh) build script to compile a compatible LLVM build locally in `$PWD/llvm18.0` (don't forget to add that to `$PATH` afterwards).
### The `resolc` Solidity frontend
To install the `resolc` Solidity frontend executable: To install the `resolc` Solidity frontend executable:
```bash ```bash
@@ -24,10 +33,23 @@ export PATH=${PWD}/llvm18.0/bin:$PATH
make install-bin make install-bin
resolc --version resolc --version
``` ```
### Cross-compilation to WASM
### LLVM Cross-compiles the Revive compiler to WASM for running it in a Node.js or browser environment.
`revive` requires a build of LLVM 18.1.4 or later including `compiler-rt`. Use the provided [build-llvm.sh](build-llvm.sh) build script to compile a compatible LLVM build locally in `$PWD/llvm18.0` (don't forget to add that to `$PATH` afterwards). Install [emscripten](https://emscripten.org/docs/getting_started/downloads.html). Tested on version 3.1.64.
To build resolc.js execute:
```bash
bash build-llvm.sh
export PATH=${PWD}/llvm18.0/bin:$PATH
export EMSDK_ROOT=<PATH_TO_EMSCRIPTEN_SDK>
bash emscripten-build-llvm.sh
source $EMSDK_ROOT/emsdk_env.sh
export LLVM_LINK_PREFIX=${PWD}/llvm18.0-emscripten
export PATH=$PATH:$PWD/llvm18.0-emscripten/bin/
make install-wasm
```
### Development ### Development
@@ -35,4 +57,15 @@ Please consult the [Makefile](Makefile) targets to learn how to run tests and be
Ensure that your branch passes `make test` locally when submitting a pull request. Ensure that your branch passes `make test` locally when submitting a pull request.
## Design overview ## Design overview
`revive` uses [solc](https://github.com/ethereum/solidity/), the Ethereum Solidity compiler, as the [Solidity frontend](crates/solidity/src/lib.rs) to process smart contracts written in Solidity. The YUL IR code (or legacy EVM assembly as a fallback for older `solc` versions) emitted by `solc` is then translated to LLVM IR, targetting a runtime similar to [Polkadots `contracts` pallet](https://docs.rs/pallet-contracts/latest/pallet_contracts/api_doc/trait.Current.html).
`revive` uses [solc](https://github.com/ethereum/solidity/), the Ethereum Solidity compiler, as the [Solidity frontend](crates/solidity/src/lib.rs) to process smart contracts written in Solidity. The YUL IR code (or legacy EVM assembly as a fallback for older `solc` versions) emitted by `solc` is then translated to LLVM IR, targetting [Polkadots `revive` pallet](https://docs.rs/pallet-revive/latest/pallet_revive/trait.SyscallDoc.html).
[Frontend](https://github.com/matter-labs/era-compiler-solidity) and [code generator](https://github.com/matter-labs/era-compiler-llvm-context) are based of ZKSync `zksolc`.
## Tests
Before running the tests, ensure that Geth (Go Ethereum) is installed on your system. Follow the installation guide here: [Installing Geth](https://geth.ethereum.org/docs/getting-started/installing-geth).
Once Geth is installed, you can run the tests using the following command:
```bash
make test
```
+12
View File
@@ -0,0 +1,12 @@
# 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. Manually upload `resolc.js` and `resolc.wasm` from the `build-revive-wasm` action artifacts.
6. Update the [contract-docs](https://github.com/paritytech/contract-docs/) accordingly
+30
View File
@@ -0,0 +1,30 @@
# Known issues
The following is known and we are either working on it or it is a hard limitation. Please do not open a new issue.
## Release
`0.1.0-dev-2`
## Missing features
- [Libraries with public functions are not supported](https://github.com/paritytech/revive/issues/91)
- [Automatic import resolution is not supported](https://github.com/paritytech/revive/issues/98)
- The emulated EVM linear contract memory is limited to 64kb in size. Will be fixed with support for metered dynamic memory.
- [The contract calldata is currently limited to 1kb in size](https://github.com/paritytech/revive/issues/57)
- [EIP-4844 opcodes are not supported](https://github.com/paritytech/revive/issues/64)
- [Delegate calls are not supported](https://github.com/paritytech/revive/issues/67)
- [The `blockhash` opcode is not supported](https://github.com/paritytech/revive/issues/61)
- [The `extcodesize` opcode is not supported](https://github.com/paritytech/revive/issues/58)
- [The `origin` opcode is not supported](https://github.com/paritytech/revive/issues/59)
- [Gas limits for contract calls are ignored](https://github.com/paritytech/revive/issues/60)
- [Gas related opcodes are not supported](https://github.com/paritytech/revive/issues/60)
- IPFS metadata hashes are not supported
- [Compiled contract artifacts can exceed the pallet static memory limit and fail to deploy](https://github.com/paritytech/revive/issues/96).
- [Transfers to inexistant accounts will fail if the transferred value lies below the ED.](https://github.com/paritytech/revive/issues/83) Will be fixed in the pallet to make the ED completely transparent for contracts.
## Wontfix
Please consult our documentation to learn more about Solidity and EVM features likely to remain unsupported (and why they will not be supported).
TODO: Insert link to the relevant documentation section.
+3 -7
View File
@@ -5,17 +5,13 @@ set -euo pipefail
INSTALL_DIR="${PWD}/llvm18.0" INSTALL_DIR="${PWD}/llvm18.0"
mkdir -p ${INSTALL_DIR} mkdir -p ${INSTALL_DIR}
# Clone LLVM 18 (any revision after commit bd32aaa is supposed to work)
if [ ! -d "llvm-project" ]; then
git clone --depth 1 --branch release/18.x https://github.com/llvm/llvm-project.git
fi
# Build LLVM, clang # Build LLVM, clang
LLVM_SRC_PREFIX=${PWD}/llvm-project LLVM_SRC_PREFIX=${PWD}/llvm-project
LLVM_SRC_DIR=${LLVM_SRC_PREFIX}/llvm LLVM_SRC_DIR=${LLVM_SRC_PREFIX}/llvm
LLVM_BUILD_DIR=${PWD}/build/llvm LLVM_BUILD_DIR=${PWD}/build/llvm
./clone-llvm.sh "${LLVM_SRC_PREFIX}"
if [ ! -d ${LLVM_BUILD_DIR} ] ; then if [ ! -d ${LLVM_BUILD_DIR} ] ; then
mkdir -p ${LLVM_BUILD_DIR} mkdir -p ${LLVM_BUILD_DIR}
fi fi
Executable
+18
View File
@@ -0,0 +1,18 @@
#!/bin/bash
set -euo pipefail
# Default directory for cloning the llvm-project repository
DEFAULT_DIR="llvm-project"
# Check if a directory argument is provided
if [ $# -eq 1 ]; then
DIR=$1
else
DIR=$DEFAULT_DIR
fi
# Clone LLVM 18 (any revision after commit bd32aaa is supposed to work)
if [ ! -d "${DIR}" ]; then
git clone --depth 1 --branch release/18.x https://github.com/llvm/llvm-project.git "${DIR}"
fi
+73
View File
@@ -0,0 +1,73 @@
# Benchmarks
## Table of Contents
- [Benchmark Results](#benchmark-results)
- [Baseline](#baseline)
- [OddPorduct](#oddporduct)
- [TriangleNumber](#trianglenumber)
- [FibonacciRecursive](#fibonaccirecursive)
- [FibonacciIterative](#fibonacciiterative)
- [FibonacciBinet](#fibonaccibinet)
- [SHA1](#sha1)
## Benchmark Results
### Baseline
| | `EVM` | `PVMInterpreter` |
|:--------|:------------------------|:-------------------------------- |
| **`0`** | `3.36 us` (✅ **1.00x**) | `11.84 us` (❌ *3.52x slower*) |
### OddPorduct
| | `EVM` | `PVMInterpreter` |
|:-------------|:-------------------------|:-------------------------------- |
| **`10000`** | `3.11 ms` (✅ **1.00x**) | `1.53 ms` (🚀 **2.03x faster**) |
| **`100000`** | `30.70 ms` (✅ **1.00x**) | `15.54 ms` (🚀 **1.98x faster**) |
| **`300000`** | `92.68 ms` (✅ **1.00x**) | `45.47 ms` (🚀 **2.04x faster**) |
### TriangleNumber
| | `EVM` | `PVMInterpreter` |
|:-------------|:-------------------------|:-------------------------------- |
| **`10000`** | `2.29 ms` (✅ **1.00x**) | `1.09 ms` (🚀 **2.11x faster**) |
| **`100000`** | `22.84 ms` (✅ **1.00x**) | `10.66 ms` (🚀 **2.14x faster**) |
| **`360000`** | `82.29 ms` (✅ **1.00x**) | `37.01 ms` (🚀 **2.22x faster**) |
### FibonacciRecursive
| | `EVM` | `PVMInterpreter` |
|:---------|:--------------------------|:--------------------------------- |
| **`12`** | `135.67 us` (✅ **1.00x**) | `125.02 us` (✅ **1.09x faster**) |
| **`16`** | `903.75 us` (✅ **1.00x**) | `762.79 us` (✅ **1.18x faster**) |
| **`20`** | `6.12 ms` (✅ **1.00x**) | `4.96 ms` (✅ **1.23x faster**) |
| **`24`** | `42.05 ms` (✅ **1.00x**) | `33.86 ms` (✅ **1.24x faster**) |
### FibonacciIterative
| | `EVM` | `PVMInterpreter` |
|:----------|:-------------------------|:-------------------------------- |
| **`64`** | `15.04 us` (✅ **1.00x**) | `29.45 us` (❌ *1.96x slower*) |
| **`128`** | `26.36 us` (✅ **1.00x**) | `42.19 us` (❌ *1.60x slower*) |
| **`256`** | `48.61 us` (✅ **1.00x**) | `65.71 us` (❌ *1.35x slower*) |
### FibonacciBinet
| | `EVM` | `PVMInterpreter` |
|:----------|:-------------------------|:-------------------------------- |
| **`64`** | `15.22 us` (✅ **1.00x**) | `41.46 us` (❌ *2.72x slower*) |
| **`128`** | `17.05 us` (✅ **1.00x**) | `42.84 us` (❌ *2.51x slower*) |
| **`256`** | `19.00 us` (✅ **1.00x**) | `44.36 us` (❌ *2.34x slower*) |
### SHA1
| | `EVM` | `PVMInterpreter` |
|:----------|:--------------------------|:--------------------------------- |
| **`1`** | `110.04 us` (✅ **1.00x**) | `216.11 us` (❌ *1.96x slower*) |
| **`64`** | `209.04 us` (✅ **1.00x**) | `309.48 us` (❌ *1.48x slower*) |
| **`512`** | `903.65 us` (✅ **1.00x**) | `980.49 us` (✅ **1.09x slower**) |
---
Made with [criterion-table](https://github.com/nu11ptr/criterion-table)
+4 -9
View File
@@ -9,17 +9,16 @@ description = "revive compiler benchmarks"
[features] [features]
default = ["bench-pvm-interpreter"] default = ["bench-pvm-interpreter"]
bench-pvm-interpreter = [] bench-pvm-interpreter = ["revive-runner"]
bench-pvm = []
bench-evm = ["revive-differential"] bench-evm = ["revive-differential"]
bench-extensive = []
[dependencies] [dependencies]
hex = { workspace = true } hex = { workspace = true }
polkavm = { workspace = true } alloy-primitives = { workspace = true }
revive-integration = { workspace = true } revive-integration = { workspace = true }
revive-differential = { workspace = true, optional = true } revive-differential = { workspace = true, optional = true }
alloy-primitives = { workspace = true } revive-runner = { workspace = true, optional = true }
[dev-dependencies] [dev-dependencies]
criterion = { workspace = true } criterion = { workspace = true }
@@ -27,7 +26,3 @@ criterion = { workspace = true }
[[bench]] [[bench]]
name = "execute" name = "execute"
harness = false harness = false
[[bench]]
name = "prepare"
harness = false
+32 -118
View File
@@ -1,104 +1,42 @@
#![cfg(any(feature = "bench-pvm-interpreter", feature = "bench-evm"))]
use alloy_primitives::U256; use alloy_primitives::U256;
use criterion::{ use criterion::{
criterion_group, criterion_main, measurement::Measurement, BenchmarkGroup, BenchmarkId, criterion_group, criterion_main,
Criterion, measurement::{Measurement, WallTime},
BenchmarkGroup, BenchmarkId, Criterion,
}; };
use revive_integration::cases::Contract; use revive_integration::cases::Contract;
fn bench<P, L, I, M>(mut group: BenchmarkGroup<'_, M>, parameters: &[P], labels: &[L], contract: I) fn bench<P, L, I>(
where mut group: BenchmarkGroup<'_, WallTime>,
parameters: &[P],
labels: &[L],
contract: I,
) where
P: Clone, P: Clone,
L: std::fmt::Display, L: std::fmt::Display,
I: Fn(P) -> Contract, I: Fn(P) -> Contract,
M: Measurement,
{ {
assert_eq!(parameters.len(), labels.len()); assert_eq!(parameters.len(), labels.len());
group.sample_size(10);
for (p, l) in parameters.iter().zip(labels.iter()) { for (p, l) in parameters.iter().zip(labels.iter()) {
let contract = contract(p.clone());
#[cfg(feature = "bench-evm")] #[cfg(feature = "bench-evm")]
{ group.bench_with_input(BenchmarkId::new("EVM", l), p, |b, _| {
let contract = contract(p.clone()); let code = &contract.evm_runtime;
group.bench_with_input(BenchmarkId::new("EVM", l), p, move |b, _| { let input = &contract.calldata;
b.iter(|| { b.iter_custom(|iters| revive_benchmarks::measure_evm(code, input, iters));
revive_differential::execute(revive_differential::prepare( });
contract.evm_runtime.clone(),
contract.calldata.clone(),
));
});
});
}
#[cfg(not(feature = "bench-extensive"))] #[cfg(feature = "bench-pvm-interpreter")]
{ group.bench_with_input(BenchmarkId::new("PVMInterpreter", l), p, |b, _| {
#[cfg(all(feature = "bench-pvm-interpreter", not(feature = "bench-extensive")))] let specs = revive_benchmarks::create_specs(&contract);
{ b.iter_custom(|iters| revive_benchmarks::measure_pvm(&specs, iters));
let contract = contract(p.clone()); });
let (transaction, mut instance, export) = revive_benchmarks::prepare_pvm(
&contract.pvm_runtime,
contract.calldata,
polkavm::BackendKind::Interpreter,
);
group.bench_with_input(BenchmarkId::new("PVMInterpreter", l), p, |b, _| {
b.iter(|| {
let _ = transaction.clone().call_on(&mut instance, export);
});
});
}
#[cfg(all(feature = "bench-pvm", not(feature = "bench-extensive")))]
{
let contract = contract(p.clone());
let (transaction, mut instance, export) = revive_benchmarks::prepare_pvm(
&contract.pvm_runtime,
contract.calldata,
polkavm::BackendKind::Compiler,
);
group.bench_with_input(BenchmarkId::new("PVM", l), p, |b, _| {
b.iter(|| {
let _ = transaction.clone().call_on(&mut instance, export);
});
});
}
}
#[cfg(feature = "bench-extensive")]
{
use revive_benchmarks::instantiate_engine;
use revive_integration::mock_runtime::{instantiate_module, recompile_code, State};
#[cfg(feature = "bench-pvm-interpreter")]
{
let contract = contract(p.clone());
let engine = instantiate_engine(polkavm::BackendKind::Interpreter);
let module = recompile_code(&contract.pvm_runtime, &engine);
let transaction = State::default()
.transaction()
.with_default_account(&contract.pvm_runtime)
.calldata(contract.calldata);
group.bench_with_input(BenchmarkId::new("PVMInterpreter", l), p, |b, _| {
b.iter(|| {
let (mut instance, export) = instantiate_module(&module, &engine);
let _ = transaction.clone().call_on(&mut instance, export);
});
});
}
#[cfg(feature = "bench-pvm")]
{
let contract = contract(p.clone());
let engine = instantiate_engine(polkavm::BackendKind::Compiler);
let module = recompile_code(&contract.pvm_runtime, &engine);
let transaction = State::default()
.transaction()
.with_default_account(&contract.pvm_runtime)
.calldata(contract.calldata);
group.bench_with_input(BenchmarkId::new("PVM", l), p, |b, _| {
b.iter(|| {
let (mut instance, export) = instantiate_module(&module, &engine);
let _ = transaction.clone().call_on(&mut instance, export);
});
});
}
}
} }
group.finish(); group.finish();
@@ -108,15 +46,7 @@ fn group<'error, M>(c: &'error mut Criterion<M>, group_name: &str) -> BenchmarkG
where where
M: Measurement, M: Measurement,
{ {
#[cfg(feature = "bench-extensive")] c.benchmark_group(group_name)
{
let mut group = c.benchmark_group(group_name);
group.sample_size(10);
group
}
#[cfg(not(feature = "bench-extensive"))]
return c.benchmark_group(group_name);
} }
fn bench_baseline(c: &mut Criterion) { fn bench_baseline(c: &mut Criterion) {
@@ -128,33 +58,21 @@ fn bench_baseline(c: &mut Criterion) {
fn bench_odd_product(c: &mut Criterion) { fn bench_odd_product(c: &mut Criterion) {
let group = group(c, "OddPorduct"); let group = group(c, "OddPorduct");
#[cfg(feature = "bench-extensive")] let parameters = &[10_000, 100_000, 300000];
let parameters = &[300000, 1200000, 12000000, 180000000, 720000000];
#[cfg(not(feature = "bench-extensive"))]
let parameters = &[10_000, 100_000];
bench(group, parameters, parameters, Contract::odd_product); bench(group, parameters, parameters, Contract::odd_product);
} }
fn bench_triangle_number(c: &mut Criterion) { fn bench_triangle_number(c: &mut Criterion) {
let group = group(c, "TriangleNumber"); let group = group(c, "TriangleNumber");
#[cfg(feature = "bench-extensive")] let parameters = &[10_000, 100_000, 360000];
let parameters = &[360000, 1440000, 14400000, 216000000, 864000000];
#[cfg(not(feature = "bench-extensive"))]
let parameters = &[10_000, 100_000];
bench(group, parameters, parameters, Contract::triangle_number); bench(group, parameters, parameters, Contract::triangle_number);
} }
fn bench_fibonacci_recurisve(c: &mut Criterion) { fn bench_fibonacci_recurisve(c: &mut Criterion) {
let group = group(c, "FibonacciRecursive"); let group = group(c, "FibonacciRecursive");
#[cfg(feature = "bench-extensive")] let parameters = [12, 16, 20, 24]
let parameters = [24, 27, 31, 36, 39]
.iter()
.map(|p| U256::from(*p))
.collect::<Vec<_>>();
#[cfg(not(feature = "bench-extensive"))]
let parameters = [12, 16, 20]
.iter() .iter()
.map(|p| U256::from(*p)) .map(|p| U256::from(*p))
.collect::<Vec<_>>(); .collect::<Vec<_>>();
@@ -164,12 +82,6 @@ fn bench_fibonacci_recurisve(c: &mut Criterion) {
fn bench_fibonacci_iterative(c: &mut Criterion) { fn bench_fibonacci_iterative(c: &mut Criterion) {
let group = group(c, "FibonacciIterative"); let group = group(c, "FibonacciIterative");
#[cfg(feature = "bench-extensive")]
let parameters = [256, 162500, 650000, 6500000, 100000000, 400000000]
.iter()
.map(|p| U256::from(*p))
.collect::<Vec<_>>();
#[cfg(not(feature = "bench-extensive"))]
let parameters = [64, 128, 256] let parameters = [64, 128, 256]
.iter() .iter()
.map(|p| U256::from(*p)) .map(|p| U256::from(*p))
@@ -193,7 +105,9 @@ fn bench_sha1(c: &mut Criterion) {
let parameters = &[vec![0xff], vec![0xff; 64], vec![0xff; 512]]; let parameters = &[vec![0xff], vec![0xff; 64], vec![0xff; 512]];
let labels = parameters.iter().map(|p| p.len()).collect::<Vec<_>>(); let labels = parameters.iter().map(|p| p.len()).collect::<Vec<_>>();
bench(group, parameters, &labels, Contract::sha1); bench(group, parameters, &labels, |input| {
Contract::sha1(input.into())
});
} }
criterion_group!( criterion_group!(
-171
View File
@@ -1,171 +0,0 @@
use alloy_primitives::U256;
use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion};
use revive_integration::cases::Contract;
fn bench(
c: &mut Criterion,
group_name: &str,
#[cfg(feature = "bench-evm")] evm_runtime: Vec<u8>,
#[cfg(any(feature = "bench-pvm-interpreter", feature = "bench-pvm"))] pvm_runtime: Vec<u8>,
) {
let mut group = c.benchmark_group(group_name);
let code_size = 0;
#[cfg(feature = "bench-evm")]
group.bench_with_input(
BenchmarkId::new("Evm", code_size),
&evm_runtime,
|b, code| b.iter(|| revive_differential::prepare(code.clone(), Vec::new())),
);
#[cfg(feature = "bench-pvm-interpreter")]
{
let engine = revive_benchmarks::instantiate_engine(polkavm::BackendKind::Interpreter);
group.bench_with_input(
BenchmarkId::new("PVMInterpreterCompile", code_size),
&(&pvm_runtime, engine),
|b, (code, engine)| {
b.iter(|| {
revive_integration::mock_runtime::recompile_code(code, engine);
});
},
);
}
#[cfg(feature = "bench-pvm-interpreter")]
{
let engine = revive_benchmarks::instantiate_engine(polkavm::BackendKind::Interpreter);
let module = revive_integration::mock_runtime::recompile_code(&pvm_runtime, &engine);
group.bench_with_input(
BenchmarkId::new("PVMInterpreterInstantiate", code_size),
&(module, engine),
|b, (module, engine)| {
b.iter(|| {
revive_integration::mock_runtime::instantiate_module(module, engine);
});
},
);
}
#[cfg(feature = "bench-pvm")]
{
let engine = revive_benchmarks::instantiate_engine(polkavm::BackendKind::Compiler);
group.bench_with_input(
BenchmarkId::new("PVMCompile", code_size),
&(&pvm_runtime, engine),
|b, (code, engine)| {
b.iter(|| {
revive_integration::mock_runtime::recompile_code(code, engine);
});
},
);
}
#[cfg(feature = "bench-pvm")]
{
let engine = revive_benchmarks::instantiate_engine(polkavm::BackendKind::Compiler);
let module = revive_integration::mock_runtime::recompile_code(&pvm_runtime, &engine);
group.bench_with_input(
BenchmarkId::new("PVMInstantiate", code_size),
&(module, engine),
|b, (module, engine)| {
b.iter(|| {
revive_integration::mock_runtime::instantiate_module(module, engine);
});
},
);
}
group.finish();
}
fn bench_baseline(c: &mut Criterion) {
bench(
c,
"PrepareBaseline",
#[cfg(feature = "bench-evm")]
Contract::baseline().evm_runtime,
#[cfg(any(feature = "bench-pvm-interpreter", feature = "bench-pvm"))]
Contract::baseline().pvm_runtime,
);
}
fn bench_odd_product(c: &mut Criterion) {
bench(
c,
"PrepareOddProduct",
#[cfg(feature = "bench-evm")]
Contract::odd_product(0).evm_runtime,
#[cfg(any(feature = "bench-pvm-interpreter", feature = "bench-pvm"))]
Contract::baseline().pvm_runtime,
);
}
fn bench_triangle_number(c: &mut Criterion) {
bench(
c,
"PrepareTriangleNumber",
#[cfg(feature = "bench-evm")]
Contract::triangle_number(0).evm_runtime,
#[cfg(any(feature = "bench-pvm-interpreter", feature = "bench-pvm"))]
Contract::triangle_number(0).pvm_runtime,
);
}
fn bench_fibonacci_recursive(c: &mut Criterion) {
bench(
c,
"PrepareFibonacciRecursive",
#[cfg(feature = "bench-evm")]
Contract::fib_recursive(U256::ZERO).evm_runtime,
#[cfg(any(feature = "bench-pvm-interpreter", feature = "bench-pvm"))]
Contract::fib_recursive(U256::ZERO).pvm_runtime,
);
}
fn bench_fibonacci_iterative(c: &mut Criterion) {
bench(
c,
"PrepareFibonacciIterative",
#[cfg(feature = "bench-evm")]
Contract::fib_iterative(U256::ZERO).evm_runtime,
#[cfg(any(feature = "bench-pvm-interpreter", feature = "bench-pvm"))]
Contract::fib_iterative(U256::ZERO).pvm_runtime,
);
}
fn bench_fibonacci_binet(c: &mut Criterion) {
bench(
c,
"PrepareFibonacciBinet",
#[cfg(feature = "bench-evm")]
Contract::fib_binet(U256::ZERO).evm_runtime,
#[cfg(any(feature = "bench-pvm-interpreter", feature = "bench-pvm"))]
Contract::fib_binet(U256::ZERO).pvm_runtime,
);
}
fn bench_sha1(c: &mut Criterion) {
bench(
c,
"PrepareSHA1",
#[cfg(feature = "bench-evm")]
Contract::sha1(Default::default()).evm_runtime,
#[cfg(any(feature = "bench-pvm-interpreter", feature = "bench-pvm"))]
Contract::sha1(Default::default()).pvm_runtime,
);
}
criterion_group!(
name = prepare;
config = Criterion::default();
targets = bench_baseline,
bench_odd_product,
bench_triangle_number,
bench_fibonacci_recursive,
bench_fibonacci_iterative,
bench_fibonacci_binet,
bench_sha1
);
criterion_main!(prepare);
+67 -25
View File
@@ -1,28 +1,70 @@
use polkavm::{BackendKind, Config, Engine, ExportIndex, Instance, SandboxKind}; #[cfg(feature = "bench-pvm-interpreter")]
use revive_integration::mock_runtime::{self, TransactionBuilder}; pub fn create_specs(contract: &revive_integration::cases::Contract) -> revive_runner::Specs {
use revive_integration::mock_runtime::{State, Transaction}; use revive_runner::*;
use SpecsAction::*;
pub fn prepare_pvm( Specs {
code: &[u8], differential: false,
input: Vec<u8>, actions: vec![
backend: BackendKind, Instantiate {
) -> (TransactionBuilder, Instance<Transaction>, ExportIndex) { code: Code::Bytes(contract.pvm_runtime.to_vec()),
let mut config = Config::new(); origin: TestAddress::Alice,
config.set_backend(Some(backend)); data: Default::default(),
config.set_sandbox(Some(SandboxKind::Linux)); value: Default::default(),
gas_limit: Default::default(),
let (instance, export_index) = mock_runtime::prepare(code, Some(config)); storage_deposit_limit: Default::default(),
let transaction = State::default() salt: Default::default(),
.transaction() },
.with_default_account(code) Call {
.calldata(input); origin: TestAddress::Alice,
dest: TestAddress::Instantiated(0),
(transaction, instance, export_index) data: contract.calldata.to_vec(),
value: Default::default(),
gas_limit: Default::default(),
storage_deposit_limit: Default::default(),
},
],
..Default::default()
}
} }
pub fn instantiate_engine(backend: BackendKind) -> Engine { #[cfg(feature = "bench-pvm-interpreter")]
let mut config = Config::new(); pub fn measure_pvm(specs: &revive_runner::Specs, iters: u64) -> std::time::Duration {
config.set_backend(Some(backend)); use revive_runner::*;
config.set_sandbox(Some(SandboxKind::Linux)); let mut total_time = std::time::Duration::default();
mock_runtime::setup(Some(config))
for _ in 0..iters {
let results = specs.clone().run();
let CallResult::Exec { result, wall_time } =
results.get(1).expect("contract should have been called")
else {
panic!("expected a execution result");
};
let ret = result.result.as_ref().unwrap();
assert!(!ret.did_revert());
total_time += *wall_time;
}
total_time
}
#[cfg(feature = "bench-evm")]
pub fn measure_evm(code: &[u8], input: &[u8], iters: u64) -> std::time::Duration {
let mut total_time = std::time::Duration::default();
let code = hex::encode(code);
for _ in 0..iters {
let log = revive_differential::Evm::default()
.code_blob(code.as_bytes().to_vec())
.input(input.to_vec().into())
.bench(true)
.run();
assert!(log.output.run_success(), "evm run failed: {log:?}");
total_time += log.execution_time().unwrap();
}
total_time
} }
-3
View File
@@ -8,7 +8,4 @@ authors.workspace = true
build = "build.rs" build = "build.rs"
description = "compiler builtins for the revive compiler" description = "compiler builtins for the revive compiler"
[features]
riscv-64 = []
[dependencies] [dependencies]
-3
View File
@@ -1,8 +1,5 @@
use std::{env, fs, io::Read, path::Path, process::Command}; use std::{env, fs, io::Read, path::Path, process::Command};
#[cfg(not(feature = "riscv-64"))]
pub const BUILTINS_ARCHIVE_FILE: &str = "libclang_rt.builtins-riscv32.a";
#[cfg(feature = "riscv-64")]
pub const BUILTINS_ARCHIVE_FILE: &str = "libclang_rt.builtins-riscv64.a"; pub const BUILTINS_ARCHIVE_FILE: &str = "libclang_rt.builtins-riscv64.a";
fn main() { fn main() {
-39
View File
@@ -1,39 +0,0 @@
---
name: Bug report
about: Use this template for reporting issues
title: ''
labels: bug
assignees: ''
---
### 🐛 Bug Report
#### 📝 Description
Provide a clear and concise description of the bug.
#### 🔄 Reproduction Steps
Steps to reproduce the behaviour
#### 🤔 Expected Behavior
Describe what you expected to happen.
#### 😯 Current Behavior
Describe what actually happened.
#### 🖥️ Environment
Any relevant environment details.
#### 📋 Additional Context
Add any other context about the problem here. If applicable, add screenshots to help explain.
#### 📎 Log Output
```
Paste any relevant log output here.
```
-21
View File
@@ -1,21 +0,0 @@
---
name: Feature request
about: Use this template for requesting features
title: ''
labels: feat
assignees: ''
---
### 🌟 Feature Request
#### 📝 Description
Provide a clear and concise description of the feature you'd like to see.
#### 🤔 Rationale
Explain why this feature is important and how it benefits the project.
#### 📋 Additional Context
Add any other context or information about the feature request here.
-20
View File
@@ -1,20 +0,0 @@
# What ❔
<!-- What are the changes this PR brings about? -->
<!-- Example: This PR adds a PR template to the repo. -->
<!-- (For bigger PRs adding more context is appreciated) -->
## Why ❔
<!-- Why are these changes done? What goal do they contribute to? What are the principles behind them? -->
<!-- Example: PR templates ensure PR reviewers, observers, and future iterators are in context about the evolution of repos. -->
## Checklist
<!-- Check your PR fulfills the following items. -->
<!-- For draft PRs check the boxes as you complete them. -->
- [ ] PR title corresponds to the body of PR.
- [ ] Tests for the changes have been added / updated.
- [ ] Documentation comments have been added / updated.
- [ ] Code has been formatted via `cargo fmt` and checked with `cargo clippy`.
-9
View File
@@ -1,9 +0,0 @@
name: Cargo license check
on: pull_request
jobs:
cargo-deny:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: EmbarkStudios/cargo-deny-action@v1
-23
View File
@@ -1,23 +0,0 @@
name: "Rust CI"
on:
pull_request:
jobs:
build:
name: cargo build
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions-rust-lang/setup-rust-toolchain@v1
- run: cargo build --verbose
formatting:
name: cargo fmt
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions-rust-lang/setup-rust-toolchain@v1
with:
components: rustfmt
- name: Rustfmt Check
uses: actions-rust-lang/rustfmt@v1
-17
View File
@@ -1,17 +0,0 @@
name: Leaked Secrets Scan
on: [pull_request]
jobs:
TruffleHog:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@ac593985615ec2ede58e132d2e21d2b1cbd6127c # v3
with:
fetch-depth: 0
- name: TruffleHog OSS
uses: trufflesecurity/trufflehog@0c66d30c1f4075cee1aada2e1ab46dabb1b0071a
with:
path: ./
base: ${{ github.event.repository.default_branch }}
head: HEAD
extra_args: --debug --only-verified
-3
View File
@@ -13,9 +13,6 @@ description = "Shared constants of the revive compiler"
[lib] [lib]
doctest = false doctest = false
[features]
riscv-64 = []
[dependencies] [dependencies]
anyhow = { workspace = true } anyhow = { workspace = true }
serde = { workspace = true, features = ["derive"] } serde = { workspace = true, features = ["derive"] }
+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 = crate::byte_length::BYTE_LENGTH_VALUE * BIT_LENGTH_BYTE; pub const BIT_LENGTH_VALUE: usize = BIT_LENGTH_WORD;
/// 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 =
+5 -8
View File
@@ -6,15 +6,12 @@ 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;
@@ -25,7 +22,7 @@ pub const BYTE_LENGTH_WORD: usize = 32;
pub const BYTE_LENGTH_VALUE: usize = 32; pub const BYTE_LENGTH_VALUE: usize = 32;
/// Byte length of the runtime block number type. /// Byte length of the runtime block number type.
pub const BYTE_LENGTH_BLOCK_NUMBER: usize = 8; pub const BYTE_LENGTH_BLOCK_NUMBER: usize = 4;
/// Byte length of the runtime block timestamp type. /// Byte length of the runtime block timestamp type.
pub const BYTE_LENGTH_BLOCK_TIMESTAMP: usize = 8; pub const BYTE_LENGTH_BLOCK_TIMESTAMP: usize = 4;
+1 -3
View File
@@ -1,6 +1,4 @@
//! //! The revive exit code constants.
//! The exit code constants.
//!
/// The common application success exit code. /// The common application success exit code.
pub const EXIT_CODE_SUCCESS: i32 = 0; pub const EXIT_CODE_SUCCESS: i32 = 0;
+3
View File
@@ -41,3 +41,6 @@ pub static EXTENSION_POLKAVM_ASSEMBLY: &str = "pvmasm";
/// The PolkaVM bytecode file extension. /// The PolkaVM bytecode file extension.
pub static EXTENSION_POLKAVM_BINARY: &str = "pvm"; pub static EXTENSION_POLKAVM_BINARY: &str = "pvm";
/// The ELF shared object file extension.
pub static EXTENSION_SHARED_OBJECT: &str = "so";
-2
View File
@@ -6,7 +6,6 @@ pub(crate) mod byte_length;
pub(crate) mod evm_version; pub(crate) mod evm_version;
pub(crate) mod exit_code; pub(crate) mod exit_code;
pub(crate) mod extension; pub(crate) mod extension;
pub(crate) mod polkavm;
pub(crate) mod utils; pub(crate) mod utils;
pub use self::base::*; pub use self::base::*;
@@ -15,5 +14,4 @@ pub use self::byte_length::*;
pub use self::evm_version::EVMVersion; pub use self::evm_version::EVMVersion;
pub use self::exit_code::*; pub use self::exit_code::*;
pub use self::extension::*; pub use self::extension::*;
pub use self::polkavm::address::*;
pub use self::utils::*; pub use self::utils::*;
-115
View File
@@ -1,115 +0,0 @@
//! The PolkaVM address constants.
/// The corresponding simulation predefined address.
pub const POLKAVM_ADDRESS_TO_L1: u16 = 0xFFFF;
/// The corresponding simulation predefined address.
pub const POLKAVM_ADDRESS_PRECOMPILE: u16 = 0xFFFD;
/// The corresponding simulation predefined address.
pub const POLKAVM_ADDRESS_META: u16 = 0xFFFC;
/// The corresponding simulation predefined address.
pub const POLKAVM_ADDRESS_MIMIC_CALL: u16 = 0xFFFB;
/// The corresponding simulation predefined address.
pub const POLKAVM_ADDRESS_SYSTEM_MIMIC_CALL: u16 = 0xFFFA;
/// The corresponding simulation predefined address.
pub const POLKAVM_ADDRESS_MIMIC_CALL_BYREF: u16 = 0xFFF9;
/// The corresponding simulation predefined address.
pub const POLKAVM_ADDRESS_SYSTEM_MIMIC_CALL_BYREF: u16 = 0xFFF8;
/// The corresponding simulation predefined address.
pub const POLKAVM_ADDRESS_RAW_FAR_CALL: u16 = 0xFFF7;
/// The corresponding simulation predefined address.
pub const POLKAVM_ADDRESS_RAW_FAR_CALL_BYREF: u16 = 0xFFF6;
/// The corresponding simulation predefined address.
pub const POLKAVM_ADDRESS_SYSTEM_CALL: u16 = 0xFFF5;
/// The corresponding simulation predefined address.
pub const POLKAVM_ADDRESS_SYSTEM_CALL_BYREF: u16 = 0xFFF4;
/// The corresponding simulation predefined address.
pub const POLKAVM_ADDRESS_SET_CONTEXT_VALUE_CALL: u16 = 0xFFF3;
/// The corresponding simulation predefined address.
pub const POLKAVM_ADDRESS_SET_PUBDATA_PRICE: u16 = 0xFFF2;
/// The corresponding simulation predefined address.
pub const POLKAVM_ADDRESS_INCREMENT_TX_COUNTER: u16 = 0xFFF1;
/// The corresponding simulation predefined address.
pub const POLKAVM_ADDRESS_GET_GLOBAL_PTR_CALLDATA: u16 = 0xFFF0;
/// The corresponding simulation predefined address.
pub const POLKAVM_ADDRESS_GET_GLOBAL_CALL_FLAGS: u16 = 0xFFEF;
/// The corresponding simulation predefined address.
pub const POLKAVM_ADDRESS_GET_GLOBAL_PTR_RETURN_DATA: u16 = 0xFFEE;
/// The corresponding simulation predefined address.
pub const POLKAVM_ADDRESS_EVENT_INITIALIZE: u16 = 0xFFED;
/// The corresponding simulation predefined address.
pub const POLKAVM_ADDRESS_EVENT_WRITE: u16 = 0xFFEC;
/// The corresponding simulation predefined address.
pub const POLKAVM_ADDRESS_ACTIVE_PTR_LOAD_CALLDATA: u16 = 0xFFEB;
/// The corresponding simulation predefined address.
pub const POLKAVM_ADDRESS_ACTIVE_PTR_LOAD_RETURN_DATA: u16 = 0xFFEA;
/// The corresponding simulation predefined address.
pub const POLKAVM_ADDRESS_ACTIVE_PTR_ADD: u16 = 0xFFE9;
/// The corresponding simulation predefined address.
pub const POLKAVM_ADDRESS_ACTIVE_PTR_SHRINK: u16 = 0xFFE8;
/// The corresponding simulation predefined address.
pub const POLKAVM_ADDRESS_ACTIVE_PTR_PACK: u16 = 0xFFE7;
/// The corresponding simulation predefined address.
pub const POLKAVM_ADDRESS_MULTIPLICATION_HIGH_REGISTER: u16 = 0xFFE6;
/// The corresponding simulation predefined address.
pub const POLKAVM_ADDRESS_GET_GLOBAL_EXTRA_ABI_DATA: u16 = 0xFFE5;
/// The corresponding simulation predefined address.
pub const POLKAVM_ADDRESS_ACTIVE_PTR_DATA_LOAD: u16 = 0xFFE4;
/// The corresponding simulation predefined address.
pub const POLKAVM_ADDRESS_ACTIVE_PTR_DATA_COPY: u16 = 0xFFE3;
/// The corresponding simulation predefined address.
pub const POLKAVM_ADDRESS_ACTIVE_PTR_DATA_SIZE: u16 = 0xFFE2;
/// The corresponding simulation predefined address.
pub const POLKAVM_ADDRESS_CONST_ARRAY_DECLARE: u16 = 0xFFE1;
/// The corresponding simulation predefined address.
pub const POLKAVM_ADDRESS_CONST_ARRAY_SET: u16 = 0xFFE0;
/// The corresponding simulation predefined address.
pub const POLKAVM_ADDRESS_CONST_ARRAY_FINALIZE: u16 = 0xFFDF;
/// The corresponding simulation predefined address.
pub const POLKAVM_ADDRESS_CONST_ARRAY_GET: u16 = 0xFFDE;
/// The corresponding simulation predefined address.
pub const POLKAVM_ADDRESS_DECOMMIT: u16 = 0xFFDD;
/// The corresponding simulation predefined address.
pub const POLKAVM_ADDRESS_ACTIVE_PTR_LOAD_DECOMMIT: u16 = 0xFFDC;
/// The corresponding simulation predefined address.
pub const POLKAVM_ADDRESS_RETURN_FORWARD: u16 = 0xFFDB;
/// The corresponding simulation predefined address.
pub const POLKAVM_ADDRESS_REVERT_FORWARD: u16 = 0xFFDA;
/// The corresponding simulation predefined address.
pub const POLKAVM_ADDRESS_ACTIVE_PTR_SWAP: u16 = 0xFFD9;
-5
View File
@@ -1,5 +0,0 @@
//!
//! The PolkaVM constants.
//!
pub mod address;
+7 -3
View File
@@ -7,6 +7,10 @@ authors.workspace = true
repository.workspace = true repository.workspace = true
[dependencies] [dependencies]
evm-interpreter = { workspace = true } hex = { workspace = true }
primitive-types = { workspace = true } tempfile = { workspace = true }
alloy-primitives = { workspace = true } serde = { workspace = true }
serde_json = { workspace = true }
alloy-primitives = { workspace = true, features = ["serde"] }
alloy-genesis = { workspace = true }
alloy-serde = { workspace = true }
+40
View File
@@ -0,0 +1,40 @@
{
"config": {
"chainId": 420420420,
"homesteadBlock": 0,
"eip150Block": 0,
"eip155Block": 0,
"eip158Block": 0,
"byzantiumBlock": 0,
"constantinopleBlock": 0,
"petersburgBlock": 0,
"istanbulBlock": 0,
"berlinBlock": 0,
"londonBlock": 0,
"arrowGlacierBlock": 0,
"grayGlacierBlock": 0,
"shanghaiTime": 0,
"cancunTime": 0,
"terminalTotalDifficulty": 0,
"terminalTotalDifficultyPassed": true
},
"coinbase": "0x0000000000000000000000000000000000000000",
"difficulty": "0x20000",
"extraData": "",
"gasLimit": "0xffffffff",
"nonce": "0x0000000000000042",
"mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"timestamp": "0x00",
"alloc": {
"0101010101010101010101010101010101010101": {
"balance": "1000000000"
},
"0202020202020202020202020202020202020202": {
"balance": "1000000000"
},
"0303030303030303030303030303030303030303": {
"balance": "1000000000"
}
}
}
+199
View File
@@ -0,0 +1,199 @@
use std::time::Duration;
/// Parse a go formatted duration.
///
/// Sources:
/// - https://crates.io/crates/go-parse-duration (fixed an utf8 bug)
/// - https://github.com/golang/go/blob/master/src/time/format.go
pub fn parse_go_duration(value: &str) -> Result<Duration, String> {
parse_duration(value).map(|ns| Duration::from_nanos(ns.unsigned_abs()))
}
fn parse_duration(string: &str) -> Result<i64, String> {
// [-+]?([0-9]*(\.[0-9]*)?[a-z]+)+
let mut s = string;
let mut d: i64 = 0; // duration to be returned
let mut neg = false;
// Consume [-+]?
if !s.is_empty() {
let c = *s.as_bytes().first().unwrap();
if c == b'-' || c == b'+' {
neg = c == b'-';
s = &s[1..];
}
}
// Special case: if all that is left is "0", this is zero.
if s == "0" {
return Ok(0);
}
if s.is_empty() {
return Err(format!("invalid duration: {string}"));
}
while !s.is_empty() {
// integers before, after decimal point
let mut v: i64;
let mut f: i64 = 0;
// value = v + f / scale
let mut scale: f64 = 1f64;
// The next character must be [0-9.]
let c = *s.as_bytes().first().unwrap();
if !(c == b'.' || c.is_ascii_digit()) {
return Err(format!("invalid duration: {string}"));
}
// Consume [0-9]*
let pl = s.len();
match leading_int(s) {
Ok((_v, _s)) => {
v = _v;
s = _s;
}
Err(_) => {
return Err(format!("invalid duration: {string}"));
}
}
let pre = pl != s.len(); // whether we consume anything before a period
// Consume (\.[0-9]*)?
let mut post = false;
if !s.is_empty() && *s.as_bytes().first().unwrap() == b'.' {
s = &s[1..];
let pl = s.len();
let (f_, scale_, s_) = leading_fraction(s);
{
f = f_;
scale = scale_;
s = s_;
}
post = pl != s.len();
}
if !pre && !post {
// no digits (e.g. ".s" or "-.s")
return Err(format!("invalid duration: {string}"));
}
// Consume unit.
let mut i = 0;
while i < s.len() {
let c = *s.as_bytes().get(i).unwrap();
if c == b'.' || c.is_ascii_digit() {
break;
}
i += 1;
}
if i == 0 {
return Err(format!("missing unit in duration: {string}"));
}
let u = &s[..i];
s = &s[i..];
let unit = match u {
"ns" => 1i64,
"us" => 1000i64,
"µs" => 1000i64, // U+00B5 = micro symbol
"μs" => 1000i64, // U+03BC = Greek letter mu
"ms" => 1000000i64,
"s" => 1000000000i64,
"m" => 60000000000i64,
"h" => 3600000000000i64,
_ => {
return Err(format!("unknown unit {u} in duration {string}"));
}
};
if v > (1 << (63 - 1)) / unit {
// overflow
return Err(format!("invalid duration {string}"));
}
v *= unit;
if f > 0 {
// f64 is needed to be nanosecond accurate for fractions of hours.
// v >= 0 && (f*unit/scale) <= 3.6e+12 (ns/h, h is the largest unit)
v += (f as f64 * (unit as f64 / scale)) as i64;
if v < 0 {
// overflow
return Err(format!("invalid duration {string}"));
}
}
d += v;
if d < 0 {
// overflow
return Err(format!("invalid duration {string}"));
}
}
if neg {
d = -d;
}
Ok(d)
}
fn leading_int(s: &str) -> Result<(i64, &str), String> {
let mut x = 0;
let mut i = 0;
while i < s.len() {
let c = s.chars().nth(i).unwrap();
if !c.is_ascii_digit() {
break;
}
if x > (1 << (63 - 1)) / 10 {
return Err("overflow".into());
}
let d = i64::from(c.to_digit(10).unwrap());
x = x * 10 + d;
if x < 0 {
// overflow
return Err("overflow".into());
}
i += 1;
}
Ok((x, &s[i..]))
}
fn leading_fraction(s: &str) -> (i64, f64, &str) {
let mut i = 0;
let mut x = 0i64;
let mut scale = 1f64;
let mut overflow = false;
while i < s.len() {
let c = s.chars().nth(i).unwrap();
if !c.is_ascii_digit() {
break;
}
if overflow {
continue;
}
if x > (1 << (63 - 1)) / 10 {
// It's possible for overflow to give a positive number, so take care.
overflow = true;
continue;
}
let d = i64::from(c.to_digit(10).unwrap());
let y = x * 10 + d;
if y < 0 {
overflow = true;
continue;
}
x = y;
scale *= 10f64;
i += 1;
}
(x, scale, &s[i..])
}
#[cfg(test)]
mod tests {
use super::parse_duration;
#[test]
fn test_parse_duration() {
assert_eq!(parse_duration("8.731µs"), Ok(8731));
assert_eq!(parse_duration("50ns"), Ok(50));
assert_eq!(parse_duration("3ms"), Ok(3000000));
assert_eq!(parse_duration("2us"), Ok(2000));
assert_eq!(parse_duration("4.0s"), Ok(4000000000));
assert_eq!(parse_duration("1h45m"), Ok(6300000000000));
assert_eq!(
parse_duration("1"),
Err(String::from("missing unit in duration: 1")),
);
}
}
+555 -144
View File
@@ -1,159 +1,570 @@
use alloy_primitives::{keccak256, Address, B256}; use core::str;
use evm_interpreter::{ use std::{
interpreter::{EtableInterpreter, RunInterpreter}, collections::BTreeMap,
trap::CallCreateTrap, io::Write,
Context, Etable, ExitError, Log, Machine, RuntimeBackend, RuntimeBaseBackend, path::PathBuf,
RuntimeEnvironment, RuntimeState, TransactionContext, Valids, process::{Command, Stdio},
str::FromStr,
time::Duration,
}; };
use primitive_types::{H160, H256, U256};
static RUNTIME_ETABLE: Etable<RuntimeState, UnimplementedHandler, CallCreateTrap> = use alloy_genesis::{Genesis, GenesisAccount};
Etable::runtime(); use alloy_primitives::{hex::ToHexExt, Address, Bytes, B256, U256};
use alloy_serde::storage::deserialize_storage_map;
use serde::{Deserialize, Serialize};
use serde_json::{Deserializer, Value};
use tempfile::{NamedTempFile, TempPath};
#[derive(Default)] pub use self::go_duration::parse_go_duration;
pub struct UnimplementedHandler {
logs: Vec<Log>, mod go_duration;
const GENESIS_JSON: &str = include_str!("../genesis.json");
const EXECUTABLE_NAME: &str = "evm";
const EXECUTABLE_ARGS: [&str; 8] = [
"--log.format=json",
"run",
"--dump",
"--nomemory=false",
"--noreturndata=false",
"--json",
"--codefile",
"-",
];
const EXECUTABLE_ARGS_BENCH: [&str; 6] = [
"run",
"--bench",
"--nomemory=false",
"--noreturndata=false",
"--codefile",
"-",
];
const GAS_USED_MARKER: &str = "EVM gas used:";
const REVERT_MARKER: &str = " error: ";
/// The geth EVM state dump structure
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
pub struct StateDump {
pub root: Bytes,
pub accounts: BTreeMap<Address, Account>,
} }
impl RuntimeEnvironment for UnimplementedHandler { impl From<StateDump> for Genesis {
fn block_hash(&self, _number: U256) -> H256 { fn from(value: StateDump) -> Self {
unimplemented!() let mut genesis: Genesis = serde_json::from_str(GENESIS_JSON).unwrap();
} genesis.alloc = value
fn block_number(&self) -> U256 { .accounts
U256::from(123) .iter()
} .map(|(address, account)| (*address, account.clone().into()))
fn block_coinbase(&self) -> H160 { .collect();
unimplemented!() genesis
}
fn block_timestamp(&self) -> U256 {
U256::from(456)
}
fn block_difficulty(&self) -> U256 {
unimplemented!()
}
fn block_randomness(&self) -> Option<H256> {
unimplemented!()
}
fn block_gas_limit(&self) -> U256 {
unimplemented!()
}
fn block_base_fee_per_gas(&self) -> U256 {
unimplemented!()
}
fn chain_id(&self) -> U256 {
unimplemented!()
} }
} }
impl RuntimeBaseBackend for UnimplementedHandler { /// The geth EVM state dump account structure
fn balance(&self, _address: H160) -> U256 { #[derive(Clone, Debug, Serialize, Deserialize)]
unimplemented!() pub struct Account {
} pub balance: U256,
fn code_size(&self, _address: H160) -> U256 { pub nonce: u64,
unimplemented!() pub code: Option<Bytes>,
} #[serde(
fn code_hash(&self, _address: H160) -> H256 { default,
unimplemented!() skip_serializing_if = "Option::is_none",
} deserialize_with = "deserialize_storage_map"
fn code(&self, _address: H160) -> Vec<u8> { )]
unimplemented!() pub storage: Option<BTreeMap<B256, B256>>,
} pub key: Option<B256>,
fn storage(&self, _address: H160, _index: H256) -> H256 {
unimplemented!()
}
fn exists(&self, _address: H160) -> bool {
unimplemented!()
}
fn nonce(&self, _address: H160) -> U256 {
unimplemented!()
}
} }
impl RuntimeBackend for UnimplementedHandler { impl From<Account> for GenesisAccount {
fn original_storage(&self, _address: H160, _index: H256) -> H256 { fn from(value: Account) -> Self {
unimplemented!() GenesisAccount {
} balance: value.balance,
nonce: Some(value.nonce),
fn deleted(&self, _address: H160) -> bool { code: value.code,
unimplemented!() storage: value.storage,
} private_key: value.key,
fn is_cold(&self, _address: H160, _index: Option<H256>) -> bool {
unimplemented!()
}
fn mark_hot(&mut self, _address: H160, _index: Option<H256>) {
unimplemented!()
}
fn set_storage(&mut self, _address: H160, _index: H256, _value: H256) -> Result<(), ExitError> {
unimplemented!()
}
fn log(&mut self, log: Log) -> Result<(), ExitError> {
self.logs.push(log);
Ok(())
}
fn mark_delete(&mut self, _address: H160) {
unimplemented!()
}
fn reset_storage(&mut self, _address: H160) {
unimplemented!()
}
fn set_code(&mut self, _address: H160, _code: Vec<u8>) -> Result<(), ExitError> {
unimplemented!()
}
fn reset_balance(&mut self, _address: H160) {
unimplemented!()
}
fn deposit(&mut self, _address: H160, _value: U256) {
unimplemented!()
}
fn withdrawal(&mut self, _address: H160, _value: U256) -> Result<(), ExitError> {
unimplemented!()
}
fn inc_nonce(&mut self, _address: H160) -> Result<(), ExitError> {
unimplemented!()
}
}
#[derive(Clone)]
pub struct PreparedEvm {
pub valids: Valids,
pub vm: Machine<RuntimeState>,
}
pub fn prepare(code: Vec<u8>, data: Vec<u8>) -> PreparedEvm {
let address = Address::default().create2(B256::default(), keccak256([]).0);
let caller = Address::default().create2(B256::default(), keccak256([]).0);
let state = RuntimeState {
context: Context {
address: H160::from(address.0 .0),
caller: H160::from(caller.0 .0),
apparent_value: U256::default(),
},
transaction_context: TransactionContext {
gas_price: U256::default(),
origin: H160::default(),
} }
.into(),
retbuf: Vec::new(),
};
PreparedEvm {
valids: Valids::new(&code[..]),
vm: evm_interpreter::Machine::new(code.into(), data.to_vec().into(), 1024, 0xFFFF, state),
} }
} }
pub fn execute(pre: PreparedEvm) -> (Vec<u8>, Vec<Log>) { /// Contains the output from geth `emv` invocations
let mut vm = EtableInterpreter::new_valid(pre.vm, &RUNTIME_ETABLE, pre.valids); #[derive(Clone, Debug, Default, Serialize, Deserialize)]
let mut handler = UnimplementedHandler::default(); pub struct EvmOutput {
vm.run(&mut handler).exit().unwrap().unwrap(); pub output: Bytes,
(vm.retval.clone(), handler.logs) #[serde(rename = "gasUsed")]
pub gas_used: U256,
pub error: Option<String>,
}
impl EvmOutput {
/// Return if there was no error found.
///
/// Panics if the gas used is zero as this indicates nothing was run.
pub fn run_success(&self) -> bool {
assert_ne!(self.gas_used, U256::ZERO, "nothing was executed: {self:?}");
self.error.is_none()
}
}
/// Contains the full log from geth `emv` invocations
#[derive(Clone, Debug)]
pub struct EvmLog {
pub account_deployed: Option<Address>,
pub output: EvmOutput,
pub state_dump: StateDump,
pub stderr: String,
}
impl EvmLog {
pub const EXECUTION_TIME_MARKER: &'static str = "execution time:";
/// Parse the reported execution time from stderr (requires --bench)
pub fn execution_time(&self) -> Result<Duration, String> {
for line in self.stderr.lines() {
if let Some(value) = line.split("execution time:").nth(1) {
return parse_go_duration(value.trim());
}
}
Err(format!(
"execution time marker '{}' not found in raw EVM log",
Self::EXECUTION_TIME_MARKER
))
}
fn parse_gas_used_from_bench(&mut self) {
for line in self.stderr.lines() {
if let Some(gas_line) = line.split(GAS_USED_MARKER).nth(1) {
let gas_used = gas_line.trim().parse::<u64>().unwrap_or_else(|error| {
panic!("invalid output '{gas_line}' for gas used: {error}")
});
self.output.gas_used = U256::from(gas_used);
}
}
}
}
impl From<&str> for EvmLog {
fn from(value: &str) -> Self {
let mut output = None;
let mut state_dump = None;
for value in Deserializer::from_str(value).into_iter::<Value>() {
let Ok(value) = value else { continue };
if let Ok(value @ EvmOutput { .. }) = serde_json::from_value(value.clone()) {
output = Some(value);
continue;
}
if let Ok(value @ StateDump { .. }) = serde_json::from_value(value) {
state_dump = Some(value);
}
}
if let (Some(output), Some(state_dump)) = (output, state_dump) {
return Self {
account_deployed: None,
output,
state_dump,
stderr: value.into(),
};
}
EvmLog {
account_deployed: None,
output: EvmOutput {
error: value.find(REVERT_MARKER).map(|_| REVERT_MARKER.to_string()),
..Default::default()
},
state_dump: Default::default(),
stderr: Default::default(),
}
}
}
/// Builder for running contracts in geth `evm`
pub struct Evm {
genesis_json: Option<String>,
genesis_path: Option<PathBuf>,
code: Option<Vec<u8>>,
input: Option<Bytes>,
receiver: Option<String>,
sender: String,
value: Option<u128>,
gas: Option<u64>,
create: bool,
bench: bool,
}
impl Default for Evm {
fn default() -> Self {
Self {
genesis_json: Some(GENESIS_JSON.to_string()),
genesis_path: None,
code: None,
input: None,
receiver: None,
sender: Address::default().encode_hex(),
value: None,
gas: None,
create: false,
bench: false,
}
}
}
impl Evm {
/// Create a new EVM with the given `genesis`
pub fn from_genesis(genesis: Genesis) -> Self {
Self::default().genesis_json(genesis)
}
/// Run the `code`
pub fn code_blob(self, blob: Vec<u8>) -> Self {
Self {
code: Some(blob),
..self
}
}
/// Set the calldata
pub fn input(self, bytes: Bytes) -> Self {
Self {
input: (!bytes.is_empty()).then_some(bytes),
..self
}
}
/// Set the create flag
pub fn deploy(self, enable: bool) -> Self {
Self {
create: enable,
..self
}
}
/// Set the transferred value
pub fn value(self, value: u128) -> Self {
Self {
value: Some(value),
..self
}
}
/// Set the gas limit
pub fn gas(self, limit: u64) -> Self {
Self {
gas: Some(limit),
..self
}
}
/// Provide the prestate genesis configuration
pub fn genesis_json(self, genesis: Genesis) -> Self {
let genesis_json = serde_json::to_string(&genesis).expect("state dump should be valid");
// TODO: Investigate
let genesis_json = genesis_json.replace("\"0x0\"", "0").into();
Self {
genesis_json,
genesis_path: None,
..self
}
}
/// Provide a path to the genesis file to be used
pub fn genesis_path(self, path: PathBuf) -> Self {
Self {
genesis_path: Some(path),
genesis_json: None,
..self
}
}
/// Set the callee address
pub fn receiver(self, address: Address) -> Self {
Self {
receiver: Some(address.encode_hex()),
..self
}
}
/// Set the caller address
pub fn sender(self, address: Address) -> Self {
Self {
sender: address.encode_hex(),
..self
}
}
/// Run as a benchmark
pub fn bench(self, flag: bool) -> Self {
Self {
bench: flag,
..self
}
}
/// Calculate the address of the contract account this deploy call would create
pub fn expect_account_created(&self) -> Address {
assert!(self.create, "expected a deploy call");
let sender = Address::from_str(&self.sender).expect("sender address should be valid");
let genesis: Genesis = match (self.genesis_json.as_ref(), self.genesis_path.as_ref()) {
(Some(json), None) => serde_json::from_str(json).unwrap(),
(None, Some(path)) => {
serde_json::from_str(&std::fs::read_to_string(path).unwrap()).unwrap()
}
_ => panic!("provided a genesis json and a genesis json path"),
};
let nonce = genesis
.alloc
.get(&sender)
.map(|account| account.nonce.unwrap_or(0))
.unwrap_or(0);
sender.create(nonce)
}
/// Return the path to the genesis file;
/// writes the genesis file into a tmpdir if necessary.
///
/// `TempPath`` will delete on drop, so need to keep it around
fn write_genesis_file(&self, temp_path: &mut Option<TempPath>) -> String {
match (self.genesis_json.as_ref(), self.genesis_path.as_ref()) {
(Some(json), None) => {
let mut temp_file = NamedTempFile::new().unwrap();
temp_file.write_all(json.as_bytes()).unwrap();
let path = temp_file.into_temp_path();
*temp_path = Some(path);
temp_path.as_ref().unwrap().display().to_string()
}
(None, Some(path)) => path.display().to_string(),
_ => panic!("provided a genesis json and a genesis json path"),
}
}
/// Run the call in a geth `evm` subprocess.
///
/// Definitively not a hairy plumbing function.
pub fn run(self) -> EvmLog {
let mut temp_path = None;
let genesis_json_path = &self.write_genesis_file(&mut temp_path);
// Static args
let mut command = Command::new(PathBuf::from(EXECUTABLE_NAME));
command
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::piped());
if self.bench {
command.args(EXECUTABLE_ARGS_BENCH);
} else {
command.args(EXECUTABLE_ARGS);
};
// Dynamic args
command.args(["--prestate", genesis_json_path]);
command.args(["--sender", &self.sender]);
if let Some(input) = &self.input {
command.args(["--input", hex::encode(input).as_str()]);
}
let account_deployed = if self.create {
command.arg("--create");
self.expect_account_created().into()
} else {
None
};
match (&self.code, &self.receiver) {
(Some(_), None) => {}
(None, Some(address)) => {
command.args(["--receiver", address]);
}
(Some(_), Some(_)) => panic!("code and receiver specified"),
_ => panic!("no code file or receiver specified"),
}
if let Some(gas) = self.gas {
command.args(["--gas", &format!("{gas}")]);
}
if let Some(value) = self.value {
command.args(["--value", &format!("{value}")]);
}
// Run the evm subprocess and assert success return value
let process = command.spawn().unwrap_or_else(|error| {
panic!("{EXECUTABLE_NAME} subprocess spawning error: {error:?}")
});
let buf = vec![];
process
.stdin
.as_ref()
.unwrap_or_else(|| panic!("{EXECUTABLE_NAME} stdin getting error"))
.write_all(self.code.as_ref().unwrap_or(&buf))
.unwrap_or_else(|err| panic!("{EXECUTABLE_NAME} stdin writing error: {err:?}"));
let output = process
.wait_with_output()
.unwrap_or_else(|err| panic!("{EXECUTABLE_NAME} subprocess output error: {err}"));
assert!(
output.status.success(),
"{EXECUTABLE_NAME} command failed: {output:?}",
);
drop(temp_path);
let stdout = str::from_utf8(output.stdout.as_slice())
.unwrap_or_else(|err| panic!("{EXECUTABLE_NAME} stdout failed to parse: {err}"));
let stderr = str::from_utf8(output.stderr.as_slice())
.unwrap_or_else(|err| panic!("{EXECUTABLE_NAME} stderr failed to parse: {err}"));
let mut log: EvmLog = stdout.into();
log.stderr = stderr.into();
if self.bench {
log.parse_gas_used_from_bench();
}
// Set the deployed account
log.account_deployed = account_deployed;
log
}
}
#[cfg(test)]
mod tests {
use std::{str::FromStr, time::Duration};
use alloy_genesis::Genesis;
use alloy_primitives::{Bytes, B256, U256};
use crate::{Evm, EvmLog, EvmOutput, StateDump};
const OUTPUT_JSON_OK: &str = r#"{"output":"0000000000000000000000000000000000000000000000000000000000000000","gasUsed":"0x11d"}"#;
const OUTPUT_JSON_REVERTED: &str =
r#"{"output":"","gasUsed":"0x2d","error":"execution reverted"}"#;
const STATE_DUMP: &str = r#"
{
"root": "eb5d51177cb9049b848ea92f87f9a3f00abfb683d0866c2eddecc5692ad27f86",
"accounts": {
"0x1f2a98889594024BFfdA3311CbE69728d392C06D": {
"balance": "0",
"nonce": 1,
"root": "0x63cfcda8d81a8b1840b1b9722c37f929a4037e53ad1ce6abdef31c0c8bac1f61",
"codeHash": "0xa6e0062c5ba829446695f179b97702a75f7d354e33445d2e928ed00e1a39e88f",
"code": "0x608060405260043610610028575f3560e01c80633fa4f2451461002c578063b144adfb1461004a575b5f80fd5b610034610086565b60405161004191906100c5565b60405180910390f35b348015610055575f80fd5b50610070600480360381019061006b919061013c565b61008d565b60405161007d91906100c5565b60405180910390f35b5f34905090565b5f8173ffffffffffffffffffffffffffffffffffffffff16319050919050565b5f819050919050565b6100bf816100ad565b82525050565b5f6020820190506100d85f8301846100b6565b92915050565b5f80fd5b5f73ffffffffffffffffffffffffffffffffffffffff82169050919050565b5f61010b826100e2565b9050919050565b61011b81610101565b8114610125575f80fd5b50565b5f8135905061013681610112565b92915050565b5f60208284031215610151576101506100de565b5b5f61015e84828501610128565b9150509291505056fea2646970667358221220a2109c2f05a629fff4640e9f0cf12a698bbea9b0858a4029901e88bf5d1c926964736f6c63430008190033",
"storage": {
"0x0000000000000000000000000000000000000000000000000000000000000000": "02"
},
"address": "0x1f2a98889594024bffda3311cbe69728d392c06d",
"key": "0xcbeeb4463624bc2f332dcfe2b479eddb1c380ec862ee63d9f31b31b854fb7c61"
}
}
}"#;
const EVM_BIN_FIXTURE: &str = "6080604052348015600e575f80fd5b506040516101403803806101408339818101604052810190602e9190607f565b805f806101000a81548160ff0219169083151502179055505060a5565b5f80fd5b5f8115159050919050565b606181604f565b8114606a575f80fd5b50565b5f81519050607981605a565b92915050565b5f602082840312156091576090604b565b5b5f609c84828501606d565b91505092915050565b608f806100b15f395ff3fe6080604052348015600e575f80fd5b50600436106026575f3560e01c8063cde4efa914602a575b5f80fd5b60306032565b005b5f8054906101000a900460ff16155f806101000a81548160ff02191690831515021790555056fea264697066735822122046c92dd2fd612b1ed93d184dad4c49f61c44690722c4a6c7c746ebeb0aadeb4a64736f6c63430008190033";
const EVM_BIN_RUNTIME_FIXTURE: &str = "6080604052348015600e575f80fd5b50600436106026575f3560e01c8063cde4efa914602a575b5f80fd5b60306032565b005b5f8054906101000a900460ff16155f806101000a81548160ff02191690831515021790555056fea264697066735822122046c92dd2fd612b1ed93d184dad4c49f61c44690722c4a6c7c746ebeb0aadeb4a64736f6c63430008190033";
const EVM_BIN_FIXTURE_INPUT: &str =
"0000000000000000000000000000000000000000000000000000000000000001";
const EVM_BIN_RUNTIME_FIXTURE_INPUT: &str = "cde4efa9";
const STDERR_BENCH_OK: &str = r#"EVM gas used: 560071
execution time: 1.460881ms
allocations: 29
allocated bytes: 2558
"#;
const STDERR_BENCH_REVERT: &str = r#"EVM gas used: 69
execution time: 10.11µs
allocations: 43
allocated bytes: 3711"#;
const STDOUT_BENCH_REVERT: &str = r#" error: execution reverted"#;
#[test]
fn parse_evm_output_ok() {
serde_json::from_str::<EvmOutput>(OUTPUT_JSON_OK).unwrap();
}
#[test]
fn parse_evm_output_revert() {
serde_json::from_str::<EvmOutput>(OUTPUT_JSON_REVERTED).unwrap();
}
#[test]
fn parse_evm_output_bench_ok() {
let mut log = EvmLog::from("");
log.stderr = STDERR_BENCH_OK.into();
log.parse_gas_used_from_bench();
assert!(log.output.run_success());
assert_eq!(log.execution_time().unwrap(), Duration::from_nanos(1460881));
}
#[test]
fn parse_evm_output_bench_revert() {
let mut log = EvmLog::from(STDOUT_BENCH_REVERT);
log.stderr = STDERR_BENCH_REVERT.into();
log.parse_gas_used_from_bench();
assert!(!log.output.run_success());
}
#[test]
fn parse_state_dump() {
serde_json::from_str::<StateDump>(STATE_DUMP).unwrap();
}
#[test]
fn evm_log_from_str() {
let log = format!("{OUTPUT_JSON_OK}\n{STATE_DUMP}");
let _ = EvmLog::from(log.as_str());
}
#[test]
fn generate_genesis() {
let log = format!("{OUTPUT_JSON_OK}\n{STATE_DUMP}");
let log = EvmLog::from(log.as_str());
let mut genesis: Genesis = log.state_dump.into();
let storage = genesis
.alloc
.pop_first()
.expect("should have one account in genesis")
.1
.storage
.expect("genesis account should have storage");
let storage_value = storage
.get(&B256::ZERO)
.expect("genesis account should have key 0 occupied");
assert_eq!(*storage_value, B256::from(U256::from(2)));
}
#[test]
fn flipper() {
let log_runtime = Evm::default()
.code_blob(EVM_BIN_RUNTIME_FIXTURE.as_bytes().to_vec())
.input(Bytes::from_str(EVM_BIN_RUNTIME_FIXTURE_INPUT).unwrap())
.run();
assert!(log_runtime.output.run_success());
}
#[test]
fn prestate() {
let log_deploy = Evm::default()
.code_blob(EVM_BIN_FIXTURE.as_bytes().to_vec())
.input(Bytes::from_str(EVM_BIN_FIXTURE_INPUT).unwrap())
.deploy(true)
.run();
assert!(log_deploy.output.run_success());
let address = log_deploy.account_deployed.unwrap();
let genesis: Genesis = log_deploy.state_dump.into();
let log_runtime = Evm::default()
.genesis_json(genesis)
.receiver(address)
.input(Bytes::from_str(EVM_BIN_RUNTIME_FIXTURE_INPUT).unwrap())
.run();
assert!(log_runtime.output.run_success(), "{:?}", log_runtime.output);
}
#[test]
#[ignore] // https://github.com/ethereum/go-ethereum/issues/30778
fn bench_flipper() {
let log_runtime = Evm::default()
.code_blob(EVM_BIN_RUNTIME_FIXTURE.as_bytes().to_vec())
.input(Bytes::from_str(EVM_BIN_RUNTIME_FIXTURE_INPUT).unwrap())
.bench(true)
.run();
assert!(log_runtime.output.run_success());
assert!(log_runtime.execution_time().unwrap() > Duration::from_nanos(0));
}
} }
+2
View File
@@ -14,11 +14,13 @@ alloy-sol-types = { workspace = true }
hex = { workspace = true } hex = { workspace = true }
env_logger = { workspace = true } env_logger = { workspace = true }
log = { workspace = true } log = { workspace = true }
serde_json = { workspace = true }
revive-solidity = { workspace = true } revive-solidity = { workspace = true }
revive-differential = { workspace = true } revive-differential = { workspace = true }
revive-llvm-context = { workspace = true } revive-llvm-context = { workspace = true }
revive-common = { workspace = true } revive-common = { workspace = true }
revive-runner = { workspace = true }
[dev-dependencies] [dev-dependencies]
sha1 = { workspace = true } sha1 = { workspace = true }
+8 -8
View File
@@ -1,10 +1,10 @@
{ {
"Baseline": 934, "Baseline": 1110,
"Computation": 4360, "Computation": 2389,
"DivisionArithmetics": 39824, "DivisionArithmetics": 14822,
"ERC20": 46471, "ERC20": 23973,
"Events": 1749, "Events": 1605,
"FibonacciIterative": 2973, "FibonacciIterative": 2023,
"Flipper": 3563, "Flipper": 1989,
"SHA1": 32543 "SHA1": 17026
} }
+31
View File
@@ -0,0 +1,31 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8;
/* runner.json
{
"differential": false,
"actions": [
{
"Instantiate": {
"code": {
"Solidity": {
"contract": "BaseFee"
}
}
}
},
{
"VerifyCall": {
"success": true
}
}
]
}
*/
contract BaseFee {
constructor() payable {
assert(block.basefee == 0);
}
}
+15
View File
@@ -1,5 +1,20 @@
// SPDX-License-Identifier: MIT // SPDX-License-Identifier: MIT
/* runner.json
{
"actions": [
{
"Instantiate": {
"origin": "Alice",
"value": 0
}
}
]
}
*/
pragma solidity ^0.8; pragma solidity ^0.8;
contract Baseline { contract Baseline {
+25
View File
@@ -2,6 +2,31 @@
pragma solidity ^0.8; pragma solidity ^0.8;
/* runner.json
{
"differential": true,
"actions": [
{
"Instantiate": {
"code": {
"Solidity": {
"contract": "Bitwise"
}
}
}
},
{
"Call": {
"dest": {
"Instantiated": 0
},
"data": "3fa4f245"
}
}
]
}
*/
contract Bitwise { contract Bitwise {
function opByte(uint i, uint x) public payable returns (uint ret) { function opByte(uint i, uint x) public payable returns (uint ret) {
assembly { assembly {
+38 -1
View File
@@ -2,12 +2,49 @@
pragma solidity ^0.8; pragma solidity ^0.8;
/* runner.json
{
"differential": true,
"actions": [
{
"Instantiate": {
"code": {
"Solidity": {
"contract": "Block"
}
}
}
},
{
"Call": {
"dest": {
"Instantiated": 0
},
"data": "8381f58a"
}
},
{
"Call": {
"dest": {
"Instantiated": 0
},
"data": "b80777ea"
}
}
]
}
*/
contract Block { contract Block {
function timestamp() public view returns (uint ret) { function timestamp() public view returns (uint ret) {
ret = block.timestamp; ret = block.timestamp;
} }
function number() public view returns (uint ret) { function number() public view returns (uint ret) {
ret = block.number; if (block.number == 0) {
ret = 1;
} else {
ret = block.number;
}
} }
} }
@@ -0,0 +1,33 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8;
/* runner.json
{
"differential": false,
"actions": [
{
"Instantiate": {
"code": {
"Solidity": {
"contract": "BlockHash"
}
},
"data": "4545454545454545454545454545454545454545454545454545454545454545"
}
}
]
}
*/
contract BlockHash {
constructor(bytes32 expected) payable {
assert(blockhash(0) == expected);
assert(blockhash(1) == 0);
assert(
blockhash(
0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
) == 0
);
}
}
+47 -9
View File
@@ -2,19 +2,57 @@
pragma solidity ^0.8; pragma solidity ^0.8;
contract Call { /* runner.json
function value_transfer(address payable destination) public payable { {
destination.transfer(msg.value); "differential": true,
} "actions": [
{
"Upload": {
"code": {
"Solidity": {
"contract": "Callee"
}
}
}
},
{
"Instantiate": {
"code": {
"Solidity": {
"contract": "Caller"
}
},
"value": 123
}
},
{
"Call": {
"dest": {
"Instantiated": 0
},
"data": "5a6535fc00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000004cafebabe00000000000000000000000000000000000000000000000000000000"
}
}
]
}
*/
contract Callee {
function echo(bytes memory payload) public pure returns (bytes memory) { function echo(bytes memory payload) public pure returns (bytes memory) {
return payload; return payload;
} }
function call( receive() external payable {}
address callee, }
bytes memory payload
) public pure returns (bytes memory) { contract Caller {
return Call(callee).echo(payload); constructor() payable {
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);
} }
} }
@@ -2,6 +2,44 @@
pragma solidity ^0.8; pragma solidity ^0.8;
/* runner.json
{
"actions": [
{
"Instantiate": {}
},
{
"Call": {
"dest": {
"Instantiated": 0
},
"data": "0f760610000000000000000000000000000000000000000000000000000000000000000d"
}
},
{
"VerifyCall": {
"success": true,
"output": "000000000000000000000000000000000000000000000000000000000000005b"
}
},
{
"Call": {
"dest": {
"Instantiated": 0
},
"data": "00261b660000000000000000000000000000000000000000000000000000000000000005"
}
},
{
"VerifyCall": {
"success": true,
"output": "00000000000000000000000000000000000000000000000000000000000003b1"
}
}
]
}
*/
contract Computation { contract Computation {
function triangle_number(int64 n) public pure returns (int64 sum) { function triangle_number(int64 n) public pure returns (int64 sum) {
unchecked { unchecked {
+49
View File
@@ -2,6 +2,47 @@
pragma solidity ^0.8; pragma solidity ^0.8;
/* runner.json
{
"differential": true,
"actions": [
{
"Instantiate": {
"code": {
"Solidity": {
"contract": "Context"
}
}
}
},
{
"Call": {
"dest": {
"Instantiated": 0
},
"data": "846a1ee1"
}
},
{
"Call": {
"dest": {
"Instantiated": 0
},
"data": "fc9c8d39"
}
},
{
"Call": {
"dest": {
"Instantiated": 0
},
"data": "3af973b1"
}
}
]
}
*/
contract Context { contract Context {
function address_this() public view returns (address ret) { function address_this() public view returns (address ret) {
ret = address(this); ret = address(this);
@@ -10,4 +51,12 @@ 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;
}
} }
+57 -6
View File
@@ -1,13 +1,64 @@
// SPDX-License-Identifier: MIT // SPDX-License-Identifier: MIT
pragma solidity ^0.8.24; pragma solidity ^0.8;
/* 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 {
address creator; constructor() payable {}
constructor() payable {
creator = msg.sender;
}
} }
contract CreateB { contract CreateB {
+35
View File
@@ -0,0 +1,35 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
/* runner.json
{
"differential": true,
"actions": [
{
"Instantiate": {
"code": {
"Solidity": {
"contract": "TestSha3"
}
}
}
},
{
"Call": {
"dest": {
"Instantiated": 0
},
"data": "f9fbd5540000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000c68656c6c6f20776f726c64210000000000000000000000000000000000000000"
}
}
]
}
*/
contract TestSha3 {
function test(string memory _pre) external payable returns (bytes32) {
bytes32 hash = keccak256(bytes(_pre));
return bytes32(uint(hash) + 1);
}
}
+84
View File
@@ -0,0 +1,84 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8;
/* runner.json
{
"differential": true,
"actions": [
{
"Upload": {
"code": {
"Solidity": {
"contract": "Logic"
}
}
}
},
{
"Instantiate": {
"code": {
"Solidity": {
"contract": "Tester"
}
}
}
},
{
"Call": {
"dest": {
"Instantiated": 0
},
"value": 123,
"data": "6466414b0000000000000000000000000000000000000000000000000000000000000020"
}
}
]
}
*/
contract Logic {
// NOTE: storage layout must be the same as contract Tester
uint256 public num;
address public sender;
uint256 public value;
uint public immutable multiplier = 4;
event DidSetVars();
function setVars(uint256 _num) public payable returns (uint256) {
num = _num * multiplier;
sender = msg.sender;
value = msg.value;
emit DidSetVars();
return _num;
}
}
contract Tester {
uint256 public num;
address public sender;
uint256 public value;
uint public immutable multiplier = 2;
function setVars(uint256 _num) public payable returns (bool, bytes memory) {
Logic impl = new Logic();
// Tester's storage is set, Logic is not modified.
(bool success, bytes memory data) = address(impl).delegatecall(
abi.encodeWithSignature("setVars(uint256)", _num)
);
assert(success);
assert(impl.num() == 0);
assert(impl.sender() == address(0));
assert(impl.value() == 0);
assert(num == _num * 4);
assert(sender == msg.sender);
assert(value == msg.value);
return (success, data);
}
}
+49
View File
@@ -2,6 +2,32 @@
pragma solidity ^0.8; pragma solidity ^0.8;
/* runner.json
{
"differential": true,
"actions": [
{
"Upload": {
"code": {
"Solidity": {
"contract": "ERC20"
}
}
}
},
{
"Instantiate": {
"code": {
"Solidity": {
"contract": "ERC20Tester"
}
}
}
}
]
}
*/
// https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v3.0.0/contracts/token/ERC20/IERC20.sol // https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v3.0.0/contracts/token/ERC20/IERC20.sol
interface IERC20 { interface IERC20 {
function totalSupply() external view returns (uint); function totalSupply() external view returns (uint);
@@ -72,3 +98,26 @@ 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);
}
}
+33
View File
@@ -2,6 +2,39 @@
pragma solidity ^0.8; pragma solidity ^0.8;
/* runner.json
{
"differential": true,
"actions": [
{
"Instantiate": {
"code": {
"Solidity": {
"contract": "Events"
}
}
}
},
{
"Call": {
"dest": {
"Instantiated": 0
},
"data": "4d43bec90000000000000000000000000000000000000000000000000000000000000000"
}
},
{
"Call": {
"dest": {
"Instantiated": 0
},
"data": "4d43bec9000000000000000000000000000000000000000000000000000000000000007b"
}
}
]
}
*/
contract Events { contract Events {
event A() anonymous; event A() anonymous;
event E(uint, uint indexed, uint indexed, uint indexed); event E(uint, uint indexed, uint indexed, uint indexed);
+12
View File
@@ -14,4 +14,16 @@ 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())
}
}
} }
+24 -1
View File
@@ -2,7 +2,29 @@
pragma solidity ^0.8; pragma solidity ^0.8;
// https://medium.com/coinmonks/fibonacci-in-solidity-8477d907e22a /* runner.json
{
"actions": [
{
"Instantiate": {}
},
{
"Call": {
"dest": {
"Instantiated": 0
},
"data": "6b83dd2e0000000000000000000000000000000000000000000000000000000000000006"
}
},
{
"VerifyCall": {
"success": true,
"output": "0000000000000000000000000000000000000000000000000000000000000008"
}
}
]
}
*/
contract FibonacciRecursive { contract FibonacciRecursive {
function f(uint n) internal pure returns (uint) { function f(uint n) internal pure returns (uint) {
@@ -36,6 +58,7 @@ contract FibonacciIterative {
} }
} }
// https://medium.com/coinmonks/fibonacci-in-solidity-8477d907e22a
contract FibonacciBinet { contract FibonacciBinet {
function fib3(uint n) external pure returns (uint a) { function fib3(uint n) external pure returns (uint a) {
if (n == 0) { if (n == 0) {
+32
View File
@@ -0,0 +1,32 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8;
/* runner.json
{
"differential": false,
"actions": [
{
"Instantiate": {
"code": {
"Solidity": {
"contract": "GasLeft"
}
}
}
},
{
"VerifyCall": {
"success": true
}
}
]
}
*/
contract GasLeft {
constructor() payable {
assert(gasleft() > gasleft());
assert(gasleft() > 0 && gasleft() < 0xffffffffffffffff);
}
}
+31
View File
@@ -0,0 +1,31 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8;
/* runner.json
{
"differential": false,
"actions": [
{
"Instantiate": {
"code": {
"Solidity": {
"contract": "GasLimit"
}
}
}
},
{
"VerifyCall": {
"success": true
}
}
]
}
*/
contract GasLimit {
constructor() payable {
assert(block.gaslimit == 2000000000000);
}
}
+31
View File
@@ -0,0 +1,31 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8;
/* runner.json
{
"differential": false,
"actions": [
{
"Instantiate": {
"code": {
"Solidity": {
"contract": "GasPrice"
}
}
}
},
{
"VerifyCall": {
"success": true
}
}
]
}
*/
contract GasPrice {
constructor() payable {
assert(tx.gasprice == 1);
}
}
@@ -0,0 +1,63 @@
// 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);
}
}
+25
View File
@@ -2,6 +2,31 @@
pragma solidity ^0.8; pragma solidity ^0.8;
/* runner.json
{
"differential": true,
"actions": [
{
"Instantiate": {
"code": {
"Solidity": {
"contract": "MCopy"
}
}
}
},
{
"Call": {
"dest": {
"Instantiated": 0
},
"data": "0ee188b0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000030102030000000000000000000000000000000000000000000000000000000000"
}
}
]
}
*/
contract MCopy { contract MCopy {
function memcpy(bytes memory payload) public pure returns (bytes memory) { function memcpy(bytes memory payload) public pure returns (bytes memory) {
return payload; return payload;
+34
View File
@@ -2,6 +2,40 @@
pragma solidity ^0.8; pragma solidity ^0.8;
/* runner.json
{
"differential": true,
"actions": [
{
"Instantiate": {
"code": {
"Solidity": {
"contract": "MSize",
"solc_optimizer": false
}
}
}
},
{
"Call": {
"dest": {
"Instantiated": 0
},
"data": "f016832c"
}
},
{
"Call": {
"dest": {
"Instantiated": 0
},
"data": "f4a63aa5"
}
}
]
}
*/
contract MSize { contract MSize {
uint[] public data; uint[] public data;
+101
View File
@@ -0,0 +1,101 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8;
/* runner.json
{
"differential": true,
"actions": [
{
"Instantiate": {
"code": {
"Solidity": {
"contract": "MStore8"
}
}
}
},
{
"Call": {
"dest": {
"Instantiated": 0
},
"data": "b09ad4210000000000000000000000000000000000000000000000000000000000000000"
}
},
{
"Call": {
"dest": {
"Instantiated": 0
},
"data": "b09ad4210000000000000000000000000000000000000000000000000000000000000001"
}
},
{
"Call": {
"dest": {
"Instantiated": 0
},
"data": "b09ad4210000000000000000000000000000000000000000000000000000000000000002"
}
},
{
"Call": {
"dest": {
"Instantiated": 0
},
"data": "b09ad42100000000000000000000000000000000000000000000000000000000000000ff"
}
},
{
"Call": {
"dest": {
"Instantiated": 0
},
"data": "b09ad4210000000000000000000000000000000000000000000000000000000000000100"
}
},
{
"Call": {
"dest": {
"Instantiated": 0
},
"data": "b09ad4210000000000000000000000000000000000000000000000000000000000000101"
}
},
{
"Call": {
"dest": {
"Instantiated": 0
},
"data": "b09ad4210000000000000000000000000000000000000000000000000000000000000102"
}
},
{
"Call": {
"dest": {
"Instantiated": 0
},
"data": "b09ad42100000000000000000000000000000000000000000000000000000000075bcd15"
}
},
{
"Call": {
"dest": {
"Instantiated": 0
},
"data": "b09ad421ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
}
}
]
}
*/
contract MStore8 {
function mStore8(uint value) public pure returns (uint256 word) {
assembly {
mstore8(0x80, value)
word := mload(0x80)
}
}
}
@@ -0,0 +1,53 @@
// 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)
}
}
}
+25
View File
@@ -1,6 +1,31 @@
// SPDX-License-Identifier: BSD-2-Clause // SPDX-License-Identifier: BSD-2-Clause
pragma solidity ^0.8.4; pragma solidity ^0.8.4;
/* runner.json
{
"differential": true,
"actions": [
{
"Instantiate": {
"code": {
"Solidity": {
"contract": "SHA1"
}
}
}
},
{
"Call": {
"dest": {
"Instantiated": 0
},
"data": "1605782b00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000200ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
}
}
]
}
*/
contract SHA1 { contract SHA1 {
function sha1(bytes memory data) public pure returns (bytes20 ret) { function sha1(bytes memory data) public pure returns (bytes20 ret) {
assembly { assembly {
+42 -1
View File
@@ -2,13 +2,54 @@
pragma solidity ^0.8; pragma solidity ^0.8;
/* runner.json
{
"differential": true,
"actions": [
{
"Instantiate": {
"code": {
"Solidity": {
"contract": "Storage"
}
}
}
},
{
"Call": {
"dest": {
"Instantiated": 0
},
"data": "fabc9efaffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
}
},
{
"Call": {
"dest": {
"Instantiated": 0
},
"data": "558b9f9bffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
}
}
]
}
*/
contract Storage { contract Storage {
function transient(uint value) public returns (uint ret) { function transient(uint value) public returns (uint ret) {
assembly { assembly {
let slot := 123 let slot := 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff00
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)
}
}
} }
@@ -0,0 +1,54 @@
// 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
@@ -0,0 +1,54 @@
// 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);
}
}
+58 -5
View File
@@ -1,13 +1,66 @@
// SPDX-License-Identifier: MIT // SPDX-License-Identifier: MIT
pragma solidity ^0.8; pragma solidity ^0.8;
/* runner.json
{
"differential": true,
"actions": [
{
"Upload": {
"code": {
"Solidity": {
"contract": "ValueTester"
}
}
}
},
{
"Instantiate": {
"value": 1024,
"code": {
"Solidity": {
"contract": "Value"
}
}
}
},
{
"Call": {
"dest": {
"Instantiated": 0
},
"value": 123,
"data": "3fa4f245"
}
}
]
}
*/
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;
}
} }
+38
View File
@@ -1,6 +1,44 @@
// SPDX-License-Identifier: MIT // SPDX-License-Identifier: MIT
pragma solidity ^0.8; pragma solidity ^0.8;
/* runner.json
{
"actions": [
{
"Instantiate": {
"data": "0000000000000000000000000000000000000000000000000000000000000001"
}
},
{
"VerifyStorage": {
"contract": {
"Instantiated": 0
},
"key": "0000000000000000000000000000000000000000000000000000000000000000",
"expected": "0100000000000000000000000000000000000000000000000000000000000000"
}
},
{
"Call": {
"dest": {
"Instantiated": 0
},
"data": "cde4efa9"
}
},
{
"VerifyStorage": {
"contract": {
"Instantiated": 0
},
"key": "0000000000000000000000000000000000000000000000000000000000000000",
"expected": "0000000000000000000000000000000000000000000000000000000000000000"
}
}
]
}
*/
contract Flipper { contract Flipper {
bool coin; bool coin;
-12
View File
@@ -1,12 +0,0 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8;
contract MStore8 {
function mStore8(uint value) public pure returns (uint256 word) {
assembly {
mstore8(0x80, value)
word := mload(0x80)
}
}
}
+15 -27
View File
@@ -1,10 +1,8 @@
use alloy_primitives::{Address, I256, U256}; use alloy_primitives::{Address, Bytes, I256, U256};
use alloy_sol_types::{sol, SolCall, SolConstructor}; use alloy_sol_types::{sol, SolCall, SolConstructor};
use revive_solidity::test_utils::*; use revive_solidity::test_utils::*;
use crate::mock_runtime::{CallOutput, State};
#[derive(Clone)] #[derive(Clone)]
pub struct Contract { pub struct Contract {
pub name: &'static str, pub name: &'static str,
@@ -93,7 +91,7 @@ sol!(
function sha1(bytes memory data) public pure returns (bytes20 ret); function sha1(bytes memory data) public pure returns (bytes20 ret);
} }
); );
case!("SHA1.sol", SHA1, sha1Call, sha1, pre: Vec<u8>); case!("SHA1.sol", SHA1, sha1Call, sha1, pre: Bytes);
sol!( sol!(
contract ERC20 { contract ERC20 {
@@ -163,7 +161,7 @@ sol!(
function mStore8(uint value) public pure returns (uint256 word); function mStore8(uint value) public pure returns (uint256 word);
} }
); );
case!("mStore8.sol", MStore8, mStore8Call, mstore8, value: U256); case!("MStore8.sol", MStore8, mStore8Call, mstore8, value: U256);
sol!( sol!(
contract Events { contract Events {
@@ -180,17 +178,23 @@ 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 {
function memcpy(bytes memory payload) public pure returns (bytes memory); function memcpy(bytes memory payload) public pure returns (bytes memory);
} }
); );
case!("MCopy.sol", MCopy, memcpyCall, memcpy, payload: Vec<u8>); case!("MCopy.sol", MCopy, memcpyCall, memcpy, payload: Bytes);
sol!( sol!(
contract Call { contract Call {
@@ -205,15 +209,17 @@ sol!(
} }
); );
case!("Call.sol", Call, value_transferCall, call_value_transfer, destination: Address); case!("Call.sol", Call, value_transferCall, call_value_transfer, destination: Address);
case!("Call.sol", Call, callCall, call_call, destination: Address, payload: Vec<u8>); case!("Call.sol", Call, callCall, call_call, destination: Address, payload: Bytes);
case!("Call.sol", "Call", vec![], call_constructor); 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 {
@@ -230,24 +236,6 @@ sol!(
case!("Storage.sol", Storage, transientCall, storage_transient, value: U256); case!("Storage.sol", Storage, transientCall, storage_transient, value: U256);
impl Contract { impl Contract {
/// Execute the contract.
///
/// Useful helper if the contract state can be ignored,
/// as it spares the deploy transaciton.
///
/// - Inserts an account with given `code` into a new state.
/// - Callee and caller account will be `Transaction::default_address()`.
/// - Sets the calldata.
/// - Doesn't execute the constructor or deploy code.
/// - Calls the "call" export on a default backend config.
pub fn execute(&self) -> (State, CallOutput) {
State::default()
.transaction()
.with_default_account(&self.pvm_runtime)
.calldata(self.calldata.clone())
.call()
}
fn build(calldata: Vec<u8>, name: &'static str, code: &str) -> Self { fn build(calldata: Vec<u8>, name: &'static str, code: &str) -> Self {
Self { Self {
name, name,
@@ -260,7 +248,7 @@ impl Contract {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use alloy_primitives::U256; use alloy_primitives::{Bytes, U256};
use rayon::iter::{IntoParallelIterator, ParallelIterator}; use rayon::iter::{IntoParallelIterator, ParallelIterator};
use serde::{de::Deserialize, Serialize}; use serde::{de::Deserialize, Serialize};
use std::{collections::BTreeMap, fs::File}; use std::{collections::BTreeMap, fs::File};
@@ -305,7 +293,7 @@ mod tests {
(|| Contract::odd_product(0)) as fn() -> Contract, (|| Contract::odd_product(0)) as fn() -> Contract,
(|| Contract::fib_iterative(U256::ZERO)) as fn() -> Contract, (|| Contract::fib_iterative(U256::ZERO)) as fn() -> Contract,
Contract::erc20 as fn() -> Contract, Contract::erc20 as fn() -> Contract,
(|| Contract::sha1(Vec::new())) as fn() -> Contract, (|| Contract::sha1(Bytes::new())) as fn() -> Contract,
(|| Contract::division_arithmetics_div(U256::ZERO, U256::ZERO)) as fn() -> Contract, (|| Contract::division_arithmetics_div(U256::ZERO, U256::ZERO)) as fn() -> Contract,
(|| Contract::event(U256::ZERO)) as fn() -> Contract, (|| Contract::event(U256::ZERO)) as fn() -> Contract,
] ]
-38
View File
@@ -1,42 +1,4 @@
use alloy_primitives::{Address, U256};
use cases::Contract;
use mock_runtime::{CallOutput, State};
use crate::mock_runtime::{Event, ReturnFlags};
pub mod cases; pub mod cases;
pub mod mock_runtime;
#[cfg(test)] #[cfg(test)]
mod tests; mod tests;
pub fn assert_success(contract: &Contract, differential: bool) -> (State, CallOutput) {
let (state, output) = contract.execute();
assert_eq!(output.flags, ReturnFlags::Success);
if differential {
let evm =
revive_differential::prepare(contract.evm_runtime.clone(), contract.calldata.clone());
let (evm_output, evm_log) = revive_differential::execute(evm);
assert_eq!(output.data.clone(), evm_output);
assert_eq!(output.events.len(), evm_log.len());
assert_eq!(
output.events,
evm_log
.iter()
.map(|log| Event {
address: Address::from_slice(log.address.as_bytes()),
data: log.data.clone(),
topics: log
.topics
.iter()
.map(|topic| U256::from_be_bytes(topic.0))
.collect(),
})
.collect::<Vec<_>>()
);
}
(state, output)
}
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
-3
View File
@@ -7,9 +7,6 @@ repository.workspace = true
authors.workspace = true authors.workspace = true
description = "revive compiler linker utils" description = "revive compiler linker utils"
[features]
riscv-64 = []
[dependencies] [dependencies]
inkwell = { workspace = true } inkwell = { workspace = true }
tempfile = { workspace = true } tempfile = { workspace = true }
+5 -15
View File
@@ -8,14 +8,7 @@ SECTIONS {
.text : { KEEP(*(.text.polkavm_export)) *(.text .text.*) } .text : { KEEP(*(.text.polkavm_export)) *(.text .text.*) }
}"#; }"#;
#[cfg(not(feature = "riscv-64"))]
const BUILTINS_ARCHIVE_FILE: &str = "libclang_rt.builtins-riscv32.a";
#[cfg(feature = "riscv-64")]
const BUILTINS_ARCHIVE_FILE: &str = "libclang_rt.builtins-riscv64.a"; const BUILTINS_ARCHIVE_FILE: &str = "libclang_rt.builtins-riscv64.a";
#[cfg(not(feature = "riscv-64"))]
const BUILTINS_LIB_NAME: &str = "clang_rt.builtins-riscv32";
#[cfg(feature = "riscv-64")]
const BUILTINS_LIB_NAME: &str = "clang_rt.builtins-riscv64"; const BUILTINS_LIB_NAME: &str = "clang_rt.builtins-riscv64";
fn invoke_lld(cmd_args: &[&str]) -> bool { fn invoke_lld(cmd_args: &[&str]) -> bool {
@@ -29,9 +22,10 @@ fn invoke_lld(cmd_args: &[&str]) -> bool {
unsafe { LLDELFLink(args.as_ptr(), args.len()) == 0 } unsafe { LLDELFLink(args.as_ptr(), args.len()) == 0 }
} }
fn polkavm_linker<T: AsRef<[u8]>>(code: T) -> anyhow::Result<Vec<u8>> { pub fn polkavm_linker<T: AsRef<[u8]>>(code: T, strip_binary: bool) -> 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(strip_binary);
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))
@@ -63,6 +57,7 @@ pub fn link<T: AsRef<[u8]>>(input: T) -> anyhow::Result<Vec<u8>> {
"--relocatable", "--relocatable",
"--emit-relocs", "--emit-relocs",
"--no-relax", "--no-relax",
"--unique",
"--gc-sections", "--gc-sections",
"--library-path", "--library-path",
dir.path().to_str().expect("should be utf8"), dir.path().to_str().expect("should be utf8"),
@@ -78,10 +73,5 @@ pub fn link<T: AsRef<[u8]>>(input: T) -> anyhow::Result<Vec<u8>> {
return Err(anyhow::anyhow!("ld.lld failed")); return Err(anyhow::anyhow!("ld.lld failed"));
} }
if env::var("PVM_LINKER_DUMP_SO").is_ok() { Ok(fs::read(&output_path)?)
fs::copy(&output_path, "/tmp/out.so")?;
};
let blob = fs::read(&output_path)?;
polkavm_linker(blob)
} }
+76 -8
View File
@@ -1,5 +1,19 @@
fn llvm_config(arg: &str) -> String { use std::{
let output = std::process::Command::new("llvm-config") env,
path::{Path, PathBuf},
};
const LLVM_LINK_PREFIX: &str = "LLVM_LINK_PREFIX";
fn locate_llvm_config() -> PathBuf {
let prefix = env::var_os(LLVM_LINK_PREFIX)
.map(|p| PathBuf::from(p).join("bin"))
.unwrap_or_default();
prefix.join("llvm-config")
}
fn llvm_config(llvm_config_path: &Path, arg: &str) -> String {
let output = std::process::Command::new(llvm_config_path)
.args([arg]) .args([arg])
.output() .output()
.unwrap_or_else(|_| panic!("`llvm-config {arg}` failed")); .unwrap_or_else(|_| panic!("`llvm-config {arg}` failed"));
@@ -8,8 +22,11 @@ fn llvm_config(arg: &str) -> String {
.unwrap_or_else(|_| panic!("output of `llvm-config {arg}` should be utf8")) .unwrap_or_else(|_| panic!("output of `llvm-config {arg}` should be utf8"))
} }
fn set_rustc_link_flags() { fn set_rustc_link_flags(llvm_config_path: &Path) {
println!("cargo:rustc-link-search=native={}", llvm_config("--libdir")); println!(
"cargo:rustc-link-search=native={}",
llvm_config(llvm_config_path, "--libdir")
);
for lib in [ for lib in [
"lldELF", "lldELF",
@@ -22,19 +39,70 @@ fn set_rustc_link_flags() {
"LLVMTargetParser", "LLVMTargetParser",
"LLVMBinaryFormat", "LLVMBinaryFormat",
"LLVMDemangle", "LLVMDemangle",
// The `llvm-sys` crate relies on `llvm-config` to obtain a list of required LLVM libraries
// during the build process. This works well in typical native environments, where `llvm-config`
// can accurately list the necessary libraries.
// However, when cross-compiling to WebAssembly using Emscripten, `llvm-config` fails to recognize
// JavaScript-based libraries, making it necessary to manually inject the required dependencies.
"LLVMRISCVDisassembler",
"LLVMRISCVAsmParser",
"LLVMRISCVCodeGen",
"LLVMRISCVDesc",
"LLVMRISCVInfo",
"LLVMExecutionEngine",
"LLVMOption",
"LLVMMCDisassembler",
"LLVMPasses",
"LLVMHipStdPar",
"LLVMCFGuard",
"LLVMCoroutines",
"LLVMipo",
"LLVMVectorize",
"LLVMInstrumentation",
"LLVMFrontendOpenMP",
"LLVMFrontendOffloading",
"LLVMGlobalISel",
"LLVMAsmPrinter",
"LLVMSelectionDAG",
"LLVMCodeGen",
"LLVMTarget",
"LLVMObjCARCOpts",
"LLVMCodeGenTypes",
"LLVMIRPrinter",
"LLVMScalarOpts",
"LLVMInstCombine",
"LLVMAggressiveInstCombine",
"LLVMTransformUtils",
"LLVMBitWriter",
"LLVMAnalysis",
"LLVMProfileData",
"LLVMDebugInfoDWARF",
"LLVMObject",
"LLVMMCParser",
"LLVMIRReader",
"LLVMAsmParser",
"LLVMMC",
"LLVMDebugInfoCodeView",
"LLVMBitReader",
"LLVMRemarks",
"LLVMBitstreamReader",
] { ] {
println!("cargo:rustc-link-lib=static={lib}"); println!("cargo:rustc-link-lib=static={lib}");
} }
#[cfg(target_os = "linux")] let target_os = std::env::var("CARGO_CFG_TARGET_OS").unwrap_or_default();
{ if target_os == "linux" {
println!("cargo:rustc-link-lib=dylib=stdc++"); println!("cargo:rustc-link-lib=dylib=stdc++");
println!("cargo:rustc-link-lib=tinfo"); println!("cargo:rustc-link-lib=tinfo");
} }
} }
fn main() { fn main() {
llvm_config("--cxxflags") println!("cargo:rerun-if-env-changed={}", LLVM_LINK_PREFIX);
let llvm_config_path = locate_llvm_config();
llvm_config(&llvm_config_path, "--cxxflags")
.split_whitespace() .split_whitespace()
.fold(&mut cc::Build::new(), |builder, flag| builder.flag(flag)) .fold(&mut cc::Build::new(), |builder, flag| builder.flag(flag))
.flag("-Wno-unused-parameter") .flag("-Wno-unused-parameter")
@@ -42,7 +110,7 @@ fn main() {
.file("src/linker.cpp") .file("src/linker.cpp")
.compile("liblinker.a"); .compile("liblinker.a");
set_rustc_link_flags(); set_rustc_link_flags(&llvm_config_path);
println!("cargo:rerun-if-changed=build.rs"); println!("cargo:rerun-if-changed=build.rs");
} }
+1 -5
View File
@@ -13,10 +13,6 @@ description = "Shared front end code of the revive PolkaVM compilers"
[lib] [lib]
doctest = false doctest = false
[features]
riscv-zbb = []
riscv-64 = []
[dependencies] [dependencies]
anyhow = { workspace = true } anyhow = { workspace = true }
semver = { workspace = true } semver = { workspace = true }
@@ -34,7 +30,7 @@ polkavm-disassembler = { workspace = true }
polkavm-common = { workspace = true } polkavm-common = { workspace = true }
revive-common = { workspace = true } revive-common = { workspace = true }
pallet-contracts-pvm-llapi = { workspace = true } revive-runtime-api = { workspace = true }
revive-linker = { workspace = true } revive-linker = { workspace = true }
revive-builtins = { workspace = true } revive-builtins = { workspace = true }
revive-stdlib = { workspace = true } revive-stdlib = { workspace = true }
@@ -15,6 +15,8 @@ pub enum IRType {
LLVM, LLVM,
/// Whether to dump the assembly code. /// Whether to dump the assembly code.
Assembly, Assembly,
/// Whether to dump the ELF shared object
SO,
/// Whether to jump JSON /// Whether to jump JSON
#[cfg(debug_assertions)] #[cfg(debug_assertions)]
JSON, JSON,
@@ -31,6 +33,7 @@ impl IRType {
Self::Assembly => revive_common::EXTENSION_POLKAVM_ASSEMBLY, Self::Assembly => revive_common::EXTENSION_POLKAVM_ASSEMBLY,
#[cfg(debug_assertions)] #[cfg(debug_assertions)]
Self::JSON => revive_common::EXTENSION_JSON, Self::JSON => revive_common::EXTENSION_JSON,
Self::SO => revive_common::EXTENSION_SHARED_OBJECT,
} }
} }
} }
+66 -33
View File
@@ -13,41 +13,52 @@ use self::ir_type::IRType;
#[derive(Debug, Default, Serialize, Deserialize, Clone)] #[derive(Debug, Default, Serialize, Deserialize, Clone)]
pub struct DebugConfig { pub struct DebugConfig {
/// The directory to dump the IRs to. /// The directory to dump the IRs to.
pub output_directory: PathBuf, pub output_directory: Option<PathBuf>,
/// Whether debug info should be emitted.
pub emit_debug_info: bool,
} }
impl DebugConfig { impl DebugConfig {
/// A shortcut constructor. /// A shortcut constructor.
pub fn new(output_directory: PathBuf) -> Self { pub const fn new(output_directory: Option<PathBuf>, emit_debug_info: bool) -> Self {
Self { output_directory } Self {
output_directory,
emit_debug_info,
}
} }
/// Dumps the Yul IR. /// Dumps the Yul IR.
pub fn dump_yul(&self, contract_path: &str, code: &str) -> anyhow::Result<()> { pub fn dump_yul(&self, contract_path: &str, code: &str) -> anyhow::Result<()> {
let mut file_path = self.output_directory.to_owned(); if let Some(output_directory) = self.output_directory.as_ref() {
let full_file_name = Self::full_file_name(contract_path, None, IRType::Yul); let mut file_path = output_directory.to_owned();
file_path.push(full_file_name); let full_file_name = Self::full_file_name(contract_path, None, IRType::Yul);
std::fs::write(file_path, code)?; file_path.push(full_file_name);
std::fs::write(file_path, code)?;
}
Ok(()) Ok(())
} }
/// Dumps the EVM legacy assembly IR. /// Dumps the EVM legacy assembly IR.
pub fn dump_evmla(&self, contract_path: &str, code: &str) -> anyhow::Result<()> { pub fn dump_evmla(&self, contract_path: &str, code: &str) -> anyhow::Result<()> {
let mut file_path = self.output_directory.to_owned(); if let Some(output_directory) = self.output_directory.as_ref() {
let full_file_name = Self::full_file_name(contract_path, None, IRType::EVMLA); let mut file_path = output_directory.to_owned();
file_path.push(full_file_name); let full_file_name = Self::full_file_name(contract_path, None, IRType::EVMLA);
std::fs::write(file_path, code)?; file_path.push(full_file_name);
std::fs::write(file_path, code)?;
}
Ok(()) Ok(())
} }
/// Dumps the Ethereal IR. /// Dumps the Ethereal IR.
pub fn dump_ethir(&self, contract_path: &str, code: &str) -> anyhow::Result<()> { pub fn dump_ethir(&self, contract_path: &str, code: &str) -> anyhow::Result<()> {
let mut file_path = self.output_directory.to_owned(); if let Some(output_directory) = self.output_directory.as_ref() {
let full_file_name = Self::full_file_name(contract_path, None, IRType::EthIR); let mut file_path = output_directory.to_owned();
file_path.push(full_file_name); let full_file_name = Self::full_file_name(contract_path, None, IRType::EthIR);
std::fs::write(file_path, code)?; file_path.push(full_file_name);
std::fs::write(file_path, code)?;
}
Ok(()) Ok(())
} }
@@ -58,12 +69,15 @@ impl DebugConfig {
contract_path: &str, contract_path: &str,
module: &inkwell::module::Module, module: &inkwell::module::Module,
) -> anyhow::Result<()> { ) -> anyhow::Result<()> {
let llvm_code = module.print_to_string().to_string(); if let Some(output_directory) = self.output_directory.as_ref() {
let llvm_code = module.print_to_string().to_string();
let mut file_path = self.output_directory.to_owned(); let mut file_path = output_directory.to_owned();
let full_file_name = Self::full_file_name(contract_path, Some("unoptimized"), IRType::LLVM); let full_file_name =
file_path.push(full_file_name); Self::full_file_name(contract_path, Some("unoptimized"), IRType::LLVM);
std::fs::write(file_path, llvm_code)?; file_path.push(full_file_name);
std::fs::write(file_path, llvm_code)?;
}
Ok(()) Ok(())
} }
@@ -74,22 +88,39 @@ impl DebugConfig {
contract_path: &str, contract_path: &str,
module: &inkwell::module::Module, module: &inkwell::module::Module,
) -> anyhow::Result<()> { ) -> anyhow::Result<()> {
let llvm_code = module.print_to_string().to_string(); if let Some(output_directory) = self.output_directory.as_ref() {
let llvm_code = module.print_to_string().to_string();
let mut file_path = self.output_directory.to_owned(); let mut file_path = output_directory.to_owned();
let full_file_name = Self::full_file_name(contract_path, Some("optimized"), IRType::LLVM); let full_file_name =
file_path.push(full_file_name); Self::full_file_name(contract_path, Some("optimized"), IRType::LLVM);
std::fs::write(file_path, llvm_code)?; file_path.push(full_file_name);
std::fs::write(file_path, llvm_code)?;
}
Ok(()) Ok(())
} }
/// Dumps the assembly. /// Dumps the assembly.
pub fn dump_assembly(&self, contract_path: &str, code: &str) -> anyhow::Result<()> { pub fn dump_assembly(&self, contract_path: &str, code: &str) -> anyhow::Result<()> {
let mut file_path = self.output_directory.to_owned(); if let Some(output_directory) = self.output_directory.as_ref() {
let full_file_name = Self::full_file_name(contract_path, None, IRType::Assembly); let mut file_path = output_directory.to_owned();
file_path.push(full_file_name); let full_file_name = Self::full_file_name(contract_path, None, IRType::Assembly);
std::fs::write(file_path, code)?; file_path.push(full_file_name);
std::fs::write(file_path, code)?;
}
Ok(())
}
/// Dumps the code object.
pub fn dump_object(&self, contract_path: &str, code: &[u8]) -> anyhow::Result<()> {
if let Some(output_directory) = self.output_directory.as_ref() {
let mut file_path = output_directory.to_owned();
let full_file_name = Self::full_file_name(contract_path, None, IRType::SO);
file_path.push(full_file_name);
std::fs::write(file_path, code)?;
}
Ok(()) Ok(())
} }
@@ -102,10 +133,12 @@ impl DebugConfig {
contract_suffix: Option<&str>, contract_suffix: Option<&str>,
stage_json: &Vec<u8>, stage_json: &Vec<u8>,
) -> anyhow::Result<()> { ) -> anyhow::Result<()> {
let mut file_path = self.output_directory.to_owned(); if let Some(output_directory) = self.output_directory.as_ref() {
let full_file_name = Self::full_file_name(contract_path, contract_suffix, IRType::JSON); let mut file_path = output_directory.to_owned();
file_path.push(full_file_name); let full_file_name = Self::full_file_name(contract_path, contract_suffix, IRType::JSON);
std::fs::write(file_path, stage_json)?; file_path.push(full_file_name);
std::fs::write(file_path, stage_json)?;
}
Ok(()) Ok(())
} }
+3 -1
View File
@@ -16,6 +16,7 @@ pub use self::polkavm::context::argument::Argument as PolkaVMArgument;
pub use self::polkavm::context::attribute::Attribute as PolkaVMAttribute; pub use self::polkavm::context::attribute::Attribute as PolkaVMAttribute;
pub use self::polkavm::context::build::Build as PolkaVMBuild; pub use self::polkavm::context::build::Build as PolkaVMBuild;
pub use self::polkavm::context::code_type::CodeType as PolkaVMCodeType; pub use self::polkavm::context::code_type::CodeType as PolkaVMCodeType;
pub use self::polkavm::context::debug_info::DebugInfo;
pub use self::polkavm::context::evmla_data::EVMLAData as PolkaVMContextEVMLAData; pub use self::polkavm::context::evmla_data::EVMLAData as PolkaVMContextEVMLAData;
pub use self::polkavm::context::function::block::evmla_data::key::Key as PolkaVMFunctionBlockKey; pub use self::polkavm::context::function::block::evmla_data::key::Key as PolkaVMFunctionBlockKey;
pub use self::polkavm::context::function::block::evmla_data::EVMLAData as PolkaVMFunctionBlockEVMLAData; pub use self::polkavm::context::function::block::evmla_data::EVMLAData as PolkaVMFunctionBlockEVMLAData;
@@ -27,9 +28,11 @@ 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;
@@ -58,7 +61,6 @@ pub use self::polkavm::evm::return_data as polkavm_evm_return_data;
pub use self::polkavm::evm::storage as polkavm_evm_storage; pub use self::polkavm::evm::storage as polkavm_evm_storage;
pub use self::polkavm::metadata_hash::MetadataHash as PolkaVMMetadataHash; pub use self::polkavm::metadata_hash::MetadataHash as PolkaVMMetadataHash;
pub use self::polkavm::r#const as polkavm_const; pub use self::polkavm::r#const as polkavm_const;
pub use self::polkavm::utils as polkavm_utils;
pub use self::polkavm::Dependency as PolkaVMDependency; pub use self::polkavm::Dependency as PolkaVMDependency;
pub use self::polkavm::DummyDependency as PolkaVMDummyDependency; pub use self::polkavm::DummyDependency as PolkaVMDummyDependency;
pub use self::polkavm::DummyLLVMWritable as PolkaVMDummyLLVMWritable; pub use self::polkavm::DummyLLVMWritable as PolkaVMDummyLLVMWritable;
@@ -9,7 +9,7 @@ use itertools::Itertools;
use self::size_level::SizeLevel; use self::size_level::SizeLevel;
/// The LLVM optimizer settings. /// The LLVM optimizer and code-gen settings.
#[derive(Debug, Serialize, Deserialize, Clone, Eq)] #[derive(Debug, Serialize, Deserialize, Clone, Eq)]
pub struct Settings { pub struct Settings {
/// The middle-end optimization level. /// The middle-end optimization level.
@@ -21,8 +21,6 @@ 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,
@@ -43,7 +41,6 @@ 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,
@@ -65,7 +62,6 @@ 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,
@@ -206,20 +202,10 @@ 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 -56
View File
@@ -1,75 +1,20 @@
//! 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. /// The pointer width sized type.
pub const ZKEVM_VERSION: semver::Version = semver::Version::new(1, 3, 2);
/// The register width sized type
pub static XLEN: usize = revive_common::BIT_LENGTH_X32; pub static XLEN: usize = revive_common::BIT_LENGTH_X32;
/// The heap memory pointer pointer global variable name. /// The heap memory pointer pointer global variable name.
pub static GLOBAL_HEAP_MEMORY_POINTER: &str = "memory_pointer"; pub static GLOBAL_HEAP_MEMORY_POINTER: &str = "memory_pointer";
/// The calldata pointer global variable name.
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 extra ABI data global variable name.
pub static GLOBAL_EXTRA_ABI_DATA: &str = "extra_abi_data";
/// 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 number of the extra ABI data arguments.
pub const EXTRA_ABI_DATA_SIZE: usize = 0;
/// 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;
@@ -1,71 +0,0 @@
//! 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 = "seal_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";
@@ -1,7 +1,43 @@
//! The LLVM debug information. //! The LLVM debug information.
use std::cell::RefCell;
use inkwell::debug_info::AsDIScope; use inkwell::debug_info::AsDIScope;
use num::Zero; use inkwell::debug_info::DIScope;
/// Debug info scope stack
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ScopeStack<'ctx> {
stack: Vec<DIScope<'ctx>>,
}
// Abstract the type of the DIScope stack.
impl<'ctx> ScopeStack<'ctx> {
pub fn from(item: DIScope<'ctx>) -> Self {
Self { stack: vec![item] }
}
/// Return the top of the scope stack, or None if the stack is empty.
pub fn top(&self) -> Option<DIScope<'ctx>> {
self.stack.last().copied()
}
/// Push a scope onto the stack.
pub fn push(&mut self, scope: DIScope<'ctx>) {
self.stack.push(scope)
}
/// Pop the scope at the top of the stack and return it.
/// Return None if the stack is empty.
pub fn pop(&mut self) -> Option<DIScope<'ctx>> {
self.stack.pop()
}
/// Return the number of scopes on the stack.
pub fn len(&self) -> usize {
self.stack.len()
}
}
/// The LLVM debug information. /// The LLVM debug information.
pub struct DebugInfo<'ctx> { pub struct DebugInfo<'ctx> {
@@ -9,6 +45,8 @@ pub struct DebugInfo<'ctx> {
compile_unit: inkwell::debug_info::DICompileUnit<'ctx>, compile_unit: inkwell::debug_info::DICompileUnit<'ctx>,
/// The debug info builder. /// The debug info builder.
builder: inkwell::debug_info::DebugInfoBuilder<'ctx>, builder: inkwell::debug_info::DebugInfoBuilder<'ctx>,
/// Enclosing debug info scopes.
scope_stack: RefCell<ScopeStack<'ctx>>,
} }
impl<'ctx> DebugInfo<'ctx> { impl<'ctx> DebugInfo<'ctx> {
@@ -35,19 +73,43 @@ impl<'ctx> DebugInfo<'ctx> {
Self { Self {
compile_unit, compile_unit,
builder, builder,
scope_stack: RefCell::new(ScopeStack::from(compile_unit.as_debug_info_scope())),
} }
} }
/// Prepare an LLVM-IR module for debug-info generation
pub fn initialize_module(
&self,
llvm: &'ctx inkwell::context::Context,
module: &inkwell::module::Module<'ctx>,
) {
let debug_metadata_value = llvm
.i32_type()
.const_int(inkwell::debug_info::debug_metadata_version() as u64, false);
module.add_basic_value_flag(
"Debug Info Version",
inkwell::module::FlagBehavior::Warning,
debug_metadata_value,
);
self.push_scope(self.compilation_unit().get_file().as_debug_info_scope());
}
/// Finalize debug-info for an LLVM-IR module.
pub fn finalize_module(&self) {
self.builder().finalize()
}
/// Creates a function info. /// Creates a function info.
pub fn create_function( pub fn create_function(
&self, &self,
name: &str, name: &str,
) -> anyhow::Result<inkwell::debug_info::DISubprogram<'ctx>> { ) -> anyhow::Result<inkwell::debug_info::DISubprogram<'ctx>> {
let flags = inkwell::debug_info::DIFlagsConstants::ZERO;
let subroutine_type = self.builder.create_subroutine_type( let subroutine_type = self.builder.create_subroutine_type(
self.compile_unit.get_file(), self.compile_unit.get_file(),
Some(self.create_type(revive_common::BIT_LENGTH_FIELD)?), Some(self.create_word_type(Some(flags))?.as_type()),
&[], &[],
inkwell::debug_info::DIFlags::zero(), flags,
); );
let function = self.builder.create_function( let function = self.builder.create_function(
@@ -60,7 +122,7 @@ impl<'ctx> DebugInfo<'ctx> {
true, true,
false, false,
1, 1,
inkwell::debug_info::DIFlags::zero(), flags,
false, false,
); );
@@ -74,24 +136,55 @@ impl<'ctx> DebugInfo<'ctx> {
Ok(function) Ok(function)
} }
/// Creates a primitive type info. /// Creates primitive integer type debug-info.
pub fn create_type( pub fn create_primitive_type(
&self, &self,
bit_length: usize, bit_length: usize,
) -> anyhow::Result<inkwell::debug_info::DIType<'ctx>> { flags: Option<inkwell::debug_info::DIFlags>,
) -> anyhow::Result<inkwell::debug_info::DIBasicType<'ctx>> {
let di_flags = flags.unwrap_or(inkwell::debug_info::DIFlagsConstants::ZERO);
let di_encoding: u32 = 0;
let type_name = String::from("U") + bit_length.to_string().as_str();
self.builder self.builder
.create_basic_type( .create_basic_type(type_name.as_str(), bit_length as u64, di_encoding, di_flags)
"U256",
bit_length as u64,
0,
inkwell::debug_info::DIFlags::zero(),
)
.map(|basic_type| basic_type.as_type())
.map_err(|error| anyhow::anyhow!("Debug info error: {}", error)) .map_err(|error| anyhow::anyhow!("Debug info error: {}", error))
} }
/// Finalizes the builder. /// Returns the debug-info model of word-sized integer types.
pub fn finalize(&self) { pub fn create_word_type(
self.builder.finalize(); &self,
flags: Option<inkwell::debug_info::DIFlags>,
) -> anyhow::Result<inkwell::debug_info::DIBasicType<'ctx>> {
self.create_primitive_type(revive_common::BIT_LENGTH_WORD, flags)
}
/// Return the DIBuilder.
pub fn builder(&self) -> &inkwell::debug_info::DebugInfoBuilder<'ctx> {
&self.builder
}
/// Return the compilation unit. {
pub fn compilation_unit(&self) -> &inkwell::debug_info::DICompileUnit<'ctx> {
&self.compile_unit
}
/// Push a debug-info scope onto the stack.
pub fn push_scope(&self, scope: DIScope<'ctx>) {
self.scope_stack.borrow_mut().push(scope)
}
/// Pop the top of the debug-info scope stack and return it.
pub fn pop_scope(&self) -> Option<DIScope<'ctx>> {
self.scope_stack.borrow_mut().pop()
}
/// Return the top of the debug-info scope stack.
pub fn top_scope(&self) -> Option<DIScope<'ctx>> {
self.scope_stack.borrow().top()
}
/// Return the number of debug-info scopes on the scope stack.
pub fn num_scopes(&self) -> usize {
self.scope_stack.borrow().len()
} }
} }
@@ -13,7 +13,7 @@ pub struct EVMLAData<'ctx> {
pub stack: Vec<Argument<'ctx>>, pub stack: Vec<Argument<'ctx>>,
} }
impl<'ctx> EVMLAData<'ctx> { impl EVMLAData<'_> {
/// The default stack size. /// The default stack size.
pub const DEFAULT_STACK_SIZE: usize = 64; pub const DEFAULT_STACK_SIZE: usize = 64;
@@ -17,4 +17,8 @@ impl<'ctx> Declaration<'ctx> {
) -> Self { ) -> Self {
Self { r#type, value } Self { r#type, value }
} }
pub fn function_value(&self) -> inkwell::values::FunctionValue<'ctx> {
self.value
}
} }
@@ -11,15 +11,6 @@ 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.
@@ -31,32 +22,9 @@ 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";
@@ -72,121 +40,12 @@ 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);
@@ -236,54 +95,13 @@ 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,
} }
} }
@@ -11,6 +11,8 @@ pub mod yul_data;
use std::collections::HashMap; use std::collections::HashMap;
use inkwell::debug_info::AsDIScope;
use crate::optimizer::settings::size_level::SizeLevel; use crate::optimizer::settings::size_level::SizeLevel;
use crate::optimizer::Optimizer; use crate::optimizer::Optimizer;
use crate::polkavm::context::attribute::Attribute; use crate::polkavm::context::attribute::Attribute;
@@ -48,12 +50,6 @@ 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;
@@ -91,13 +87,8 @@ 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.
@@ -105,6 +96,14 @@ impl<'ctx> Function<'ctx> {
self.declaration self.declaration
} }
/// Returns the debug-info scope.
pub fn get_debug_scope(&self) -> Option<inkwell::debug_info::DIScope<'ctx>> {
self.declaration()
.function_value()
.get_subprogram()
.map(|scp| scp.as_debug_info_scope())
}
/// Returns the N-th parameter of the function. /// Returns the N-th parameter of the function.
pub fn get_nth_param(&self, index: usize) -> inkwell::values::BasicValueEnum<'ctx> { pub fn get_nth_param(&self, index: usize) -> inkwell::values::BasicValueEnum<'ctx> {
self.declaration() self.declaration()
@@ -42,8 +42,7 @@ 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 = let function_type = context.function_type::<inkwell::types::BasicTypeEnum>(vec![], 0);
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,
@@ -55,12 +54,14 @@ where
} }
fn into_llvm(self, context: &mut Context<D>) -> anyhow::Result<()> { fn into_llvm(self, context: &mut Context<D>) -> anyhow::Result<()> {
context.set_current_function(runtime::FUNCTION_DEPLOY_CODE)?; context.set_current_function(runtime::FUNCTION_DEPLOY_CODE, None)?;
context.set_basic_block(context.current_function().borrow().entry_block()); context.set_basic_block(context.current_function().borrow().entry_block());
context.set_code_type(CodeType::Deploy); context.set_code_type(CodeType::Deploy);
self.inner.into_llvm(context)?; self.inner.into_llvm(context)?;
context.set_debug_location(0, 0, None)?;
match context match context
.basic_block() .basic_block()
.get_last_instruction() .get_last_instruction()
@@ -73,8 +74,11 @@ where
} }
context.set_basic_block(context.current_function().borrow().return_block()); context.set_basic_block(context.current_function().borrow().return_block());
context.set_debug_location(0, 0, None)?;
context.build_return(None); context.build_return(None);
context.pop_debug_scope();
Ok(()) Ok(())
} }
} }
@@ -1,12 +1,10 @@
//! 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;
@@ -20,37 +18,12 @@ 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;
/// The number of mandatory arguments.
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.
pub fn initialize_globals<D>(context: &mut Context<D>) -> anyhow::Result<()> pub fn initialize_globals<D>(context: &mut Context<D>) -> anyhow::Result<()>
where where
D: Dependency + Clone, D: Dependency + Clone,
{ {
let calldata_type = context.array_type(context.byte_type(), Self::MAX_CALLDATA_SIZE);
context.set_global(
crate::polkavm::GLOBAL_CALLDATA_POINTER,
calldata_type,
AddressSpace::Stack,
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()),
@@ -61,20 +34,17 @@ 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.integer_const(crate::polkavm::XLEN, 0))?, context.build_sbrk(
context.xlen_type().const_zero(),
context.xlen_type().const_zero(),
)?,
)?; )?;
context.set_global( context.set_global(
crate::polkavm::GLOBAL_CALLDATA_SIZE, crate::polkavm::GLOBAL_CALLDATA_SIZE,
context.word_type(),
AddressSpace::Stack,
context.word_undef(),
);
context.set_global(
crate::polkavm::GLOBAL_RETURN_DATA_SIZE,
context.xlen_type(), context.xlen_type(),
AddressSpace::Stack, AddressSpace::Stack,
context.xlen_type().const_zero().as_basic_value_enum(), context.xlen_type().get_undef(),
); );
context.set_global( context.set_global(
@@ -84,94 +54,25 @@ impl Entry {
context.word_const(0), context.word_const(0),
); );
let extra_abi_data_type = context.array_type(
context.word_type().as_basic_type_enum(),
crate::polkavm::EXTRA_ABI_DATA_SIZE,
);
context.set_global(
crate::polkavm::GLOBAL_EXTRA_ABI_DATA,
extra_abi_data_type,
AddressSpace::Stack,
extra_abi_data_type.const_zero(),
);
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(())
} }
/// Load the calldata via seal `input` and initialize the calldata end /// Populate the calldata size global value.
/// and calldata size globals. pub fn load_calldata_size<D>(context: &mut Context<D>) -> anyhow::Result<()>
pub fn load_calldata<D>(context: &mut Context<D>) -> anyhow::Result<()>
where where
D: Dependency + Clone, D: Dependency + Clone,
{ {
let input_pointer = context let call_data_size_pointer = context
.get_global(crate::polkavm::GLOBAL_CALLDATA_POINTER)? .get_global(crate::polkavm::GLOBAL_CALLDATA_SIZE)?
.value .value
.as_pointer_value(); .as_pointer_value();
let input_pointer_casted = context.builder.build_ptr_to_int( let call_data_size_value = context
input_pointer, .build_runtime_call(revive_runtime_api::polkavm_imports::CALL_DATA_SIZE, &[])
context.xlen_type(), .expect("the call_data_size syscall method should return a value")
"input_pointer_casted",
)?;
let length_pointer = context.build_alloca(context.xlen_type(), "len_ptr");
let length_pointer_casted = context.builder.build_ptr_to_int(
length_pointer.value,
context.xlen_type(),
"length_pointer_casted",
)?;
context.build_store(
length_pointer,
context.integer_const(crate::polkavm::XLEN, Self::MAX_CALLDATA_SIZE as u64),
)?;
context.build_runtime_call(
runtime_api::imports::INPUT,
&[input_pointer_casted.into(), length_pointer_casted.into()],
);
// Store the calldata size
let calldata_size = context
.build_load(length_pointer, "input_size")?
.into_int_value(); .into_int_value();
let calldata_size_casted = context.builder().build_int_z_extend( context
calldata_size, .builder()
context.word_type(), .build_store(call_data_size_pointer, call_data_size_value)?;
"zext_input_len",
)?;
context.set_global(
crate::polkavm::GLOBAL_CALLDATA_SIZE,
context.word_type(),
AddressSpace::Stack,
calldata_size_casted,
);
Ok(()) Ok(())
} }
@@ -182,6 +83,8 @@ impl Entry {
where where
D: Dependency + Clone, D: Dependency + Clone,
{ {
context.set_debug_location(0, 0, None)?;
let is_deploy = context let is_deploy = context
.current_function() .current_function()
.borrow() .borrow()
@@ -232,12 +135,25 @@ 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, false); let entry_function_type = context.function_type(entry_arguments, 0);
context.add_function(runtime::FUNCTION_ENTRY, entry_function_type, 0, None)?; context.add_function(
runtime::FUNCTION_ENTRY,
entry_function_type,
0,
Some(inkwell::module::Linkage::External),
)?;
for symbol in runtime_api::exports::EXPORTS { context.declare_global(
context.declare_extern_function(symbol)?; 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(())
} }
@@ -258,37 +174,19 @@ where
true, true,
); );
context.set_current_function(runtime_api::exports::DEPLOY)?; context.set_current_function(runtime::FUNCTION_ENTRY, None)?;
context.set_basic_block(context.current_function().borrow().entry_block());
assert!(context
.build_call(entry, &[context.bool_const(true).into()], "entry_deploy")
.is_none());
context.set_basic_block(context.current_function().borrow().return_block);
context.build_unreachable();
context.set_current_function(runtime_api::exports::CALL)?;
context.set_basic_block(context.current_function().borrow().entry_block());
assert!(context
.build_call(entry, &[context.bool_const(false).into()], "entry_call")
.is_none());
context.set_basic_block(context.current_function().borrow().return_block);
context.build_unreachable();
context.set_current_function(runtime::FUNCTION_ENTRY)?;
context.set_basic_block(context.current_function().borrow().entry_block()); context.set_basic_block(context.current_function().borrow().entry_block());
Self::initialize_globals(context)?; Self::initialize_globals(context)?;
Self::load_calldata(context)?; Self::load_calldata_size(context)?;
Self::leave_entry(context)?; Self::leave_entry(context)?;
context.build_unconditional_branch(context.current_function().borrow().return_block()); context.build_unconditional_branch(context.current_function().borrow().return_block());
context.set_basic_block(context.current_function().borrow().return_block()); context.set_basic_block(context.current_function().borrow().return_block());
context.build_unreachable(); context.build_unreachable();
context.pop_debug_scope();
Ok(()) Ok(())
} }
} }
@@ -0,0 +1,118 @@
//! 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::External),
)?;
Ok(())
}
fn into_llvm(self, context: &mut Context<D>) -> anyhow::Result<()> {
context.set_current_function(runtime::FUNCTION_LOAD_IMMUTABLE_DATA, None)?;
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);
context.pop_debug_scope();
Ok(())
}
}
@@ -2,6 +2,7 @@
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.
@@ -12,3 +13,6 @@ 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,8 +42,7 @@ 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 = let function_type = context.function_type::<inkwell::types::BasicTypeEnum>(vec![], 0);
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,
@@ -55,11 +54,14 @@ where
} }
fn into_llvm(self, context: &mut Context<D>) -> anyhow::Result<()> { fn into_llvm(self, context: &mut Context<D>) -> anyhow::Result<()> {
context.set_current_function(runtime::FUNCTION_RUNTIME_CODE)?; context.set_current_function(runtime::FUNCTION_RUNTIME_CODE, None)?;
context.set_basic_block(context.current_function().borrow().entry_block()); context.set_basic_block(context.current_function().borrow().entry_block());
context.set_code_type(CodeType::Runtime); context.set_code_type(CodeType::Runtime);
self.inner.into_llvm(context)?; self.inner.into_llvm(context)?;
context.set_debug_location(0, 0, None)?;
match context match context
.basic_block() .basic_block()
.get_last_instruction() .get_last_instruction()
@@ -74,6 +76,8 @@ where
context.set_basic_block(context.current_function().borrow().return_block()); context.set_basic_block(context.current_function().borrow().return_block());
context.build_unreachable(); context.build_unreachable();
context.pop_debug_scope();
Ok(()) Ok(())
} }
} }
@@ -37,7 +37,7 @@ impl<'ctx> Global<'ctx> {
.add_global(r#type, Some(address_space.into()), name); .add_global(r#type, Some(address_space.into()), name);
let global = Self { r#type, value }; let global = Self { r#type, value };
global.value.set_linkage(inkwell::module::Linkage::Private); global.value.set_linkage(inkwell::module::Linkage::External);
global global
.value .value
.set_visibility(inkwell::GlobalVisibility::Default); .set_visibility(inkwell::GlobalVisibility::Default);
@@ -51,4 +51,31 @@ 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
}
} }
+311 -214
View File
@@ -5,7 +5,7 @@ pub mod argument;
pub mod attribute; pub mod attribute;
pub mod build; pub mod build;
pub mod code_type; pub mod code_type;
// pub mod debug_info; pub mod debug_info;
pub mod evmla_data; pub mod evmla_data;
pub mod function; pub mod function;
pub mod global; pub mod global;
@@ -21,12 +21,13 @@ use std::cell::RefCell;
use std::collections::HashMap; use std::collections::HashMap;
use std::rc::Rc; use std::rc::Rc;
use inkwell::debug_info::AsDIScope;
use inkwell::debug_info::DIScope;
use inkwell::types::BasicType; use inkwell::types::BasicType;
use inkwell::values::BasicValue; 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;
@@ -36,7 +37,7 @@ use self::address_space::AddressSpace;
use self::attribute::Attribute; use self::attribute::Attribute;
use self::build::Build; use self::build::Build;
use self::code_type::CodeType; use self::code_type::CodeType;
// use self::debug_info::DebugInfo; use self::debug_info::DebugInfo;
use self::evmla_data::EVMLAData; use self::evmla_data::EVMLAData;
use self::function::declaration::Declaration as FunctionDeclaration; use self::function::declaration::Declaration as FunctionDeclaration;
use self::function::intrinsics::Intrinsics; use self::function::intrinsics::Intrinsics;
@@ -86,9 +87,9 @@ where
/// Whether to append the metadata hash at the end of bytecode. /// Whether to append the metadata hash at the end of bytecode.
include_metadata_hash: bool, include_metadata_hash: bool,
/// The debug info of the current module. /// The debug info of the current module.
// debug_info: DebugInfo<'ctx>, debug_info: Option<DebugInfo<'ctx>>,
/// The debug configuration telling whether to dump the needed IRs. /// The debug configuration telling whether to dump the needed IRs.
debug_config: Option<DebugConfig>, debug_config: DebugConfig,
/// The Solidity data. /// The Solidity data.
solidity_data: Option<SolidityData>, solidity_data: Option<SolidityData>,
@@ -124,36 +125,57 @@ where
.expect("the stdlib module should be linkable"); .expect("the stdlib module should be linkable");
} }
/// Link in the PolkaVM guest module, containing imported and exported functions, /// Link in the PolkaVM imports module, containing imported functions,
/// and marking them as external (they need to be relocatable as too). /// and marking them as external (they need to be relocatable as too).
fn link_polkavm_guest_module( fn link_polkavm_imports(
llvm: &'ctx inkwell::context::Context, llvm: &'ctx inkwell::context::Context,
module: &inkwell::module::Module<'ctx>, module: &inkwell::module::Module<'ctx>,
) { ) {
module module
.link_in_module( .link_in_module(
pallet_contracts_pvm_llapi::polkavm_guest::module(llvm, "polkavm_guest").unwrap(), revive_runtime_api::polkavm_imports::module(llvm, "polkavm_imports").unwrap(),
) )
.expect("the PolkaVM guest API module should be linkable"); .expect("the PolkaVM imports module should be linkable");
for export in runtime_api::exports::EXPORTS { for import in revive_runtime_api::polkavm_imports::IMPORTS {
module
.get_function(export)
.expect("should be declared")
.add_attribute(
inkwell::attributes::AttributeLoc::Function,
llvm.create_enum_attribute(Attribute::NoReturn as u32, 0),
);
}
for import in runtime_api::imports::IMPORTS {
module module
.get_function(import) .get_function(import)
.expect("should be declared") .unwrap_or_else(|| panic!("{import} import should be declared"))
.set_linkage(inkwell::module::Linkage::External); .set_linkage(inkwell::module::Linkage::External);
} }
} }
fn link_polkavm_exports(&self, contract_path: &str) -> anyhow::Result<()> {
let exports = revive_runtime_api::polkavm_exports::module(self.llvm(), "polkavm_exports")
.map_err(|error| {
anyhow::anyhow!(
"The contract `{}` exports module loading error: {}",
contract_path,
error
)
})?;
self.module.link_in_module(exports).map_err(|error| {
anyhow::anyhow!(
"The contract `{}` exports module linking error: {}",
contract_path,
error
)
})
}
fn link_immutable_data(&self, contract_path: &str) -> anyhow::Result<()> {
let size = self.solidity().immutables_size() as u32;
let immutables = revive_runtime_api::immutable_data::module(self.llvm(), size);
self.module.link_in_module(immutables).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,
@@ -161,7 +183,7 @@ where
size: u32, size: u32,
) { ) {
module module
.link_in_module(pallet_contracts_pvm_llapi::polkavm_guest::min_stack_size( .link_in_module(revive_runtime_api::calling_convention::min_stack_size(
llvm, llvm,
"polkavm_stack_size", "polkavm_stack_size",
size, size,
@@ -181,6 +203,16 @@ where
); );
} }
/// Configure the revive datalayout.
fn set_data_layout(
llvm: &'ctx inkwell::context::Context,
module: &inkwell::module::Module<'ctx>,
) {
let source_module = revive_stdlib::module(llvm, "revive_stdlib").unwrap();
let data_layout = source_module.get_data_layout();
module.set_data_layout(&data_layout);
}
/// Initializes a new LLVM context. /// Initializes a new LLVM context.
pub fn new( pub fn new(
llvm: &'ctx inkwell::context::Context, llvm: &'ctx inkwell::context::Context,
@@ -188,15 +220,21 @@ where
optimizer: Optimizer, optimizer: Optimizer,
dependency_manager: Option<D>, dependency_manager: Option<D>,
include_metadata_hash: bool, include_metadata_hash: bool,
debug_config: Option<DebugConfig>, debug_config: DebugConfig,
) -> Self { ) -> Self {
Self::set_data_layout(llvm, &module);
Self::link_stdlib_module(llvm, &module); Self::link_stdlib_module(llvm, &module);
Self::link_polkavm_guest_module(llvm, &module); Self::link_polkavm_imports(llvm, &module);
Self::set_polkavm_stack_size(llvm, &module, Self::POLKAVM_STACK_SIZE); Self::set_polkavm_stack_size(llvm, &module, Self::POLKAVM_STACK_SIZE);
Self::set_module_flags(llvm, &module); Self::set_module_flags(llvm, &module);
let intrinsics = Intrinsics::new(llvm, &module); let intrinsics = Intrinsics::new(llvm, &module);
let llvm_runtime = LLVMRuntime::new(llvm, &module, &optimizer); let llvm_runtime = LLVMRuntime::new(llvm, &module, &optimizer);
let debug_info = debug_config.emit_debug_info.then(|| {
let debug_info = DebugInfo::new(&module);
debug_info.initialize_module(llvm, &module);
debug_info
});
Self { Self {
llvm, llvm,
@@ -213,7 +251,8 @@ where
dependency_manager, dependency_manager,
include_metadata_hash, include_metadata_hash,
// debug_info,
debug_info,
debug_config, debug_config,
solidity_data: None, solidity_data: None,
@@ -230,12 +269,15 @@ where
) -> anyhow::Result<Build> { ) -> anyhow::Result<Build> {
let module_clone = self.module.clone(); let module_clone = self.module.clone();
let target_machine = TargetMachine::new(Target::PVM, self.optimizer.settings())?; self.link_polkavm_exports(contract_path)?;
target_machine.set_target_data(self.module()); self.link_immutable_data(contract_path)?;
let target_machine = TargetMachine::new(Target::PVM, self.optimizer.settings())?;
self.module().set_triple(&target_machine.get_triple());
self.debug_config
.dump_llvm_ir_unoptimized(contract_path, self.module())?;
if let Some(ref debug_config) = self.debug_config {
debug_config.dump_llvm_ir_unoptimized(contract_path, self.module())?;
}
self.verify().map_err(|error| { self.verify().map_err(|error| {
anyhow::anyhow!( anyhow::anyhow!(
"The contract `{}` unoptimized LLVM IR verification error: {}", "The contract `{}` unoptimized LLVM IR verification error: {}",
@@ -253,9 +295,10 @@ where
error error
) )
})?; })?;
if let Some(ref debug_config) = self.debug_config {
debug_config.dump_llvm_ir_optimized(contract_path, self.module())?; self.debug_config
} .dump_llvm_ir_optimized(contract_path, self.module())?;
self.verify().map_err(|error| { self.verify().map_err(|error| {
anyhow::anyhow!( anyhow::anyhow!(
"The contract `{}` optimized LLVM IR verification error: {}", "The contract `{}` optimized LLVM IR verification error: {}",
@@ -274,11 +317,17 @@ where
) )
})?; })?;
let bytecode = revive_linker::link(buffer.as_slice())?; let shared_object = revive_linker::link(buffer.as_slice())?;
self.debug_config
.dump_object(contract_path, &shared_object)?;
let polkavm_bytecode =
revive_linker::polkavm_linker(shared_object, !self.debug_config().emit_debug_info)?;
let build = match crate::polkavm::build_assembly_text( let build = match crate::polkavm::build_assembly_text(
contract_path, contract_path,
&bytecode, &polkavm_bytecode,
metadata_hash, metadata_hash,
self.debug_config(), self.debug_config(),
) { ) {
@@ -371,6 +420,15 @@ 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
@@ -381,49 +439,31 @@ 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,
mut linkage: Option<inkwell::module::Linkage>, 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);
if self.debug_info().is_some() {
self.builder().unset_current_debug_location();
let func_scope = match value.get_subprogram() {
None => {
let fn_name = value.get_name().to_str()?;
let scp = self.build_function_debug_info(fn_name, 0)?;
value.set_subprogram(scp);
scp
}
Some(scp) => scp,
};
self.push_debug_scope(func_scope.as_debug_info_scope());
self.set_debug_location(0, 0, Some(func_scope.as_debug_info_scope()))?;
}
let entry_block = self.llvm.append_basic_block(value, "entry"); let entry_block = self.llvm.append_basic_block(value, "entry");
let return_block = self.llvm.append_basic_block(value, "return"); let return_block = self.llvm.append_basic_block(value, "return");
@@ -434,12 +474,6 @@ 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(
@@ -460,13 +494,11 @@ 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());
self.pop_debug_scope();
Ok(function) Ok(function)
} }
@@ -482,15 +514,95 @@ where
.expect("Must be declared before use") .expect("Must be declared before use")
} }
/// Sets the current active function. /// Sets the current active function. If debug-info generation is enabled,
pub fn set_current_function(&mut self, name: &str) -> anyhow::Result<()> { /// constructs a debug-scope and pushes in on the scope-stack.
pub fn set_current_function(&mut self, name: &str, line: Option<u32>) -> anyhow::Result<()> {
let function = self.functions.get(name).cloned().ok_or_else(|| { let function = self.functions.get(name).cloned().ok_or_else(|| {
anyhow::anyhow!("Failed to activate an undeclared function `{}`", name) anyhow::anyhow!("Failed to activate an undeclared function `{}`", name)
})?; })?;
self.current_function = Some(function); self.current_function = Some(function);
if let Some(scope) = self.current_function().borrow().get_debug_scope() {
self.push_debug_scope(scope);
}
self.set_debug_location(line.unwrap_or_default(), 0, None)?;
Ok(()) Ok(())
} }
/// Builds a debug-info scope for a function.
pub fn build_function_debug_info(
&self,
name: &str,
line_no: u32,
) -> anyhow::Result<inkwell::debug_info::DISubprogram<'ctx>> {
let Some(debug_info) = self.debug_info() else {
anyhow::bail!("expected debug-info builders");
};
let builder = debug_info.builder();
let file = debug_info.compilation_unit().get_file();
let scope = file.as_debug_info_scope();
let flags = inkwell::debug_info::DIFlagsConstants::PUBLIC;
let return_type = debug_info.create_word_type(Some(flags))?.as_type();
let subroutine_type = builder.create_subroutine_type(file, Some(return_type), &[], flags);
Ok(builder.create_function(
scope,
name,
None,
file,
line_no,
subroutine_type,
false,
true,
1,
flags,
false,
))
}
/// Set the debug info location.
///
/// No-op if the emitting debug info is disabled.
///
/// If `scope` is `None` the top scope will be used.
pub fn set_debug_location(
&self,
line: u32,
column: u32,
scope: Option<DIScope<'ctx>>,
) -> anyhow::Result<()> {
let Some(debug_info) = self.debug_info() else {
return Ok(());
};
let scope = match scope {
Some(scp) => scp,
None => debug_info.top_scope().expect("expected a debug-info scope"),
};
let location =
debug_info
.builder()
.create_debug_location(self.llvm(), line, column, scope, None);
self.builder().set_current_debug_location(location);
Ok(())
}
/// Pushes a debug-info scope to the stack.
pub fn push_debug_scope(&self, scope: DIScope<'ctx>) {
if let Some(debug_info) = self.debug_info() {
debug_info.push_scope(scope);
}
}
/// Pops the top of the debug-info scope stack.
pub fn pop_debug_scope(&self) {
if let Some(debug_info) = self.debug_info() {
debug_info.pop_scope();
}
}
/// Pushes a new loop context to the stack. /// Pushes a new loop context to the stack.
pub fn push_loop( pub fn push_loop(
&mut self, &mut self,
@@ -524,10 +636,6 @@ 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(),
) )
@@ -564,9 +672,14 @@ where
.expect("The dependency manager is unset") .expect("The dependency manager is unset")
} }
/// Returns the debug info.
pub fn debug_info(&self) -> Option<&DebugInfo<'ctx>> {
self.debug_info.as_ref()
}
/// Returns the debug config reference. /// Returns the debug config reference.
pub fn debug_config(&self) -> Option<&DebugConfig> { pub fn debug_config(&self) -> &DebugConfig {
self.debug_config.as_ref() &self.debug_config
} }
/// Appends a new basic block to the current function. /// Appends a new basic block to the current function.
@@ -585,59 +698,70 @@ where
self.builder.get_insert_block().expect("Always exists") self.builder.get_insert_block().expect("Always exists")
} }
/// Builds a stack allocation instruction. /// Builds an aligned stack allocation at the function entry.
/// Sets the alignment to 128 bits. pub fn build_alloca_at_entry<T: BasicType<'ctx> + Clone + Copy>(
&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();
self.basic_block() pointer
.get_last_instruction() .as_instruction()
.expect("Always exists") .unwrap()
.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)
} }
/// Allocate an int of size `bit_length` on the stack. /// Truncate `address` to the ethereum address length and store it as bytes on the stack.
/// Returns the allocation pointer and the length pointer. /// The stack allocation will be at the function entry. Returns the stack pointer.
/// /// This helper should be used when passing address arguments to the runtime, ensuring correct size and endianness.
/// Useful helper for passing runtime API parameters on the stack. pub fn build_address_argument_store(
pub fn build_stack_parameter(
&self, &self,
bit_length: usize, address: inkwell::values::IntValue<'ctx>,
name: &str, ) -> anyhow::Result<Pointer<'ctx>> {
) -> (Pointer<'ctx>, Pointer<'ctx>) { let address_type = self.integer_type(revive_common::BIT_LENGTH_ETH_ADDRESS);
let buffer_pointer = self.build_alloca(self.integer_type(bit_length), name); let address_pointer = self.build_alloca_at_entry(address_type, "address_pointer");
let symbol = match bit_length { let address_truncated =
revive_common::BIT_LENGTH_WORD => GLOBAL_I256_SIZE, self.builder()
revive_common::BIT_LENGTH_ETH_ADDRESS => GLOBAL_I160_SIZE, .build_int_truncate(address, address_type, "address_truncated")?;
revive_common::BIT_LENGTH_BLOCK_NUMBER => GLOBAL_I64_SIZE, let address_swapped = self.build_byte_swap(address_truncated.into())?;
_ => panic!("invalid stack parameter bit width: {bit_length}"), self.build_store(address_pointer, address_swapped)?;
}; Ok(address_pointer)
let length_pointer = self.get_global(symbol).expect("should be declared");
(buffer_pointer, length_pointer.into())
} }
/// Load the integer at given pointer and zero extend it to the VM word size. /// Load the address at given pointer and zero extend it to the VM word size.
pub fn build_load_word( pub fn build_load_address(
&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 value = self.build_load( let address = self.build_byte_swap(self.build_load(pointer, "address_pointer")?)?;
pointer.cast(self.integer_type(bit_length)), Ok(self
&format!("load_{name}"), .builder()
)?; .build_int_z_extend(address.into_int_value(), self.word_type(), "address_zext")?
let value_extended = self.builder().build_int_z_extend( .into())
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.
@@ -674,31 +798,25 @@ 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( let storage_value_pointer =
pointer.value, self.build_alloca(self.word_type(), "storage_value_pointer");
self.word_type(), self.build_store(storage_value_pointer, self.word_const(0))?;
"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, storage_value_length_pointer) = self let storage_value_length_pointer =
.build_stack_parameter(revive_common::BIT_LENGTH_WORD, "storage_value_pointer"); self.build_alloca(self.xlen_type(), "storage_value_length_pointer");
self.build_store(
storage_value_length_pointer,
self.word_const(revive_common::BIT_LENGTH_WORD as u64),
)?;
let transient = pointer.address_space == AddressSpace::TransientStorage; let transient = pointer.address_space == AddressSpace::TransientStorage;
self.build_runtime_call( self.build_runtime_call(
runtime_api::imports::GET_STORAGE, revive_runtime_api::polkavm_imports::GET_STORAGE,
&[ &[
self.xlen_type().const_int(transient as u64, false).into(), self.xlen_type().const_int(transient as u64, false).into(),
storage_key_pointer_casted.into(), pointer.to_int(self).into(),
self.integer_const(crate::polkavm::XLEN, 32).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(),
], ],
@@ -766,18 +884,6 @@ 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,
@@ -785,19 +891,17 @@ 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(
runtime_api::imports::SET_STORAGE, revive_runtime_api::polkavm_imports::SET_STORAGE,
&[ &[
self.xlen_type().const_int(transient as u64, false).into(), self.xlen_type().const_int(transient as u64, false).into(),
storage_key_pointer_casted.into(), pointer.to_int(self).into(),
self.integer_const(crate::polkavm::XLEN, 32).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(),
], ],
@@ -905,13 +1009,29 @@ where
.copied() .copied()
.map(inkwell::values::BasicMetadataValueEnum::from) .map(inkwell::values::BasicMetadataValueEnum::from)
.collect::<Vec<_>>(), .collect::<Vec<_>>(),
&format!("runtime API call {name}"), &format!("runtime_api_{name}_return_value"),
) )
.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,
@@ -1012,7 +1132,7 @@ where
)?; )?;
self.build_runtime_call( self.build_runtime_call(
runtime_api::imports::RETURN, revive_runtime_api::polkavm_imports::RETURN,
&[flags.into(), offset_pointer.into(), length_pointer.into()], &[flags.into(), offset_pointer.into(), length_pointer.into()],
); );
self.build_unreachable(); self.build_unreachable();
@@ -1070,16 +1190,20 @@ where
Ok(truncated) Ok(truncated)
} }
/// Build a call to PolkaVM `sbrk` for extending the heap by `size`. /// Build a call to PolkaVM `sbrk` for extending the heap from offset 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(runtime_api::SBRK), self.runtime_api_method(revive_runtime_api::polkavm_imports::SBRK),
&[size.into()], &[offset.into(), size.into()],
"call_sbrk", "call_sbrk",
)? )?
.try_as_basic_value() .try_as_basic_value()
@@ -1088,14 +1212,29 @@ where
.into_pointer_value()) .into_pointer_value())
} }
/// Call PolkaVM `sbrk` for extending the heap by `size`, /// Build a call to PolkaVM `msize` for querying the linear memory 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<inkwell::values::PointerValue<'ctx>> { ) -> anyhow::Result<()> {
let end_of_memory = self.build_sbrk(size)?; let end_of_memory = self.build_sbrk(offset, 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,
@@ -1113,7 +1252,7 @@ where
self.set_basic_block(continue_block); self.set_basic_block(continue_block);
Ok(end_of_memory) Ok(())
} }
/// Returns a pointer to `offset` into the heap, allocating /// Returns a pointer to `offset` into the heap, allocating
@@ -1128,40 +1267,12 @@ 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],
@@ -1233,7 +1344,7 @@ where
self.llvm.custom_width_int_type(crate::polkavm::XLEN as u32) self.llvm.custom_width_int_type(crate::polkavm::XLEN as u32)
} }
/// Returns the register witdh sized type. /// Returns the sentinel pointer value.
pub fn sentinel_pointer(&self) -> Pointer<'ctx> { pub fn sentinel_pointer(&self) -> Pointer<'ctx> {
let sentinel_pointer = self let sentinel_pointer = self
.xlen_type() .xlen_type()
@@ -1282,12 +1393,11 @@ 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 mut argument_types: Vec<inkwell::types::BasicMetadataTypeEnum> = argument_types let 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)
@@ -1299,11 +1409,6 @@ 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),
@@ -1496,12 +1601,4 @@ 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 number of immutables values in the contract. /// Returns the current size of immutable 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
} }
@@ -15,7 +15,7 @@ pub fn create_context(
let module = llvm.create_module("test"); let module = llvm.create_module("test");
let optimizer = Optimizer::new(optimizer_settings); let optimizer = Optimizer::new(optimizer_settings);
Context::<DummyDependency>::new(llvm, module, optimizer, None, true, None) Context::<DummyDependency>::new(llvm, module, optimizer, None, true, Default::default())
} }
#[test] #[test]
@@ -8,28 +8,12 @@ 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) {
+115 -49
View File
@@ -5,14 +5,11 @@ 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;
static STATIC_CALL_FLAG: u32 = 0b0001_0000; const 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>,
@@ -29,66 +26,71 @@ pub fn call<'ctx, D>(
where where
D: Dependency + Clone, D: Dependency + Clone,
{ {
let address_pointer = context.build_alloca(context.word_type(), "address_ptr"); let address_pointer = context.build_address_argument_store(address)?;
context.build_store(address_pointer, address)?;
let value_pointer = if let Some(value) = value { let value = value.unwrap_or_else(|| context.word_const(0));
let value_pointer = context.build_alloca(context.value_type(), "value"); let value_pointer = context.build_alloca_at_entry(context.word_type(), "value_pointer");
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)?;
let gas = context // TODO: What to supply here? Is there a weight to gas?
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.get_global(crate::polkavm::GLOBAL_RETURN_DATA_SIZE)?; let output_length_pointer = context.build_alloca_at_entry(context.xlen_type(), "output_length");
context.build_store(output_length_pointer.into(), output_length)?; context.build_store(output_length_pointer, output_length)?;
let argument_pointer = pallet_contracts_pvm_llapi::calling_convention::Spill::new( let flags = if static_call {
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, u64::MAX).as_basic_value_enum(),
context.integer_const(64, u64::MAX).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(),
pallet_contracts_pvm_llapi::calling_convention::call(context.llvm()), argument_pointer.value,
"call_arguments", argument_type,
)? 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 = runtime_api::imports::CALL; let name = revive_runtime_api::polkavm_imports::CALL;
let arguments = context.builder().build_ptr_to_int( let argument_pointer = context.builder().build_ptr_to_int(
argument_pointer, argument_pointer.value,
context.xlen_type(), context.xlen_type(),
"argument_pointer", "call_argument_pointer",
)?; )?;
let success = context let success = context
.build_runtime_call(name, &[arguments.into()]) .build_runtime_call(name, &[argument_pointer.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();
let is_success = context.builder().build_int_compare( let is_success = context.builder().build_int_compare(
inkwell::IntPredicate::EQ, inkwell::IntPredicate::EQ,
success, success,
context.xlen_type().const_zero(), context.integer_const(revive_common::BIT_LENGTH_X64, 0),
"is_success", "is_success",
)?; )?;
@@ -100,20 +102,84 @@ where
#[allow(clippy::too_many_arguments)] #[allow(clippy::too_many_arguments)]
pub fn delegate_call<'ctx, D>( pub fn delegate_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>>, input_offset: inkwell::values::IntValue<'ctx>,
_input_offset: inkwell::values::IntValue<'ctx>, input_length: inkwell::values::IntValue<'ctx>,
_input_length: inkwell::values::IntValue<'ctx>, output_offset: inkwell::values::IntValue<'ctx>,
_output_offset: inkwell::values::IntValue<'ctx>, output_length: inkwell::values::IntValue<'ctx>,
_output_length: inkwell::values::IntValue<'ctx>,
_constants: Vec<Option<num::BigUint>>, _constants: Vec<Option<num::BigUint>>,
) -> anyhow::Result<inkwell::values::BasicValueEnum<'ctx>> ) -> anyhow::Result<inkwell::values::BasicValueEnum<'ctx>>
where where
D: Dependency + Clone, D: Dependency + Clone,
{ {
todo!() let address_pointer = context.build_address_argument_store(address)?;
let input_offset = context.safe_truncate_int_to_xlen(input_offset)?;
let input_length = context.safe_truncate_int_to_xlen(input_length)?;
let output_offset = context.safe_truncate_int_to_xlen(output_offset)?;
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
.builder()
.build_int_truncate(gas, context.integer_type(64), "gas")?;
let input_pointer = context.build_heap_gep(input_offset, input_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");
context.build_store(output_length_pointer, output_length)?;
let flags = context.xlen_type().const_int(0u64, false);
let argument_type = revive_runtime_api::calling_convention::delegate_call(context.llvm());
let argument_pointer = context.build_alloca_at_entry(argument_type, "delegate_call_arguments");
let arguments = &[
flags.as_basic_value_enum(),
address_pointer.value.as_basic_value_enum(),
context
.integer_const(revive_common::BIT_LENGTH_X64, u64::MAX)
.as_basic_value_enum(),
context
.integer_const(revive_common::BIT_LENGTH_X64, u64::MAX)
.as_basic_value_enum(),
context.sentinel_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(),
argument_pointer.value,
argument_type,
arguments,
)?;
let name = revive_runtime_api::polkavm_imports::DELEGATE_CALL;
let argument_pointer = context.builder().build_ptr_to_int(
argument_pointer.value,
context.xlen_type(),
"delegate_call_argument_pointer",
)?;
let success = context
.build_runtime_call(name, &[argument_pointer.into()])
.unwrap_or_else(|| panic!("{name} should return a value"))
.into_int_value();
let is_success = context.builder().build_int_compare(
inkwell::IntPredicate::EQ,
success,
context.integer_const(revive_common::BIT_LENGTH_X64, 0),
"is_success",
)?;
Ok(context
.builder()
.build_int_z_extend(is_success, context.word_type(), "success")?
.as_basic_value_enum())
} }
/// Translates the Yul `linkersymbol` instruction. /// Translates the Yul `linkersymbol` instruction.

Some files were not shown because too many files have changed in this diff Show More