From 7198c7b9f01bbd5e8f8528440d290569e588b260 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Tue, 2 Jun 2020 14:44:08 +0200 Subject: Some cleanup and preparation for support for multiple architectures (both host and client) and OSes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- memory.c | 39 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) (limited to 'memory.c') diff --git a/memory.c b/memory.c index c721268..6262c61 100644 --- a/memory.c +++ b/memory.c @@ -5,6 +5,9 @@ char * get_string(pid_t pid, unsigned long int addr, size_t *lenp, const char **errorp) { +#if defined(__x86_64__) && defined(__IPL32__) +# error "x32 is not supported, would not be able to read memory from 64-bit applications with current method" +#endif struct iovec inv, outv; size_t off = 0, size = 0, page_off, read_size; char *out = NULL, *in = (char *)addr, *p; @@ -39,14 +42,21 @@ int get_struct(pid_t pid, unsigned long int addr, void *out, size_t size, const char **errorp) { struct iovec inv, outv; + if (!addr) { + *errorp = "NULL"; + return -1; + } *errorp = NULL; +#if defined(__x86_64__) && defined(__IPL32__) +# error "x32 is not supported, would not be able to read memory from 64-bit applications with current method" +#endif inv.iov_base = (void *)addr; inv.iov_len = size; outv.iov_base = out; outv.iov_len = size; if (process_vm_readv(pid, &outv, 1, &inv, 1, 0) == (ssize_t)size) return 0; - *errorp = errno == EFAULT ? "" : ""; + *errorp = errno == EFAULT ? "" : ""; return -1; } @@ -65,7 +75,6 @@ get_memory(pid_t pid, unsigned long int addr, size_t n, const char **errorp) } - static void add_char(char **strp, size_t *sizep, size_t *lenp, char c) { @@ -188,3 +197,29 @@ escape_memory(char *str, size_t m) free(str); return ret; } + + +char * +get_escaped_string(pid_t pid, unsigned long int addr, size_t *lenp, const char **errorp) +{ + char *r; + if (!addr) { + *errorp = "NULL"; + return NULL; + } + r = get_string(pid, addr, lenp, errorp); + return escape_memory(r, *lenp); +} + + +char * +get_escaped_memory(pid_t pid, unsigned long int addr, size_t n, const char **errorp) +{ + char *r; + if (!addr) { + *errorp = "NULL"; + return NULL; + } + r = get_memory(pid, addr, n, errorp); + return escape_memory(r, n); +} -- cgit v1.2.3-70-g09d2