Skip to content
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class IInstructionScanner {
virtual void VisitCodeSection(const std::vector<std::uint8_t>& codeSection, Domain::FileByteOffset codeSectionOffset, Domain::FileByteOffset codeSectionSize, const std::function<void(const InstructionMatch&)>& visit) const = 0;
};

std::unique_ptr<IInstructionScanner> MakeInstructionScanner(std::function<void(std::uint64_t, std::uint64_t)> progress = {});
std::unique_ptr<IInstructionScanner> MakeInstructionScanner(std::function<void(std::uint64_t, std::uint64_t)> progress = {}, bool resyncUndecodable = false);

}

Expand Down
24 changes: 17 additions & 7 deletions core/relinker/codegen/src/InstructionScanner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,31 @@ namespace Codegen {

class InstructionScanner : public IInstructionScanner {
public:
explicit InstructionScanner(std::function<void(std::uint64_t, std::uint64_t)> progress) : progress(std::move(progress)) {}
explicit InstructionScanner(std::function<void(std::uint64_t, std::uint64_t)> progress, bool resyncUndecodable) : progress(std::move(progress)), resyncUndecodable(resyncUndecodable) {}
[[nodiscard]] std::vector<InstructionMatch> ScanCodeSection(const std::vector<std::uint8_t>& codeSection, Domain::FileByteOffset codeSectionOffset, Domain::FileByteOffset codeSectionSize) const override;
void VisitCodeSection(const std::vector<std::uint8_t>& codeSection, Domain::FileByteOffset codeSectionOffset, Domain::FileByteOffset codeSectionSize, const std::function<void(const InstructionMatch&)>& visit) const override;

private:
std::function<void(std::uint64_t, std::uint64_t)> progress;
bool resyncUndecodable;
};

template<typename TVisitor>
void ScanCodeSection(const std::vector<std::uint8_t>& codeSection, const Domain::FileByteOffset codeSectionOffset, const Domain::FileByteOffset codeSectionSize, const std::function<void(std::uint64_t, std::uint64_t)>& progress, TVisitor&& visit) {
void ScanCodeSection(const std::vector<std::uint8_t>& codeSection, const Domain::FileByteOffset codeSectionOffset, const Domain::FileByteOffset codeSectionSize, const std::function<void(std::uint64_t, std::uint64_t)>& progress, TVisitor&& visit, const bool resyncUndecodable) {
if (codeSectionSize > codeSection.size()) throw CodegenException("Code section exceeds the input buffer", codeSectionOffset);
if (codeSectionSize > std::numeric_limits<Domain::FileByteOffset>::max() - codeSectionOffset) throw CodegenException("Code section offset overflow", codeSectionOffset);
const auto limit = static_cast<std::size_t>(codeSectionSize);

const X64InstructionDecoder decoder;
std::size_t i = 0;

const auto resync = [&](std::size_t from) {
for (std::size_t candidate = (from + 16) & ~std::size_t{15}; candidate < limit; candidate += 16) {
if (candidate >= 2 && codeSection[candidate - 1] == 0xcc && codeSection[candidate - 2] == 0xcc) return candidate;
}
return from + 1;
};

if (progress) progress(0, limit);
std::size_t lastReport = 0;
while (i < limit) {
Expand All @@ -38,7 +46,9 @@ void ScanCodeSection(const std::vector<std::uint8_t>& codeSection, const Domain:
length = decoder.Decode(cursor, available);
} catch (const CodegenException& e) {
if (std::all_of(cursor, cursor + available, [](const std::uint8_t b) { return b == 0; })) break;
throw CodegenException(std::string("Cannot decode instruction: ") + e.what(), codeSectionOffset + i);
if (!resyncUndecodable) throw CodegenException(std::string("Cannot decode instruction: ") + e.what(), codeSectionOffset + i);
i = resync(i);
continue;
}

const InstructionMatch match{codeSectionOffset + i, length};
Expand All @@ -56,16 +66,16 @@ void ScanCodeSection(const std::vector<std::uint8_t>& codeSection, const Domain:

std::vector<InstructionMatch> InstructionScanner::ScanCodeSection(const std::vector<std::uint8_t>& codeSection, const Domain::FileByteOffset codeSectionOffset, const Domain::FileByteOffset codeSectionSize) const {
std::vector<InstructionMatch> matches;
Codegen::ScanCodeSection(codeSection, codeSectionOffset, codeSectionSize, progress, [&](const InstructionMatch& match) { matches.push_back(match); });
Codegen::ScanCodeSection(codeSection, codeSectionOffset, codeSectionSize, progress, [&](const InstructionMatch& match) { matches.push_back(match); }, resyncUndecodable);
return matches;
}

void InstructionScanner::VisitCodeSection(const std::vector<std::uint8_t>& codeSection, const Domain::FileByteOffset codeSectionOffset, const Domain::FileByteOffset codeSectionSize, const std::function<void(const InstructionMatch&)>& visit) const {
Codegen::ScanCodeSection(codeSection, codeSectionOffset, codeSectionSize, progress, visit);
Codegen::ScanCodeSection(codeSection, codeSectionOffset, codeSectionSize, progress, visit, resyncUndecodable);
}

std::unique_ptr<IInstructionScanner> MakeInstructionScanner(std::function<void(std::uint64_t, std::uint64_t)> progress) {
return std::make_unique<InstructionScanner>(std::move(progress));
std::unique_ptr<IInstructionScanner> MakeInstructionScanner(std::function<void(std::uint64_t, std::uint64_t)> progress, const bool resyncUndecodable) {
return std::make_unique<InstructionScanner>(std::move(progress), resyncUndecodable);
}

}
28 changes: 27 additions & 1 deletion core/relinker/codegen/tests/Amd64OnlyConverterTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include <functional>
#include <optional>
#include <span>
#include <algorithm>
#include <vector>
#include <iostream>
#include <stdexcept>
Expand Down Expand Up @@ -1842,15 +1843,40 @@ void scannerZeroTail() {
const auto scanner = Codegen::MakeInstructionScanner();
const Bytes code{0xC3, 0x00, 0x00, 0x00};
require(scanner->ScanCodeSection(code, 0, code.size()).size() == 2, "Odd zero padding at segment tail must end the scan");
}

void scannerEmbeddedDataResync() {
const auto scanner = Codegen::MakeInstructionScanner({}, true);
const Bytes code{
0xC3,
0x0F, 0x78, 0x11, 0x22, 0x33, 0x44,
0xC3,
};
const auto matches = scanner->ScanCodeSection(code, 0, code.size());
require(matches.size() >= 2, "Embedded data must not fail the scan; the surrounding instructions must decode");
require(matches.front().Offset == 0, "The first instruction is decoded normally");
require(matches.back().Offset == 6, "The instruction after the data must be reported at its real position");
require(matches.back().Offset + matches.back().Length == code.size(), "The scan must run to the end of the section");
const Bytes truncated{0xC3, 0x0F};
requireFailure([&] { (void)scanner->ScanCodeSection(truncated, 0, truncated.size()); }, "Truncated non-zero tail must still fail");
require(scanner->ScanCodeSection(truncated, 0, truncated.size()).size() == 1, "Truncated non-zero tail must resync, not fail");
Bytes padded(32, 0x00);
padded[0] = 0xC3;
padded[14] = 0xcc;
padded[15] = 0xcc;
padded[16] = 0xC3;
require(scanner->ScanCodeSection(padded, 0, padded.size()).size() >= 3, "Padded sections must scan to the end");
const auto strict = Codegen::MakeInstructionScanner();
requireFailure([&] { (void)strict->ScanCodeSection(code, 0, code.size()); }, "The strict default must still refuse embedded data");
requireFailure([&] { (void)strict->ScanCodeSection(truncated, 0, truncated.size()); }, "The strict default must still refuse a truncated non-zero tail");
}

int main() {
try {
decoderLengths();
decoderRipRelative();
decoderTwoByteOpcodeLengths();
scannerZeroTail();
scannerEmbeddedDataResync();
sse4aOperands();
sha256Operands();
sha1Operands();
Expand Down
2 changes: 1 addition & 1 deletion core/relinker/relinker/src/analysis/SyscallScanner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ std::vector<std::size_t> SyscallScanner::ScanCodeSectionForSyscalls(const std::v
const auto limit = static_cast<std::size_t>(codeSectionSize);

const std::string source(input);
const auto scanner = Codegen::MakeInstructionScanner([&](auto completed, auto total) { progress.Report({Domain::ProgressStage::Decode, source, completed, total}); });
const auto scanner = Codegen::MakeInstructionScanner([&](auto completed, auto total) { progress.Report({Domain::ProgressStage::Decode, source, completed, total}); }, true);
RecentInstructions recent;
std::vector<std::size_t> trapped;
scanner->VisitCodeSection(codeSection, codeSectionOffset, codeSectionSize, [&](const Codegen::InstructionMatch& match) {
Expand Down
Loading