Skip to content

Commit a6e76cb

Browse files
jannauhoshinolina
authored andcommitted
Revert "rust: macros: fix soundness issue in module! macro"
This reverts commit 7044dcf. Signed-off-by: Janne Grunau <j@jannau.net>
1 parent 9ba8bb2 commit a6e76cb

1 file changed

Lines changed: 76 additions & 116 deletions

File tree

rust/macros/module.rs

Lines changed: 76 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,17 @@ pub(crate) fn module(ts: TokenStream) -> TokenStream {
536536
/// Used by the printing macros, e.g. [`info!`].
537537
const __LOG_PREFIX: &[u8] = b\"{name}\\0\";
538538
539+
/// The \"Rust loadable module\" mark.
540+
//
541+
// This may be best done another way later on, e.g. as a new modinfo
542+
// key or a new section. For the moment, keep it simple.
543+
#[cfg(MODULE)]
544+
#[doc(hidden)]
545+
#[used]
546+
static __IS_RUST_MODULE: () = ();
547+
548+
static mut __MOD: Option<{type_}> = None;
549+
539550
// SAFETY: `__this_module` is constructed by the kernel at load time and will not be
540551
// freed until the module is unloaded.
541552
#[cfg(MODULE)]
@@ -547,133 +558,82 @@ pub(crate) fn module(ts: TokenStream) -> TokenStream {
547558
kernel::ThisModule::from_ptr(core::ptr::null_mut())
548559
}};
549560
550-
// Double nested modules, since then nobody can access the public items inside.
551-
mod __module_init {{
552-
mod __module_init {{
553-
use super::super::{type_};
554-
555-
/// The \"Rust loadable module\" mark.
556-
//
557-
// This may be best done another way later on, e.g. as a new modinfo
558-
// key or a new section. For the moment, keep it simple.
559-
#[cfg(MODULE)]
560-
#[doc(hidden)]
561-
#[used]
562-
static __IS_RUST_MODULE: () = ();
563-
564-
static mut __MOD: Option<{type_}> = None;
565-
566-
// Loadable modules need to export the `{{init,cleanup}}_module` identifiers.
567-
/// # Safety
568-
///
569-
/// This function must not be called after module initialization, because it may be
570-
/// freed after that completes.
571-
#[cfg(MODULE)]
572-
#[doc(hidden)]
573-
#[no_mangle]
574-
#[link_section = \".init.text\"]
575-
pub unsafe extern \"C\" fn init_module() -> core::ffi::c_int {{
576-
// SAFETY: This function is inaccessible to the outside due to the double
577-
// module wrapping it. It is called exactly once by the C side via its
578-
// unique name.
579-
unsafe {{ __init() }}
580-
}}
561+
// Loadable modules need to export the `{{init,cleanup}}_module` identifiers.
562+
/// # Safety
563+
///
564+
/// This function must not be called after module initialization, because it may be
565+
/// freed after that completes.
566+
#[cfg(MODULE)]
567+
#[doc(hidden)]
568+
#[no_mangle]
569+
#[link_section = \".init.text\"]
570+
pub unsafe extern \"C\" fn init_module() -> core::ffi::c_int {{
571+
__init()
572+
}}
581573
582-
#[cfg(MODULE)]
583-
#[doc(hidden)]
584-
#[no_mangle]
585-
pub extern \"C\" fn cleanup_module() {{
586-
// SAFETY:
587-
// - This function is inaccessible to the outside due to the double
588-
// module wrapping it. It is called exactly once by the C side via its
589-
// unique name,
590-
// - furthermore it is only called after `init_module` has returned `0`
591-
// (which delegates to `__init`).
592-
unsafe {{ __exit() }}
593-
}}
574+
#[cfg(MODULE)]
575+
#[doc(hidden)]
576+
#[no_mangle]
577+
pub extern \"C\" fn cleanup_module() {{
578+
__exit()
579+
}}
594580
595-
// Built-in modules are initialized through an initcall pointer
596-
// and the identifiers need to be unique.
597-
#[cfg(not(MODULE))]
598-
#[cfg(not(CONFIG_HAVE_ARCH_PREL32_RELOCATIONS))]
599-
#[doc(hidden)]
600-
#[link_section = \"{initcall_section}\"]
601-
#[used]
602-
pub static __{name}_initcall: extern \"C\" fn() -> core::ffi::c_int = __{name}_init;
603-
604-
#[cfg(not(MODULE))]
605-
#[cfg(CONFIG_HAVE_ARCH_PREL32_RELOCATIONS)]
606-
core::arch::global_asm!(
607-
r#\".section \"{initcall_section}\", \"a\"
608-
__{name}_initcall:
609-
.long __{name}_init - .
610-
.previous
611-
\"#
612-
);
613-
614-
#[cfg(not(MODULE))]
615-
#[doc(hidden)]
616-
#[no_mangle]
617-
pub extern \"C\" fn __{name}_init() -> core::ffi::c_int {{
618-
// SAFETY: This function is inaccessible to the outside due to the double
619-
// module wrapping it. It is called exactly once by the C side via its
620-
// placement above in the initcall section.
621-
unsafe {{ __init() }}
622-
}}
581+
// Built-in modules are initialized through an initcall pointer
582+
// and the identifiers need to be unique.
583+
#[cfg(not(MODULE))]
584+
#[cfg(not(CONFIG_HAVE_ARCH_PREL32_RELOCATIONS))]
585+
#[doc(hidden)]
586+
#[link_section = \"{initcall_section}\"]
587+
#[used]
588+
pub static __{name}_initcall: extern \"C\" fn() -> core::ffi::c_int = __{name}_init;
623589
624-
#[cfg(not(MODULE))]
625-
#[doc(hidden)]
626-
#[no_mangle]
627-
pub extern \"C\" fn __{name}_exit() {{
628-
// SAFETY:
629-
// - This function is inaccessible to the outside due to the double
630-
// module wrapping it. It is called exactly once by the C side via its
631-
// unique name,
632-
// - furthermore it is only called after `__{name}_init` has returned `0`
633-
// (which delegates to `__init`).
634-
unsafe {{ __exit() }}
635-
}}
590+
#[cfg(not(MODULE))]
591+
#[cfg(CONFIG_HAVE_ARCH_PREL32_RELOCATIONS)]
592+
core::arch::global_asm!(
593+
r#\".section \"{initcall_section}\", \"a\"
594+
__{name}_initcall:
595+
.long __{name}_init - .
596+
.previous
597+
\"#
598+
);
636599
637-
/// # Safety
638-
///
639-
/// This function must only be called once.
640-
unsafe fn __init() -> core::ffi::c_int {{
641-
match <{type_} as kernel::Module>::init(kernel::c_str!(\"{name}\"), &THIS_MODULE) {{
642-
Ok(m) => {{
643-
// SAFETY: No data race, since `__MOD` can only be accessed by this
644-
// module and there only `__init` and `__exit` access it. These
645-
// functions are only called once and `__exit` cannot be called
646-
// before or during `__init`.
647-
unsafe {{
648-
__MOD = Some(m);
649-
}}
650-
return 0;
651-
}}
652-
Err(e) => {{
653-
return e.to_errno();
654-
}}
655-
}}
656-
}}
600+
#[cfg(not(MODULE))]
601+
#[doc(hidden)]
602+
#[no_mangle]
603+
pub extern \"C\" fn __{name}_init() -> core::ffi::c_int {{
604+
__init()
605+
}}
657606
658-
/// # Safety
659-
///
660-
/// This function must
661-
/// - only be called once,
662-
/// - be called after `__init` has been called and returned `0`.
663-
unsafe fn __exit() {{
664-
// SAFETY: No data race, since `__MOD` can only be accessed by this module
665-
// and there only `__init` and `__exit` access it. These functions are only
666-
// called once and `__init` was already called.
607+
#[cfg(not(MODULE))]
608+
#[doc(hidden)]
609+
#[no_mangle]
610+
pub extern \"C\" fn __{name}_exit() {{
611+
__exit()
612+
}}
613+
614+
fn __init() -> core::ffi::c_int {{
615+
match <{type_} as kernel::Module>::init(kernel::c_str!(\"{name}\"), &THIS_MODULE) {{
616+
Ok(m) => {{
667617
unsafe {{
668-
// Invokes `drop()` on `__MOD`, which should be used for cleanup.
669-
__MOD = None;
618+
__MOD = Some(m);
670619
}}
620+
return 0;
621+
}}
622+
Err(e) => {{
623+
return e.to_errno();
671624
}}
625+
}}
626+
}}
672627
673-
{modinfo}
674-
{generated_array_types}
628+
fn __exit() {{
629+
unsafe {{
630+
// Invokes `drop()` on `__MOD`, which should be used for cleanup.
631+
__MOD = None;
675632
}}
676633
}}
634+
635+
{modinfo}
636+
{generated_array_types}
677637
",
678638
type_ = info.type_,
679639
name = info.name,

0 commit comments

Comments
 (0)