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

Catch overflows in Gasman #2160

Merged
merged 3 commits into from
Feb 6, 2018
Merged

Conversation

ChrisJefferson
Copy link
Contributor

@ChrisJefferson ChrisJefferson commented Feb 5, 2018

The main purpose of this patch is to catch a couple of pointer overflows in gasman. I also add some tests to make future problems easier to catch.

I suspect there may be other overflows, particularly is the memory space gets very close to 4GB, but this fixes some problems I know about, and my prefered solution to fixing other problems would be instead to limit the size of the memory space, so (for example) the GAP workspace stops at least 1GB from the end of the memory space, and we limit largest object to 1GB.

Copy link
Member

@fingolfin fingolfin left a comment

Choose a reason for hiding this comment

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

Looks essentially good to me, just a minor quibble

src/gasman.c Outdated
@@ -1325,7 +1339,8 @@ UInt ResizeBag (
/* check that enough storage for the new bag is available */
if ( SizeAllocationArea < WORDS_BAG(sizeof(BagHeader)+new_size)
&& CollectBags( new_size, 0 ) == 0 ) {
return 0;
fputs("gap: cannot extend the workspace any more!!!!!!\n",stderr);
SyExit( 1 );
Copy link
Member

Choose a reason for hiding this comment

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

Perhaps use SyAbortBags (not terribly important, just sayin')

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, I didn't notice that function.

src/gasman.c Outdated
@@ -1943,6 +1960,11 @@ UInt CollectBags (

/* * * * * * * * * * * * * * * check phase * * * * * * * * * * * * * * */

// Check if this allocation would even fit into memory
if(((size_t)-1) - (size_t)(sizeof(BagHeader)+size) < (size_t)AllocBags) {
Copy link
Member

Choose a reason for hiding this comment

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

missing space after if. Also ((size_t)-1) should be SIZE_MAX (from ISO C).

But perhaps the more idiomatic way would be to add a check after setting stopBags:

// check for an overflow
if (stopBags < AllocBags)
  SyAbortBags("gap: cannot extend the workspace any more!!");

(I assume you want to abort, not return 0?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Pointer overflow isn't defined. gcc will (for example) optimise i < i + 1 away to true for pointer i

src/gasman.c Outdated
&& CollectBags( new_size-old_size, 0 ) == 0 ) {
return 0;
fputs("gap: cannot extend the workspace any more!!!!!\n",stderr);
Copy link
Member

Choose a reason for hiding this comment

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

So it seems the number of exclamation marks is supposed to help distinguish these errors?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The seems to be the existing pattern, I just carried it on :)

@ChrisJefferson
Copy link
Contributor Author

Hopefully all issues fixed up

ChrisJefferson and others added 3 commits February 5, 2018 22:07
Catch a couple of cases where we can cause pointer overflow.

Also no callers of NewBag or ResizeBag check the return value, so
instead of returning 0 on failure exit GAP.
StopBags may be smaller or larger than EndBags, so we cannot
use SpaceBetweenPointers. We switch to just using '-'.
@codecov
Copy link

codecov bot commented Feb 5, 2018

Codecov Report

Merging #2160 into master will increase coverage by <.01%.
The diff coverage is 77.77%.

@@            Coverage Diff             @@
##           master    #2160      +/-   ##
==========================================
+ Coverage   69.58%   69.58%   +<.01%     
==========================================
  Files         482      482              
  Lines      254628   254639      +11     
==========================================
+ Hits       177182   177196      +14     
+ Misses      77446    77443       -3
Impacted Files Coverage Δ
src/gasman.c 84.91% <77.77%> (+0.15%) ⬆️
src/stats.c 85.04% <0%> (-0.14%) ⬇️
src/funcs.c 78.44% <0%> (+0.13%) ⬆️
src/hpc/threadapi.c 36.9% <0%> (+0.18%) ⬆️
hpcgap/lib/hpc/stdtasks.g 38.87% <0%> (+0.25%) ⬆️
src/hpc/traverse.c 95.45% <0%> (+0.47%) ⬆️

@fingolfin fingolfin added kind: bug Issues describing general bugs, and PRs fixing them topic: kernel labels Feb 6, 2018
@fingolfin
Copy link
Member

Do we want to backport this (and the SyMemmove PR) to stable-4.9 ?

@markuspf markuspf merged commit b961ab0 into gap-system:master Feb 6, 2018
@ChrisJefferson ChrisJefferson deleted the memmove-print branch February 19, 2018 15:50
@fingolfin fingolfin added release notes: added PRs introducing changes that have since been mentioned in the release notes backport-to-4.9 labels Mar 22, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: bug Issues describing general bugs, and PRs fixing them release notes: added PRs introducing changes that have since been mentioned in the release notes topic: kernel
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants