Skip to content

Commit e4ecb94

Browse files
committed
remove sentinel, put statics in .rdata
1 parent 8f07c3a commit e4ecb94

File tree

5 files changed

+16
-40
lines changed

5 files changed

+16
-40
lines changed

src/lib.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,6 @@ macro_rules! etw_event {
215215
};
216216

217217
// These two statics need to be mut to avoid compiler errors about *const EventMetadata not being Sync.
218-
// It's unclear why mut avoids these errors.
219218

220219
paste! {
221220
#[cfg(target_os = "linux")]
@@ -227,7 +226,7 @@ macro_rules! etw_event {
227226

228227
paste! {
229228
#[cfg(target_os = "windows")]
230-
#[link_section = ".rsdata$zRSETW5"]
229+
#[link_section = ".rdata$zRSETW5"]
231230
#[allow(non_upper_case_globals)]
232231
#[used]
233232
static mut [<ETW_META_PTR $name>]: *const $crate::_details::EventMetadata = &ETW_META;

src/native/etw.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ use tracelogging::*;
66
use tracelogging_dynamic::EventBuilder;
77
use tracing_subscriber::registry::{LookupSpan, SpanRef};
88

9-
// Items within this .rsdata section will be sorted alphabetically, thus the start is named with "0", the end "9", and each metadata "5".
9+
// Items within this .rdata section will be sorted alphabetically, thus the start is named with "0", the end "9", and each metadata "5".
1010
// If these statics aren't mut then everything will silently fail to work.
1111
#[allow(non_upper_case_globals)]
12-
#[link_section = ".rsdata$zRSETW0"]
12+
#[link_section = ".rdata$zRSETW0"]
1313
pub(crate) static mut _start__etw_kw: usize = 0;
1414
#[allow(non_upper_case_globals)]
15-
#[link_section = ".rsdata$zRSETW9"]
15+
#[link_section = ".rdata$zRSETW9"]
1616
pub(crate) static mut _stop__etw_kw: usize = 0;
1717

1818
thread_local! {static EBW: std::cell::RefCell<EventBuilder> = RefCell::new(EventBuilder::new());}

src/native/mod.rs

-9
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,6 @@ pub mod noop;
1515
#[cfg(not(any(target_os = "windows", target_os = "linux")))]
1616
#[doc(hidden)]
1717
pub use noop::Provider;
18-
#[cfg(not(any(target_os = "windows", target_os = "linux")))]
19-
#[doc(hidden)]
20-
pub(crate) use noop::_start__etw_kw;
21-
#[cfg(not(any(target_os = "windows", target_os = "linux")))]
22-
#[doc(hidden)]
23-
pub(crate) use noop::_stop__etw_kw;
2418

2519
#[cfg(target_os = "linux")]
2620
#[doc(hidden)]
@@ -41,9 +35,6 @@ pub(crate) use tracelogging_dynamic::Guid as native_guid;
4135
#[cfg(target_os = "linux")]
4236
pub(crate) use eventheader::Guid as native_guid;
4337

44-
// Used to detect the noop implementation, for testing purposes
45-
pub(crate) const MAGIC_STATICS_SENTINEL: usize = usize::from_ne_bytes(*b"NO-OPT00");
46-
4738
use crate::error::EtwError;
4839

4940
#[doc(hidden)]

src/native/noop.rs

-6
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,6 @@ use tracing_subscriber::registry::{LookupSpan, SpanRef};
44

55
use crate::error::EtwError;
66

7-
// We use this magic value to determine
8-
#[allow(non_upper_case_globals)]
9-
pub(crate) static _start__etw_kw: usize = super::MAGIC_STATICS_SENTINEL;
10-
#[allow(non_upper_case_globals)]
11-
pub(crate) static _stop__etw_kw: usize = super::MAGIC_STATICS_SENTINEL;
12-
137
#[doc(hidden)]
148
pub struct Provider;
159

src/statics.rs

+12-20
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,9 @@ pub(crate) static GLOBAL_ACTIVITY_SEED: LazyLock<[u8; 16]> = LazyLock::new(|| {
1919
data
2020
});
2121

22-
static EVENT_METADATA: LazyLock<Box<[ParsedEventMetadata]>> = LazyLock::new(|| {
22+
fn process_static_metadata() -> Box<[ParsedEventMetadata]> {
2323
// The array of pointers are in a mutable section and can be sorted/deduped, but they are pointing to read-only static data
2424

25-
// unsafe is necessary on Linux but not Windows
26-
#[allow(unused_unsafe)]
27-
// SAFETY These aren't "real" extern statics, they are magic statics generated by gcc/llvm that cannot be mutated by other threads.
28-
unsafe {
29-
if crate::native::_start__etw_kw == crate::native::MAGIC_STATICS_SENTINEL {
30-
return Box::new([]);
31-
}
32-
}
33-
3425
let start =
3526
&raw const crate::native::_start__etw_kw as *mut *const EventMetadata;
3627
let stop =
@@ -105,7 +96,13 @@ static EVENT_METADATA: LazyLock<Box<[ParsedEventMetadata]>> = LazyLock::new(|| {
10596
let mut sorted = vec.into_boxed_slice();
10697
sorted.sort_unstable_by(|a, b| b.cmp(a));
10798
sorted
108-
});
99+
}
100+
101+
#[cfg(any(target_os = "windows", target_os = "linux"))]
102+
static EVENT_METADATA: LazyLock<Box<[ParsedEventMetadata]>> = LazyLock::new(process_static_metadata);
103+
104+
#[cfg(not(any(target_os = "windows", target_os = "linux")))]
105+
static EVENT_METADATA: [ParsedEventMetadata; 0] = [];
109106

110107
impl core::cmp::PartialEq for ParsedEventMetadata {
111108
fn eq(&self, other: &Self) -> bool {
@@ -223,15 +220,10 @@ mod test {
223220
sum += event.kw;
224221
}
225222

226-
#[allow(unused_unsafe)]
227-
let expected = unsafe {
228-
if crate::native::_start__etw_kw != crate::native::MAGIC_STATICS_SENTINEL {
229-
55
230-
}
231-
else {
232-
0
233-
}
234-
};
223+
#[cfg(any(target_os = "windows", target_os = "linux"))]
224+
let expected = 55;
225+
#[cfg(not(any(target_os = "windows", target_os = "linux")))]
226+
let expected = 0;
235227

236228
assert_eq!(sum, expected);
237229
}

0 commit comments

Comments
 (0)