Fix unsafe API change and unsafe external block

This commit is contained in:
Zhang Junyang 2025-12-08 20:52:16 +08:00 committed by Tate, Hongliang Tian
parent aae136865a
commit 2a6436d05f
22 changed files with 37 additions and 32 deletions

View File

@ -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); }

View File

@ -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);

View File

@ -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,

View File

@ -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();
}

View File

@ -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();
}

View File

@ -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::<KtestItem>();
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]>;
}

View File

@ -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();

View File

@ -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;
}

View File

@ -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.

View File

@ -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();

View File

@ -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();
}

View File

@ -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.

View File

@ -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];
}

View File

@ -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();

View File

@ -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();
}

View File

@ -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() -> !;
}

View File

@ -67,7 +67,7 @@ pub type DynamicCpuLocal<T> = CpuLocal<T, DynamicStorage<T>>;
pub type StaticCpuLocal<T> = CpuLocal<T, static_cpu_local::StaticStorage<T>>;
// These symbols are provided by the linker script.
extern "C" {
unsafe extern "C" {
fn __cpu_local_start();
fn __cpu_local_end();
}

View File

@ -8,7 +8,7 @@ struct ExTableItem {
recovery_inst_addr: Vaddr,
}
extern "C" {
unsafe extern "C" {
fn __ex_table();
fn __ex_table_end();
}

View File

@ -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();
}

View File

@ -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();
}

View File

@ -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;

View File

@ -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;