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

test cache page allocation failure #442

Closed
goatshriek opened this issue Sep 26, 2024 · 3 comments
Closed

test cache page allocation failure #442

goatshriek opened this issue Sep 26, 2024 · 3 comments
Labels
good first issue something that would be simple for a newcomer to stumpless to work on help wanted external contributations encouraged testing pertains to the test suite

Comments

@goatshriek
Copy link
Owner

Stumpless uses a simple slab cache implementation in src/cache.c to consolidate memory allocations for common structures like entries. Test coverage of this cache is fairly complete, but still has some gaps.

One such gap is the failure of a new page allocation when the cache needs one. You will test for this case by causing a memory allocation failure after the cache has been set up and needs to add a new page.

General Approach

There are a few details left out of the following approach, for you to fill in as you encounter them. If you find you need help, please ask here or on the project gitter and someone can help you get past the stumbling block.

First, familiarize yourself with the cache_alloc implementation in src/cache.c. This function looks for a free slot in the cache, and if one is not found then it allocates a new page to make more space. You don't need to fully understand this function, but it will help to see what logic causes the add_page call to happen, which is what you will cause to fail.

Next, devise a test that creates a single structure in a cache (entry structures are one such type) to set up the cache, set the memory allocation function to one that will fail, and then create new structures until the cache fills, attempts to allocate a new page, and fails. For an example of a test that creates several new structures look at TEST( BufferTargetOpenTest, Open100Targets ) in test/function/target/buffer.cpp, and TEST_F( EntryTest, AddNewParamMallocFailure ) in test/function/entry.cpp for an example of a memory allocation failure test. If testing entry creation, put your new test in test/function/entry.cpp.

Make sure that you have full coverage on add_page in src/cache.c after this test. See the test documentation for details on checking coverage.

@goatshriek goatshriek added help wanted external contributations encouraged testing pertains to the test suite good first issue something that would be simple for a newcomer to stumpless to work on hacktoberfest labels Sep 26, 2024
@rrrrrrmb
Copy link
Contributor

rrrrrrmb commented Oct 6, 2024

Hi, I'd like to work on this. I haven't really done much in C but would like to try. Based on my understanding the line of interest to us is

new_page = alloc_mem( c->page_size ); in add_page in src/cache.c

This new page allocation should fail in our case however I'm not sure how to set this up with MALLOC_FAIL_ON_SIZE since I don't know how to get the page_size value. Is there another way to do this ? Can you give pointers to solve this ?

Thanks !

@goatshriek
Copy link
Owner Author

goatshriek commented Oct 6, 2024

It is a tricky one to understand and test for, which is why there isn't a test now!

The way the test described in the issue works is to first set things up normally, allowing memory to be allocated as needed. After this, you will set the memory to one that fails in any scenario (no need to worry about the size, just use MALLOC_FAIL), and then create new objects that are backed by a cache. entry structures are a good candidate here, and creating a new one with stumpless_new_entry_str with a NULL message should work while the cache has space left, even if the malloc would fail. In the test I would create a new entry this way normally, then set malloc to fail, and then loop on entry creation until it fails. I believe this will exercise the code path in question.

@goatshriek
Copy link
Owner Author

Resolved by #464.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue something that would be simple for a newcomer to stumpless to work on help wanted external contributations encouraged testing pertains to the test suite
Projects
None yet
Development

No branches or pull requests

2 participants