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

Add better cursor support in stack allocators #1012

Merged
merged 21 commits into from
Aug 2, 2023
Merged

Add better cursor support in stack allocators #1012

merged 21 commits into from
Aug 2, 2023

Conversation

johnse-hypixel
Copy link
Contributor

@johnse-hypixel johnse-hypixel commented Jul 28, 2023

This PR fixes a bug in the stack allocator that can cause memory corruption in scenarios where multiple (nested) iterators are iterated.

=== Original message ===
This PR is to fix some tests that have errors hidden by a large bug. These test cases were not properly finishing iteration, nor calling ecs_iter_fini(). Furthermore, there are cases of overlapping lifetimes of objects allocated on the stack allocator.

The test failures in subsequent CI runs shows the errors found be adding consistency checks to the stack allocator. Then as the new tracking of freed data improved, and fixes in some test cases, the CI errors were eliminated over time.

The existing test cases exercised the various lifetime issues that the new tracking is designed to support. Behavior of the allocator is:

  • new allocations only occur at top of stack even if there are freed spaces further down
  • memory is only returned to the stack when the top-of-stack cursor is restored.
  • If we have four iterators on the stack: A {B} {C} D (B&C marked free):
    • Freeing D will return B, C, and D and leave A as the top of stack (A becomes the tailCursor)
    • Allocating E will result in A, E

The first commit intentionally has the fixes commented out and additional checks to confirm that the iterator is finished. This first commit will therefore fail CI tests a total of 7 failures.

The second commit will show these are fixed.

@johnse-hypixel johnse-hypixel changed the title Add extra tests that iterators are invalidated (fixes are commented out) Add better cursor support in stack allocators Jul 31, 2023
Copy link
Contributor Author

@johnse-hypixel johnse-hypixel left a comment

Choose a reason for hiding this comment

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

Looks great! Very nice refactor to return the cursor pointer. Much less bookkeeping.

I can't hit the "Approve", but I approve these changes!

@@ -841,6 +841,8 @@ ecs_iter_t ecs_page_iter(
ecs_check(it->next != NULL, ECS_INVALID_PARAMETER, NULL);

ecs_iter_t result = *it;
result.priv.cache.stack_cursor = NULL; /* Don't copy allocator cursor */
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes. These were the places where I ran into issues with storing pointers. Glad you had the context to know what was expected of the wrapper iterator!

@SanderMertens SanderMertens merged commit 36338a9 into SanderMertens:master Aug 2, 2023
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