1
Fork 0

Report unsafe for overriding link sections

This commit is contained in:
5225225 2022-05-16 18:04:54 +01:00
parent c52b9c10bf
commit a42a7a3eb9
3 changed files with 64 additions and 16 deletions

View file

@ -338,6 +338,17 @@ impl UnsafeCode {
.emit(); .emit();
}) })
} }
fn report_overridden_symbol_section(&self, cx: &EarlyContext<'_>, span: Span, msg: &str) {
self.report_unsafe(cx, span, |lint| {
lint.build(msg)
.note(
"the program's behavior with overridden link sections on items is unpredictable \
and Rust cannot provide guarantees when you manually override them",
)
.emit();
})
}
} }
impl EarlyLintPass for UnsafeCode { impl EarlyLintPass for UnsafeCode {
@ -385,6 +396,7 @@ impl EarlyLintPass for UnsafeCode {
"declaration of a `no_mangle` function", "declaration of a `no_mangle` function",
); );
} }
if let Some(attr) = cx.sess().find_by_name(&it.attrs, sym::export_name) { if let Some(attr) = cx.sess().find_by_name(&it.attrs, sym::export_name) {
self.report_overridden_symbol_name( self.report_overridden_symbol_name(
cx, cx,
@ -392,6 +404,14 @@ impl EarlyLintPass for UnsafeCode {
"declaration of a function with `export_name`", "declaration of a function with `export_name`",
); );
} }
if let Some(attr) = cx.sess().find_by_name(&it.attrs, sym::link_section) {
self.report_overridden_symbol_section(
cx,
attr.span,
"declaration of a function with `link_section`",
);
}
} }
ast::ItemKind::Static(..) => { ast::ItemKind::Static(..) => {
@ -402,6 +422,7 @@ impl EarlyLintPass for UnsafeCode {
"declaration of a `no_mangle` static", "declaration of a `no_mangle` static",
); );
} }
if let Some(attr) = cx.sess().find_by_name(&it.attrs, sym::export_name) { if let Some(attr) = cx.sess().find_by_name(&it.attrs, sym::export_name) {
self.report_overridden_symbol_name( self.report_overridden_symbol_name(
cx, cx,
@ -409,6 +430,14 @@ impl EarlyLintPass for UnsafeCode {
"declaration of a static with `export_name`", "declaration of a static with `export_name`",
); );
} }
if let Some(attr) = cx.sess().find_by_name(&it.attrs, sym::link_section) {
self.report_overridden_symbol_section(
cx,
attr.span,
"declaration of a static with `link_section`",
);
}
} }
_ => {} _ => {}

View file

@ -48,6 +48,9 @@ impl AssocFnTrait for AssocFnFoo {
#[export_name = "bar"] fn bar() {} //~ ERROR: declaration of a function with `export_name` #[export_name = "bar"] fn bar() {} //~ ERROR: declaration of a function with `export_name`
#[export_name = "BAR"] static BAR: u32 = 5; //~ ERROR: declaration of a static with `export_name` #[export_name = "BAR"] static BAR: u32 = 5; //~ ERROR: declaration of a static with `export_name`
#[link_section = ".example_section"] fn uwu() {} //~ ERROR: declaration of a function with `link_section`
#[link_section = ".example_section"] static UWU: u32 = 5; //~ ERROR: declaration of a static with `link_section`
struct AssocFnBar; struct AssocFnBar;
impl AssocFnBar { impl AssocFnBar {

View file

@ -51,8 +51,24 @@ LL | #[export_name = "BAR"] static BAR: u32 = 5;
| |
= note: the linker's behavior with multiple libraries exporting duplicate symbol names is undefined and Rust cannot provide guarantees when you manually override them = note: the linker's behavior with multiple libraries exporting duplicate symbol names is undefined and Rust cannot provide guarantees when you manually override them
error: declaration of a function with `link_section`
--> $DIR/lint-unsafe-code.rs:51:1
|
LL | #[link_section = ".example_section"] fn uwu() {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: the program's behavior with overridden link sections on items is unpredictable and Rust cannot provide guarantees when you manually override them
error: declaration of a static with `link_section`
--> $DIR/lint-unsafe-code.rs:52:1
|
LL | #[link_section = ".example_section"] static UWU: u32 = 5;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: the program's behavior with overridden link sections on items is unpredictable and Rust cannot provide guarantees when you manually override them
error: declaration of a method with `export_name` error: declaration of a method with `export_name`
--> $DIR/lint-unsafe-code.rs:54:5 --> $DIR/lint-unsafe-code.rs:57:5
| |
LL | #[export_name = "bar"] fn bar() {} LL | #[export_name = "bar"] fn bar() {}
| ^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^
@ -60,7 +76,7 @@ LL | #[export_name = "bar"] fn bar() {}
= note: the linker's behavior with multiple libraries exporting duplicate symbol names is undefined and Rust cannot provide guarantees when you manually override them = note: the linker's behavior with multiple libraries exporting duplicate symbol names is undefined and Rust cannot provide guarantees when you manually override them
error: declaration of a method with `export_name` error: declaration of a method with `export_name`
--> $DIR/lint-unsafe-code.rs:58:5 --> $DIR/lint-unsafe-code.rs:61:5
| |
LL | #[export_name = "bar"] fn foo() {} LL | #[export_name = "bar"] fn foo() {}
| ^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^
@ -68,79 +84,79 @@ LL | #[export_name = "bar"] fn foo() {}
= note: the linker's behavior with multiple libraries exporting duplicate symbol names is undefined and Rust cannot provide guarantees when you manually override them = note: the linker's behavior with multiple libraries exporting duplicate symbol names is undefined and Rust cannot provide guarantees when you manually override them
error: declaration of an `unsafe` function error: declaration of an `unsafe` function
--> $DIR/lint-unsafe-code.rs:61:1 --> $DIR/lint-unsafe-code.rs:64:1
| |
LL | unsafe fn baz() {} LL | unsafe fn baz() {}
| ^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^
error: declaration of an `unsafe` trait error: declaration of an `unsafe` trait
--> $DIR/lint-unsafe-code.rs:62:1 --> $DIR/lint-unsafe-code.rs:65:1
| |
LL | unsafe trait Foo {} LL | unsafe trait Foo {}
| ^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^
error: implementation of an `unsafe` trait error: implementation of an `unsafe` trait
--> $DIR/lint-unsafe-code.rs:63:1 --> $DIR/lint-unsafe-code.rs:66:1
| |
LL | unsafe impl Foo for Bar {} LL | unsafe impl Foo for Bar {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^^^
error: declaration of an `unsafe` method error: declaration of an `unsafe` method
--> $DIR/lint-unsafe-code.rs:66:5 --> $DIR/lint-unsafe-code.rs:69:5
| |
LL | unsafe fn baz(&self); LL | unsafe fn baz(&self);
| ^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^
error: implementation of an `unsafe` method error: implementation of an `unsafe` method
--> $DIR/lint-unsafe-code.rs:67:5 --> $DIR/lint-unsafe-code.rs:70:5
| |
LL | unsafe fn provided(&self) {} LL | unsafe fn provided(&self) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: implementation of an `unsafe` method error: implementation of an `unsafe` method
--> $DIR/lint-unsafe-code.rs:68:5 --> $DIR/lint-unsafe-code.rs:71:5
| |
LL | unsafe fn provided_override(&self) {} LL | unsafe fn provided_override(&self) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: implementation of an `unsafe` method error: implementation of an `unsafe` method
--> $DIR/lint-unsafe-code.rs:72:5 --> $DIR/lint-unsafe-code.rs:75:5
| |
LL | unsafe fn baz(&self) {} LL | unsafe fn baz(&self) {}
| ^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^
error: implementation of an `unsafe` method error: implementation of an `unsafe` method
--> $DIR/lint-unsafe-code.rs:73:5 --> $DIR/lint-unsafe-code.rs:76:5
| |
LL | unsafe fn provided_override(&self) {} LL | unsafe fn provided_override(&self) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: implementation of an `unsafe` method error: implementation of an `unsafe` method
--> $DIR/lint-unsafe-code.rs:92:5 --> $DIR/lint-unsafe-code.rs:95:5
| |
LL | unsafe fn provided_override(&self) {} LL | unsafe fn provided_override(&self) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: implementation of an `unsafe` method error: implementation of an `unsafe` method
--> $DIR/lint-unsafe-code.rs:103:5 --> $DIR/lint-unsafe-code.rs:106:5
| |
LL | unsafe fn provided(&self) {} LL | unsafe fn provided(&self) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: implementation of an `unsafe` method error: implementation of an `unsafe` method
--> $DIR/lint-unsafe-code.rs:109:5 --> $DIR/lint-unsafe-code.rs:112:5
| |
LL | unsafe fn provided(&self) {} LL | unsafe fn provided(&self) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: implementation of an `unsafe` method error: implementation of an `unsafe` method
--> $DIR/lint-unsafe-code.rs:113:5 --> $DIR/lint-unsafe-code.rs:116:5
| |
LL | unsafe fn baz(&self) {} LL | unsafe fn baz(&self) {}
| ^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^
error: usage of an `unsafe` block error: usage of an `unsafe` block
--> $DIR/lint-unsafe-code.rs:124:5 --> $DIR/lint-unsafe-code.rs:127:5
| |
LL | unsafe {} LL | unsafe {}
| ^^^^^^^^^ | ^^^^^^^^^
@ -204,5 +220,5 @@ LL | unsafe_in_macro!()
| |
= note: this error originates in the macro `unsafe_in_macro` (in Nightly builds, run with -Z macro-backtrace for more info) = note: this error originates in the macro `unsafe_in_macro` (in Nightly builds, run with -Z macro-backtrace for more info)
error: aborting due to 26 previous errors error: aborting due to 28 previous errors