Skip to content

Rewrite your app in Rust, piece by piece, checked against the original at every step. Works with TypeScript, JavaScript, Python, Go, Ruby, Java, C, PHP, C#, Kotlin, PowerShell and Swift.

C: count clock_gettime, gettimeofday and timespec_get as clock reads (#1437)

jakalijeyc558355fee00
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 9e4d01a1..644f91a2 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -278,6 +278,8 @@
- PowerShell: a parameter typed `[decimal]`, `[uint64]` or `[ulong]` keeps its function in
PowerShell, with the reasons C# gives for the same types, instead of bridging it as `f64` or
`i64`. `[uint32]`, `[double]` and `[single]` bridge as before (#1341 by @jakalijey).
+- C: `clock_gettime`, `gettimeofday` and `timespec_get` count as clock reads in `analyze` and
+ `bridge`, as `time` and `clock` already did (#1319 by @jakalijey).
- `analyze` no longer calls a Python, Go, Java or other non-JS unit "JS-shaped" when every export
stays in its language for another reason: the note gives the reason `bridge` gives for the first
kept export (`stamp` reads the clock, ...), and "Start here" no longer says such units expose
diff --git a/src/lang/effects.rs b/src/lang/effects.rs
index 739d7fa3..46e020b0 100644
--- a/src/lang/effects.rs
+++ b/src/lang/effects.rs
@@ -356,6 +356,9 @@ fn clock_and_random(lang: Lang) -> &'static [&'static str] {
"time(",
"rand(",
"clock(",
+ "clock_gettime(",
+ "gettimeofday(",
+ "timespec_get(",
"random(",
"arc4random(",
"arc4random_uniform(",
@@ -1574,6 +1577,22 @@ mod tests {
] {
assert_eq!(reason(Lang::Csharp, &[], code), None, "{code}");
}
+ // C's finer clocks (#1319).
+ for code in [
+ "long now_ns(void) {\n struct timespec ts;\n clock_gettime(CLOCK_MONOTONIC, &ts);\n return ts.tv_sec;\n}\n",
+ "long now_us(void) {\n struct timeval tv;\n gettimeofday(&tv, 0);\n return tv.tv_sec;\n}\n",
+ "long now_c11(void) {\n struct timespec ts;\n timespec_get(&ts, TIME_UTC);\n return ts.tv_sec;\n}\n",
+ ] {
+ assert!(reason(Lang::C, &[], code).is_some(), "{code}");
+ let facts = crate::lang::scan(Lang::C, "clock.c", code);
+ assert!(
+ facts
+ .risks
+ .contains_key(&crate::risks::Risk::ClockAndRandom),
+ "{code}: {:?}",
+ facts.risks
+ );
+ }
for body in ["process.hrtime.bigint()", "crypto.randomInt(6)"] {
let code = format!("export function f() {{\n return {body}\n}}\n");
assert!(reason(Lang::Ts, &[], &code).is_some(), "{body}");