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

Fix SegFault on serialization of objects #2

Closed

Conversation

SwenVanZanten
Copy link

@SwenVanZanten SwenVanZanten commented Mar 2, 2020

I've encountered a problem with upgrading a codebase to PHP 7.4 using the latest build of this extension.

In this situation we have some objects which contain a GearmanJob object. When serializing and unserializing a bunch of them you'll get a Segmentation Fault.

I'm not familiar with writing code in C and I'm not sure this is the proper solution to the problem.
So correct me if I'm wrong here! However it solves my problem and doesn't introduce any new once... according to the testsuite 😜

@Trainmaster
Copy link

@SwenVanZanten I think that was already fixed in master, see 89db565.

@SwenVanZanten
Copy link
Author

It did fix running in PHP 7.4, however it doesn't work for the test scenario's I've added in the PR.
If you run those on master they'll fail.

@SwenVanZanten
Copy link
Author

@Trainmaster hi any chance to check this PR out?

@oleg-st
Copy link
Contributor

oleg-st commented Mar 21, 2020

@SwenVanZanten
I can confirm the problem.
I think better solution is to remove zend_object_std_dtor call in destructors.
Because it is called in free_obj by default.

@C4RoCKeT
Copy link

I tried your fixes, and it completes all the tests, but I'm still getting segmentation faults using PHP 7.4.3.

@SwenVanZanten
Copy link
Author

I tried your fixes, and it completes all the tests, but I'm still getting segmentation faults using PHP 7.4.3.

With serialization & unserialization or another use case?

@Trainmaster
Copy link

Maybe @nikic can help us out at this point (php/php-src@1cfbb21). Because I would say that

It is recommended to initialize object handler structures by copying the std object handlers and only overwriting those you want to change.

was implemented by afd2ed8.

@nikic
Copy link
Member

nikic commented Mar 21, 2020

@Trainmaster Presumably there was a reason why the object destruction was previously skipped -- most likely, it was to work around a refcounting bug somewhere else.

From a quick look, I'd assume that this kind of code is the root problem:

/* {{{ proto object GearmanTask::__destruct()
Destroys a task object */
PHP_METHOD(GearmanTask, __destruct) {
gearman_task_obj *intern = Z_GEARMAN_TASK_P(getThis());
if (!intern) {
return;
}
zval_dtor(&intern->zworkload);
zval_dtor(&intern->zdata);
zval_dtor(&intern->zclient);
zend_object_std_dtor(&intern->std);
}
That code should be inside the free_obj handler, not in __destruct(). Putting it in __destruct() is trivially unsound if you consider that the method may be manually invoked multiple times.

@C4RoCKeT
Copy link

I tried your fixes, and it completes all the tests, but I'm still getting segmentation faults using PHP 7.4.3.

With serialization & unserialization or another use case?

Actually, just using $client = new GearmanClient(); will cause one.

@SwenVanZanten
Copy link
Author

@Trainmaster Presumably there was a reason why the object destruction was previously skipped -- most likely, it was to work around a refcounting bug somewhere else.

From a quick look, I'd assume that this kind of code is the root problem:

/* {{{ proto object GearmanTask::__destruct()
Destroys a task object */
PHP_METHOD(GearmanTask, __destruct) {
gearman_task_obj *intern = Z_GEARMAN_TASK_P(getThis());
if (!intern) {
return;
}
zval_dtor(&intern->zworkload);
zval_dtor(&intern->zdata);
zval_dtor(&intern->zclient);
zend_object_std_dtor(&intern->std);
}

That code should be inside the free_obj handler, not in __destruct(). Putting it in __destruct() is trivially unsound if you consider that the method may be manually invoked multiple times.

Thnx @nikic !

I'll give it another go tomorrow

@SwenVanZanten
Copy link
Author

Added extra test cases for GearmanTask and GearmanWorker

@ekosogin
Copy link

If it's stable please push tag with new version

@Trainmaster
Copy link

@rlerdorf removed the Note: This is no longer under active development.. Maybe he can tell us who's in charge of this repository now.

@mmoll
Copy link

mmoll commented Aug 11, 2020

I can confirm, this PR fixes segfaults seen with PHP 7.4. 👍🏿

@NoiseEee
Copy link

Hi folks, when can we see all these pull requests finally merged??

@ekosogin
Copy link

I also confirm it's stable

rlerdorf added a commit that referenced this pull request Jan 16, 2021
@rlerdorf rlerdorf closed this Jan 16, 2021
@oleg-st oleg-st mentioned this pull request Jan 16, 2021
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.

None yet

9 participants