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
|
/***************************************************************************************************
Zyan Disassembler Library (Zydis)
Original Author : Florian Bernd
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
***************************************************************************************************/
#include "config.h"
#if ENABLE(ZYDIS)
#include "ZycoreLibC.h"
#include "ZydisUtils.h"
#include <wtf/Compiler.h>
WTF_ALLOW_UNSAFE_BUFFER_USAGE_BEGIN
/* ============================================================================================== */
/* Exported functions */
/* ============================================================================================== */
/* ---------------------------------------------------------------------------------------------- */
/* Address calculation */
/* ---------------------------------------------------------------------------------------------- */
// Signed integer overflow is expected behavior in this function, for wrapping around the
// instruction pointer on jumps right at the end of the address space.
ZYAN_NO_SANITIZE("signed-integer-overflow")
ZyanStatus ZydisCalcAbsoluteAddress(const ZydisDecodedInstruction* instruction,
const ZydisDecodedOperand* operand, ZyanU64 runtime_address, ZyanU64* result_address)
{
if (!instruction || !operand || !result_address)
{
return ZYAN_STATUS_INVALID_ARGUMENT;
}
switch (operand->type)
{
case ZYDIS_OPERAND_TYPE_MEMORY:
if (!operand->mem.disp.has_displacement)
{
return ZYAN_STATUS_INVALID_ARGUMENT;
}
if (operand->mem.base == ZYDIS_REGISTER_EIP)
{
*result_address = ((ZyanU32)runtime_address + instruction->length +
(ZyanU32)operand->mem.disp.value);
return ZYAN_STATUS_SUCCESS;
}
if (operand->mem.base == ZYDIS_REGISTER_RIP)
{
*result_address = (ZyanU64)(runtime_address + instruction->length +
operand->mem.disp.value);
return ZYAN_STATUS_SUCCESS;
}
if ((operand->mem.base == ZYDIS_REGISTER_NONE) &&
(operand->mem.index == ZYDIS_REGISTER_NONE))
{
switch (instruction->address_width)
{
case 16:
*result_address = (ZyanU64)operand->mem.disp.value & 0x000000000000FFFF;
return ZYAN_STATUS_SUCCESS;
case 32:
*result_address = (ZyanU64)operand->mem.disp.value & 0x00000000FFFFFFFF;
return ZYAN_STATUS_SUCCESS;
case 64:
*result_address = (ZyanU64)operand->mem.disp.value;
return ZYAN_STATUS_SUCCESS;
default:
return ZYAN_STATUS_INVALID_ARGUMENT;
}
}
break;
case ZYDIS_OPERAND_TYPE_IMMEDIATE:
if (operand->imm.is_signed && operand->imm.is_relative)
{
*result_address = (ZyanU64)((ZyanI64)runtime_address + instruction->length +
operand->imm.value.s);
switch (instruction->machine_mode)
{
case ZYDIS_MACHINE_MODE_LONG_COMPAT_16:
case ZYDIS_MACHINE_MODE_LEGACY_16:
case ZYDIS_MACHINE_MODE_REAL_16:
case ZYDIS_MACHINE_MODE_LONG_COMPAT_32:
case ZYDIS_MACHINE_MODE_LEGACY_32:
// `XBEGIN` is a special case as it doesn't truncate computed address
// This behavior is documented by Intel (SDM Vol. 2C):
// Use of the 16-bit operand size does not cause this address to be truncated to
// 16 bits, unlike a near jump to a relative offset.
if ((instruction->operand_width == 16) &&
(instruction->mnemonic != ZYDIS_MNEMONIC_XBEGIN))
{
*result_address &= 0xFFFF;
}
break;
case ZYDIS_MACHINE_MODE_LONG_64:
break;
default:
return ZYAN_STATUS_INVALID_ARGUMENT;
}
return ZYAN_STATUS_SUCCESS;
}
break;
default:
break;
}
return ZYAN_STATUS_INVALID_ARGUMENT;
}
ZyanStatus ZydisCalcAbsoluteAddressEx(const ZydisDecodedInstruction* instruction,
const ZydisDecodedOperand* operand, ZyanU64 runtime_address,
const ZydisRegisterContext* register_context, ZyanU64* result_address)
{
// TODO: Test this with AGEN/MIB operands
// TODO: Add support for Gather/Scatter instructions
if (!instruction || !operand || !register_context || !result_address)
{
return ZYAN_STATUS_INVALID_ARGUMENT;
}
if ((operand->type != ZYDIS_OPERAND_TYPE_MEMORY) ||
((operand->mem.base == ZYDIS_REGISTER_NONE) &&
(operand->mem.index == ZYDIS_REGISTER_NONE)) ||
(operand->mem.base == ZYDIS_REGISTER_EIP) ||
(operand->mem.base == ZYDIS_REGISTER_RIP))
{
return ZydisCalcAbsoluteAddress(instruction, operand, runtime_address, result_address);
}
ZyanU64 value = operand->mem.disp.value;
if (operand->mem.base)
{
value += register_context->values[operand->mem.base];
}
if (operand->mem.index)
{
value += register_context->values[operand->mem.index] * operand->mem.scale;
}
switch (instruction->address_width)
{
case 16:
*result_address = value & 0x000000000000FFFF;
return ZYAN_STATUS_SUCCESS;
case 32:
*result_address = value & 0x00000000FFFFFFFF;
return ZYAN_STATUS_SUCCESS;
case 64:
*result_address = value;
return ZYAN_STATUS_SUCCESS;
default:
return ZYAN_STATUS_INVALID_ARGUMENT;
}
}
/* ---------------------------------------------------------------------------------------------- */
/* Accessed CPU flags */
/* ---------------------------------------------------------------------------------------------- */
ZyanStatus ZydisGetAccessedFlagsByAction(const ZydisDecodedInstruction* instruction,
ZydisCPUFlagAction action, ZydisCPUFlags* flags)
{
if (!instruction || !flags)
{
return ZYAN_STATUS_INVALID_ARGUMENT;
}
*flags = 0;
for (ZyanUSize i = 0; i < ZYAN_ARRAY_LENGTH(instruction->accessed_flags); ++i)
{
if (instruction->accessed_flags[i].action == action)
{
*flags |= (1 << i);
}
}
return ZYAN_STATUS_SUCCESS;
}
ZyanStatus ZydisGetAccessedFlagsRead(const ZydisDecodedInstruction* instruction,
ZydisCPUFlags* flags)
{
if (!instruction || !flags)
{
return ZYAN_STATUS_INVALID_ARGUMENT;
}
*flags = instruction->cpu_flags_read;
return ZYAN_STATUS_SUCCESS;
}
ZyanStatus ZydisGetAccessedFlagsWritten(const ZydisDecodedInstruction* instruction,
ZydisCPUFlags* flags)
{
if (!instruction || !flags)
{
return ZYAN_STATUS_INVALID_ARGUMENT;
}
*flags = instruction->cpu_flags_written;
return ZYAN_STATUS_SUCCESS;
}
/* ---------------------------------------------------------------------------------------------- */
/* Instruction segments */
/* ---------------------------------------------------------------------------------------------- */
ZyanStatus ZydisGetInstructionSegments(const ZydisDecodedInstruction* instruction,
ZydisInstructionSegments* segments)
{
if (!instruction || !segments)
{
return ZYAN_STATUS_INVALID_ARGUMENT;
}
ZYAN_MEMSET(segments, 0, sizeof(*segments));
// Legacy prefixes and `REX`
if (instruction->raw.prefix_count)
{
const ZyanU8 rex_offset = (instruction->attributes & ZYDIS_ATTRIB_HAS_REX) ? 1 : 0;
if (!rex_offset || (instruction->raw.prefix_count > 1))
{
segments->segments[segments->count ].type = ZYDIS_INSTR_SEGMENT_PREFIXES;
segments->segments[segments->count ].offset = 0;
segments->segments[segments->count++].size =
instruction->raw.prefix_count - rex_offset;
}
if (rex_offset)
{
segments->segments[segments->count ].type = ZYDIS_INSTR_SEGMENT_REX;
segments->segments[segments->count ].offset =
instruction->raw.prefix_count - rex_offset;
segments->segments[segments->count++].size = 1;
}
}
// Encoding prefixes
ZydisInstructionSegment segment_type = ZYDIS_INSTR_SEGMENT_NONE;
ZyanU8 segment_offset = 0;
ZyanU8 segment_size = 0;
switch (instruction->encoding)
{
case ZYDIS_INSTRUCTION_ENCODING_XOP:
segment_type = ZYDIS_INSTR_SEGMENT_XOP;
segment_offset = instruction->raw.xop.offset;
segment_size = 3;
break;
case ZYDIS_INSTRUCTION_ENCODING_VEX:
segment_type = ZYDIS_INSTR_SEGMENT_VEX;
segment_offset = instruction->raw.vex.offset;
segment_size = instruction->raw.vex.size;
break;
case ZYDIS_INSTRUCTION_ENCODING_EVEX:
segment_type = ZYDIS_INSTR_SEGMENT_EVEX;
segment_offset = instruction->raw.evex.offset;
segment_size = 4;
break;
case ZYDIS_INSTRUCTION_ENCODING_MVEX:
segment_type = ZYDIS_INSTR_SEGMENT_MVEX;
segment_offset = instruction->raw.mvex.offset;
segment_size = 4;
break;
default:
break;
}
if (segment_type)
{
segments->segments[segments->count ].type = segment_type;
segments->segments[segments->count ].offset = segment_offset;
segments->segments[segments->count++].size = segment_size;
}
// Opcode
segment_size = 1;
if ((instruction->encoding == ZYDIS_INSTRUCTION_ENCODING_LEGACY) ||
(instruction->encoding == ZYDIS_INSTRUCTION_ENCODING_3DNOW))
{
switch (instruction->opcode_map)
{
case ZYDIS_OPCODE_MAP_DEFAULT:
break;
case ZYDIS_OPCODE_MAP_0F:
ZYAN_FALLTHROUGH;
case ZYDIS_OPCODE_MAP_0F0F:
segment_size = 2;
break;
case ZYDIS_OPCODE_MAP_0F38:
ZYAN_FALLTHROUGH;
case ZYDIS_OPCODE_MAP_0F3A:
segment_size = 3;
break;
default:
ZYAN_UNREACHABLE;
}
}
segments->segments[segments->count ].type = ZYDIS_INSTR_SEGMENT_OPCODE;
if (segments->count)
{
segments->segments[segments->count].offset =
segments->segments[segments->count - 1].offset +
segments->segments[segments->count - 1].size;
} else
{
segments->segments[segments->count].offset = 0;
}
segments->segments[segments->count++].size = segment_size;
// ModRM
if (instruction->attributes & ZYDIS_ATTRIB_HAS_MODRM)
{
segments->segments[segments->count ].type = ZYDIS_INSTR_SEGMENT_MODRM;
segments->segments[segments->count ].offset = instruction->raw.modrm.offset;
segments->segments[segments->count++].size = 1;
}
// SIB
if (instruction->attributes & ZYDIS_ATTRIB_HAS_SIB)
{
segments->segments[segments->count ].type = ZYDIS_INSTR_SEGMENT_SIB;
segments->segments[segments->count ].offset = instruction->raw.sib.offset;
segments->segments[segments->count++].size = 1;
}
// Displacement
if (instruction->raw.disp.size)
{
segments->segments[segments->count ].type = ZYDIS_INSTR_SEGMENT_DISPLACEMENT;
segments->segments[segments->count ].offset = instruction->raw.disp.offset;
segments->segments[segments->count++].size = instruction->raw.disp.size / 8;
}
// Immediates
for (ZyanU8 i = 0; i < 2; ++i)
{
if (instruction->raw.imm[i].size)
{
segments->segments[segments->count ].type = ZYDIS_INSTR_SEGMENT_IMMEDIATE;
segments->segments[segments->count ].offset = instruction->raw.imm[i].offset;
segments->segments[segments->count++].size = instruction->raw.imm[i].size / 8;
}
}
if (instruction->encoding == ZYDIS_INSTRUCTION_ENCODING_3DNOW)
{
segments->segments[segments->count].type = ZYDIS_INSTR_SEGMENT_OPCODE;
segments->segments[segments->count].offset = instruction->length -1;
segments->segments[segments->count++].size = 1;
}
return ZYAN_STATUS_SUCCESS;
}
/* ---------------------------------------------------------------------------------------------- */
/* ============================================================================================== */
WTF_ALLOW_UNSAFE_BUFFER_USAGE_END
#endif /* ENABLE(ZYDIS) */
|