Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a new lint that warns for pointers to stack memory #134218

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

1c3t3a
Copy link
Member

@1c3t3a 1c3t3a commented Dec 12, 2024

This adds a new lint with level Warn to check for code like:

fn foo() -> *const i32 {
  let x = 42;
  &x
}

and produce a warning like:

error: returning a pointer to stack memory associated with a local variable
  --> <source>:12:5
   |
 LL|  &x
   |  ^^

This fixes #134215.

@rustbot
Copy link
Collaborator

rustbot commented Dec 12, 2024

r? @compiler-errors

rustbot has assigned @compiler-errors.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Dec 12, 2024
@1c3t3a
Copy link
Member Author

1c3t3a commented Dec 12, 2024

I am aware that this may warn on existing code. I wonder what is a good strategy here? Should it be an Allow lint first and then be migrated to Warn as part of an edition? Or even stay Allow forever?

@compiler-errors
Copy link
Member

Is there a reason why this was implemented as a rust lint rather than a clippy suspicious lint or something?

I guess let's find the fallout of this lint

@bors try

bors added a commit to rust-lang-ci/rust that referenced this pull request Dec 12, 2024
Add a new lint that warns for pointers to stack memory

This adds a new lint with level `Warn` to check for code like:

```rust
fn foo() -> *const i32 {
  let x = 42;
  &x
}
```

and produce a warning like:
```text
error: returning a pointer to stack memory associated with a local variable
  --> <source>:12:5
   |
 LL|  &x
   |  ^^
```
This fixes rust-lang#134215.
@bors
Copy link
Contributor

bors commented Dec 12, 2024

⌛ Trying commit 40eb5d7 with merge 6db1a6a...

@1c3t3a
Copy link
Member Author

1c3t3a commented Dec 12, 2024

Is there a reason why this was implemented as a rust lint rather than a clippy suspicious lint or something?

This was completely my judgment, I am happy to change this if requested. My reasoning was that this is a pretty fundamental lint, since the code it lints should probably never exist? And looking at the list of rustc lints I thought it might be a good fit.

@rust-log-analyzer

This comment has been minimized.

@1c3t3a 1c3t3a force-pushed the stack-memory-warning branch from 40eb5d7 to e162e89 Compare December 12, 2024 17:17
@1c3t3a
Copy link
Member Author

1c3t3a commented Dec 12, 2024

The job x86_64-gnu-llvm-18 failed! Check out the build log: (web) (plain)

Click to see the possible cause of the failure (guessed by this bot)

Oops - I don't fully understand that error. The failure looks like in the .stderr file, but it still has unexpected errors that it doesn't match on... This even happens when passing --bless.

@rust-log-analyzer

This comment has been minimized.

@compiler-errors
Copy link
Member

@craterbot check

@craterbot
Copy link
Collaborator

🚨 Error: missing start toolchain

🆘 If you have any trouble with Crater please ping @rust-lang/infra!
ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more

@compiler-errors
Copy link
Member

@craterbot run mode=check-only start=master#8e37e151835d96d6a7415e93e6876561485a3354 end=try#6db1a6a7ebb2ad243051f3ff949f987dd70cde2b

@craterbot
Copy link
Collaborator

👌 Experiment pr-134218 created and queued.
🔍 You can check out the queue and this experiment's details.

ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more

@craterbot craterbot added S-waiting-on-crater Status: Waiting on a crater run to be completed. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Dec 12, 2024
@jyn514
Copy link
Member

jyn514 commented Dec 13, 2024

The job x86_64-gnu-llvm-18 failed! Check out the build log: (web) (plain)
Click to see the possible cause of the failure (guessed by this bot)

Oops - I don't fully understand that error. The failure looks like in the .stderr file, but it still has unexpected errors that it doesn't match on... This even happens when passing --bless.

bless will generate a .stderr file for you, but it will not update the original .rs file. compiletest is complaining because it expects each warning to be annotated with //~ WARN. see https://rustc-dev-guide.rust-lang.org/tests/ui.html#error-annotations

@rust-log-analyzer

This comment has been minimized.

@1c3t3a 1c3t3a force-pushed the stack-memory-warning branch from 3c39f0f to f56a313 Compare December 13, 2024 15:49
@rustbot
Copy link
Collaborator

rustbot commented Dec 13, 2024

The Miri subtree was changed

cc @rust-lang/miri

@rust-log-analyzer

This comment has been minimized.

@1c3t3a 1c3t3a force-pushed the stack-memory-warning branch from f56a313 to 80a2f21 Compare December 13, 2024 17:40
@rust-log-analyzer

This comment has been minimized.

@1c3t3a 1c3t3a force-pushed the stack-memory-warning branch from 80a2f21 to 4e7732c Compare December 14, 2024 00:27
@compiler-errors
Copy link
Member

@craterbot end=try#6db1a6a7ebb2ad243051f3ff949f987dd70cde2b+rustflags=-Dreturn-local-variable-ptr

@craterbot
Copy link
Collaborator

📝 Configuration of the pr-134218 experiment changed.

ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more

@craterbot
Copy link
Collaborator

🚧 Experiment pr-134218 is now running

ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more

@craterbot
Copy link
Collaborator

🎉 Experiment pr-134218 is completed!
📊 121 regressed and 53 fixed (553468 total)
📰 Open the full report.

⚠️ If you notice any spurious failure please add them to the denylist!
ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more

@craterbot craterbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-crater Status: Waiting on a crater run to be completed. labels Dec 16, 2024
@1c3t3a
Copy link
Member Author

1c3t3a commented Dec 17, 2024

Looks like this just hit one crate (out of 553468!). There was an ICE happening as well, which looks unrelated. The other 119 errors look like spurious failures (something wrong with the docker invocation). Can we conclude that this lint will not break a great amount of existing users?

@estebank
Copy link
Contributor

The only one that seems related to the lint is triggering at

https://github.com/feymartynov/janus-app/blob/11d2f4dbbcd993a203df48fecda2507e37c5be03/src/plugin.rs#L247-L272

and it looks legit.

Separately, I am slightly concerned about the ICEs at https://crater-reports.s3.amazonaws.com/pr-134218/try%236db1a6a7ebb2ad243051f3ff949f987dd70cde2b%2Brustflags=-Dreturn-local-variable-ptr/gh/fmhall.chess-program/log.txt which seem to point at an issue elsewhere that was recently introduced.

@compiler-errors
Copy link
Member

r? estebank

@rustbot rustbot assigned estebank and unassigned compiler-errors Jan 23, 2025
@bors
Copy link
Contributor

bors commented Jan 27, 2025

☔ The latest upstream changes (presumably #136024) made this pull request unmergeable. Please resolve the merge conflicts.

@1c3t3a 1c3t3a force-pushed the stack-memory-warning branch from 352f384 to eb706f9 Compare January 30, 2025 18:14
@1c3t3a
Copy link
Member Author

1c3t3a commented Jan 30, 2025

Hey all, friendly ping on this PR, do you have overall feedback on this change :)

I made a small change to the lint to also include nested pointers of arbitrary depth. This was funnily enough discovered by having some production code I saw that ran into this issue, which is I think a good reason to have a lint like that?

@rust-log-analyzer

This comment has been minimized.

@1c3t3a 1c3t3a force-pushed the stack-memory-warning branch from d56da6f to 5576c0a Compare January 30, 2025 19:20
@bors
Copy link
Contributor

bors commented Jan 31, 2025

☔ The latest upstream changes (presumably #136332) made this pull request unmergeable. Please resolve the merge conflicts.

This adds a new lint with level `Warn` to check for code like:

```rust
fn foo() -> *const i32 {
  let x = 42;
  &x
}
```

and produce a warning like:
```text
error: returning a pointer to stack memory associated with a local variable
  --> <source>:12:5
   |
 LL|  &x
   |  ^^
```
@1c3t3a 1c3t3a force-pushed the stack-memory-warning branch from 5576c0a to e38f2c8 Compare January 31, 2025 14:12
@1c3t3a
Copy link
Member Author

1c3t3a commented Feb 11, 2025

Friendly ping on this one :)

@1c3t3a
Copy link
Member Author

1c3t3a commented Feb 18, 2025

Friendly ping on this again :) It would be great to get some feedback!

@1c3t3a
Copy link
Member Author

1c3t3a commented Feb 24, 2025

Friendly ping again!

cc: @estebank, @compiler-errors

@bors
Copy link
Contributor

bors commented Mar 5, 2025

☔ The latest upstream changes (presumably #138058) made this pull request unmergeable. Please resolve the merge conflicts.

@jieyouxu jieyouxu added the A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. label Mar 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Lint for returning a pointer to stack memory associated with a local variable
10 participants