diff --git a/Cargo.lock b/Cargo.lock index 4eb246995b1c..1a196172cdc8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3283,7 +3283,7 @@ dependencies = [ "regex", "serde_json", "similar", - "wasmparser 0.219.2", + "wasmparser 0.236.0", ] [[package]] diff --git a/compiler/rustc_codegen_llvm/src/allocator.rs b/compiler/rustc_codegen_llvm/src/allocator.rs index 2b5090ed6dba..23610aa856cb 100644 --- a/compiler/rustc_codegen_llvm/src/allocator.rs +++ b/compiler/rustc_codegen_llvm/src/allocator.rs @@ -8,11 +8,12 @@ use rustc_middle::bug; use rustc_middle::ty::TyCtxt; use rustc_session::config::{DebugInfo, OomStrategy}; use rustc_symbol_mangling::mangle_internal_symbol; +use smallvec::SmallVec; use crate::builder::SBuilder; use crate::declare::declare_simple_fn; use crate::llvm::{self, False, True, Type, Value}; -use crate::{SimpleCx, attributes, debuginfo}; +use crate::{SimpleCx, attributes, debuginfo, llvm_util}; pub(crate) unsafe fn codegen( tcx: TyCtxt<'_>, @@ -147,6 +148,20 @@ fn create_wrapper_function( llvm::Visibility::from_generic(tcx.sess.default_visibility()), ty, ); + + let mut attrs = SmallVec::<[_; 2]>::new(); + + let target_cpu = llvm_util::target_cpu(tcx.sess); + let target_cpu_attr = llvm::CreateAttrStringValue(cx.llcx, "target-cpu", target_cpu); + + let tune_cpu_attr = llvm_util::tune_cpu(tcx.sess) + .map(|tune_cpu| llvm::CreateAttrStringValue(cx.llcx, "tune-cpu", tune_cpu)); + + attrs.push(target_cpu_attr); + attrs.extend(tune_cpu_attr); + + attributes::apply_to_llfn(llfn, llvm::AttributePlace::Function, &attrs); + let no_return = if no_return { // -> ! DIFlagNoReturn let no_return = llvm::AttributeKind::NoReturn.create_attr(cx.llcx); diff --git a/src/tools/run-make-support/Cargo.toml b/src/tools/run-make-support/Cargo.toml index a4e7534137d5..250e0f65a9f4 100644 --- a/src/tools/run-make-support/Cargo.toml +++ b/src/tools/run-make-support/Cargo.toml @@ -17,7 +17,7 @@ object = "0.37" regex = "1.11" serde_json = "1.0" similar = "2.7" -wasmparser = { version = "0.219", default-features = false, features = ["std"] } +wasmparser = { version = "0.236", default-features = false, features = ["std", "features", "validate"] } # tidy-alphabetical-end # Shared with bootstrap and compiletest diff --git a/tests/run-make/wasm-unexpected-features/rmake.rs b/tests/run-make/wasm-unexpected-features/rmake.rs new file mode 100644 index 000000000000..142860c47ec0 --- /dev/null +++ b/tests/run-make/wasm-unexpected-features/rmake.rs @@ -0,0 +1,38 @@ +//@ only-wasm32-wasip1 + +use std::path::Path; + +use run_make_support::{cargo, path, rfs, target, wasmparser}; + +fn main() { + let target_dir = path("target"); + + cargo() + .args([ + "rustc", + "--manifest-path", + "wasm32_test/Cargo.toml", + "--profile", + "release", + "--target", + "wasm32-wasip1", + "-Zbuild-std=core,alloc,panic_abort", + "--", + "-Clink-arg=--import-memory", + "-Clinker-plugin-lto=on", + ]) + .env("RUSTFLAGS", "-Ctarget-cpu=mvp") + .env("CARGO_TARGET_DIR", &target_dir) + .run(); + + let wasm32_program_path = target_dir.join(target()).join("release").join("wasm32_program.wasm"); + verify_features(&wasm32_program_path); +} + +fn verify_features(path: &Path) { + eprintln!("verify {path:?}"); + let file = rfs::read(&path); + + let mut validator = wasmparser::Validator::new_with_features(wasmparser::WasmFeatures::WASM1); + validator.validate_all(&file).unwrap(); +} diff --git a/tests/run-make/wasm-unexpected-features/wasm32_test/Cargo.toml b/tests/run-make/wasm-unexpected-features/wasm32_test/Cargo.toml new file mode 100644 index 000000000000..9f1070e1c54c --- /dev/null +++ b/tests/run-make/wasm-unexpected-features/wasm32_test/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "wasm32_test" +version = "0.1.0" +edition = "2024" + +[lib] +crate-type = ["cdylib"] +name = "wasm32_program" + +[profile.release] +codegen-units = 1 +lto = "fat" +opt-level = "z" diff --git a/tests/run-make/wasm-unexpected-features/wasm32_test/src/lib.rs b/tests/run-make/wasm-unexpected-features/wasm32_test/src/lib.rs new file mode 100644 index 000000000000..123c2c1b6ca0 --- /dev/null +++ b/tests/run-make/wasm-unexpected-features/wasm32_test/src/lib.rs @@ -0,0 +1,34 @@ +#![no_std] + +extern crate alloc; + +use core::alloc::{GlobalAlloc, Layout}; +use core::mem::MaybeUninit; + +#[global_allocator] +static ALLOC: GlobalDlmalloc = GlobalDlmalloc; + +struct GlobalDlmalloc; + +unsafe impl GlobalAlloc for GlobalDlmalloc { + #[inline] + unsafe fn alloc(&self, _layout: Layout) -> *mut u8 { + core::ptr::null_mut() + } + + #[inline] + unsafe fn dealloc(&self, _ptr: *mut u8, _layout: Layout) {} +} + +#[used] +static mut BUF: MaybeUninit<[u8; 1024]> = MaybeUninit::uninit(); + +#[unsafe(no_mangle)] +extern "C" fn init() { + alloc::alloc::handle_alloc_error(Layout::new::<[u8; 64 * 1024]>()); +} + +#[panic_handler] +fn my_panic(_: &core::panic::PanicInfo) -> ! { + loop {} +}