Skip to content

Commit

Permalink
Remove time_t from FFI APIs
Browse files Browse the repository at this point in the history
As per rust-lang/libc#1848, we can't safely do this since the definition
of time_t depends on which version of Musl you're using on 32-bit
systems (the "time64" update for Musl 1.2), and this actually started
biting us on the arm-unknown-linux-musleabihf cross build.
Unfortunately, ISO C makes virtually no guarantees about what time_t is,
so I don't see a practical way to handle the different possibilities
robustly. We'll just cast from i64 or u64 to time_t in C and hope for
the best.
  • Loading branch information
pkgw committed Jun 23, 2022
1 parent 70a6232 commit 9fe378c
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 15 deletions.
9 changes: 3 additions & 6 deletions crates/bridge_core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,7 @@ impl<'a> CoreBridgeState<'a> {
}
}

fn input_get_mtime(&mut self, handle: *mut InputHandle) -> libc::time_t {
fn input_get_mtime(&mut self, handle: *mut InputHandle) -> i64 {
let rhandle: &mut InputHandle = unsafe { &mut *handle };

let maybe_time = match rhandle.get_unix_mtime() {
Expand All @@ -601,7 +601,7 @@ impl<'a> CoreBridgeState<'a> {
};

if let Some(t) = maybe_time {
t as libc::time_t
t
} else {
1 // Intentionally make this distinguishable from the error value 0
}
Expand Down Expand Up @@ -1020,10 +1020,7 @@ pub extern "C" fn ttbc_input_get_size(

/// Get the modification time of a Tectonic input file.
#[no_mangle]
pub extern "C" fn ttbc_input_get_mtime(
es: &mut CoreBridgeState,
handle: *mut InputHandle,
) -> libc::time_t {
pub extern "C" fn ttbc_input_get_mtime(es: &mut CoreBridgeState, handle: *mut InputHandle) -> i64 {
es.input_get_mtime(handle)
}

Expand Down
7 changes: 6 additions & 1 deletion crates/bridge_core/support/support.c
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,12 @@ ttstub_input_get_size(rust_input_handle_t handle)
time_t
ttstub_input_get_mtime(rust_input_handle_t handle)
{
return ttbc_input_get_mtime(tectonic_global_bridge_core, handle);
/* Due to the Musl 1.2 "time64" transition, we can't safely bridge time_t
* between Rust and C code. And formally, ISO C provides nearly no
* guarantees about what the type time_t actually is. So let's just cast and
* hope for the best. */
int64_t ti = ttbc_input_get_mtime(tectonic_global_bridge_core, handle);
return (time_t) ti;
}


Expand Down
4 changes: 2 additions & 2 deletions crates/engine_xdvipdfmx/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ impl XdvipdfmxEngine {
.build_date
.duration_since(SystemTime::UNIX_EPOCH)
.expect("invalid build date")
.as_secs() as libc::time_t,
.as_secs(),
};

let cdvi = CString::new(dvi)?;
Expand Down Expand Up @@ -155,7 +155,7 @@ pub mod c_api {
pub paperspec: *const libc::c_char,
pub enable_compression: libc::c_uchar,
pub deterministic_tags: libc::c_uchar,
pub build_date: libc::time_t,
pub build_date: u64,
}

#[allow(improper_ctypes)] // for CoreBridgeState
Expand Down
5 changes: 4 additions & 1 deletion crates/engine_xdvipdfmx/xdvipdfmx/dvipdfmx.c
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,9 @@ tt_engine_xdvipdfmx_main(
return 99;
}

/* See ttstub_input_get_mtime() in tectonic_bridge_core about bridging time_t
* over FFI. */

rv = dvipdfmx_main(
pdfname,
dviname,
Expand All @@ -509,7 +512,7 @@ tt_engine_xdvipdfmx_main(
(bool) config->deterministic_tags,
false, /* quiet */
0, /* verbose */
config->build_date,
(time_t) config->build_date,
config->paperspec
);

Expand Down
4 changes: 2 additions & 2 deletions crates/engine_xetex/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ impl TexEngine {
self.build_date
.duration_since(SystemTime::UNIX_EPOCH)
.expect("invalid build date")
.as_secs() as libc::time_t,
.as_secs(),
)
};

Expand Down Expand Up @@ -258,7 +258,7 @@ pub mod c_api {
api: &mut CoreBridgeState,
dump_name: *const libc::c_char,
input_file_name: *const libc::c_char,
build_date: libc::time_t,
build_date: u64,
) -> libc::c_int;
}
}
Expand Down
8 changes: 5 additions & 3 deletions crates/engine_xetex/xetex/xetex-engine-interface.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ int tt_engine_xetex_main(
ttbc_state_t *api,
const char *dump_name,
const char *input_file_name,
time_t build_date
uint64_t build_date
);

/* These functions aren't used within the C/C++ library, but are called
Expand Down Expand Up @@ -53,7 +53,7 @@ tt_engine_xetex_main(
ttbc_state_t *api,
const char *dump_name,
const char *input_file_name,
time_t build_date
uint64_t build_date
) {
int rv;

Expand All @@ -62,7 +62,9 @@ tt_engine_xetex_main(
return HISTORY_FATAL_ERROR;
}

rv = tt_run_engine(dump_name, input_file_name, build_date);
/* See ttstub_input_get_mtime() in tectonic_bridge_core about bridging time_t
* over FFI. */
rv = tt_run_engine(dump_name, input_file_name, (time_t) build_date);
ttbc_global_engine_exit();
return rv;
}

0 comments on commit 9fe378c

Please sign in to comment.