Skip to content

Commit

Permalink
[pallet-contracts] Remove riscv support (paritytech#5665)
Browse files Browse the repository at this point in the history
  • Loading branch information
pgherveou authored Sep 11, 2024
1 parent 47cc599 commit 7d30806
Show file tree
Hide file tree
Showing 10 changed files with 21 additions and 414 deletions.
2 changes: 0 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions prdoc/pr_5665.prdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Schema: Polkadot SDK PRDoc Schema (prdoc) v1.0.0
# See doc at https://raw.githubusercontent.com/paritytech/polkadot-sdk/master/prdoc/schema_user.json

title: "[pallet-contracts] remove riscv support"

doc:
- audience: Runtime Dev
description: |
RISC-V support is now being built inside the new fork pallet-revive

crates:
- name: pallet-contracts
bump: patch
- name: pallet-contracts-fixtures
bump: patch
- name: pallet-contracts-uapi
bump: patch
4 changes: 0 additions & 4 deletions substrate/frame/contracts/fixtures/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,4 @@ parity-wasm = { workspace = true }
tempfile = { workspace = true }
toml = { workspace = true }
twox-hash = { workspace = true, default-features = true }
polkavm-linker = { workspace = true, optional = true }
anyhow = { workspace = true, default-features = true }

[features]
riscv = ["polkavm-linker"]
64 changes: 1 addition & 63 deletions substrate/frame/contracts/fixtures/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

//! Compile contracts to wasm and RISC-V binaries.
//! Compile contracts to wasm.
use anyhow::{bail, Context, Result};
use parity_wasm::elements::{deserialize_file, serialize_to_file, Internal};
use std::{
Expand Down Expand Up @@ -89,12 +89,6 @@ impl Entry {
fn out_wasm_filename(&self) -> String {
format!("{}.wasm", self.name())
}

/// Return the name of the RISC-V polkavm file.
#[cfg(feature = "riscv")]
fn out_riscv_filename(&self) -> String {
format!("{}.polkavm", self.name())
}
}

/// Collect all contract entries from the given source directory.
Expand Down Expand Up @@ -236,53 +230,6 @@ fn post_process_wasm(input_path: &Path, output_path: &Path) -> Result<()> {
serialize_to_file(output_path, module).map_err(Into::into)
}

/// Build contracts for RISC-V.
#[cfg(feature = "riscv")]
fn invoke_riscv_build(current_dir: &Path) -> Result<()> {
let encoded_rustflags = [
"-Crelocation-model=pie",
"-Clink-arg=--emit-relocs",
"-Clink-arg=--export-dynamic-symbol=__polkavm_symbol_export_hack__*",
]
.join("\x1f");

let build_res = Command::new(env::var("CARGO")?)
.current_dir(current_dir)
.env_clear()
.env("PATH", env::var("PATH").unwrap_or_default())
.env("CARGO_ENCODED_RUSTFLAGS", encoded_rustflags)
.env("RUSTUP_TOOLCHAIN", "rve-nightly")
.env("RUSTUP_HOME", env::var("RUSTUP_HOME").unwrap_or_default())
.args(["build", "--release", "--target=riscv32ema-unknown-none-elf"])
.output()
.expect("failed to execute process");

if build_res.status.success() {
return Ok(())
}

let stderr = String::from_utf8_lossy(&build_res.stderr);

if stderr.contains("'rve-nightly' is not installed") {
eprintln!("RISC-V toolchain is not installed.\nDownload and install toolchain from https://github.com/paritytech/rustc-rv32e-toolchain.");
eprintln!("{}", stderr);
} else {
eprintln!("{}", stderr);
}

bail!("Failed to build contracts");
}
/// Post-process the compiled wasm contracts.
#[cfg(feature = "riscv")]
fn post_process_riscv(input_path: &Path, output_path: &Path) -> Result<()> {
let mut config = polkavm_linker::Config::default();
config.set_strip(true);
let orig = fs::read(input_path).with_context(|| format!("Failed to read {:?}", input_path))?;
let linked = polkavm_linker::program_from_elf(config, orig.as_ref())
.map_err(|err| anyhow::format_err!("Failed to link polkavm program: {}", err))?;
fs::write(output_path, linked.as_bytes()).map_err(Into::into)
}

/// Write the compiled contracts to the given output directory.
fn write_output(build_dir: &Path, out_dir: &Path, entries: Vec<Entry>) -> Result<()> {
for entry in entries {
Expand All @@ -292,12 +239,6 @@ fn write_output(build_dir: &Path, out_dir: &Path, entries: Vec<Entry>) -> Result
&out_dir.join(&wasm_output),
)?;

#[cfg(feature = "riscv")]
post_process_riscv(
&build_dir.join("target/riscv32ema-unknown-none-elf/release").join(entry.name()),
&out_dir.join(entry.out_riscv_filename()),
)?;

entry.update_cache(out_dir)?;
}

Expand Down Expand Up @@ -347,9 +288,6 @@ fn main() -> Result<()> {

invoke_wasm_build(tmp_dir_path)?;

#[cfg(feature = "riscv")]
invoke_riscv_build(tmp_dir_path)?;

write_output(tmp_dir_path, &out_dir, entries)?;
Ok(())
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,6 @@ pub use uapi::{HostFn, HostFnImpl as api};
fn panic(_info: &core::panic::PanicInfo) -> ! {
#[cfg(target_arch = "wasm32")]
core::arch::wasm32::unreachable();

#[cfg(target_arch = "riscv32")]
// Safety: The unimp instruction is guaranteed to trap
unsafe {
core::arch::asm!("unimp");
core::hint::unreachable_unchecked();
}
}

/// Utility macro to read input passed to a contract.
Expand Down
2 changes: 0 additions & 2 deletions substrate/frame/contracts/fixtures/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,5 @@ mod test {
fn out_dir_should_have_compiled_mocks() {
let out_dir: std::path::PathBuf = env!("OUT_DIR").into();
assert!(out_dir.join("dummy.wasm").exists());
#[cfg(feature = "riscv")]
assert!(out_dir.join("dummy.polkavm").exists());
}
}
3 changes: 0 additions & 3 deletions substrate/frame/contracts/uapi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ codec = { features = [
"max-encoded-len",
], optional = true, workspace = true }

[target.'cfg(target_arch = "riscv32")'.dependencies]
polkavm-derive = { workspace = true }

[package.metadata.docs.rs]
default-target = ["wasm32-unknown-unknown"]

Expand Down
5 changes: 1 addition & 4 deletions substrate/frame/contracts/uapi/src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ use paste::paste;
#[cfg(target_arch = "wasm32")]
mod wasm32;

#[cfg(target_arch = "riscv32")]
mod riscv32;

macro_rules! hash_fn {
( $name:ident, $bytes:literal ) => {
paste! {
Expand Down Expand Up @@ -66,7 +63,7 @@ fn ptr_or_sentinel(data: &Option<&[u8]>) -> *const u8 {
/// Implements [`HostFn`] for each supported target architecture.
pub enum HostFnImpl {}

/// Defines all the host apis implemented by both wasm and RISC-V vms.
/// Defines all the host apis implemented by the wasm vm.
pub trait HostFn: private::Sealed {
/// Returns the number of times specified contract exists on the call stack. Delegated calls are
/// not counted as separate calls.
Expand Down
Loading

0 comments on commit 7d30806

Please sign in to comment.