darling-JavaScriptCore/bytecode/StructureStubInfo.cpp

439 lines
14 KiB
C++
Raw Permalink Normal View History

2017-08-12 16:48:01 +00:00
/*
* Copyright (C) 2008-2020 Apple Inc. All rights reserved.
2017-08-12 16:48:01 +00:00
*
* 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 "StructureStubInfo.h"
#include "CacheableIdentifierInlines.h"
2017-08-12 16:48:01 +00:00
#include "PolymorphicAccess.h"
#include "Repatch.h"
namespace JSC {
#if ENABLE(JIT)
2020-08-29 13:27:11 +00:00
namespace StructureStubInfoInternal {
static constexpr bool verbose = false;
2020-08-29 13:27:11 +00:00
}
2017-08-12 16:48:01 +00:00
StructureStubInfo::StructureStubInfo(AccessType accessType, CodeOrigin codeOrigin)
: codeOrigin(codeOrigin)
2017-08-12 16:48:01 +00:00
, accessType(accessType)
, bufferingCountdown(Options::repatchBufferingCountdown())
, resetByGC(false)
, tookSlowPath(false)
, everConsidered(false)
2020-08-29 13:27:11 +00:00
, prototypeIsKnownObject(false)
, sawNonCell(false)
, hasConstantIdentifier(true)
, propertyIsString(false)
, propertyIsInt32(false)
, propertyIsSymbol(false)
2017-08-12 16:48:01 +00:00
{
}
StructureStubInfo::~StructureStubInfo()
{
}
void StructureStubInfo::initGetByIdSelf(const ConcurrentJSLockerBase& locker, CodeBlock* codeBlock, Structure* baseObjectStructure, PropertyOffset offset, CacheableIdentifier identifier)
2017-08-12 16:48:01 +00:00
{
ASSERT(hasConstantIdentifier);
setCacheType(locker, CacheType::GetByIdSelf);
m_identifier = identifier;
codeBlock->vm().heap.writeBarrier(codeBlock);
2017-08-12 16:48:01 +00:00
u.byIdSelf.baseObjectStructure.set(
codeBlock->vm(), codeBlock, baseObjectStructure);
2017-08-12 16:48:01 +00:00
u.byIdSelf.offset = offset;
}
void StructureStubInfo::initArrayLength(const ConcurrentJSLockerBase& locker)
2017-08-12 16:48:01 +00:00
{
setCacheType(locker, CacheType::ArrayLength);
2017-08-12 16:48:01 +00:00
}
void StructureStubInfo::initStringLength(const ConcurrentJSLockerBase& locker)
2020-08-29 13:27:11 +00:00
{
setCacheType(locker, CacheType::StringLength);
2020-08-29 13:27:11 +00:00
}
void StructureStubInfo::initPutByIdReplace(const ConcurrentJSLockerBase& locker, CodeBlock* codeBlock, Structure* baseObjectStructure, PropertyOffset offset, CacheableIdentifier identifier)
2017-08-12 16:48:01 +00:00
{
setCacheType(locker, CacheType::PutByIdReplace);
m_identifier = identifier;
codeBlock->vm().heap.writeBarrier(codeBlock);
2017-08-12 16:48:01 +00:00
u.byIdSelf.baseObjectStructure.set(
codeBlock->vm(), codeBlock, baseObjectStructure);
2017-08-12 16:48:01 +00:00
u.byIdSelf.offset = offset;
}
void StructureStubInfo::initInByIdSelf(const ConcurrentJSLockerBase& locker, CodeBlock* codeBlock, Structure* baseObjectStructure, PropertyOffset offset, CacheableIdentifier identifier)
2020-08-29 13:27:11 +00:00
{
setCacheType(locker, CacheType::InByIdSelf);
m_identifier = identifier;
codeBlock->vm().heap.writeBarrier(codeBlock);
2020-08-29 13:27:11 +00:00
u.byIdSelf.baseObjectStructure.set(
codeBlock->vm(), codeBlock, baseObjectStructure);
2020-08-29 13:27:11 +00:00
u.byIdSelf.offset = offset;
}
2017-08-12 16:48:01 +00:00
void StructureStubInfo::deref()
{
switch (m_cacheType) {
2017-08-12 16:48:01 +00:00
case CacheType::Stub:
delete u.stub;
return;
case CacheType::Unset:
case CacheType::GetByIdSelf:
case CacheType::PutByIdReplace:
2020-08-29 13:27:11 +00:00
case CacheType::InByIdSelf:
2017-08-12 16:48:01 +00:00
case CacheType::ArrayLength:
2020-08-29 13:27:11 +00:00
case CacheType::StringLength:
2017-08-12 16:48:01 +00:00
return;
}
RELEASE_ASSERT_NOT_REACHED();
}
void StructureStubInfo::aboutToDie()
{
switch (m_cacheType) {
2017-08-12 16:48:01 +00:00
case CacheType::Stub:
u.stub->aboutToDie();
return;
case CacheType::Unset:
case CacheType::GetByIdSelf:
case CacheType::PutByIdReplace:
2020-08-29 13:27:11 +00:00
case CacheType::InByIdSelf:
2017-08-12 16:48:01 +00:00
case CacheType::ArrayLength:
2020-08-29 13:27:11 +00:00
case CacheType::StringLength:
2017-08-12 16:48:01 +00:00
return;
}
RELEASE_ASSERT_NOT_REACHED();
}
AccessGenerationResult StructureStubInfo::addAccessCase(
const GCSafeConcurrentJSLocker& locker, JSGlobalObject* globalObject, CodeBlock* codeBlock, ECMAMode ecmaMode, CacheableIdentifier ident, std::unique_ptr<AccessCase> accessCase)
2017-08-12 16:48:01 +00:00
{
checkConsistency();
VM& vm = codeBlock->vm();
2020-08-29 13:27:11 +00:00
ASSERT(vm.heap.isDeferred());
AccessGenerationResult result = ([&] () -> AccessGenerationResult {
if (StructureStubInfoInternal::verbose)
dataLog("Adding access case: ", accessCase, "\n");
if (!accessCase)
return AccessGenerationResult::GaveUp;
2017-08-12 16:48:01 +00:00
2020-08-29 13:27:11 +00:00
AccessGenerationResult result;
if (m_cacheType == CacheType::Stub) {
result = u.stub->addCase(locker, vm, codeBlock, *this, WTFMove(accessCase));
2020-08-29 13:27:11 +00:00
if (StructureStubInfoInternal::verbose)
dataLog("Had stub, result: ", result, "\n");
if (result.shouldResetStubAndFireWatchpoints())
return result;
if (!result.buffered()) {
clearBufferedStructures();
2020-08-29 13:27:11 +00:00
return result;
}
} else {
std::unique_ptr<PolymorphicAccess> access = makeUnique<PolymorphicAccess>();
2020-08-29 13:27:11 +00:00
Vector<std::unique_ptr<AccessCase>, 2> accessCases;
std::unique_ptr<AccessCase> previousCase = AccessCase::fromStructureStubInfo(vm, codeBlock, ident, *this);
2020-08-29 13:27:11 +00:00
if (previousCase)
accessCases.append(WTFMove(previousCase));
accessCases.append(WTFMove(accessCase));
result = access->addCases(locker, vm, codeBlock, *this, WTFMove(accessCases));
2020-08-29 13:27:11 +00:00
if (StructureStubInfoInternal::verbose)
dataLog("Created stub, result: ", result, "\n");
2017-08-12 16:48:01 +00:00
2020-08-29 13:27:11 +00:00
if (result.shouldResetStubAndFireWatchpoints())
return result;
if (!result.buffered()) {
clearBufferedStructures();
2020-08-29 13:27:11 +00:00
return result;
}
setCacheType(locker, CacheType::Stub);
2020-08-29 13:27:11 +00:00
u.stub = access.release();
}
ASSERT(m_cacheType == CacheType::Stub);
2020-08-29 13:27:11 +00:00
RELEASE_ASSERT(!result.generatedSomeCode());
// If we didn't buffer any cases then bail. If this made no changes then we'll just try again
// subject to cool-down.
2017-08-12 16:48:01 +00:00
if (!result.buffered()) {
2020-08-29 13:27:11 +00:00
if (StructureStubInfoInternal::verbose)
dataLog("Didn't buffer anything, bailing.\n");
clearBufferedStructures();
2017-08-12 16:48:01 +00:00
return result;
}
2020-08-29 13:27:11 +00:00
// The buffering countdown tells us if we should be repatching now.
if (bufferingCountdown) {
if (StructureStubInfoInternal::verbose)
dataLog("Countdown is too high: ", bufferingCountdown, ".\n");
return result;
}
2017-08-12 16:48:01 +00:00
2020-08-29 13:27:11 +00:00
// Forget the buffered structures so that all future attempts to cache get fully handled by the
// PolymorphicAccess.
clearBufferedStructures();
2017-08-12 16:48:01 +00:00
result = u.stub->regenerate(locker, vm, globalObject, codeBlock, ecmaMode, *this);
2017-08-12 16:48:01 +00:00
2020-08-29 13:27:11 +00:00
if (StructureStubInfoInternal::verbose)
dataLog("Regeneration result: ", result, "\n");
2017-08-12 16:48:01 +00:00
2020-08-29 13:27:11 +00:00
RELEASE_ASSERT(!result.buffered());
if (!result.generatedSomeCode())
2017-08-12 16:48:01 +00:00
return result;
2020-08-29 13:27:11 +00:00
// If we generated some code then we don't want to attempt to repatch in the future until we
// gather enough cases.
bufferingCountdown = Options::repatchBufferingCountdown();
2017-08-12 16:48:01 +00:00
return result;
2020-08-29 13:27:11 +00:00
})();
vm.heap.writeBarrier(codeBlock);
2017-08-12 16:48:01 +00:00
return result;
}
void StructureStubInfo::reset(const ConcurrentJSLockerBase& locker, CodeBlock* codeBlock)
2017-08-12 16:48:01 +00:00
{
clearBufferedStructures();
2020-08-29 13:27:11 +00:00
if (m_cacheType == CacheType::Unset)
2017-08-12 16:48:01 +00:00
return;
2020-08-29 13:27:11 +00:00
2017-08-12 16:48:01 +00:00
if (Options::verboseOSR()) {
// This can be called from GC destructor calls, so we don't try to do a full dump
// of the CodeBlock.
dataLog("Clearing structure cache (kind ", static_cast<int>(accessType), ") in ", RawPointer(codeBlock), ".\n");
}
switch (accessType) {
case AccessType::TryGetById:
resetGetBy(codeBlock, *this, GetByKind::Try);
2017-08-12 16:48:01 +00:00
break;
case AccessType::GetById:
resetGetBy(codeBlock, *this, GetByKind::Normal);
2017-08-12 16:48:01 +00:00
break;
case AccessType::GetByIdWithThis:
resetGetBy(codeBlock, *this, GetByKind::WithThis);
2018-01-03 05:16:05 +00:00
break;
case AccessType::GetByIdDirect:
resetGetBy(codeBlock, *this, GetByKind::Direct);
break;
case AccessType::GetByVal:
resetGetBy(codeBlock, *this, GetByKind::NormalByVal);
break;
case AccessType::GetPrivateName:
resetGetBy(codeBlock, *this, GetByKind::PrivateName);
2020-08-29 13:27:11 +00:00
break;
2017-08-12 16:48:01 +00:00
case AccessType::Put:
resetPutByID(codeBlock, *this);
break;
case AccessType::In:
2020-08-29 13:27:11 +00:00
resetInByID(codeBlock, *this);
break;
case AccessType::InstanceOf:
resetInstanceOf(*this);
2017-08-12 16:48:01 +00:00
break;
case AccessType::DeleteByID:
resetDelBy(codeBlock, *this, DelByKind::Normal);
break;
case AccessType::DeleteByVal:
resetDelBy(codeBlock, *this, DelByKind::NormalByVal);
break;
2017-08-12 16:48:01 +00:00
}
deref();
setCacheType(locker, CacheType::Unset);
2017-08-12 16:48:01 +00:00
}
void StructureStubInfo::visitAggregate(SlotVisitor& visitor)
2017-08-12 16:48:01 +00:00
{
{
auto locker = holdLock(m_bufferedStructuresLock);
for (auto& bufferedStructure : m_bufferedStructures)
bufferedStructure.byValId().visitAggregate(visitor);
}
switch (m_cacheType) {
case CacheType::Unset:
case CacheType::ArrayLength:
case CacheType::StringLength:
return;
case CacheType::PutByIdReplace:
case CacheType::InByIdSelf:
case CacheType::GetByIdSelf:
m_identifier.visitAggregate(visitor);
return;
case CacheType::Stub:
u.stub->visitAggregate(visitor);
return;
}
2017-08-12 16:48:01 +00:00
RELEASE_ASSERT_NOT_REACHED();
return;
}
2017-08-12 16:48:01 +00:00
void StructureStubInfo::visitWeakReferences(const ConcurrentJSLockerBase& locker, CodeBlock* codeBlock)
{
VM& vm = codeBlock->vm();
{
auto locker = holdLock(m_bufferedStructuresLock);
m_bufferedStructures.removeIf(
[&] (auto& entry) -> bool {
return !vm.heap.isMarked(entry.structure());
});
}
switch (m_cacheType) {
2017-08-12 16:48:01 +00:00
case CacheType::GetByIdSelf:
case CacheType::PutByIdReplace:
2020-08-29 13:27:11 +00:00
case CacheType::InByIdSelf:
if (vm.heap.isMarked(u.byIdSelf.baseObjectStructure.get()))
2017-08-12 16:48:01 +00:00
return;
break;
case CacheType::Stub:
if (u.stub->visitWeak(vm))
return;
break;
default:
return;
}
reset(locker, codeBlock);
2017-08-12 16:48:01 +00:00
resetByGC = true;
}
bool StructureStubInfo::propagateTransitions(SlotVisitor& visitor)
{
switch (m_cacheType) {
2017-08-12 16:48:01 +00:00
case CacheType::Unset:
case CacheType::ArrayLength:
2020-08-29 13:27:11 +00:00
case CacheType::StringLength:
2017-08-12 16:48:01 +00:00
return true;
case CacheType::GetByIdSelf:
case CacheType::PutByIdReplace:
2020-08-29 13:27:11 +00:00
case CacheType::InByIdSelf:
2017-08-12 16:48:01 +00:00
return u.byIdSelf.baseObjectStructure->markIfCheap(visitor);
case CacheType::Stub:
return u.stub->propagateTransitions(visitor);
}
RELEASE_ASSERT_NOT_REACHED();
return true;
}
StubInfoSummary StructureStubInfo::summary(VM& vm) const
2020-08-29 13:27:11 +00:00
{
StubInfoSummary takesSlowPath = StubInfoSummary::TakesSlowPath;
StubInfoSummary simple = StubInfoSummary::Simple;
if (m_cacheType == CacheType::Stub) {
2020-08-29 13:27:11 +00:00
PolymorphicAccess* list = u.stub;
for (unsigned i = 0; i < list->size(); ++i) {
const AccessCase& access = list->at(i);
if (access.doesCalls(vm)) {
2020-08-29 13:27:11 +00:00
takesSlowPath = StubInfoSummary::TakesSlowPathAndMakesCalls;
simple = StubInfoSummary::MakesCalls;
break;
}
}
}
if (tookSlowPath || sawNonCell)
return takesSlowPath;
if (!everConsidered)
return StubInfoSummary::NoInformation;
return simple;
}
StubInfoSummary StructureStubInfo::summary(VM& vm, const StructureStubInfo* stubInfo)
2020-08-29 13:27:11 +00:00
{
if (!stubInfo)
return StubInfoSummary::NoInformation;
return stubInfo->summary(vm);
2020-08-29 13:27:11 +00:00
}
2017-08-12 16:48:01 +00:00
bool StructureStubInfo::containsPC(void* pc) const
{
if (m_cacheType != CacheType::Stub)
2017-08-12 16:48:01 +00:00
return false;
return u.stub->containsPC(pc);
}
void StructureStubInfo::setCacheType(const ConcurrentJSLockerBase&, CacheType newCacheType)
{
switch (m_cacheType) {
case CacheType::Unset:
case CacheType::ArrayLength:
case CacheType::StringLength:
case CacheType::Stub:
break;
case CacheType::PutByIdReplace:
case CacheType::InByIdSelf:
case CacheType::GetByIdSelf:
m_identifier = nullptr;
break;
}
m_cacheType = newCacheType;
}
#if ASSERT_ENABLED
void StructureStubInfo::checkConsistency()
{
if (thisValueIsInThisGPR()) {
// We currently use a union for both "thisGPR" and "propertyGPR". If this were
// not the case, we'd need to take one of them out of the union.
RELEASE_ASSERT(hasConstantIdentifier);
}
}
#endif // ASSERT_ENABLED
2017-08-12 16:48:01 +00:00
#endif // ENABLE(JIT)
} // namespace JSC