1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629
/*
* Copyright (c) 2021 Bitdefender
* SPDX-License-Identifier: Apache-2.0
*/
//! Decodes instructions.
use crate::cpu_modes::CpuModes;
use crate::cpuid::Cpuid;
use crate::decode_error::{status_to_error, DecodeError};
use crate::fpu_flags::FpuFlags;
use crate::instruction_category::Category;
use crate::isa_set::IsaSet;
use crate::mnemonic::Mnemonic;
use crate::operand;
use crate::operand::{OpAccess, Operands, OperandsLookup};
use crate::rflags::flags_raw;
use crate::tuple::Tuple;
use core::convert::TryFrom;
use core::fmt;
use core::mem;
/// Represents a succesfull instruction decoding, or failure.
pub type DecodeResult = Result<DecodedInstruction, DecodeError>;
/// The decoding mode to use.
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
pub enum DecodeMode {
/// 16-bit.
Bits16,
/// 32-bit.
Bits32,
/// 64-bit.
Bits64,
}
#[doc(hidden)]
impl From<DecodeMode> for (u8, u8) {
fn from(mode: DecodeMode) -> Self {
match mode {
DecodeMode::Bits16 => (ffi::ND_CODE_16 as u8, ffi::ND_DATA_16 as u8),
DecodeMode::Bits32 => (ffi::ND_CODE_32 as u8, ffi::ND_DATA_32 as u8),
DecodeMode::Bits64 => (ffi::ND_CODE_64 as u8, ffi::ND_DATA_64 as u8),
}
}
}
/// Encoding mode.
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
pub enum EncodingMode {
/// Legacy encoded instruction.
Legacy,
/// XOP encoded instruction.
Xop,
/// VEX encoded instruction.
Vex,
/// EVEX encoded instruction.
Evex,
}
#[doc(hidden)]
impl EncodingMode {
pub(crate) fn from_raw(value: u32) -> Result<Self, DecodeError> {
match value {
ffi::ND_ENCM_LEGACY => Ok(EncodingMode::Legacy),
ffi::ND_ENCM_XOP => Ok(EncodingMode::Xop),
ffi::ND_ENCM_VEX => Ok(EncodingMode::Vex),
ffi::ND_ENCM_EVEX => Ok(EncodingMode::Evex),
_ => Err(DecodeError::InternalError(value.into())),
}
}
}
/// VEX mode.
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
pub enum VexMode {
/// 2B VEX prefix (0xC5).
Vex2b,
/// 3B VEX prefix (0xC4).
Vex3b,
}
#[doc(hidden)]
impl VexMode {
pub(crate) fn from_raw(value: u32) -> Result<Self, DecodeError> {
match value {
ffi::ND_VEXM_2B => Ok(VexMode::Vex2b),
ffi::ND_VEXM_3B => Ok(VexMode::Vex3b),
_ => Err(DecodeError::InternalError(value.into())),
}
}
}
/// Addressing mode.
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
pub enum AddressingMode {
/// 16 bit addressing.
Addr16,
/// 32 bit addressing.
Addr32,
/// 64 bit addressing.
Addr64,
}
#[doc(hidden)]
impl AddressingMode {
pub(crate) fn from_raw(value: u32) -> Result<Self, DecodeError> {
match value {
ffi::ND_ADDR_16 => Ok(AddressingMode::Addr16),
ffi::ND_ADDR_32 => Ok(AddressingMode::Addr32),
ffi::ND_ADDR_64 => Ok(AddressingMode::Addr64),
_ => Err(DecodeError::InternalError(value.into())),
}
}
}
/// Operand mode/size.
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
pub enum OperandSize {
/// 16 bit operand size.
OpSize16,
/// 32 bit operand size.
OpSize32,
/// 64 bit operand size.
OpSize64,
}
#[doc(hidden)]
impl OperandSize {
pub(crate) fn from_raw(value: u32) -> Result<Self, DecodeError> {
match value {
ffi::ND_OPSZ_16 => Ok(OperandSize::OpSize16),
ffi::ND_OPSZ_32 => Ok(OperandSize::OpSize32),
ffi::ND_OPSZ_64 => Ok(OperandSize::OpSize64),
_ => Err(DecodeError::InternalError(value.into())),
}
}
}
/// Operand mode/size.
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
pub enum VectorSize {
/// 128 bit vector size.
VecSize128,
/// 256 bit vector size.
VecSize256,
/// 512 bit vector size.
VecSize512,
}
#[doc(hidden)]
impl VectorSize {
pub(crate) fn from_raw(value: u32) -> Result<Self, DecodeError> {
match value {
ffi::ND_VECM_128 => Ok(VectorSize::VecSize128),
ffi::ND_VECM_256 => Ok(VectorSize::VecSize256),
ffi::ND_VECM_512 => Ok(VectorSize::VecSize512),
_ => Err(DecodeError::InternalError(value.into())),
}
}
}
/// Describes the way an instruction accesses the flags register.
///
/// Individual bits can be checked using the [rflags](crate::rflags) module.
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
pub struct FlagsAccess {
/// RFLAGS access mode, as per the entire register.
pub mode: OpAccess,
/// Tested flags.
pub tested: u32,
/// Modified (according to the result) flags.
pub modified: u32,
/// Flags that are always set to 1.
pub set: u32,
/// Flags that are always cleared to 0.
pub cleared: u32,
/// Undefined flags.
pub undefined: u32,
}
/// EVEX rounding mode.
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
pub enum EvexRounding {
/// Round to nearest equal.
NearestEqual,
/// Round down.
Down,
/// Round up.
Up,
/// round to zero.
Zero,
}
#[doc(hidden)]
impl EvexRounding {
pub(crate) fn from_raw(value: u8) -> Result<Self, DecodeError> {
if value == ffi::_ND_ROUNDING::ND_RND_RNE as u8 {
Ok(EvexRounding::NearestEqual)
} else if value == ffi::_ND_ROUNDING::ND_RND_RD as u8 {
Ok(EvexRounding::Down)
} else if value == ffi::_ND_ROUNDING::ND_RND_RU as u8 {
Ok(EvexRounding::Up)
} else if value == ffi::_ND_ROUNDING::ND_RND_RZ as u8 {
Ok(EvexRounding::Zero)
} else {
Err(DecodeError::InternalError(value.into()))
}
}
}
/// Indicates which prefixes are valid for an instruction.
#[allow(clippy::struct_excessive_bools)]
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
pub struct ValidPrefixes {
/// The instruction supports REP prefix.
pub rep: bool,
/// The instruction supports REPZ/REPNZ prefixes.
pub rep_cond: bool,
/// The instruction supports LOCK prefix.
pub lock: bool,
/// The instruction supports XACQUIRE/XRELEASE prefixes.
pub hle: bool,
/// The instruction supports only XACQUIRE.
pub xacquire: bool,
/// The instruction supports only XRELEASE.
pub xrelease: bool,
/// The instruction supports BND prefix.
pub bnd: bool,
/// The instruction supports branch hints.
pub bhint: bool,
/// HLE prefix is accepted without LOCK.
pub hle_no_lock: bool,
/// The instruction supports the DNT (Do Not Track) CET prefix.
pub dnt: bool,
}
#[doc(hidden)]
impl ValidPrefixes {
pub(crate) fn from_raw(raw: ffi::ND_VALID_PREFIXES) -> Self {
let raw = unsafe { raw.__bindgen_anon_1 };
Self {
rep: raw.Rep() != 0,
rep_cond: raw.RepCond() != 0,
lock: raw.Lock() != 0,
hle: raw.Hle() != 0,
xacquire: raw.Xacquire() != 0,
xrelease: raw.Xrelease() != 0,
bnd: raw.Bnd() != 0,
bhint: raw.Bhint() != 0,
hle_no_lock: raw.HleNoLock() != 0,
dnt: raw.Dnt() != 0,
}
}
}
/// Indicates which decorators are valid for an instruction.
#[allow(clippy::struct_excessive_bools)]
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
pub struct ValidDecorators {
/// The instruction supports embedded rounding mode.
pub er: bool,
/// The instruction supports suppress all exceptions mode.
pub sae: bool,
/// The instruction supports zeroing.
pub zero: bool,
/// The instruction supports mask registers.
pub mask: bool,
/// The instruction supports broadcast.
pub broadcast: bool,
}
#[doc(hidden)]
impl ValidDecorators {
pub(crate) fn from_raw(raw: ffi::ND_VALID_DECORATORS) -> Self {
let raw = unsafe { raw.__bindgen_anon_1 };
Self {
er: raw.Er() != 0,
sae: raw.Sae() != 0,
zero: raw.Zero() != 0,
mask: raw.Mask() != 0,
broadcast: raw.Broadcast() != 0,
}
}
}
/// A decoded instruction.
#[derive(Copy, Clone, Debug)]
pub struct DecodedInstruction {
inner: ffi::INSTRUX,
ip: u64,
instruction: Mnemonic,
length: usize,
}
impl DecodedInstruction {
/// Decodes an array of bytes.
///
/// # Arguments
///
/// * `code` - An [`u8`] slice that holds the code to be decoded. Note that decoding is attempted only from offset
/// 0 inside this code chunk.
/// * `mode` - The mode in which to decode the instruction.
/// * `ip` - The instruction pointer value to use when formatting the decoded instruction. Does not affect the
/// decoding process in any way. If not needed, use [decode](DecodedInstruction::decode) instead.
///
/// # Errors
///
/// Wil return `Err` if the given bytes do not encode a valid instruction in the given mode.
///
/// # Examples
///
/// ```
/// # use bddisasm::DecodeError;
/// #
/// # fn main() -> Result<(), DecodeError> {
/// use bddisasm::{DecodedInstruction, DecodeMode, Mnemonic};
///
/// let code = vec![0x48, 0x8b, 0x05, 0xf9, 0xff, 0xff, 0xff];
///
/// let instruction = DecodedInstruction::decode_with_ip(&code, DecodeMode::Bits64, 0)?;
/// assert_eq!(format!("{}", instruction), "MOV rax, qword ptr [rel 0x0]");
///
/// let instruction = DecodedInstruction::decode_with_ip(&code, DecodeMode::Bits64, 0x100)?;
/// assert_eq!(format!("{}", instruction), "MOV rax, qword ptr [rel 0x100]");
/// # Ok(())
/// # }
/// ```
///
/// # Panics
///
/// This function will panic if the result of the C library is unrecognized. This can not happen under normal
/// circumstances.
pub fn decode_with_ip(code: &[u8], mode: DecodeMode, ip: u64) -> DecodeResult {
let mut instrux: mem::MaybeUninit<ffi::INSTRUX> = mem::MaybeUninit::uninit();
let instrux = instrux.as_mut_ptr();
let (code_def, data_def) = mode.into();
let status = unsafe {
ffi::NdDecodeEx(
instrux,
code.as_ptr(),
code.len() as u64,
code_def,
data_def,
)
};
status_to_error(status)?;
let instrux = unsafe { *instrux };
Ok(DecodedInstruction {
inner: instrux,
ip,
instruction: Mnemonic::try_from(instrux.Instruction).unwrap(),
length: instrux.Length as usize,
})
}
/// Decodes an array of bytes.
///
/// This function is a thin wrapper over [`decode_with_ip`](DecodedInstruction::decode_with_ip) and behaves exactly
/// the same. It sets `ip` to 0.
///
/// # Errors
///
/// Wil return `Err` if the given bytes do not encode a valid instruction in the given mode.
///
/// # Panics
///
/// This function will panic if the result of the C library is unrecognized. This can not happen under normal
/// circumstances.
pub fn decode(code: &[u8], mode: DecodeMode) -> DecodeResult {
Self::decode_with_ip(code, mode, 0)
}
/// Get the mnemonic of the instruction.
#[inline]
#[must_use]
pub fn mnemonic(&self) -> Mnemonic {
self.instruction
}
/// Get the instruction length, in bytes.
///
/// It is guaranteed that no instruction will exceed a length of 15 bytes.
#[inline]
#[must_use]
pub fn length(&self) -> usize {
self.length
}
/// Get the instruction operands.
///
/// For working with specific operands (like the source or destination),
/// [`operand_lookup()`](DecodedInstruction::operand_lookup) might be a better choice.
///
/// The number of elements in the returned vector will always be equal to
/// [`operands_count`](DecodedInstruction::operands_count).
///
/// # Examples
///
/// See [`operand`] for more in-depth examples on how to work with instruction operands.
///
/// ```
/// # use bddisasm::DecodeError;
/// #
/// # fn main() -> Result<(), DecodeError> {
/// use bddisasm::{DecodedInstruction, DecodeMode, Mnemonic};
///
/// // `MOV ebx, dword ptr [ecx+edi]`
/// let instruction = DecodedInstruction::decode(b"\x8b\x1c\x39", DecodeMode::Bits32)?; ///
/// let operands = instruction.operands();
/// // The first operand is the destination
/// let dst = operands[0];
/// // The second operand is the source
/// let src = operands[1];
///
/// // Print information about the operands
/// println!("{:#?} {:#?}", dst.info, src.info);
/// # Ok(())
/// # }
/// ```
///
/// # Panics
///
/// This function will panic if the operand returned by the C library is invalid. This can not happen under normal
/// circumstances.
#[must_use]
pub fn operands(&self) -> Operands {
let mut operands = Operands::default();
let op_count = self.inner.OperandsCount();
for op_index in 0..op_count {
operands.operands[op_index as usize] =
operand::Operand::from_raw(self.inner.Operands[op_index as usize]).unwrap();
}
operands.actual_count = op_count as usize;
operands
}
/// Returns the CPUID support flag.
///
/// If [`None`], the instruction is supported on any CPU, and no CPUID flag exists.
///
/// # Examples
///
/// See [cpuid](crate::cpuid) for more in-depth examples on how to use the CPUID information.
///
/// ```
/// # use bddisasm::DecodeError;
/// #
/// # fn main() -> Result<(), DecodeError> {
/// use bddisasm::{DecodedInstruction, DecodeMode, Mnemonic};
///
/// // `RDMSR`
/// let instruction = DecodedInstruction::decode(b"\x0f\x32", DecodeMode::Bits64)?;
/// let cpuid = instruction.cpuid();
/// assert!(cpuid.is_some());
///
/// let cpuid = cpuid.unwrap();
/// assert_eq!(cpuid.leaf, 1);
/// assert_eq!(cpuid.sub_leaf, None);
/// assert_eq!(cpuid.register, 2);
/// assert_eq!(cpuid.bit, 5);
///
/// # Ok(())
/// # }
/// ```
#[must_use]
pub fn cpuid(&self) -> Option<Cpuid> {
let cpuid = unsafe { self.inner.CpuidFlag.__bindgen_anon_1 };
let leaf = cpuid.Leaf;
if leaf == ffi::ND_CFF_NO_LEAF {
None
} else {
let sub_leaf = cpuid.SubLeaf();
let sub_leaf = if sub_leaf == ffi::ND_CFF_NO_SUBLEAF {
None
} else {
Some(sub_leaf)
};
let register = cpuid.Reg() as u8;
let bit = u64::from(cpuid.Bit());
Some(Cpuid {
leaf,
sub_leaf,
register,
bit,
})
}
}
/// Get the encoding mode used.
///
/// # Panics
///
/// This function will panic if the encoding mode is unrecognized. This can not happen under normal circumstances.
#[inline]
#[must_use]
pub fn encoding_mode(&self) -> EncodingMode {
EncodingMode::from_raw(u32::from(self.inner.EncMode())).unwrap()
}
/// Get the VEX mode, if any.
///
/// # Panics
///
/// This function will panic if the VEX mode is unrecognized. This can not happen under normal circumstances.
#[inline]
#[must_use]
pub fn vex_mode(&self) -> Option<VexMode> {
if self.has_vex() {
Some(VexMode::from_raw(u32::from(self.inner.VexMode())).unwrap())
} else {
None
}
}
/// Get the addressing mode.
///
/// # Panics
///
/// This function will panic if the addressing mode is unrecognized. This can not happen under normal circumstances.
#[inline]
#[must_use]
pub fn addr_mode(&self) -> AddressingMode {
AddressingMode::from_raw(u32::from(self.inner.AddrMode())).unwrap()
}
/// Get the operand mode/size.
///
/// This is computed based on the passed-in [`DecodeMode`] and instruction prefixes.
///
/// # Remarks
///
/// The effective mode can be different (see [`effective_op_mode`](DecodedInstruction::effective_op_mode)).
///
/// # Examples
///
/// Using [`DecodeMode::Bits64`], `0x50` encodes a `PUSH rax` instruction with an operand size of
/// 32 because it has no prefix that promotes it, but the effective size is 64 because the instruction always
/// operates on 64 bits.
///
/// ```
/// # use bddisasm::DecodeError;
/// #
/// # fn main() -> Result<(), DecodeError> {
/// use bddisasm::{DecodedInstruction, DecodeMode, Mnemonic, OperandSize};
///
/// let ins =
/// DecodedInstruction::decode(&[0x50], DecodeMode::Bits64)?;
/// assert_eq!(ins.mnemonic(), Mnemonic::PUSH);
/// assert_eq!(ins.op_mode(), OperandSize::OpSize32);
/// assert_eq!(ins.effective_op_mode(), OperandSize::OpSize64);
///
/// let ins =
/// DecodedInstruction::decode(&[0x48, 0x50], DecodeMode::Bits64)?;
/// assert_eq!(ins.mnemonic(), Mnemonic::PUSH);
/// assert_eq!(ins.op_mode(), OperandSize::OpSize64);
/// assert_eq!(ins.effective_op_mode(), OperandSize::OpSize64);
/// # Ok(())
/// # }
/// ```
///
/// # Panics
///
/// This function will panic if the operand size is unrecognized. This can not happen under normal circumstances.
#[inline]
#[must_use]
pub fn op_mode(&self) -> OperandSize {
OperandSize::from_raw(u32::from(self.inner.OpMode())).unwrap()
}
/// Get the effective operand mode/size.
///
/// Unlike [`op_mode`](DecodedInstruction::op_mode), this takes into account the actual instruction.
///
/// # Panics
///
/// This function will panic if the operand size is unrecognized. This can not happen under normal circumstances.
#[inline]
#[must_use]
pub fn effective_op_mode(&self) -> OperandSize {
OperandSize::from_raw(u32::from(self.inner.EfOpMode())).unwrap()
}
/// Get the Vector mode/size, if any.
///
/// This is computed based on the passed-in [`DecodeMode`] and instruction prefixes.
///
/// # Remarks
///
/// The effective mode can be different (see [`effective_vec_mode`](DecodedInstruction::effective_vec_mode)).
///
/// # Panics
///
/// This function will panic if the vector mode is unrecognized. This can not happen under normal circumstances.
#[inline]
#[must_use]
pub fn vec_mode(&self) -> Option<VectorSize> {
if self.has_vector() {
Some(VectorSize::from_raw(u32::from(self.inner.VecMode())).unwrap())
} else {
None
}
}
/// Get the Vector mode/size, if any.
///
/// Unlike [`vec_mode`](DecodedInstruction::vec_mode), this takes into account the actual instruction.
///
/// # Panics
///
/// This function will panic if the vector mode is unrecognized. This can not happen under normal circumstances.
#[inline]
#[must_use]
pub fn effective_vec_mode(&self) -> Option<VectorSize> {
if self.has_vector() {
Some(VectorSize::from_raw(u32::from(self.inner.EfVecMode())).unwrap())
} else {
None
}
}
/// `true` if REX is present.
#[inline]
#[must_use]
pub fn has_rex(&self) -> bool {
self.inner.HasRex() != 0
}
/// `true` if VEX is present.
#[inline]
#[must_use]
pub fn has_vex(&self) -> bool {
self.inner.HasVex() != 0
}
/// `true` if XOP is present.
#[inline]
#[must_use]
pub fn has_xop(&self) -> bool {
self.inner.HasXop() != 0
}
/// `true` if EVEX is present.
#[inline]
#[must_use]
pub fn has_evex(&self) -> bool {
self.inner.HasEvex() != 0
}
/// `true` if 0x66 is present.
#[inline]
#[must_use]
pub fn has_op_size(&self) -> bool {
self.inner.HasOpSize() != 0
}
/// `true` if 0x67 is present.
#[inline]
#[must_use]
pub fn has_addr_size(&self) -> bool {
self.inner.HasAddrSize() != 0
}
/// `true` if 0xF0 is present.
#[inline]
#[must_use]
pub fn has_lock(&self) -> bool {
self.inner.HasLock() != 0
}
/// `true` if 0xF2 is present.
#[inline]
#[must_use]
pub fn has_repnz_xacquire_bnd(&self) -> bool {
self.inner.HasRepnzXacquireBnd() != 0
}
/// `true` if 0xF3 is present.
#[inline]
#[must_use]
pub fn has_rep_repz_xrelease(&self) -> bool {
self.inner.HasRepRepzXrelease() != 0
}
/// `true` if segment override is present.
#[inline]
#[must_use]
pub fn has_seg(&self) -> bool {
self.inner.HasSeg() != 0
}
/// `true` if the instruction is repeated up to `RCX` times.
#[inline]
#[must_use]
pub fn is_repeated(&self) -> bool {
self.inner.IsRepeated() != 0
}
/// `true` if the instruction is XACQUIRE enabled.
#[inline]
#[must_use]
pub fn is_xacquire_enabled(&self) -> bool {
self.inner.IsXacquireEnabled() != 0
}
/// `true` if the instruction is XRELEASE enabled.
#[inline]
#[must_use]
pub fn is_xrelease_enabled(&self) -> bool {
self.inner.IsXreleaseEnabled() != 0
}
/// `true` if the instruction uses RIP relative addressing.
#[inline]
#[must_use]
pub fn is_rip_relative(&self) -> bool {
self.inner.IsRipRelative() != 0
}
/// `true` if this is an indirect CALL/JMP that is CET tracked.
#[inline]
#[must_use]
pub fn is_cet_tracked(&self) -> bool {
self.inner.IsCetTracked() != 0
}
/// `true` if we have valid MODRM.
#[inline]
#[must_use]
pub fn has_mod_rm(&self) -> bool {
self.inner.HasModRm() != 0
}
/// `true` if we have valid SIB.
#[inline]
#[must_use]
pub fn has_sib(&self) -> bool {
self.inner.HasSib() != 0
}
/// `true` if the instruction has displacement.
#[inline]
#[must_use]
pub fn has_disp(&self) -> bool {
self.inner.HasDisp() != 0
}
/// `true` if the instruction contains a direct address (ie, `CALL far 0x9A`).
#[inline]
#[must_use]
pub fn has_addr(&self) -> bool {
self.inner.HasAddr() != 0
}
/// `true` if the instruction contains a moffset (ie, `MOV al, [mem], 0xA0`).
#[inline]
#[must_use]
pub fn has_moffset(&self) -> bool {
self.inner.HasMoffset() != 0
}
/// `true` if immediate is present.
#[inline]
#[must_use]
pub fn has_imm1(&self) -> bool {
self.inner.HasImm1() != 0
}
/// `true` if second immediate is present.
#[inline]
#[must_use]
pub fn has_imm2(&self) -> bool {
self.inner.HasImm2() != 0
}
/// `true` if the instruction contains a relative offset (ie, `Jcc 0x7x`).
#[inline]
#[must_use]
pub fn has_rel_offs(&self) -> bool {
self.inner.HasRelOffs() != 0
}
/// `true` if SSE immediate that encodes additional registers is present.
#[inline]
#[must_use]
pub fn has_sse_imm(&self) -> bool {
self.inner.HasSseImm() != 0
}
/// `true` if the instruction uses compressed displacement.
#[inline]
#[must_use]
pub fn has_comp_disp(&self) -> bool {
self.inner.HasCompDisp() != 0
}
/// `true` if the instruction uses broadcast addressing.
#[inline]
#[must_use]
pub fn has_broadcast(&self) -> bool {
self.inner.HasBroadcast() != 0
}
/// `true` if the instruction has mask.
#[inline]
#[must_use]
pub fn has_mask(&self) -> bool {
self.inner.HasMask() != 0
}
/// `true` if the instruction uses zeroing.
#[inline]
#[must_use]
pub fn has_zero(&self) -> bool {
self.inner.HasZero() != 0
}
/// `true` if the instruction has embedded rounding.
#[inline]
#[must_use]
pub fn has_er(&self) -> bool {
self.inner.HasEr() != 0
}
/// `true` if the instruction has SAE.
#[inline]
#[must_use]
pub fn has_sae(&self) -> bool {
self.inner.HasSae() != 0
}
/// `true` if the instruction ignores embedded rounding.
#[inline]
#[must_use]
pub fn has_ign_er(&self) -> bool {
self.inner.HasIgnEr() != 0
}
/// `true` if changing prefix.
#[inline]
#[must_use]
pub fn has_mandatory_66(&self) -> bool {
self.inner.HasMandatory66() != 0
}
/// 0x66 is mandatory prefix. Does not behave as REP prefix.
#[inline]
#[must_use]
pub fn has_mandatory_f2(&self) -> bool {
self.inner.HasMandatoryF2() != 0
}
/// 0x66 is mandatory prefix. Does not behave as REP prefix.
#[inline]
#[must_use]
pub fn has_mandatory_f3(&self) -> bool {
self.inner.HasMandatoryF3() != 0
}
/// The length of the instruction word. 2, 4 or 8.
#[inline]
#[must_use]
pub fn word_length(&self) -> usize {
self.inner.WordLength() as usize
}
/// The total number of bytes consumed by prefixes.
///
/// This will also be the offset to the first opcode. The primary opcode will always be the last one.
#[inline]
#[must_use]
pub fn pref_length(&self) -> usize {
self.inner.PrefLength() as usize
}
/// Number of opcode bytes. Max 3.
#[inline]
#[must_use]
pub fn op_length(&self) -> usize {
self.inner.OpLength() as usize
}
/// Displacement length, in bytes. Maximum 4.
#[inline]
#[must_use]
pub fn disp_length(&self) -> usize {
self.inner.DispLength() as usize
}
/// Absolute address length, in bytes. Maximum 8 bytes.
#[inline]
#[must_use]
pub fn addr_length(&self) -> usize {
self.inner.AddrLength() as usize
}
/// Memory offset length, in bytes. Maximum 8 bytes.
#[inline]
#[must_use]
pub fn moffset_length(&self) -> usize {
self.inner.MoffsetLength() as usize
}
/// First immediate length, in bytes. Maximum 8 bytes.
#[inline]
#[must_use]
pub fn imm1_length(&self) -> usize {
self.inner.Imm1Length() as usize
}
/// Second immediate length, in bytes. Maximum 8 bytes.
#[inline]
#[must_use]
pub fn imm2_length(&self) -> usize {
self.inner.Imm2Length() as usize
}
/// Relative offset length, in bytes. Maximum 4 bytes.
#[inline]
#[must_use]
pub fn rel_offs_length(&self) -> usize {
self.inner.RelOffsLength() as usize
}
/// The offset of the first opcode, inside the instruction.
#[inline]
#[must_use]
pub fn op_offset(&self) -> usize {
self.inner.OpOffset() as usize
}
/// The offset of the nominal opcode, inside the instruction.
#[inline]
#[must_use]
pub fn main_op_offset(&self) -> usize {
self.inner.MainOpOffset() as usize
}
/// The offset of the displacement, inside the instruction.
#[inline]
#[must_use]
pub fn disp_offset(&self) -> Option<usize> {
let value = self.inner.DispOffset() as usize;
if value == 0 {
None
} else {
Some(value)
}
}
/// The offset of the hard-coded address.
#[inline]
#[must_use]
pub fn addr_offset(&self) -> Option<usize> {
let value = self.inner.AddrOffset() as usize;
if value == 0 {
None
} else {
Some(value)
}
}
/// The offset of the absolute address, inside the instruction.
#[inline]
#[must_use]
pub fn moffset_offset(&self) -> Option<usize> {
let value = self.inner.MoffsetOffset() as usize;
if value == 0 {
None
} else {
Some(value)
}
}
/// The offset of the immediate, inside the instruction.
#[inline]
#[must_use]
pub fn imm1_offset(&self) -> Option<usize> {
let value = self.inner.Imm1Offset() as usize;
if value == 0 {
None
} else {
Some(value)
}
}
/// The offset of the second immediate, if any, inside the instruction.
#[inline]
#[must_use]
pub fn imm2_offset(&self) -> Option<usize> {
let value = self.inner.Imm2Offset() as usize;
if value == 0 {
None
} else {
Some(value)
}
}
/// The offset of the relative offset used in instruction.
#[inline]
#[must_use]
pub fn rel_offs_offset(&self) -> Option<usize> {
let value = self.inner.RelOffsOffset() as usize;
if value == 0 {
None
} else {
Some(value)
}
}
/// The offset of the SSE immediate, if any, inside the instruction.
#[inline]
#[must_use]
pub fn sse_imm_offset(&self) -> Option<usize> {
let value = self.inner.SseImmOffset() as usize;
if value == 0 {
None
} else {
Some(value)
}
}
/// The offset of the mod rm byte inside the instruction, if any.
#[inline]
#[must_use]
pub fn mod_rm_offset(&self) -> Option<usize> {
let value = self.inner.ModRmOffset() as usize;
if value == 0 {
None
} else {
Some(value)
}
}
/// Number of words accessed on/from the stack.
#[inline]
#[must_use]
pub fn stack_words(&self) -> usize {
self.inner.StackWords() as usize
}
/// The last rep/repz/repnz prefix. if any.
#[inline]
#[must_use]
pub fn rep(&self) -> Option<u8> {
let value = self.inner.Rep;
if value == 0 {
None
} else {
Some(value)
}
}
/// The last segment override prefix. if none. `FS`/`GS` if 64 bit.
#[inline]
#[must_use]
pub fn seg(&self) -> Option<u8> {
let value = self.inner.Seg;
if value == 0 {
None
} else {
Some(value)
}
}
/// Get the `ModRM` byte.
#[inline]
#[must_use]
pub fn mod_rm(&self) -> Option<u8> {
if self.has_mod_rm() {
Some(unsafe { self.inner.ModRm.ModRm })
} else {
None
}
}
/// Get the `SIB` byte.
#[inline]
#[must_use]
pub fn sib(&self) -> Option<u8> {
if self.has_sib() {
Some(unsafe { self.inner.Sib.Sib })
} else {
None
}
}
/// Get the 2-bytes `VEX` prefix.
#[inline]
#[must_use]
pub fn vex2(&self) -> Option<(u8, u8)> {
if matches!(self.vex_mode(), Some(VexMode::Vex2b)) {
let vex2 = self.inner.__bindgen_anon_1;
let vex2 = unsafe { vex2.Vex2.Vex };
Some((vex2[0], vex2[1]))
} else {
None
}
}
/// Get the 3-bytes `VEX` prefix.
#[inline]
#[must_use]
pub fn vex3(&self) -> Option<(u8, u8, u8)> {
if matches!(self.vex_mode(), Some(VexMode::Vex3b)) {
let vex3 = self.inner.__bindgen_anon_1;
let vex3 = unsafe { vex3.Vex3.Vex };
Some((vex3[0], vex3[1], vex3[2]))
} else {
None
}
}
/// Get the `XOP` bytes.
#[inline]
#[must_use]
pub fn xop(&self) -> Option<(u8, u8, u8)> {
if self.has_xop() {
let xop = self.inner.__bindgen_anon_1;
let xop = unsafe { xop.Xop.Xop };
Some((xop[0], xop[1], xop[2]))
} else {
None
}
}
/// Get the `EVEX` bytes.
#[inline]
#[must_use]
pub fn evex(&self) -> Option<(u8, u8, u8, u8)> {
if self.has_evex() {
let evex = self.inner.__bindgen_anon_1;
let evex = unsafe { evex.Evex.Evex };
Some((evex[0], evex[1], evex[2], evex[3]))
} else {
None
}
}
/// Get the absolute offset, if any.
#[inline]
#[must_use]
pub fn moffset(&self) -> Option<u64> {
if self.has_moffset() {
Some(unsafe { self.inner.__bindgen_anon_2.Moffset })
} else {
None
}
}
/// Get the displacement. Max 4 bytes. Used in `ModRM` instructions.
#[inline]
#[must_use]
pub fn disp(&self) -> Option<u32> {
if self.has_disp() {
Some(unsafe { self.inner.__bindgen_anon_2.Displacement })
} else {
None
}
}
/// Get the relative offset, used for branches. Max 4 bytes.
#[inline]
#[must_use]
pub fn rel_offset(&self) -> Option<u32> {
if self.has_rel_offs() {
Some(unsafe { self.inner.__bindgen_anon_2.RelativeOffset })
} else {
None
}
}
/// Get the first immediate.
#[inline]
#[must_use]
pub fn immediate1(&self) -> Option<u64> {
if self.has_imm1() {
Some(self.inner.Immediate1)
} else {
None
}
}
/// Get the second immediate. Used mainly for [`Mnemonic::ENTER`].
#[inline]
#[must_use]
pub fn immediate2(&self) -> Option<u8> {
if self.has_imm2() {
Some(unsafe { self.inner.__bindgen_anon_3.Immediate2 })
} else {
None
}
}
/// Get the SSE immediate. It is used to select a register.
#[inline]
#[must_use]
pub fn sse_immediate(&self) -> Option<u8> {
if self.has_sse_imm() {
Some(unsafe { self.inner.__bindgen_anon_3.SseImmediate })
} else {
None
}
}
/// Get the condition byte.
#[inline]
#[must_use]
pub fn cond(&self) -> Option<u8> {
if (self.inner.Attributes & ffi::ND_FLAG_COND as u64) != 0 {
Some(self.inner.__bindgen_anon_4.Condition())
} else {
None
}
}
/// `true` if the instruction is 3DNow!.
///
/// The opcode is the last byte.
#[inline]
#[must_use]
pub fn is_3d_now(&self) -> bool {
(self.inner.Attributes & ffi::ND_FLAG_3DNOW as u64) != 0
}
/// Get the number of operands.
#[inline]
#[must_use]
pub fn operands_count(&self) -> usize {
self.inner.OperandsCount() as usize
}
/// Number of explicit operands.
///
/// Use this if you want to ignore implicit operands such as stack, flags, etc.
#[inline]
#[must_use]
pub fn exp_operands_count(&self) -> usize {
self.inner.ExpOperandsCount() as usize
}
/// Get the `CS` access mode.
#[inline]
#[must_use]
pub fn cs_access(&self) -> OpAccess {
OpAccess::from_raw(ffi::ND_OPERAND_ACCESS {
Access: self.inner.CsAccess,
})
}
/// Get the `RIP` access mode.
#[inline]
#[must_use]
pub fn rip_access(&self) -> OpAccess {
OpAccess::from_raw(ffi::ND_OPERAND_ACCESS {
Access: self.inner.RipAccess,
})
}
/// Get the stack access mode.
#[inline]
#[must_use]
pub fn stack_access(&self) -> OpAccess {
OpAccess::from_raw(ffi::ND_OPERAND_ACCESS {
Access: self.inner.StackAccess,
})
}
/// Get the memory access mode.
///
/// This includes the stack or shadow stack access.
#[inline]
#[must_use]
pub fn memory_access(&self) -> OpAccess {
OpAccess::from_raw(ffi::ND_OPERAND_ACCESS {
Access: self.inner.MemoryAccess,
})
}
/// `true` if the instruction is a branch.
#[inline]
#[must_use]
pub fn is_branch(&self) -> bool {
self.inner.BranchInfo.IsBranch() != 0
}
/// `true` if the instruction is a conditional branch.
#[inline]
#[must_use]
pub fn is_conditional_branch(&self) -> bool {
self.inner.BranchInfo.IsConditional() != 0
}
/// `true` if the instruction is a indirect branch.
#[inline]
#[must_use]
pub fn is_indirect_branch(&self) -> bool {
self.inner.BranchInfo.IsIndirect() != 0
}
/// `true` if the instruction is a far branch.
#[inline]
#[must_use]
pub fn is_far_branch(&self) -> bool {
self.inner.BranchInfo.IsFar() != 0
}
/// Get the rflags access.
#[must_use]
pub fn flags_access(&self) -> FlagsAccess {
let facc = self.inner.FlagsAccess;
let mode = OpAccess::from_raw(ffi::ND_OPERAND_ACCESS {
Access: self.inner.RflAccess,
});
FlagsAccess {
mode,
tested: flags_raw(facc.Tested),
modified: flags_raw(facc.Modified),
set: flags_raw(facc.Set),
cleared: flags_raw(facc.Cleared),
undefined: flags_raw(facc.Undefined),
}
}
/// `FPU` status word C0-C3 bits access.
///
/// # Panics
///
/// This function will panic if the access mode is unrecognized. This can not happen under normal circumstances.
#[inline]
#[must_use]
pub fn fpu_flags_access(&self) -> FpuFlags {
FpuFlags::from_raw(self.inner.FpuFlagsAccess).unwrap()
}
/// `EVEX` tuple type.
///
/// # Panics
///
/// This function will panic if the EVEX tuple type is unrecognized. This can not happen under normal circumstances.
#[inline]
#[must_use]
pub fn evex_tuple(&self) -> Option<Tuple> {
if self.has_evex() {
Some(Tuple::from_raw(u32::from(self.inner.TupleType)).unwrap())
} else {
None
}
}
/// `EVEX` rounding mode.
///
/// # Panics
///
/// This function will panic if the EVEX rounding mode is unrecognized. This can not happen under normal
/// circumstances.
#[inline]
#[must_use]
pub fn evex_rounding(&self) -> Option<EvexRounding> {
if self.has_er() {
Some(EvexRounding::from_raw(self.inner.RoundingMode()).unwrap())
} else {
None
}
}
/// Get the instruction category.
///
/// # Panics
///
/// This function will panic if the cateogory not recognized. This can not happen under normal circumstances.
#[inline]
#[must_use]
pub fn category(&self) -> Category {
Category::try_from(self.inner.Category).unwrap()
}
/// Get the ISA set.
///
/// # Panics
///
/// This function will panic if the ISA set not recognized. This can not happen under normal circumstances.
#[inline]
#[must_use]
pub fn isa_set(&self) -> IsaSet {
IsaSet::try_from(self.inner.IsaSet).unwrap()
}
/// Get the CPU modes in which the instruction is valid.
///
/// # Examples
///
/// See [`cpu_modes`](crate::cpu_modes) for examples.
#[inline]
#[must_use]
pub fn valid_cpu_modes(&self) -> CpuModes {
CpuModes::from_raw(self.inner.ValidModes)
}
/// Get the valid prefixes for this instruction.
#[inline]
#[must_use]
pub fn valid_prefixes(&self) -> ValidPrefixes {
ValidPrefixes::from_raw(self.inner.ValidPrefixes)
}
/// Get the decorators accepted by the instruction.
#[inline]
#[must_use]
pub fn valid_decorators(&self) -> ValidDecorators {
ValidDecorators::from_raw(self.inner.ValidDecorators)
}
/// Get the main/nominal opcode.
#[inline]
#[must_use]
pub fn primary_op_code(&self) -> u8 {
unsafe { self.inner.__bindgen_anon_4.PrimaryOpCode }
}
/// `true` if the instruction is a SIMD instruction that operates on vector regs.
#[inline]
#[must_use]
pub fn has_vector(&self) -> bool {
self.inner.Attributes & ffi::ND_FLAG_VECTOR as u64 != 0
}
}
impl<'a> DecodedInstruction {
/// Get the instruction bytes.
#[inline]
#[must_use]
pub fn bytes(&'a self) -> &'a [u8] {
&self.inner.InstructionBytes[..self.inner.Length as usize]
}
/// Get the opcode bytes (escape codes and main op code).
#[inline]
#[must_use]
pub fn op_code_bytes(&'a self) -> &'a [u8] {
&self.inner.OpCodeBytes[..self.op_length()]
}
/// Get an operand lookup table.
///
/// This can be useful when needing to work with a specific operand without needing to iterate over the operands
/// returned by [operands()](DecodedInstruction::operands), and without needing to rely on the order of the
/// operands.
///
/// # Examples
///
/// See [`OperandsLookup`] for examples.
///
/// # Panics
///
/// This function will panic if the result of the C library is unrecognized. This can not happen under normal
/// circumstances.
#[inline]
#[must_use]
pub fn operand_lookup(&'a self) -> OperandsLookup {
OperandsLookup::from_raw(&self.inner)
}
}
impl fmt::Display for DecodedInstruction {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let mut buffer: [u8; ffi::ND_MIN_BUF_SIZE as usize] = [0; ffi::ND_MIN_BUF_SIZE as usize];
let status = unsafe {
ffi::NdToText(
&self.inner,
self.ip,
buffer.len() as u32,
buffer.as_mut_ptr().cast::<i8>(),
)
};
match status_to_error(status) {
Ok(_) => {
let text = core::str::from_utf8(&buffer).unwrap();
write!(f, "{}", text.trim_matches(char::from(0)))
}
Err(_) => Err(fmt::Error),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn decode() {
let code = vec![0xb8, 0x00, 0x00, 0x00, 0x00];
let ins = DecodedInstruction::decode(&code, DecodeMode::Bits32).expect("Unable to decode");
assert_eq!(ins.instruction, Mnemonic::MOV);
assert_eq!(ins.bytes(), code);
assert_eq!(format!("{}", ins), "MOV eax, 0x00000000");
}
#[test]
fn decode_with_ip() {
let code = b"\x48\x8b\x05\xf9\xff\xff\xff";
let ins = DecodedInstruction::decode_with_ip(code, DecodeMode::Bits64, 0x100)
.expect("Unable to decode");
assert_eq!(ins.instruction, Mnemonic::MOV);
assert_eq!(ins.bytes(), code);
assert_eq!(format!("{}", ins), "MOV rax, qword ptr [rel 0x100]");
}
fn get_tokens(line: &str, index: usize) -> u32 {
let tokens = line.split(" ").collect::<Vec<&str>>();
let tokens = tokens
.into_iter()
.filter(|s| !s.is_empty())
.collect::<Vec<&str>>();
let token = tokens[index];
if token.starts_with("0x") {
u32::from_str_radix(token.trim_start_matches("0x"), 16).unwrap()
} else {
token.parse().unwrap()
}
}
#[test]
fn constants() {
// These will probably never change, so it is not really worth it to generate the enums and the `From`
// implementations at every build, but these tests should be enough to catch the unlikely situation in which
// a new constant is added.
let bindings = include_str!("../../bddisasm-sys/csrc/inc/bddisasm.h");
let mut shadow_stack_count: u8 = 0;
let mut tuple_count: u32 = 0;
let mut evex_rounding: u8 = 0;
for line in bindings.lines() {
if line.starts_with("#define ND_ENCM_") {
assert!(EncodingMode::from_raw(get_tokens(line, 2)).is_ok());
} else if line.starts_with("#define ND_VEXM_") {
assert!(VexMode::from_raw(get_tokens(line, 2)).is_ok());
} else if line.starts_with("#define ND_ADDR_") {
assert!(AddressingMode::from_raw(get_tokens(line, 2)).is_ok());
} else if line.starts_with("#define ND_OPSZ_") {
assert!(OperandSize::from_raw(get_tokens(line, 2)).is_ok());
} else if line.starts_with("#define ND_VECM_") {
assert!(VectorSize::from_raw(get_tokens(line, 2)).is_ok());
} else if line.starts_with("#define ND_SIZE_")
&& !line.starts_with("#define ND_SIZE_TO_MASK(sz)")
{
// TODO: this test should be in `operand.rs`, but since we are already parsing `bddisasm.h` here it
// felt wastefull to also parse it there.
assert!(operand::OpSize::from_raw(get_tokens(line, 2)).is_ok());
} else if line.starts_with(" ND_SHSTK_") {
// TODO: this test should be in `operand.rs`, but since we are already parsing `bddisasm.h` here it
// felt wastefull to also parse it there.
assert!(operand::ShadowStackAccess::from_raw(shadow_stack_count).is_ok());
shadow_stack_count += 1;
} else if line.starts_with("#define ND_FPU_FLAG_") {
// TODO: this test should be in `fpu_flags.rs`, but since we are already parsing `bddisasm.h` here it
// felt wastefull to also parse it there.
assert!(
crate::fpu_flags::FpuFlagsAccess::from_raw(get_tokens(line, 2) as u8).is_ok()
);
} else if line.starts_with(" ND_TUPLE_") {
// TODO: this test should be in `operand.rs`, but since we are already parsing `bddisasm.h` here it
// felt wastefull to also parse it there.
assert!(Tuple::from_raw(tuple_count).is_ok());
tuple_count += 1;
} else if line.starts_with(" ND_RND_") {
// TODO: this test should be in `operand.rs`, but since we are already parsing `bddisasm.h` here it
// felt wastefull to also parse it there.
assert!(EvexRounding::from_raw(evex_rounding).is_ok());
evex_rounding += 1;
}
}
}
#[test]
fn status() {
let status = include_str!("../../bddisasm-sys/csrc/inc/bddisasm_status.h");
for line in status.lines() {
if line.starts_with("#define ND_STATUS_SUCCESS")
|| line.starts_with("#define ND_STATUS_HINT_OPERAND_NOT_USED")
{
assert!(status_to_error(get_tokens(line, 2)).is_ok());
} else if line.starts_with("#define ND_STATUS_") {
assert!(status_to_error(get_tokens(line, 2)).is_err());
}
}
}
#[test]
fn check_all_evex_roundings() {
// This is a really contrieved way of making sure that we check all variants of `ffi::_ND_ROUNDING`. If a new
// one is added, this will fail to build. We do this vecause `EvexRounding::from_raw` takes an `u8`.
// NOTE: When a new variant is added, `EvexRounding::from_raw` must be updated.
match ffi::_ND_ROUNDING::ND_RND_RNE {
ffi::_ND_ROUNDING::ND_RND_RNE => {}
ffi::_ND_ROUNDING::ND_RND_RD => {}
ffi::_ND_ROUNDING::ND_RND_RU => {}
ffi::_ND_ROUNDING::ND_RND_RZ => {}
}
}
}