From a42a7a3eb90d67f9931e912b7e62f697c8467b3a Mon Sep 17 00:00:00 2001 From: 5225225 <5225225@mailbox.org> Date: Mon, 16 May 2022 18:04:54 +0100 Subject: Report unsafe for overriding link sections --- compiler/rustc_lint/src/builtin.rs | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) (limited to 'compiler/rustc_lint/src') diff --git a/compiler/rustc_lint/src/builtin.rs b/compiler/rustc_lint/src/builtin.rs index a87083a082b..64d9d4aaf17 100644 --- a/compiler/rustc_lint/src/builtin.rs +++ b/compiler/rustc_lint/src/builtin.rs @@ -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 { @@ -385,6 +396,7 @@ 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, @@ -392,6 +404,14 @@ impl EarlyLintPass for UnsafeCode { "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(..) => { @@ -402,6 +422,7 @@ 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, @@ -409,6 +430,14 @@ impl EarlyLintPass for UnsafeCode { "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`", + ); + } } _ => {} -- cgit 1.4.1-3-g733a5