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

mir-interpret: add method to read wide strings from Memory #69326

Merged
merged 1 commit into from
Mar 9, 2020

Conversation

JOE1994
Copy link
Contributor

@JOE1994 JOE1994 commented Feb 20, 2020

Implemented step2 from instructions laid out in rust-lang/miri#707.

Added 2 new methods to struct rustc_mir::interpret::InterpCx.

  • read_os_str_from_wide_str (src/librustc_mir/interpret/operand.rs)
  • write_os_str_to_wide_str (src/librustc_mir/interpret/place.rs)

These methods are intended to be used for environment variable emulation in Windows.

@rust-highfive
Copy link
Collaborator

r? @ecstatic-morse

(rust_highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Feb 20, 2020
@JOE1994
Copy link
Contributor Author

JOE1994 commented Feb 20, 2020

r? @RalfJung

@rust-highfive

This comment has been minimized.

@JOE1994
Copy link
Contributor Author

JOE1994 commented Feb 20, 2020

I have some implementation questions..

  1. Current return type of read_os_str_from_wide_str is InterpResult<'tcx, std::ffi::OsString>. Is that okay as it is? Or should it instead be InterpResult<'tcx, &std::ffi::OsStr> (similar to the method read_str which returns InterpResult<'tcx, &str>)

  2. write_os_str_to_wide_str inserts a NULL terminator to the string to be stored. Currently, read_os_str_from_wide_str doesn't use a NULL terminator to check the length(or termination) of a string. In this case, inserting a NULL terminator, is only being done to mimic behavior of what would be done in Windows. I haven't yet written the NULL terminator discarding logic in read_os_str_from_wide_str.
    I'm not sure whether I should

    • Add NULL terminator discarding logic in read_os_str_from_wide_str.
      OR
    • Remove NULL terminator inserting logic in write_os_str_to_wide_str.

Thank you for reading 👍

@tesuji
Copy link
Contributor

tesuji commented Feb 21, 2020

If the conversion is for Windows only, consider https://doc.rust-lang.org/nightly/std/ffi/index.html#on-windows

@RalfJung
Copy link
Member

RalfJung commented Feb 22, 2020

read_os_str_from_wide_str doesn't use a NULL terminator to check the length(or termination) of a string

That is pretty odd, and why should that work? The users of these APIs have no idea how long the string is going to be.

You used mplace.len(), that makes little sense as it assumes that this is a slice or array. But the case where you need this method is when you got a raw ptr to some memory, and you have no idea how long the string is. That is all you got in the Windows APIs you want to implement: you have a place containing the pointer, which you read to get the pointer, and then you have to work with that. You don't get a place containing the string.

Current return type of read_os_str_from_wide_str is InterpResult<'tcx, std::ffi::OsString>. Is that okay as it is? Or should it instead be InterpResult<'tcx, &std::ffi::OsStr>

There should be nothing about OsStr or OsString on the rustc side, only Miri needs this. On the Miri side, InterpResult<'tcx, std::ffi::OsString> seems like a reasonable return type for read_os_str_from_wide_str. We cannot do &OsStr because we cannot directly use the underlying storage; that is okay.

For in this PR, you only need to add on the rustc side whatever primitives are needed to implement read_os_str_from_wide_str and write_os_str_to_wide_str in Miri. And I think the only primitive you need is Memory::read_wide_c_str, which should be like Memory:read_c_str except that it returns a Vec<u16>. That should use read_scalar to repeatedly read u16 until it finds a NULL, and push everything into a Vec, and return that. (We can't take a shortcut here and directly access the underlying bytes rhe way read_c_str does because we have to care about endianess.)

All that business about actually interpreting this sequence of u16 should be left to Miri.

@RalfJung RalfJung added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Feb 28, 2020
@rust-highfive

This comment has been minimized.

@JOE1994 JOE1994 changed the title mir-interpret: add methods to read/write wide strings mir-interpret: add methods to read wide strings from Memory Mar 3, 2020
@JOE1994 JOE1994 changed the title mir-interpret: add methods to read wide strings from Memory mir-interpret: add method to read wide strings from Memory Mar 3, 2020
@rust-highfive

This comment has been minimized.

@JOE1994 JOE1994 changed the title mir-interpret: add method to read wide strings from Memory (WIP) mir-interpret: add method to read wide strings from Memory Mar 3, 2020
@RalfJung
Copy link
Member

RalfJung commented Mar 4, 2020

Hint: before pushing, always do ./x.py check. That command completes fairly quickly, and it should find errors such as the ones you are hitting now.

@JOE1994 JOE1994 changed the title (WIP) mir-interpret: add method to read wide strings from Memory mir-interpret: add method to read wide strings from Memory Mar 4, 2020
Copy link
Member

@RalfJung RalfJung left a comment

Choose a reason for hiding this comment

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

Yeah this looks good. :)

Could you please squash the commits? No need to have 17 commits for this.^^

@RalfJung
Copy link
Member

RalfJung commented Mar 8, 2020

Ah I had missed that force-push, GH doesn't send emails for those so in the future please let me know that this is ready for review again. :)

@bors r+ rollup

@bors
Copy link
Contributor

bors commented Mar 8, 2020

📌 Commit 05f6482 has been approved by RalfJung

@bors
Copy link
Contributor

bors commented Mar 8, 2020

🌲 The tree is currently closed for pull requests below priority 1000, this pull request will be tested once the tree is reopened

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Mar 8, 2020
bors added a commit that referenced this pull request Mar 8, 2020
Rollup of 7 pull requests

Successful merges:

 - #69120 (Don't give invalid suggestion on desugared span.)
 - #69326 (mir-interpret: add method to read wide strings from Memory)
 - #69608 (Expose target libdir information via print command)
 - #69734 (Change DIBuilderCreateEnumerator signature to match LLVM 9)
 - #69800 (Compile address sanitizer test with debuginfo)
 - #69807 (Cleanup E0391 explanation)
 - #69820 (clean up E0392 explanation)

Failed merges:

r? @ghost
@bors bors merged commit ff96178 into rust-lang:master Mar 9, 2020
@JOE1994 JOE1994 deleted the os_str_widestring branch March 13, 2020 02:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

8 participants