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
|
/*
Title: Validate addresses in objects.
Copyright (c) 2006
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 WIN32
#include "winconfig.h"
#else
#include "config.h"
#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(const void *pt)
{
if (gMem.SpaceForAddress(pt) == 0)
Crash ("Bad pointer 0x%08x found", pt);
}
void DoCheck (const PolyWord pt)
{
if (pt == PolyWord::FromUnsigned(0)) return;
if (pt.IsTagged()) return;
/* Test corrected 20/2/95 - the stack contains code pointers
(for return addresses) as well as simple data pointers */
ASSERT (pt.IsCodePtr() || pt.IsDataPtr());
CheckAddress(pt.AsAddress());
}
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-1);
ASSERT (OBJ_IS_LENGTH(L));
POLYUNSIGNED n = OBJ_OBJECT_LENGTH(L);
if (n == 0) return;
ASSERT (n > 0);
PolyWord *end = pt + n;
CheckAddress (end-1);
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_STACK_OBJ)
{
StackObject *stack = (StackObject *) base;
POLYUNSIGNED skip = stack->p_sp - (PolyWord*)base;
ASSERT (INRANGE(stack->p_sp, (PolyWord*)base, (PolyWord*)end));
ASSERT (INRANGE(stack->p_hr, (PolyWord*)base, (PolyWord*)end));
/* Skip to the start of the stack. */
ASSERT (skip < n);
pt += skip;
n -= skip;
}
else 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;
if (gMem.IsIOPointer(pt.AsAddress())) 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 mutable area. This is where new objects are created.
// The immutable areas are only modified by the GC.
for (unsigned i = 0; i < gMem.nlSpaces; i++)
{
LocalMemSpace *space = gMem.lSpaces[i];
if (space->isMutable)
memCheck.ScanAddressesInRegion(space->pointer, space->top);
}
// Scan the permanent mutable areas.
for (unsigned j = 0; j < gMem.npSpaces; j++)
{
MemSpace *space = gMem.pSpaces[j];
if (space->isMutable)
memCheck.ScanAddressesInRegion(space->bottom, space->top);
}
}
|