Skip to content

Commit

Permalink
Updates to accomodate.
Browse files Browse the repository at this point in the history
  • Loading branch information
Hoyt Koepke committed Jul 23, 2024
1 parent 2640914 commit 554b3dc
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 26 deletions.
10 changes: 0 additions & 10 deletions rust/file_utils/src/safe_file_creator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,6 @@ impl SafeFileCreator {
if let Some(metadata) = self.original_metadata.as_ref() {
set_file_metadata(&self.dest_path, metadata, false)?;
}
let original_permissions = if self.dest_path.exists() {
Some(fs::metadata(&self.dest_path)?.permissions())
} else {
None
};

// Set the original file's permissions to the new file if they exist
if let Some(permissions) = original_permissions {
fs::set_permissions(&self.dest_path, permissions.clone())?;
}

Ok(())
}
Expand Down
5 changes: 2 additions & 3 deletions xetldfs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ mod xet_rfile;

use crate::path_utils::resolve_path_from_fd;
use crate::runtime::{
activate_fd_runtime, interposing_disabled, process_in_interposable_state,
with_interposing_disabled,
activate_fd_runtime, in_interposable_state, interposing_disabled, with_interposing_disabled,
};

#[allow(unused)]
Expand Down Expand Up @@ -452,7 +451,7 @@ hook! {
return real!(mmap)(addr, length, prot, flags, fd, offset);
}

if process_in_interposable_state() {
if in_interposable_state() {
if xet::materialize_file_under_fd(fd) {
ld_trace!("mmap: Materialized pointer file under descriptor {fd}.");
} else {
Expand Down
56 changes: 47 additions & 9 deletions xetldfs/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,14 @@ use tokio::runtime::{Builder, Runtime};
use crate::ld_trace;

thread_local! {
static INTERPOSING_DISABLE_REQUESTS : AtomicU32 = const { AtomicU32::new(0) };
// External interposing is a bit less strict, as interactions between interposed
// functions (e.g. stat) sometimes need to be interposed to be correct.
static EXTERNAL_INTERPOSING_GUARDS : AtomicU32 = const { AtomicU32::new(0) };

// For internal processes, we need the interposing to be complete and strict;
// this covers all requests within xet-core.
// Here, no functions (stat functions included) should be interposed.
static ABSOLUTE_INTERPOSING_DISABLE_GUARDS: AtomicU32 = const { AtomicU32::new(0) };
}

// Guaranteed to be zero on library load for all the static initializers.
Expand All @@ -28,7 +35,7 @@ lazy_static! {
let rt = Builder::new_multi_thread()
.worker_threads(1)
.on_thread_start(|| {
INTERPOSING_DISABLE_REQUESTS.with(|init| {
ABSOLUTE_INTERPOSING_DISABLE_GUARDS.with(|init| {
init.store(1, Ordering::Relaxed);
});
})
Expand Down Expand Up @@ -62,7 +69,18 @@ pub fn tokio_run<F: std::future::Future>(future: F) -> F::Output {
}

// Step 3: Run all the tokio stuff, which may include spawning other processes.
let result = tokio::task::block_in_place(|| TOKIO_RUNTIME.handle().block_on(future));
let result = tokio::task::block_in_place(|| {
// This is needed as the task::block_in_place may either run this on the current thread or
// spin up a new thread to prevent tokio threads from blocking,
// so this ensures that interposing is disabled within this.
let _lg = absolute_interposing_disable();

TOKIO_RUNTIME.handle().block_on(async move {
// May not be necessary here but doesn't hurt.
let _lg = absolute_interposing_disable();
future.await
})
});

// Step 4:
if unsafe { libc::sigaction(SIGCHLD, &old_action, std::ptr::null_mut()) } != 0 {
Expand Down Expand Up @@ -94,11 +112,15 @@ where
}

#[inline]
pub fn process_in_interposable_state() -> bool {
pub fn in_interposable_state() -> bool {
let pid = unsafe { libc::getpid() as u64 };
let s_pid = *FD_RUNTIME_PID;
if pid == s_pid {
true
let ok = ABSOLUTE_INTERPOSING_DISABLE_GUARDS.with(|init| init.load(Ordering::Relaxed) == 0);
if !ok {
ld_trace!("XetLDFS: Disabling interposing from internal interposing state.");
}
ok
} else {
ld_trace!("XetLDFS: process not in interposable state: {pid} != {s_pid}");
false
Expand All @@ -116,13 +138,13 @@ pub fn raw_runtime_activated() -> bool {

#[inline]
pub fn runtime_activated() -> bool {
raw_runtime_activated() && process_in_interposable_state()
raw_runtime_activated() && in_interposable_state()
}

#[inline]
pub fn interposing_disabled() -> bool {
if runtime_activated() {
INTERPOSING_DISABLE_REQUESTS.with(|init| init.load(Ordering::Relaxed) != 0)
EXTERNAL_INTERPOSING_GUARDS.with(|init| init.load(Ordering::Relaxed) != 0)
} else {
true
}
Expand All @@ -132,16 +154,32 @@ pub struct InterposingDisable {}

impl Drop for InterposingDisable {
fn drop(&mut self) {
let v = INTERPOSING_DISABLE_REQUESTS.with(|v| v.fetch_sub(1, Ordering::Relaxed));
let v = EXTERNAL_INTERPOSING_GUARDS.with(|v| v.fetch_sub(1, Ordering::Relaxed));
assert_ne!(v, 0);
}
}

pub fn with_interposing_disabled() -> InterposingDisable {
INTERPOSING_DISABLE_REQUESTS.with(|v| v.fetch_add(1, Ordering::Relaxed));
EXTERNAL_INTERPOSING_GUARDS.with(|v| v.fetch_add(1, Ordering::Relaxed));
InterposingDisable {}
}

////////////////////////
// Internal
pub struct AbsoluteInterposingDisable {}

impl Drop for AbsoluteInterposingDisable {
fn drop(&mut self) {
let v = ABSOLUTE_INTERPOSING_DISABLE_GUARDS.with(|v| v.fetch_sub(1, Ordering::Relaxed));
assert_ne!(v, 0);
}
}

pub fn absolute_interposing_disable() -> AbsoluteInterposingDisable {
ABSOLUTE_INTERPOSING_DISABLE_GUARDS.with(|v| v.fetch_add(1, Ordering::Relaxed));
AbsoluteInterposingDisable {}
}

/////////////
// Some things for the Xet Runtime

Expand Down
6 changes: 2 additions & 4 deletions xetldfs/src/stat_interposing.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use crate::runtime::{
interposing_disabled, process_in_interposable_state, with_interposing_disabled,
};
use crate::runtime::{in_interposable_state, interposing_disabled, with_interposing_disabled};
use crate::{hook, my_open, my_openat, real};
use libc::*;

Expand Down Expand Up @@ -35,7 +33,7 @@ unsafe fn stat_impl(

let r = base();

if !process_in_interposable_state() || r < 0 {
if !in_interposable_state() || r < 0 {
return r;
}

Expand Down

0 comments on commit 554b3dc

Please sign in to comment.