Skip to content

Fix array going away during sorting #16654

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

Closed
wants to merge 1 commit into from
Closed

Conversation

iluuu1994
Copy link
Member

@iluuu1994 iluuu1994 commented Oct 31, 2024

Fixes GH-16648

Zend/zend_hash.c Outdated
Comment on lines 2965 to 2969
/* The array _should_ be added to the gc buffer. However, this causes an
* issue for non-user hashtables that use zend_hash_destroy(), which does
* not remove the destroyed array from the gc buffer, assuming it was
* never added. */
// gc_check_possible_root((zend_refcounted *)ht);
Copy link
Member

Choose a reason for hiding this comment

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

You may try to move this locking code into asort() and others.

Copy link
Member Author

Choose a reason for hiding this comment

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

We'll have to (re)move HT_ASSERT_RC1(ht); from zend_hash_sort_ex(), but I think this should work.

Copy link
Member

Choose a reason for hiding this comment

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

moving HT_ASSERT_RC1(ht); is bad :(

Copy link
Member Author

Choose a reason for hiding this comment

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

@dstogov We could introduce another variant of zend_hash_sort_ex() that omits it, which can be used explicitly for cases like this. We could also add HASH_FLAG_ALLOW_COW_VIOLATION, but since user-code may run during sorting this is likely worse.

Copy link
Member

Choose a reason for hiding this comment

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

I think, zend_array_sort() with the new protection should work.

@iluuu1994
Copy link
Member Author

Since this now adds new API, it's likely better to just fix in master.

@bwoebi
Copy link
Member

bwoebi commented Oct 31, 2024

@iluuu1994 Adding new API has never been a blocker. (Changing API, yes.) And it does fix a real bug.

@dstogov
Copy link
Member

dstogov commented Nov 1, 2024

@iluuu1994 Adding new API has never been a blocker. (Changing API, yes.) And it does fix a real bug.

Actually, it's better not to add new API calls.
I suggest to remove ZEND_API for PHP-8.2/8.3 and re-add it for PHP-8.4 and master.
Everything else looks fine.

@iluuu1994
Copy link
Member Author

iluuu1994 commented Nov 1, 2024

Right. What I was referencing was this scenario: Extensions start using the new API, the user updates to the new extension without updating PHP, after which dynamic loading of the extension would fail. This can be prevented by the approach Dmitry described. Another upside is that the API may actually still be changed until master is eventually released, if we were to find some flaw in it.

@bukka
Copy link
Member

bukka commented Nov 2, 2024

It's ok to add a new API but probably a bit nicer for extension authors not to add it in if it's not necessary. So agree that keeping it internal in general might be better even though it's already in exported headers and it's just a hint for most users (all except Windows where ZEND_API actually matters). However there is extra bit in this case because we usually take inline functions in headers as part of the ABI so using function that is not part of the ABI in it might be a bit surprising

@iluuu1994 iluuu1994 closed this in 2bdce61 Nov 4, 2024
@iluuu1994
Copy link
Member Author

iluuu1994 commented Nov 4, 2024

all except Windows where ZEND_API actually matters

FWIW, ZEND_API matters for all extensions that are dynamically built. That would include basically all user extensions, and optional extensions on most distributions. In those cases, loading the extension will immediately fail, so it should be obvious that these functions shouldn't be used. Statically linked extensions should be able to use the functions without any problems.

Edit: And just as I'm writing this, I realized we shouldn't use this function for ext/intl/collator/collator_sort.c yet. 🙂

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

Successfully merging this pull request may close these issues.

4 participants