diff --git a/core/relinker/codegen/include/codegen/IInstructionScanner.hpp b/core/relinker/codegen/include/codegen/IInstructionScanner.hpp index 2ea39a4e4f5a..65a3be648021 100644 --- a/core/relinker/codegen/include/codegen/IInstructionScanner.hpp +++ b/core/relinker/codegen/include/codegen/IInstructionScanner.hpp @@ -17,7 +17,7 @@ class IInstructionScanner { virtual void VisitCodeSection(const std::vector& codeSection, Domain::FileByteOffset codeSectionOffset, Domain::FileByteOffset codeSectionSize, const std::function& visit) const = 0; }; -std::unique_ptr MakeInstructionScanner(std::function progress = {}); +std::unique_ptr MakeInstructionScanner(std::function progress = {}, bool resyncUndecodable = false); } diff --git a/core/relinker/codegen/src/InstructionScanner.cpp b/core/relinker/codegen/src/InstructionScanner.cpp index 328e1e532bd9..58c6795459b9 100644 --- a/core/relinker/codegen/src/InstructionScanner.cpp +++ b/core/relinker/codegen/src/InstructionScanner.cpp @@ -10,16 +10,17 @@ namespace Codegen { class InstructionScanner : public IInstructionScanner { public: - explicit InstructionScanner(std::function progress) : progress(std::move(progress)) {} + explicit InstructionScanner(std::function progress, bool resyncUndecodable) : progress(std::move(progress)), resyncUndecodable(resyncUndecodable) {} [[nodiscard]] std::vector ScanCodeSection(const std::vector& codeSection, Domain::FileByteOffset codeSectionOffset, Domain::FileByteOffset codeSectionSize) const override; void VisitCodeSection(const std::vector& codeSection, Domain::FileByteOffset codeSectionOffset, Domain::FileByteOffset codeSectionSize, const std::function& visit) const override; private: std::function progress; + bool resyncUndecodable; }; template -void ScanCodeSection(const std::vector& codeSection, const Domain::FileByteOffset codeSectionOffset, const Domain::FileByteOffset codeSectionSize, const std::function& progress, TVisitor&& visit) { +void ScanCodeSection(const std::vector& codeSection, const Domain::FileByteOffset codeSectionOffset, const Domain::FileByteOffset codeSectionSize, const std::function& progress, TVisitor&& visit, const bool resyncUndecodable) { if (codeSectionSize > codeSection.size()) throw CodegenException("Code section exceeds the input buffer", codeSectionOffset); if (codeSectionSize > std::numeric_limits::max() - codeSectionOffset) throw CodegenException("Code section offset overflow", codeSectionOffset); const auto limit = static_cast(codeSectionSize); @@ -27,6 +28,13 @@ void ScanCodeSection(const std::vector& codeSection, const Domain: 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) { @@ -38,7 +46,9 @@ void ScanCodeSection(const std::vector& 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}; @@ -56,16 +66,16 @@ void ScanCodeSection(const std::vector& codeSection, const Domain: std::vector InstructionScanner::ScanCodeSection(const std::vector& codeSection, const Domain::FileByteOffset codeSectionOffset, const Domain::FileByteOffset codeSectionSize) const { std::vector 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& codeSection, const Domain::FileByteOffset codeSectionOffset, const Domain::FileByteOffset codeSectionSize, const std::function& visit) const { - Codegen::ScanCodeSection(codeSection, codeSectionOffset, codeSectionSize, progress, visit); + Codegen::ScanCodeSection(codeSection, codeSectionOffset, codeSectionSize, progress, visit, resyncUndecodable); } -std::unique_ptr MakeInstructionScanner(std::function progress) { - return std::make_unique(std::move(progress)); +std::unique_ptr MakeInstructionScanner(std::function progress, const bool resyncUndecodable) { + return std::make_unique(std::move(progress), resyncUndecodable); } } diff --git a/core/relinker/codegen/tests/Amd64OnlyConverterTests.cpp b/core/relinker/codegen/tests/Amd64OnlyConverterTests.cpp index e1a802b28c2c..71585bc16e23 100644 --- a/core/relinker/codegen/tests/Amd64OnlyConverterTests.cpp +++ b/core/relinker/codegen/tests/Amd64OnlyConverterTests.cpp @@ -31,6 +31,7 @@ #include #include #include +#include #include #include #include @@ -1842,8 +1843,31 @@ 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() { @@ -1851,6 +1875,8 @@ int main() { decoderLengths(); decoderRipRelative(); decoderTwoByteOpcodeLengths(); + scannerZeroTail(); + scannerEmbeddedDataResync(); sse4aOperands(); sha256Operands(); sha1Operands(); diff --git a/core/relinker/relinker/src/analysis/SyscallScanner.cpp b/core/relinker/relinker/src/analysis/SyscallScanner.cpp index 6dc95462657a..1a22855ce2b4 100644 --- a/core/relinker/relinker/src/analysis/SyscallScanner.cpp +++ b/core/relinker/relinker/src/analysis/SyscallScanner.cpp @@ -187,7 +187,7 @@ std::vector SyscallScanner::ScanCodeSectionForSyscalls(const std::v const auto limit = static_cast(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 trapped; scanner->VisitCodeSection(codeSection, codeSectionOffset, codeSectionSize, [&](const Codegen::InstructionMatch& match) {