Skip to content
This repository has been archived by the owner on Jan 6, 2025. It is now read-only.

Commit

Permalink
add message argument to xabort()
Browse files Browse the repository at this point in the history
  • Loading branch information
GBuella committed May 21, 2017
1 parent 8a50595 commit 280abb2
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 31 deletions.
8 changes: 4 additions & 4 deletions src/disasm_wrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,15 @@ intercept_disasm_init(const unsigned char *begin, const unsigned char *end)
* The handle here must be passed to capstone each time it is used.
*/
if (cs_open(CS_ARCH_X86, CS_MODE_64, &context->handle) != CS_ERR_OK)
xabort();
xabort("cs_open");

/*
* Kindly ask capstone to return some details about the instruction.
* Without this, it only prints the instruction, and we would need
* to parse the resulting string.
*/
if (cs_option(context->handle, CS_OPT_DETAIL, CS_OPT_ON) != 0)
xabort();
xabort("cs_option - CS_OPT_DETAIL");

/*
* Overriding the printing routine used by capstone,
Expand All @@ -112,10 +112,10 @@ intercept_disasm_init(const unsigned char *begin, const unsigned char *end)
.realloc = realloc,
.vsnprintf = nop_vsnprintf};
if (cs_option(context->handle, CS_OPT_MEM, (size_t)&x) != 0)
xabort();
xabort("cs_option - CS_OPT_MEM");

if ((context->insn = cs_malloc(context->handle)) == NULL)
xabort();
xabort("cs_malloc");

return context;
}
Expand Down
8 changes: 5 additions & 3 deletions src/intercept.c
Original file line number Diff line number Diff line change
Expand Up @@ -442,11 +442,13 @@ log_header(void)
* by libc.
*/
void
xabort(void)
xabort(const char *msg)
{
static const char msg[] = "libsyscall_intercept error\n";
static const char main_msg[] = " libsyscall_intercept error\n";

syscall_no_intercept(SYS_write, 2, msg, sizeof(msg));
if (msg != NULL)
syscall_no_intercept(SYS_write, 2, msg, strlen(msg));
syscall_no_intercept(SYS_write, 2, main_msg, sizeof(main_msg));
syscall_no_intercept(SYS_exit_group, 1);

__builtin_trap();
Expand Down
2 changes: 1 addition & 1 deletion src/intercept.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ void intercept_patch_with_postfix(unsigned char *syscall_addr,

#define INTERCEPTOR_EXIT_CODE 111

__attribute__((noreturn)) void xabort(void);
__attribute__((noreturn)) void xabort(const char *);

struct range {
unsigned char *address;
Expand Down
17 changes: 10 additions & 7 deletions src/intercept_desc.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,11 @@ open_orig_file(const struct intercept_desc *desc)

fd = syscall_no_intercept(SYS_open, desc->path, O_RDONLY);

if (fd < 0)
xabort();
if (fd < 0) {
syscall_no_intercept(SYS_write, 2,
desc->path, strlen(desc->path));
xabort(" open_orig_file");
}

return fd;
}
Expand Down Expand Up @@ -143,7 +146,7 @@ find_sections(struct intercept_desc *desc, long fd)
}

if (!text_section_found)
xabort();
xabort("text section not found");
}

/*
Expand Down Expand Up @@ -715,14 +718,14 @@ allocate_trampoline_table(struct intercept_desc *desc)
size = 64 * 0x1000; /* XXX: don't just guess */

if ((maps = fopen("/proc/self/maps", "r")) == NULL)
xabort();
xabort("fopen /proc/self/maps");

while ((fgets(line, sizeof(line), maps)) != NULL) {
unsigned char *start;
unsigned char *end;

if (sscanf(line, "%p-%p", (void **)&start, (void **)&end) != 2)
xabort();
xabort("sscanf from /proc/self/maps");

/*
* Let's see if an existing mapping overlaps
Expand All @@ -744,7 +747,7 @@ allocate_trampoline_table(struct intercept_desc *desc)

if (guess + size >= desc->text_start + INT32_MAX) {
/* Too far away */
xabort();
xabort("unable to find place for trampoline table");
}
}

Expand All @@ -756,7 +759,7 @@ allocate_trampoline_table(struct intercept_desc *desc)
-1, 0);

if (desc->trampoline_table == MAP_FAILED)
xabort();
xabort("unable to allocate space for trampoline table");

desc->trampoline_table_size = size;

Expand Down
12 changes: 6 additions & 6 deletions src/intercept_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ xmmap_anon(size_t size)
MAP_PRIVATE | MAP_ANON, -1, 0);

if (addr == MAP_FAILED)
xabort();
xabort("xmmap_anon");

return addr;
}
Expand All @@ -71,7 +71,7 @@ xmremap(void *addr, size_t old, size_t new)
old, new, MREMAP_MAYMOVE);

if (addr == MAP_FAILED)
xabort();
xabort("xmremap");

return addr;
}
Expand All @@ -80,7 +80,7 @@ void
xmunmap(void *addr, size_t len)
{
if (syscall_no_intercept(SYS_munmap, addr, len) != 0)
xabort();
xabort("xmunmap");
}

long
Expand All @@ -89,7 +89,7 @@ xlseek(long fd, unsigned long off, int whence)
long result = syscall_no_intercept(SYS_lseek, fd, off, whence);

if (result < 0)
xabort();
xabort("xlseek");

return result;
}
Expand All @@ -99,7 +99,7 @@ xread(long fd, void *buffer, size_t size)
{
if (syscall_no_intercept(SYS_read, fd,
(long)buffer, (long)size) != (long)size)
xabort();
xabort("xread");
}

/*
Expand Down Expand Up @@ -133,7 +133,7 @@ intercept_setup_log(const char *path_base, const char *trunc)
log_fd = syscall_no_intercept(SYS_open, path, flags, 0700);

if (log_fd < 0)
xabort();
xabort("setup_log");
}

/*
Expand Down
22 changes: 12 additions & 10 deletions src/patcher.c
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ create_jump(unsigned char opcode, unsigned char *from, void *to)
ptrdiff_t delta = ((unsigned char *)to) - (from + JUMP_INS_SIZE);

if (delta > ((ptrdiff_t)INT32_MAX) || delta < ((ptrdiff_t)INT32_MIN))
xabort();
xabort("create_jump distance check");

int32_t delta32 = (int32_t)delta;
unsigned char *d = (unsigned char *)&delta32;
Expand Down Expand Up @@ -181,7 +181,7 @@ check_trampoline_usage(const struct intercept_desc *desc)
size_t used = (size_t)(desc->next_trampoline - desc->trampoline_table);

if (used + TRAMPOLINE_SIZE >= desc->trampoline_table_size)
xabort();
xabort("trampoline space not enough");
}

/*
Expand Down Expand Up @@ -490,7 +490,8 @@ create_patch_wrappers(struct intercept_desc *desc)
patch->syscall_offset);

intercept_log(buffer, (size_t)l);
xabort();
xabort("not enough space for patching"
" around syscal");
}
}

Expand Down Expand Up @@ -719,7 +720,8 @@ create_wrapper(struct patch_desc *patch,
}

if (patch->syscall_offset > UINT32_MAX)
xabort(); /* libc larger than 2 gigabytes? wow */
xabort("patch->syscall_offset > UINT32_MAX");
/* libc larger than 2 gigabytes? wow */

/* the instruction pushing the syscall's address to the stack */
create_push_imm(begin + o_push_origin, (uint32_t)patch->syscall_offset);
Expand Down Expand Up @@ -790,7 +792,7 @@ create_short_jump(unsigned char *from, unsigned char *to)
ptrdiff_t d = to - (from + 2);

if (d < - 128 || d > 127)
xabort();
xabort("create_short_jump distance check");

from[0] = SHORT_JMP_OPCODE;
from[1] = (unsigned char)((char)d);
Expand Down Expand Up @@ -824,14 +826,14 @@ activate_patches(struct intercept_desc *desc)

if (syscall_no_intercept(SYS_mprotect, first_page, size,
PROT_READ | PROT_WRITE | PROT_EXEC) != 0)
xabort();
xabort("mprotect PROT_READ | PROT_WRITE | PROT_EXEC");

for (unsigned i = 0; i < desc->count; ++i) {
const struct patch_desc *patch = desc->items + i;

if (patch->dst_jmp_patch < desc->text_start ||
patch->dst_jmp_patch > desc->text_end)
xabort();
xabort("dst_jmp_patch outside text");

/*
* The dst_jmp_patch pointer contains the address where
Expand Down Expand Up @@ -902,7 +904,7 @@ activate_patches(struct intercept_desc *desc)

if (syscall_no_intercept(SYS_mprotect, first_page, size,
PROT_READ | PROT_EXEC) != 0)
xabort();
xabort("mprotect PROT_READ | PROT_EXEC");
}

/*
Expand All @@ -920,7 +922,7 @@ next_asm_wrapper_space(void)
unsigned char *result;

if (next + tmpl_size + PAGE_SIZE > sizeof(asm_wrapper_space))
xabort();
xabort("not enough space in asm_wrapper_space");

result = asm_wrapper_space + next;

Expand All @@ -943,5 +945,5 @@ mprotect_asm_wrappers(void)
round_down_address(asm_wrapper_space + PAGE_SIZE),
sizeof(asm_wrapper_space) - PAGE_SIZE,
PROT_READ | PROT_EXEC) != 0)
xabort();
xabort("mprotect_asm_wrappers PROT_READ | PROT_EXEC");
}

0 comments on commit 280abb2

Please sign in to comment.