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 _umtx_op shim to freebsd #2321

Closed
wants to merge 3 commits into from
Closed

Conversation

b-ncMN
Copy link
Contributor

@b-ncMN b-ncMN commented Jul 3, 2022

This fixes the concurrent-panic.rs test on freebsd

Signed-off-by: InfRandomness infrandomness@gmail.com

@oli-obk

This comment was marked as off-topic.

@b-ncMN

This comment was marked as off-topic.

@oli-obk

This comment was marked as off-topic.

@b-ncMN

This comment was marked as off-topic.

@oli-obk

This comment was marked as off-topic.

@b-ncMN
Copy link
Contributor Author

b-ncMN commented Jul 4, 2022

Well yeah that's what I've been doing already for the past 2 PRs.

I know, but I just worry that list will get bigger and bigger, and the script isn't really the best place for it. Also if we do something similar for other targets, I think we want to make it easier to make sure we don't regress things, and make it easier to figure out what new things are passing.

I'm not saying anything is wrong here, I'm only trying to figure out if we can improve this. Sorry for hijacking your PR for this ^^ I'll mark it as off topic and take it elsewhere

sure, how could we do that?

@oli-obk
Copy link
Contributor

oli-obk commented Jul 5, 2022

I opened #2324 for that

@b-ncMN b-ncMN marked this pull request as ready for review July 5, 2022 22:15
@b-ncMN
Copy link
Contributor Author

b-ncMN commented Jul 5, 2022

This MR is ready for reviews
it is not currently mergeable because the code is to be improved by a large margin, however, I would like some reviews to help me make it better

@b-ncMN b-ncMN force-pushed the _umtx_op-shim branch 2 times, most recently from f2982b4 to 14cfe9f Compare July 6, 2022 04:45
src/shims/unix/freebsd/futex.rs Outdated Show resolved Hide resolved
src/shims/unix/freebsd/futex.rs Outdated Show resolved Hide resolved
src/shims/unix/freebsd/futex.rs Outdated Show resolved Hide resolved
Comment on lines 29 to 30
uaddr: &OpTy<'tcx, Tag>,
uaddr2: &OpTy<'tcx, Tag>,
Copy link
Contributor

Choose a reason for hiding this comment

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

Use the Pointer type for these two and move the conversion to the caller

Copy link
Contributor Author

@b-ncMN b-ncMN Jul 6, 2022

Choose a reason for hiding this comment

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

I couldn't make that work, I've posted about it on zulip

Copy link
Contributor

Choose a reason for hiding this comment

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

ok, they are unused for now anyway, so let's just ignore them (keep OpTy and we'll see about them once they are used)

Comment on lines 54 to 57
// The `val` value is invalid. Double check this against the manual.
let einval = this.eval_libc("EINVAL").ok()?;
this.set_last_error(einval).ok()?;
None
Copy link
Contributor

Choose a reason for hiding this comment

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

This seems wrong. https://man7.org/linux/man-pages/man2/futex.2.html suggests that this is not an error case:

This operation tests that the value at the futex word
pointed to by the address uaddr still contains the
expected value val, and if so, then sleeps waiting for a
FUTEX_WAKE operation on the futex word.

If the futex value does not match val, then the call fails
immediately with the error EAGAIN.

Copy link
Contributor

Choose a reason for hiding this comment

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

https://helpmanual.io/man2/_umtx_op-freebsd/ says

The current value of the variable pointed to by the Fa obj argument is compared with the Fa val . If they are equal, the requesting thread is put to interruptible sleep until woken up or the optionally specified timeout expires.

and does not say what happens if the value differs.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This seems wrong. man7.org/linux/man-pages/man2/futex.2.html suggests that this is not an error case:

This operation tests that the value at the futex word
pointed to by the address uaddr still contains the
expected value val, and if so, then sleeps waiting for a
FUTEX_WAKE operation on the futex word.

If the futex value does not match val, then the call fails
immediately with the error EAGAIN.

You seem to be looking at the linux futex function, not the freebsd _umtx_op function

Copy link
Contributor

Choose a reason for hiding this comment

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

yes, but the freebsd function does not document what happens if the value differs either. I guess erroring out is the safe thing to do and we can address this if it becomes a problem.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes, but the freebsd function does not document what happens if the value differs either. I guess erroring out is the safe thing to do and we can address this if it becomes a problem.

we do sort of error out, just with a different code

Ok(op) =>
match op {
OpType::UmtxOpWait =>
if this.read_scalar(obj).ok()?.to_u64().ok()? == val {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think you want to deref_operand first, otherwise you're comparing the pointer address with val

Copy link
Contributor

Choose a reason for hiding this comment

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

Also I don't know what Pointer to a variable of type Vt long . in the man pages means, could this be a pointer to a 32 bit value on 32 bit platforms?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Also I don't know what Pointer to a variable of type Vt long . in the man pages means, could this be a pointer to a 32 bit value on 32 bit platforms?

I'm not entirely sure, I'll search around

src/shims/unix/freebsd/futex.rs Outdated Show resolved Hide resolved
@oli-obk oli-obk self-assigned this Jul 6, 2022
ci.sh Outdated
@@ -60,7 +60,7 @@ case $HOST_TARGET in
MIRI_TEST_TARGET=i686-unknown-linux-gnu run_tests
MIRI_TEST_TARGET=aarch64-apple-darwin run_tests
MIRI_TEST_TARGET=i686-pc-windows-msvc run_tests
MIRI_TEST_TARGET=x86_64-unknown-freebsd run_tests_minimal hello integer vec current_dir data_race env
MIRI_TEST_TARGET=x86_64-unknown-freebsd run_tests_minimal hello integer vec current_dir data_race env concurrent-panic
Copy link
Member

Choose a reason for hiding this comment

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

If this adds support for futexes, shouldn't there be a lot more tests that work now?
concurrent-panic is an awfully specific test to check. It's not really about concurrency at all. Which part of the Rust stdlib uses _umtx_op that is called by concurrent-panic? AFAIK it uses an RwLock, is that where it comes from?

Copy link
Member

Choose a reason for hiding this comment

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

Answering my own question, it's in condvar.

Specifically I don't think this will give us good test coverage for the new operation, that's why we also want some other tests. At least sync should be added, I think. If those all pass, that gives me higher confidence that the implementation is correct.

}
}

struct Futex<'mir, 'tcx> {}
Copy link
Member

Choose a reason for hiding this comment

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

What is the point of this type? Please add a doc comment.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What is the point of this type? Please add a doc comment.

This is just here to be able to bound an implementation

Copy link
Member

Choose a reason for hiding this comment

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

In that case please remove it and follow the extension trait pattern that all the other files are using.

pub fn futex<'tcx>(
this: &mut MiriEvalContext<'_, 'tcx>,
// Object to operate on
obj: &OpTy<'tcx, Tag>,
Copy link
Member

Choose a reason for hiding this comment

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

All the other arguments are already interpreted at some Rust type; please do the same with this one. The comment seems to indicate it's a pointer, so then this should have type Pointer<Option<Tag>>.

use crate::InterpResult;
use crate::OpTy;
use crate::{MiriEvalContext, Tag};
use std::fmt::Pointer;
Copy link
Member

Choose a reason for hiding this comment

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

You do not want to import this trait here. It's the wrong Pointer.^^

Best to start by copy-pasting the header from some other file. I think you can replace all your imports here by use crate::*; which is what we have in most files.

this.futex_wait(
this.read_scalar(obj)?.to_machine_usize()?,
this.get_active_thread(),
u32::MAX,
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
u32::MAX,
u32::MAX, // we set the bitset to include all bits

@RalfJung RalfJung marked this pull request as draft July 9, 2022 14:28
@RalfJung RalfJung added the S-waiting-on-author Status: Waiting for the PR author to address review comments label Jul 11, 2022
@b-ncMN b-ncMN closed this Aug 4, 2022
@b-ncMN
Copy link
Contributor Author

b-ncMN commented Aug 4, 2022

I am not planning to continue on adding support to freebsd, having lost interest to code in rust (and therefore not wanting to do more on the related rust-lang projects)

@RalfJung
Copy link
Member

RalfJung commented Aug 4, 2022

@InfRandomness that's a bummer. Anyway thanks for the work you did; adding new Unix targets is now easier than it used to be! :)

@b-ncMN b-ncMN reopened this Oct 19, 2022
This fixes the `concurrent-panic.rs` test on freebsd

Signed-off-by: InfRandomness <infrandomness@gmail.com>
Signed-off-by: InfRandomness <infrandomness@gmail.com>
- Changes return type of futex() to InterpResult<'tcx> and removes every ok() calls
- Add documentation and link to `_umtx_op` man pages
- Move comments to parameters and remove pointless assignments
- Replace `OpTy`s usages to `Pointer`s
- Run `deref_operand` on `read_scalar` output in Futex wait operation
- Replace `to_u64` to `to_machine_usize`

Signed-off-by: InfRandomness <infrandomness@gmail.com>
@RalfJung
Copy link
Member

There hasn't been any activity here for 4 months, so I am going to close this PR. @InfRandomness or anyone else who is interested, feel free to pick this up again in the future if/when you have time!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-author Status: Waiting for the PR author to address review comments
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants