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

Getpid shim #2215

Merged
merged 1 commit into from
Jun 11, 2022
Merged

Getpid shim #2215

merged 1 commit into from
Jun 11, 2022

Conversation

b-ncMN
Copy link
Contributor

@b-ncMN b-ncMN commented Jun 8, 2022

No description provided.

@b-ncMN b-ncMN changed the title This adds a getpid shim Getpid shim Jun 8, 2022
@b-ncMN b-ncMN force-pushed the getpid_shim branch 2 times, most recently from 2a6b1ec to 1958475 Compare June 8, 2022 09:35
@b-ncMN b-ncMN marked this pull request as ready for review June 8, 2022 11:30
@b-ncMN
Copy link
Contributor Author

b-ncMN commented Jun 8, 2022

I was not able to test and verify if the windows function work properly, this happens when I try to :

cargo miri run --target x86_64-pc-windows-msvc
    Finished dev [unoptimized + debuginfo] target(s) in 0.03s
     Running `/home/infrandomness/.cargo/bin/cargo-miri target/miri/x86_64-pc-windows-msvc/debug/getpidtest.exe`
warning: unnecessary `unsafe` block
  --> src/main.rs:12:5
   |
12 |     unsafe {
   |     ^^^^^^ unnecessary `unsafe` block
   |
   = note: `#[warn(unused_unsafe)]` on by default

error: unsupported operation: can't call foreign function: GetCurrentProcessId
  --> src/main.rs:5:19
   |
5  |         let pid = processthreadsapi::GetCurrentProcessId();
   |                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ can't call foreign function: GetCurrentProcessId
   |
   = help: this is likely not a bug in the program; it indicates that the program performed an operation that the interpreter does not support
           
           ```
           
The code I am using is : 
```rs
use winapi::um::processthreadsapi;

extern "C" fn foo() {
    unsafe {
        let pid = processthreadsapi::GetCurrentProcessId();
        println!("pid : {}", pid);

    }
}

fn main() {
    unsafe {
        foo();
    }
}

I tried giving multiple different ABIs to foo without success, I can't seem to be able to call winapi::..::GetCurrentProcessId()

@b-ncMN b-ncMN force-pushed the getpid_shim branch 2 times, most recently from 393337a to f5bc58c Compare June 8, 2022 14:15
@RalfJung
Copy link
Member

RalfJung commented Jun 8, 2022

This is a good start, thanks. :) I left some comments.

What this is also missing is a test. The test should probably just call std::process::id.

@b-ncMN
Copy link
Contributor Author

b-ncMN commented Jun 9, 2022

This seems ready to me, is there anything else?

@RalfJung
Copy link
Member

RalfJung commented Jun 9, 2022

Please also add a test that runs on all platforms and just calls std::process::id(), to make sure that does not error. Currently, the Windows codepath is uncovered by the test suite.

@b-ncMN
Copy link
Contributor Author

b-ncMN commented Jun 9, 2022

Please also add a test that runs on all platforms and just calls std::process::id(), to make sure that does not error. Currently, the Windows codepath is uncovered by the test suite.

I couldn't find where to create windows' test, libc.rs seems to be reserved for unixy OSs

@b-ncMN
Copy link
Contributor Author

b-ncMN commented Jun 9, 2022

Please also add a test that runs on all platforms and just calls std::process::id(), to make sure that does not error. Currently, the Windows codepath is uncovered by the test suite.

I couldn't find where to create windows' test, libc.rs seems to be reserved for unixy OSs

Also winapi does not seem to be a dependency of miri, so I wonder if I'm supposed to create the test into test/pass/....rs

@RalfJung
Copy link
Member

I couldn't find where to create windows' test, libc.rs seems to be reserved for unixy OSs

Just create a file that calls std::process::id(). That will work on all platforms. This is not a libc test.

@b-ncMN
Copy link
Contributor Author

b-ncMN commented Jun 10, 2022

I couldn't find where to create windows' test, libc.rs seems to be reserved for unixy OSs

Just create a file that calls std::process::id(). That will work on all platforms. This is not a libc test.

This is weird, I'm having this as an error while trying what you told me :

actual output differed from expected tests/pass/getpid.stderr
Diff < left / right > :
>error: unsupported operation: `getpid` not available when isolation is enabled
>  --> RUSTLIB/std/src/sys/PLATFORM/os.rs:LL:CC
>   |
>LL |     unsafe { libc::getpid() as u32 }
>   |              ^^^^^^^^^^^^^^ `getpid` not available when isolation is enabled
>   |
>   = help: pass the flag `-Zmiri-disable-isolation` to disable isolation;
>   = help: or pass `-Zmiri-isolation-error=warn` to configure Miri to return an error code from isolated operations (if supported for that operation) and continue with a warning
>           
>   = note: inside `std::sys::PLATFORM::os::getpid` at RUSTLIB/std/src/sys/PLATFORM/os.rs:LL:CC
>   = note: inside `std::process::id` at RUSTLIB/std/src/process.rs:LL:CC
>note: inside `getpid` at $DIR/getpid.rs:LL:CC
>  --> $DIR/getpid.rs:LL:CC
>   |
>LL |     std::process::id()
>   |     ^^^^^^^^^^^^^^^^^^
>note: inside `main` at $DIR/getpid.rs:LL:CC
>  --> $DIR/getpid.rs:LL:CC
>   |
>LL |     getpid();
>   |     ^^^^^^^^
>
>note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
>
>error: aborting due to previous error
>
>



There were 1 unmatched diagnostics that occurred outside the testfile and had not pattern
    Error: unsupported operation: `getpid` not available when isolation is enabled

failures:
    tests/pass/getpid.rs

meanwhile my code is :

// compile-flags: -Zmiri-isolation-error=warn-nobacktrace

fn getpid() -> u32 {
    std::process::id()
}

fn main() {
    getpid();
}

Oh right, std::process::id() calls std::sys::unix::os::getpid(), which itself calls libc::getpid(), therefore, what in the world

@RalfJung
Copy link
Member

RalfJung commented Jun 10, 2022

The flag needs to be -Zmiri-disable-isolation.

-Zmiri-isolation-error only works for shims that can return an error code when isolation is enabled. Which getpid isn't.

@b-ncMN b-ncMN force-pushed the getpid_shim branch 3 times, most recently from 1fe7ae8 to def51a7 Compare June 10, 2022 21:44
@b-ncMN
Copy link
Contributor Author

b-ncMN commented Jun 10, 2022

I've tried to cleanup a bit the mess that this tree is, let me know if you need me to squash further

@b-ncMN b-ncMN force-pushed the getpid_shim branch 2 times, most recently from 8e27fa1 to 7aea83a Compare June 10, 2022 22:20
@RalfJung
Copy link
Member

This LGTM but please squash it all into a single commit. The last commit doesn't even change anything.^^

@RalfJung
Copy link
Member

Oh, that failure is funny. It only occurs with optimizations enabled.

Does inlining here "work around" the this.frame_in_std() check? That would be a problem...

@RalfJung
Copy link
Member

RalfJung commented Jun 10, 2022

Yeah this is the full error:

error: unsupported operation: can't call foreign function: GetCurrentProcessId
 --> tests/pass/getpid.rs:4:5
  |
4 |     std::process::id()
  |     ^^^^^^^^^^^^^^^^^^ can't call foreign function: GetCurrentProcessId
  |
  = help: this is likely not a bug in the program; it indicates that the program performed an operation that the interpreter does not support
          
  = note: inside `getpid` at /home/r/.rustup/toolchains/miri/lib/rustlib/src/rust/library/std/src/sys/windows/os.rs:327:14
note: inside `main` at tests/pass/getpid.rs:8:5
 --> tests/pass/getpid.rs:8:5
  |
8 |     getpid();
  |     ^^^^^^^^

Notice the "inside getpid at /home/r/.rustup/toolchains/miri/lib/rustlib/src/rust/library/std/src/sys/windows/os.rs:327:14" -- getpid is our function but the span is in the stdlib!

I guess so far we have been lucky that MIR inlining didn't do this for other calls.

Can we check the current span instead, somehow? That should be stable under inlining (as the backtrace shows).

bors added a commit that referenced this pull request Jun 11, 2022
make frame_in_std check work with inlining

`@InfRandomness` this should help with your trouble in #2215
@b-ncMN
Copy link
Contributor Author

b-ncMN commented Jun 11, 2022

Yeah this is the full error:

error: unsupported operation: can't call foreign function: GetCurrentProcessId
 --> tests/pass/getpid.rs:4:5
  |
4 |     std::process::id()
  |     ^^^^^^^^^^^^^^^^^^ can't call foreign function: GetCurrentProcessId
  |
  = help: this is likely not a bug in the program; it indicates that the program performed an operation that the interpreter does not support
          
  = note: inside `getpid` at /home/r/.rustup/toolchains/miri/lib/rustlib/src/rust/library/std/src/sys/windows/os.rs:327:14
note: inside `main` at tests/pass/getpid.rs:8:5
 --> tests/pass/getpid.rs:8:5
  |
8 |     getpid();
  |     ^^^^^^^^

Notice the "inside getpid at /home/r/.rustup/toolchains/miri/lib/rustlib/src/rust/library/std/src/sys/windows/os.rs:327:14" -- getpid is our function but the span is in the stdlib!

I guess so far we have been lucky that MIR inlining didn't do this for other calls.

Can we check the current span instead, somehow? That should be stable under inlining (as the backtrace shows).

The issue didn't reproduce in the latest CI jobs,
this is odd

@RalfJung
Copy link
Member

The issue didn't reproduce in the latest CI jobs,
this is odd

It only reproduced on windows. And I fixed it in #2225. :)

@RalfJung
Copy link
Member

Thanks!
@bors r+

@bors
Copy link
Contributor

bors commented Jun 11, 2022

📌 Commit 3e03054 has been approved by RalfJung

@b-ncMN
Copy link
Contributor Author

b-ncMN commented Jun 11, 2022

The issue didn't reproduce in the latest CI jobs,
this is odd

It only reproduced on windows. And I fixed it in #2225. :)

Oh, I thought I had to apply that fix in here somewhere,
amazing,
so glad this finally merged

@bors
Copy link
Contributor

bors commented Jun 11, 2022

⌛ Testing commit 3e03054 with merge c5f7a7d...

@bors
Copy link
Contributor

bors commented Jun 11, 2022

☀️ Test successful - checks-actions
Approved by: RalfJung
Pushing c5f7a7d to master...

@bors bors merged commit c5f7a7d into rust-lang:master Jun 11, 2022
bors added a commit that referenced this pull request Jun 28, 2024
…03, r=RalfJung

Remove GetCurrentProcessId's frame_in_std check

Most of the support required to close #1727 was actually added a while back, in #2215.

However, for some reason, even though the Unix/Linux syscall equivalent has no `frame_in_std()` check, the Windows `GetCurrentProcessId` check did. While the vast majority of use cases use `std::process::id`, there's no particular reason to penalize any Windows code that is no_std or for whatever other reason choses to call the function directly (e.g. via the generated [windows-sys](https://docs.rs/windows-sys/latest/windows_sys/Win32/System/Threading/fn.GetCurrentProcessId.html) method). The emulation should still work fine. Given there's no reason not to, we might as well simplify the code a tiny bit and save that branch / frame check during runtime too.

This PR removes the `frame_in_std` restriction for `GetCurrentProcessId`, and also moves it into the environment related shim section per discussion in #1727 (comment).

Still passes existing tests/pass/getpid.rs test.

Closes #1727 unless we wish to give a dummy value when isolated, which we don't seem to want to do at this time.
github-actions bot pushed a commit that referenced this pull request Jan 25, 2025
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.

3 participants