Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This rule finds uses of pointers that likely point to local variables in expired stack frames. A pointer to a local variable is only valid until the function returns, after which it becomes a dangling pointer.
fix any pointer stored in
g_stackremains valid for all uses infill_stackandcheck_stack. Sinceg_stackis global and used afterset_stackreturns, it must not point to a local (stack) array; instead it should point to memory that outlivesset_stack, such as static or heap-allocated storage.The minimal way to fix this without changing the intended functionality (measuring stack usage) is:
g_stackfrom a plainchar *to a small struct that holds:char *buf).size_t sizeindicating the size of that buffer.set_stack, stop storing the address of the localstackarray, and instead just updateg_stack.sizetosizeof(stack); do not let any pointer tostackescape.fill_stack, allocate a temporary local bufferchar buf[8192];, fill it with0x33, and updateg_stack.buf(andg_stack.size) to point to this local buffer just for the duration offill_stack. Immediately after filling, calluseto make sure the compiler actually touches the buffer, then resetg_stack.buftoNULLbefore returning so that no dangling pointer remains.check_stack, instead of reading fromg_stackas if it still held a valid pointer into theset_stackframe, treatg_stack.sizeas “how much stack was used” (reported fromset_stack) and compare it against the limit; do not dereferenceg_stack.bufat all.This preserves the observable behavior relevant to the test (checking that the measured “stack size” does not exceed a threshold) while preventing any use of a pointer to expired stack memory. All changes are limited to
contrib/linux-kernel/test/test.caround the globalg_stackand the three functionsset_stack,fill_stack, andcheck_stackReferences:
https://en.wikipedia.org/wiki/Dangling_pointer