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

Inner statics in generic fns aren't handled correctly. #9186

Closed
alexcrichton opened this issue Sep 14, 2013 · 3 comments · Fixed by #10189
Closed

Inner statics in generic fns aren't handled correctly. #9186

alexcrichton opened this issue Sep 14, 2013 · 3 comments · Fixed by #10189
Labels
A-codegen Area: Code generation
Milestone

Comments

@alexcrichton
Copy link
Member

It turns out that an inner static is only generated once. Hence if you have a generic function and the static somehow takes the generic type into account, the static will have the same address for all instantiations. Turns out this is a soundness problem with TLS:

use std::local_data;

fn foo<T>() -> local_data::Key<T> {
    local_data_key!(foo: T);
    return foo;
}

fn main() {
    let k1 = foo::<int>();
    let k2 = foo::<float>();

    local_data::set(k1, 1);
    local_data::set(k2, 1f);

    println!("{:#x}", local_data::get(k1, |x| *x.unwrap()));
    println!("{}", local_data::get(k2, |x| *x.unwrap()));
}
$ rust run foo.rs
warning: no debug symbols in executable (-arch x86_64)
0x3ff0000000000000
1

Notably, the inner static foo has the same address each time the function foo is instantiated, hence k1 and k2 are the same value, so the first local_data::get is actually printing the hex representation of 1f.

Naturally this allows arbitrary coercion of types via TLS in safe code, this is a definite soundness issue.

You can't declare inner functions with the type parameters declared in outer functions, so perhaps the same rule should apply to statics as well? If not, then this is a codegen problem.

@alexcrichton
Copy link
Member Author

Nominating for the well-defined milestone (whether type parameters can appear in statics).

@catamorphism
Copy link
Contributor

accepted backwards-compatible

@nikomatsakis
Copy link
Contributor

I think that the generic types etc should not be in scope for the static -- that is, trans et al are doing the right thing, just not resolve

Jarcho pushed a commit to Jarcho/rust that referenced this issue Aug 29, 2022
Add lint recommending using `std::iter::once` and `std::iter::empty`

```
changelog: [`iter_once`]: add new lint
changelog: [`iter_empty`]: add new lint
```

fixes rust-lang#9186

- \[ ] Followed [lint naming conventions][lint_naming]
- \[x] Added passing UI tests (including committed `.stderr` file)
- \[x] `cargo test` passes locally
- \[x] Executed `cargo dev update_lints`
- \[x] Added lint documentation
- \[x] Run `cargo dev fmt`

[lint_naming]: https://rust-lang.github.io/rfcs/0344-conventions-galore.html#lints

The lint doesn't really follow the naming conventions. I don't have any better idea so I'm open to suggestions.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-codegen Area: Code generation
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants