Skip to content

Support ThreadLocalRef (by treating it like a static variable) - #1452

Merged
fzaiser merged 5 commits into
model-checking:mainfrom
fzaiser:threadlocalref
Aug 5, 2022
Merged

fzaiser merged 5 commits into
model-checking:mainfrom
fzaiser:threadlocalref

Conversation

@fzaiser

@fzaiser fzaiser commented Aug 4, 2022

Copy link
Copy Markdown
Contributor

Description of changes:

Kani did not support thread locals, in particular the MIR construct ThreadLocalRef. As discovered #1400, this is the most used feature that is currently unsupported in Kani. In particular it is needed to compile tokio.

Since Kani is single-threaded, thread locals are treated like static variables and translated in the same way.

Resolved issues:

Resolves #1450
Resolves #541
Resolves #157

Call-outs:

Testing:

  • How is this change tested? A previously failing regression test that now passes. It uses the same code as static variables, so the same code path is also tested in other tests using static variables.

  • Is this a refactor change? No

Checklist

  • Each commit message has a non-empty body, explaining why the change was made
  • Methods or procedures are documented
  • Regression or unit tests are included, or existing tests cover the modified code
  • My PR is restricted to a single feature or bugfix

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 and MIT licenses.

@fzaiser
fzaiser requested a review from a team as a code owner August 4, 2022 18:32
@fzaiser fzaiser changed the title Support ThreadLocalRef (by treating them like static variables) Support ThreadLocalRef (by treating it like a static variable) Aug 4, 2022
#[kani::proof]
fn main() {
COND.with(|&b|{
COND.with(|&b| {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How did the formatter miss this?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this test may have been committed before we had the format checker?

@celinval celinval Aug 4, 2022 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The format checker runs in all files, not just the ones that have changed. So that's a mystery.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think cargo fmt formats everything in all subfolders. Opened an issue: #1455

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. Fixed here #1459

Comment thread tests/kani/ThreadLocalRef/main.rs Resolved
@fzaiser fzaiser self-assigned this Aug 4, 2022

@celinval celinval left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me.

}
}

pub fn codegen_static_pointer(&mut self, def_id: DefId, is_thread_local: bool) -> Expr {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a comment.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Comment thread tests/kani/ThreadLocalRef/main.rs Outdated
static COMPLEX_DATA: RefCell<&'static str> = RefCell::new("before");
}

pub fn main() {}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you don't need this

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, removed

Location::none(),
"https://github.com/model-checking/kani/issues/541",
)
Rvalue::ThreadLocalRef(def_id) => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should add a user warning here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What kind of warning do you have in mind? If you mean the single-threaded assumption, I don't think this is the place to warn about it. If anything, we should warn about that if the user tries to spawn threads. But at the moment it is not possible to spawn threads anyway. If we warn for thread locals, the user will get spurious warnings even if they only use one thread (tokio uses thread locals even in single-threaded mode for instance).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do understand the concern of giving spurious warnings and that using thread-local in single threaded analysis is a legit use case.

However, my main concern is that we are making assumptions here that might not hold later. This assumption could be easily missed when we enable multi-thread support.

Note that this is already the behavior for all intrinsics that assume single threads. Here is the message we print:

macro_rules! emit_concurrency_warning {
($intrinsic: expr, $loc: expr) => {{
warn!(
"Kani does not support concurrency for now. `{}` in {} treated as a sequential operation.",
$intrinsic,
$loc.short_string()
);
}};
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I've seen that message before (actually a whole wall of text made up of just this message) and found it very distracting. If it's not actually a soundness issue in Kani, I don't think we should print warnings. That's just going to lower the user's trust in the tool. But that's a separate issue, for this PR, I'll go with the emit_concurrency_warning as you suggested.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Issue filed here to discuss: #1460

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a concurrency warning in this PR.

@danielsn danielsn left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should probably address Celina's comments, but beyond that LGTM

@fzaiser
fzaiser merged commit aa437ed into model-checking:main Aug 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support ThreadLocalRef (needed for tokio) Rvalue::ThreadLocalRef is unimplemented Thread local statics is currently unimplemented

3 participants