Skip to content

Commit 9d66a2b

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

File tree

1 file changed

+45
-13
lines changed

1 file changed

+45
-13
lines changed

src/coreclr/jit/emitxarch.cpp

Lines changed: 45 additions & 13 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
{
@@ -14694,6 +14694,8 @@ BYTE* emitter::emitOutputAM(BYTE* dst, instrDesc* id, code_t code, CnsVal* addc)
1469414694
}
1469514695
else if (TakesEvexPrefix(id) || TakesApxExtendedEvexPrefix(id))
1469614696
{
14697+
ssize_t compressedDsp;
14698+
assert(!TryEvexCompressDisp8Byte(id, dsp, &compressedDsp, &dspInByte));
1469714699
dspInByte = false;
1469814700
}
1469914701
else
@@ -15567,16 +15569,17 @@ BYTE* emitter::emitOutputSV(BYTE* dst, instrDesc* id, code_t code, CnsVal* addc)
1556715569

1556815570
if (IsEvexEncodableInstruction(ins) || IsApxExtendedEvexInstruction(ins))
1556915571
{
15572+
ssize_t compressedDsp;
15573+
1557015574
if (HasCompressedDisplacement(id))
1557115575
{
15572-
ssize_t compressedDsp;
15573-
bool isCompressed = TryEvexCompressDisp8Byte(id, dsp, &compressedDsp, &dspInByte);
15574-
15576+
bool isCompressed = TryEvexCompressDisp8Byte(id, dsp, &compressedDsp, &dspInByte);
1557515577
assert(isCompressed && dspInByte);
1557615578
dsp = (int)compressedDsp;
1557715579
}
1557815580
else if (TakesEvexPrefix(id) || TakesApxExtendedEvexPrefix(id))
1557915581
{
15582+
assert(!TryEvexCompressDisp8Byte(id, dsp, &compressedDsp, &dspInByte));
1558015583
dspInByte = false;
1558115584
}
1558215585
else
@@ -15626,10 +15629,25 @@ BYTE* emitter::emitOutputSV(BYTE* dst, instrDesc* id, code_t code, CnsVal* addc)
1562615629
// Adjust the offset by the amount currently pushed on the CPU stack
1562715630
dsp += emitCurStackLvl;
1562815631

15629-
if (TakesEvexPrefix(id) || TakesApxExtendedEvexPrefix(id))
15632+
if (IsEvexEncodableInstruction(ins) || IsApxExtendedEvexInstruction(ins))
1563015633
{
15631-
assert(!HasCompressedDisplacement(id));
15632-
dspInByte = false;
15634+
ssize_t compressedDsp;
15635+
15636+
if (HasCompressedDisplacement(id))
15637+
{
15638+
bool isCompressed = TryEvexCompressDisp8Byte(id, dsp, &compressedDsp, &dspInByte);
15639+
assert(isCompressed && dspInByte);
15640+
dsp = (int)compressedDsp;
15641+
}
15642+
else if (TakesEvexPrefix(id) || TakesApxExtendedEvexPrefix(id))
15643+
{
15644+
assert(!TryEvexCompressDisp8Byte(id, dsp, &compressedDsp, &dspInByte));
15645+
dspInByte = false;
15646+
}
15647+
else
15648+
{
15649+
dspInByte = ((signed char)dsp == (ssize_t)dsp);
15650+
}
1563315651
}
1563415652
else
1563515653
{
@@ -18020,13 +18038,27 @@ bool emitter::TryEvexCompressDisp8Byte(instrDesc* id, ssize_t dsp, ssize_t* comp
1802018038
return *fitsInByte;
1802118039
}
1802218040

18023-
if (*fitsInByte && !TakesEvexPrefix(id))
18041+
if (*fitsInByte)
1802418042
{
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.
18043+
if (!TakesEvexPrefix(id))
18044+
{
18045+
// We already fit into a byte and do not otherwise require the EVEX prefix
18046+
// which means we can use the VEX encoding instead and be even smaller.
1802718047

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

1803218064
insTupleType tt = insTupleTypeInfo(ins);

0 commit comments

Comments
 (0)