Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions core/relinker/codegen/src/x86/Sha1Operands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,18 @@ Sha1Operands DecodeSha1(const std::uint8_t* data, const std::size_t length) {
pos += 1;
continue;
}
if (b == PrefixLock) {
throw CodegenException("SHA-1 instruction with a LOCK prefix");
}
if (b == PrefixOperandSize || b == PrefixRepne || b == PrefixRep) {
throw CodegenException("Not a SHA-1 instruction");
}
if (b != PrefixLock && b != PrefixAddressSize &&
if (b != PrefixAddressSize &&
b != PrefixSegCs && b != PrefixSegSs && b != PrefixSegDs &&
b != PrefixSegEs && b != PrefixSegFs && b != PrefixSegGs) {
break;
}
if (b != PrefixLock)
prefixes.push_back(b);
prefixes.push_back(b);
rex = 0;
pos += 1;
}
Expand Down
8 changes: 5 additions & 3 deletions core/relinker/codegen/src/x86/Sha256Operands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,18 @@ Sha256Operands DecodeSha256(const std::uint8_t* data, const std::size_t length)
pos += 1;
continue;
}
if (b == PrefixLock) {
throw CodegenException("SHA-256 instruction with a LOCK prefix");
}
if (b == PrefixOperandSize || b == PrefixRepne || b == PrefixRep) {
throw CodegenException("Not a SHA-256 instruction");
}
if (b != PrefixLock && b != PrefixAddressSize &&
if (b != PrefixAddressSize &&
b != PrefixSegCs && b != PrefixSegSs && b != PrefixSegDs &&
b != PrefixSegEs && b != PrefixSegFs && b != PrefixSegGs) {
break;
}
if (b != PrefixLock)
prefixes.push_back(b);
prefixes.push_back(b);
rex = 0;
pos += 1;
}
Expand Down
5 changes: 4 additions & 1 deletion core/relinker/codegen/src/x86/Sse4aOperands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,14 @@ Sse4aOperands DecodeSse4a(const std::uint8_t* data, const std::size_t length) {
pos += 1;
continue;
}
if (b == PrefixLock) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This never runs for the other two SSE4a lowerings: MOVNTSS/MOVNTSD are dispatched to _matchMovnts (Amd64OnlyInstructionMatcher.cpp:401-405) before DecodeSse4a, and that path only rejects the register form, so f0 f3 0f 2b 00 still converts with Intel substitution: MOVNTSS ... in place and the site becomes f0 f3 0f 11 00 (lock movss, #UD, 0xc000001d natively); same for MOVNTSD and for f3 f0 .... Pre-existing on main, but the title claims the SSE4a family. A LOCK scan in _matchMovnts throwing MOVNTSS/MOVNTSD with a LOCK prefix (as ClzeroOperands.cpp:29-31) plus two requireFailure cases would close it; otherwise please name it as a follow-up.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Kept the decoder fixes scoped here as agreed with @oneandonlydean. The description now explicitly excludes MOVNTSS/MOVNTSD and names independent follow-up #10713. That PR rejects LOCK in the in-place matcher, covers both prefix orders for both MOVNT forms at converter level, and checks the failure offset. The new regression fails on unchanged main and passes with the guard; native AMD probes fault for all four LOCK encodings while all four unlocked controls execute. AI-assisted.

throw CodegenException("SSE4a instruction with a LOCK prefix");
}
if (b == PrefixOperandSize) {
operandSizeOverride = true;
} else if (b == PrefixRepne) {
repnePrefix = true;
} else if (b != PrefixLock && b != PrefixRep && b != PrefixAddressSize &&
} else if (b != PrefixRep && b != PrefixAddressSize &&
b != PrefixSegCs && b != PrefixSegSs && b != PrefixSegDs &&
b != PrefixSegEs && b != PrefixSegFs && b != PrefixSegGs) {
break;
Expand Down
12 changes: 12 additions & 0 deletions core/relinker/codegen/tests/Amd64OnlyConverterTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,8 @@ void decoderTwoByteOpcodeLengths() {
}

void sse4aOperands() {
requireFailure([] { const Bytes bytes = {0xF0, 0x66, 0x0F, 0x78, 0xC0, 0x08, 0x00}; (void)Codegen::DecodeSse4a(bytes.data(), bytes.size()); }, "LOCK EXTRQ was accepted");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit, optional: these exercise the decoders directly; one converter-level case in the style of LOCK RDPRU was accepted (line ~514) for a LOCK SHA site would also pin that the conversion fails rather than lowering, which I checked by hand (FAIL: Not a SHA-256 instruction (offset 0x4000) on this branch, Intel substitution: SHA256RNDS2 ... -> stub 785 bytes on main).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added converter-level rejection cases for both SHA-256 and SHA-1 in cfa8c10. They prepend LOCK to the existing valid segment fixtures and require conversion to throw with file offset 0x200; the unchanged valid fixtures still lower successfully. All 240 Linux relinker-only checks pass. AI-assisted.

requireFailure([] { const Bytes bytes = {0xF2, 0xF0, 0x0F, 0x79, 0xC1}; (void)Codegen::DecodeSse4a(bytes.data(), bytes.size()); }, "LOCK INSERTQ was accepted");
const auto check = [](const Bytes& site, const bool insertq, const int dst, const int src, const int length, const int index) {
const auto operands = Codegen::DecodeSse4a(site.data(), site.size());
require(operands.Insertq == insertq && !operands.RegisterForm && operands.Destination == dst && operands.Source == src && operands.Length == length && operands.Index == index, "SSE4a operands were decoded incorrectly");
Expand All @@ -231,6 +233,8 @@ void sse4aOperands() {
}

void sha256Operands() {
requireFailure([] { const Bytes bytes = {0xF0, 0x0F, 0x38, 0xCB, 0xCA}; (void)Codegen::DecodeSha256(bytes.data(), bytes.size()); }, "LOCK SHA256RNDS2 was accepted");
requireFailure([] { const Bytes bytes = {0x2E, 0xF0, 0x0F, 0x38, 0xCC, 0x08}; (void)Codegen::DecodeSha256(bytes.data(), bytes.size()); }, "LOCK SHA256MSG1 memory form was accepted");
const auto check = [](const Bytes& site, const Codegen::Sha256Operation operation, const int dst, const int src) {
const auto operands = Codegen::DecodeSha256(site.data(), site.size());
require(operands.Operation == operation && operands.Destination == dst && operands.Source == src, "SHA-256 operands were decoded incorrectly");
Expand Down Expand Up @@ -261,6 +265,8 @@ void sha256Operands() {
}

void sha1Operands() {
requireFailure([] { const Bytes bytes = {0xF0, 0x0F, 0x3A, 0xCC, 0xCA, 0x00}; (void)Codegen::DecodeSha1(bytes.data(), bytes.size()); }, "LOCK SHA1RNDS4 was accepted");
requireFailure([] { const Bytes bytes = {0x2E, 0xF0, 0x0F, 0x38, 0xC9, 0x08}; (void)Codegen::DecodeSha1(bytes.data(), bytes.size()); }, "LOCK SHA1MSG1 memory form was accepted");
const auto check = [](const Bytes& site, const Codegen::Sha1Operation operation, const int dst, const int src, const int function) {
const auto operands = Codegen::DecodeSha1(site.data(), site.size());
require(operands.Operation == operation && operands.Destination == dst && operands.Source == src && operands.Function == function, "SHA-1 operands were decoded incorrectly");
Expand Down Expand Up @@ -561,6 +567,9 @@ void converterSha256() {
0x90,
0xC3};
std::copy(text.begin(), text.end(), file.begin() + 0x200);
auto locked = file;
locked.insert(locked.begin() + 0x200, 0xF0);
require(failureOffset([&] { (void)converter->Convert(locked, {segmentHeader(text.size() + 1)}); }, "LOCK SHA-256 was accepted") == 0x200, "LOCK SHA-256 failure does not carry the file offset");
const auto result = converter->Convert(file, {segmentHeader(text.size())});
require(result.Trampolines.size() == 2 && result.Reports.size() == 2 && result.Bytes == file, "SHA-256 sites were not lowered through stubs");
const auto& rounds = result.Trampolines[0];
Expand Down Expand Up @@ -596,6 +605,9 @@ void converterSha1() {
0x90,
0xC3};
std::copy(text.begin(), text.end(), file.begin() + 0x200);
auto locked = file;
locked.insert(locked.begin() + 0x200, 0xF0);
require(failureOffset([&] { (void)converter->Convert(locked, {segmentHeader(text.size() + 1)}); }, "LOCK SHA-1 was accepted") == 0x200, "LOCK SHA-1 failure does not carry the file offset");
const auto result = converter->Convert(file, {segmentHeader(text.size())});
require(result.Trampolines.size() == 3 && result.Reports.size() == 3 && result.Bytes == file, "SHA-1 sites were not lowered through stubs");
require(result.Trampolines[0].Offset == 0x200 && result.Trampolines[0].Length == 5 && result.Reports[0].InstructionName == "SHA1RNDS4", "SHA1RNDS4 was not lowered as one site");
Expand Down
Loading