Skip to content

Commit a2f2617

Browse files
committed
Add safe rules for time
1 parent ad633bb commit a2f2617

8 files changed

Lines changed: 939 additions & 1 deletion

File tree

libc-dep/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ edition = "2024"
66
[dependencies]
77
libc = "0.2"
88
nix = { version = "0.30", features = ["socket", "net", "fs", "poll", "time", "user", "dir", "term", "event", "hostname"] }
9+
jiff = "0.2"

rule-preprocessor/src/semantic.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,14 @@ fn build_rustc_args(crate_root: &Path) -> Vec<String> {
4646
args.push("-L".to_string());
4747
args.push(format!("dependency={}", deps.display()));
4848

49-
for dep in &["libcc2rs", "libc", "brotli_sys", "rustls_ffi", "nix"] {
49+
for dep in &[
50+
"libcc2rs",
51+
"libc",
52+
"brotli_sys",
53+
"rustls_ffi",
54+
"nix",
55+
"jiff",
56+
] {
5057
if let Some(rlib) = find_rlib(deps.as_path(), dep) {
5158
args.push("--extern".to_string());
5259
args.push(format!("{}={}", dep, rlib.display()));

rules/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ libcc2rs = { version = "0.1.0", path = "../libcc2rs" }
1414
brotli-sys = "0.3"
1515
rustls-ffi = { version = "0.15.3", default-features = false }
1616
nix = { version = "0.30", features = ["socket", "net", "fs", "poll", "time", "user", "dir", "term", "event", "hostname"] }
17+
jiff = "0.2"

rules/time/tgt_refcount.rs

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,151 @@ fn t2() -> libcc2rs::Timeval {
1414
fn t3() -> libcc2rs::Timespec {
1515
Default::default()
1616
}
17+
18+
fn f1(a0: Ptr<::libc::time_t>) -> ::libc::time_t {
19+
let __out = a0;
20+
match nix::time::clock_gettime(nix::time::ClockId::CLOCK_REALTIME) {
21+
Ok(__ts) => {
22+
let __s = __ts.tv_sec();
23+
if !__out.is_null() {
24+
__out.write(__s);
25+
}
26+
__s
27+
}
28+
Err(__e) => {
29+
libcc2rs::cpp2rust_errno().write(__e as i32);
30+
-1
31+
}
32+
}
33+
}
34+
35+
fn f2(a0: ::libc::clockid_t, a1: Ptr<Timespec>) -> i32 {
36+
match nix::time::clock_gettime(nix::time::ClockId::from_raw(a0)) {
37+
Ok(__ts) => {
38+
a1.with_mut(|__t| {
39+
*__t.tv_sec.borrow_mut() = __ts.tv_sec() as i64;
40+
*__t.tv_nsec.borrow_mut() = __ts.tv_nsec() as i64;
41+
});
42+
0
43+
}
44+
Err(__e) => {
45+
libcc2rs::cpp2rust_errno().write(__e as i32);
46+
-1
47+
}
48+
}
49+
}
50+
51+
fn f4(a0: Ptr<::libc::time_t>, a1: Ptr<Tm>) -> Ptr<Tm> {
52+
let __res = a1;
53+
match jiff::Timestamp::from_second(a0.read()) {
54+
Ok(__ts) => {
55+
let __dt = __ts.to_zoned(jiff::tz::TimeZone::UTC);
56+
__res.with_mut(|__tm| {
57+
*__tm.tm_sec.borrow_mut() = __dt.second() as i32;
58+
*__tm.tm_min.borrow_mut() = __dt.minute() as i32;
59+
*__tm.tm_hour.borrow_mut() = __dt.hour() as i32;
60+
*__tm.tm_mday.borrow_mut() = __dt.day() as i32;
61+
*__tm.tm_mon.borrow_mut() = __dt.month() as i32 - 1;
62+
*__tm.tm_year.borrow_mut() = __dt.year() as i32 - 1900;
63+
*__tm.tm_wday.borrow_mut() = __dt.weekday().to_sunday_zero_offset() as i32;
64+
*__tm.tm_yday.borrow_mut() = __dt.day_of_year() as i32 - 1;
65+
*__tm.tm_isdst.borrow_mut() = 0;
66+
*__tm.tm_gmtoff.borrow_mut() = 0;
67+
*__tm.tm_zone.borrow_mut() = Ptr::from_string_literal(b"GMT");
68+
});
69+
__res
70+
}
71+
Err(_) => {
72+
libcc2rs::cpp2rust_errno().write(::libc::EOVERFLOW);
73+
Ptr::null()
74+
}
75+
}
76+
}
77+
78+
fn f6(a0: Ptr<u8>, a1: usize, a2: Ptr<u8>, a3: Ptr<Tm>) -> usize {
79+
let __dt = a3.with(|__tm| {
80+
jiff::civil::DateTime::new(
81+
(*__tm.tm_year.borrow() + 1900) as i16,
82+
(*__tm.tm_mon.borrow() + 1) as i8,
83+
*__tm.tm_mday.borrow() as i8,
84+
*__tm.tm_hour.borrow() as i8,
85+
*__tm.tm_min.borrow() as i8,
86+
*__tm.tm_sec.borrow() as i8,
87+
0,
88+
)
89+
});
90+
let __text = match __dt {
91+
Ok(__d) => {
92+
jiff::fmt::strtime::format(a2.to_rust_string().as_str(), __d).unwrap_or_default()
93+
}
94+
Err(_) => String::new(),
95+
};
96+
if __text.is_empty() || __text.len() + 1 > a1 {
97+
0
98+
} else {
99+
let mut __dst = a0.clone();
100+
for __b in __text.as_bytes() {
101+
__dst.write(*__b);
102+
__dst += 1;
103+
}
104+
__dst.write(0);
105+
__text.len()
106+
}
107+
}
108+
109+
fn f7(a0: Ptr<u8>, a1: Ptr<Timeval>) -> i32 {
110+
let __times = a1;
111+
let __at = __times.with(|__tv| {
112+
nix::sys::time::TimeVal::new(
113+
*__tv.tv_sec.borrow() as ::libc::time_t,
114+
*__tv.tv_usec.borrow() as ::libc::suseconds_t,
115+
)
116+
});
117+
let __mt = __times.offset(1).with(|__tv| {
118+
nix::sys::time::TimeVal::new(
119+
*__tv.tv_sec.borrow() as ::libc::time_t,
120+
*__tv.tv_usec.borrow() as ::libc::suseconds_t,
121+
)
122+
});
123+
match nix::sys::stat::utimes(a0.to_rust_string().as_str(), &__at, &__mt) {
124+
Ok(()) => 0,
125+
Err(__e) => {
126+
libcc2rs::cpp2rust_errno().write(__e as i32);
127+
-1
128+
}
129+
}
130+
}
131+
132+
#[cfg(target_os = "linux")]
133+
fn f8(a0: Ptr<Timeval>, a1: Ptr<::libc::timezone>) -> i32 {
134+
match nix::time::clock_gettime(nix::time::ClockId::CLOCK_REALTIME) {
135+
Ok(__ts) => {
136+
a0.with_mut(|__tv| {
137+
*__tv.tv_sec.borrow_mut() = __ts.tv_sec() as i64;
138+
*__tv.tv_usec.borrow_mut() = (__ts.tv_nsec() / 1000) as i64;
139+
});
140+
0
141+
}
142+
Err(__e) => {
143+
libcc2rs::cpp2rust_errno().write(__e as i32);
144+
-1
145+
}
146+
}
147+
}
148+
149+
#[cfg(target_os = "macos")]
150+
fn f8(a0: Ptr<Timeval>, a1: AnyPtr) -> i32 {
151+
match nix::time::clock_gettime(nix::time::ClockId::CLOCK_REALTIME) {
152+
Ok(__ts) => {
153+
a0.with_mut(|__tv| {
154+
*__tv.tv_sec.borrow_mut() = __ts.tv_sec() as i64;
155+
*__tv.tv_usec.borrow_mut() = (__ts.tv_nsec() / 1000) as i64;
156+
});
157+
0
158+
}
159+
Err(__e) => {
160+
libcc2rs::cpp2rust_errno().write(__e as i32);
161+
-1
162+
}
163+
}
164+
}

tests/lit/lit/formats/Cpp2RustTest.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,10 @@ def build_rust(self):
212212
libc_dep_deps.glob("libnix-*.rlib"),
213213
key=lambda p: p.stat().st_mtime,
214214
)
215+
jiff_rlib = max(
216+
libc_dep_deps.glob("libjiff-*.rlib"),
217+
key=lambda p: p.stat().st_mtime,
218+
)
215219
cmd = [
216220
"rustc",
217221
"+" + read_rust_version(),
@@ -240,6 +244,8 @@ def build_rust(self):
240244
f"libc={libc_rlib}",
241245
"--extern",
242246
f"nix={nix_rlib}",
247+
"--extern",
248+
f"jiff={jiff_rlib}",
243249
]
244250
_, err, returncode = lit.util.executeCommand(cmd, str(self.tmp_dir))
245251
if exp.should_not_compile:

0 commit comments

Comments
 (0)