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();
})
}
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`",
);
}
}
_ => {}