|
| 1 | +use std::ffi::OsString; |
| 2 | +use std::fmt::Write; |
1 | 3 | use std::time::{Duration, SystemTime}; |
2 | 4 |
|
| 5 | +use chrono::{DateTime, Datelike, Local, Timelike, Utc}; |
| 6 | + |
3 | 7 | use crate::concurrency::thread::MachineCallback; |
4 | 8 | use crate::*; |
5 | 9 |
|
@@ -107,6 +111,80 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> { |
107 | 111 | Ok(0) |
108 | 112 | } |
109 | 113 |
|
| 114 | + // The localtime() function shall convert the time in seconds since the Epoch pointed to by |
| 115 | + // timer into a broken-down time, expressed as a local time. |
| 116 | + // https://linux.die.net/man/3/localtime_r |
| 117 | + fn localtime_r( |
| 118 | + &mut self, |
| 119 | + timep: &OpTy<'tcx, Provenance>, |
| 120 | + result_op: &OpTy<'tcx, Provenance>, |
| 121 | + ) -> InterpResult<'tcx, Pointer<Option<Provenance>>> { |
| 122 | + let this = self.eval_context_mut(); |
| 123 | + |
| 124 | + this.assert_target_os_is_unix("localtime_r"); |
| 125 | + this.check_no_isolation("`localtime_r`")?; |
| 126 | + |
| 127 | + let timep = this.deref_pointer(timep)?; |
| 128 | + let result = this.deref_pointer_as(result_op, this.libc_ty_layout("tm"))?; |
| 129 | + |
| 130 | + // The input "represents the number of seconds elapsed since the Epoch, |
| 131 | + // 1970-01-01 00:00:00 +0000 (UTC)". |
| 132 | + let sec_since_epoch: i64 = this |
| 133 | + .read_scalar(&timep)? |
| 134 | + .to_int(this.libc_ty_layout("time_t").size)? |
| 135 | + .try_into() |
| 136 | + .unwrap(); |
| 137 | + let dt_utc: DateTime<Utc> = |
| 138 | + DateTime::from_timestamp(sec_since_epoch, 0).expect("Invalid timestamp"); |
| 139 | + // Convert that to local time, then return the broken-down time value. |
| 140 | + let dt: DateTime<Local> = DateTime::from(dt_utc); |
| 141 | + |
| 142 | + // This value is always set to -1, because there is no way to know if dst is in effect with |
| 143 | + // chrono crate yet. |
| 144 | + // This may not be consistent with libc::localtime_r's result. |
| 145 | + let tm_isdst = -1; |
| 146 | + |
| 147 | + // tm_zone represents the timezone value in the form of: +0730, +08, -0730 or -08. |
| 148 | + // 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; |
| 151 | + let mut tm_zone = String::new(); |
| 152 | + if offset_in_second < 0 { |
| 153 | + tm_zone.push('-'); |
| 154 | + } else { |
| 155 | + tm_zone.push('+'); |
| 156 | + } |
| 157 | + let offset_hour = offset_in_second.abs() / 3600; |
| 158 | + write!(tm_zone, "{:02}", offset_hour).unwrap(); |
| 159 | + let offset_min = (offset_in_second.abs() % 3600) / 60; |
| 160 | + if offset_min != 0 { |
| 161 | + write!(tm_zone, "{:02}", offset_min).unwrap(); |
| 162 | + } |
| 163 | + |
| 164 | + // FIXME: String de-duplication is needed so that we only allocate this string only once |
| 165 | + // even when there are multiple calls to this function. |
| 166 | + let tm_zone_ptr = |
| 167 | + this.alloc_os_str_as_c_str(&OsString::from(tm_zone), MiriMemoryKind::Machine.into())?; |
| 168 | + |
| 169 | + this.write_pointer(tm_zone_ptr, &this.project_field_named(&result, "tm_zone")?)?; |
| 170 | + this.write_int_fields_named( |
| 171 | + &[ |
| 172 | + ("tm_sec", dt.second().into()), |
| 173 | + ("tm_min", dt.minute().into()), |
| 174 | + ("tm_hour", dt.hour().into()), |
| 175 | + ("tm_mday", dt.day().into()), |
| 176 | + ("tm_mon", dt.month0().into()), |
| 177 | + ("tm_year", dt.year().checked_sub(1900).unwrap().into()), |
| 178 | + ("tm_wday", dt.weekday().num_days_from_sunday().into()), |
| 179 | + ("tm_yday", dt.ordinal0().into()), |
| 180 | + ("tm_isdst", tm_isdst), |
| 181 | + ("tm_gmtoff", tm_gmtoff.into()), |
| 182 | + ], |
| 183 | + &result, |
| 184 | + )?; |
| 185 | + |
| 186 | + Ok(result.ptr()) |
| 187 | + } |
110 | 188 | #[allow(non_snake_case, clippy::arithmetic_side_effects)] |
111 | 189 | fn GetSystemTimeAsFileTime( |
112 | 190 | &mut self, |
|
0 commit comments