Skip to content

Conversation

qianxichen233
Copy link
Contributor

@qianxichen233 qianxichen233 commented Aug 20, 2025

This PR is splited from PR #164

This PR focuses on spliting out the fix for qsort function in glibc for wasm compatibility

In C, it is legal to cast a function pointer to another type (potentially with fewer parameters) and then invoke it with the “wrong” number of arguments. The qsort implementation in glibc relies on this trick. However, this behavior violates Wasm’s strict function signature rules and results in a runtime error under Wasm. More discussion here

While it’s possible to work around this with wasm-opt’s emulated function pointer casting, that approach introduces significant overhead across the entire Wasm binary. Instead, this PR directly modifies glibc’s source to remove the qsort function pointer trick, avoiding the need for binary rewriting.

The main change is duplicating the qsort implementation with a function signature that has one fewer parameter. This eliminates the function pointer casting and makes qsort work in a Wasm environment.

@rennergade
Copy link
Contributor

this needs comments explaining the reasons for updates in the code, and a bit more full description here

@rennergade rennergade changed the title fix migration - glibc - qsort fix glibc - qsort fix Aug 20, 2025
@yzhang71 yzhang71 self-assigned this Aug 25, 2025
Copy link
Member

@Yaxuan-w Yaxuan-w left a comment

Choose a reason for hiding this comment

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

It seems moving qsort operations to entirely in user space. I noticed the reason of this listed as "wasm compatibility". I'm not sure if the reason is callback function signature mismatch. Could you make it a bit detailed?

}
libc_hidden_def (qsort)
libc_hidden_def (qsort)
Copy link
Member

Choose a reason for hiding this comment

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

Add new line

@qianxichen233
Copy link
Contributor Author

It seems moving qsort operations to entirely in user space. I noticed the reason of this listed as "wasm compatibility". I'm not sure if the reason is callback function signature mismatch. Could you make it a bit detailed?

See updated PR description

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.

4 participants