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

Use the interpreted program's TZ variable in localtime_r #3523

Merged
merged 3 commits into from
Apr 29, 2024

Conversation

saethlin
Copy link
Member

@saethlin saethlin commented Apr 27, 2024

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 #3522

@saethlin saethlin marked this pull request as ready for review April 27, 2024 19:02
Cargo.toml Outdated
@@ -25,6 +25,7 @@ aes = { version = "0.8.3", features = ["hazmat"] }
measureme = "11"
ctrlc = "3.2.5"
chrono = { version = "0.4.38", default-features = false, features = ["clock"] }
chrono-tz = "0.4"
Copy link
Member

Choose a reason for hiding this comment

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

Why did you add such an ancient version of chrono-tz? The current version is 0.9.

Copy link
Member Author

Choose a reason for hiding this comment

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

Probably because 4 looks like 9.

let tz = this.get_var(OsStr::new("TZ"))?.unwrap_or_else(|| OsString::from("UTC"));
let tz = match tz.into_string() {
Ok(tz) => Tz::from_str(&tz).unwrap_or(Tz::Zulu),
_ => Tz::Zulu,
Copy link
Member

Choose a reason for hiding this comment

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

Why is Zulu the fallback, not UTC...?

@@ -146,17 +156,17 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {

// tm_zone represents the timezone value in the form of: +0730, +08, -0730 or -08.
// This may not be consistent with libc::localtime_r's result.
let offset_in_second = Local::now().offset().local_minus_utc();
let tm_gmtoff = offset_in_second;
let offset_in_seconds = Utc::now().with_timezone(&tz).offset().fix().local_minus_utc();
Copy link
Member

Choose a reason for hiding this comment

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

Can't dt be used for this, why do we need Utc::now?

Cargo.toml Outdated
@@ -25,6 +25,7 @@ aes = { version = "0.8.3", features = ["hazmat"] }
measureme = "11"
ctrlc = "3.2.5"
chrono = { version = "0.4.38", default-features = false, features = ["clock"] }
Copy link
Member

Choose a reason for hiding this comment

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

If I understand the docs correctly, we don't need the clock feature any more?

Size::from_bytes(u64::try_from(name.len()).unwrap().checked_add(1).unwrap()),
ecx,
)?;
ecx.read_os_str_from_c_str(var_ptr).map(|s| Some(s.to_owned()))
Copy link
Member

Choose a reason for hiding this comment

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

This duplicates a bunch of code in getenv, doesn't it? Can the code be shared, e.g. by having getenv call this?

Copy link
Member Author

Choose a reason for hiding this comment

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

It does duplicate some code. getenv can't call it as-written but with some rearrangement it's possible to share the logic.

src/shims/env.rs Outdated
pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
/// Try to get an environment variable from the interpreted program's environment. This is
/// useful for implementing shims which are documented to read from the environment.
fn get_var(&mut self, name: &OsStr) -> InterpResult<'tcx, Option<OsString>> {
Copy link
Member

Choose a reason for hiding this comment

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

get_env_var may be better since this is a global namespace.

@RalfJung
Copy link
Member

@bors r+

@bors
Copy link
Contributor

bors commented Apr 29, 2024

📌 Commit 92715f5 has been approved by RalfJung

It is now in the queue for this repository.

@bors
Copy link
Contributor

bors commented Apr 29, 2024

⌛ Testing commit 92715f5 with merge fc0db10...

@bors
Copy link
Contributor

bors commented Apr 29, 2024

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

@bors bors merged commit fc0db10 into rust-lang:master Apr 29, 2024
8 checks passed
@saethlin saethlin deleted the localtime_r-env branch May 27, 2024 22:10
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.

localtime_r shim uses TZ variable from the host, not the interpreted program
3 participants