1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
|
/*
Title: Validate addresses in objects.
Copyright (c) 2006, 2012
David C.J. Matthews
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#elif defined(_WIN32)
#include "winconfig.h"
#else
#error "No configuration file"
#endif
#ifdef HAVE_ASSERT_H
#include <assert.h>
#define ASSERT(x) assert(x)
#else
#define ASSERT(x)
#endif
#include "globals.h"
#include "diagnostics.h"
#include "machine_dep.h"
#include "scanaddrs.h"
#include "memmgr.h"
#define INRANGE(val,start,end)\
(start <= val && val < end)
static void CheckAddress(PolyWord *pt)
{
MemSpace *space = gMem.SpaceForAddress(pt-1);
if (space == 0)
{
Log("Check: Bad pointer %p (no space found)\n", pt);
ASSERT(space != 0);
}
if (space->spaceType == ST_STACK) // This may not have valid length words.
return;
PolyObject *obj = (PolyObject*)pt;
POLYUNSIGNED length = obj->Length();
if (pt+length > space->top)
{
Log("Check: Bad pointer %p (space %p) length %" POLYUFMT "\n", pt, space, length);
ASSERT(pt+length <= space->top);
}
if (space->spaceType == ST_LOCAL)
{
LocalMemSpace *lSpace = (LocalMemSpace*)space;
if (!((pt > lSpace->bottom && pt+length <= lSpace->lowerAllocPtr) ||
(pt > lSpace->upperAllocPtr && pt+length <= space->top)))
{
Log("Check: Bad pointer %p (space %p) length %" POLYUFMT " outside allocated area\n", pt, space, length);
ASSERT((pt > lSpace->bottom && pt+length <= lSpace->lowerAllocPtr) ||
(pt > lSpace->upperAllocPtr && pt+length <= space->top));
}
}
}
void DoCheck (const PolyWord pt)
{
if (pt == PolyWord::FromUnsigned(0)) return;
if (pt.IsTagged()) return;
CheckAddress(pt.AsStackAddr());
}
class ScanCheckAddress: public ScanAddress
{
public:
virtual PolyObject *ScanObjectAddress(PolyObject *pt) { DoCheck(pt); return pt; }
};
void DoCheckObject (const PolyObject *base, POLYUNSIGNED L)
{
PolyWord *pt = (PolyWord*)base;
CheckAddress(pt);
MemSpace *space = gMem.SpaceForAddress(pt-1);
if (space == 0)
Crash ("Bad pointer 0x%08x found", pt);
ASSERT (OBJ_IS_LENGTH(L));
POLYUNSIGNED n = OBJ_OBJECT_LENGTH(L);
if (n == 0) return;
ASSERT (n > 0);
ASSERT(pt-1 >= space->bottom && pt+n <= space->top);
byte flags = GetTypeBits(L); /* discards GC flag and mutable bit */
if (flags == F_BYTE_OBJ) /* possibly signed byte object */
return; /* Nothing more to do */
if (flags == F_CODE_OBJ) /* code object */
{
ScanCheckAddress checkAddr;
/* We flush the instruction cache here in case we change any of the
instructions when we update addresses. */
machineDependent->FlushInstructionCache(pt, (n + 1) * sizeof(PolyWord));
machineDependent->ScanConstantsWithinCode((PolyObject *)base, (PolyObject *)base, n, &checkAddr);
/* Skip to the constants. */
base->GetConstSegmentForCode(n, pt, n);
}
else ASSERT (flags == 0); /* ordinary word object */
while (n--) DoCheck (*pt++);
}
void DoCheckPointer (const PolyWord pt)
{
if (pt == PolyWord::FromUnsigned(0)) return;
if (OBJ_IS_AN_INTEGER(pt)) return;
DoCheck (pt);
if (pt.IsDataPtr())
{
PolyObject *obj = pt.AsObjPtr();
DoCheckObject (obj, obj->LengthWord());
}
}
// Check all the objects in the memory. Used to check the garbage collector
//
void DoCheckMemory()
{
ScanCheckAddress memCheck;
// Scan the local areas.
for (std::vector<LocalMemSpace*>::iterator i = gMem.lSpaces.begin(); i < gMem.lSpaces.end(); i++)
{
LocalMemSpace *space = *i;
memCheck.ScanAddressesInRegion(space->bottom, space->lowerAllocPtr);
memCheck.ScanAddressesInRegion(space->upperAllocPtr, space->top);
}
// Scan the permanent mutable areas.
for (std::vector<PermanentMemSpace*>::iterator i = gMem.pSpaces.begin(); i < gMem.pSpaces.end(); i++)
{
PermanentMemSpace *space = *i;
if (space->isMutable && ! space->byteOnly)
memCheck.ScanAddressesInRegion(space->bottom, space->top);
}
}
|