Skip to content

Commit 3f84a23

Browse files
committed
[1.4>master] [MERGE chakra-core#2303 @kunalspathak] TypeSharing Bug fix
Merge pull request chakra-core#2303 from kunalspathak:typesharingfix See: https://microsoft.visualstudio.com/DefaultCollection/OS/_workItems?id=10058868&triage=true&_a=edit When we share type for ChangePrototype scenario, there is an instance where we could assign type with slotCapacity different than the layout of underline object. For example, initially we cache the map of oldType to newCachedType (both didn’t have properties) in prototype object `P`. Later, newCachedType evolves when properties were added on it. Also, oldType’s slotCapacity got shrunk when we update the ctor cache of an object. In future, when we try to set same object `P` as prototype to another object, we notice that currentType aka oldType’s slotcapacity was shrunk. So we try to shrink the newCachedType’s slotCapacity as well, but it doesn’t shrink to the extent oldType was shrunk. The reason being because newCachedType has successors added that were not present on oldType. Thus we end up assigning a type to an object with different slotCapacity than object’s layout. Any further operations on this object can give undefined behavior. Fix: We try to shrink slotCapacity of newCachedType and after shrinking if it still doesn’t match oldType’s slotCapacity, we create newType and set it on object. We also update the cache with this newType.
2 parents 257378a + 1c38588 commit 3f84a23

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

lib/Runtime/Types/PathTypeHandler.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1552,6 +1552,16 @@ namespace Js
15521552
Assert(cachedDynamicTypeHandler->GetInlineSlotCapacity() >= roundedInlineSlotCapacity);
15531553
Assert(cachedDynamicTypeHandler->GetInlineSlotCapacity() >= GetPropertyCount());
15541554
cachedDynamicTypeHandler->ShrinkSlotAndInlineSlotCapacity();
1555+
1556+
// If slot capacity doesn't match after shrinking, it could be because oldType was shrunk and
1557+
// newType evolved. In that case, we should update the cache with new type
1558+
if (cachedDynamicTypeHandler->GetInlineSlotCapacity() != roundedInlineSlotCapacity)
1559+
{
1560+
cachedDynamicType = nullptr;
1561+
#if DBG
1562+
swprintf_s(reason, 1024, _u("InlineSlotCapacity mismatch after shrinking. Required = %d, Cached = %d"), roundedInlineSlotCapacity, cachedDynamicTypeHandler->GetInlineSlotCapacity());
1563+
#endif
1564+
}
15551565
}
15561566
}
15571567
}

test/Prototypes/ChangePrototype.baseline

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,6 @@ Changing __proto__
2626
TypeSharing: Updating prototype object's DictionarySlot cache in __proto__.
2727
Changing __proto__
2828
TypeSharing: Reusing prototype object's DictionarySlot cache in __proto__.
29+
TypeSharing: Updating prototype object's InlineSlot cache in CreateObject.
30+
TypeSharing: Updating prototype object's DictionarySlot cache in __proto__.
31+
TypeSharing: Updating prototype object's DictionarySlot cache in __proto__.

test/Prototypes/ChangePrototype.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,5 +80,27 @@ function test2() {
8080
y.__proto__ = obj; // cached T2's inlineSlotCapacity doesn't match y's T1
8181
}
8282

83+
function test3() {
84+
// no switches needed
85+
var proto = {};
86+
87+
function foo() {
88+
}
89+
90+
var x = new foo();
91+
var y = new foo();
92+
y.__proto__ = proto; // empty type cached in map of proto object
93+
y._a = 1; // evolve cached type created above
94+
y._b = 1;
95+
y._c = 1;
96+
y._d = 1;
97+
var z = new foo(); // this shrunk oldType's slotCapacity from 8 to 2.
98+
99+
// retrived the cached type which was evolved.
100+
// Realized that oldType's slotCapacity has shrunk, we shrink slot capacity of cachedType but it doesn't match because cachedType has evolved
101+
z.__proto__ = proto;
102+
}
103+
83104
test1();
84-
test2();
105+
test2();
106+
test3();

0 commit comments

Comments
 (0)