fix: const-initialized thread locals
Signed-off-by: volcano0dr <volcano_dr@163.com>
diff --git a/sgx_tstd/src/thread/local.rs b/sgx_tstd/src/thread/local.rs
index a3440bd..aa009d0 100644
--- a/sgx_tstd/src/thread/local.rs
+++ b/sgx_tstd/src/thread/local.rs
@@ -157,9 +157,9 @@
static mut VAL: $t = $init;
Ok(&VAL)
} else {
- Err(AccessError {
- msg: "If TLS data needs to be destructed, TCS policy must be bound.",
- })
+ Err($crate::thread::AccessError::new(
+ "If TLS data needs to be destructed, TCS policy must be bound."
+ ))
}
}
@@ -205,6 +205,12 @@
msg: &'static str,
}
+impl AccessError {
+ pub fn new(msg: &'static str) -> Self {
+ Self { msg }
+ }
+}
+
impl fmt::Display for AccessError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(self.msg, f)
@@ -368,9 +374,9 @@
};
Ok(value)
} else {
- Err(AccessError {
- msg: "If TLS data needs to be destructed, TCS policy must be bound.",
- })
+ Err(AccessError::new(
+ "If TLS data needs to be destructed, TCS policy must be bound."
+ ))
}
}
}
@@ -470,9 +476,9 @@
#[inline(never)]
unsafe fn try_initialize<F: FnOnce() -> T>(&self, init: F) -> Result<&'static T, AccessError> {
if mem::needs_drop::<T>() && thread::thread_policy() == SgxThreadPolicy::Unbound {
- return Err(AccessError {
- msg: "If TLS data needs to be destructed, TCS policy must be bound.",
- });
+ return Err(AccessError::new(
+ "If TLS data needs to be destructed, TCS policy must be bound."
+ ));
}
if !super::pthread_info_tls.m_pthread.is_null() {
@@ -489,9 +495,7 @@
// SAFETY: See comment above (his function doc).
Ok(self.inner.initialize(init))
} else {
- Err(AccessError {
- msg: "Failed to register destructor.",
- })
+ Err(AccessError::new("Failed to register destructor."))
}
} else {
Ok(self.inner.initialize(init))
@@ -604,9 +608,7 @@
let ptr = self.os.get() as *mut Value<T>;
if ptr as usize == 1 {
// destructor is running
- return Err(AccessError {
- msg: "Destructor is running.",
- });
+ return Err(AccessError::new("Destructor is running."));
}
let ptr = if ptr.is_null() {