Skip to content

Fix UAF/Iterator Invalidation issue in optimization context.#4193

Merged
copybara-service[bot] merged 1 commit intomainfrom
test_908984112
May 4, 2026
Merged

Fix UAF/Iterator Invalidation issue in optimization context.#4193
copybara-service[bot] merged 1 commit intomainfrom
test_908984112

Conversation

@copybara-service
Copy link
Copy Markdown

Fix UAF/Iterator Invalidation issue in optimization context.

The OptimizationContext contains (among other things) a map from FunctionBase* to the topo sort of that function. This topo sort is stored in a list that is made to delete itself if the function is changed using a ChangeListener.

If one of the functionBases with this topo sort list is deleted the OptimizationContext was not notified so both a dangling pointer to the function and a (now detached) change listener was kept around.

This could lead to use after free and iterator invalidation issues.

First there is the obvious risk that if a pointer is reissued by the allocator map index operations could incorrectly return data for the old function.

More likely however if a new FunctionBase is created the resize of the map will cause UAF when we try to add the copied listeners onto the deleted function (and again when we try to delete the moved-away listeners for that matter).

This needs to be solved by having the change-listener inform the OptimizationContext that it needs to be deleted and not calling UnregisterChangeListener in this case because the function it's attached to is currently iterating over the list of change-listeners so modifying it could invalidate iterators.

The OptimizationContext contains (among other things) a map from FunctionBase* to the topo sort of that function. This topo sort is stored in a list that is made to delete itself if the function is changed using a ChangeListener.

If one of the functionBases with this topo sort list is deleted the OptimizationContext was not notified so both a dangling pointer to the function and a (now detached) change listener was kept around.

This could lead to use after free and iterator invalidation issues.

First there is the obvious risk that if a pointer is reissued by the allocator map index operations could incorrectly return data for the old function.

More likely however if a new FunctionBase is created the resize of the map will cause UAF when we try to add the copied listeners onto the deleted function (and again when we try to delete the moved-away listeners for that matter).

This needs to be solved by having the change-listener inform the OptimizationContext that it needs to be deleted and not calling UnregisterChangeListener in this case because the function it's attached to is currently iterating over the list of change-listeners so modifying it could invalidate iterators.

PiperOrigin-RevId: 910157057
@copybara-service copybara-service Bot merged commit ae934e2 into main May 4, 2026
@copybara-service copybara-service Bot deleted the test_908984112 branch May 4, 2026 18:54
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.

1 participant