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
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Catch overflows and stop functions returning in gasman
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.
  • Loading branch information
ChrisJefferson committed Feb 5, 2018
commit b5da0cebc8510f4624bee37bae7ad7d070a15132
16 changes: 11 additions & 5 deletions src/gasman.c
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@
#include <src/gaputils.h>
#include <src/io.h>

#include <stdio.h>

/****************************************************************************
**
Expand Down Expand Up @@ -298,7 +299,7 @@ void CHANGED_BAG(Bag bag) {
answer in units of a word (ie sizeof(UInt) bytes), which should
therefore be small enough not to cause problems. */

static inline UInt SpaceBetweenPointers(Bag * a, Bag * b)
static inline UInt SpaceBetweenPointers(const Bag * a, const Bag * b)
{
GAP_ASSERT(b <= a);
UInt res = (((UInt)((UInt)(a) - (UInt)(b))) / sizeof(Bag));
Expand Down Expand Up @@ -1096,7 +1097,7 @@ Bag NewBag (
if ( (FreeMptrBags == 0 || SizeAllocationArea < WORDS_BAG(sizeof(BagHeader)+size))
&& CollectBags( size, 0 ) == 0 )
{
return 0;
SyAbortBags("cannot extend the workspace any more!!!!");
}

#ifdef COUNT_BAGS
Expand Down Expand Up @@ -1302,9 +1303,9 @@ UInt ResizeBag (
else if (CONST_PTR_BAG(bag) + WORDS_BAG(old_size) == AllocBags) {
CLEAR_CANARY();
// check that enough storage for the new bag is available
if (EndBags < CONST_PTR_BAG(bag) + WORDS_BAG(new_size)
if (SpaceBetweenPointers(EndBags, CONST_PTR_BAG(bag)) < WORDS_BAG(new_size)
&& CollectBags( new_size-old_size, 0 ) == 0 ) {
return 0;
SyAbortBags("cannot extend the workspace any more!!!!!");
}

// update header pointer in case bag moved
Expand Down Expand Up @@ -1332,7 +1333,7 @@ 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;
SyAbortBags("Cannot extend the workspace any more!!!!!!");
}
CLEAR_CANARY();

Expand Down Expand Up @@ -1952,6 +1953,11 @@ UInt CollectBags (

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

// Check if this allocation would even fit into memory
if (SIZE_MAX - (size_t)(sizeof(BagHeader) + size) < (size_t)AllocBags) {
return 0;
}

// store in 'stopBags' where this allocation takes us
Bag * stopBags = AllocBags + WORDS_BAG(sizeof(BagHeader)+size);

Expand Down