Fix out-of-bounds write for causal ancestral alleles - #192
Merged
Conversation
The node traversal allocated an array with one entry per node, but pushes the virtual root when the causal allele is the ancestral allele. The virtual root's ID is num_nodes, so the traversal wrote one element past the end of the array. Numba compiles with bounds checking disabled, so this failed silently. Move the compiled kernels into a new tstrait.jit module, sizing the traversal buffer from the tree arrays, which already include the virtual root, and dropping it from the returned values. The stack is now a preallocated array with the causal nodes passed in as an array, which removes numba.typed from the codebase along with the special case for an empty stack. Add tests/test_jit.py, which exercises the kernels through their py_func attribute. Running the untranslated Python gives us numpy's bounds checking and lets coverage measure the kernels, which reach 100% statement and branch coverage from small hand written examples.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #192 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 8 9 +1
Lines 394 417 +23
Branches 54 56 +2
=========================================
+ Hits 394 417 +23
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Member
|
Looks ok to me, from what I understand is going on here! Will drop linking to the issue: #191 |
Replace the single hand written tree with trees from the tskit generators, covering polytomies at and below the root, ladders, and the degenerate cases in which the virtual root has no children, one child, or many: a single node, a tree sequence with no nodes, isolated samples, and a forest containing an unreachable node. Each topology is drawn in the class docstring so that the expected values can be checked against it. Run every test against both the compiled kernel and the Python it was written as, through the jit and nojit parameters of the fixtures. This replaces the smoke test comparing the two implementations to each other, since they are now both checked against fixed expectations. Use the documented tree sequences in tests/data.py for the accumulation kernel, rather than inventing node to individual maps, and add tests composing the two kernels the way genetic_value does.
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.
The node traversal allocated an array with one entry per node, but pushes the virtual root when the causal allele is the ancestral allele. The virtual root's ID is num_nodes, so the traversal wrote one element past the end of the array. Numba compiles with bounds checking disabled, so this failed silently.
Move the compiled kernels into a new tstrait.jit module, sizing the traversal buffer from the tree arrays, which already include the virtual root, and dropping it from the returned values. The stack is now a preallocated array with the causal nodes passed in as an array, which removes numba.typed from the codebase along with the special case for an empty stack.
Add tests/test_jit.py, which exercises the kernels through their py_func attribute. Running the untranslated Python gives us numpy's bounds checking and lets coverage measure the kernels, which reach 100% statement and branch coverage from small hand written examples.