| OLD | NEW |
| 1 // Copyright 2012 Google Inc. All Rights Reserved. | 1 // Copyright 2012 Google Inc. All Rights Reserved. |
| 2 // | 2 // |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); | 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 // you may not use this file except in compliance with the License. | 4 // you may not use this file except in compliance with the License. |
| 5 // You may obtain a copy of the License at | 5 // You may obtain a copy of the License at |
| 6 // | 6 // |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 | 7 // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 // | 8 // |
| 9 // Unless required by applicable law or agreed to in writing, software | 9 // Unless required by applicable law or agreed to in writing, software |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, | 10 // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 // See the License for the specific language governing permissions and | 12 // See the License for the specific language governing permissions and |
| 13 // limitations under the License. | 13 // limitations under the License. |
| 14 | 14 |
| 15 #include "syzygy/core/disassembler_util.h" | 15 #include "syzygy/core/disassembler_util.h" |
| 16 | 16 |
| 17 #include <vector> |
| 18 |
| 17 #include "base/logging.h" | 19 #include "base/logging.h" |
| 18 #include "gmock/gmock.h" | 20 #include "gmock/gmock.h" |
| 19 #include "gtest/gtest.h" | 21 #include "gtest/gtest.h" |
| 20 #include "syzygy/assm/unittest_util.h" | 22 #include "syzygy/assm/unittest_util.h" |
| 23 #include "syzygy/core/disassembler_util_unittest_vex_utils.h" |
| 21 | 24 |
| 22 namespace core { | 25 namespace core { |
| 23 | 26 |
| 24 namespace { | 27 namespace { |
| 25 | 28 |
| 26 // Decompose a block of code using distorm wrapper. | 29 // Decompose a block of code using distorm wrapper. |
| 27 _DecodeResult DecomposeCode(const uint8_t* code_data, | 30 _DecodeResult DecomposeCode(const uint8_t* code_data, |
| 28 size_t length, | 31 size_t length, |
| 29 _DInst result[], | 32 _DInst result[], |
| 30 const unsigned int max_results, | 33 const unsigned int max_results, |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 111 const uint8_t kRet[] = {0xC3}; | 114 const uint8_t kRet[] = {0xC3}; |
| 112 const uint8_t kRetN[] = {0xC2, 0x08, 0x00}; | 115 const uint8_t kRetN[] = {0xC2, 0x08, 0x00}; |
| 113 const uint8_t kJe[] = {0x74, 0xCA}; | 116 const uint8_t kJe[] = {0x74, 0xCA}; |
| 114 const uint8_t kSysEnter[] = {0x0F, 0x34}; | 117 const uint8_t kSysEnter[] = {0x0F, 0x34}; |
| 115 const uint8_t kSysExit[] = {0x0F, 0x35}; | 118 const uint8_t kSysExit[] = {0x0F, 0x35}; |
| 116 | 119 |
| 117 // Interrupts. | 120 // Interrupts. |
| 118 const uint8_t kInt2[] = {0xCD, 0x02}; | 121 const uint8_t kInt2[] = {0xCD, 0x02}; |
| 119 const uint8_t kInt3[] = {0xCC}; | 122 const uint8_t kInt3[] = {0xCC}; |
| 120 | 123 |
| 121 // VEX encoded instructions that Distorm doesn't handle properly. | |
| 122 const std::vector<std::vector<uint8_t>> kVexInstructions = { | |
| 123 // AVX instructions. | |
| 124 {0xC4, 0xE3, 0xFD, 0x00, 0xED, 0x44}, // vpermq | |
| 125 {0xC4, 0xE2, 0x4D, 0x36, 0xC0}, // vpermd | |
| 126 {0xC4, 0xE2, 0x7D, 0x5A, 0x45, 0xD0}, // vbroadcasti128 | |
| 127 {0xC4, 0xE3, 0x7D, 0x38, 0x2C, 0x0F, 0x01}, // vinserti128 | |
| 128 {0xC4, 0xE2, 0x79, 0x78, 0xC0}, // vpbroadcastb | |
| 129 {0xC4, 0xE2, 0x7D, 0x58, 0x40, 0x04}, // vpbroadcastd | |
| 130 {0xC4, 0xE2, 0x7D, 0x18, 0xC0}, // vbroadcastss | |
| 131 {0xC4, 0xE3, 0x7D, 0x39, 0xC8, 0x01}, // vextracti128 | |
| 132 {0xC4, 0xE2, 0x7D, 0x90, 0x1C, 0x88}, // vpgatherdd | |
| 133 {0xC4, 0xE2, 0x7D, 0x8C, 0x00}, // vpmaskmovd | |
| 134 {0xC4, 0xE2, 0x7D, 0x8E, 0x90}, // vpmaskmovd | |
| 135 | |
| 136 // F16C instructions. | |
| 137 {0xC4, 0xE3, 0x79, 0x1D, 0xC8, 0x00}, // vcvtps2ph | |
| 138 {0xC4, 0xE2, 0x79, 0x13, 0xE0}, // vcvtps2ps | |
| 139 }; | |
| 140 | 124 |
| 141 void TestBadlyDecodedInstruction(const uint8_t* code, size_t code_length) { | 125 void TestBadlyDecodedInstruction(const uint8_t* code, size_t code_length) { |
| 142 _DInst inst[1] = {}; | 126 _DInst inst[1] = {}; |
| 143 unsigned int inst_count = 0; | 127 unsigned int inst_count = 0; |
| 144 _DecodeResult result = RawDecomposeCode( | 128 _DecodeResult result = RawDecomposeCode( |
| 145 code, code_length, inst, arraysize(inst), &inst_count); | 129 code, code_length, inst, arraysize(inst), &inst_count); |
| 146 EXPECT_EQ(DECRES_MEMORYERR, result); | 130 EXPECT_EQ(DECRES_MEMORYERR, result); |
| 147 EXPECT_EQ(0u, inst_count); | 131 EXPECT_EQ(0u, inst_count); |
| 148 | 132 |
| 149 result = DecomposeCode( | 133 result = DecomposeCode( |
| (...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 364 results, | 348 results, |
| 365 kMaxResults, | 349 kMaxResults, |
| 366 &result_count)); | 350 &result_count)); |
| 367 EXPECT_EQ(1, result_count); | 351 EXPECT_EQ(1, result_count); |
| 368 EXPECT_EQ(64, results[0].ops[0].size); | 352 EXPECT_EQ(64, results[0].ops[0].size); |
| 369 } | 353 } |
| 370 | 354 |
| 371 // If this test starts failing then Distorm now properly handles the AVX2 | 355 // If this test starts failing then Distorm now properly handles the AVX2 |
| 372 // instructions. Please remove the workaround in disassembler_util.cc. | 356 // instructions. Please remove the workaround in disassembler_util.cc. |
| 373 TEST(DisassemblerUtilTest, TestBadlyDecodedVexInstructions) { | 357 TEST(DisassemblerUtilTest, TestBadlyDecodedVexInstructions) { |
| 374 for (const auto iter : kVexInstructions) { | 358 for (const auto iter : unittests::kVexInstructions) { |
| 375 EXPECT_NO_FATAL_FAILURE( | 359 EXPECT_NO_FATAL_FAILURE( |
| 376 TestBadlyDecodedInstruction(iter.data(), iter.size())); | 360 TestBadlyDecodedInstruction(iter.data(), iter.size())); |
| 377 } | 361 } |
| 378 } | 362 } |
| 379 | 363 |
| 364 TEST(DisassemblerUtilTest, TestBadlyDecodedVexInstructionsModRMVariants) { |
| 365 for (const auto iter : unittests::kVexInstructionsModRMVariants) { |
| 366 _DInst inst[1] = {}; |
| 367 unsigned int inst_count = 0; |
| 368 |
| 369 _DecodeResult result = DecomposeCode(iter.data(), iter.size(), inst, |
| 370 arraysize(inst), &inst_count); |
| 371 EXPECT_EQ(DECRES_SUCCESS, result); |
| 372 EXPECT_EQ(1u, inst_count); |
| 373 EXPECT_EQ(iter.size(), inst[0].size); |
| 374 } |
| 375 } |
| 376 |
| 380 TEST(DisassemblerUtilTest, TestBadlyDecodedCRC32) { | 377 TEST(DisassemblerUtilTest, TestBadlyDecodedCRC32) { |
| 381 // CRC32 with a 16 bit operand size prefix is not handled correctly by | 378 // CRC32 with a 16 bit operand size prefix is not handled correctly by |
| 382 // distorm. | 379 // distorm. |
| 383 EXPECT_NO_FATAL_FAILURE( | 380 EXPECT_NO_FATAL_FAILURE( |
| 384 TestBadlyDecodedInstruction(kCrc32CX, sizeof(kCrc32CX))); | 381 TestBadlyDecodedInstruction(kCrc32CX, sizeof(kCrc32CX))); |
| 385 } | 382 } |
| 386 | 383 |
| 387 } // namespace core | 384 } // namespace core |
| OLD | NEW |