diff --git a/osdk/src/base_crate/main.rs.template b/osdk/src/base_crate/main.rs.template index bc51792e1..7ef117ed9 100644 --- a/osdk/src/base_crate/main.rs.template +++ b/osdk/src/base_crate/main.rs.template @@ -7,7 +7,7 @@ extern crate #TARGET_NAME#; #[panic_handler] fn panic(info: &core::panic::PanicInfo) -> ! { - extern "Rust" { + unsafe extern "Rust" { pub fn __ostd_panic_handler(info: &core::panic::PanicInfo) -> !; } unsafe { __ostd_panic_handler(info); } diff --git a/osdk/src/commands/test.rs b/osdk/src/commands/test.rs index 1413f6930..812846707 100644 --- a/osdk/src/commands/test.rs +++ b/osdk/src/commands/test.rs @@ -102,7 +102,6 @@ pub static KTEST_CRATE_WHITELIST: Option<&[&str]> = Some(&{:#?}); ActionChoice::Test, &["--cfg ktest", "-C panic=unwind"], ); - std::env::remove_var("RUSTFLAGS"); drop(dir_guard); bundle.run(config, ActionChoice::Test); diff --git a/ostd/libs/linux-bzimage/setup/src/x86/amd64_efi/efi.rs b/ostd/libs/linux-bzimage/setup/src/x86/amd64_efi/efi.rs index 1b5df67ed..e2779b7d5 100644 --- a/ostd/libs/linux-bzimage/setup/src/x86/amd64_efi/efi.rs +++ b/ostd/libs/linux-bzimage/setup/src/x86/amd64_efi/efi.rs @@ -14,7 +14,7 @@ pub(super) const PAGE_SIZE: u64 = 4096; /// SAFETY: The name does not collide with other symbols. #[unsafe(no_mangle)] -extern "sysv64" fn main_efi_common64( +unsafe extern "sysv64" fn main_efi_common64( handle: Handle, system_table: *const SystemTable, boot_params_ptr: *mut BootParams, diff --git a/ostd/libs/linux-bzimage/setup/src/x86/legacy_i386/alloc.rs b/ostd/libs/linux-bzimage/setup/src/x86/legacy_i386/alloc.rs index 8af67c3ba..8307b8661 100644 --- a/ostd/libs/linux-bzimage/setup/src/x86/legacy_i386/alloc.rs +++ b/ostd/libs/linux-bzimage/setup/src/x86/legacy_i386/alloc.rs @@ -24,7 +24,7 @@ pub(super) unsafe fn init(boot_params: &'static linux_boot_params::BootParams) { let mut used = core::array::from_fn(|_| 0..0); - extern "C" { + unsafe extern "C" { fn __executable_start(); fn __executable_end(); } diff --git a/ostd/libs/linux-bzimage/setup/src/x86/mod.rs b/ostd/libs/linux-bzimage/setup/src/x86/mod.rs index 0bead8a87..df2ff1352 100644 --- a/ostd/libs/linux-bzimage/setup/src/x86/mod.rs +++ b/ostd/libs/linux-bzimage/setup/src/x86/mod.rs @@ -30,7 +30,7 @@ pub fn image_load_offset() -> isize { /// The load address of the `entry_legacy32` symbol specified in the linker script. const CODE32_START: isize = 0x100000; - extern "C" { + unsafe extern "C" { fn entry_legacy32(); } @@ -44,7 +44,7 @@ global_asm!( /// Returns an immutable slice containing the payload (i.e., the kernel). fn payload() -> &'static [u8] { - extern "C" { + unsafe extern "C" { fn __payload_start(); fn __payload_end(); } diff --git a/ostd/libs/ostd-test/src/lib.rs b/ostd/libs/ostd-test/src/lib.rs index aa75f9270..7f84037d1 100644 --- a/ostd/libs/ostd-test/src/lib.rs +++ b/ostd/libs/ostd-test/src/lib.rs @@ -95,6 +95,7 @@ pub enum KtestError { /// The information of the unit test. #[derive(Clone, PartialEq, Debug)] +#[repr(C)] pub struct KtestItemInfo { /// The path of the module, not including the function name. /// @@ -112,7 +113,8 @@ pub struct KtestItemInfo { pub col: usize, } -#[derive(Clone, PartialEq, Debug)] +#[derive(Clone, Debug)] +#[repr(C)] pub struct KtestItem { fn_: fn() -> (), should_panic: (bool, Option<&'static str>), @@ -181,14 +183,18 @@ impl KtestItem { macro_rules! ktest_array { () => {{ - extern "C" { + unsafe extern "C" { fn __ktest_array(); fn __ktest_array_end(); } let item_size = size_of::(); - let l = (__ktest_array_end as usize - __ktest_array as usize) / item_size; - // SAFETY: __ktest_array is a static section consisting of KtestItem. - unsafe { core::slice::from_raw_parts(__ktest_array as *const KtestItem, l) } + let array_ptr = __ktest_array as *const () as *const KtestItem; + let array_end_ptr = __ktest_array_end as *const () as *const KtestItem; + // SAFETY: The pointer arithmetic is valid since both pointers point to + // the same section. + let l = unsafe { array_end_ptr.offset_from(array_ptr) as usize } / item_size; + // SAFETY: `__ktest_array` is a static section consisting of `KtestItem`s. + unsafe { core::slice::from_raw_parts(array_ptr, l) } }}; } @@ -224,7 +230,7 @@ impl core::iter::Iterator for KtestIter { // The whitelists that will be generated by the OSDK as static consts. // They deliver the target tests that the user wants to run. -extern "Rust" { +unsafe extern "Rust" { static KTEST_TEST_WHITELIST: Option<&'static [&'static str]>; static KTEST_CRATE_WHITELIST: Option<&'static [&'static str]>; } diff --git a/ostd/src/arch/loongarch/trap/mod.rs b/ostd/src/arch/loongarch/trap/mod.rs index 2199db699..07fbc8b49 100644 --- a/ostd/src/arch/loongarch/trap/mod.rs +++ b/ostd/src/arch/loongarch/trap/mod.rs @@ -38,7 +38,7 @@ pub(crate) unsafe fn init_on_cpu() { /// Handle traps (only from kernel). // SAFETY: The name does not collide with other symbols. #[unsafe(no_mangle)] -extern "C" fn trap_handler(f: &mut TrapFrame) { +unsafe extern "C" fn trap_handler(f: &mut TrapFrame) { let cause = estat::read().cause(); let badi = loongArch64::register::badi::read().raw(); let badv = loongArch64::register::badv::read().vaddr(); diff --git a/ostd/src/arch/riscv/boot/smp.rs b/ostd/src/arch/riscv/boot/smp.rs index 128714a00..c17c09757 100644 --- a/ostd/src/arch/riscv/boot/smp.rs +++ b/ostd/src/arch/riscv/boot/smp.rs @@ -116,7 +116,7 @@ unsafe fn bringup_ap(hart_id: u32) { /// /// The caller must ensure that `__ap_boot_info_array_pointer` is safe to write. unsafe fn fill_boot_info_ptr(info_ptr: *const PerApRawInfo) { - extern "C" { + unsafe extern "C" { static mut __ap_boot_info_array_pointer: *const PerApRawInfo; } @@ -132,7 +132,7 @@ unsafe fn fill_boot_info_ptr(info_ptr: *const PerApRawInfo) { /// /// The caller must ensure that `__ap_boot_page_table_pointer` is safe to write. unsafe fn fill_boot_page_table_ptr(pt_ptr: Paddr) { - extern "C" { + unsafe extern "C" { static mut __ap_boot_page_table_pointer: Paddr; } diff --git a/ostd/src/arch/riscv/mm/util.rs b/ostd/src/arch/riscv/mm/util.rs index e86a18ec0..d4c729c84 100644 --- a/ostd/src/arch/riscv/mm/util.rs +++ b/ostd/src/arch/riscv/mm/util.rs @@ -8,7 +8,7 @@ core::arch::global_asm!(include_str!("memset_fallible.S"), SSTATUS_SUM = const S core::arch::global_asm!(include_str!("atomic_load_fallible.S"), SSTATUS_SUM = const SSTATUS_SUM); core::arch::global_asm!(include_str!("atomic_cmpxchg_fallible.S"), SSTATUS_SUM = const SSTATUS_SUM); -extern "C" { +unsafe extern "C" { /// Copies `size` bytes from `src` to `dst`. This function works with exception handling /// and can recover from page fault. /// Returns number of bytes that failed to copy. diff --git a/ostd/src/arch/riscv/trap/mod.rs b/ostd/src/arch/riscv/trap/mod.rs index 88d7e4e89..4dbef13dd 100644 --- a/ostd/src/arch/riscv/trap/mod.rs +++ b/ostd/src/arch/riscv/trap/mod.rs @@ -42,7 +42,7 @@ pub(crate) unsafe fn init_on_cpu() { /// Handle traps (only from kernel). // SAFETY: The name does not collide with other symbols. #[unsafe(no_mangle)] -extern "C" fn trap_handler(f: &mut TrapFrame) { +unsafe extern "C" fn trap_handler(f: &mut TrapFrame) { fn enable_local_if(cond: bool) { if cond { enable_local(); diff --git a/ostd/src/arch/x86/boot/smp.rs b/ostd/src/arch/x86/boot/smp.rs index 553e64d3a..b6b284d96 100644 --- a/ostd/src/arch/x86/boot/smp.rs +++ b/ostd/src/arch/x86/boot/smp.rs @@ -174,7 +174,7 @@ unsafe fn copy_ap_boot_code() { /// /// The caller must ensure the pointer to be filled is valid to write. unsafe fn fill_boot_info_ptr(info_ptr: *const PerApRawInfo) { - extern "C" { + unsafe extern "C" { static mut __ap_boot_info_array_pointer: *const PerApRawInfo; } @@ -188,7 +188,7 @@ unsafe fn fill_boot_info_ptr(info_ptr: *const PerApRawInfo) { /// /// The caller must ensure the pointer to be filled is valid to write. unsafe fn fill_boot_pt_ptr(pt_ptr: Paddr) { - extern "C" { + unsafe extern "C" { static mut __boot_page_table_pointer: u32; } @@ -201,7 +201,7 @@ unsafe fn fill_boot_pt_ptr(pt_ptr: Paddr) { } // The symbols are defined in the linker script. -extern "C" { +unsafe extern "C" { fn __ap_boot_start(); fn __ap_boot_end(); } @@ -218,7 +218,7 @@ unsafe fn wake_up_aps_via_mailbox(num_cpus: u32) { use crate::arch::kernel::acpi::AcpiMemoryHandler; // The symbols are defined in `ap_boot.S`. - extern "C" { + unsafe extern "C" { fn ap_boot_from_real_mode(); fn ap_boot_from_long_mode(); } diff --git a/ostd/src/arch/x86/mm/util.rs b/ostd/src/arch/x86/mm/util.rs index 951bafd9d..08777ad49 100644 --- a/ostd/src/arch/x86/mm/util.rs +++ b/ostd/src/arch/x86/mm/util.rs @@ -6,7 +6,7 @@ core::arch::global_asm!(include_str!("memset_fallible.S")); core::arch::global_asm!(include_str!("atomic_load_fallible.S")); core::arch::global_asm!(include_str!("atomic_cmpxchg_fallible.S")); -extern "C" { +unsafe extern "C" { /// Copies `size` bytes from `src` to `dst`. This function works with exception handling /// and can recover from page fault. /// Returns number of bytes that failed to copy. diff --git a/ostd/src/arch/x86/trap/idt.rs b/ostd/src/arch/x86/trap/idt.rs index 9abb780ee..b4b44422e 100644 --- a/ostd/src/arch/x86/trap/idt.rs +++ b/ostd/src/arch/x86/trap/idt.rs @@ -16,7 +16,7 @@ global_asm!(include_str!("trap.S")); const NUM_INTERRUPTS: usize = 256; -extern "C" { +unsafe extern "C" { #[link_name = "trap_handler_table"] static VECTORS: [usize; NUM_INTERRUPTS]; } diff --git a/ostd/src/arch/x86/trap/mod.rs b/ostd/src/arch/x86/trap/mod.rs index eb40d0b9a..47d938296 100644 --- a/ostd/src/arch/x86/trap/mod.rs +++ b/ostd/src/arch/x86/trap/mod.rs @@ -133,7 +133,7 @@ pub(super) struct RawUserContext { /// Handle traps (only from kernel). // SAFETY: The name does not collide with other symbols. #[unsafe(no_mangle)] -extern "sysv64" fn trap_handler(f: &mut TrapFrame) { +unsafe extern "sysv64" fn trap_handler(f: &mut TrapFrame) { fn enable_local_if(cond: bool) { if cond { enable_local(); diff --git a/ostd/src/boot/memory_region.rs b/ostd/src/boot/memory_region.rs index 2211d7d4f..fd4b21bf0 100644 --- a/ostd/src/boot/memory_region.rs +++ b/ostd/src/boot/memory_region.rs @@ -78,7 +78,7 @@ impl MemoryRegion { /// we need to explicitly construct and append this memory region. pub fn kernel() -> Self { // These are physical addresses provided by the linker script. - extern "C" { + unsafe extern "C" { fn __kernel_start(); fn __kernel_end(); } diff --git a/ostd/src/boot/mod.rs b/ostd/src/boot/mod.rs index dbb1622b8..91dd9b81d 100644 --- a/ostd/src/boot/mod.rs +++ b/ostd/src/boot/mod.rs @@ -122,7 +122,7 @@ pub(crate) fn init_after_heap() { pub(crate) fn call_ostd_main() -> ! { // The entry point of kernel code, which should be defined by the package that // uses OSTD. - extern "Rust" { + unsafe extern "Rust" { fn __ostd_main() -> !; } diff --git a/ostd/src/cpu/local/mod.rs b/ostd/src/cpu/local/mod.rs index 081199a54..f25b3d361 100644 --- a/ostd/src/cpu/local/mod.rs +++ b/ostd/src/cpu/local/mod.rs @@ -67,7 +67,7 @@ pub type DynamicCpuLocal = CpuLocal>; pub type StaticCpuLocal = CpuLocal>; // These symbols are provided by the linker script. -extern "C" { +unsafe extern "C" { fn __cpu_local_start(); fn __cpu_local_end(); } diff --git a/ostd/src/ex_table.rs b/ostd/src/ex_table.rs index cebbfbf2d..c2bf0e42d 100644 --- a/ostd/src/ex_table.rs +++ b/ostd/src/ex_table.rs @@ -8,7 +8,7 @@ struct ExTableItem { recovery_inst_addr: Vaddr, } -extern "C" { +unsafe extern "C" { fn __ex_table(); fn __ex_table_end(); } diff --git a/ostd/src/io/io_port/allocator.rs b/ostd/src/io/io_port/allocator.rs index f43600b47..b316c61fb 100644 --- a/ostd/src/io/io_port/allocator.rs +++ b/ostd/src/io/io_port/allocator.rs @@ -80,7 +80,7 @@ pub(in crate::io) unsafe fn init() { // SAFETY: `MAX_IO_PORT` is guaranteed not to exceed the maximum value specified by architecture. let mut allocator = IdAlloc::with_capacity(crate::arch::io::MAX_IO_PORT as usize); - extern "C" { + unsafe extern "C" { fn __sensitive_io_ports_start(); fn __sensitive_io_ports_end(); } diff --git a/ostd/src/lib.rs b/ostd/src/lib.rs index d61e5cd4a..770c6b7b7 100644 --- a/ostd/src/lib.rs +++ b/ostd/src/lib.rs @@ -143,7 +143,7 @@ pub(crate) static IN_BOOTSTRAP_CONTEXT: AtomicBool = AtomicBool::new(true); /// The component system uses this function to call the initialization functions of /// the components. fn invoke_ffi_init_funcs() { - extern "C" { + unsafe extern "C" { fn __sinit_array(); fn __einit_array(); } diff --git a/ostd/src/mm/frame/allocator.rs b/ostd/src/mm/frame/allocator.rs index 916917ee2..c5033712c 100644 --- a/ostd/src/mm/frame/allocator.rs +++ b/ostd/src/mm/frame/allocator.rs @@ -175,7 +175,7 @@ pub trait GlobalFrameAllocator: Sync { fn add_free_memory(&self, addr: Paddr, size: usize); } -extern "Rust" { +unsafe extern "Rust" { /// The global frame allocator's reference exported by /// [`crate::global_frame_allocator`]. static __GLOBAL_FRAME_ALLOCATOR_REF: &'static dyn GlobalFrameAllocator; diff --git a/ostd/src/mm/heap/mod.rs b/ostd/src/mm/heap/mod.rs index 6718f67f3..0afbeedd0 100644 --- a/ostd/src/mm/heap/mod.rs +++ b/ostd/src/mm/heap/mod.rs @@ -51,7 +51,7 @@ pub trait GlobalHeapAllocator: Sync { fn dealloc(&self, slot: HeapSlot) -> Result<(), AllocError>; } -extern "Rust" { +unsafe extern "Rust" { /// The reference to the global heap allocator generated by the /// [`crate::global_heap_allocator`] attribute. static __GLOBAL_HEAP_ALLOCATOR_REF: &'static dyn GlobalHeapAllocator;