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 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829
|
/*
* Copyright (C) 2014-2024 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "FTLOperations.h"
#if ENABLE(FTL_JIT)
#include "BytecodeStructs.h"
#include "ClonedArguments.h"
#include "CommonSlowPaths.h"
#include "DirectArguments.h"
#include "FTLJITCode.h"
#include "FTLLazySlowPath.h"
#include "FrameTracers.h"
#include "InlineCallFrame.h"
#include "JSArrayIterator.h"
#include "JSAsyncFunction.h"
#include "JSAsyncGeneratorFunction.h"
#include "JSCInlines.h"
#include "JSGeneratorFunction.h"
#include "JSImmutableButterfly.h"
#include "JSInternalPromise.h"
#include "JSIteratorHelper.h"
#include "JSLexicalEnvironment.h"
#include "JSMapIterator.h"
#include "JSSetIterator.h"
#include "RegExpObject.h"
#include "ResourceExhaustion.h"
#include "VMTrapsInlines.h"
#include <wtf/Assertions.h>
IGNORE_WARNINGS_BEGIN("frame-address")
WTF_ALLOW_UNSAFE_BUFFER_USAGE_BEGIN
namespace JSC { namespace FTL {
JSC_DEFINE_NOEXCEPT_JIT_OPERATION(operationPopulateObjectInOSR, void, (JSGlobalObject* globalObject, ExitTimeObjectMaterialization* materialization, EncodedJSValue* encodedValue, EncodedJSValue* values))
{
using namespace DFG;
VM& vm = globalObject->vm();
CallFrame* callFrame = DECLARE_CALL_FRAME(vm);
JITOperationPrologueCallFrameTracer tracer(vm, callFrame);
CodeBlock* codeBlock = callFrame->codeBlock();
// We cannot GC. We've got pointers in evil places.
// FIXME: We are not doing anything that can GC here, and this is
// probably unnecessary.
DeferGCForAWhile deferGC(vm);
switch (materialization->type()) {
case PhantomNewArrayWithConstantSize: {
JSArray* array = jsCast<JSArray*>(JSValue::decode(*encodedValue));
for (unsigned i = materialization->properties().size(); i--;) {
const ExitPropertyValue& property = materialization->properties()[i];
JSValue value = JSValue::decode(values[i]);
unsigned index = property.location().info();
Butterfly* butterfly = array->butterfly();
switch (materialization->indexingType()) {
case ALL_DOUBLE_INDEXING_TYPES: {
ASSERT(value.isNumber());
double valueAsDouble = value.asNumber();
butterfly->contiguousDouble().at(array, index) = valueAsDouble;
break;
}
case ALL_INT32_INDEXING_TYPES:
case ALL_CONTIGUOUS_INDEXING_TYPES: {
butterfly->contiguous().at(array, index).set(vm, array, value);
break;
}
default:
RELEASE_ASSERT_NOT_REACHED();
break;
}
}
break;
}
case PhantomNewObject: {
JSFinalObject* object = jsCast<JSFinalObject*>(JSValue::decode(*encodedValue));
Structure* structure = object->structure();
// Figure out what the heck to populate the object with. Use
// getPropertiesConcurrently() because that happens to be
// lower-level and more convenient. It doesn't change the
// materialization of the property table. We want to have
// minimal visible effects on the system. Also, don't mind
// that this is O(n^2). It doesn't matter. We only get here
// from OSR exit.
for (const PropertyTableEntry& entry : structure->getPropertiesConcurrently()) {
for (unsigned i = materialization->properties().size(); i--;) {
const ExitPropertyValue& property = materialization->properties()[i];
if (property.location().kind() != NamedPropertyPLoc)
continue;
if (codeBlock->identifier(property.location().info()).impl() != entry.key())
continue;
object->putDirectOffset(vm, entry.offset(), JSValue::decode(values[i]));
}
}
break;
}
case PhantomNewFunction:
case PhantomNewGeneratorFunction:
case PhantomNewAsyncFunction:
case PhantomNewAsyncGeneratorFunction:
case PhantomDirectArguments:
case PhantomClonedArguments:
case PhantomCreateRest:
case PhantomSpread:
case PhantomNewArrayWithSpread:
case PhantomNewArrayBuffer:
// Those are completely handled by operationMaterializeObjectInOSR
break;
case PhantomCreateActivation: {
JSLexicalEnvironment* activation = jsCast<JSLexicalEnvironment*>(JSValue::decode(*encodedValue));
// Figure out what to populate the activation with
for (unsigned i = materialization->properties().size(); i--;) {
const ExitPropertyValue& property = materialization->properties()[i];
if (property.location().kind() != ClosureVarPLoc)
continue;
activation->variableAt(ScopeOffset(property.location().info())).set(vm, activation, JSValue::decode(values[i]));
}
break;
}
case PhantomNewInternalFieldObject: {
auto materialize = [&] (auto* target) {
using JSCellType = std::remove_reference_t<decltype(*target)>;
// Figure out what to populate the iterator with
for (unsigned i = materialization->properties().size(); i--;) {
const ExitPropertyValue& property = materialization->properties()[i];
if (property.location().kind() != InternalFieldObjectPLoc)
continue;
ASSERT(property.location().info() < JSCellType::numberOfInternalFields);
target->internalField(static_cast<typename JSCellType::Field>(property.location().info())).set(vm, target, JSValue::decode(values[i]));
}
};
JSObject* target = jsCast<JSObject*>(JSValue::decode(*encodedValue));
switch (target->type()) {
case JSArrayIteratorType:
materialize(jsCast<JSArrayIterator*>(target));
break;
case JSMapIteratorType:
materialize(jsCast<JSMapIterator*>(target));
break;
case JSSetIteratorType:
materialize(jsCast<JSSetIterator*>(target));
break;
case JSIteratorHelperType:
materialize(jsCast<JSIteratorHelper*>(target));
break;
case JSPromiseType:
if (target->classInfo() == JSInternalPromise::info())
materialize(jsCast<JSInternalPromise*>(target));
else {
ASSERT(target->classInfo() == JSPromise::info());
materialize(jsCast<JSPromise*>(target));
}
break;
default:
RELEASE_ASSERT_NOT_REACHED();
break;
}
break;
}
case PhantomNewRegexp: {
RegExpObject* regExpObject = jsCast<RegExpObject*>(JSValue::decode(*encodedValue));
for (unsigned i = materialization->properties().size(); i--;) {
const ExitPropertyValue& property = materialization->properties()[i];
if (property.location().kind() != RegExpObjectLastIndexPLoc)
continue;
regExpObject->setLastIndex(globalObject, JSValue::decode(values[i]), false /* shouldThrow */);
break;
}
break;
}
default:
RELEASE_ASSERT_NOT_REACHED();
break;
}
}
JSC_DEFINE_NOEXCEPT_JIT_OPERATION(operationMaterializeObjectInOSR, JSCell*, (JSGlobalObject* globalObject, ExitTimeObjectMaterialization* materialization, EncodedJSValue* values))
{
using namespace DFG;
VM& vm = globalObject->vm();
CallFrame* callFrame = DECLARE_CALL_FRAME(vm);
JITOperationPrologueCallFrameTracer tracer(vm, callFrame);
// It's too hairy to handle TerminationExceptions during OSR object materialization.
// Let's just wait until after.
DeferTerminationForAWhile deferTermination(vm);
// We cannot GC. We've got pointers in evil places.
DeferGCForAWhile deferGC(vm);
switch (materialization->type()) {
case PhantomNewArrayWithConstantSize: {
auto scope = DECLARE_THROW_SCOPE(vm);
size_t size = materialization->size();
Structure* structure = globalObject->arrayStructureForIndexingTypeDuringAllocation(materialization->indexingType());
JSArray* result = JSArray::tryCreate(vm, structure, size);
if (UNLIKELY(!result)) {
throwOutOfMemoryError(globalObject, scope);
OPERATION_RETURN(scope, nullptr);
}
return result;
}
case PhantomNewObject: {
// Figure out what the structure is
Structure* structure = nullptr;
for (unsigned i = materialization->properties().size(); i--;) {
const ExitPropertyValue& property = materialization->properties()[i];
if (property.location() != PromotedLocationDescriptor(StructurePLoc))
continue;
RELEASE_ASSERT(JSValue::decode(values[i]).asCell()->inherits<Structure>());
structure = jsCast<Structure*>(JSValue::decode(values[i]));
break;
}
RELEASE_ASSERT(structure);
JSFinalObject* result = JSFinalObject::create(vm, structure);
// The real values will be put subsequently by
// operationPopulateNewObjectInOSR. We can't fill them in
// now, because they may not be available yet (typically
// because we have a cyclic dependency graph).
// We put a dummy value here in order to avoid super-subtle
// GC-and-OSR-exit crashes in case we have a bug and some
// field is, for any reason, not filled later.
// We use a random-ish number instead of a sensible value like
// undefined to make possible bugs easier to track.
for (const PropertyTableEntry& entry : structure->getPropertiesConcurrently())
result->putDirectOffset(vm, entry.offset(), jsNumber(19723));
return result;
}
case PhantomNewFunction:
case PhantomNewGeneratorFunction:
case PhantomNewAsyncGeneratorFunction:
case PhantomNewAsyncFunction: {
// Figure out what the executable and activation are
FunctionExecutable* executable = nullptr;
JSScope* activation = nullptr;
for (unsigned i = materialization->properties().size(); i--;) {
const ExitPropertyValue& property = materialization->properties()[i];
if (property.location() == PromotedLocationDescriptor(FunctionExecutablePLoc)) {
RELEASE_ASSERT(JSValue::decode(values[i]).asCell()->inherits<FunctionExecutable>());
executable = jsCast<FunctionExecutable*>(JSValue::decode(values[i]));
}
if (property.location() == PromotedLocationDescriptor(FunctionActivationPLoc)) {
RELEASE_ASSERT(JSValue::decode(values[i]).asCell()->inherits<JSScope>());
activation = jsCast<JSScope*>(JSValue::decode(values[i]));
}
}
RELEASE_ASSERT(executable && activation);
CodeBlock* codeBlock = baselineCodeBlockForOriginAndBaselineCodeBlock(materialization->origin(), callFrame->codeBlock()->baselineAlternative());
JSGlobalObject* globalObject = codeBlock->globalObject();
if (materialization->type() == PhantomNewFunction)
return JSFunction::createWithInvalidatedReallocationWatchpoint(vm, globalObject, executable, activation);
else if (materialization->type() == PhantomNewGeneratorFunction)
return JSGeneratorFunction::createWithInvalidatedReallocationWatchpoint(vm, globalObject, executable, activation);
else if (materialization->type() == PhantomNewAsyncGeneratorFunction)
return JSAsyncGeneratorFunction::createWithInvalidatedReallocationWatchpoint(vm, globalObject, executable, activation);
ASSERT(materialization->type() == PhantomNewAsyncFunction);
return JSAsyncFunction::createWithInvalidatedReallocationWatchpoint(vm, globalObject, executable, activation);
}
case PhantomCreateActivation: {
// Figure out what the scope and symbol table are
JSScope* scope = nullptr;
SymbolTable* table = nullptr;
for (unsigned i = materialization->properties().size(); i--;) {
const ExitPropertyValue& property = materialization->properties()[i];
if (property.location() == PromotedLocationDescriptor(ActivationScopePLoc)) {
RELEASE_ASSERT(JSValue::decode(values[i]).asCell()->inherits<JSScope>());
scope = jsCast<JSScope*>(JSValue::decode(values[i]));
} else if (property.location() == PromotedLocationDescriptor(ActivationSymbolTablePLoc)) {
RELEASE_ASSERT(JSValue::decode(values[i]).asCell()->inherits<SymbolTable>());
table = jsCast<SymbolTable*>(JSValue::decode(values[i]));
}
}
RELEASE_ASSERT(scope);
RELEASE_ASSERT(table);
CodeBlock* codeBlock = baselineCodeBlockForOriginAndBaselineCodeBlock(
materialization->origin(), callFrame->codeBlock()->baselineAlternative());
Structure* structure = codeBlock->globalObject()->activationStructure();
// It doesn't matter what values we initialize as bottom values inside the activation constructor because
// activation sinking will set bottom values for each slot.
// FIXME: Slight optimization would be to create a constructor that doesn't initialize all slots.
JSLexicalEnvironment* result = JSLexicalEnvironment::create(vm, structure, scope, table, jsUndefined());
RELEASE_ASSERT(materialization->properties().size() - 2 == table->scopeSize());
// The real values will be put subsequently by
// operationPopulateNewObjectInOSR. See the PhantomNewObject
// case for details.
for (unsigned i = materialization->properties().size(); i--;) {
const ExitPropertyValue& property = materialization->properties()[i];
if (property.location().kind() != ClosureVarPLoc)
continue;
result->variableAt(ScopeOffset(property.location().info())).set(
vm, result, jsNumber(29834));
}
if (validationEnabled()) {
// Validate to make sure every slot in the scope has one value.
ConcurrentJSLocker locker(table->m_lock);
for (auto iter = table->begin(locker), end = table->end(locker); iter != end; ++iter) {
bool found = false;
for (unsigned i = materialization->properties().size(); i--;) {
const ExitPropertyValue& property = materialization->properties()[i];
if (property.location().kind() != ClosureVarPLoc)
continue;
if (ScopeOffset(property.location().info()) == iter->value.scopeOffset()) {
found = true;
break;
}
}
ASSERT_UNUSED(found, found);
}
unsigned numberOfClosureVarPloc = 0;
for (unsigned i = materialization->properties().size(); i--;) {
const ExitPropertyValue& property = materialization->properties()[i];
if (property.location().kind() == ClosureVarPLoc)
numberOfClosureVarPloc++;
}
ASSERT_UNUSED(numberOfClosureVarPloc, numberOfClosureVarPloc == table->scopeSize());
}
return result;
}
case PhantomNewInternalFieldObject: {
// Figure out what structure.
Structure* structure = nullptr;
for (unsigned i = materialization->properties().size(); i--;) {
const ExitPropertyValue& property = materialization->properties()[i];
if (property.location() == PromotedLocationDescriptor(StructurePLoc)) {
RELEASE_ASSERT(JSValue::decode(values[i]).asCell()->inherits<Structure>());
structure = jsCast<Structure*>(JSValue::decode(values[i]));
}
}
RELEASE_ASSERT(structure);
// The real values will be put subsequently by
// operationPopulateNewObjectInOSR. See the PhantomNewObject
// case for details.
switch (structure->typeInfo().type()) {
case JSArrayIteratorType: {
JSArrayIterator* result = JSArrayIterator::createWithInitialValues(vm, structure);
RELEASE_ASSERT(materialization->properties().size() - 1 == JSArrayIterator::numberOfInternalFields);
return result;
}
case JSMapIteratorType: {
JSMapIterator* result = JSMapIterator::createWithInitialValues(vm, structure);
RELEASE_ASSERT(materialization->properties().size() - 1 == JSMapIterator::numberOfInternalFields);
return result;
}
case JSSetIteratorType: {
JSSetIterator* result = JSSetIterator::createWithInitialValues(vm, structure);
RELEASE_ASSERT(materialization->properties().size() - 1 == JSSetIterator::numberOfInternalFields);
return result;
}
case JSIteratorHelperType: {
JSIteratorHelper* result = JSIteratorHelper::createWithInitialValues(vm, structure);
RELEASE_ASSERT(materialization->properties().size() - 1 == JSIteratorHelper::numberOfInternalFields);
return result;
}
case JSPromiseType: {
if (structure->classInfoForCells() == JSInternalPromise::info()) {
JSInternalPromise* result = JSInternalPromise::createWithInitialValues(vm, structure);
RELEASE_ASSERT(materialization->properties().size() - 1 == JSInternalPromise::numberOfInternalFields);
return result;
}
ASSERT(structure->classInfoForCells() == JSPromise::info());
JSPromise* result = JSPromise::createWithInitialValues(vm, structure);
RELEASE_ASSERT(materialization->properties().size() - 1 == JSPromise::numberOfInternalFields);
return result;
}
default:
RELEASE_ASSERT_NOT_REACHED();
return nullptr;
}
}
case PhantomCreateRest:
case PhantomDirectArguments:
case PhantomClonedArguments: {
if (!materialization->origin().inlineCallFrame()) {
switch (materialization->type()) {
case PhantomDirectArguments:
return DirectArguments::createByCopying(globalObject, callFrame);
case PhantomClonedArguments: {
ClonedArguments* result = ClonedArguments::createWithMachineFrame(globalObject, callFrame, ArgumentsMode::Cloned);
RELEASE_ASSERT(result);
return result;
}
case PhantomCreateRest: {
CodeBlock* codeBlock = baselineCodeBlockForOriginAndBaselineCodeBlock(
materialization->origin(), callFrame->codeBlock()->baselineAlternative());
unsigned numberOfArgumentsToSkip = codeBlock->numberOfArgumentsToSkip();
JSGlobalObject* globalObject = codeBlock->globalObject();
Structure* structure = globalObject->restParameterStructure();
JSValue* argumentsToCopyRegion = callFrame->addressOfArgumentsStart() + numberOfArgumentsToSkip;
unsigned arraySize = callFrame->argumentCount() > numberOfArgumentsToSkip ? callFrame->argumentCount() - numberOfArgumentsToSkip : 0;
return constructArray(globalObject, structure, argumentsToCopyRegion, arraySize);
}
default:
RELEASE_ASSERT_NOT_REACHED();
return nullptr;
}
}
// First figure out the argument count. If there isn't one then we represent the machine frame.
unsigned argumentCount = 0;
if (materialization->origin().inlineCallFrame()->isVarargs()) {
for (unsigned i = materialization->properties().size(); i--;) {
const ExitPropertyValue& property = materialization->properties()[i];
if (property.location() != PromotedLocationDescriptor(ArgumentCountPLoc))
continue;
argumentCount = JSValue::decode(values[i]).asUInt32();
break;
}
} else
argumentCount = materialization->origin().inlineCallFrame()->argumentCountIncludingThis;
RELEASE_ASSERT(argumentCount);
JSFunction* callee = nullptr;
if (materialization->origin().inlineCallFrame()->isClosureCall) {
for (unsigned i = materialization->properties().size(); i--;) {
const ExitPropertyValue& property = materialization->properties()[i];
if (property.location() != PromotedLocationDescriptor(ArgumentsCalleePLoc))
continue;
callee = jsCast<JSFunction*>(JSValue::decode(values[i]));
break;
}
} else
callee = materialization->origin().inlineCallFrame()->calleeConstant();
RELEASE_ASSERT(callee);
CodeBlock* codeBlock = baselineCodeBlockForOriginAndBaselineCodeBlock(
materialization->origin(), callFrame->codeBlock()->baselineAlternative());
// We have an inline frame and we have all of the data we need to recreate it.
switch (materialization->type()) {
case PhantomDirectArguments: {
unsigned length = argumentCount - 1;
unsigned capacity = std::max(length, static_cast<unsigned>(codeBlock->numParameters() - 1));
DirectArguments* result = DirectArguments::create(
vm, codeBlock->globalObject()->directArgumentsStructure(), length, capacity);
result->setCallee(vm, callee);
for (unsigned i = materialization->properties().size(); i--;) {
const ExitPropertyValue& property = materialization->properties()[i];
if (property.location().kind() != ArgumentPLoc)
continue;
unsigned index = property.location().info();
if (index >= capacity)
continue;
// We don't want to use setIndexQuickly(), since that's only for the passed-in
// arguments but sometimes the number of named arguments is greater. For
// example:
//
// function foo(a, b, c) { ... }
// foo();
//
// setIndexQuickly() would fail for indices 0, 1, 2 - but we need to recover
// those here.
result->argument(DirectArgumentsOffset(index)).set(
vm, result, JSValue::decode(values[i]));
}
return result;
}
case PhantomClonedArguments: {
unsigned length = argumentCount - 1;
JSGlobalObject* globalObject = codeBlock->globalObject();
ClonedArguments* result = ClonedArguments::createEmpty(vm, nullptr, globalObject->clonedArgumentsStructure(), callee, length, nullptr);
RELEASE_ASSERT_RESOURCE_AVAILABLE(result, MemoryExhaustion, "Crash intentionally because memory is exhausted.");
for (unsigned i = materialization->properties().size(); i--;) {
const ExitPropertyValue& property = materialization->properties()[i];
if (property.location().kind() != ArgumentPLoc)
continue;
unsigned index = property.location().info();
if (index >= length)
continue;
result->putDirectIndex(globalObject, index, JSValue::decode(values[i]));
}
return result;
}
case PhantomCreateRest: {
unsigned numberOfArgumentsToSkip = codeBlock->numberOfArgumentsToSkip();
JSGlobalObject* globalObject = codeBlock->globalObject();
Structure* structure = globalObject->restParameterStructure();
ASSERT(argumentCount > 0);
unsigned arraySize = (argumentCount - 1) > numberOfArgumentsToSkip ? argumentCount - 1 - numberOfArgumentsToSkip : 0;
// FIXME: we should throw an out of memory error here if tryCreate() fails.
// https://bugs.webkit.org/show_bug.cgi?id=169784
JSArray* array = JSArray::tryCreate(vm, structure, arraySize);
RELEASE_ASSERT(array);
for (unsigned i = materialization->properties().size(); i--;) {
const ExitPropertyValue& property = materialization->properties()[i];
if (property.location().kind() != ArgumentPLoc)
continue;
unsigned argIndex = property.location().info();
if (numberOfArgumentsToSkip > argIndex)
continue;
unsigned arrayIndex = argIndex - numberOfArgumentsToSkip;
if (arrayIndex >= arraySize)
continue;
array->putDirectIndex(globalObject, arrayIndex, JSValue::decode(values[i]));
}
#if ASSERT_ENABLED
// We avoid this O(n^2) loop when asserts are disabled, but the condition checked here
// must hold to ensure the correctness of the above loop because of how we allocate the array.
for (unsigned targetIndex = 0; targetIndex < arraySize; ++targetIndex) {
bool found = false;
for (unsigned i = materialization->properties().size(); i--;) {
const ExitPropertyValue& property = materialization->properties()[i];
if (property.location().kind() != ArgumentPLoc)
continue;
unsigned argIndex = property.location().info();
if (numberOfArgumentsToSkip > argIndex)
continue;
unsigned arrayIndex = argIndex - numberOfArgumentsToSkip;
if (arrayIndex >= arraySize)
continue;
if (arrayIndex == targetIndex) {
found = true;
break;
}
}
ASSERT(found);
}
#endif // ASSERT_ENABLED
return array;
}
default:
RELEASE_ASSERT_NOT_REACHED();
return nullptr;
}
}
case PhantomSpread: {
JSArray* array = nullptr;
for (unsigned i = materialization->properties().size(); i--;) {
const ExitPropertyValue& property = materialization->properties()[i];
if (property.location().kind() == SpreadPLoc) {
array = jsCast<JSArray*>(JSValue::decode(values[i]));
break;
}
}
RELEASE_ASSERT(array);
// Note: it is sound for JSImmutableButterfly::createFromArray to call getDirectIndex here
// because we're guaranteed we won't be calling any getters. The reason for this is
// that we only support PhantomSpread over CreateRest, which is an array we create.
// Any attempts to put a getter on any indices on the rest array will escape the array.
auto* fixedArray = JSImmutableButterfly::createFromArray(globalObject, vm, array);
RELEASE_ASSERT(fixedArray);
return fixedArray;
}
case PhantomNewArrayBuffer: {
JSImmutableButterfly* immutableButterfly = nullptr;
for (unsigned i = materialization->properties().size(); i--;) {
const ExitPropertyValue& property = materialization->properties()[i];
if (property.location().kind() == NewArrayBufferPLoc) {
immutableButterfly = jsCast<JSImmutableButterfly*>(JSValue::decode(values[i]));
break;
}
}
RELEASE_ASSERT(immutableButterfly);
// For now, we use array allocation profile in the actual CodeBlock. It is OK since current NewArrayBuffer
// and PhantomNewArrayBuffer are always bound to a specific op_new_array_buffer.
CodeBlock* codeBlock = baselineCodeBlockForOriginAndBaselineCodeBlock(materialization->origin(), callFrame->codeBlock()->baselineAlternative());
const auto* currentInstruction = codeBlock->instructions().at(materialization->origin().bytecodeIndex()).ptr();
if (!currentInstruction->is<OpNewArrayBuffer>()) {
// This case can happen if Object.keys, an OpCall, and others is first converted into a NewArrayBuffer which is then converted into a PhantomNewArrayBuffer.
// There is no need to update the array allocation profile in that case.
Structure* structure = globalObject->arrayStructureForIndexingTypeDuringAllocation(immutableButterfly->indexingMode());
return CommonSlowPaths::allocateNewArrayBuffer(vm, structure, immutableButterfly);
}
auto newArrayBuffer = currentInstruction->as<OpNewArrayBuffer>();
ArrayAllocationProfile* profile = &newArrayBuffer.metadata(codeBlock).m_arrayAllocationProfile;
// FIXME: Share the code with CommonSlowPaths. Currently, codeBlock etc. are slightly different.
IndexingType indexingMode = profile->selectIndexingType();
Structure* structure = globalObject->arrayStructureForIndexingTypeDuringAllocation(indexingMode);
ASSERT(isCopyOnWrite(indexingMode));
ASSERT(!structure->outOfLineCapacity());
if (UNLIKELY(immutableButterfly->indexingMode() != indexingMode)) {
auto* newButterfly = JSImmutableButterfly::create(vm, indexingMode, immutableButterfly->length());
for (unsigned i = 0; i < immutableButterfly->length(); ++i)
newButterfly->setIndex(vm, i, immutableButterfly->get(i));
immutableButterfly = newButterfly;
// FIXME: This is kinda gross and only works because we can't inline new_array_bufffer in the baseline.
// We also cannot allocate a new butterfly from compilation threads since it's invalid to allocate cells from
// a compilation thread.
WTF::storeStoreFence();
codeBlock->constantRegister(newArrayBuffer.m_immutableButterfly).set(vm, codeBlock, immutableButterfly);
WTF::storeStoreFence();
}
JSArray* result = CommonSlowPaths::allocateNewArrayBuffer(vm, structure, immutableButterfly);
ArrayAllocationProfile::updateLastAllocationFor(profile, result);
return result;
}
case PhantomNewArrayWithSpread: {
CodeBlock* codeBlock = baselineCodeBlockForOriginAndBaselineCodeBlock(
materialization->origin(), callFrame->codeBlock()->baselineAlternative());
JSGlobalObject* globalObject = codeBlock->globalObject();
Structure* structure = globalObject->arrayStructureForIndexingTypeDuringAllocation(ArrayWithContiguous);
CheckedUint32 checkedArraySize = 0;
unsigned numProperties = 0;
for (unsigned i = materialization->properties().size(); i--;) {
const ExitPropertyValue& property = materialization->properties()[i];
if (property.location().kind() == NewArrayWithSpreadArgumentPLoc) {
++numProperties;
JSValue value = JSValue::decode(values[i]);
if (JSImmutableButterfly* immutableButterfly = jsDynamicCast<JSImmutableButterfly*>(value))
checkedArraySize += immutableButterfly->publicLength();
else
checkedArraySize += 1;
}
}
// FIXME: we should throw an out of memory error here if checkedArraySize has hasOverflowed() or tryCreate() fails.
// https://bugs.webkit.org/show_bug.cgi?id=169784
unsigned arraySize = checkedArraySize; // Crashes if overflowed.
JSArray* result = JSArray::tryCreate(vm, structure, arraySize);
RELEASE_ASSERT(result);
#if ASSERT_ENABLED
// Ensure we see indices for everything in the range: [0, numProperties)
for (unsigned i = 0; i < numProperties; ++i) {
bool found = false;
for (unsigned j = 0; j < materialization->properties().size(); ++j) {
const ExitPropertyValue& property = materialization->properties()[j];
if (property.location().kind() == NewArrayWithSpreadArgumentPLoc && property.location().info() == i) {
found = true;
break;
}
}
ASSERT(found);
}
#endif // ASSERT_ENABLED
Vector<JSValue, 8> arguments;
arguments.grow(numProperties);
for (unsigned i = materialization->properties().size(); i--;) {
const ExitPropertyValue& property = materialization->properties()[i];
if (property.location().kind() == NewArrayWithSpreadArgumentPLoc) {
JSValue value = JSValue::decode(values[i]);
RELEASE_ASSERT(property.location().info() < numProperties);
arguments[property.location().info()] = value;
}
}
unsigned arrayIndex = 0;
for (JSValue value : arguments) {
if (JSImmutableButterfly* immutableButterfly = jsDynamicCast<JSImmutableButterfly*>(value)) {
for (unsigned i = 0; i < immutableButterfly->publicLength(); i++) {
ASSERT(immutableButterfly->get(i));
result->putDirectIndex(globalObject, arrayIndex, immutableButterfly->get(i));
++arrayIndex;
}
} else {
// We are not spreading.
result->putDirectIndex(globalObject, arrayIndex, value);
++arrayIndex;
}
}
return result;
}
case PhantomNewRegexp: {
RegExp* regExp = nullptr;
for (unsigned i = materialization->properties().size(); i--;) {
const ExitPropertyValue& property = materialization->properties()[i];
if (property.location() == PromotedLocationDescriptor(RegExpObjectRegExpPLoc)) {
RELEASE_ASSERT(JSValue::decode(values[i]).asCell()->inherits<RegExp>());
regExp = jsCast<RegExp*>(JSValue::decode(values[i]));
}
}
RELEASE_ASSERT(regExp);
CodeBlock* codeBlock = baselineCodeBlockForOriginAndBaselineCodeBlock(materialization->origin(), callFrame->codeBlock()->baselineAlternative());
Structure* structure = codeBlock->globalObject()->regExpStructure();
static constexpr bool areLegacyFeaturesEnabled = true;
return RegExpObject::create(vm, structure, regExp, areLegacyFeaturesEnabled);
}
default:
RELEASE_ASSERT_NOT_REACHED();
return nullptr;
}
}
JSC_DEFINE_NOEXCEPT_JIT_OPERATION(operationSwitchStringAndGetIndex, unsigned, (JSGlobalObject* globalObject, const UnlinkedStringJumpTable* unlinkedTable, JSString* string))
{
VM& vm = globalObject->vm();
CallFrame* callFrame = DECLARE_CALL_FRAME(vm);
JITOperationPrologueCallFrameTracer tracer(vm, callFrame);
auto throwScope = DECLARE_THROW_SCOPE(vm);
unsigned length = string->length();
if (length < unlinkedTable->minLength() || length > unlinkedTable->maxLength())
return std::numeric_limits<unsigned>::max();
auto str = string->value(globalObject);
RETURN_IF_EXCEPTION(throwScope, 0);
return unlinkedTable->indexForValue(str->impl(), std::numeric_limits<unsigned>::max());
}
JSC_DEFINE_NOEXCEPT_JIT_OPERATION(operationTypeOfObjectAsTypeofType, int32_t, (JSGlobalObject* globalObject, JSCell* object))
{
VM& vm = globalObject->vm();
CallFrame* callFrame = DECLARE_CALL_FRAME(vm);
JITOperationPrologueCallFrameTracer tracer(vm, callFrame);
ASSERT(jsDynamicCast<JSObject*>(object));
if (object->structure()->masqueradesAsUndefined(globalObject))
return static_cast<int32_t>(TypeofType::Undefined);
if (object->isCallable())
return static_cast<int32_t>(TypeofType::Function);
return static_cast<int32_t>(TypeofType::Object);
}
JSC_DEFINE_NOEXCEPT_JIT_OPERATION(operationCompileFTLLazySlowPath, void*, (CallFrame* callFrame, unsigned index))
{
VM& vm = callFrame->deprecatedVM();
// Don't need an ActiveScratchBufferScope here because we DeferGCForAWhile.
// We cannot GC. We've got pointers in evil places.
DeferGCForAWhile deferGC(vm);
CodeBlock* codeBlock = callFrame->codeBlock();
JITCode* jitCode = codeBlock->jitCode()->ftl();
LazySlowPath& lazySlowPath = *jitCode->lazySlowPaths[index];
lazySlowPath.generate(codeBlock);
return lazySlowPath.stub().code().taggedPtr();
}
JSC_DEFINE_NOEXCEPT_JIT_OPERATION_WITH_ATTRIBUTES(operationReportBoundsCheckEliminationErrorAndCrash, NO_RETURN_DUE_TO_CRASH, void, (intptr_t codeBlockAsIntPtr, int32_t nodeIndex, int32_t child1Index, int32_t child2Index, int32_t checkedIndex, int32_t bounds))
{
CodeBlock* codeBlock = std::bit_cast<CodeBlock*>(codeBlockAsIntPtr);
dataLogLn("Bounds Check Eimination error found @ D@", nodeIndex, ": AssertInBounds(index D@", child1Index, ": ", checkedIndex, ", bounds D@", child2Index, " ", bounds, ") in ", codeBlock);
CRASH();
}
} } // namespace JSC::FTL
IGNORE_WARNINGS_END
WTF_ALLOW_UNSAFE_BUFFER_USAGE_END
#endif // ENABLE(FTL_JIT)
|