Skip to content

Commit

Permalink
Improve error handling in Decoder
Browse files Browse the repository at this point in the history
  • Loading branch information
ZehMatt committed May 2, 2024
1 parent 98a83e7 commit 9fb0bb7
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 5 deletions.
1 change: 1 addition & 0 deletions include/zasm/core/errors.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ namespace zasm
// Decoder.
InvalidInstruction,
OutOfBounds,
InstructionTooLong,
// Encoder.
ImpossibleInstruction,
AddressOutOfRange,
Expand Down
4 changes: 4 additions & 0 deletions include/zasm/decoder/decoder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ namespace zasm

Result decode(const void* data, std::size_t len, std::uint64_t address) noexcept;

MachineMode getMode() const;

const Error& getLastError() const;

private:
ZydisDecoder _decoder{};
MachineMode _mode{};
Expand Down
41 changes: 36 additions & 5 deletions src/zasm/src/decoder/decoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,19 @@ namespace zasm
break;
}

if (status != ZYAN_STATUS_SUCCESS)
switch (status)
{
// TODO: Translate proper error.
_status = Error{ ErrorCode::NotInitialized };
case ZYAN_STATUS_SUCCESS:
break;
case ZYAN_STATUS_INVALID_ARGUMENT:
_status = Error{ ErrorCode::InvalidParameter };
break;
case ZYAN_STATUS_INVALID_OPERATION:
_status = Error{ ErrorCode::InvalidOperation };
break;
default:
_status = Error{ ErrorCode::NotInitialized };
break;
}
}

Expand Down Expand Up @@ -169,8 +178,20 @@ namespace zasm
ZyanStatus status = ZydisDecoderDecodeFull(&_decoder, data, len, &instr, instrOps.data());
if (status != ZYAN_STATUS_SUCCESS)
{
// TODO: Translate proper error.
return zasm::makeUnexpected(Error{ ErrorCode::InvalidOperation });
switch (status)
{
case ZYDIS_STATUS_NO_MORE_DATA:
_status = Error{ ErrorCode::OutOfBounds };
break;
case ZYDIS_STATUS_INSTRUCTION_TOO_LONG:
_status = Error{ ErrorCode::InstructionTooLong };
break;
default:
_status = Error{ ErrorCode::InvalidInstruction };
break;
}

return zasm::makeUnexpected(_status);
}

InstructionDetail::CPUFlags flags{};
Expand Down Expand Up @@ -207,4 +228,14 @@ namespace zasm
return res;
}

MachineMode Decoder::getMode() const
{
return _mode;
}

const Error& Decoder::getLastError() const
{
return _status;
}

} // namespace zasm

0 comments on commit 9fb0bb7

Please sign in to comment.