Skip to content

Commit

Permalink
Report unsafe for overriding link sections
Browse files Browse the repository at this point in the history
  • Loading branch information
5225225 committed May 16, 2022
1 parent c52b9c1 commit a42a7a3
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 16 deletions.
29 changes: 29 additions & 0 deletions compiler/rustc_lint/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,17 @@ impl UnsafeCode {
.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 {
Expand Down Expand Up @@ -385,13 +396,22 @@ impl EarlyLintPass for UnsafeCode {
"declaration of a `no_mangle` function",
);
}

if let Some(attr) = cx.sess().find_by_name(&it.attrs, sym::export_name) {
self.report_overridden_symbol_name(
cx,
attr.span,
"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(..) => {
Expand All @@ -402,13 +422,22 @@ impl EarlyLintPass for UnsafeCode {
"declaration of a `no_mangle` static",
);
}

if let Some(attr) = cx.sess().find_by_name(&it.attrs, sym::export_name) {
self.report_overridden_symbol_name(
cx,
attr.span,
"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`",
);
}
}

_ => {}
Expand Down
3 changes: 3 additions & 0 deletions src/test/ui/lint/lint-unsafe-code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ impl AssocFnTrait for AssocFnFoo {
#[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`

#[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;

impl AssocFnBar {
Expand Down
48 changes: 32 additions & 16 deletions src/test/ui/lint/lint-unsafe-code.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -51,96 +51,112 @@ 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

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`
--> $DIR/lint-unsafe-code.rs:54:5
--> $DIR/lint-unsafe-code.rs:57:5
|
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

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() {}
| ^^^^^^^^^^^^^^^^^^^^^^
|
= 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
--> $DIR/lint-unsafe-code.rs:61:1
--> $DIR/lint-unsafe-code.rs:64:1
|
LL | unsafe fn baz() {}
| ^^^^^^^^^^^^^^^^^^

error: declaration of an `unsafe` trait
--> $DIR/lint-unsafe-code.rs:62:1
--> $DIR/lint-unsafe-code.rs:65:1
|
LL | unsafe trait Foo {}
| ^^^^^^^^^^^^^^^^^^^

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 {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^

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

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) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^

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) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

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) {}
| ^^^^^^^^^^^^^^^^^^^^^^^

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) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

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) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

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) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^

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) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^

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) {}
| ^^^^^^^^^^^^^^^^^^^^^^^

error: usage of an `unsafe` block
--> $DIR/lint-unsafe-code.rs:124:5
--> $DIR/lint-unsafe-code.rs:127:5
|
LL | unsafe {}
| ^^^^^^^^^
Expand Down Expand Up @@ -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)

error: aborting due to 26 previous errors
error: aborting due to 28 previous errors

0 comments on commit a42a7a3

Please sign in to comment.