2022-11-20 20:52:09 +00:00
|
|
|
/***************************************************************************************************
|
2021-05-25 21:23:39 +01:00
|
|
|
|
|
|
|
Zyan Disassembler Library (Zydis)
|
|
|
|
|
|
|
|
Original Author : Joel Hoener
|
|
|
|
|
|
|
|
* 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.
|
|
|
|
|
|
|
|
***************************************************************************************************/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
*
|
2022-11-20 20:52:09 +00:00
|
|
|
* This file implements fuzz target for decoder, formatter and various utility functions.
|
2021-05-25 21:23:39 +01:00
|
|
|
*/
|
|
|
|
|
2022-11-20 20:52:09 +00:00
|
|
|
#include "ZydisFuzzShared.h"
|
2021-05-25 21:23:39 +01:00
|
|
|
|
|
|
|
/* ============================================================================================== */
|
|
|
|
/* Enums and types */
|
|
|
|
/* ============================================================================================== */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Main fuzzer control block data structure.
|
|
|
|
*/
|
|
|
|
typedef struct ZydisFuzzControlBlock_
|
|
|
|
{
|
|
|
|
ZydisMachineMode machine_mode;
|
2022-11-20 20:52:09 +00:00
|
|
|
ZydisStackWidth stack_width;
|
2021-05-25 21:23:39 +01:00
|
|
|
ZyanBool decoder_mode[ZYDIS_DECODER_MODE_MAX_VALUE + 1];
|
|
|
|
ZydisFormatterStyle formatter_style;
|
|
|
|
ZyanU64 u64; // u64 used for all kind of non-overlapping purposes
|
|
|
|
ZyanUPointer formatter_properties[ZYDIS_FORMATTER_PROP_MAX_VALUE + 1];
|
|
|
|
char string[16];
|
|
|
|
ZyanU16 formatter_max_len;
|
|
|
|
} ZydisFuzzControlBlock;
|
|
|
|
|
|
|
|
/* ============================================================================================== */
|
2022-11-20 20:52:09 +00:00
|
|
|
/* Fuzz target */
|
2021-05-25 21:23:39 +01:00
|
|
|
/* ============================================================================================== */
|
|
|
|
|
|
|
|
// We disable enum sanitization here because we actually want Zydis to be tested with
|
|
|
|
// possibly invalid enum values in mind, thus need to be able to create them here.
|
|
|
|
ZYAN_NO_SANITIZE("enum")
|
2022-11-20 20:52:09 +00:00
|
|
|
int ZydisFuzzTarget(ZydisStreamRead read_fn, void* stream_ctx)
|
2021-05-25 21:23:39 +01:00
|
|
|
{
|
|
|
|
ZydisFuzzControlBlock control_block;
|
|
|
|
if (read_fn(
|
|
|
|
stream_ctx, (ZyanU8*)&control_block, sizeof(control_block)) != sizeof(control_block))
|
|
|
|
{
|
|
|
|
ZYDIS_MAYBE_FPUTS("Not enough bytes to fuzz\n", ZYAN_STDERR);
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
|
|
|
control_block.string[ZYAN_ARRAY_LENGTH(control_block.string) - 1] = 0;
|
|
|
|
|
|
|
|
ZydisDecoder decoder;
|
|
|
|
if (!ZYAN_SUCCESS(ZydisDecoderInit(&decoder, control_block.machine_mode,
|
2022-11-20 20:52:09 +00:00
|
|
|
control_block.stack_width)))
|
2021-05-25 21:23:39 +01:00
|
|
|
{
|
|
|
|
ZYDIS_MAYBE_FPUTS("Failed to initialize decoder\n", ZYAN_STDERR);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
for (int mode = 0; mode <= ZYDIS_DECODER_MODE_MAX_VALUE; ++mode)
|
|
|
|
{
|
|
|
|
if (!ZYAN_SUCCESS(ZydisDecoderEnableMode(&decoder, (ZydisDecoderMode)mode,
|
|
|
|
control_block.decoder_mode[mode] ? 1 : 0)))
|
|
|
|
{
|
|
|
|
ZYDIS_MAYBE_FPUTS("Failed to adjust decoder-mode\n", ZYAN_STDERR);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ZydisFormatter formatter;
|
|
|
|
if (!ZYAN_SUCCESS(ZydisFormatterInit(&formatter, control_block.formatter_style)))
|
|
|
|
{
|
|
|
|
ZYDIS_MAYBE_FPUTS("Failed to initialize formatter\n", ZYAN_STDERR);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
for (int prop = 0; prop <= ZYDIS_FORMATTER_PROP_MAX_VALUE; ++prop)
|
|
|
|
{
|
|
|
|
switch (prop)
|
|
|
|
{
|
|
|
|
case ZYDIS_FORMATTER_PROP_DEC_PREFIX:
|
|
|
|
case ZYDIS_FORMATTER_PROP_DEC_SUFFIX:
|
|
|
|
case ZYDIS_FORMATTER_PROP_HEX_PREFIX:
|
|
|
|
case ZYDIS_FORMATTER_PROP_HEX_SUFFIX:
|
|
|
|
control_block.formatter_properties[prop] =
|
|
|
|
control_block.formatter_properties[prop] ? (ZyanUPointer)&control_block.string : 0;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!ZYAN_SUCCESS(ZydisFormatterSetProperty(&formatter, (ZydisFormatterProperty)prop,
|
|
|
|
control_block.formatter_properties[prop])))
|
|
|
|
{
|
|
|
|
ZYDIS_MAYBE_FPUTS("Failed to set formatter-attribute\n", ZYAN_STDERR);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ZyanU8 buffer[32];
|
|
|
|
ZyanUSize input_len = read_fn(stream_ctx, buffer, sizeof(buffer));
|
|
|
|
ZydisDecodedInstruction instruction;
|
2022-11-20 20:52:09 +00:00
|
|
|
ZydisDecodedOperand operands[ZYDIS_MAX_OPERAND_COUNT];
|
2021-05-25 21:23:39 +01:00
|
|
|
|
|
|
|
// Fuzz decoder.
|
2022-11-20 20:52:09 +00:00
|
|
|
ZyanStatus status = ZydisDecoderDecodeFull(&decoder, buffer, input_len, &instruction, operands);
|
2021-05-25 21:23:39 +01:00
|
|
|
if (!ZYAN_SUCCESS(status))
|
|
|
|
{
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2022-11-20 20:52:09 +00:00
|
|
|
ZydisValidateEnumRanges(&instruction, operands, instruction.operand_count);
|
|
|
|
|
2021-05-25 21:23:39 +01:00
|
|
|
// Fuzz formatter.
|
|
|
|
char format_buffer[256];
|
|
|
|
// Allow the control block to artificially restrict the buffer size.
|
|
|
|
ZyanUSize output_len = ZYAN_MIN(sizeof(format_buffer), control_block.formatter_max_len);
|
2022-11-20 20:52:09 +00:00
|
|
|
ZydisFormatterFormatInstruction(&formatter, &instruction, operands,
|
|
|
|
instruction.operand_count_visible, format_buffer, output_len, control_block.u64, NULL);
|
2021-05-25 21:23:39 +01:00
|
|
|
|
|
|
|
// Fuzz tokenizer.
|
|
|
|
const ZydisFormatterToken* token;
|
2022-11-20 20:52:09 +00:00
|
|
|
status = ZydisFormatterTokenizeInstruction(&formatter, &instruction, operands,
|
|
|
|
instruction.operand_count_visible, format_buffer, output_len, control_block.u64, &token,
|
|
|
|
NULL);
|
2021-05-25 21:23:39 +01:00
|
|
|
|
|
|
|
// Walk tokens.
|
|
|
|
while (ZYAN_SUCCESS(status))
|
|
|
|
{
|
|
|
|
ZydisTokenType type;
|
|
|
|
ZyanConstCharPointer value;
|
|
|
|
if (!ZYAN_SUCCESS(status = ZydisFormatterTokenGetValue(token, &type, &value)))
|
|
|
|
{
|
|
|
|
ZYDIS_MAYBE_FPUTS("Failed to get token value\n", ZYAN_STDERR);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
status = ZydisFormatterTokenNext(&token);
|
|
|
|
}
|
|
|
|
|
2022-11-20 20:52:09 +00:00
|
|
|
if (instruction.operand_count_visible > 0)
|
2021-05-25 21:23:39 +01:00
|
|
|
{
|
|
|
|
// Fuzz single operand formatting. We reuse rt-address for operand selection.
|
|
|
|
// It's casted to u8 because modulo is way cheaper on that.
|
2022-11-20 20:52:09 +00:00
|
|
|
ZyanU8 op_idx = (ZyanU8)control_block.u64 % instruction.operand_count_visible;
|
|
|
|
const ZydisDecodedOperand* op = &operands[op_idx];
|
|
|
|
|
|
|
|
ZydisFormatterFormatOperand(&formatter, &instruction, op, format_buffer, output_len,
|
|
|
|
control_block.u64, NULL);
|
2021-05-25 21:23:39 +01:00
|
|
|
|
|
|
|
// Fuzz single operand tokenization.
|
2022-11-20 20:52:09 +00:00
|
|
|
ZydisFormatterTokenizeOperand(&formatter, &instruction, op, format_buffer, output_len,
|
|
|
|
control_block.u64, &token, NULL);
|
2021-05-25 21:23:39 +01:00
|
|
|
|
|
|
|
// Address translation helper.
|
|
|
|
ZyanU64 abs_addr;
|
2022-11-20 20:52:09 +00:00
|
|
|
ZydisCalcAbsoluteAddress(&instruction, op, control_block.u64, &abs_addr);
|
2021-05-25 21:23:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Mnemonic helpers.
|
|
|
|
ZydisMnemonicGetString((ZydisMnemonic)control_block.u64);
|
|
|
|
ZydisMnemonicGetStringWrapped((ZydisMnemonic)control_block.u64);
|
|
|
|
|
|
|
|
// Instruction segment helper.
|
2022-11-20 20:52:09 +00:00
|
|
|
# ifndef ZYDIS_DISABLE_SEGMENT
|
2021-05-25 21:23:39 +01:00
|
|
|
ZydisInstructionSegments segments;
|
|
|
|
ZydisGetInstructionSegments(&instruction, &segments);
|
2022-11-20 20:52:09 +00:00
|
|
|
# endif
|
2021-05-25 21:23:39 +01:00
|
|
|
|
|
|
|
// Feature enable check helper.
|
|
|
|
ZydisIsFeatureEnabled((ZydisFeature)control_block.u64);
|
|
|
|
|
|
|
|
// Register helpers.
|
|
|
|
ZydisRegisterEncode((ZydisRegisterClass)(control_block.u64 >> 8), (ZyanU8)control_block.u64);
|
|
|
|
ZydisRegisterGetId((ZydisRegister)control_block.u64);
|
|
|
|
ZydisRegisterGetClass((ZydisRegister)control_block.u64);
|
|
|
|
ZydisRegisterGetWidth(control_block.machine_mode, (ZydisRegister)control_block.u64);
|
|
|
|
ZydisRegisterGetLargestEnclosing(control_block.machine_mode, (ZydisRegister)control_block.u64);
|
|
|
|
ZydisRegisterGetString((ZydisRegister)control_block.u64);
|
|
|
|
ZydisRegisterGetStringWrapped((ZydisRegister)control_block.u64);
|
|
|
|
ZydisRegisterClassGetWidth(control_block.machine_mode, (ZydisRegisterClass)control_block.u64);
|
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ============================================================================================== */
|