Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

py: Fix handling of large number of *args, and add more tests #8472

Merged
merged 4 commits into from Apr 1, 2022

Conversation

dpgeorge
Copy link
Member

There were two issues with the existing code:

  1. "1 << i" is computed as a 32-bit number so would overflow when executed on 64-bit machines (when mp_uint_t is 64-bit). This meant that *args beyond 32 positions would not be handled correctly.

  2. star_args must fit as a positive small int so that it is encoded correctly in the emitted code. MP_SMALL_INT_BITS is too big because it overflows a small int by 1 bit. MP_SMALL_INT_BITS - 1 does not work because it produces a signed small int which is then sign extended when extracted (even by mp_obj_get_int_truncated), and this sign extension means that any position arg after *args is also treated as a star-arg. So the maximum bit position is MP_SMALL_INT_BITS - 2. This means that MP_OBJ_SMALL_INT_VALUE() can be used instead of mp_obj_get_int_truncated() to get the value of star_args.

These are fixed by this PR.

Also, removed an unnecessary check for kw_value == MP_OBJ_NULL.

And added a few tests.

Signed-off-by: Damien George <damien@micropython.org>
@dpgeorge
Copy link
Member Author

@dlech this is a follow up to your work. I would appreciate a review, thanks!

Copy link
Sponsor Contributor

@dlech dlech left a comment

Choose a reason for hiding this comment

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

Nice finds on all of the int issues. 👍

@@ -34,3 +34,6 @@ def f2(*args, **kwargs):


f2(*iter(range(4)), **{'a': 1})

# case where *args is not a tuple/list and takes up most of the memory allocated for **kwargs
f2(*range(100), **{str(i): i for i in range(100)})
Copy link
Sponsor Contributor

@dlech dlech Mar 31, 2022

Choose a reason for hiding this comment

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

range is a sequence, so by itself, it is not any different than tuple/list in this usage. This is why the test above has iter(range(...)) - it hides the __len__.

Copy link
Member Author

Choose a reason for hiding this comment

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

This particular test crashed MicroPython prior to your changes. The point is that range(100) doesn't match the code path that tests if the type is &mp_type_list/&mp_type_tuple.

But maybe it is even better to wrap it in iter(...).

Copy link
Member Author

Choose a reason for hiding this comment

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

You were right, it wasn't testing the correct code path on the new version. Now updated to use iter.



def test(n):
s = "f(" + ",".join(str(i) for i in range(n)) + ", *('a', 'b'), 'c', 'd')"
Copy link
Sponsor Contributor

Choose a reason for hiding this comment

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

This might be a bit more legible using a format string (all of the quotes close to each other make me a bit cross-eyed).

Suggested change
s = "f(" + ",".join(str(i) for i in range(n)) + ", *('a', 'b'), 'c', 'd')"
s = "f({}, *('a', 'b'), 'c', 'd')".format(",".join(str(i) for i in range(n)))

Copy link
Member Author

Choose a reason for hiding this comment

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

updated, and changed the 1-char strings to numbers to make it easier to read

There were two issues with the existing code:

1. "1 << i" is computed as a 32-bit number so would overflow when
   executed on 64-bit machines (when mp_uint_t is 64-bit).  This meant that
   *args beyond 32 positions would not be handled correctly.

2. star_args must fit as a positive small int so that it is encoded
   correctly in the emitted code.  MP_SMALL_INT_BITS is too big because it
   overflows a small int by 1 bit.  MP_SMALL_INT_BITS - 1 does not work
   because it produces a signed small int which is then sign extended when
   extracted (even by mp_obj_get_int_truncated), and this sign extension
   means that any position arg after *args is also treated as a star-arg.
   So the maximum bit position is MP_SMALL_INT_BITS - 2.  This means that
   MP_OBJ_SMALL_INT_VALUE() can be used instead of
   mp_obj_get_int_truncated() to get the value of star_args.

These issues are fixed by this commit, and a test added.

Signed-off-by: Damien George <damien@micropython.org>
The values are always real objects, only the key can be MP_OBJ_NULL to
indicate a **kwargs entry.

Signed-off-by: Damien George <damien@micropython.org>
Signed-off-by: Damien George <damien@micropython.org>
@dpgeorge dpgeorge merged commit 1dbf393 into micropython:master Apr 1, 2022
@dpgeorge dpgeorge deleted the py-fun-call-star-args-test branch April 1, 2022 02:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants