Skip to content

Conversation

@Arshia001
Copy link
Member

This PR also contains a fix to a potential deadlock in the Linker. Mostly the same implementation as #5879 otherwise.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a potential deadlock in the Linker and addresses a crash that occurs when resolving TLS symbols from modules that don't export their __tls_base. The deadlock fix involves moving the is_closure check earlier in the call chain to avoid holding multiple locks simultaneously.

  • Refactored is_closure to be called before acquiring exclusive store access, preventing potential deadlocks
  • Added proper error handling for TLS symbols without TLS base exports
  • Updated error messages for better clarity when TLS resolution fails

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.

File Description
lib/wasix/src/syscalls/wasix/reflect_signature.rs Moves the is_closure check to execute before data_and_store_mut() to avoid deadlock; reuses the computed cacheable value in error handling
lib/wasix/src/state/linker.rs Adds Display impl for ModuleHandle; adds MissingTlsBaseExport error variant; updates is_closure signature to require FunctionEnvMut parameter; improves TLS error handling with better error propagation
lib/wasix/src/state/handles/mod.rs Updates is_closure to be a static method taking FunctionEnvMut parameter, consistent with the linker API changes

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@zebreus
Copy link
Contributor

zebreus commented Jan 8, 2026

Also, please add a test that doesn't crash when a module is missing the TLS base export.

Comment on lines 1590 to 1601
// Otherwise, fall back to the path where we apply DL ops and acquire
// a write lock afterwards
lock_instance_group_state!(
group_state_guard,
group_state,
self,
LinkError::InstanceGroupIsDead
);
write_linker_state!(linker_state, self, group_state, ctx);
Ok(linker_state
.allocated_closure_functions
.contains_key(&function_id))
Copy link
Contributor

@zebreus zebreus Jan 8, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general I like the try_lock approach. However combining it with the direct locking increases the complexity even more.

We could return an error in case the try_lock fails. The reflection function can then be adjusted to be like

fn reflection() {
  do_pending_linker_ops()?;
  let Ok(is_closure) = is_closure() else {
    trace!("Unlikely")
    return Ok(Errno::Again);
  };
  ...

I think that would simplify the control flow, however it would also mean that reflect_signature can now also return an error if the caller didn't do anything wrong, which is stupid.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is similar to what we are doing in allocate_closure where we return Ok(Errno::Fault) in that case.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But I think both are bad ideas.

Copy link
Contributor

@zebreus zebreus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not happy with the solution in refelct_signature but we need to merge this at some point

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants