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 all commits
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
36 changes: 30 additions & 6 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,8 +299,9 @@ 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));
return res;
}
Expand All @@ -312,6 +314,16 @@ static inline UInt SpaceBetweenPointers(Bag * a, Bag * b)
#define SizeAllBagsArea SpaceBetweenPointers(AllocBags, OldBags)
#define SizeWorkspace SpaceBetweenPointers(EndBags, MptrBags)

#if defined(GAP_KERNEL_DEBUG)
static int SanityCheckGasmanPointers(void)
{
return MptrBags <= OldBags &&
OldBags <= YoungBags &&
YoungBags <= AllocBags &&
AllocBags <= EndBags;
}
#endif

/****************************************************************************
**
*V FreeMptrBags . . . . . . . . . . . . . . . list of free bag identifiers
Expand Down Expand Up @@ -1032,6 +1044,7 @@ void InitBags (
/* Set ChangedBags to a proper initial value */
ChangedBags = 0;

GAP_ASSERT(SanityCheckGasmanPointers());
}


Expand Down Expand Up @@ -1084,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 @@ -1117,6 +1130,8 @@ Bag NewBag (
/* set the masterpointer */
SET_PTR_BAG(bag, DATA(header));

GAP_ASSERT(SanityCheckGasmanPointers());

/* return the identifier of the new bag */
return bag;
}
Expand Down Expand Up @@ -1288,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 @@ -1318,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 @@ -1372,6 +1387,7 @@ UInt ResizeBag (
sizeof(Obj) * WORDS_BAG(old_size));
}

GAP_ASSERT(SanityCheckGasmanPointers());
/* return success */
return 1;
}
Expand Down Expand Up @@ -1650,6 +1666,7 @@ UInt CollectBags (
UInt done; /* do we have to make a full gc */
UInt i; /* loop variable */

GAP_ASSERT(SanityCheckGasmanPointers());
CANARY_DISABLE_VALGRIND();
CLEAR_CANARY();
#ifdef DEBUG_MASTERPOINTERS
Expand Down Expand Up @@ -1936,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 Expand Up @@ -2067,7 +2089,7 @@ UInt CollectBags (
/* information after the check phase */
if ( MsgsFuncBags )
(*MsgsFuncBags)( FullBags, 5,
SpaceBetweenPointers(EndBags, stopBags)/(1024/sizeof(Bag)));
(EndBags - stopBags)/(1024/sizeof(Bag)));
if ( MsgsFuncBags )
(*MsgsFuncBags)( FullBags, 6,
SizeWorkspace/(1024/sizeof(Bag)));
Expand All @@ -2092,6 +2114,8 @@ UInt CollectBags (

CANARY_ENABLE_VALGRIND();

GAP_ASSERT(SanityCheckGasmanPointers());

/* return success */
return 1;
}
Expand Down