Skip to content

Conversation

brendandahl
Copy link
Collaborator

Fixes #21348

@brendandahl
Copy link
Collaborator Author

I'm guessing this will pick up some failures in other places...

Copy link
Collaborator

@sbc100 sbc100 left a comment

Choose a reason for hiding this comment

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

Nice!

I wonder if we will see any tests fail because of this :)

There could be some cases where a developer might be relying on the VM to automatically inject zero values for missing arguments.. so it might be safer to only report too many arguments and not too few?

@brendandahl
Copy link
Collaborator Author

I wonder if we will see any tests fail because of this :)

There could be some cases where a developer might be relying on the VM to automatically inject zero values for missing arguments.. so it might be safer to only report too many arguments and not too few?

Looks like a number of tests fail where we rely on that behavior. I'll change it to just check for too many arguments.

@sbc100
Copy link
Collaborator

sbc100 commented Feb 15, 2024

I wonder if we will see any tests fail because of this :)
There could be some cases where a developer might be relying on the VM to automatically inject zero values for missing arguments.. so it might be safer to only report too many arguments and not too few?

Looks like a number of tests fail where we rely on that behavior. I'll change it to just check for too many arguments.

I know ASYNCIFY relies on this.. was there anything else?

@brendandahl
Copy link
Collaborator Author

I've looked through a few and it seems the majority come from _emscripten_thread_init.

@brendandahl
Copy link
Collaborator Author

We could try to fix them to be "more correct", but code size could be worse and it would change the behavior of existings APIs. Any thoughts?

@sbc100
Copy link
Collaborator

sbc100 commented Feb 15, 2024

I've looked through a few and it seems the majority come from _emscripten_thread_init.

Is that because the entry point for a pthread sometimes has zero args and sometimes as 1 argument? I think that is hard to work around sadly :( Luckily the zero arg form is technically undefined behaviour so we could just disallow that.

@sbc100
Copy link
Collaborator

sbc100 commented Feb 15, 2024

We could try to fix them to be "more correct", but code size could be worse and it would change the behavior of existings APIs. Any thoughts?

I don't think its worth adding complexity but perhaps there are some cases that are actually bugs.

@brendandahl
Copy link
Collaborator Author

I've looked through a few and it seems the majority come from _emscripten_thread_init.

Is that because the entry point for a pthread sometimes has zero args and sometimes as 1 argument? I think that is hard to work around sadly :( Luckily the zero arg form is technically undefined behaviour so we could just disallow that.

It's mainly from here:

Module['__emscripten_thread_init'](e.data.pthread_ptr, /*is_main=*/0, /*is_runtime=*/0, /*can_block=*/1);
we don't pass in the last two arguments int default_stacksize, int start_profiling

@sbc100
Copy link
Collaborator

sbc100 commented Feb 15, 2024

I've looked through a few and it seems the majority come from _emscripten_thread_init.

Is that because the entry point for a pthread sometimes has zero args and sometimes as 1 argument? I think that is hard to work around sadly :( Luckily the zero arg form is technically undefined behaviour so we could just disallow that.

It's mainly from here:

Module['__emscripten_thread_init'](e.data.pthread_ptr, /*is_main=*/0, /*is_runtime=*/0, /*can_block=*/1);

we don't pass in the last two arguments int default_stacksize, int start_profiling

That one might be easy enough to fix..

@@ -546,7 +547,7 @@ def is_ptr_arg(i):
c_names = {}

def make_call_args(i):
if pre_arg:
if has_pre_arg:
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't see how this is any different to bool(pre_arg) which itself is identical to pre_arg in an if statement, no?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

There's an assignment above that does call_args = pre_arg, then we append to call_args which modifies pre_arg as well.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Alternatively, we could instead clone/copy pre_arg to call_args so modifying one does not modify the other.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ah, in that case I think its better to do call_args = list(pre_arg) or call_args = copy(pre_arg) above.

Copy link
Collaborator

Choose a reason for hiding this comment

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

oops call_args = pre_arg.copy()

Copy link
Collaborator

@sbc100 sbc100 left a comment

Choose a reason for hiding this comment

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

LGTM.

Maybe update the PR title/descrption to mention that we only check for too-many and not too-few and why (because there can be valid use case for passing too few). Maybe add a comment before the assertion to that effect too?

@brendandahl brendandahl changed the title Add assertion checking the number of args passed into exports. Add assertion checking if too many args are passed into exports. Feb 20, 2024
@brendandahl brendandahl enabled auto-merge (squash) March 27, 2024 21:07
@brendandahl brendandahl merged commit fbdd924 into emscripten-core:main Mar 27, 2024
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.

Add assertions for number of arguments for exported functions
2 participants