Add shims for env var emulation in Windows
This PR attempts to implement the final step of the instructions laid out in https://github.com/rust-lang/miri/issues/707#issuecomment-561564057 , and is yet a work in progress.
### STATUS
- [x] Add general **_target_** methods for **read_str/alloc_str** that dispatch to either **c_str** or **wide_str** variants
(**helpers.rs**)
- [x] Implement shims `fn getenvironmentvariablew`/`fn setenvironmentvariablew`
(`std::env::var()`, `std::env::set_var()`)
- [x] Implement shim `GetEnvironmentStringsW` (`std::env::vars()`)
- [x] Implement shim `FreeEnvironmentStringsW`
### ISSUES (updated on 03/21/2020)
- MIRI errors while running `std::env::remove_var()` in Windows.
MIRI complaining about raw pointer usage in
Rust standard library [*src/libstd/sys/windows/os.rs*](https://github.com/rust-lang/miri/pull/1225#issuecomment-602100146).
### TODO (probably on a separate PR)
- Move string helpers into a new file to avoid bloating **src/helpers.rs** too much. (**shims/os_str.rs**)
Change helper fn 'write_os_str_to_wide_str'
Helper fn `write_os_str_to_wide_str` now works on `Scalar<Tag>`, just like other **read/write os_str** helper functions.
The 2nd commit contains some changes made to `src/shims/env.rs` in PR #1225, in order to keep it from bloating too much.
helper functions for env_var emulation in Windows
Moving some of the changes submitted in PR #1225, in order to prevent the original PR from bloating too much.
fix conditional compilation condition for os_str <-> bytes conversion
Turns out that condition was wrong and these functions never got compiled... Cc @christianpoveda
give some context in error messages
### Some examples for how different errors look now
Unsupported operation:
```
error: unsupported operation: Miri does not support threading
--> /home/r/.rustup/toolchains/miri/lib/rustlib/src/rust/src/libstd/sys/unix/thread.rs:68:19
|
68 | let ret = libc::pthread_create(&mut native, &attr, thread_start, &*p as *const _ as *mut _);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Miri does not support threading
|
= help: this is likely not a bug in the program; it indicates that the program performed an operation that the interpreter does not support
```
Unsupported operation that works without isolation:
```
error: unsupported operation: `clock_gettime` not available when isolation is enabled
--> /home/r/.rustup/toolchains/miri/lib/rustlib/src/rust/src/libstd/sys/unix/time.rs:349:22
|
349 | cvt(unsafe { libc::clock_gettime(clock, &mut t.t) }).unwrap();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `clock_gettime` not available when isolation is enabled
|
= help: pass the flag `-Zmiri-disable-isolation` to disable isolation
```
Program abort:
```
error: program stopped: the evaluated program aborted execution
--> /home/r/.rustup/toolchains/miri/lib/rustlib/src/rust/src/libstd/panicking.rs:530:18
|
530 | unsafe { intrinsics::abort() }
| ^^^^^^^^^^^^^^^^^^^ the evaluated program aborted execution
|
```
UB:
```
error: Undefined Behavior: type validation failed: encountered 2, but expected a boolean
--> tests/compile-fail/validity/invalid_bool.rs:2:23
|
2 | let _b = unsafe { std::mem::transmute::<u8, bool>(2) }; //~ ERROR encountered 2, but expected a boolean
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered 2, but expected a boolean
|
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
```
Experimental UB:
```
error: Undefined Behavior: not granting access to tag <1562> because incompatible item is protected: [Unique for <1567> (call 1189)]
--> tests/compile-fail/stacked_borrows/aliasing_mut1.rs:3:1
|
3 | pub fn safe(_x: &mut i32, _y: &mut i32) {} //~ ERROR protect
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ not granting access to tag <1562> because incompatible item is protected: [Unique for <1567> (call 1189)]
|
= help: this indicates a potential bug in the program: it performed an invalid operation, but the rules it violated are still experimental
```
Fixes https://github.com/rust-lang/miri/issues/417