Skip to content

Fix subtle bug in optimize_ops that could seg fault.#651

Merged
lgritz merged 1 commit intoAcademySoftwareFoundation:masterfrom
lgritz:lg-mixbug
Jun 30, 2016
Merged

Fix subtle bug in optimize_ops that could seg fault.#651
lgritz merged 1 commit intoAcademySoftwareFoundation:masterfrom
lgritz:lg-mixbug

Conversation

@lgritz
Copy link
Copy Markdown
Collaborator

@lgritz lgritz commented Jun 30, 2016

The optimize_ops method had a convenience reference to the op being
examined in the loop. We carefully do a reserve() first to make sure
that there was enough room for one insertion so that the reference would
still be valid if the constant-folding function has to add an op. But
this relied on the assumption that no folder would insert more than one
op as it did its work, which was always true (once).

Oops, much later, a new way to speed up "mix" ops had a particular corner
case in which up to 7 new ops might be inserted, and I never went back
and fixed the now-broken assumption here.

The fix here is to change the reference to the op (which cannot be
changed dynamically) into a pointer to the op, which can be re-pointed
after calling the folding function to make sure that it's valid and
pointing to the (potentially new) correct position as it continues on in
the loop. So it is no longer sensitive to changes to the vector
allocation that might happen inside the folder.

I haven't changed this code for a long time. It's shocking that no
problems cropped up from this bug until this week! I guess it's not a
very common "corner case".

@fpsunflower
Copy link
Copy Markdown
Contributor

LGTM

Good catch! Also surprised that we didn't run into this earlier ...

The optimize_ops method had a convenience reference to the op being
examined in the loop.  We carefully do a reserve() first to make sure
that there was enough room for one insertion so that the reference would
still be valid if the constant-folding function has to add an op.  But
this relied on the assumption that no folder would insert more than one
op as it did its work, which was always true (once).

Oops, much later, a new way to speed up "mix" ops had a particular corner
case in which up to 7 new ops might be inserted, and I never went back
and fixed the now-broken assumption here.

The fix here is to change the reference to the op (which cannot be
changed dynamically) into a pointer to the op, which can be re-pointed
after calling the folding function to make sure that it's valid and
pointing to the (potentially new) correct position as it continues on in
the loop. So it is no longer sensitive to changes to the vector
allocation that might happen inside the folder.

I haven't changed this code for a long time. It's shocking that no
problems cropped up from this bug until this week! I guess it's not a
very common "corner case".
@lgritz lgritz merged commit 1d5b94b into AcademySoftwareFoundation:master Jun 30, 2016
@lgritz lgritz deleted the lg-mixbug branch June 30, 2016 21:20
lgritz added a commit to lgritz/OpenShadingLanguage that referenced this pull request Jul 2, 2016
…Foundation#651)

The optimize_ops method had a convenience reference to the op being
examined in the loop.  We carefully do a reserve() first to make sure
that there was enough room for one insertion so that the reference would
still be valid if the constant-folding function has to add an op.  But
this relied on the assumption that no folder would insert more than one
op as it did its work, which was always true (once).

Oops, much later, a new way to speed up "mix" ops had a particular corner
case in which up to 7 new ops might be inserted, and I never went back
and fixed the now-broken assumption here.

The fix here is to change the reference to the op (which cannot be
changed dynamically) into a pointer to the op, which can be re-pointed
after calling the folding function to make sure that it's valid and
pointing to the (potentially new) correct position as it continues on in
the loop. So it is no longer sensitive to changes to the vector
allocation that might happen inside the folder.

I haven't changed this code for a long time. It's shocking that no
problems cropped up from this bug until this week! I guess it's not a
very common "corner case".
ElaraFX pushed a commit to ElaraFX/OpenShadingLanguage that referenced this pull request Jan 28, 2017
…Foundation#651)

The optimize_ops method had a convenience reference to the op being
examined in the loop.  We carefully do a reserve() first to make sure
that there was enough room for one insertion so that the reference would
still be valid if the constant-folding function has to add an op.  But
this relied on the assumption that no folder would insert more than one
op as it did its work, which was always true (once).

Oops, much later, a new way to speed up "mix" ops had a particular corner
case in which up to 7 new ops might be inserted, and I never went back
and fixed the now-broken assumption here.

The fix here is to change the reference to the op (which cannot be
changed dynamically) into a pointer to the op, which can be re-pointed
after calling the folding function to make sure that it's valid and
pointing to the (potentially new) correct position as it continues on in
the loop. So it is no longer sensitive to changes to the vector
allocation that might happen inside the folder.

I haven't changed this code for a long time. It's shocking that no
problems cropped up from this bug until this week! I guess it's not a
very common "corner case".
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.

2 participants