Skip to content

Commit 1c7e827

Browse files
committed
Auto merge of rust-lang#3523 - saethlin:localtime_r-env, r=RalfJung
Use the interpreted program's TZ variable in localtime_r This requires a bit of wiring and a new dependency, but the tests should correctly pass now regardless of what the host's time zone is. Fixes rust-lang/miri#3522
2 parents bc8ea93 + 7afef08 commit 1c7e827

File tree

6 files changed

+153
-151
lines changed

6 files changed

+153
-151
lines changed

src/tools/miri/Cargo.lock

+76-125
Original file line numberDiff line numberDiff line change
@@ -37,21 +37,6 @@ dependencies = [
3737
"memchr",
3838
]
3939

40-
[[package]]
41-
name = "android-tzdata"
42-
version = "0.1.1"
43-
source = "registry+https://github.com/rust-lang/crates.io-index"
44-
checksum = "e999941b234f3131b00bc13c22d06e8c5ff726d1b6318ac7eb276997bbb4fef0"
45-
46-
[[package]]
47-
name = "android_system_properties"
48-
version = "0.1.5"
49-
source = "registry+https://github.com/rust-lang/crates.io-index"
50-
checksum = "819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311"
51-
dependencies = [
52-
"libc",
53-
]
54-
5540
[[package]]
5641
name = "annotate-snippets"
5742
version = "0.9.2"
@@ -121,12 +106,6 @@ dependencies = [
121106
"serde",
122107
]
123108

124-
[[package]]
125-
name = "bumpalo"
126-
version = "3.16.0"
127-
source = "registry+https://github.com/rust-lang/crates.io-index"
128-
checksum = "79296716171880943b8470b5f8d03aa55eb2e645a4874bdbb28adb49162e012c"
129-
130109
[[package]]
131110
name = "camino"
132111
version = "1.1.6"
@@ -177,10 +156,29 @@ version = "0.4.38"
177156
source = "registry+https://github.com/rust-lang/crates.io-index"
178157
checksum = "a21f936df1771bf62b77f047b726c4625ff2e8aa607c01ec06e5a05bd8463401"
179158
dependencies = [
180-
"android-tzdata",
181-
"iana-time-zone",
182159
"num-traits",
183-
"windows-targets 0.52.3",
160+
]
161+
162+
[[package]]
163+
name = "chrono-tz"
164+
version = "0.9.0"
165+
source = "registry+https://github.com/rust-lang/crates.io-index"
166+
checksum = "93698b29de5e97ad0ae26447b344c482a7284c737d9ddc5f9e52b74a336671bb"
167+
dependencies = [
168+
"chrono",
169+
"chrono-tz-build",
170+
"phf",
171+
]
172+
173+
[[package]]
174+
name = "chrono-tz-build"
175+
version = "0.3.0"
176+
source = "registry+https://github.com/rust-lang/crates.io-index"
177+
checksum = "0c088aee841df9c3041febbb73934cfc39708749bf96dc827e3359cd39ef11b1"
178+
dependencies = [
179+
"parse-zoneinfo",
180+
"phf",
181+
"phf_codegen",
184182
]
185183

186184
[[package]]
@@ -249,12 +247,6 @@ dependencies = [
249247
"windows-sys 0.52.0",
250248
]
251249

252-
[[package]]
253-
name = "core-foundation-sys"
254-
version = "0.8.6"
255-
source = "registry+https://github.com/rust-lang/crates.io-index"
256-
checksum = "06ea2b9bc92be3c2baa9334a323ebca2d6f074ff852cd1d7b11064035cd3868f"
257-
258250
[[package]]
259251
name = "cpufeatures"
260252
version = "0.2.12"
@@ -379,29 +371,6 @@ version = "0.28.1"
379371
source = "registry+https://github.com/rust-lang/crates.io-index"
380372
checksum = "4271d37baee1b8c7e4b708028c57d816cf9d2434acb33a549475f78c181f6253"
381373

382-
[[package]]
383-
name = "iana-time-zone"
384-
version = "0.1.60"
385-
source = "registry+https://github.com/rust-lang/crates.io-index"
386-
checksum = "e7ffbb5a1b541ea2561f8c41c087286cc091e21e556a4f09a8f6cbf17b69b141"
387-
dependencies = [
388-
"android_system_properties",
389-
"core-foundation-sys",
390-
"iana-time-zone-haiku",
391-
"js-sys",
392-
"wasm-bindgen",
393-
"windows-core",
394-
]
395-
396-
[[package]]
397-
name = "iana-time-zone-haiku"
398-
version = "0.1.2"
399-
source = "registry+https://github.com/rust-lang/crates.io-index"
400-
checksum = "f31827a206f56af32e590ba56d5d2d085f558508192593743f16b2306495269f"
401-
dependencies = [
402-
"cc",
403-
]
404-
405374
[[package]]
406375
name = "indenter"
407376
version = "0.3.3"
@@ -455,15 +424,6 @@ dependencies = [
455424
"libc",
456425
]
457426

458-
[[package]]
459-
name = "js-sys"
460-
version = "0.3.69"
461-
source = "registry+https://github.com/rust-lang/crates.io-index"
462-
checksum = "29c15563dc2726973df627357ce0c9ddddbea194836909d655df6a75d2cf296d"
463-
dependencies = [
464-
"wasm-bindgen",
465-
]
466-
467427
[[package]]
468428
name = "lazy_static"
469429
version = "1.4.0"
@@ -587,6 +547,7 @@ version = "0.1.0"
587547
dependencies = [
588548
"aes",
589549
"chrono",
550+
"chrono-tz",
590551
"colored",
591552
"ctrlc",
592553
"directories",
@@ -690,6 +651,15 @@ dependencies = [
690651
"windows-targets 0.48.5",
691652
]
692653

654+
[[package]]
655+
name = "parse-zoneinfo"
656+
version = "0.3.0"
657+
source = "registry+https://github.com/rust-lang/crates.io-index"
658+
checksum = "c705f256449c60da65e11ff6626e0c16a0a0b96aaa348de61376b249bc340f41"
659+
dependencies = [
660+
"regex",
661+
]
662+
693663
[[package]]
694664
name = "perf-event-open-sys"
695665
version = "3.0.0"
@@ -699,6 +669,44 @@ dependencies = [
699669
"libc",
700670
]
701671

672+
[[package]]
673+
name = "phf"
674+
version = "0.11.2"
675+
source = "registry+https://github.com/rust-lang/crates.io-index"
676+
checksum = "ade2d8b8f33c7333b51bcf0428d37e217e9f32192ae4772156f65063b8ce03dc"
677+
dependencies = [
678+
"phf_shared",
679+
]
680+
681+
[[package]]
682+
name = "phf_codegen"
683+
version = "0.11.2"
684+
source = "registry+https://github.com/rust-lang/crates.io-index"
685+
checksum = "e8d39688d359e6b34654d328e262234662d16cc0f60ec8dcbe5e718709342a5a"
686+
dependencies = [
687+
"phf_generator",
688+
"phf_shared",
689+
]
690+
691+
[[package]]
692+
name = "phf_generator"
693+
version = "0.11.2"
694+
source = "registry+https://github.com/rust-lang/crates.io-index"
695+
checksum = "48e4cc64c2ad9ebe670cb8fd69dd50ae301650392e81c05f9bfcb2d5bdbc24b0"
696+
dependencies = [
697+
"phf_shared",
698+
"rand",
699+
]
700+
701+
[[package]]
702+
name = "phf_shared"
703+
version = "0.11.2"
704+
source = "registry+https://github.com/rust-lang/crates.io-index"
705+
checksum = "90fcb95eef784c2ac79119d1dd819e162b5da872ce6f3c3abe1e8ca1c082f72b"
706+
dependencies = [
707+
"siphasher",
708+
]
709+
702710
[[package]]
703711
name = "pin-project-lite"
704712
version = "0.2.13"
@@ -931,6 +939,12 @@ dependencies = [
931939
"lazy_static",
932940
]
933941

942+
[[package]]
943+
name = "siphasher"
944+
version = "0.3.11"
945+
source = "registry+https://github.com/rust-lang/crates.io-index"
946+
checksum = "38b58827f4464d87d377d175e90bf58eb00fd8716ff0a62f80356b5e61555d0d"
947+
934948
[[package]]
935949
name = "smallvec"
936950
version = "1.13.1"
@@ -1094,60 +1108,6 @@ version = "0.11.0+wasi-snapshot-preview1"
10941108
source = "registry+https://github.com/rust-lang/crates.io-index"
10951109
checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
10961110

1097-
[[package]]
1098-
name = "wasm-bindgen"
1099-
version = "0.2.92"
1100-
source = "registry+https://github.com/rust-lang/crates.io-index"
1101-
checksum = "4be2531df63900aeb2bca0daaaddec08491ee64ceecbee5076636a3b026795a8"
1102-
dependencies = [
1103-
"cfg-if",
1104-
"wasm-bindgen-macro",
1105-
]
1106-
1107-
[[package]]
1108-
name = "wasm-bindgen-backend"
1109-
version = "0.2.92"
1110-
source = "registry+https://github.com/rust-lang/crates.io-index"
1111-
checksum = "614d787b966d3989fa7bb98a654e369c762374fd3213d212cfc0251257e747da"
1112-
dependencies = [
1113-
"bumpalo",
1114-
"log",
1115-
"once_cell",
1116-
"proc-macro2",
1117-
"quote",
1118-
"syn",
1119-
"wasm-bindgen-shared",
1120-
]
1121-
1122-
[[package]]
1123-
name = "wasm-bindgen-macro"
1124-
version = "0.2.92"
1125-
source = "registry+https://github.com/rust-lang/crates.io-index"
1126-
checksum = "a1f8823de937b71b9460c0c34e25f3da88250760bec0ebac694b49997550d726"
1127-
dependencies = [
1128-
"quote",
1129-
"wasm-bindgen-macro-support",
1130-
]
1131-
1132-
[[package]]
1133-
name = "wasm-bindgen-macro-support"
1134-
version = "0.2.92"
1135-
source = "registry+https://github.com/rust-lang/crates.io-index"
1136-
checksum = "e94f17b526d0a461a191c78ea52bbce64071ed5c04c9ffe424dcb38f74171bb7"
1137-
dependencies = [
1138-
"proc-macro2",
1139-
"quote",
1140-
"syn",
1141-
"wasm-bindgen-backend",
1142-
"wasm-bindgen-shared",
1143-
]
1144-
1145-
[[package]]
1146-
name = "wasm-bindgen-shared"
1147-
version = "0.2.92"
1148-
source = "registry+https://github.com/rust-lang/crates.io-index"
1149-
checksum = "af190c94f2773fdb3729c55b007a722abb5384da03bc0986df4c289bf5567e96"
1150-
11511111
[[package]]
11521112
name = "winapi"
11531113
version = "0.3.9"
@@ -1170,15 +1130,6 @@ version = "0.4.0"
11701130
source = "registry+https://github.com/rust-lang/crates.io-index"
11711131
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
11721132

1173-
[[package]]
1174-
name = "windows-core"
1175-
version = "0.52.0"
1176-
source = "registry+https://github.com/rust-lang/crates.io-index"
1177-
checksum = "33ab640c8d7e35bf8ba19b884ba838ceb4fba93a4e8c65a9059d08afcfc683d9"
1178-
dependencies = [
1179-
"windows-targets 0.52.3",
1180-
]
1181-
11821133
[[package]]
11831134
name = "windows-sys"
11841135
version = "0.48.0"

src/tools/miri/Cargo.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ smallvec = "1.7"
2424
aes = { version = "0.8.3", features = ["hazmat"] }
2525
measureme = "11"
2626
ctrlc = "3.2.5"
27-
chrono = { version = "0.4.38", default-features = false, features = ["clock"] }
27+
chrono = { version = "0.4.38", default-features = false }
28+
chrono-tz = "0.9"
2829
directories = "5"
2930

3031
# Copied from `compiler/rustc/Cargo.toml`.

src/tools/miri/src/shims/env.rs

+13-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::ffi::OsString;
1+
use std::ffi::{OsStr, OsString};
22

33
use rustc_data_structures::fx::FxHashMap;
44

@@ -99,4 +99,15 @@ impl<'tcx> EnvVars<'tcx> {
9999
}
100100

101101
impl<'mir, 'tcx: 'mir> EvalContextExt<'mir, 'tcx> for crate::MiriInterpCx<'mir, 'tcx> {}
102-
pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {}
102+
pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
103+
/// Try to get an environment variable from the interpreted program's environment. This is
104+
/// useful for implementing shims which are documented to read from the environment.
105+
fn get_env_var(&mut self, name: &OsStr) -> InterpResult<'tcx, Option<OsString>> {
106+
let this = self.eval_context_ref();
107+
match &this.machine.env_vars {
108+
EnvVars::Uninit => return Ok(None),
109+
EnvVars::Unix(vars) => vars.get(this, name),
110+
EnvVars::Windows(vars) => vars.get(name),
111+
}
112+
}
113+
}

src/tools/miri/src/shims/time.rs

+18-8
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
use std::ffi::OsString;
1+
use std::ffi::{OsStr, OsString};
22
use std::fmt::Write;
3+
use std::str::FromStr;
34
use std::time::{Duration, SystemTime};
45

5-
use chrono::{DateTime, Datelike, Local, Timelike, Utc};
6+
use chrono::{DateTime, Datelike, Offset, Timelike, Utc};
7+
use chrono_tz::Tz;
68

79
use crate::concurrency::thread::MachineCallback;
810
use crate::*;
@@ -136,8 +138,16 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
136138
.unwrap();
137139
let dt_utc: DateTime<Utc> =
138140
DateTime::from_timestamp(sec_since_epoch, 0).expect("Invalid timestamp");
141+
142+
// Figure out what time zone is in use
143+
let tz = this.get_env_var(OsStr::new("TZ"))?.unwrap_or_else(|| OsString::from("UTC"));
144+
let tz = match tz.into_string() {
145+
Ok(tz) => Tz::from_str(&tz).unwrap_or(Tz::UTC),
146+
_ => Tz::UTC,
147+
};
148+
139149
// Convert that to local time, then return the broken-down time value.
140-
let dt: DateTime<Local> = DateTime::from(dt_utc);
150+
let dt: DateTime<Tz> = dt_utc.with_timezone(&tz);
141151

142152
// This value is always set to -1, because there is no way to know if dst is in effect with
143153
// chrono crate yet.
@@ -146,17 +156,17 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
146156

147157
// tm_zone represents the timezone value in the form of: +0730, +08, -0730 or -08.
148158
// This may not be consistent with libc::localtime_r's result.
149-
let offset_in_second = Local::now().offset().local_minus_utc();
150-
let tm_gmtoff = offset_in_second;
159+
let offset_in_seconds = dt.offset().fix().local_minus_utc();
160+
let tm_gmtoff = offset_in_seconds;
151161
let mut tm_zone = String::new();
152-
if offset_in_second < 0 {
162+
if offset_in_seconds < 0 {
153163
tm_zone.push('-');
154164
} else {
155165
tm_zone.push('+');
156166
}
157-
let offset_hour = offset_in_second.abs() / 3600;
167+
let offset_hour = offset_in_seconds.abs() / 3600;
158168
write!(tm_zone, "{:02}", offset_hour).unwrap();
159-
let offset_min = (offset_in_second.abs() % 3600) / 60;
169+
let offset_min = (offset_in_seconds.abs() % 3600) / 60;
160170
if offset_min != 0 {
161171
write!(tm_zone, "{:02}", offset_min).unwrap();
162172
}

0 commit comments

Comments
 (0)