From 8a387d0ee7e1c0261317a42ee44a63a7f63f0022 Mon Sep 17 00:00:00 2001 From: Anton Kochkov Date: Tue, 4 Jun 2024 00:26:48 +0800 Subject: [PATCH] core/file: add API for the `oC` command --- librz/core/cfile.c | 43 +++++++++++++++++++++++++++++ librz/core/cmd/cmd_open.c | 33 +--------------------- librz/include/rz_core.h | 1 + test/integration/test_analysis_il.c | 5 ++-- 4 files changed, 48 insertions(+), 34 deletions(-) diff --git a/librz/core/cfile.c b/librz/core/cfile.c index eeb8a69faa4..485cacb2344 100644 --- a/librz/core/cfile.c +++ b/librz/core/cfile.c @@ -1268,6 +1268,49 @@ RZ_API RZ_BORROW RzCoreFile *rz_core_file_open(RZ_NONNULL RzCore *r, RZ_NONNULL return fh; } +/** + * Allocates the memory chunk of \p len size, copies the contents at the current offset, and opens it as a file. + * \param core RzCore instanced + * \param len Size of the chunk to be created and copied + * \param offset Offset from where to copy bytes + * \return true on success, false otherwise + */ +RZ_API bool rz_core_file_malloc_copy_chunk(RzCore *core, size_t len, ut64 offset) { + rz_return_val_if_fail(core && len); + + bool res = false; + ut8 *data = RZ_NEWS(ut8, len); + if (!data) { + return false; + } + if (!rz_io_read_at(core->io, offset, data, len)) { + RZ_LOG_ERROR("Cannot read %d bytes from offset 0x%" PFMT64x ".\n", len, offset); + goto err; + } + + char uri[100]; + rz_strf(uri, "malloc://%d", len); + RzCoreFile *cfile = rz_core_file_open(core, uri, RZ_PERM_RWX, 0); + if (!cfile) { + RZ_LOG_ERROR("Cannot open '%s'.\n", uri); + goto err; + } + + if (!rz_core_bin_load(core, uri, 0)) { + RZ_LOG_ERROR("Cannot load binary info of '%s'.\n", uri); + goto err; + } + + RzIODesc *desc = rz_io_desc_get(core->io, cfile->fd); + rz_warn_if_fail(desc); + rz_io_desc_write_at(desc, 0, data, len); + res = true; + +err: + free(data); + return res; +} + RZ_IPI void rz_core_file_io_desc_closed(RzCore *core, RzIODesc *desc) { // remove all references to the closed desc RzListIter *it; diff --git a/librz/core/cmd/cmd_open.c b/librz/core/cmd/cmd_open.c index d98f1cc6501..aa78614063f 100644 --- a/librz/core/cmd/cmd_open.c +++ b/librz/core/cmd/cmd_open.c @@ -877,38 +877,7 @@ RZ_IPI RzCmdStatus rz_open_malloc_handler(RzCore *core, int argc, const char **a RZ_LOG_ERROR("Invalid length %d.\n", len); return RZ_CMD_STATUS_ERROR; } - - RzCmdStatus res = RZ_CMD_STATUS_ERROR; - ut8 *data = RZ_NEWS(ut8, len); - if (!data) { - return RZ_CMD_STATUS_ERROR; - } - if (!rz_io_read_at(core->io, core->offset, data, len)) { - RZ_LOG_ERROR("Cannot read %d bytes from current offset.\n", len); - goto err; - } - - char uri[100]; - rz_strf(uri, "malloc://%d", len); - RzCoreFile *cfile = rz_core_file_open(core, uri, RZ_PERM_RWX, 0); - if (!cfile) { - RZ_LOG_ERROR("Cannot open '%s'.\n", uri); - goto err; - } - - if (!rz_core_bin_load(core, uri, 0)) { - RZ_LOG_ERROR("Cannot load binary info of '%s'.\n", uri); - goto err; - } - - RzIODesc *desc = rz_io_desc_get(core->io, cfile->fd); - rz_warn_if_fail(desc); - rz_io_desc_write_at(desc, 0, data, len); - res = RZ_CMD_STATUS_OK; - -err: - free(data); - return res; + return bool2status(rz_core_file_malloc_copy_chunk(core, len, core->offset)); } static RzCmdStatus open_nobin_file(RzCore *core, const char *uri, ut64 addr, int perms) { diff --git a/librz/include/rz_core.h b/librz/include/rz_core.h index 56a9754c4a7..9e4660069be 100644 --- a/librz/include/rz_core.h +++ b/librz/include/rz_core.h @@ -528,6 +528,7 @@ RZ_API int rz_core_setup_debugger(RzCore *r, const char *debugbackend, bool atta RZ_API bool rz_core_file_open_load(RZ_NONNULL RzCore *core, RZ_NONNULL const char *filepath, ut64 addr, int perms, bool write_mode); RZ_API RZ_BORROW RzCoreFile *rz_core_file_open(RZ_NONNULL RzCore *core, RZ_NONNULL const char *file, int flags, ut64 loadaddr); RZ_API RZ_BORROW RzCoreFile *rz_core_file_open_many(RZ_NONNULL RzCore *r, RZ_NULLABLE const char *file, int perm, ut64 loadaddr); +RZ_API bool rz_core_file_malloc_copy_chunk(RzCore *core, size_t len, ut64 offset); RZ_API RzCoreFile *rz_core_file_get_by_fd(RzCore *core, int fd); RZ_API void rz_core_file_close(RzCoreFile *fh); RZ_API bool rz_core_file_close_fd(RzCore *core, int fd); diff --git a/test/integration/test_analysis_il.c b/test/integration/test_analysis_il.c index 30a0495ea04..b728ebb6af0 100644 --- a/test/integration/test_analysis_il.c +++ b/test/integration/test_analysis_il.c @@ -86,9 +86,10 @@ static bool test_analysis_il() { rz_core_file_open_load(core, "malloc://0x1000", 0x40000, RZ_PERM_R, false); rz_core_file_open_load(core, "malloc://0x10", 0x50000, RZ_PERM_R, false); - rz_core_cmd_lines(core, "oC 0x10 @ obj.seckrit # New file mapping from 0x0-0xf\n"); - ut64 obj_seckrit = rz_num_get(core->num, "obj.seckrit"); + // New file mapping from 0x0-0xf + rz_core_file_malloc_copy_chunk(core, 0x10, obj_seckrit); + RzIOMap *map = rz_io_map_get(core->io, 0); rz_io_map_remap(core->io, map->id, obj_seckrit);