| 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, |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 111 const uint8_t kRet[] = {0xC3}; | 111 const uint8_t kRet[] = {0xC3}; |
| 112 const uint8_t kRetN[] = {0xC2, 0x08, 0x00}; | 112 const uint8_t kRetN[] = {0xC2, 0x08, 0x00}; |
| 113 const uint8_t kJe[] = {0x74, 0xCA}; | 113 const uint8_t kJe[] = {0x74, 0xCA}; |
| 114 const uint8_t kSysEnter[] = {0x0F, 0x34}; | 114 const uint8_t kSysEnter[] = {0x0F, 0x34}; |
| 115 const uint8_t kSysExit[] = {0x0F, 0x35}; | 115 const uint8_t kSysExit[] = {0x0F, 0x35}; |
| 116 | 116 |
| 117 // Interrupts. | 117 // Interrupts. |
| 118 const uint8_t kInt2[] = {0xCD, 0x02}; | 118 const uint8_t kInt2[] = {0xCD, 0x02}; |
| 119 const uint8_t kInt3[] = {0xCC}; | 119 const uint8_t kInt3[] = {0xCC}; |
| 120 | 120 |
| 121 // Improperly handled VEX encoded instructions. | 121 // VEX encoded instructions that Distorm doesn't handle properly. |
| 122 const uint8_t kVpermq[] = {0xC4, 0xE3, 0xFD, 0x00, 0xED, 0x44}; | 122 const std::vector<std::vector<uint8_t>> kVexInstructions = { |
| 123 const uint8_t kVpermd[] = {0xC4, 0xE2, 0x4D, 0x36, 0xC0}; | 123 // AVX instructions. |
| 124 const uint8_t kVbroadcasti128[] = {0xC4, 0xE2, 0x7D, 0x5A, 0x45, 0xD0}; | 124 {0xC4, 0xE3, 0xFD, 0x00, 0xED, 0x44}, // vpermq |
| 125 const uint8_t kVinserti128[] = {0xC4, 0xE3, 0x7D, 0x38, 0x2C, 0x0F, 0x01}; | 125 {0xC4, 0xE2, 0x4D, 0x36, 0xC0}, // vpermd |
| 126 const uint8_t kVpbroadcastb[] = {0xC4, 0xE2, 0x79, 0x78, 0xC0}; | 126 {0xC4, 0xE2, 0x7D, 0x5A, 0x45, 0xD0}, // vbroadcasti128 |
| 127 const uint8_t kVbroadcastss[] = {0xC4, 0xE2, 0x7d, 0x18, 0xC0}; | 127 {0xC4, 0xE3, 0x7D, 0x38, 0x2C, 0x0F, 0x01}, // vinserti128 |
| 128 const uint8_t kVextracti128[] = {0xC4, 0xE3, 0x7D, 0x39, 0xC8, 0x01}; | 128 {0xC4, 0xE2, 0x79, 0x78, 0xC0}, // vpbroadcastb |
| 129 const uint8_t kVcvtps2ph[] = {0xC4, 0xE3, 0x79, 0x1D, 0xC8, 0x00}; | 129 {0xC4, 0xE2, 0x7D, 0x58, 0x40, 0x04}, // vpbroadcastd |
| 130 const uint8_t kVcvtps2ps[] = {0xC4, 0xE2, 0x79, 0x13, 0xE0}; | 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 }; |
| 131 | 140 |
| 132 void TestBadlyDecodedInstruction(const uint8_t* code, size_t code_length) { | 141 void TestBadlyDecodedInstruction(const uint8_t* code, size_t code_length) { |
| 133 _DInst inst[1] = {}; | 142 _DInst inst[1] = {}; |
| 134 unsigned int inst_count = 0; | 143 unsigned int inst_count = 0; |
| 135 _DecodeResult result = RawDecomposeCode( | 144 _DecodeResult result = RawDecomposeCode( |
| 136 code, code_length, inst, arraysize(inst), &inst_count); | 145 code, code_length, inst, arraysize(inst), &inst_count); |
| 137 EXPECT_EQ(DECRES_MEMORYERR, result); | 146 EXPECT_EQ(DECRES_MEMORYERR, result); |
| 138 EXPECT_EQ(0u, inst_count); | 147 EXPECT_EQ(0u, inst_count); |
| 139 | 148 |
| 140 result = DecomposeCode( | 149 result = DecomposeCode( |
| (...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 352 EXPECT_EQ(DECRES_SUCCESS, | 361 EXPECT_EQ(DECRES_SUCCESS, |
| 353 DecomposeCode(kFxrstor, | 362 DecomposeCode(kFxrstor, |
| 354 sizeof(kFxrstor), | 363 sizeof(kFxrstor), |
| 355 results, | 364 results, |
| 356 kMaxResults, | 365 kMaxResults, |
| 357 &result_count)); | 366 &result_count)); |
| 358 EXPECT_EQ(1, result_count); | 367 EXPECT_EQ(1, result_count); |
| 359 EXPECT_EQ(64, results[0].ops[0].size); | 368 EXPECT_EQ(64, results[0].ops[0].size); |
| 360 } | 369 } |
| 361 | 370 |
| 362 // If one of these test starts failing then Distorm now properly handles the | 371 // If this test starts failing then Distorm now properly handles the AVX2 |
| 363 // AVX2 instructions. Please remove the workaround in disassembler_util.cc. | 372 // instructions. Please remove the workaround in disassembler_util.cc. |
| 364 | 373 TEST(DisassemblerUtilTest, TestBadlyDecodedVexInstructions) { |
| 365 TEST(DisassemblerUtilTest, TestBadlyDecodedVpermq) { | 374 for (const auto iter : kVexInstructions) { |
| 366 EXPECT_NO_FATAL_FAILURE(TestBadlyDecodedInstruction( | 375 EXPECT_NO_FATAL_FAILURE( |
| 367 kVpermq, sizeof(kVpermq))); | 376 TestBadlyDecodedInstruction(iter.data(), iter.size())); |
| 368 } | 377 } |
| 369 | |
| 370 TEST(DisassemblerUtilTest, TestBadlyDecodedVpermd) { | |
| 371 EXPECT_NO_FATAL_FAILURE(TestBadlyDecodedInstruction( | |
| 372 kVpermd, sizeof(kVpermd))); | |
| 373 } | |
| 374 | |
| 375 TEST(DisassemblerUtilTest, TestBadlyDecodedVbroadcasti128) { | |
| 376 EXPECT_NO_FATAL_FAILURE(TestBadlyDecodedInstruction( | |
| 377 kVbroadcasti128, sizeof(kVbroadcasti128))); | |
| 378 } | |
| 379 | |
| 380 TEST(DisassemblerUtilTest, TestBadlyDecodedVinserti128) { | |
| 381 EXPECT_NO_FATAL_FAILURE(TestBadlyDecodedInstruction( | |
| 382 kVinserti128, sizeof(kVinserti128))); | |
| 383 } | |
| 384 | |
| 385 TEST(DisassemblerUtilTest, TestBadlyDecodedVpbroadcastb) { | |
| 386 EXPECT_NO_FATAL_FAILURE(TestBadlyDecodedInstruction( | |
| 387 kVpbroadcastb, sizeof(kVpbroadcastb))); | |
| 388 } | |
| 389 | |
| 390 TEST(DisassemblerUtilTest, TestBadlyDecodedVbroadcastss) { | |
| 391 EXPECT_NO_FATAL_FAILURE(TestBadlyDecodedInstruction( | |
| 392 kVpbroadcastb, sizeof(kVbroadcastss))); | |
| 393 } | |
| 394 | |
| 395 TEST(DisassemblerUtilTest, TestBadlyDecodedVextracti128) { | |
| 396 EXPECT_NO_FATAL_FAILURE(TestBadlyDecodedInstruction( | |
| 397 kVextracti128, sizeof(kVextracti128))); | |
| 398 } | |
| 399 | |
| 400 TEST(DisassemblerUtilTest, TestBadlyDecodedVcvtps2ph) { | |
| 401 EXPECT_NO_FATAL_FAILURE(TestBadlyDecodedInstruction( | |
| 402 kVcvtps2ph, sizeof(kVcvtps2ph))); | |
| 403 } | |
| 404 | |
| 405 TEST(DisassemblerUtilTest, TestBadlyDecodedVcvtps2ps) { | |
| 406 EXPECT_NO_FATAL_FAILURE(TestBadlyDecodedInstruction( | |
| 407 kVcvtps2ps, sizeof(kVcvtps2ps))); | |
| 408 } | 378 } |
| 409 | 379 |
| 410 TEST(DisassemblerUtilTest, TestBadlyDecodedCRC32) { | 380 TEST(DisassemblerUtilTest, TestBadlyDecodedCRC32) { |
| 411 // CRC32 with a 16 bit operand size prefix is not handled correctly by | 381 // CRC32 with a 16 bit operand size prefix is not handled correctly by |
| 412 // distorm. | 382 // distorm. |
| 413 EXPECT_NO_FATAL_FAILURE( | 383 EXPECT_NO_FATAL_FAILURE( |
| 414 TestBadlyDecodedInstruction(kCrc32CX, sizeof(kCrc32CX))); | 384 TestBadlyDecodedInstruction(kCrc32CX, sizeof(kCrc32CX))); |
| 415 } | 385 } |
| 416 | 386 |
| 417 } // namespace core | 387 } // namespace core |
| OLD | NEW |