Add shm:// rzpipe test

This commit is contained in:
Anton Kochkov 2021-09-28 12:46:07 +08:00 committed by Anton Kochkov
parent 50f9be4fc8
commit 4080a08e6a
5 changed files with 172 additions and 18 deletions

View file

@ -23,6 +23,7 @@ tasks:
sed -i "/'file',/d" test/unit/meson.build
# Workaround to avoid running rz-pipe test since there is no python3 symlink but a python3.8 binary available.
rm -f test/db/cmd/cmd_pipe
rm -f test/db/archos/not-windows-any/cmd_pipe
meson --prefix=${HOME} build
ninja -C build
- install: |

View file

@ -23,8 +23,6 @@ typedef struct {
ut32 size;
} RzIOShm;
#define RzIOSHM_FD(x) (((RzIOShm *)(x))->fd)
#define SHMATSZ 0x9000; // 32*1024*1024; /* 32MB : XXX not used correctly? */
static int shm__write(RzIO *io, RzIODesc *fd, const ut8 *buf, int count) {
@ -62,6 +60,8 @@ static int shm__close(RzIODesc *fd) {
#else
if (shm->buf) {
ret = shmdt(((RzIOShm *)(fd->data))->buf);
} else {
ret = close(shm->fd);
}
#endif
free(shm->name);
@ -97,28 +97,48 @@ static inline int getshmfd(RzIOShm *shm) {
}
#endif
static RzIODesc *shm__open(RzIO *io, const char *pathname, int rw, int mode) {
if (strncmp(pathname, "shm://", 6)) {
static RzIODesc *shm__open(RzIO *io, const char *uri, int rw, int mode) {
if (strncmp(uri, "shm://", 6)) {
return NULL;
}
RzIOShm *shm = RZ_NEW0(RzIOShm);
if (!shm) {
return NULL;
}
const char *ptr = pathname + 6;
shm->name = strdup(ptr);
const char *name = strstr(uri, "://");
if (!name) {
free(shm);
return NULL;
}
name += 3;
// The shared memory size is an optional parameter
char *size = strstr(name, "/");
if (size) {
*size = 0;
size += 1;
}
shm->name = rz_str_newf("/%s", name);
#if HAVE_SHM_OPEN
shm->id = rz_str_hash(ptr);
shm->fd = shm_open(ptr, O_CREAT | (rw ? O_RDWR : O_RDONLY), 0644);
shm->id = rz_str_hash(name);
shm->fd = shm_open(shm->name, O_CREAT | (rw ? O_RDWR : O_RDONLY), 0644);
if (shm->fd == -1) {
RZ_LOG_ERROR("Cannot connect to shared memory \"%s\" (0x%08x)\n", shm->name, shm->id);
free(shm->name);
free(shm);
return NULL;
}
struct stat st;
fstat(shm->fd, &st);
shm->size = st.st_size;
ut64 given_size = 0;
// If the memory size is supplied - we use it,
// otherwise try to read it from the file descriptor itself
if (size && (given_size = rz_num_math(NULL, size))) {
shm->size = given_size;
} else {
struct stat st;
fstat(shm->fd, &st);
shm->size = st.st_size;
}
shm->buf = mmap(NULL, shm->size, (rw ? (PROT_READ | PROT_WRITE) : PROT_READ), MAP_SHARED, shm->fd, 0);
if (shm->buf == MAP_FAILED) {
RZ_LOG_ERROR("Cannot mmap shared memory \"%s\" (0x%08x)\n", shm->name, shm->id);
@ -141,14 +161,15 @@ static RzIODesc *shm__open(RzIO *io, const char *pathname, int rw, int mode) {
}
shm->size = SHMATSZ;
if (shm->fd == -1) {
eprintf("Cannot connect to shared memory (%d)\n", shm->id);
RZ_LOG_ERROR("Cannot connect to shared memory (%d)\n", shm->id);
free(shm->name);
free(shm);
return NULL;
}
#endif
eprintf("Connected to shared memory \"%s\" (0x%08x)\n", shm->name, shm->id);
return rz_io_desc_new(io, &rz_io_plugin_shm, pathname, rw, mode, shm);
RZ_LOG_INFO("Connected to shared memory \"%s\" (0x%08x) size 0x%x\n",
shm->name, shm->id, shm->size);
return rz_io_desc_new(io, &rz_io_plugin_shm, uri, rw, mode, shm);
}
RzIOPlugin rz_io_plugin_shm = {

View file

@ -0,0 +1,99 @@
NAME=rzpipe.py - shared memory (shm://)
FILE=bins/elf/_Exit (42)
CMDS=<<EOF
?q `env~?^ASAN=1$`
?+ env LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libasan.so.5
#!pipe python3 scripts/shared_memory.py
EOF
EXPECT=<<EOF
Copying bins/elf/_Exit (42)...
Copied bins/elf/_Exit (42) succesfully
-------------
Shared buffer size 0x360c
-------------
{'arch': 'x86', 'baddr': 134512640, 'binsz': 13073, 'bintype': 'elf', 'bits': 32, 'class': 'ELF32', 'compiler': 'GCC: (GNU) 9.2.1 20190827 (Red Hat 9.2.1-1)', 'endian': 'LE', 'intrp': '/lib/ld-linux.so.2', 'laddr': 0, 'lang': 'c++', 'machine': 'Intel 80386', 'maxopsz': 16, 'minopsz': 1, 'os': 'linux', 'pcalign': 0, 'relro': 'partial', 'rpath': 'NONE', 'subsys': 'linux', 'stripped': False, 'crypto': False, 'havecode': True, 'va': True, 'sanitiz': False, 'static': False, 'linenum': True, 'lsyms': True, 'canary': False, 'PIE': False, 'RELROCS': True, 'NX': True}
- offset - 0 1 2 3 4 5 6 7 8 9 A B C D E F 0123456789ABCDEF
0x08049020 5589 e583 ec08 83ec 0c6a 2ae8 e0ff ffff U........j*.....
0x08049020 1 16 entry0
0x08049010 1 6 sym.imp._Exit
;-- section..text:
;-- .text:
;-- _start():
/ entry0 ();
| 0x08049020 push ebp ; [10] -r-x section size 16 named .text
| 0x08049021 mov ebp, esp
| 0x08049023 sub esp, 8
| 0x08049026 sub esp, 0xc
| 0x08049029 push 0x2a ; '*' ; 42 ; int status
\ 0x0804902b call sym.imp._Exit ; void _Exit(int status)
Copying bins/pe/winver.exe...
Copied bins/pe/winver.exe succesfully
-------------
Shared buffer size 0x400
-------------
{'arch': 'x86', 'baddr': 4194304, 'binsz': 4294967295, 'bintype': 'pe', 'bits': 32, 'retguard': False, 'class': 'PE32', 'cmp.csum': '0x00000000', 'compiled': 'Thu Jan 1 00:00:00 1970 UTC', 'endian': 'LE', 'hdr.csum': '0x00000000', 'laddr': 0, 'lang': 'c', 'machine': 'i386', 'maxopsz': 16, 'minopsz': 1, 'os': 'windows', 'overlay': True, 'cc': 'cdecl', 'pcalign': 0, 'signed': False, 'subsys': 'Windows CUI', 'stripped': False, 'crypto': False, 'havecode': True, 'va': True, 'sanitiz': False, 'static': False, 'linenum': False, 'lsyms': False, 'canary': False, 'PIE': False, 'RELROCS': False, 'NX': False}
- offset - 0 1 2 3 4 5 6 7 8 9 A B C D E F 0123456789ABCDEF
0x00401000 6880 1040 00ff 15b4 1140 00ff 3590 1040 h..@.....@..5..@
0x00401000 1 59 entry0
0x004011b4 1 8 fcn.004011b4
0x004011bc 8 3653 -> 7171 fcn.004011bc
0x004011b0 1 17 fcn.004011b0
;-- section.sect_0:
/ entry0 ();
| 0x00401000 push 0x401080 ; [00] -rwx section size 4096 named sect_0
| 0x00401005 call dword [fcn.004011b4] ; 0x4011b4
| 0x0040100b push dword [0x401090]
| 0x00401011 push dword [0x40108c]
| 0x00401017 push dword [0x401088]
| 0x0040101d push dword [0x401084]
| 0x00401023 push 0x40103c ; " * a PE overriding OS values: OS Ver %i.%i.%i PlatformID %i\n\n" ; int32_t arg_73h
| 0x00401028 call dword [fcn.004011bc] ; 0x4011bc
| 0x0040102e add esp, 0x10
| 0x00401031 nop
| 0x00401032 push 0
| 0x00401034 call dword [fcn.004011b0] ; 0x4011b0
\ 0x0040103a int3
Copying bins/mach0/mach0_2-x86_64...
Copied bins/mach0/mach0_2-x86_64 succesfully
-------------
Shared buffer size 0x10e8
-------------
{'arch': 'x86', 'baddr': 4294967296, 'binsz': 4294967295, 'bintype': 'mach0', 'bits': 64, 'class': 'MACH064', 'compiler': '', 'endian': 'LE', 'intrp': '/usr/lib/dyld', 'laddr': 0, 'lang': 'c', 'machine': 'x86 64 all', 'maxopsz': 16, 'minopsz': 1, 'os': 'macos', 'pcalign': 0, 'subsys': 'darwin', 'stripped': False, 'crypto': False, 'havecode': True, 'va': True, 'sanitiz': False, 'static': False, 'linenum': False, 'lsyms': False, 'canary': False, 'PIE': True, 'RELROCS': False, 'NX': False}
- offset - 0 1 2 3 4 5 6 7 8 9 A B C D E F 0123456789ABCDEF
0x100000f70 5548 89e5 4883 ec10 c745 fc00 0000 0089 UH..H....E......
0x100000f70 1 36 entry0
0x100000f40 1 45 sym._foo
0x100000ef0 4 65 sym._bar
0x1000001d5 1 171 fcn.1000001d5
0x1000002e0 13 3088 -> 3110 fcn.1000002e0
;-- main:
;-- _main:
;-- func.100000f70:
/ entry0 (int64_t arg1, int64_t arg2);
| ; var int64_t var_10h @ rbp-0x10
| ; var int64_t var_8h @ rbp-0x8
| ; var int64_t var_4h @ rbp-0x4
| ; arg int64_t arg1 @ rdi
| ; arg int64_t arg2 @ rsi
| 0x100000f70 push rbp
| 0x100000f71 mov rbp, rsp
| 0x100000f74 sub rsp, 0x10
| 0x100000f78 mov dword [var_4h], 0
| 0x100000f7f mov dword [var_8h], edi ; arg1
| 0x100000f82 mov qword [var_10h], rsi ; arg2
| 0x100000f86 mov edi, dword [var_8h] ; int64_t arg1
| 0x100000f89 call sym._foo
| 0x100000f8e add rsp, 0x10
| 0x100000f92 pop rbp
\ 0x100000f93 ret
EOF
RUN

View file

@ -1,9 +1,9 @@
import rzpipe
r2 = rzpipe.open()
r2.cmd("aa")
rzp = rzpipe.open()
rzp.cmd("aa")
print("\nFunction names:")
for func in r2.cmdj("aflj"):
for func in rzp.cmdj("aflj"):
print(func["name"])
print("\nDisassembly of entry0:")
print(r2.cmd("pdf"))
print(rzp.cmd("pdf"))

33
test/scripts/shared_memory.py Executable file
View file

@ -0,0 +1,33 @@
#!/usr/bin/env python3
from multiprocessing import shared_memory
import rzpipe
FILENAMES = ["bins/elf/_Exit (42)", "bins/pe/winver.exe", "bins/mach0/mach0_2-x86_64"]
for fname in FILENAMES:
with open(fname, "rb") as f:
data = f.read()
data_size = len(data)
shm = shared_memory.SharedMemory(create=True, size=data_size)
print("Copying %s..." % fname)
shm.buf[:data_size] = data[:]
print("Copied %s succesfully" % fname)
print("-------------")
print("Shared buffer size 0x{0:x}".format(data_size))
print("-------------")
rzp = rzpipe.open("shm://{0:s}/{1:d}".format(shm.name, data_size))
rzp.cmd("e scr.color=0")
rzp.cmd("e scr.utf8=false")
rzp.cmd("e scr.interactive=false")
infoj = rzp.cmdj("ij")
print(infoj["bin"])
print(rzp.cmd("px 16"))
rzp.cmd("aaa")
print(rzp.cmd("afl"))
print(rzp.cmd("pdf @ entry0"))
rzp.quit()
shm.close()
shm.unlink()