Skip to content

Commit c8df8ef

Browse files
committed
Ensure that we check for compressed displacement using the signed value
1 parent dfc08c9 commit c8df8ef

File tree

1 file changed

+46
-15
lines changed

1 file changed

+46
-15
lines changed

src/coreclr/jit/emitxarch.cpp

Lines changed: 46 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5158,7 +5158,7 @@ inline UNATIVE_OFFSET emitter::emitInsSizeSVCalcDisp(instrDesc* id, code_t code,
51585158
ssize_t compressedDsp;
51595159
bool fitsInByte;
51605160

5161-
if (TryEvexCompressDisp8Byte(id, offs, &compressedDsp, &fitsInByte))
5161+
if (TryEvexCompressDisp8Byte(id, int(offs), &compressedDsp, &fitsInByte))
51625162
{
51635163
if (!TakesEvexPrefix(id))
51645164
{
@@ -5213,7 +5213,7 @@ inline UNATIVE_OFFSET emitter::emitInsSizeSVCalcDisp(instrDesc* id, code_t code,
52135213
{
52145214
ssize_t compressedDsp;
52155215

5216-
if (TryEvexCompressDisp8Byte(id, offs, &compressedDsp, &useSmallEncoding))
5216+
if (TryEvexCompressDisp8Byte(id, int(offs), &compressedDsp, &useSmallEncoding))
52175217
{
52185218
if (!TakesEvexPrefix(id))
52195219
{
@@ -14684,16 +14684,17 @@ BYTE* emitter::emitOutputAM(BYTE* dst, instrDesc* id, code_t code, CnsVal* addc)
1468414684
}
1468514685
else if (IsEvexEncodableInstruction(ins) || IsApxExtendedEvexInstruction(ins))
1468614686
{
14687+
ssize_t compressedDsp;
14688+
1468714689
if (HasCompressedDisplacement(id))
1468814690
{
14689-
ssize_t compressedDsp;
1469014691
bool isCompressed = TryEvexCompressDisp8Byte(id, dsp, &compressedDsp, &dspInByte);
14691-
1469214692
assert(isCompressed && dspInByte);
1469314693
dsp = compressedDsp;
1469414694
}
1469514695
else if (TakesEvexPrefix(id) || TakesApxExtendedEvexPrefix(id))
1469614696
{
14697+
assert(!TryEvexCompressDisp8Byte(id, dsp, &compressedDsp, &dspInByte));
1469714698
dspInByte = false;
1469814699
}
1469914700
else
@@ -15567,16 +15568,17 @@ BYTE* emitter::emitOutputSV(BYTE* dst, instrDesc* id, code_t code, CnsVal* addc)
1556715568

1556815569
if (IsEvexEncodableInstruction(ins) || IsApxExtendedEvexInstruction(ins))
1556915570
{
15571+
ssize_t compressedDsp;
15572+
1557015573
if (HasCompressedDisplacement(id))
1557115574
{
15572-
ssize_t compressedDsp;
15573-
bool isCompressed = TryEvexCompressDisp8Byte(id, dsp, &compressedDsp, &dspInByte);
15574-
15575+
bool isCompressed = TryEvexCompressDisp8Byte(id, dsp, &compressedDsp, &dspInByte);
1557515576
assert(isCompressed && dspInByte);
1557615577
dsp = (int)compressedDsp;
1557715578
}
1557815579
else if (TakesEvexPrefix(id) || TakesApxExtendedEvexPrefix(id))
1557915580
{
15581+
assert(!TryEvexCompressDisp8Byte(id, dsp, &compressedDsp, &dspInByte));
1558015582
dspInByte = false;
1558115583
}
1558215584
else
@@ -15626,10 +15628,25 @@ BYTE* emitter::emitOutputSV(BYTE* dst, instrDesc* id, code_t code, CnsVal* addc)
1562615628
// Adjust the offset by the amount currently pushed on the CPU stack
1562715629
dsp += emitCurStackLvl;
1562815630

15629-
if (TakesEvexPrefix(id) || TakesApxExtendedEvexPrefix(id))
15631+
if (IsEvexEncodableInstruction(ins) || IsApxExtendedEvexInstruction(ins))
1563015632
{
15631-
assert(!HasCompressedDisplacement(id));
15632-
dspInByte = false;
15633+
ssize_t compressedDsp;
15634+
15635+
if (HasCompressedDisplacement(id))
15636+
{
15637+
bool isCompressed = TryEvexCompressDisp8Byte(id, dsp, &compressedDsp, &dspInByte);
15638+
assert(isCompressed && dspInByte);
15639+
dsp = (int)compressedDsp;
15640+
}
15641+
else if (TakesEvexPrefix(id) || TakesApxExtendedEvexPrefix(id))
15642+
{
15643+
assert(!TryEvexCompressDisp8Byte(id, dsp, &compressedDsp, &dspInByte));
15644+
dspInByte = false;
15645+
}
15646+
else
15647+
{
15648+
dspInByte = ((signed char)dsp == (ssize_t)dsp);
15649+
}
1563315650
}
1563415651
else
1563515652
{
@@ -18020,13 +18037,27 @@ bool emitter::TryEvexCompressDisp8Byte(instrDesc* id, ssize_t dsp, ssize_t* comp
1802018037
return *fitsInByte;
1802118038
}
1802218039

18023-
if (*fitsInByte && !TakesEvexPrefix(id))
18040+
if (*fitsInByte)
1802418041
{
18025-
// We already fit into a byte and do not otherwise require the EVEX prefix
18026-
// which means we can use the VEX encoding instead and be even smaller.
18042+
if (!TakesEvexPrefix(id))
18043+
{
18044+
// We already fit into a byte and do not otherwise require the EVEX prefix
18045+
// which means we can use the VEX encoding instead and be even smaller.
1802718046

18028-
assert(*compressedDsp == dsp);
18029-
return false;
18047+
assert(*compressedDsp == dsp);
18048+
return false;
18049+
}
18050+
}
18051+
else
18052+
{
18053+
ssize_t compressedTest = dsp / 64;
18054+
18055+
if (static_cast<signed char>(compressedTest) != compressedTest)
18056+
{
18057+
// We are larger than the maximum possible compressed displacement
18058+
assert(*compressedDsp == dsp);
18059+
return false;
18060+
}
1803018061
}
1803118062

1803218063
insTupleType tt = insTupleTypeInfo(ins);

0 commit comments

Comments
 (0)