Skip to content

Commit f524f99

Browse files
authored
Merge pull request #18929 from gacholio/mhnarrow
Narrow 32 bit values when setting fields in MHInterpreter
2 parents 3c43f65 + c02bdce commit f524f99

File tree

2 files changed

+34
-11
lines changed

2 files changed

+34
-11
lines changed

runtime/vm/MHInterpreter.hpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,27 @@ class VM_MHInterpreter
7676
*/
7777
private:
7878

79+
/**
80+
* Narrow a 32-bit value (via masking or sign-extension) based on its
81+
* type (boolean, byte, char, short).
82+
*
83+
* @param fieldClass[in] J9Class representing the primitive type
84+
* @param value[in/out] The value to narrow (in place)
85+
*/
86+
VMINLINE void
87+
narrow32BitValue(J9Class *fieldClass, U_32 &value) const
88+
{
89+
if (fieldClass == _vm->booleanReflectClass) {
90+
value &= 1;
91+
} else if (fieldClass == _vm->byteReflectClass) {
92+
value = (U_32)(I_32)(I_8)value;
93+
} else if (fieldClass == _vm->charReflectClass) {
94+
value &= 0xFFFF;
95+
} else if (fieldClass == _vm->shortReflectClass) {
96+
value = (U_32)(I_32)(I_16)value;
97+
}
98+
}
99+
79100
/**
80101
* Fetch the vmSlot field from the j.l.i.PrimitiveHandle.
81102
* Note, the meaning of the vmSlot field depends on the type of the MethodHandle.

runtime/vm/MHInterpreter.inc

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,9 @@ VM_MHInterpreter::dispatchLoop(j9object_t methodHandle)
462462
nextAction = THROW_NPE;
463463
goto done;
464464
}
465-
_objectAccessBarrier->inlineMixedObjectStoreU32(_currentThread, objectref, fieldOffset, *(U_32*)_currentThread->sp, isVolatile);
465+
U_32 value = *(U_32*)_currentThread->sp;
466+
narrow32BitValue(fieldClass, value);
467+
_objectAccessBarrier->inlineMixedObjectStoreU32(_currentThread, objectref, fieldOffset, value, isVolatile);
466468
_currentThread->sp += 2;
467469
}
468470
} else {
@@ -522,19 +524,19 @@ VM_MHInterpreter::dispatchLoop(j9object_t methodHandle)
522524
J9Class *fieldClass = J9VM_J9CLASS_FROM_HEAPCLASS(_currentThread, fieldClassObject);
523525
U_32 modifiers = getPrimitiveHandleModifiers(methodHandle);
524526
bool isVolatile = (J9StaticFieldRefVolatile == (modifiers & J9StaticFieldRefVolatile));
525-
{
526-
if (J9ROMCLASS_IS_PRIMITIVE_TYPE(fieldClass->romClass)) {
527-
if (8 == ((J9ROMReflectClass *)(fieldClass->romClass))->elementSize) {
528-
_objectAccessBarrier->inlineStaticStoreU64(_currentThread, defc, (U_64*)srcAddress, *(U_64*)_currentThread->sp, isVolatile);
529-
_currentThread->sp += 3;
530-
} else {
531-
_objectAccessBarrier->inlineStaticStoreU32(_currentThread, defc, (U_32*)srcAddress, *(U_32*)_currentThread->sp, isVolatile);
532-
_currentThread->sp += 2;
533-
}
527+
if (J9ROMCLASS_IS_PRIMITIVE_TYPE(fieldClass->romClass)) {
528+
if (8 == ((J9ROMReflectClass *)(fieldClass->romClass))->elementSize) {
529+
_objectAccessBarrier->inlineStaticStoreU64(_currentThread, defc, (U_64*)srcAddress, *(U_64*)_currentThread->sp, isVolatile);
530+
_currentThread->sp += 3;
534531
} else {
535-
_objectAccessBarrier->inlineStaticStoreObject(_currentThread, defc, (j9object_t*)srcAddress, *(j9object_t*)_currentThread->sp, isVolatile);
532+
U_32 value = *(U_32*)_currentThread->sp;
533+
narrow32BitValue(fieldClass, value);
534+
_objectAccessBarrier->inlineStaticStoreU32(_currentThread, defc, (U_32*)srcAddress, value, isVolatile);
536535
_currentThread->sp += 2;
537536
}
537+
} else {
538+
_objectAccessBarrier->inlineStaticStoreObject(_currentThread, defc, (j9object_t*)srcAddress, *(j9object_t*)_currentThread->sp, isVolatile);
539+
_currentThread->sp += 2;
538540
}
539541
goto returnFromSend;
540542
}

0 commit comments

Comments
 (0)