Skip to content

CPP: Allocation and Deallocation libraries#2463

Merged
jbj merged 33 commits intogithub:masterfrom
geoffw0:overflowcalc
Dec 19, 2019
Merged

CPP: Allocation and Deallocation libraries#2463
jbj merged 33 commits intogithub:masterfrom
geoffw0:overflowcalc

Conversation

@geoffw0
Copy link
Contributor

@geoffw0 geoffw0 commented Nov 27, 2019

There's quite a lot going on here, but the core of it is taking the functionality from commons/Alloc.qll, cleaning it up a lot, and repackaging it as models/interfaces/Allocation.qll and models/interfaces/Deallocation.qll. This allows us to reason about allocations and their sizes consistently regardless of whether they are malloc, realloc, new, new [] etc. Alloc.qll is now a legacy wrapper for the new library.

I've updated the NoSpaceForZeroTerminator.ql and OverflowCalculated.ql queries to use the new library, which improves their results since they were previously looking for calls to malloc only.

There is quite a lot of existing query test coverage, I have extended it in a few places.

@geoffw0 geoffw0 added the C++ label Nov 27, 2019
@geoffw0 geoffw0 requested a review from a team as a code owner November 27, 2019 16:26
Copy link
Contributor

@jbj jbj 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! It would have been easier to review if more of the Library commits had been squashed together.

@geoffw0
Copy link
Contributor Author

geoffw0 commented Nov 28, 2019

Added alloca to the model (this fixes a failing internal test).

Co-Authored-By: Jonas Jensen <jbj@github.com>
@jbj
Copy link
Contributor

jbj commented Nov 28, 2019

qlformat failed

@geoffw0
Copy link
Contributor Author

geoffw0 commented Dec 3, 2019

Formatting fixed.

Copy link
Contributor

@jbj jbj left a comment

Choose a reason for hiding this comment

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

There's now a test failure and merge conflicts.

@geoffw0
Copy link
Contributor Author

geoffw0 commented Dec 16, 2019

There's now a test failure and merge conflicts.

The merge conflicts should now be fixed. I'll have to wait for the tests to finish again before I can see why it failed.

@geoffw0
Copy link
Contributor Author

geoffw0 commented Dec 17, 2019

I've just fixed the failing test - the model needed to account for alloca so that the MemoryNeverFreed.ql and MemoryMayNotBeFreed.ql can make a suitable exception, as allocas do not need to be freed.

I've also modernized these two queries as it's something I'd been meaning to do and doing so makes the exception cleaner.

(latest four commits are new and should be reviewed)

where
alloc.requiresDealloc() and
not exists(alloc.(NewOrNewArrayExpr).getPlacementPointer()) and
not allocMayBeFreed(alloc)
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't requiresDealloc be false for a placement-new expression? It's hard to use this library if the caller must know about the special cases of alloc that are possible.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I considered that, but wasn't sure because we also don't know that a placement new expression doesn't require a dealloc either. I'd imagine it often does, but sometimes the entire pool is expected to be destroyed instead.

In the interest of avoiding false positives I think I'll make this change...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@jbj jbj merged commit 18d4772 into github:master Dec 19, 2019
@geoffw0 geoffw0 deleted the overflowcalc branch May 9, 2024 12:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants