Skip to content

Commit

Permalink
objects: Improve errors
Browse files Browse the repository at this point in the history
  • Loading branch information
dd86k committed Mar 19, 2024
1 parent 89e64c3 commit bec84f4
Show file tree
Hide file tree
Showing 8 changed files with 485 additions and 292 deletions.
42 changes: 26 additions & 16 deletions src/adbg/error.d
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import adbg.include.capstone : csh, cs_errno, cs_strerror;
// adbg_ensure_params(lvalue, "name")
// - returns string if null found
// - automatically set error code
// adbg_errorn(AdbgError)
// - returns null

extern (C):

Expand All @@ -37,30 +39,35 @@ enum AdbgError {
// 0-99: Generic
//
success = 0,
invalidArgument = 1,
emptyArgument = 2,
uninitiated = 4, // Only when user value is important like for fopen
invalidOption = 5,
invalidOptionValue = 6,
invalidArgument = 1, /// Argument is null or zero
nullArgument = invalidArgument, // Old alias for invalidArgument
emptyArgument = 2, /// Argument contains an empty dataset
uninitiated = 4, /// Instance was not initiated
invalidOption = 5, /// Invalid option
invalidValue = 6, /// Invalid value for option
invalidOptionValue = invalidValue,
offsetBounds = 7, /// File offset is outside of file size
indexBounds = 8, /// Index is outside of bounds of list
unavailable = 9, /// Feature or item is unavailable
unfindable = 10, /// Item cannot be found in list
//
// 100-199: Debugger
//
debuggerUnattached = 100,
debuggerUnpaused = 101,
debuggerInvalidAction = 102, /// Wrong action from creation method.
debuggerPresent = 103, /// Debugger already present in remote process
debuggerUnattached = 100,
debuggerUnpaused = 101,
debuggerInvalidAction = 102, /// Wrong action from creation method.
debuggerPresent = 103, /// Debugger already present in remote process
// Old meanings
notAttached = debuggerUnattached, /// Old value for debuggerUnattached
notPaused = debuggerUnpaused, /// Old value for debuggerUnpaused
invalidAction = debuggerInvalidAction, ///
//
// 200-299: Disasembler
//
disasmUnsupportedMachine = 202,
disasmIllegalInstruction = 220,
disasmEndOfData = 221,
disasmOpcodeLimit = 221,
disasmUnsupportedMachine = 202,
disasmIllegalInstruction = 220,
disasmEndOfData = 221,
disasmOpcodeLimit = 221,
// Old meanings
unsupportedPlatform = disasmUnsupportedMachine,
illegalInstruction = disasmIllegalInstruction,
Expand All @@ -80,7 +87,7 @@ enum AdbgError {
objectInvalidEndian = 313,
objectInvalidType = 314,
objectInvalidABI = 315,
objectOutsideBounds = 320,
objectOutsideBounds = offsetBounds,
// Old meanings
unknownObjFormat = objectUnknownFormat,
unsupportedObjFormat = objectUnsupportedFormat,
Expand Down Expand Up @@ -130,7 +137,7 @@ private __gshared adbg_error_t error;

//TODO: Strongly consider string, provides .ptr and .length
private struct adbg_error_msg_t {
uint code;
int code;
const(char) *msg;
}
private immutable const(char) *defaultMsg = "Unknown error occured.";
Expand All @@ -143,6 +150,10 @@ private immutable adbg_error_msg_t[] errors_msg = [
{ AdbgError.uninitiated, "Object or structure is uninitiated." },
{ AdbgError.invalidOption, "Option unknown." },
{ AdbgError.invalidOptionValue, "Option received invalid value." },
{ AdbgError.offsetBounds, "File offset outside file size." },
{ AdbgError.indexBounds, "Index outside of list." },
{ AdbgError.unavailable, "Feature or item is unavailable." },
{ AdbgError.unfindable, "Index outside of list." },
//
// Debugger
//
Expand Down Expand Up @@ -171,7 +182,6 @@ private immutable adbg_error_msg_t[] errors_msg = [
{ AdbgError.objectInvalidEndian, "Invalid endianess value for object." },
{ AdbgError.objectInvalidType, "Invalid object type." },
{ AdbgError.objectInvalidABI, "Invalid ABI value for object." },
{ AdbgError.objectOutsideBounds, "Access outside file or module bounds." },
//
// Symbols
//
Expand Down
17 changes: 13 additions & 4 deletions src/adbg/object/format/ar.d
Original file line number Diff line number Diff line change
Expand Up @@ -106,17 +106,25 @@ int adbg_object_ar_load(adbg_object_t *o) {
return 0;
}

//TODO: Can this be transformed into functions with _start & _next?
ar_member_header* adbg_object_ar_header(adbg_object_t *o, size_t index) {
if (o == null)
if (o == null) {
adbg_oops(AdbgError.invalidArgument);
return null;
}

version (Trace) trace("index=%zu", index);

ar_member_header *p = cast(ar_member_header*)(o.buffer + ar_file_header.sizeof);
void *max = o.buffer + 0x8000_0000; // 2 GiB limit
for (size_t i; p < max; ++i) {
if (i == index)
return p.EndMarker == AR_EOL ? p : null;
if (i == index) {
if (p.EndMarker != AR_EOL) {
adbg_oops(AdbgError.offsetBounds);
return null;
}
return p;
}

// Adjust pointer
size_t offset = atoi(p.Size.ptr) + ar_member_header.sizeof;
Expand All @@ -130,6 +138,7 @@ ar_member_header* adbg_object_ar_header(adbg_object_t *o, size_t index) {
}
}

adbg_oops(AdbgError.unfindable);
return null;
}

Expand Down Expand Up @@ -158,7 +167,7 @@ void* adbg_object_ar_data(adbg_object_t *o, ar_member_header *mhdr) {
return null;
}
if (adbg_object_outboundpl(o, p, size)) {
adbg_oops(AdbgError.objectOutsideBounds);
adbg_oops(AdbgError.offsetBounds);
return null;
}
return p;
Expand Down
73 changes: 58 additions & 15 deletions src/adbg/object/format/elf.d
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ enum ELF_SHT_HIPROC = 0x7fffffff; /// Processor specific
enum ELF_SHT_LOUSER = 0x80000000; /// Application specific
enum ELF_SHT_HIUSER = 0xffffffff; /// Application specific
/// This section contains unwind function table entries for stack unwinding
enum ELF_SHT_X86_64_UNWIND = 0x70000001;
enum ELF_SHT_X86_64_UNWIND = 0x70000001; // ELF_SHT_LOPROC + 1?

// Section flags

Expand Down Expand Up @@ -701,6 +701,7 @@ struct Elf64_Chdr {


//TODO: Move them to some include folder
// There's adbg.include.linux.user

struct elf_prstatus32_timeval {
uint tv_sec;
Expand Down Expand Up @@ -1123,15 +1124,27 @@ int adbg_object_elf_load(adbg_object_t *o) {
}

Elf32_Ehdr* adbg_object_elf_ehdr32(adbg_object_t *o) {
if (o == null) return null;
if (o == null) {
adbg_oops(AdbgError.invalidArgument);
return null;
}
// Return as-is, already swapped
return o.i.elf32.ehdr;
}

Elf32_Phdr* adbg_object_elf_phdr32(adbg_object_t *o, size_t index) {
if (o == null) return null;
if (o.i.elf32.phdr == null) return null;
if (index >= o.i.elf32.ehdr.e_phnum) return null;
if (o == null) {
adbg_oops(AdbgError.invalidArgument);
return null;
}
if (o.i.elf32.phdr == null) {
adbg_oops(AdbgError.unavailable);
return null;
}
if (index >= o.i.elf32.ehdr.e_phnum) {
adbg_oops(AdbgError.indexBounds);
return null;
}

Elf32_Phdr *phdr = &o.i.elf32.phdr[index];
if (o.p.reversed && o.i.elf32.reversed_phdr[index] == false) {
Expand All @@ -1149,9 +1162,18 @@ Elf32_Phdr* adbg_object_elf_phdr32(adbg_object_t *o, size_t index) {
}

Elf32_Shdr* adbg_object_elf_shdr32(adbg_object_t *o, size_t index) {
if (o == null) return null;
if (o.i.elf32.shdr == null) return null;
if (index >= o.i.elf32.ehdr.e_shnum) return null;
if (o == null) {
adbg_oops(AdbgError.invalidArgument);
return null;
}
if (o.i.elf32.shdr == null) {
adbg_oops(AdbgError.unavailable);
return null;
}
if (index >= o.i.elf32.ehdr.e_shnum) {
adbg_oops(AdbgError.indexBounds);
return null;
}

Elf32_Shdr *shdr = &o.i.elf32.shdr[index];
if (o.p.reversed && o.i.elf32.reversed_phdr[index] == false) {
Expand All @@ -1171,15 +1193,27 @@ Elf32_Shdr* adbg_object_elf_shdr32(adbg_object_t *o, size_t index) {
}

Elf64_Ehdr* adbg_object_elf_ehdr64(adbg_object_t *o) {
if (o == null) return null;
if (o == null) {
adbg_oops(AdbgError.invalidArgument);
return null;
}
// Return as-is, already swapped
return o.i.elf64.ehdr;
}

Elf64_Phdr* adbg_object_elf_phdr64(adbg_object_t *o, size_t index) {
if (o == null) return null;
if (o.i.elf64.phdr == null) return null;
if (index >= o.i.elf64.ehdr.e_phnum) return null;
if (o == null) {
adbg_oops(AdbgError.invalidArgument);
return null;
}
if (o.i.elf64.phdr == null) {
adbg_oops(AdbgError.unavailable);
return null;
}
if (index >= o.i.elf64.ehdr.e_phnum) {
adbg_oops(AdbgError.indexBounds);
return null;
}

Elf64_Phdr *phdr = &o.i.elf64.phdr[index];
if (o.p.reversed && o.i.elf64.reversed_phdr[index] == false) {
Expand All @@ -1197,9 +1231,18 @@ Elf64_Phdr* adbg_object_elf_phdr64(adbg_object_t *o, size_t index) {
}

Elf64_Shdr* adbg_object_elf_shdr64(adbg_object_t *o, size_t index) {
if (o == null) return null;
if (o.i.elf64.shdr == null) return null;
if (index >= o.i.elf64.ehdr.e_shnum) return null;
if (o == null) {
adbg_oops(AdbgError.invalidArgument);
return null;
}
if (o.i.elf64.shdr == null) {
adbg_oops(AdbgError.unavailable);
return null;
}
if (index >= o.i.elf64.ehdr.e_shnum) {
adbg_oops(AdbgError.indexBounds);
return null;
}

Elf64_Shdr *shdr = &o.i.elf64.shdr[index];
if (o.p.reversed && o.i.elf64.reversed_phdr[index] == false) {
Expand Down
50 changes: 25 additions & 25 deletions src/adbg/object/format/lx.d
Original file line number Diff line number Diff line change
Expand Up @@ -267,58 +267,58 @@ enum {
}

struct lx_flat_bundle_prefix {
ubyte b32_cnt;
ubyte b32_type;
ushort b32_obj;
ubyte b32_cnt;
ubyte b32_type;
ushort b32_obj;
}

struct flat_null_prefix {
ubyte b32_cnt;
ubyte b32_type;
ubyte b32_cnt;
ubyte b32_type;
}

/* values for the b32_type field */
alias bundle_types = int;
enum {
FLT_BNDL_EMPTY = 0,
FLT_BNDL_ENTRY16,
FLT_BNDL_GATE16,
FLT_BNDL_ENTRY32,
FLT_BNDL_ENTRYFWD
FLT_BNDL_EMPTY = 0,
FLT_BNDL_ENTRY16,
FLT_BNDL_GATE16,
FLT_BNDL_ENTRY32,
FLT_BNDL_ENTRYFWD
}

struct lx_flat_bundle_entry32 {
ubyte e32_flags; /* flag bits are same as in OS/2 1.x */
uint e32_offset;
ubyte e32_flags; /* flag bits are same as in OS/2 1.x */
uint e32_offset;
}

struct lx_flat_bundle_gate16 {
ubyte e32_flags; /* flag bits are same as in OS/2 1.x */
ushort offset;
ushort callgate;
ubyte e32_flags; /* flag bits are same as in OS/2 1.x */
ushort offset;
ushort callgate;
}

/*
* other, unused bundle types are:
*/

struct lx_flat_bundle_entry16 {
ubyte e32_flags; /* flag bits are same as in OS/2 1.x */
ushort e32_offset;
ubyte e32_flags; /* flag bits are same as in OS/2 1.x */
ushort e32_offset;
}

struct lx_flat_bundle_entryfwd {
ubyte e32_flags; /* flag bits are same as in OS/2 1.x */
ushort modord;
uint value;
ubyte e32_flags; /* flag bits are same as in OS/2 1.x */
ushort modord;
uint value;
}

struct lx_flat_res_table {
ushort type_id;
ushort name_id;
uint res_size;
ushort object;
uint offset;
ushort type_id;
ushort name_id;
uint res_size;
ushort object;
uint offset;
}

/* fixup record source flags */
Expand Down
Loading

0 comments on commit bec84f4

Please sign in to comment.