Lint against invalid POSIX symbol definitions#158522
Conversation
|
r? @chenyukang rustbot has assigned @chenyukang. Use Why was this reviewer chosen?The reviewer was selected based on:
|
315bb46 to
070c3d9
Compare
This comment has been minimized.
This comment has been minimized.
|
cc @rust-lang/miri |
|
Makes sense to me. Included in this FCP is preapproval for extending this to all extern functions referenced by the standard library when linked, such that defining a function of the same name in your program with a mismatched signature would cause problems. @rfcbot fcp merge lang |
|
@traviscross has proposed to merge this. The next step is review by the rest of the tagged team members: No concerns currently listed. Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up! cc @rust-lang/lang-advisors: FCP proposed for lang, please feel free to register concerns. |
|
@rfcbot reviewed |
|
Tagging relnotes as a reminder to extend the release notes on this lint to list the additional symbols. |
|
🔔 This is now entering its final comment period, as per the review above. 🔔 |
| ExpectedSymbol { symbol: "bcmp", lang: LanguageItems::bcmp_fn }, | ||
| ExpectedSymbol { symbol: "strlen", lang: LanguageItems::strlen_fn }, | ||
| // POSIX symbols | ||
| ExpectedSymbol { symbol: "open", lang: LanguageItems::open_fn }, |
There was a problem hiding this comment.
A definition of open64 with the wrong signature would still bypass the lint?
There was a problem hiding this comment.
Yes, only symbols defined in this list are checked, but given that T-lang wants to lint on all the functions used by core/std #158522 (comment) it makes sense to add open64 (and any other libc function) in a follow-up PR.
This comment was marked as resolved.
This comment was marked as resolved.
02eca56 to
3155f8f
Compare
|
This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed. Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers. |
This comment was marked as resolved.
This comment was marked as resolved.
3155f8f to
676a82f
Compare
|
The final comment period, with a disposition to merge, as per the review above, is now complete. As the automated representative of the governance process, I would like to thank the author for their work and everyone else who contributed. |
|
@bors r=chenyukang |
Rollup of 5 pull requests Successful merges: - #156712 (Pointer authentication config and user facing options) - #158522 (Lint against invalid POSIX symbol definitions) - #159229 (ast_lowering: Reject invalid direct_const_arg owners) - #159270 (Only force backtraces when a panic occurs in bootstrap) - #159308 (Reorganize `tests/ui/issues` [24/N])
Rollup merge of #158522 - Urgau:runtime-symbols-posix, r=chenyukang Lint against invalid POSIX symbol definitions This PR extends the `invalid_runtime_symbol_definitions` and `suspicious_runtime_symbol_definitions` lints to lint againts the following POSIX symbols: - I/O: `open`, `read`, `write`, `close` - Alloc: `malloc`, `realloc`, `free` - Program: `exit` This is meant to address the multiple reports of users tripping over `std` symbols: - [Why does `#[no_mangle] fn open() {}` make `cargo t` hang?](https://users.rust-lang.org/t/why-does-no-mangle-fn-open-make-cargo-t-hang/103423) - [Pointer becomes misaligned in test with `no_mangle`](https://users.rust-lang.org/t/pointer-becomes-misaligned-in-test-with-no-mangle/126580) Those symbols are defined in `std`, so as long as the `std` crate is imported (and we are on a UNIX-like target) we lint on those symbols. Follow up to #155521
Rollup of 5 pull requests Successful merges: - rust-lang/rust#156712 (Pointer authentication config and user facing options) - rust-lang/rust#158522 (Lint against invalid POSIX symbol definitions) - rust-lang/rust#159229 (ast_lowering: Reject invalid direct_const_arg owners) - rust-lang/rust#159270 (Only force backtraces when a panic occurs in bootstrap) - rust-lang/rust#159308 (Reorganize `tests/ui/issues` [24/N])
Replace weak only lang items with a custom attribute When I added the `invalid_runtime_symbol_definitions` and `suspicious_runtime_symbol_definitions` lints in #155521 I added the concept of "weak only" lang item. It's a `LangItem` that has no implementation, this is useful because the `core` symbols where not directly called, they were inserted by the compiler. This mechanism work well, but T-lang [approved](#158522 (comment)) the extension to "all extern functions referenced by the standard library", which makes the lang item approach impractical as we needs to update 5 files just to declare them. I'm not sure we can ask library contributor to do the dance for each `extern "C"` functions. But the most problematic thing is that many extern functions in `std` come from `libc` not `std`, and adding lang items there is just no feasible I think. Instead this PR proposed that we introduce a proper mechanism for them by adding a attribute `#[rustc_canonical_symbol = "..."]`, which is encoded and decoded in `rmeta` like lang items and diagnostics items. This simplifies the declaration as we only need to put the attribute to be effective `#[rustc_canonical_symbol = "open"]`. It also opens up many possibilities (none implemented here) like putting them on `use` statements (so we don't need to modify `libc`) or having the attribute be placed on a module. The table may also be useful on it's own if we want someday to do it for all crates, not just the standard library. Follow up to #155521 and #158522
Replace weak only lang items with a custom attribute When I added the `invalid_runtime_symbol_definitions` and `suspicious_runtime_symbol_definitions` lints in #155521 I added the concept of "weak only" lang item. It's a `LangItem` that has no implementation, this is useful because the `core` symbols where not directly called, they were inserted by the compiler. This mechanism work well, but T-lang [approved](#158522 (comment)) the extension to "all extern functions referenced by the standard library", which makes the lang item approach impractical as we needs to update 5 files just to declare them. I'm not sure we can ask library contributor to do the dance for each `extern "C"` functions. But the most problematic thing is that many extern functions in `std` come from `libc` not `std`, and adding lang items there is just no feasible I think. Instead this PR proposed that we introduce a proper mechanism for them by adding a attribute `#[rustc_canonical_symbol = "..."]`, which is encoded and decoded in `rmeta` like lang items and diagnostics items. This simplifies the declaration as we only need to put the attribute to be effective `#[rustc_canonical_symbol = "open"]`. It also opens up many possibilities (none implemented here) like putting them on `use` statements (so we don't need to modify `libc`) or having the attribute be placed on a module. The table may also be useful on it's own if we want someday to do it for all crates, not just the standard library. Follow up to #155521 and #158522
Replace weak only lang items with a custom attribute When I added the `invalid_runtime_symbol_definitions` and `suspicious_runtime_symbol_definitions` lints in #155521 I added the concept of "weak only" lang item. It's a `LangItem` that has no implementation, this is useful because the `core` symbols where not directly called, they were inserted by the compiler. This mechanism work well, but T-lang [approved](#158522 (comment)) the extension to "all extern functions referenced by the standard library", which makes the lang item approach impractical as we needs to update 5 files just to declare them. I'm not sure we can ask library contributor to do the dance for each `extern "C"` functions. But the most problematic thing is that many extern functions in `std` come from `libc` not `std`, and adding lang items there is just no feasible I think. Instead this PR proposed that we introduce a proper mechanism for them by adding a attribute `#[rustc_canonical_symbol = "..."]`, which is encoded and decoded in `rmeta` like lang items and diagnostics items. This simplifies the declaration as we only need to put the attribute to be effective `#[rustc_canonical_symbol = "open"]`. It also opens up many possibilities (none implemented here) like putting them on `use` statements (so we don't need to modify `libc`) or having the attribute be placed on a module. The table may also be useful on it's own if we want someday to do it for all crates, not just the standard library. Follow up to #155521 and #158522
Replace weak only lang items with a custom attribute When I added the `invalid_runtime_symbol_definitions` and `suspicious_runtime_symbol_definitions` lints in #155521 I added the concept of "weak only" lang item. It's a `LangItem` that has no implementation, this is useful because the `core` symbols where not directly called, they were inserted by the compiler. This mechanism work well, but T-lang [approved](#158522 (comment)) the extension to "all extern functions referenced by the standard library", which makes the lang item approach impractical as we needs to update 5 files just to declare them. I'm not sure we can ask library contributor to do the dance for each `extern "C"` functions. But the most problematic thing is that many extern functions in `std` come from `libc` not `std`, and adding lang items there is just no feasible I think. Instead this PR proposed that we introduce a proper mechanism for them by adding a attribute `#[rustc_canonical_symbol = "..."]`, which is encoded and decoded in `rmeta` like lang items and diagnostics items. This simplifies the declaration as we only need to put the attribute to be effective `#[rustc_canonical_symbol = "open"]`. It also opens up many possibilities (none implemented here) like putting them on `use` statements (so we don't need to modify `libc`) or having the attribute be placed on a module. The table may also be useful on it's own if we want someday to do it for all crates, not just the standard library. Follow up to #155521 and #158522
This PR extends the
invalid_runtime_symbol_definitionsandsuspicious_runtime_symbol_definitionslints to lint againts the following POSIX symbols:open,read,write,closemalloc,realloc,freeexitThis is meant to address the multiple reports of users tripping over
stdsymbols:#[no_mangle] fn open() {}makecargo thang?no_mangleThose symbols are defined in
std, so as long as thestdcrate is imported (and we are on a UNIX-like target) we lint on those symbols.Follow up to #155521