Skip to content

Commit e41a770

Browse files
committed
fix(mxc-etw): open ETW trace on caller thread so start_session reports real status
Review item #4 (Shailendra): start_session previously returned Ok(EtwSession) as soon as the pump thread was spawned, but OpenTraceW ran later inside that thread; if it failed we still handed back a live-looking session and logged 'consumer started' (silent failure = false audit coverage). Split the two Win32 calls instead of adding a channel handshake (avoids any lost-wakeup/hang risk): the quick, synchronous OpenTraceW now runs on the caller thread (open_trace), and only the blocking ProcessTrace runs on the pump thread (run_trace). start_session returns Err if OpenTraceW fails (reclaiming the boxed Sender so the consumer disconnects, stopping the session, joining the consumer) and returns Ok/logs 'started' only once capture is genuinely open. Opened handle + LoggerName buffer + boxed Sender are carried to the pump via a Send OpenedTrace so they outlive ProcessTrace. Box-verified on 7F203-MXC-001: consumer started=True, failed-to-start=False, 50 OCSF rows / 50 JSONL, BuffersLost=0 (no regression to capture/emit).
1 parent daad769 commit e41a770

1 file changed

Lines changed: 81 additions & 23 deletions

File tree

‎crates/openshell-driver-mxc/src/etw_consumer.rs‎

Lines changed: 81 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,9 @@ use windows::Win32::System::Diagnostics::Etw::{
6363
CloseTrace, ControlTraceW, EnableTraceEx2, EVENT_HEADER, EVENT_HEADER_EXTENDED_DATA_ITEM,
6464
EVENT_PROPERTY_INFO, EVENT_RECORD, EVENT_TRACE_CONTROL_STOP, EVENT_TRACE_LOGFILEW,
6565
EVENT_TRACE_PROPERTIES, EVENT_TRACE_REAL_TIME_MODE, OpenTraceW, PROCESS_TRACE_MODE_EVENT_RECORD,
66-
PROCESS_TRACE_MODE_REAL_TIME, ProcessTrace, StartTraceW, TdhGetEventInformation,
67-
TRACE_EVENT_INFO, TRACE_LEVEL_VERBOSE, WNODE_FLAG_TRACED_GUID, CONTROLTRACE_HANDLE,
66+
PROCESS_TRACE_MODE_REAL_TIME, PROCESSTRACE_HANDLE, ProcessTrace, StartTraceW,
67+
TdhGetEventInformation, TRACE_EVENT_INFO, TRACE_LEVEL_VERBOSE, WNODE_FLAG_TRACED_GUID,
68+
CONTROLTRACE_HANDLE,
6869
};
6970

7071
use openshell_ocsf::{
@@ -248,11 +249,22 @@ impl Drop for EtwSession {
248249
}
249250
}
250251

251-
/// Wrapper to move a raw `Sender` pointer across the thread boundary into the
252-
/// blocking `ProcessTrace` worker. SAFETY: the boxed `Sender` lives until the
253-
/// worker reclaims it after `ProcessTrace` returns.
254-
struct SendPtr(*mut mpsc::Sender<RawEtwEvent>);
255-
unsafe impl Send for SendPtr {}
252+
/// A successfully-opened real-time trace, handed to the pump thread to run the
253+
/// blocking `ProcessTrace`. Produced by [`open_trace`] on the *caller* thread so
254+
/// an `OpenTraceW` failure is surfaced synchronously (review #4) rather than
255+
/// dying silently on the worker after `start_session` already returned `Ok`.
256+
///
257+
/// SAFETY (`Send`): the contained raw `Sender` pointer and trace handle are only
258+
/// ever touched by the single pump thread that takes ownership of this struct;
259+
/// the boxed `Sender` lives until that thread reclaims it after `ProcessTrace`
260+
/// returns, and `name` (the `LoggerName` buffer `OpenTraceW` referenced) is kept
261+
/// alive for the whole `ProcessTrace` duration.
262+
struct OpenedTrace {
263+
handle: PROCESSTRACE_HANDLE,
264+
name: Vec<u16>,
265+
tx_ptr: *mut mpsc::Sender<RawEtwEvent>,
266+
}
267+
unsafe impl Send for OpenedTrace {}
256268

257269
// ---------------------------------------------------------------------------
258270
// Public API
@@ -308,14 +320,37 @@ pub(crate) fn start_session(index: Arc<Mutex<AttributionIndex>>) -> Result<EtwSe
308320
format!("failed to spawn ETW consumer thread: {e}")
309321
})?;
310322

311-
let send_ptr = SendPtr(Box::into_raw(Box::new(tx)));
312-
let pump_thread = std::thread::Builder::new()
323+
// Open the trace on THIS thread (review #4): `OpenTraceW` is a quick,
324+
// synchronous call, so we can return its failure to the caller instead of
325+
// reporting the session "started" and then having the worker die silently.
326+
// Only the *blocking* `ProcessTrace` runs on the pump thread. On failure we
327+
// reclaim the boxed Sender (which disconnects the consumer's channel so it
328+
// exits), stop the session, and join the consumer before returning `Err`.
329+
let tx_ptr = Box::into_raw(Box::new(tx));
330+
let opened = match open_trace(tx_ptr) {
331+
Ok(o) => o,
332+
Err(e) => {
333+
unsafe { drop(Box::from_raw(tx_ptr)) };
334+
stop_session(handle);
335+
let _ = consumer_thread.join();
336+
return Err(e);
337+
}
338+
};
339+
340+
let pump_thread = match std::thread::Builder::new()
313341
.name("etw-ocsf-pump".into())
314-
.spawn(move || process_trace_loop(send_ptr))
315-
.map_err(|e| {
342+
.spawn(move || run_trace(opened))
343+
{
344+
Ok(t) => t,
345+
Err(e) => {
346+
// The trace is open but we couldn't spawn the pump. Stop the session,
347+
// reclaim the boxed Sender so the consumer disconnects, and join it.
348+
unsafe { drop(Box::from_raw(tx_ptr)) };
316349
stop_session(handle);
317-
format!("failed to spawn ETW pump thread: {e}")
318-
})?;
350+
let _ = consumer_thread.join();
351+
return Err(format!("failed to spawn ETW pump thread: {e}"));
352+
}
353+
};
319354

320355
tracing::info!(session = SESSION_NAME, "MXC ETW→OCSF consumer started (Sandboxing provider)");
321356

@@ -471,9 +506,13 @@ fn cleanup_stale_session() {
471506
// ProcessTrace loop (dedicated blocking thread)
472507
// ---------------------------------------------------------------------------
473508

509+
/// Open the real-time consumer with `OpenTraceW` on the **caller** thread so the
510+
/// result is synchronous (review #4). `tx_ptr` is the boxed event `Sender`; on
511+
/// failure the caller reclaims it (we do not drop it here). On success the boxed
512+
/// `Sender` and the `LoggerName` buffer are handed to the returned [`OpenedTrace`]
513+
/// so they outlive the subsequent blocking `ProcessTrace`.
474514
#[allow(clippy::field_reassign_with_default)]
475-
fn process_trace_loop(send_ptr: SendPtr) {
476-
let tx_ptr = send_ptr.0;
515+
fn open_trace(tx_ptr: *mut mpsc::Sender<RawEtwEvent>) -> Result<OpenedTrace, String> {
477516
let mut name = session_name_wide();
478517

479518
let mut logfile = EVENT_TRACE_LOGFILEW::default();
@@ -483,20 +522,39 @@ fn process_trace_loop(send_ptr: SendPtr) {
483522
logfile.Anonymous2.EventRecordCallback = Some(event_record_callback);
484523
logfile.Context = tx_ptr.cast::<c_void>();
485524

486-
let trace_handle = unsafe { OpenTraceW(&mut logfile) };
487-
if trace_handle.Value == u64::MAX {
488-
tracing::error!(err = %std::io::Error::last_os_error(), "ETW OpenTraceW failed");
489-
// Reclaim the boxed Sender so the consumer thread's channel closes.
490-
unsafe { drop(Box::from_raw(tx_ptr)) };
491-
return;
525+
let handle = unsafe { OpenTraceW(&mut logfile) };
526+
if handle.Value == u64::MAX {
527+
return Err(format!(
528+
"ETW OpenTraceW failed: {}",
529+
std::io::Error::last_os_error()
530+
));
492531
}
493532

494-
let _ = unsafe { ProcessTrace(&[trace_handle], None, None) };
533+
Ok(OpenedTrace {
534+
handle,
535+
name,
536+
tx_ptr,
537+
})
538+
}
539+
540+
/// Run the blocking `ProcessTrace` pump for an already-opened trace, then clean
541+
/// up. Owns [`OpenedTrace`] for its whole lifetime so the `LoggerName` buffer and
542+
/// boxed `Sender` stay valid until `ProcessTrace` returns.
543+
fn run_trace(opened: OpenedTrace) {
544+
let OpenedTrace {
545+
handle,
546+
name,
547+
tx_ptr,
548+
} = opened;
549+
550+
let _ = unsafe { ProcessTrace(&[handle], None, None) };
495551

496552
unsafe {
497-
let _ = CloseTrace(trace_handle);
553+
let _ = CloseTrace(handle);
498554
drop(Box::from_raw(tx_ptr));
499555
}
556+
// Keep the LoggerName buffer alive until ProcessTrace has fully returned.
557+
drop(name);
500558
}
501559

502560
unsafe extern "system" fn event_record_callback(event_record: *mut EVENT_RECORD) {

0 commit comments

Comments
 (0)