From f978932903cac8cf508ef78b8133fafd9fe5a5b8 Mon Sep 17 00:00:00 2001 From: StackOverflowExcept1on <109800286+StackOverflowExcept1on@users.noreply.github.com> Date: Tue, 12 Aug 2025 00:39:24 +0300 Subject: [PATCH 1/2] fix(compiler/rustc_codegen_llvm): apply `target-cpu` attribute --- Cargo.lock | 2 +- compiler/rustc_codegen_llvm/src/allocator.rs | 17 ++++++++- src/tools/run-make-support/Cargo.toml | 2 +- .../wasm-unexpected-features/rmake.rs | 38 +++++++++++++++++++ .../wasm32_test/Cargo.toml | 13 +++++++ .../wasm32_test/src/lib.rs | 34 +++++++++++++++++ 6 files changed, 103 insertions(+), 3 deletions(-) create mode 100644 tests/run-make/wasm-unexpected-features/rmake.rs create mode 100644 tests/run-make/wasm-unexpected-features/wasm32_test/Cargo.toml create mode 100644 tests/run-make/wasm-unexpected-features/wasm32_test/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index 4eb246995b1ca..1a196172cdc80 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 2b5090ed6dbab..23610aa856cb1 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 a4e7534137d5e..250e0f65a9f44 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 0000000000000..142860c47ec0d --- /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 0000000000000..9f1070e1c54c3 --- /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 0000000000000..123c2c1b6ca0c --- /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 {} +} From 3a250b79390e9d0258ae463c875aac6c643956db Mon Sep 17 00:00:00 2001 From: StackOverflowExcept1on <109800286+StackOverflowExcept1on@users.noreply.github.com> Date: Thu, 14 Aug 2025 12:26:51 +0300 Subject: [PATCH 2/2] rewrite test with `#![no_core]` --- .../run-make/wasm-unexpected-features/foo.rs | 43 +++++++++++++++++++ .../wasm-unexpected-features/rmake.rs | 32 +++++--------- .../wasm32_test/Cargo.toml | 13 ------ .../wasm32_test/src/lib.rs | 34 --------------- 4 files changed, 53 insertions(+), 69 deletions(-) create mode 100644 tests/run-make/wasm-unexpected-features/foo.rs delete mode 100644 tests/run-make/wasm-unexpected-features/wasm32_test/Cargo.toml delete mode 100644 tests/run-make/wasm-unexpected-features/wasm32_test/src/lib.rs diff --git a/tests/run-make/wasm-unexpected-features/foo.rs b/tests/run-make/wasm-unexpected-features/foo.rs new file mode 100644 index 0000000000000..5c7aa2d619046 --- /dev/null +++ b/tests/run-make/wasm-unexpected-features/foo.rs @@ -0,0 +1,43 @@ +#![no_core] +#![crate_type = "cdylib"] +#![feature(no_core, lang_items, allocator_internals, rustc_attrs)] +#![needs_allocator] +#![allow(internal_features)] + +#[rustc_std_internal_symbol] +unsafe fn __rust_alloc(_size: usize, _align: usize) -> *mut u8 { + 0 as *mut u8 +} + +unsafe extern "Rust" { + #[rustc_std_internal_symbol] + fn __rust_alloc_error_handler(size: usize, align: usize) -> !; +} + +#[used] +static mut BUF: [u8; 1024] = [0; 1024]; + +#[unsafe(no_mangle)] +extern "C" fn init() { + unsafe { + __rust_alloc_error_handler(0, 0); + } +} + +mod minicore { + #[lang = "pointee_sized"] + pub trait PointeeSized {} + + #[lang = "meta_sized"] + pub trait MetaSized: PointeeSized {} + + #[lang = "sized"] + pub trait Sized: MetaSized {} + + #[lang = "copy"] + pub trait Copy {} + impl Copy for u8 {} + + #[lang = "drop_in_place"] + fn drop_in_place(_: *mut T) {} +} diff --git a/tests/run-make/wasm-unexpected-features/rmake.rs b/tests/run-make/wasm-unexpected-features/rmake.rs index 142860c47ec0d..416b5ef4caa3d 100644 --- a/tests/run-make/wasm-unexpected-features/rmake.rs +++ b/tests/run-make/wasm-unexpected-features/rmake.rs @@ -2,31 +2,19 @@ use std::path::Path; -use run_make_support::{cargo, path, rfs, target, wasmparser}; +use run_make_support::{rfs, rustc, 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) + rustc() + .input("foo.rs") + .target("wasm32-wasip1") + .target_cpu("mvp") + .opt_level("z") + .lto("fat") + .linker_plugin_lto("on") + .link_arg("--import-memory") .run(); - - let wasm32_program_path = target_dir.join(target()).join("release").join("wasm32_program.wasm"); - verify_features(&wasm32_program_path); + verify_features(Path::new("foo.wasm")); } fn verify_features(path: &Path) { diff --git a/tests/run-make/wasm-unexpected-features/wasm32_test/Cargo.toml b/tests/run-make/wasm-unexpected-features/wasm32_test/Cargo.toml deleted file mode 100644 index 9f1070e1c54c3..0000000000000 --- a/tests/run-make/wasm-unexpected-features/wasm32_test/Cargo.toml +++ /dev/null @@ -1,13 +0,0 @@ -[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 deleted file mode 100644 index 123c2c1b6ca0c..0000000000000 --- a/tests/run-make/wasm-unexpected-features/wasm32_test/src/lib.rs +++ /dev/null @@ -1,34 +0,0 @@ -#![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 {} -}