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 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
|
/*
Title: Address scanner
Copyright (c) 2006-8, 2012, 2019 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) 0
#endif
#include <new>
#include "globals.h"
#include "scanaddrs.h"
#include "machine_dep.h"
#include "diagnostics.h"
#include "memmgr.h"
// Process the value at a given location and update it as necessary.
POLYUNSIGNED ScanAddress::ScanAddressAt(PolyWord *pt)
{
PolyWord val = *pt;
PolyWord newVal = val;
if (IS_INT(val) || val == PolyWord::FromUnsigned(0))
{
// We can get zeros in the constant area if we garbage collect
// while compiling some code. */
}
else
{
ASSERT(OBJ_IS_DATAPTR(val));
// Any sort of address
newVal = ScanObjectAddress(val.AsObjPtr());
}
if (newVal != val) // Only update if we need to.
*pt = newVal;
return 0;
}
// General purpose object processor, Processes all the addresses in an object.
// Handles the various kinds of object that may contain addresses.
void ScanAddress::ScanAddressesInObject(PolyObject *obj, POLYUNSIGNED lengthWord)
{
do
{
ASSERT (OBJ_IS_LENGTH(lengthWord));
if (OBJ_IS_BYTE_OBJECT(lengthWord))
return; /* Nothing more to do */
POLYUNSIGNED length = OBJ_OBJECT_LENGTH(lengthWord);
PolyWord *baseAddr = (PolyWord*)obj;
if (OBJ_IS_CODE_OBJECT(lengthWord))
{
// Scan constants within the code.
machineDependent->ScanConstantsWithinCode(obj, obj, length, this);
// Skip to the constants and get ready to scan them.
obj->GetConstSegmentForCode(length, baseAddr, length);
}
else if (OBJ_IS_CLOSURE_OBJECT(lengthWord))
{
// The first word is a code pointer so we need to treat it specially
// but it is possible it hasn't yet been set.
if ((*(uintptr_t*)baseAddr & 1) == 0)
{
POLYUNSIGNED lengthWord = ScanCodeAddressAt((PolyObject**)baseAddr); // N.B. This could side-effect *baseAddr
if (lengthWord != 0)
ScanAddressesInObject(*(PolyObject**)baseAddr, lengthWord);
}
baseAddr += sizeof(PolyObject*) / sizeof(PolyWord);
length -= sizeof(PolyObject*) / sizeof(PolyWord);
}
PolyWord *endWord = baseAddr + length;
// We want to minimise the actual recursion we perform so we try to
// use tail recursion if we can. We first scan from the end and
// remove any words that don't need recursion.
POLYUNSIGNED lastLengthWord = 0;
while (endWord != baseAddr)
{
PolyWord wordAt = endWord[-1];
if (IS_INT(wordAt) || wordAt == PolyWord::FromUnsigned(0))
endWord--; // Don't need to look at this.
else if ((lastLengthWord = ScanAddressAt(endWord-1)) != 0)
// We need to process this one
break;
else endWord--; // We're not interested in this.
}
if (endWord == baseAddr)
return; // We've done everything.
// There is at least one word that needs to be processed, the
// one at endWord-1.
// Now process from the beginning forward to see if there are
// any words before this that need to be handled. This way we are more
// likely to handle the head of a list by recursion and the
// tail by looping (tail recursion).
while (baseAddr < endWord-1)
{
PolyWord wordAt = *baseAddr;
if (IS_INT(wordAt) || wordAt == PolyWord::FromUnsigned(0))
baseAddr++; // Don't need to look at this.
else
{
POLYUNSIGNED lengthWord = ScanAddressAt(baseAddr);
if (lengthWord != 0)
{
wordAt = *baseAddr; // Reload because it may have been side-effected
// We really have to process this recursively.
ASSERT(wordAt.IsDataPtr());
ScanAddressesInObject(wordAt.AsObjPtr(), lengthWord);
baseAddr++;
}
else baseAddr++;
}
}
// Finally process the last word we found that has to be processed.
// Do this by looping rather than recursion.
PolyWord wordAt = *baseAddr; // Last word to do.
// This must be an address
ASSERT(wordAt.IsDataPtr());
obj = wordAt.AsObjPtr();
lengthWord = lastLengthWord;
} while(1);
}
void ScanAddress::ScanAddressesInRegion(PolyWord *region, PolyWord *end)
{
PolyWord *pt = region;
while (pt < end)
{
#ifdef POLYML32IN64
if ((((uintptr_t)pt) & 4) == 0)
{
// Skip any padding. The length word should be on an odd-word boundary.
pt++;
continue;
}
#endif
pt++; // Skip length word.
// pt actually points AT the object here.
PolyObject *obj = (PolyObject*)pt;
if (obj->ContainsForwardingPtr()) /* skip over moved object */
{
// We can now get multiple forwarding pointers as a result
// of applying ShareData repeatedly. Perhaps we should
// turn the forwarding pointers back into normal words in
// an extra pass.
obj = obj->FollowForwardingChain();
ASSERT(obj->ContainsNormalLengthWord());
pt += obj->Length();
}
else
{
ASSERT(obj->ContainsNormalLengthWord());
POLYUNSIGNED length = obj->Length();
if (pt+length > end)
Crash("Malformed object at %p - length %lu\n", pt, length);
if (length != 0)
ScanAddressesInObject(obj);
pt += length;
}
}
}
// Extract a constant from the code.
PolyObject *ScanAddress::GetConstantValue(byte *addressOfConstant, ScanRelocationKind code, PolyWord *base)
{
switch (code)
{
case PROCESS_RELOC_DIRECT: // 32 or 64 bit address of target
{
POLYUNSIGNED valu;
unsigned i;
byte *pt = addressOfConstant;
if (pt[3] & 0x80) valu = 0-1; else valu = 0;
for (i = sizeof(PolyWord); i > 0; i--)
valu = (valu << 8) | pt[i-1];
if (valu == 0 || PolyWord::FromUnsigned(valu).IsTagged())
return 0;
else return PolyWord::FromUnsigned(valu).AsObjPtr(base);
}
case PROCESS_RELOC_I386RELATIVE: // 32 bit relative address
{
POLYSIGNED disp;
byte *pt = addressOfConstant;
// Get the displacement. This is signed.
if (pt[3] & 0x80) disp = -1; else disp = 0; // Set the sign just in case.
for(unsigned i = 4; i > 0; i--) disp = (disp << 8) | pt[i-1];
byte *absAddr = pt + disp + 4; // The address is relative to AFTER the constant
return (PolyObject*)absAddr;
}
default:
ASSERT(false);
return 0;
}
}
// Store a constant value. Also used with a patch table when importing a saved heap which has
// been exported using the C exporter.
void ScanAddress::SetConstantValue(byte *addressOfConstant, PolyObject *p, ScanRelocationKind code)
{
switch (code)
{
case PROCESS_RELOC_DIRECT: // 32 or 64 bit address of target
{
POLYUNSIGNED valu = ((PolyWord)p).AsUnsigned();
for (unsigned i = 0; i < sizeof(PolyWord); i++)
{
addressOfConstant[i] = (byte)(valu & 255);
valu >>= 8;
}
}
break;
case PROCESS_RELOC_I386RELATIVE: // 32 bit relative address
{
// This offset may be positive or negative
intptr_t newDisp = (byte*)p - addressOfConstant - 4;
#if (SIZEOF_VOIDP != 4)
ASSERT(newDisp < (intptr_t)0x80000000 && newDisp >= -(intptr_t)0x80000000);
#endif
for (unsigned i = 0; i < 4; i++) {
addressOfConstant[i] = (byte)(newDisp & 0xff);
newDisp >>= 8;
}
}
break;
}
}
void ScanAddress::ScanConstant(PolyObject *base, byte *addressOfConstant, ScanRelocationKind code)
{
PolyObject *p = GetConstantValue(addressOfConstant, code);
if (p != 0)
{
PolyObject *oldValue = p;
// If this was a relative address we must have a code address.
if (code == PROCESS_RELOC_I386RELATIVE)
ScanCodeAddressAt(&p);
else p = ScanObjectAddress(p);
if (p != oldValue) // Update it if it has changed.
SetConstantValue(addressOfConstant, p, code);
}
}
void ScanAddress::ScanRuntimeWord(PolyWord *w)
{
if (w->IsTagged()) {} // Don't need to do anything
else {
ASSERT(w->IsDataPtr());
*w = ScanObjectAddress(w->AsObjPtr());
}
}
// This gets called in two circumstances. It may be called for the roots
// in which case the stack will be empty and we want to process it completely
// or it is called for a constant address in which case it will have been
// called from RecursiveScan::ScanAddressesInObject and that can process
// any addresses.
PolyObject *RecursiveScan::ScanObjectAddress(PolyObject *obj)
{
PolyWord pWord = obj;
// Test to see if this needs to be scanned.
// It may update the word.
bool test = TestForScan(&pWord);
obj = pWord.AsObjPtr();
if (test)
{
MarkAsScanning(obj);
if (obj->IsByteObject())
Completed(obj); // Don't need to put it on the stack
// If we already have something on the stack we must being called
// recursively to process a constant in a code segment. Just push
// it on the stack and let the caller deal with it.
else if (StackIsEmpty())
RecursiveScan::ScanAddressesInObject(obj, obj->LengthWord());
else
PushToStack(obj, (PolyWord*)obj);
}
return obj;
}
// This is called via ScanAddressesInRegion to process the permanent mutables. It is
// also called from ScanObjectAddress to process root addresses.
// It processes all the addresses reachable from the object.
// This is almost the same as MTGCProcessMarkPointers::ScanAddressesInObject.
void RecursiveScan::ScanAddressesInObject(PolyObject *obj, POLYUNSIGNED lengthWord)
{
if (OBJ_IS_BYTE_OBJECT(lengthWord))
return; // Ignore byte cells and don't call Completed on them
PolyWord *baseAddr = (PolyWord*)obj;
while (true)
{
ASSERT (OBJ_IS_LENGTH(lengthWord));
// Get the length and base address. N.B. If this is a code segment
// these will be side-effected by GetConstSegmentForCode.
POLYUNSIGNED length = OBJ_OBJECT_LENGTH(lengthWord);
if (OBJ_IS_CODE_OBJECT(lengthWord) || OBJ_IS_CLOSURE_OBJECT(lengthWord))
{
// It's better to process the whole code object in one go.
// For the moment do that for closure objects as well.
ScanAddress::ScanAddressesInObject(obj, lengthWord);
length = 0; // Finished
}
// else it's a normal object,
// If there are only two addresses in this cell that need to be
// followed we follow them immediately and treat this cell as done.
// If there are more than two we push the address of this cell on
// the stack, follow the first address and then rescan it. That way
// list cells are processed once only but we don't overflow the
// stack by pushing all the addresses in a very large vector.
PolyWord *endWord = (PolyWord*)obj + length;
PolyObject *firstWord = 0;
PolyObject *secondWord = 0;
PolyWord *restartFrom = baseAddr;
while (baseAddr != endWord)
{
PolyWord wordAt = *baseAddr;
if (wordAt.IsDataPtr() && wordAt != PolyWord::FromUnsigned(0))
{
// Normal address. We can have words of all zeros at least in the
// situation where we have a partially constructed code segment where
// the constants at the end of the code have not yet been filled in.
if (TestForScan(baseAddr)) // Test value at baseAddr (may side-effect it)
{
PolyObject *wObj = (*baseAddr).AsObjPtr();
if (wObj->IsByteObject())
{
// Can do this now - don't need to push it
MarkAsScanning(wObj);
Completed(wObj);
}
else if (firstWord == 0)
{
firstWord = wObj;
// We mark the word immediately. We can have
// two words in an object that are the same
// and we don't want to process it again.
MarkAsScanning(firstWord);
}
else if (secondWord == 0)
{
secondWord = wObj;
restartFrom = baseAddr;
}
else break; // More than two words.
}
}
baseAddr++;
}
if (baseAddr == endWord)
{
// We have done everything except possibly firstWord and secondWord.
// Note: Unfortunately the way that ScanAddressesInRegion works means that
// we call Completed on the addresses of cells in the permanent areas without
// having called TestForScan.
Completed(obj);
if (secondWord != 0)
{
MarkAsScanning(secondWord);
// Put this on the stack. If this is a list node we will be
// pushing the tail.
PushToStack(secondWord, (PolyWord*)secondWord);
}
}
else // Put this back on the stack while we process the first word
PushToStack(obj, restartFrom);
if (firstWord != 0)
{
// Process it immediately.
obj = firstWord;
baseAddr = (PolyWord*)obj;
}
else if (StackIsEmpty())
return;
else
PopFromStack(obj, baseAddr);
lengthWord = obj->LengthWord();
}
}
// The stack is allocated as a series of blocks chained together.
#define RSTACK_SEGMENT_SIZE 1000
class RScanStack {
public:
RScanStack(): nextStack(0), lastStack(0), sp(0) {}
~RScanStack() { delete(nextStack); }
RScanStack *nextStack;
RScanStack *lastStack;
unsigned sp;
struct { PolyObject *obj; PolyWord *base; } stack[RSTACK_SEGMENT_SIZE];
};
RecursiveScanWithStack::~RecursiveScanWithStack()
{
delete(stack);
}
bool RecursiveScanWithStack::StackIsEmpty(void)
{
return stack == 0 || (stack->sp == 0 && stack->lastStack == 0);
}
void RecursiveScanWithStack::PushToStack(PolyObject *obj, PolyWord *base)
{
if (stack == 0 || stack->sp == RSTACK_SEGMENT_SIZE)
{
if (stack != 0 && stack->nextStack != 0)
stack = stack->nextStack;
else
{
// Need a new segment
try {
RScanStack *s = new RScanStack;
s->lastStack = stack;
if (stack != 0)
stack->nextStack = s;
stack = s;
}
catch (std::bad_alloc &) {
StackOverflow();
return;
}
}
}
stack->stack[stack->sp].obj = obj;
stack->stack[stack->sp].base = base;
stack->sp++;
}
void RecursiveScanWithStack::PopFromStack(PolyObject *&obj, PolyWord *&base)
{
if (stack->sp == 0)
{
// Chain to the previous stack if any
ASSERT(stack->lastStack != 0);
// Before we do, delete any further one to free some memory
delete(stack->nextStack);
stack->nextStack = 0;
stack = stack->lastStack;
ASSERT(stack->sp == RSTACK_SEGMENT_SIZE);
}
--stack->sp;
obj = stack->stack[stack->sp].obj;
base = stack->stack[stack->sp].base;
}
|