Skip to content

Commit

Permalink
[JSC] DFG constant property load should check the validity at the mai…
Browse files Browse the repository at this point in the history
…n thread

https://bugs.webkit.org/show_bug.cgi?id=267134
rdar://120443399

Reviewed by Mark Lam.

Consider the following case,

    CheckStructure O, S1 | S3
    GetByOffset O, offset

And S1 -> S2 -> S3 structure transition happens.
By changing object concurrently with the compiler, it is possible that we will constant fold the property with O + S2.
While we insert watchpoints into S1 and S3, we cannot notice the change of the property in S2.
If we change O to S3 before running code, CheckStructure passes and we can use a value loaded from O + S2.

1. If S1 and S3 transitions are both already watched by DFG / FTL, then we do not need to care about the issue.
   CheckStructure ensures that O is S1 or S3. And both has watchpoints which fires when transition happens.
   So, if we are transitioning from S1 to S2 while compiling, it already invalidates the code.
2. If there is only one Structure (S1), then we can keep the current optimization by checking this condition at the main thread.
   CheckStructure ensures that O is S1. And this means that if the assumption is met at the main thread, then we can continue
   using this code safely. To check this condition, we added DesiredObjectProperties, which records JSObject*, offset, value, and structure.
   And at the end of compilation, in the main thread, we check this assumption is still met.

* Source/JavaScriptCore/JavaScriptCore.xcodeproj/project.pbxproj:
* Source/JavaScriptCore/Sources.txt:
* Source/JavaScriptCore/dfg/DFGDesiredObjectProperties.cpp: Added.
(JSC::DFG::DesiredObjectProperties::addLazily):
(JSC::DFG::DesiredObjectProperties::areStillValidOnMainThread):
* Source/JavaScriptCore/dfg/DFGDesiredObjectProperties.h: Added.
* Source/JavaScriptCore/dfg/DFGGraph.cpp:
(JSC::DFG::Graph::tryGetConstantProperty):
* Source/JavaScriptCore/dfg/DFGPlan.cpp:
(JSC::DFG::Plan::cancel):
(JSC::DFG::Plan::isStillValidOnMainThread):
* Source/JavaScriptCore/dfg/DFGPlan.h:

Originally-landed-as: 272448.7@safari-7618-branch (3160120). rdar://121477582
Canonical link: https://commits.webkit.org/273486@main
  • Loading branch information
Constellation authored and robert-jenner committed Jan 25, 2024
1 parent 77a6809 commit 6471469
Show file tree
Hide file tree
Showing 7 changed files with 180 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1970,6 +1970,8 @@
E33F50811B8429A400413856 /* JSInternalPromise.h in Headers */ = {isa = PBXBuildFile; fileRef = E33F507F1B8429A400413856 /* JSInternalPromise.h */; settings = {ATTRIBUTES = (Private, ); }; };
E3400EC122A1CC7B009DED54 /* FunctionExecutableInlines.h in Headers */ = {isa = PBXBuildFile; fileRef = E3400EC022A1CC78009DED54 /* FunctionExecutableInlines.h */; };
E3443E3128E9711700C0020C /* ArrayPrototypeInlines.h in Headers */ = {isa = PBXBuildFile; fileRef = E3443E3028E9711700C0020C /* ArrayPrototypeInlines.h */; settings = {ATTRIBUTES = (Private, ); }; };
E34565A72B4753D800ED9B78 /* DFGDesiredObjectProperties.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E34565A42B4753D700ED9B78 /* DFGDesiredObjectProperties.cpp */; };
E34565A82B4753D800ED9B78 /* DFGDesiredObjectProperties.h in Headers */ = {isa = PBXBuildFile; fileRef = E34565A52B4753D800ED9B78 /* DFGDesiredObjectProperties.h */; };
E348876228F516C600527E5E /* ObjectPrototypeInlines.h in Headers */ = {isa = PBXBuildFile; fileRef = E348876128F516C600527E5E /* ObjectPrototypeInlines.h */; };
E349A7812491F161001BA336 /* DFGCodeOriginPool.h in Headers */ = {isa = PBXBuildFile; fileRef = E349A7802491F15A001BA336 /* DFGCodeOriginPool.h */; settings = {ATTRIBUTES = (Private, ); }; };
E34D4FAA2984F25B00CE5F9F /* WasmCallsiteCollection.h in Headers */ = {isa = PBXBuildFile; fileRef = E34D4FA92984F25A00CE5F9F /* WasmCallsiteCollection.h */; settings = {ATTRIBUTES = (Private, ); }; };
Expand Down Expand Up @@ -5622,6 +5624,8 @@
E33F50881B844A1A00413856 /* InternalPromiseConstructor.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; path = InternalPromiseConstructor.js; sourceTree = "<group>"; };
E3400EC022A1CC78009DED54 /* FunctionExecutableInlines.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = FunctionExecutableInlines.h; sourceTree = "<group>"; };
E3443E3028E9711700C0020C /* ArrayPrototypeInlines.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ArrayPrototypeInlines.h; sourceTree = "<group>"; };
E34565A42B4753D700ED9B78 /* DFGDesiredObjectProperties.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = DFGDesiredObjectProperties.cpp; path = dfg/DFGDesiredObjectProperties.cpp; sourceTree = "<group>"; };
E34565A52B4753D800ED9B78 /* DFGDesiredObjectProperties.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = DFGDesiredObjectProperties.h; path = dfg/DFGDesiredObjectProperties.h; sourceTree = "<group>"; };
E348876128F516C600527E5E /* ObjectPrototypeInlines.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ObjectPrototypeInlines.h; sourceTree = "<group>"; };
E349A77F2491F159001BA336 /* DFGCodeOriginPool.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; name = DFGCodeOriginPool.cpp; path = dfg/DFGCodeOriginPool.cpp; sourceTree = "<group>"; };
E349A7802491F15A001BA336 /* DFGCodeOriginPool.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = DFGCodeOriginPool.h; path = dfg/DFGCodeOriginPool.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -9030,6 +9034,8 @@
E3BFA5CD21E853A1009C0EBA /* DFGDesiredGlobalProperty.h */,
0F8F2B97172F04FD007DBDA5 /* DFGDesiredIdentifiers.cpp */,
0F8F2B98172F04FD007DBDA5 /* DFGDesiredIdentifiers.h */,
E34565A42B4753D700ED9B78 /* DFGDesiredObjectProperties.cpp */,
E34565A52B4753D800ED9B78 /* DFGDesiredObjectProperties.h */,
C2C0F7CB17BBFC5B00464FE4 /* DFGDesiredTransitions.cpp */,
C2C0F7CC17BBFC5B00464FE4 /* DFGDesiredTransitions.h */,
0FE853491723CDA500B618F5 /* DFGDesiredWatchpoints.cpp */,
Expand Down Expand Up @@ -10624,6 +10630,7 @@
0F2FC77316E12F740038D976 /* DFGDCEPhase.h in Headers */,
E3BFA5D021E853A1009C0EBA /* DFGDesiredGlobalProperty.h in Headers */,
0F8F2B9A172F0501007DBDA5 /* DFGDesiredIdentifiers.h in Headers */,
E34565A82B4753D800ED9B78 /* DFGDesiredObjectProperties.h in Headers */,
C2C0F7CE17BBFC5B00464FE4 /* DFGDesiredTransitions.h in Headers */,
0FE8534C1723CDA500B618F5 /* DFGDesiredWatchpoints.h in Headers */,
C2981FD917BAEE4B00A3BC98 /* DFGDesiredWeakReferences.h in Headers */,
Expand Down Expand Up @@ -13148,6 +13155,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
E34565A72B4753D800ED9B78 /* DFGDesiredObjectProperties.cpp in Sources */,
52CD0F682242F71C004A18A5 /* testdfg.cpp in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
1 change: 1 addition & 0 deletions Source/JavaScriptCore/Sources.txt
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,7 @@ dfg/DFGCriticalEdgeBreakingPhase.cpp
dfg/DFGDCEPhase.cpp
dfg/DFGDesiredGlobalProperties.cpp
dfg/DFGDesiredIdentifiers.cpp
dfg/DFGDesiredObjectProperties.cpp
dfg/DFGDesiredTransitions.cpp
dfg/DFGDesiredWatchpoints.cpp
dfg/DFGDesiredWeakReferences.cpp
Expand Down
71 changes: 71 additions & 0 deletions Source/JavaScriptCore/dfg/DFGDesiredObjectProperties.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright (C) 2023 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 "DFGDesiredObjectProperties.h"

#if ENABLE(DFG_JIT)

#include "JSObjectInlines.h"

namespace JSC { namespace DFG {

bool DesiredObjectProperties::addLazily(JSObject* object, PropertyOffset offset, JSValue value, Structure* structure)
{
auto result = m_properties.add(std::tuple { object, offset }, std::tuple { value, structure });
if (result.isNewEntry)
return true;
auto [expectedValue, expectedStructure] = result.iterator->value;
return expectedValue == value && expectedStructure == structure;
}

// Ultimately, we can use the code when
// 1. the main thread's object is holding an expected structure.
// 2. the main thread's object's property is expected one.
bool DesiredObjectProperties::areStillValidOnMainThread(VM&)
{
for (auto& [key, values] : m_properties) {
auto [object, offset] = key;
auto [expectedValue, expectedStructure] = values;

Structure* structure = object->structure();

if (UNLIKELY(structure != expectedStructure))
return false;

if (UNLIKELY(!structure->isValidOffset(offset)))
return false;

JSValue value = object->getDirect(offset);
if (UNLIKELY(value != expectedValue))
return false;
}
return true;
}

} } // namespace JSC::DFG

#endif // ENABLE(DFG_JIT)

55 changes: 55 additions & 0 deletions Source/JavaScriptCore/dfg/DFGDesiredObjectProperties.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright (C) 2023 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.
*/

#pragma once

#if ENABLE(DFG_JIT)

#include "DFGFrozenValue.h"
#include "DFGStructureAbstractValue.h"
#include <wtf/HashMap.h>

namespace JSC {

class CodeBlock;
class VM;

namespace DFG {

class CommonData;
class WatchpointCollector;

class DesiredObjectProperties {
public:
bool addLazily(JSObject*, PropertyOffset, JSValue, Structure*);
bool areStillValidOnMainThread(VM&);

private:
HashMap<std::tuple<JSObject*, PropertyOffset>, std::tuple<JSValue, Structure*>> m_properties;
};

} } // namespace JSC::DFG

#endif // ENABLE(DFG_JIT)
40 changes: 36 additions & 4 deletions Source/JavaScriptCore/dfg/DFGGraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1341,12 +1341,44 @@ JSValue Graph::tryGetConstantProperty(
// incompatible with the getDirect we're trying to do. The easiest way to do that is to
// determine if the structure belongs to the proven set.

Locker cellLock { object->cellLock() };
Structure* structure = object->structure();
if (!structureSet.toStructureSet().contains(structure))
JSValue result;
auto set = structureSet.toStructureSet();
{
Locker cellLock { object->cellLock() };
Structure* structure = object->structure();
if (!set.contains(structure))
return JSValue();
result = object->getDirectConcurrently(cellLock, structure, offset);
}

if (!result)
return JSValue();

// If all structures are watched, we don't need to check them in the main thread because they already registered watchpoints
// for transitions. If the object gets transition while compiling, then it invalidates the code.
bool allAreWatched = true;
for (unsigned i = structureSet.size(); i--;) {
RegisteredStructure structure = structureSet[i];
if (!structure->dfgShouldWatch()) {
allAreWatched = false;
break;
}
}
if (allAreWatched)
return result;

// However, if structures transitions are not watched, then object can get to the one of the structures transitively while it is changing the value.
// But we can still optimize it if StructureSet is only one: in that case, there is no way to fulfill Structure requirement while changing the property
// and avoiding the replacement watchpoint firing.
if (structureSet.size() != 1)
return JSValue();

m_plan.weakReferences().addLazily(object);
m_plan.weakReferences().addLazily(result);
if (!m_plan.objectProperties().addLazily(object, offset, result, set.onlyStructure()))
return JSValue();

return object->getDirectConcurrently(cellLock, structure, offset);
return result;
}

JSValue Graph::tryGetConstantProperty(JSValue base, Structure* structure, PropertyOffset offset)
Expand Down
7 changes: 6 additions & 1 deletion Source/JavaScriptCore/dfg/DFGPlan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ void Plan::cancel()
m_identifiers = DesiredIdentifiers();
m_weakReferences = DesiredWeakReferences();
m_transitions = DesiredTransitions();
m_objectProperties = DesiredObjectProperties();
m_callback = nullptr;
}

Expand Down Expand Up @@ -542,7 +543,11 @@ void Plan::reallyAdd(CommonData* commonData)

bool Plan::isStillValidOnMainThread()
{
return m_watchpoints.areStillValidOnMainThread(*m_vm, m_identifiers);
if (!m_watchpoints.areStillValidOnMainThread(*m_vm, m_identifiers))
return false;
if (!m_objectProperties.areStillValidOnMainThread(*m_vm))
return false;
return true;
}

CompilationResult Plan::finalize()
Expand Down
3 changes: 3 additions & 0 deletions Source/JavaScriptCore/dfg/DFGPlan.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "CompilationResult.h"
#include "DFGDesiredGlobalProperties.h"
#include "DFGDesiredIdentifiers.h"
#include "DFGDesiredObjectProperties.h"
#include "DFGDesiredTransitions.h"
#include "DFGDesiredWatchpoints.h"
#include "DFGDesiredWeakReferences.h"
Expand Down Expand Up @@ -92,6 +93,7 @@ class Plan final : public JITPlan {
DesiredIdentifiers& identifiers() { return m_identifiers; }
DesiredWeakReferences& weakReferences() { return m_weakReferences; }
DesiredTransitions& transitions() { return m_transitions; }
DesiredObjectProperties& objectProperties() { return m_objectProperties; }
RecordedStatuses& recordedStatuses() { return m_recordedStatuses; }

bool willTryToTierUp() const { return m_willTryToTierUp; }
Expand Down Expand Up @@ -132,6 +134,7 @@ class Plan final : public JITPlan {
DesiredIdentifiers m_identifiers;
DesiredWeakReferences m_weakReferences;
DesiredTransitions m_transitions;
DesiredObjectProperties m_objectProperties;
RecordedStatuses m_recordedStatuses;

HashMap<BytecodeIndex, FixedVector<BytecodeIndex>> m_tierUpInLoopHierarchy;
Expand Down

0 comments on commit 6471469

Please sign in to comment.