mirror of
https://github.com/rizinorg/jsdec.git
synced 2026-11-07 06:45:57 -10:00
Initial work with QuickJS from Frida
This commit is contained in:
1 parent
3eb1a5b27c
commit
a87fa8cb0f
35 files changed
+813
-109292
No files matched your search
File renamed without changes.
+5
-14
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"parserOptions": {
|
||||
"ecmaVersion": 6,
|
||||
"sourceType": "script",
|
||||
"ecmaVersion": "latest",
|
||||
"sourceType": "module",
|
||||
"ecmaFeatures": {}
|
||||
},
|
||||
"extends": "eslint:recommended",
|
||||
@@ -29,17 +29,8 @@
|
||||
"no-control-regex": "warn"
|
||||
},
|
||||
"globals": {
|
||||
"Global": true,
|
||||
"Duktape": true,
|
||||
"TextDecoder": true,
|
||||
"require": true,
|
||||
"include": true,
|
||||
"read_file": true,
|
||||
"module": true,
|
||||
"console": true,
|
||||
"rzcmd": true,
|
||||
"rz_func_graph": true,
|
||||
"___internal_load": true,
|
||||
"___internal_require": true
|
||||
"Limits": true,
|
||||
"rizin": true,
|
||||
"console": true
|
||||
}
|
||||
}
|
||||
+2
-12
@@ -1,12 +1,2 @@
|
||||
/nbproject/
|
||||
/node_modules/
|
||||
p/build
|
||||
p/clean
|
||||
p/core_pdd.so
|
||||
p/jsdec-test
|
||||
/themes/*.json
|
||||
core_pdd.so
|
||||
p/*.o
|
||||
p/duktape/*.o
|
||||
.DS_Store
|
||||
.cache/
|
||||
build*/
|
||||
subprojects/libquick*/
|
||||
+10
-11
@@ -31,23 +31,22 @@ Files: eslint-check.sh
|
||||
Copyright: 2018-2019 Giovanni Dante Grazioli <[email protected]>
|
||||
License: BSD-3-Clause
|
||||
|
||||
Files: p/duktape/*
|
||||
Copyright: 2013-2019 Duktape
|
||||
License: MIT
|
||||
Files: subprojects/*
|
||||
Copyright: 2023 Giovanni Dante Grazioli <[email protected]>
|
||||
License: BSD-3-Clause
|
||||
|
||||
Files: p/meson.build
|
||||
Files: subprojects/**/*
|
||||
Copyright: 2023 Giovanni Dante Grazioli <[email protected]>
|
||||
License: BSD-3-Clause
|
||||
|
||||
Files: meson.build
|
||||
Copyright: 2021 Giovanni Dante Grazioli <[email protected]>
|
||||
License: BSD-3-Clause
|
||||
|
||||
Files: p/meson_options.txt
|
||||
Files: meson_options.txt
|
||||
Copyright: 2021 Giovanni Dante Grazioli <[email protected]>
|
||||
License: BSD-3-Clause
|
||||
|
||||
Files: p/.clang-format
|
||||
Files: .clang-format
|
||||
Copyright: 2021 Giovanni Dante Grazioli <[email protected]>
|
||||
License: BSD-3-Clause
|
||||
|
||||
Files: p/Makefile
|
||||
Copyright: 2017-2021 Giovanni Dante Grazioli <[email protected]>
|
||||
Copyright: 2017-2018 pancake <[email protected]>
|
||||
License: BSD-3-Clause
|
||||
@@ -1,73 +0,0 @@
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives.
|
||||
|
||||
Copyright [yyyy] [name of copyright owner]
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
@@ -1,9 +0,0 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) <year> <copyright holders>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
@@ -0,0 +1,346 @@
|
||||
// SPDX-FileCopyrightText: 2018-2023 Giovanni Dante Grazioli <[email protected]>
|
||||
// SPDX-License-Identifier: BSD-3-Clause
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <rz_types.h>
|
||||
#include <rz_lib.h>
|
||||
#include <rz_cmd.h>
|
||||
#include <rz_core.h>
|
||||
#include <rz_cons.h>
|
||||
#include <rz_analysis.h>
|
||||
|
||||
#include "jsdec.h"
|
||||
|
||||
typedef struct rz_core_t RzCore;
|
||||
|
||||
typedef struct exec_context_t {
|
||||
RzCore *core;
|
||||
void *bed;
|
||||
} ExecContext;
|
||||
|
||||
#undef RZ_API
|
||||
#define RZ_API static
|
||||
#undef RZ_IPI
|
||||
#define RZ_IPI static
|
||||
#define SETDESC(x, y) rz_config_node_desc(x, y)
|
||||
#define SETPREF(x, y, z) SETDESC(rz_config_set(cfg, x, y), z)
|
||||
|
||||
#define name_args(name) (internal_##name##_args)
|
||||
#define name_help(name) (internal_##name##_help)
|
||||
#define name_handler(name) (internal_##name##_handler)
|
||||
#define command_handler(name, arg) \
|
||||
RZ_IPI RzCmdStatus name_handler(name)(RzCore * core, int argc, const char **argv) { \
|
||||
if (argc != 1) { \
|
||||
return RZ_CMD_STATUS_WRONG_ARGS; \
|
||||
} \
|
||||
jsdec_main(core, arg); \
|
||||
return RZ_CMD_STATUS_OK; \
|
||||
}
|
||||
#define static_description_no_args(command, description) \
|
||||
static const RzCmdDescArg name_args(command)[] = { \
|
||||
{ 0 }, \
|
||||
}; \
|
||||
static const RzCmdDescHelp name_help(command) = { \
|
||||
.summary = description, \
|
||||
.args = name_args(command), \
|
||||
}
|
||||
#define rz_cmd_desc_argv_new_warn(rcmd, root, cmd) \
|
||||
rz_warn_if_fail(rz_cmd_desc_argv_new(rcmd, root, #cmd, name_handler(cmd), &name_help(cmd)))
|
||||
|
||||
static JSValue js_analysis_bytes(JSContext *ctx, RzCore *core, RzAnalysisBytes *ab) {
|
||||
RzAnalysisOp *aop = ab->op;
|
||||
JSValue op = JS_NewObject(ctx);
|
||||
JS_SetPropertyStr(ctx, op, "offset", JS_NewBigUint64(ctx, aop->addr));
|
||||
if (aop->ptr != UT64_MAX) {
|
||||
JS_SetPropertyStr(ctx, op, "ptr", JS_NewBigUint64(ctx, aop->ptr));
|
||||
}
|
||||
if (aop->val != UT64_MAX) {
|
||||
JS_SetPropertyStr(ctx, op, "val", JS_NewBigUint64(ctx, aop->val));
|
||||
}
|
||||
JS_SetPropertyStr(ctx, op, "opcode", JS_NewString(ctx, rz_str_get_null(ab->opcode)));
|
||||
JS_SetPropertyStr(ctx, op, "disasm", JS_NewString(ctx, rz_str_get_null(ab->disasm)));
|
||||
JS_SetPropertyStr(ctx, op, "type", JS_NewString(ctx, rz_analysis_optype_to_string(aop->type)));
|
||||
if (aop->jump != UT64_MAX) {
|
||||
JS_SetPropertyStr(ctx, op, "jump", JS_NewBigInt64(ctx, aop->jump));
|
||||
}
|
||||
if (aop->fail != UT64_MAX) {
|
||||
JS_SetPropertyStr(ctx, op, "fail", JS_NewBigUint64(ctx, aop->fail));
|
||||
}
|
||||
const char *comment = rz_meta_get_string(core->analysis, RZ_META_TYPE_COMMENT, aop->addr);
|
||||
if (RZ_STR_ISNOTEMPTY(comment)) {
|
||||
JS_SetPropertyStr(ctx, op, "comment", JS_NewString(ctx, comment));
|
||||
}
|
||||
return op;
|
||||
}
|
||||
|
||||
static JSValue js_analysis_opcodes(JSContext *ctx, RzCore *core) {
|
||||
RzAnalysisBytes *ab;
|
||||
void **it;
|
||||
JSValue ops = JS_NewArray(ctx);
|
||||
st64 op_idx = 0;
|
||||
|
||||
RzPVector *vec = rz_core_analysis_bytes(core, core->block, core->blocksize, 0);
|
||||
rz_pvector_foreach (vec, it) {
|
||||
if (!it || !*it) {
|
||||
continue;
|
||||
}
|
||||
ab = *it;
|
||||
if (!ab->op || !strcmp(ab->opcode, "nop")) {
|
||||
continue;
|
||||
}
|
||||
JSValue op = js_analysis_bytes(ctx, core, ab);
|
||||
JS_SetPropertyInt64(ctx, ops, op_idx, op);
|
||||
op_idx++;
|
||||
}
|
||||
rz_pvector_free(vec);
|
||||
return ops;
|
||||
}
|
||||
|
||||
static JSValue js_function_bbs(JSContext *ctx, RzCore *core, RzAnalysisFunction *fcn) {
|
||||
JSValue bbs = JS_NewArray(ctx);
|
||||
RzAnalysisBlock *bbi;
|
||||
RzListIter *iter;
|
||||
st64 bbs_idx = 0;
|
||||
ut64 old_offset = core->offset;
|
||||
ut64 old_bsize = core->blocksize;
|
||||
rz_list_foreach (fcn->bbs, iter, bbi) {
|
||||
rz_core_block_size(core, bbi->size);
|
||||
rz_core_seek(core, bbi->addr, true);
|
||||
|
||||
JSValue bb = JS_NewObject(ctx);
|
||||
JS_SetPropertyStr(ctx, bb, "address", JS_NewBigUint64(ctx, bbi->addr));
|
||||
if (bbi->jump != UT64_MAX) {
|
||||
JS_SetPropertyStr(ctx, bb, "jump", JS_NewBigUint64(ctx, bbi->jump));
|
||||
}
|
||||
if (bbi->fail != UT64_MAX) {
|
||||
JS_SetPropertyStr(ctx, bb, "fail", JS_NewBigUint64(ctx, bbi->fail));
|
||||
}
|
||||
JSValue ops = js_analysis_opcodes(ctx, core);
|
||||
JS_SetPropertyStr(ctx, bb, "ops", ops);
|
||||
JS_SetPropertyInt64(ctx, bbs, bbs_idx, bb);
|
||||
bbs_idx++;
|
||||
}
|
||||
rz_core_block_size(core, old_bsize);
|
||||
rz_core_seek(core, old_offset, true);
|
||||
return bbs;
|
||||
}
|
||||
|
||||
static JSValue js_command(JSContext *ctx, JSValueConst jsThis, int argc, JSValueConst *argv) {
|
||||
if (argc != 1) {
|
||||
return JS_EXCEPTION;
|
||||
}
|
||||
ExecContext *ectx = (ExecContext *)JS_GetContextOpaque(ctx);
|
||||
RzCore *core = ectx->core;
|
||||
rz_cons_sleep_end(ectx->bed);
|
||||
|
||||
const char *command = JS_ToCString(ctx, argv[0]);
|
||||
if (!command) {
|
||||
ectx->bed = rz_cons_sleep_begin();
|
||||
return JS_EXCEPTION;
|
||||
}
|
||||
|
||||
char *output = rz_core_cmd_str(core, command);
|
||||
JS_FreeCString(ctx, command);
|
||||
|
||||
JSValue result = JS_NewString(ctx, output);
|
||||
free(output);
|
||||
ectx->bed = rz_cons_sleep_begin();
|
||||
return result;
|
||||
}
|
||||
|
||||
static JSValue js_graph(JSContext *ctx, JSValueConst jsThis, int argc, JSValueConst *argv) {
|
||||
if (argc != 0) {
|
||||
return JS_EXCEPTION;
|
||||
}
|
||||
ExecContext *ectx = (ExecContext *)JS_GetContextOpaque(ctx);
|
||||
RzCore *core = ectx->core;
|
||||
rz_cons_sleep_end(ectx->bed);
|
||||
|
||||
RzAnalysisFunction *fcn = rz_analysis_get_fcn_in(core->analysis, core->offset, -1);
|
||||
if (!fcn) {
|
||||
ectx->bed = rz_cons_sleep_begin();
|
||||
return JS_ThrowInternalError(ctx, "Cannot find function at 0x%08" PFMT64x, core->offset);
|
||||
}
|
||||
|
||||
JSValue graph = JS_NewArray(ctx);
|
||||
JSValue object = JS_NewObject(ctx);
|
||||
JS_SetPropertyInt64(ctx, graph, 0, object);
|
||||
JS_SetPropertyStr(ctx, object, "name", JS_NewString(ctx, rz_str_get_null(fcn->name)));
|
||||
JSValue blocks = js_function_bbs(ctx, core, fcn);
|
||||
JS_SetPropertyStr(ctx, object, "blocks", blocks);
|
||||
ectx->bed = rz_cons_sleep_begin();
|
||||
return graph;
|
||||
}
|
||||
|
||||
static JSValue js_console_log(JSContext *ctx, JSValueConst jsThis, int argc, JSValueConst *argv) {
|
||||
for (int i = 0; i < argc; ++i) {
|
||||
if (i != 0) {
|
||||
rz_cons_print(" ");
|
||||
}
|
||||
const char *str = JS_ToCString(ctx, argv[i]);
|
||||
if (!str) {
|
||||
return JS_EXCEPTION;
|
||||
}
|
||||
rz_cons_print(str);
|
||||
JS_FreeCString(ctx, str);
|
||||
}
|
||||
rz_cons_newline();
|
||||
rz_cons_flush();
|
||||
return JS_UNDEFINED;
|
||||
}
|
||||
|
||||
static jsdec_t *jsdec_create(void *opaque, const char *arg) {
|
||||
jsdec_t *dec = jsdec_new();
|
||||
if (!dec) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
JSContext *ctx = jsdec_context(dec);
|
||||
JS_SetContextOpaque(ctx, opaque);
|
||||
|
||||
JSValue global = JS_GetGlobalObject(ctx);
|
||||
JS_SetPropertyStr(ctx, global, "Global", JS_NewObject(ctx));
|
||||
|
||||
JSValue limits = JS_NewObject(ctx);
|
||||
JS_SetPropertyStr(ctx, global, "Limits", limits);
|
||||
JS_SetPropertyStr(ctx, limits, "UT16_MAX", JS_NewBigUint64(ctx, UT16_MAX));
|
||||
JS_SetPropertyStr(ctx, limits, "UT32_MAX", JS_NewBigUint64(ctx, UT32_MAX));
|
||||
JS_SetPropertyStr(ctx, limits, "UT64_MAX", JS_NewBigUint64(ctx, UT64_MAX));
|
||||
JS_SetPropertyStr(ctx, limits, "ST16_MAX", JS_NewBigInt64(ctx, ST16_MAX));
|
||||
JS_SetPropertyStr(ctx, limits, "ST32_MAX", JS_NewBigInt64(ctx, ST32_MAX));
|
||||
JS_SetPropertyStr(ctx, limits, "ST64_MAX", JS_NewBigInt64(ctx, ST64_MAX));
|
||||
|
||||
JSValue console = JS_NewObject(ctx);
|
||||
JS_SetPropertyStr(ctx, global, "console", console);
|
||||
JS_SetPropertyStr(ctx, console, "log", JS_NewCFunction(ctx, js_console_log, "log", 1));
|
||||
|
||||
JSValue rizin = JS_NewObject(ctx);
|
||||
JS_SetPropertyStr(ctx, global, "rizin", rizin);
|
||||
JS_SetPropertyStr(ctx, rizin, "command", JS_NewCFunction(ctx, js_command, "command", 1));
|
||||
JS_SetPropertyStr(ctx, rizin, "graph", JS_NewCFunction(ctx, js_graph, "graph", 1));
|
||||
|
||||
JSValue process = JS_NewObject(ctx);
|
||||
JS_SetPropertyStr(ctx, global, "process", process);
|
||||
JSValue args = JS_NewArray(ctx);
|
||||
if (RZ_STR_ISNOTEMPTY(arg)) {
|
||||
JS_SetPropertyInt64(ctx, args, 0, JS_NewString(ctx, arg));
|
||||
}
|
||||
JS_SetPropertyStr(ctx, process, "args", args);
|
||||
|
||||
JS_FreeValue(ctx, global);
|
||||
return dec;
|
||||
}
|
||||
|
||||
static bool jsdec_main(RzCore *core, const char *arg) {
|
||||
ExecContext ectx;
|
||||
ectx.core = core;
|
||||
|
||||
jsdec_t *dec = jsdec_create(&ectx, arg);
|
||||
if (!dec) {
|
||||
return false;
|
||||
}
|
||||
|
||||
ectx.bed = rz_cons_sleep_begin();
|
||||
bool ret = jsdec_run(dec);
|
||||
rz_cons_sleep_end(ectx.bed);
|
||||
|
||||
jsdec_free(dec);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static const RzCmdDescHelp pdd_usage = {
|
||||
.summary = "Core plugin for jsdec",
|
||||
};
|
||||
|
||||
|
||||
//static_description_no_args(cmd_pdd_star,"decompiled code is returned to rizin as comment (via CCu)");
|
||||
static_description_no_args(pdd, "decompile current function");
|
||||
static_description_no_args(pddt, "lists the supported architectures");
|
||||
static_description_no_args(pddc, "decompiled code is returned to rizin as 'file:line code' (via CL)");
|
||||
static_description_no_args(pdda, "decompile current function with side assembly");
|
||||
static_description_no_args(pddb, "decompile current function but show only scopes");
|
||||
static_description_no_args(pddo, "decompile current function side by side with offsets");
|
||||
static_description_no_args(pddj, "decompile current function as json");
|
||||
static_description_no_args(pddA, "decompile current function with annotation output");
|
||||
static_description_no_args(pddf, "decompile all functions");
|
||||
static_description_no_args(pddi, "generate issue data");
|
||||
|
||||
command_handler(pdd, NULL);
|
||||
command_handler(pddt, "--architectures");
|
||||
command_handler(pdda, "--assembly");
|
||||
command_handler(pddA, "--annotation");
|
||||
command_handler(pddb, "--blocks");
|
||||
command_handler(pddc, "--as-code-line");
|
||||
command_handler(pddf, "--all-functions");
|
||||
command_handler(pddi, "--issue");
|
||||
command_handler(pddj, "--as-json");
|
||||
command_handler(pddo, "--offsets");
|
||||
|
||||
static bool rz_cmd_pdd_init(RzCore *core) {
|
||||
RzCmd *rcmd = core->rcmd;
|
||||
RzConfig *cfg = core->config;
|
||||
RzCmdDesc *root_cd = rz_cmd_get_desc(rcmd, "pd");
|
||||
if (!root_cd) {
|
||||
rz_warn_if_reached();
|
||||
return false;
|
||||
}
|
||||
|
||||
rz_config_lock(cfg, false);
|
||||
SETPREF("jsdec.asm", "false", "if true, shows pseudo next to the assembly.");
|
||||
SETPREF("jsdec.blocks", "false", "if true, shows only scopes blocks.");
|
||||
SETPREF("jsdec.casts", "false", "if false, hides all casts in the pseudo code.");
|
||||
SETPREF("jsdec.debug", "false", "do not catch exceptions in jsdec.");
|
||||
SETPREF("jsdec.highlight", "default", "highlights the current address.");
|
||||
SETPREF("jsdec.paddr", "false", "if true, all xrefs uses physical addresses compare.");
|
||||
SETPREF("jsdec.slow", "false", "load all the data before to avoid multirequests to rizin.");
|
||||
SETPREF("jsdec.xrefs", "false", "if true, shows all xrefs in the pseudo code.");
|
||||
rz_config_lock(cfg, true);
|
||||
|
||||
RzCmdDesc *pdd = rz_cmd_desc_group_new(rcmd, root_cd, "pdd", name_handler(pdd), &name_help(pdd), &pdd_usage);
|
||||
if (!pdd) {
|
||||
rz_warn_if_reached();
|
||||
return false;
|
||||
}
|
||||
|
||||
rz_cmd_desc_argv_new_warn(rcmd, pdd, pddt);
|
||||
rz_cmd_desc_argv_new_warn(rcmd, pdd, pdda);
|
||||
rz_cmd_desc_argv_new_warn(rcmd, pdd, pddA);
|
||||
rz_cmd_desc_argv_new_warn(rcmd, pdd, pddb);
|
||||
rz_cmd_desc_argv_new_warn(rcmd, pdd, pddc);
|
||||
rz_cmd_desc_argv_new_warn(rcmd, pdd, pddf);
|
||||
rz_cmd_desc_argv_new_warn(rcmd, pdd, pddi);
|
||||
rz_cmd_desc_argv_new_warn(rcmd, pdd, pddj);
|
||||
rz_cmd_desc_argv_new_warn(rcmd, pdd, pddo);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool rz_cmd_pdd_fini(RzCore *core) {
|
||||
RzCmd *rcmd = core->rcmd;
|
||||
RzCmdDesc *cd = rz_cmd_get_desc(rcmd, "pdd");
|
||||
return rz_cmd_desc_remove(rcmd, cd);
|
||||
}
|
||||
|
||||
RzCorePlugin rz_core_plugin_jsdec = {
|
||||
.name = "jsdec",
|
||||
.author = "deroad",
|
||||
.desc = "Pseudo-code decompiler for rizin",
|
||||
.license = "BSD-3-Clause",
|
||||
.init = rz_cmd_pdd_init,
|
||||
.fini = rz_cmd_pdd_fini,
|
||||
};
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#define _RZ_API __declspec(dllexport)
|
||||
#else
|
||||
#define _RZ_API
|
||||
#endif
|
||||
|
||||
#ifndef CORELIB
|
||||
_RZ_API RzLibStruct rizin_plugin = {
|
||||
.type = RZ_LIB_TYPE_CORE,
|
||||
.data = &rz_core_plugin_jsdec,
|
||||
.version = RZ_VERSION,
|
||||
};
|
||||
#endif
|
||||
@@ -1,4 +1,4 @@
|
||||
// SPDX-FileCopyrightText: 2018-2021 Giovanni Dante Grazioli <[email protected]>
|
||||
// SPDX-FileCopyrightText: 2018-2023 Giovanni Dante Grazioli <[email protected]>
|
||||
// SPDX-License-Identifier: BSD-3-Clause
|
||||
|
||||
#include <string.h>
|
||||
@@ -0,0 +1,178 @@
|
||||
// SPDX-FileCopyrightText: 2023 Giovanni Dante Grazioli <[email protected]>
|
||||
// SPDX-License-Identifier: BSD-3-Clause
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include "jsdec.h"
|
||||
#include "js/bytecode.h"
|
||||
|
||||
struct jsdec_s {
|
||||
JSRuntime *runtime;
|
||||
JSContext *context;
|
||||
};
|
||||
|
||||
#define macro_str(s) #s
|
||||
#define errorf(...) fprintf(stderr, __VA_ARGS__)
|
||||
|
||||
void js_print_exception(JSContext *ctx, JSValueConst val) {
|
||||
const char *strval = JS_ToCString(ctx, val);
|
||||
if (strval) {
|
||||
fprintf(stderr, "%s\n", strval);
|
||||
JS_FreeCString(ctx, strval);
|
||||
} else {
|
||||
fprintf(stderr, "[exception]\n");
|
||||
}
|
||||
}
|
||||
|
||||
void jsdec_handle_exception(JSContext *ctx) {
|
||||
JSValue exception = JS_GetException(ctx);
|
||||
int is_error = JS_IsError(ctx, exception);
|
||||
|
||||
js_print_exception(ctx, exception);
|
||||
if (!is_error) {
|
||||
JS_FreeValue(ctx, exception);
|
||||
return;
|
||||
}
|
||||
|
||||
JSValue stack = JS_GetPropertyStr(ctx, exception, "stack");
|
||||
if (!JS_IsUndefined(stack)) {
|
||||
js_print_exception(ctx, stack);
|
||||
}
|
||||
JS_FreeValue(ctx, stack);
|
||||
JS_FreeValue(ctx, exception);
|
||||
}
|
||||
|
||||
#if 0
|
||||
static void print_jsval(JSValue val) {
|
||||
if(JS_IsNumber(val)) {
|
||||
errorf("JS_IsNumber\n");
|
||||
} else if(JS_IsBigInt(NULL, val)) {
|
||||
errorf("JS_IsBigInt\n");
|
||||
} else if(JS_IsBigFloat(val)) {
|
||||
errorf("JS_IsBigFloat\n");
|
||||
} else if(JS_IsBigDecimal(val)) {
|
||||
errorf("JS_IsBigDecimal\n");
|
||||
} else if(JS_IsBool(val)) {
|
||||
errorf("JS_IsBool\n");
|
||||
} else if(JS_IsNull(val)) {
|
||||
errorf("JS_IsNull\n");
|
||||
} else if(JS_IsUndefined(val)) {
|
||||
errorf("JS_IsUndefined\n");
|
||||
} else if(JS_IsException(val)) {
|
||||
errorf("JS_IsException\n");
|
||||
} else if(JS_IsUninitialized(val)) {
|
||||
errorf("JS_IsUninitialized\n");
|
||||
} else if(JS_IsString(val)) {
|
||||
errorf("JS_IsString\n");
|
||||
} else if(JS_IsSymbol(val)) {
|
||||
errorf("JS_IsSymbol\n");
|
||||
} else if(JS_IsObject(val)) {
|
||||
errorf("JS_IsObject\n");
|
||||
} else if(JS_VALUE_GET_TAG(val) == JS_TAG_MODULE) {
|
||||
errorf("Module\n");
|
||||
} else if(JS_VALUE_GET_TAG(val) == JS_TAG_FUNCTION_BYTECODE) {
|
||||
errorf("Function bytecode\n");
|
||||
} else {
|
||||
errorf("No idea...\n");
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
static int js_add_module(JSContext *ctx, const uint8_t *bytecode, const uint32_t size) {
|
||||
JSValue obj = JS_ReadObject(ctx, bytecode, size, JS_READ_OBJ_BYTECODE);
|
||||
if (JS_IsException(obj)) {
|
||||
jsdec_handle_exception(ctx);
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int js_load_module(JSContext *ctx, const uint8_t *bytes, const uint32_t size) {
|
||||
JSValue obj = JS_ReadObject(ctx, bytes, size, JS_READ_OBJ_BYTECODE);
|
||||
if (JS_IsException(obj)) {
|
||||
jsdec_handle_exception(ctx);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (JS_VALUE_GET_TAG(obj) == JS_TAG_MODULE &&
|
||||
JS_ResolveModule(ctx, obj) < 0) {
|
||||
JS_FreeValue(ctx, obj);
|
||||
errorf("Error: failed to resolve jsdec module\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
JSValue val = JS_EvalFunction(ctx, obj);
|
||||
if (JS_IsException(val)) {
|
||||
jsdec_handle_exception(ctx);
|
||||
return 0;
|
||||
}
|
||||
JS_FreeValue(ctx, val);
|
||||
return 1;
|
||||
}
|
||||
|
||||
#include "js/bytecode_mod.h"
|
||||
|
||||
void jsdec_free(jsdec_t *dec) {
|
||||
if (!dec) {
|
||||
return;
|
||||
}
|
||||
|
||||
JS_FreeContext(dec->context);
|
||||
JS_FreeRuntime(dec->runtime);
|
||||
free(dec);
|
||||
}
|
||||
|
||||
jsdec_t *jsdec_new() {
|
||||
JSRuntime *rt = JS_NewRuntime();
|
||||
if (!rt) {
|
||||
errorf("Error: failed to create qjs runtime\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
JSContext *ctx = JS_NewContextRaw(rt);
|
||||
if (!ctx) {
|
||||
errorf("Error: failed to create qjs context\n");
|
||||
JS_FreeRuntime(rt);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// initialize all intrisic
|
||||
JS_AddIntrinsicBaseObjects(ctx);
|
||||
JS_AddIntrinsicRegExp(ctx);
|
||||
JS_AddIntrinsicJSON(ctx);
|
||||
JS_AddIntrinsicMapSet(ctx);
|
||||
JS_AddIntrinsicPromise(ctx);
|
||||
JS_AddIntrinsicBigInt(ctx);
|
||||
JS_AddIntrinsicBigFloat(ctx);
|
||||
JS_AddIntrinsicBigDecimal(ctx);
|
||||
JS_AddIntrinsicOperators(ctx);
|
||||
JS_AddIntrinsicEval(ctx);
|
||||
JS_EnableBignumExt(ctx, 1);
|
||||
|
||||
if (!js_load_all_modules(ctx)) {
|
||||
JS_FreeContext(ctx);
|
||||
JS_FreeRuntime(rt);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
jsdec_t *dec = malloc(sizeof(jsdec_t));
|
||||
if (!dec) {
|
||||
errorf("Error: failed to allocate jsdec_t\n");
|
||||
JS_FreeContext(ctx);
|
||||
JS_FreeRuntime(rt);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
dec->context = ctx;
|
||||
dec->runtime = rt;
|
||||
return dec;
|
||||
}
|
||||
|
||||
JSContext *jsdec_context(const jsdec_t *dec) {
|
||||
return dec->context;
|
||||
}
|
||||
|
||||
int jsdec_run(const jsdec_t *dec) {
|
||||
return js_load_module(dec->context, main_bytecode, main_bytecode_size);
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
// SPDX-FileCopyrightText: 2023 Giovanni Dante Grazioli <[email protected]>
|
||||
// SPDX-License-Identifier: BSD-3-Clause
|
||||
|
||||
#ifndef JSDEC_H
|
||||
#define JSDEC_H
|
||||
|
||||
#include <quickjs.h>
|
||||
|
||||
typedef struct jsdec_s jsdec_t;
|
||||
|
||||
void jsdec_free(jsdec_t *dec);
|
||||
jsdec_t *jsdec_new();
|
||||
JSContext *jsdec_context(const jsdec_t *dec);
|
||||
int jsdec_run(const jsdec_t *dec);
|
||||
void jsdec_handle_exception(JSContext *ctx);
|
||||
|
||||
#endif /* JSDEC_H */
|
||||
File renamed without changes.
File renamed without changes.
File renamed without changes.
-1350
File diff suppressed because it is too large.
Load diff
+76
@@ -0,0 +1,76 @@
|
||||
project('jsdec', 'c', meson_version: '>=0.55.0')
|
||||
|
||||
cc = meson.get_compiler('c')
|
||||
|
||||
libquickjs_proj = subproject('libquickjs', default_options: ['default_library=static'])
|
||||
libquickjs_dep = libquickjs_proj.get_variable('quickjs_dep')
|
||||
qjsc = libquickjs_proj.get_variable('qjsc')
|
||||
|
||||
jsdec_incs = ['.', 'c']
|
||||
jsdec_c_args = []
|
||||
jsdec_src = [
|
||||
'c' / 'jsdec.c',
|
||||
]
|
||||
jsdec_deps = [
|
||||
libquickjs_dep,
|
||||
]
|
||||
|
||||
if get_option('standalone')
|
||||
add_project_arguments('-DJSDEC_STANDALONE', language: 'c')
|
||||
endif
|
||||
|
||||
modjs_gen = executable('modjs_gen', 'tools' / 'modjs_gen.c',
|
||||
dependencies: [],
|
||||
include_directories: [],
|
||||
implicit_include_directories: false,
|
||||
install: false,
|
||||
)
|
||||
|
||||
subdir('js')
|
||||
|
||||
if get_option('standalone')
|
||||
|
||||
jsdec_src += [
|
||||
bytecode_h,
|
||||
bytecode_mod_h,
|
||||
'c' / 'jsdec-standalone.c',
|
||||
]
|
||||
|
||||
jsdec_standalone = executable('jsdec-standalone', jsdec_src,
|
||||
c_args : jsdec_c_args,
|
||||
dependencies: jsdec_deps,
|
||||
include_directories: include_directories(jsdec_incs),
|
||||
implicit_include_directories: false,
|
||||
install: false,
|
||||
)
|
||||
else
|
||||
# build plugin for Rizin
|
||||
rz_core_dep = dependency('rz_core')
|
||||
jsdec_deps += rz_core_dep
|
||||
jsdec_deps += dependency('rz_util')
|
||||
jsdec_deps += dependency('rz_cons')
|
||||
jsdec_deps += dependency('rz_config')
|
||||
jsdec_deps += dependency('rz_io')
|
||||
rizin_plugdir = get_option('rizin_plugdir')
|
||||
if rizin_plugdir == ''
|
||||
rizin_plugdir = rz_core_dep.get_variable(pkgconfig: 'plugindir', cmake: 'rz_core_PLUGINDIR')
|
||||
plugin_jsdec_dir = join_paths(get_option('prefix'), rizin_plugdir, 'jsdec')
|
||||
else
|
||||
plugin_jsdec_dir = join_paths(rizin_plugdir, 'jsdec')
|
||||
endif
|
||||
|
||||
jsdec_src += [
|
||||
bytecode_h,
|
||||
bytecode_mod_h,
|
||||
'c' / 'jsdec-plugin.c'
|
||||
]
|
||||
|
||||
shared_library('core_pdd', jsdec_src,
|
||||
c_args : jsdec_c_args,
|
||||
dependencies: jsdec_deps,
|
||||
include_directories: include_directories(jsdec_incs),
|
||||
implicit_include_directories: false,
|
||||
install: true,
|
||||
install_dir: rizin_plugdir
|
||||
)
|
||||
endif
|
||||
@@ -0,0 +1,2 @@
|
||||
option('rizin_plugdir', type: 'string', value: '', description: 'rizin install directory')
|
||||
option('standalone', type: 'boolean', value: false, description: 'enables/disables building only the standalone executable for testing purposes')
|
||||
-49
@@ -1,49 +0,0 @@
|
||||
CFLAGS+=-g -O3 -std=c99 -Wall -fPIC -I./duktape
|
||||
CFLAGS_PKG=$(shell pkg-config --cflags rz_util rz_io rz_cons rz_core)
|
||||
|
||||
LDFLAGS+=-lm
|
||||
LDFLAGS_PKG=$(shell pkg-config --libs rz_util rz_io rz_cons rz_core)
|
||||
|
||||
LIBEXT=$(shell rizin -H LIBEXT)
|
||||
PLUGDIR=$(shell rizin -H RZ_USER_PLUGINS)
|
||||
|
||||
V=@
|
||||
ECHO=echo
|
||||
RM=rm -f
|
||||
CP=cp -f
|
||||
MKDIR=mkdir -p
|
||||
|
||||
SRCS=duktape/duktape.c duktape/duk_console.c core_pdd.c
|
||||
OBJS=$(SRCS:.c=.o)
|
||||
BIN=core_pdd.$(LIBEXT)
|
||||
|
||||
all: clean build install
|
||||
|
||||
build: $(BIN)
|
||||
|
||||
$(BIN): $(OBJS)
|
||||
$(V)$(ECHO) "[CC] $@"
|
||||
$(V)$(CC) $(LDFLAGS) $(LDFLAGS_PKG) -shared $^ -o $@
|
||||
|
||||
%.o: %.c
|
||||
$(V)$(ECHO) "[CC] $@"
|
||||
$(V)$(CC) $(CFLAGS) $(CFLAGS_PKG) -c $< -o $@
|
||||
|
||||
$(PLUGDIR):
|
||||
$(V)$(MKDIR) $@
|
||||
|
||||
install: uninstall $(PLUGDIR) $(BIN)
|
||||
$(V)$(ECHO) "[INSTALL]" $(PLUGDIR)/$(BIN)
|
||||
$(V)$(RM) $(PLUGDIR)/$(BIN)
|
||||
$(V)$(CP) $(BIN) $(PLUGDIR)
|
||||
|
||||
uninstall:
|
||||
$(V)$(RM) $(PLUGDIR)/$(BIN)
|
||||
|
||||
clean:
|
||||
$(V)$(RM) $(BIN) $(OBJS) || sleep 0
|
||||
|
||||
testbin:
|
||||
$(V)$(CC) $(CFLAGS) -DUSE_RZCONS=0 -o jsdec-test duktape/duktape.c duktape/duk_console.c jsdec-test.c $(LDFLAGS)
|
||||
|
||||
.PHONY: clean all install uninstall testbin build
|
||||
-372
@@ -1,372 +0,0 @@
|
||||
// SPDX-FileCopyrightText: 2018-2021 Giovanni Dante Grazioli <[email protected]>
|
||||
// SPDX-License-Identifier: BSD-3-Clause
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <rz_types.h>
|
||||
#include <rz_lib.h>
|
||||
#include <rz_cmd.h>
|
||||
#include <rz_core.h>
|
||||
#include <rz_cons.h>
|
||||
#include <rz_analysis.h>
|
||||
#include "duktape.h"
|
||||
#include "duk_console.h"
|
||||
#include "duk_missing.h"
|
||||
|
||||
#include "jsdec_ctx.h"
|
||||
|
||||
#undef RZ_API
|
||||
#define RZ_API static
|
||||
#undef RZ_IPI
|
||||
#define RZ_IPI static
|
||||
#define SETDESC(x, y) rz_config_node_desc(x, y)
|
||||
#define SETPREF(x, y, z) SETDESC(rz_config_set(cfg, x, y), z)
|
||||
|
||||
/* for compatibility. */
|
||||
#ifndef RZ_HOME_DATADIR
|
||||
#define RZ_HOME_DATADIR RZ_HOMEDIR
|
||||
#endif
|
||||
|
||||
#define name_args(name) (internal_##name##_args)
|
||||
#define name_help(name) (internal_##name##_help)
|
||||
#define name_handler(name) (internal_##name##_handler)
|
||||
#define command_handler(name, command) \
|
||||
RZ_IPI RzCmdStatus name_handler(name)(RzCore * core, int argc, const char **argv) { \
|
||||
if (argc != 1) { \
|
||||
return RZ_CMD_STATUS_WRONG_ARGS; \
|
||||
} \
|
||||
duk_jsdec(core, command); \
|
||||
return RZ_CMD_STATUS_OK; \
|
||||
}
|
||||
#define static_description_no_args(command, description) \
|
||||
static const RzCmdDescArg name_args(command)[] = { \
|
||||
{ 0 }, \
|
||||
}; \
|
||||
static const RzCmdDescHelp name_help(command) = { \
|
||||
.summary = description, \
|
||||
.args = name_args(command), \
|
||||
}
|
||||
#define rz_cmd_desc_argv_new_warn(rcmd, root, cmd) \
|
||||
rz_warn_if_fail(rz_cmd_desc_argv_new(rcmd, root, #cmd, name_handler(cmd), &name_help(cmd)))
|
||||
|
||||
#ifdef USE_JSC
|
||||
#include "jsdec_jsc.c"
|
||||
#endif
|
||||
|
||||
static char *jsdec_read_file(const char *file) {
|
||||
if (!file) {
|
||||
return 0;
|
||||
}
|
||||
char *homedir;
|
||||
char *env = rz_sys_getenv("JSDEC_HOME");
|
||||
if (env) {
|
||||
homedir = env;
|
||||
} else {
|
||||
#ifdef JSDEC_HOME
|
||||
homedir = rz_str_new(JSDEC_HOME);
|
||||
#else
|
||||
homedir = rz_str_home(RZ_HOME_DATADIR RZ_SYS_DIR
|
||||
"rzpm" RZ_SYS_DIR "git" RZ_SYS_DIR "jsdec");
|
||||
#endif
|
||||
}
|
||||
size_t len = 0;
|
||||
if (!homedir) {
|
||||
return 0;
|
||||
}
|
||||
char *filepath = rz_str_newf("%s" RZ_SYS_DIR "%s", homedir, file);
|
||||
free(homedir);
|
||||
char *text = rz_file_slurp(filepath, &len);
|
||||
if (text && len > 0) {
|
||||
free(filepath);
|
||||
return text;
|
||||
}
|
||||
free(filepath);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static duk_ret_t duk_rzcmd(duk_context *ctx) {
|
||||
if (duk_is_string(ctx, 0)) {
|
||||
const char *command = duk_safe_to_string(ctx, 0);
|
||||
//fprintf (stderr, "RZCMD: %s\n", command);
|
||||
//fflush (stderr);
|
||||
JsDecCtx *jsdec_ctx = jsdec_ctx_get(ctx);
|
||||
rz_cons_sleep_end(jsdec_ctx->bed);
|
||||
char *output = rz_core_cmd_str(jsdec_ctx->core, command);
|
||||
jsdec_ctx->bed = rz_cons_sleep_begin();
|
||||
duk_push_string(ctx, output);
|
||||
free(output);
|
||||
return 1;
|
||||
}
|
||||
return DUK_RET_TYPE_ERROR;
|
||||
}
|
||||
|
||||
static duk_ret_t duk_internal_load(duk_context *ctx) {
|
||||
if (!duk_is_string(ctx, 0)) {
|
||||
return DUK_RET_TYPE_ERROR;
|
||||
}
|
||||
const char *fullname = duk_safe_to_string(ctx, 0);
|
||||
char *text = jsdec_read_file(fullname);
|
||||
if (!text) {
|
||||
printf("Error: '%s' not found.\n", fullname);
|
||||
return DUK_RET_TYPE_ERROR;
|
||||
}
|
||||
duk_push_string(ctx, text);
|
||||
free(text);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static duk_ret_t duk_internal_require(duk_context *ctx) {
|
||||
char fullname[256];
|
||||
if (!duk_is_string(ctx, 0)) {
|
||||
return DUK_RET_TYPE_ERROR;
|
||||
}
|
||||
snprintf(fullname, sizeof(fullname), "%s.js", duk_safe_to_string(ctx, 0));
|
||||
#ifdef USE_JSC
|
||||
const char *js = jsdec_jsc(fullname);
|
||||
#else
|
||||
char *js = jsdec_read_file(fullname);
|
||||
#endif
|
||||
if (!js) {
|
||||
printf("Error: '%s' not found.\n", fullname);
|
||||
return DUK_RET_TYPE_ERROR;
|
||||
}
|
||||
duk_push_lstring(ctx, fullname, strlen(fullname));
|
||||
duk_eval_file(ctx, js);
|
||||
#ifndef USE_JSC
|
||||
free(js);
|
||||
#endif
|
||||
return 1;
|
||||
}
|
||||
|
||||
static char *function_graph_nodes(RzCore *core) {
|
||||
RzAnalysisFunction *fcn = rz_analysis_get_fcn_in(core->analysis, core->offset, -1);
|
||||
if (!fcn) {
|
||||
return strdup("[]");
|
||||
}
|
||||
|
||||
PJ *pj = pj_new();
|
||||
if (!pj) {
|
||||
return strdup("[]");
|
||||
}
|
||||
|
||||
pj_a(pj);
|
||||
char *fcn_name_escaped = rz_str_escape_utf8_for_json(fcn->name, -1);
|
||||
pj_o(pj);
|
||||
pj_ks(pj, "name", rz_str_get_null(fcn_name_escaped));
|
||||
free(fcn_name_escaped);
|
||||
pj_k(pj, "blocks");
|
||||
pj_a(pj);
|
||||
|
||||
RzAnalysisBlock *bbi;
|
||||
RzListIter *iter;
|
||||
rz_list_foreach (fcn->bbs, iter, bbi) {
|
||||
pj_o(pj);
|
||||
pj_k(pj, "ops");
|
||||
pj_a(pj);
|
||||
ut8 *buf = malloc(bbi->size);
|
||||
if (buf) {
|
||||
rz_io_read_at(core->io, bbi->addr, buf, bbi->size);
|
||||
rz_core_print_disasm_json(core, bbi->addr, buf, bbi->size, 0, pj);
|
||||
free(buf);
|
||||
} else {
|
||||
printf("Error: cannot allocate %" PFMT64u " byte(s)\n", bbi->size);
|
||||
}
|
||||
pj_end(pj);
|
||||
pj_end(pj);
|
||||
}
|
||||
|
||||
pj_end(pj);
|
||||
pj_end(pj);
|
||||
pj_end(pj);
|
||||
|
||||
return pj_drain(pj);
|
||||
}
|
||||
|
||||
static duk_ret_t duk_rz_func_graph(duk_context *ctx) {
|
||||
JsDecCtx *jsdec_ctx = jsdec_ctx_get(ctx);
|
||||
rz_cons_sleep_end(jsdec_ctx->bed);
|
||||
char *output = function_graph_nodes(jsdec_ctx->core);
|
||||
jsdec_ctx->bed = rz_cons_sleep_begin();
|
||||
duk_push_string(ctx, output);
|
||||
free(output);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void duk_jsdec_init(duk_context *ctx, JsDecCtx *jsdec_ctx) {
|
||||
duk_push_global_stash(ctx);
|
||||
duk_push_pointer(ctx, (void *)jsdec_ctx);
|
||||
duk_put_prop_string(ctx, -2, "jsdec_ctx");
|
||||
duk_pop(ctx);
|
||||
|
||||
duk_push_c_function(ctx, duk_internal_require, 1);
|
||||
duk_put_global_string(ctx, "___internal_require");
|
||||
duk_push_c_function(ctx, duk_internal_load, 1);
|
||||
duk_put_global_string(ctx, "___internal_load");
|
||||
duk_push_c_function(ctx, duk_rzcmd, 1);
|
||||
duk_put_global_string(ctx, "rzcmd");
|
||||
|
||||
duk_push_c_function(ctx, duk_rz_func_graph, 1);
|
||||
duk_put_global_string(ctx, "rz_func_graph");
|
||||
}
|
||||
|
||||
JsDecCtx *jsdec_ctx_get(duk_context *ctx) {
|
||||
duk_push_global_stash(ctx);
|
||||
duk_get_prop_string(ctx, -1, "jsdec_ctx");
|
||||
JsDecCtx *r = duk_require_pointer(ctx, -1);
|
||||
duk_pop_2(ctx);
|
||||
return r;
|
||||
}
|
||||
|
||||
//static void duk_rizin_debug_stack(duk_context* ctx) {
|
||||
// duk_push_context_dump(ctx);
|
||||
// printf("%s\n", duk_to_string(ctx, -1));
|
||||
// duk_pop(ctx);
|
||||
//}
|
||||
|
||||
static void eval_file(duk_context *ctx, const char *file) {
|
||||
//fprintf (stderr, "REQUIRE: %s\n", file);
|
||||
//fflush (stderr);
|
||||
#ifdef USE_JSC
|
||||
const char *js = jsdec_jsc(file);
|
||||
#else
|
||||
char *js = jsdec_read_file(file);
|
||||
#endif
|
||||
if (js) {
|
||||
duk_push_lstring(ctx, file, strlen(file));
|
||||
duk_eval_file_noresult(ctx, js);
|
||||
#ifndef USE_JSC
|
||||
free(js);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
static void jsdec_fatal_function(void *udata, const char *msg) {
|
||||
fprintf(stderr, "*** FATAL ERROR: %s\n", (msg ? msg : "no message"));
|
||||
fflush(stderr);
|
||||
abort();
|
||||
}
|
||||
|
||||
static void duk_jsdec(RzCore *core, const char *input) {
|
||||
char args[1024] = { 0 };
|
||||
JsDecCtx jsdec_ctx;
|
||||
jsdec_ctx.core = core;
|
||||
jsdec_ctx.bed = rz_cons_sleep_begin();
|
||||
duk_context *ctx = duk_create_heap(0, 0, 0, 0, jsdec_fatal_function);
|
||||
duk_console_init(ctx, 0);
|
||||
// Long_init (ctx);
|
||||
duk_jsdec_init(ctx, &jsdec_ctx);
|
||||
eval_file(ctx, "require.js");
|
||||
eval_file(ctx, "jsdec-duk.js");
|
||||
#ifdef USE_JSC
|
||||
if (*input) {
|
||||
snprintf(args, sizeof(args), "try{if(typeof jsdec_main == 'function'){jsdec_main(\"%s\".split(/\\s+/));}else{console.log('Fatal error. Cannot call jsdec_main.');}}catch(_____e){console.log(_____e.stack||_____e);}", input);
|
||||
} else {
|
||||
snprintf(args, sizeof(args), "try{if(typeof jsdec_main == 'function'){jsdec_main([]);}else{console.log('Fatal error. Cannot call jsdec_main.');}}catch(_____e){console.log(_____e.stack||_____e);}");
|
||||
}
|
||||
#else
|
||||
if (*input) {
|
||||
snprintf(args, sizeof(args), "try{if(typeof jsdec_main == 'function'){jsdec_main(\"%s\".split(/\\s+/));}else{console.log('Fatal error. Cannot use RZ_HOME_DATADIR or JSDEC_HOME.');}}catch(_____e){console.log(_____e.stack||_____e);}", input);
|
||||
} else {
|
||||
snprintf(args, sizeof(args), "try{if(typeof jsdec_main == 'function'){jsdec_main([]);}else{console.log('Fatal error. Cannot use RZ_HOME_DATADIR or JSDEC_HOME.');}}catch(_____e){console.log(_____e.stack||_____e);}");
|
||||
}
|
||||
#endif
|
||||
duk_eval_string_noresult(ctx, args);
|
||||
//duk_rizin_debug_stack(ctx);
|
||||
duk_destroy_heap(ctx);
|
||||
rz_cons_sleep_end(jsdec_ctx.bed);
|
||||
}
|
||||
|
||||
static const RzCmdDescHelp pdd_usage = {
|
||||
.summary = "Core plugin for jsdec",
|
||||
};
|
||||
|
||||
//static_description_no_args(cmd_pdd_star,"decompiled code is returned to rizin as comment (via CCu)");
|
||||
static_description_no_args(pdd, "decompile current function");
|
||||
static_description_no_args(pddt, "lists the supported architectures");
|
||||
static_description_no_args(pddc, "decompiled code is returned to rizin as 'file:line code' (via CL)");
|
||||
static_description_no_args(pdda, "decompile current function with side assembly");
|
||||
static_description_no_args(pddb, "decompile current function but show only scopes");
|
||||
static_description_no_args(pddo, "decompile current function side by side with offsets");
|
||||
static_description_no_args(pddj, "decompile current function as json");
|
||||
static_description_no_args(pddA, "decompile current function with annotation output");
|
||||
static_description_no_args(pddf, "decompile all functions");
|
||||
static_description_no_args(pddi, "generate issue data");
|
||||
|
||||
command_handler(pdd, "");
|
||||
command_handler(pddt, "--architectures");
|
||||
command_handler(pdda, "--assembly");
|
||||
command_handler(pddA, "--annotation");
|
||||
command_handler(pddb, "--blocks");
|
||||
command_handler(pddc, "--as-code-line");
|
||||
command_handler(pddf, "--all-functions");
|
||||
command_handler(pddi, "--issue");
|
||||
command_handler(pddj, "--as-json");
|
||||
command_handler(pddo, "--offsets");
|
||||
|
||||
static bool rz_cmd_pdd_init(RzCore *core) {
|
||||
RzCmd *rcmd = core->rcmd;
|
||||
RzConfig *cfg = core->config;
|
||||
RzCmdDesc *root_cd = rz_cmd_get_desc(rcmd, "pd");
|
||||
if (!root_cd) {
|
||||
rz_warn_if_reached();
|
||||
return false;
|
||||
}
|
||||
|
||||
rz_config_lock(cfg, false);
|
||||
SETPREF("jsdec.asm", "false", "if true, shows pseudo next to the assembly.");
|
||||
SETPREF("jsdec.blocks", "false", "if true, shows only scopes blocks.");
|
||||
SETPREF("jsdec.casts", "false", "if false, hides all casts in the pseudo code.");
|
||||
SETPREF("jsdec.debug", "false", "do not catch exceptions in jsdec.");
|
||||
SETPREF("jsdec.highlight", "default", "highlights the current address.");
|
||||
SETPREF("jsdec.paddr", "false", "if true, all xrefs uses physical addresses compare.");
|
||||
SETPREF("jsdec.slow", "false", "load all the data before to avoid multirequests to rizin.");
|
||||
SETPREF("jsdec.xrefs", "false", "if true, shows all xrefs in the pseudo code.");
|
||||
rz_config_lock(cfg, true);
|
||||
|
||||
RzCmdDesc *pdd = rz_cmd_desc_group_new(rcmd, root_cd, "pdd", name_handler(pdd), &name_help(pdd), &pdd_usage);
|
||||
if (!pdd) {
|
||||
rz_warn_if_reached();
|
||||
return false;
|
||||
}
|
||||
|
||||
rz_cmd_desc_argv_new_warn(rcmd, pdd, pddt);
|
||||
rz_cmd_desc_argv_new_warn(rcmd, pdd, pdda);
|
||||
rz_cmd_desc_argv_new_warn(rcmd, pdd, pddA);
|
||||
rz_cmd_desc_argv_new_warn(rcmd, pdd, pddb);
|
||||
rz_cmd_desc_argv_new_warn(rcmd, pdd, pddc);
|
||||
rz_cmd_desc_argv_new_warn(rcmd, pdd, pddf);
|
||||
rz_cmd_desc_argv_new_warn(rcmd, pdd, pddi);
|
||||
rz_cmd_desc_argv_new_warn(rcmd, pdd, pddj);
|
||||
rz_cmd_desc_argv_new_warn(rcmd, pdd, pddo);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool rz_cmd_pdd_fini(RzCore *core) {
|
||||
RzCmd *rcmd = core->rcmd;
|
||||
RzCmdDesc *cd = rz_cmd_get_desc(rcmd, "pdd");
|
||||
return rz_cmd_desc_remove(rcmd, cd);
|
||||
}
|
||||
|
||||
RzCorePlugin rz_core_plugin_jsdec = {
|
||||
.name = "jsdec",
|
||||
.author = "deroad",
|
||||
.desc = "Pseudo-code decompiler for rizin",
|
||||
.license = "BSD-3-Clause",
|
||||
.init = rz_cmd_pdd_init,
|
||||
.fini = rz_cmd_pdd_fini,
|
||||
};
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#define _RZ_API __declspec(dllexport)
|
||||
#else
|
||||
#define _RZ_API
|
||||
#endif
|
||||
|
||||
#ifndef CORELIB
|
||||
_RZ_API RzLibStruct rizin_plugin = {
|
||||
.type = RZ_LIB_TYPE_CORE,
|
||||
.data = &rz_core_plugin_jsdec,
|
||||
.version = RZ_VERSION,
|
||||
};
|
||||
#endif
|
||||
File diff suppressed because it is too large.
Load diff
@@ -1,203 +0,0 @@
|
||||
/*
|
||||
* Minimal 'console' binding.
|
||||
*
|
||||
* https://github.com/DeveloperToolsWG/console-object/blob/master/api.md
|
||||
* https://developers.google.com/web/tools/chrome-devtools/debug/console/console-reference
|
||||
* https://developer.mozilla.org/en/docs/Web/API/console
|
||||
*/
|
||||
|
||||
#ifndef USE_RZCONS
|
||||
#define USE_RZCONS 1
|
||||
#endif
|
||||
#if USE_RZCONS
|
||||
#include <rz_cons.h>
|
||||
#endif
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdarg.h>
|
||||
#include "duktape.h"
|
||||
#include "duk_console.h"
|
||||
|
||||
#include "../jsdec_ctx.h"
|
||||
|
||||
/* XXX: Add some form of log level filtering. */
|
||||
|
||||
/* XXX: Should all output be written via e.g. console.write(formattedMsg)?
|
||||
* This would make it easier for user code to redirect all console output
|
||||
* to a custom backend.
|
||||
*/
|
||||
|
||||
/* XXX: Init console object using duk_def_prop() when that call is available. */
|
||||
|
||||
static duk_ret_t duk__console_log_helper(duk_context *ctx, const char *error_name) {
|
||||
duk_idx_t n = duk_get_top(ctx);
|
||||
duk_idx_t i;
|
||||
|
||||
duk_get_global_string(ctx, "console");
|
||||
duk_get_prop_string(ctx, -1, "format");
|
||||
|
||||
for (i = 0; i < n; i++) {
|
||||
if (duk_check_type_mask(ctx, i, DUK_TYPE_MASK_OBJECT)) {
|
||||
/* Slow path formatting. */
|
||||
duk_dup(ctx, -1); /* console.format */
|
||||
duk_dup(ctx, i);
|
||||
duk_call(ctx, 1);
|
||||
duk_replace(ctx, i); /* arg[i] = console.format(arg[i]); */
|
||||
}
|
||||
}
|
||||
|
||||
duk_pop_2(ctx);
|
||||
|
||||
duk_push_string(ctx, " ");
|
||||
duk_insert(ctx, 0);
|
||||
duk_join(ctx, n);
|
||||
|
||||
if (error_name) {
|
||||
duk_push_error_object(ctx, DUK_ERR_ERROR, "%s", duk_require_string(ctx, -1));
|
||||
duk_push_string(ctx, "name");
|
||||
duk_push_string(ctx, error_name);
|
||||
duk_def_prop(ctx, -3, DUK_DEFPROP_FORCE | DUK_DEFPROP_HAVE_VALUE); /* to get e.g. 'Trace: 1 2 3' */
|
||||
duk_get_prop_string(ctx, -1, "stack");
|
||||
}
|
||||
|
||||
#if USE_RZCONS
|
||||
JsDecCtx *jsctx = jsdec_ctx_get (ctx);
|
||||
rz_cons_sleep_end (jsctx->bed);
|
||||
rz_cons_strcat (duk_to_string (ctx, -1));
|
||||
rz_cons_newline ();
|
||||
jsctx->bed = rz_cons_sleep_begin ();
|
||||
#else
|
||||
duk_uint_t flags = (duk_uint_t) duk_get_current_magic(ctx);
|
||||
FILE *output = (flags & DUK_CONSOLE_STDOUT_ONLY) ? stdout : stderr;
|
||||
|
||||
fprintf(output, "%s\n", duk_to_string(ctx, -1));
|
||||
if (flags & DUK_CONSOLE_FLUSH) {
|
||||
fflush(output);
|
||||
}
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
static duk_ret_t duk__console_assert(duk_context *ctx) {
|
||||
if (duk_to_boolean(ctx, 0)) {
|
||||
return 0;
|
||||
}
|
||||
duk_remove(ctx, 0);
|
||||
|
||||
return duk__console_log_helper(ctx, "AssertionError");
|
||||
}
|
||||
|
||||
static duk_ret_t duk__console_log(duk_context *ctx) {
|
||||
return duk__console_log_helper(ctx, NULL);
|
||||
}
|
||||
|
||||
static duk_ret_t duk__console_trace(duk_context *ctx) {
|
||||
return duk__console_log_helper(ctx, "Trace");
|
||||
}
|
||||
|
||||
static duk_ret_t duk__console_info(duk_context *ctx) {
|
||||
return duk__console_log_helper(ctx, NULL);
|
||||
}
|
||||
|
||||
static duk_ret_t duk__console_warn(duk_context *ctx) {
|
||||
return duk__console_log_helper(ctx, NULL);
|
||||
}
|
||||
|
||||
static duk_ret_t duk__console_error(duk_context *ctx) {
|
||||
return duk__console_log_helper(ctx, "Error");
|
||||
}
|
||||
|
||||
static duk_ret_t duk__console_dir(duk_context *ctx) {
|
||||
/* For now, just share the formatting of .log() */
|
||||
return duk__console_log_helper(ctx, 0);
|
||||
}
|
||||
|
||||
static void duk__console_reg_vararg_func(duk_context *ctx, duk_c_function func, const char *name, duk_uint_t flags) {
|
||||
duk_push_c_function(ctx, func, DUK_VARARGS);
|
||||
duk_push_string(ctx, "name");
|
||||
duk_push_string(ctx, name);
|
||||
duk_def_prop(ctx, -3, DUK_DEFPROP_HAVE_VALUE | DUK_DEFPROP_FORCE); /* Improve stacktraces by displaying function name */
|
||||
duk_set_magic(ctx, -1, (duk_int_t) flags);
|
||||
duk_put_prop_string(ctx, -2, name);
|
||||
}
|
||||
|
||||
void duk_console_init(duk_context *ctx, duk_uint_t flags) {
|
||||
duk_uint_t flags_orig;
|
||||
|
||||
/* If both DUK_CONSOLE_STDOUT_ONLY and DUK_CONSOLE_STDERR_ONLY where specified,
|
||||
* just turn off DUK_CONSOLE_STDOUT_ONLY and keep DUK_CONSOLE_STDERR_ONLY.
|
||||
*/
|
||||
if ((flags & DUK_CONSOLE_STDOUT_ONLY) && (flags & DUK_CONSOLE_STDERR_ONLY)) {
|
||||
flags &= ~DUK_CONSOLE_STDOUT_ONLY;
|
||||
}
|
||||
/* Remember the (possibly corrected) flags we received. */
|
||||
flags_orig = flags;
|
||||
|
||||
duk_push_object(ctx);
|
||||
|
||||
/* Custom function to format objects; user can replace.
|
||||
* For now, try JX-formatting and if that fails, fall back
|
||||
* to ToString(v).
|
||||
*/
|
||||
duk_eval_string(ctx,
|
||||
"(function (E) {"
|
||||
"return function format(v){"
|
||||
"try{"
|
||||
"return E('jx',v);"
|
||||
"}catch(e){"
|
||||
"return String(v);" /* String() allows symbols, ToString() internal algorithm doesn't. */
|
||||
"}"
|
||||
"};"
|
||||
"})(Duktape.enc)");
|
||||
duk_put_prop_string(ctx, -2, "format");
|
||||
|
||||
flags = flags_orig;
|
||||
if (!(flags & DUK_CONSOLE_STDOUT_ONLY) && !(flags & DUK_CONSOLE_STDERR_ONLY)) {
|
||||
/* No output indicators were specified; these levels go to stdout. */
|
||||
flags |= DUK_CONSOLE_STDOUT_ONLY;
|
||||
}
|
||||
duk__console_reg_vararg_func(ctx, duk__console_assert, "assert", flags);
|
||||
duk__console_reg_vararg_func(ctx, duk__console_log, "log", flags);
|
||||
duk__console_reg_vararg_func(ctx, duk__console_log, "debug", flags); /* alias to console.log */
|
||||
duk__console_reg_vararg_func(ctx, duk__console_trace, "trace", flags);
|
||||
duk__console_reg_vararg_func(ctx, duk__console_info, "info", flags);
|
||||
|
||||
flags = flags_orig;
|
||||
if (!(flags & DUK_CONSOLE_STDOUT_ONLY) && !(flags & DUK_CONSOLE_STDERR_ONLY)) {
|
||||
/* No output indicators were specified; these levels go to stderr. */
|
||||
flags |= DUK_CONSOLE_STDERR_ONLY;
|
||||
}
|
||||
duk__console_reg_vararg_func(ctx, duk__console_warn, "warn", flags);
|
||||
duk__console_reg_vararg_func(ctx, duk__console_error, "error", flags);
|
||||
duk__console_reg_vararg_func(ctx, duk__console_error, "exception", flags); /* alias to console.error */
|
||||
duk__console_reg_vararg_func(ctx, duk__console_dir, "dir", flags);
|
||||
|
||||
duk_put_global_string(ctx, "console");
|
||||
|
||||
/* Proxy wrapping: ensures any undefined console method calls are
|
||||
* ignored silently. This was required specifically by the
|
||||
* DeveloperToolsWG proposal (and was implemented also by Firefox:
|
||||
* https://bugzilla.mozilla.org/show_bug.cgi?id=629607). This is
|
||||
* apparently no longer the preferred way of implementing console.
|
||||
* When Proxy is enabled, whitelist at least .toJSON() to avoid
|
||||
* confusing JX serialization of the console object.
|
||||
*/
|
||||
|
||||
if (flags & DUK_CONSOLE_PROXY_WRAPPER) {
|
||||
/* Tolerate failure to initialize Proxy wrapper in case
|
||||
* Proxy support is disabled.
|
||||
*/
|
||||
(void) duk_peval_string_noresult(ctx,
|
||||
"(function(){"
|
||||
"var D=function(){};"
|
||||
"var W={toJSON:true};" /* whitelisted */
|
||||
"console=new Proxy(console,{"
|
||||
"get:function(t,k){"
|
||||
"var v=t[k];"
|
||||
"return typeof v==='function'||W[k]?v:D;"
|
||||
"}"
|
||||
"});"
|
||||
"})();"
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,29 +0,0 @@
|
||||
#if !defined(DUK_CONSOLE_H_INCLUDED)
|
||||
#define DUK_CONSOLE_H_INCLUDED
|
||||
|
||||
#include "duktape.h"
|
||||
|
||||
#if defined(__cplusplus)
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* Use a proxy wrapper to make undefined methods (console.foo()) no-ops. */
|
||||
#define DUK_CONSOLE_PROXY_WRAPPER (1U << 0)
|
||||
|
||||
/* Flush output after every call. */
|
||||
#define DUK_CONSOLE_FLUSH (1U << 1)
|
||||
|
||||
/* Send output to stdout only (default is mixed stdout/stderr). */
|
||||
#define DUK_CONSOLE_STDOUT_ONLY (1U << 2)
|
||||
|
||||
/* Send output to stderr only (default is mixed stdout/stderr). */
|
||||
#define DUK_CONSOLE_STDERR_ONLY (1U << 3)
|
||||
|
||||
/* Initialize the console system */
|
||||
extern void duk_console_init(duk_context *ctx, duk_uint_t flags);
|
||||
|
||||
#if defined(__cplusplus)
|
||||
}
|
||||
#endif /* end 'extern "C"' wrapper */
|
||||
|
||||
#endif /* DUK_CONSOLE_H_INCLUDED */
|
||||
@@ -1,11 +0,0 @@
|
||||
#ifndef DUKTAPE_MISSING_FUNCTIONS
|
||||
#define DUKTAPE_MISSING_FUNCTIONS
|
||||
|
||||
#define duk_eval_file(ctx,src) \
|
||||
((void) duk_eval_raw((ctx), (src), 0, 1 /*args*/ | DUK_COMPILE_EVAL | DUK_COMPILE_NOSOURCE | DUK_COMPILE_STRLEN))
|
||||
|
||||
#define duk_eval_file_noresult(ctx,src) \
|
||||
((void) duk_eval_raw((ctx), (src), 0, 1 /*args*/ | DUK_COMPILE_EVAL | DUK_COMPILE_NOSOURCE | DUK_COMPILE_STRLEN | DUK_COMPILE_NORESULT))
|
||||
|
||||
|
||||
#endif
|
||||
File diff suppressed because it is too large.
Load diff
-99905
File diff suppressed because it is too large.
Load diff
-1450
File diff suppressed because it is too large.
Load diff
@@ -1,18 +0,0 @@
|
||||
// SPDX-FileCopyrightText: 2018-2021 Giovanni Dante Grazioli <[email protected]>
|
||||
// SPDX-License-Identifier: BSD-3-Clause
|
||||
|
||||
#ifndef JSDEC_CTX_H
|
||||
#define JSDEC_CTX_H
|
||||
|
||||
#include <duktape.h>
|
||||
|
||||
typedef struct rz_core_t RzCore;
|
||||
|
||||
typedef struct jsdec_ctx_t {
|
||||
RzCore *core;
|
||||
void *bed;
|
||||
} JsDecCtx;
|
||||
|
||||
JsDecCtx *jsdec_ctx_get(duk_context *ctx);
|
||||
|
||||
#endif //JSDEC_CTX_H
|
||||
@@ -1,18 +0,0 @@
|
||||
// SPDX-FileCopyrightText: 2021 Giovanni Dante Grazioli <[email protected]>
|
||||
// SPDX-License-Identifier: BSD-3-Clause
|
||||
|
||||
typedef struct {
|
||||
const char *name;
|
||||
const char *code;
|
||||
} RzJSC;
|
||||
|
||||
@JSC_SOURCES@
|
||||
|
||||
static const char *jsdec_jsc(const char *fullname) {
|
||||
for (ut32 i = 0; i < RZ_JSC_SIZE; ++i) {
|
||||
if (!strcmp(fullname, rz_jsc_file[i].name)) {
|
||||
return rz_jsc_file[i].code;
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
@@ -1,46 +0,0 @@
|
||||
# SPDX-FileCopyrightText: 2021 Giovanni Dante Grazioli <[email protected]>
|
||||
# SPDX-License-Identifier: BSD-3-Clause
|
||||
|
||||
import sys
|
||||
import glob
|
||||
import os
|
||||
import json
|
||||
|
||||
def const_var_name(name):
|
||||
return 'jsc_' + name.replace(os.sep, '_').replace('.', '_').replace('-', '_')
|
||||
|
||||
def main(argc, argv):
|
||||
if argc != 2:
|
||||
print("usage: {} <path/to/js/files>".format(argv[0]))
|
||||
sys.exit(1)
|
||||
path = os.path.join(argv[1])
|
||||
path_len = len(path)
|
||||
|
||||
js_files = glob.glob(os.path.join(path, '**/*.js'), recursive=True)
|
||||
js_files.remove(os.path.join(path, 'jsdec-test.js'))
|
||||
|
||||
for file in js_files:
|
||||
vname = const_var_name(file.replace(path, ""))
|
||||
code = ''
|
||||
count = 0
|
||||
with open(file, "rb") as f:
|
||||
raw = f.read()
|
||||
for byte in raw:
|
||||
if count > 0 and count % 32 == 0:
|
||||
code += "\n\t"
|
||||
code += "{}, ".format(byte)
|
||||
count += 1
|
||||
print('const unsigned char ' + vname + '[' + str(count + 1) + '] = {\n\t' + code + ' 0\n};\n')
|
||||
|
||||
print('\n#define RZ_JSC_SIZE ({})\n\n'.format(len(js_files)))
|
||||
print('const RzJSC rz_jsc_file[RZ_JSC_SIZE] = {')
|
||||
for file in js_files:
|
||||
name = file.replace(path, "").replace(os.sep, '/')
|
||||
if name.startswith("/"):
|
||||
name = name[1:]
|
||||
vname = const_var_name(file.replace(path, ""))
|
||||
print('\t{ .name = "' + name + '", .code = (const char *)' + vname + ' },')
|
||||
print('};')
|
||||
|
||||
if __name__ == '__main__':
|
||||
main(len(sys.argv), sys.argv)
|
||||
@@ -1,71 +0,0 @@
|
||||
project('jsdec', 'c', meson_version: '>=0.55.0')
|
||||
|
||||
pyth = import('python').find_installation()
|
||||
cc = meson.get_compiler('c')
|
||||
|
||||
incs = ['.', 'duktape']
|
||||
deps = []
|
||||
c_args = []
|
||||
|
||||
rz_core_dep = dependency('rz_core')
|
||||
deps += rz_core_dep
|
||||
deps += dependency('rz_util')
|
||||
deps += dependency('rz_cons')
|
||||
deps += dependency('rz_config')
|
||||
deps += dependency('rz_io')
|
||||
deps += cc.find_library('m', required: false)
|
||||
|
||||
rizin_plugdir = get_option('rizin_plugdir')
|
||||
if rizin_plugdir == ''
|
||||
rizin_plugdir = rz_core_dep.get_variable(pkgconfig: 'plugindir', cmake: 'rz_core_PLUGINDIR')
|
||||
plugin_jsdec_dir = join_paths(get_option('prefix'), rizin_plugdir, 'jsdec')
|
||||
else
|
||||
plugin_jsdec_dir = join_paths(rizin_plugdir, 'jsdec')
|
||||
endif
|
||||
|
||||
|
||||
files = [
|
||||
'core_pdd.c',
|
||||
'duktape/duktape.c',
|
||||
'duktape/duk_console.c'
|
||||
]
|
||||
|
||||
c_args += ('-DJSDEC_HOME="' + plugin_jsdec_dir + '"')
|
||||
|
||||
jsc_folder = get_option('jsc_folder')
|
||||
if jsc_folder != ''
|
||||
c_args += '-DUSE_JSC'
|
||||
generator_jsc = files('make_jsc.py')
|
||||
rc = run_command(pyth, generator_jsc, jsc_folder, check: true)
|
||||
if rc.returncode() != 0
|
||||
error('failed to generate jsc files.')
|
||||
endif
|
||||
jsdec_jsc = configuration_data()
|
||||
jsdec_jsc.set('JSC_SOURCES', rc.stdout())
|
||||
rz_version_h = configure_file(
|
||||
input: 'jsdec_jsc.c.in',
|
||||
output: 'jsdec_jsc.c',
|
||||
configuration: jsdec_jsc,
|
||||
)
|
||||
endif
|
||||
|
||||
message('Rizin Plugin Dir: ' + plugin_jsdec_dir)
|
||||
if jsc_folder != ''
|
||||
message('JS to C Folder: ' + jsc_folder)
|
||||
endif
|
||||
|
||||
shared_library('core_pdd', files,
|
||||
c_args : c_args,
|
||||
dependencies: deps,
|
||||
include_directories: include_directories(incs),
|
||||
implicit_include_directories: false,
|
||||
install: true,
|
||||
install_dir: rizin_plugdir
|
||||
)
|
||||
|
||||
if jsc_folder == ''
|
||||
install_subdir(join_paths('..', 'libdec'), install_dir: plugin_jsdec_dir)
|
||||
install_subdir(join_paths('..', 'themes'), install_dir: plugin_jsdec_dir)
|
||||
install_data(join_paths('..', 'jsdec-duk.js'), install_dir: plugin_jsdec_dir)
|
||||
install_data(join_paths('..', 'require.js'), install_dir: plugin_jsdec_dir)
|
||||
endif
|
||||
@@ -1,2 +0,0 @@
|
||||
option('rizin_plugdir', type: 'string', value: '', description: 'rizin install directory')
|
||||
option('jsc_folder', type: 'string', value: '', description: 'When a path is defined, the js code, found in its subfolders, is embedded into the C code.')
|
||||
@@ -0,0 +1,4 @@
|
||||
[wrap-git]
|
||||
url = https://github.com/frida/quickjs.git
|
||||
revision = c81f05c9859cea5f83a80057416a0c7affe9b876
|
||||
patch_directory = libquickjs
|
||||
@@ -0,0 +1,107 @@
|
||||
project('quickjs', 'c',
|
||||
version: '2021-03-27-frida',
|
||||
)
|
||||
|
||||
cc = meson.get_compiler('c')
|
||||
|
||||
have_msvc = cc.get_id() == 'msvc'
|
||||
have_emscripten = cc.get_define('EMSCRIPTEN') != ''
|
||||
|
||||
threads_dep = dependency('threads')
|
||||
dl_dep = cc.find_library('dl', required: false)
|
||||
m_dep = cc.find_library('m', required: false)
|
||||
|
||||
headers = [
|
||||
'quickjs.h',
|
||||
]
|
||||
|
||||
sources = [
|
||||
'quickjs.c',
|
||||
'libregexp.c',
|
||||
'libunicode.c',
|
||||
'cutils.c',
|
||||
'libbf.c',
|
||||
]
|
||||
|
||||
cdata = configuration_data()
|
||||
cdata.set_quoted('CONFIG_VERSION', meson.project_version())
|
||||
|
||||
if host_machine.endian() == 'big'
|
||||
cdata.set('WORDS_BIGENDIAN', 1)
|
||||
endif
|
||||
|
||||
if cc.has_header('unistd.h')
|
||||
cdata.set('HAVE_UNISTD_H', 1)
|
||||
endif
|
||||
|
||||
if cc.has_function('malloc_usable_size')
|
||||
cdata.set('HAVE_MALLOC_USABLE_SIZE', 1)
|
||||
endif
|
||||
|
||||
if get_option('libc')
|
||||
headers += 'quickjs-libc.h'
|
||||
sources += 'quickjs-libc.c'
|
||||
endif
|
||||
|
||||
cdata.set('CONFIG_BIGNUM', get_option('bignum'))
|
||||
|
||||
atomics = false
|
||||
opt = get_option('atomics')
|
||||
if not opt.disabled()
|
||||
supported = not (have_msvc or have_emscripten)
|
||||
if opt.auto()
|
||||
atomics = supported
|
||||
elif supported
|
||||
atomics = true
|
||||
else
|
||||
error('Atomics support was requested but is not available')
|
||||
endif
|
||||
endif
|
||||
cdata.set('CONFIG_ATOMICS', atomics)
|
||||
|
||||
stack_check = false
|
||||
opt = get_option('stack_check')
|
||||
if not opt.disabled()
|
||||
supported = not (have_msvc or have_emscripten)
|
||||
if opt.auto()
|
||||
stack_check = supported
|
||||
elif supported
|
||||
stack_check = true
|
||||
else
|
||||
error('Stack limitation was requested but is not available')
|
||||
endif
|
||||
endif
|
||||
cdata.set('CONFIG_STACK_CHECK', stack_check)
|
||||
|
||||
install_headers(headers, subdir: 'quickjs')
|
||||
|
||||
configure_file(
|
||||
output: 'config.h',
|
||||
configuration: cdata,
|
||||
)
|
||||
add_project_arguments(
|
||||
have_msvc ? '/FI' : '-include', meson.current_build_dir() / 'config.h',
|
||||
'-D_GNU_SOURCE=1',
|
||||
language: 'c',
|
||||
)
|
||||
|
||||
quickjs = library('quickjs', sources,
|
||||
dependencies: [threads_dep, dl_dep, m_dep],
|
||||
install: true,
|
||||
)
|
||||
quickjs_dep = declare_dependency(
|
||||
link_with: quickjs,
|
||||
include_directories: include_directories('.'),
|
||||
)
|
||||
|
||||
qjsc_sources = [
|
||||
'qjsc.c',
|
||||
'quickjs-libc.c',
|
||||
]
|
||||
|
||||
qjsc = executable('qjsc', qjsc_sources,
|
||||
dependencies: quickjs_dep,
|
||||
include_directories: include_directories('.'),
|
||||
implicit_include_directories: false,
|
||||
install: false,
|
||||
)
|
||||
@@ -0,0 +1,3 @@
|
||||
[wrap-git]
|
||||
https://github.com/frida/quickjs.git
|
||||
revision = c81f05c9859cea5f83a80057416a0c7affe9b876
|
||||
File renamed without changes.
@@ -0,0 +1,62 @@
|
||||
// SPDX-FileCopyrightText: 2023 Giovanni Dante Grazioli <[email protected]>
|
||||
// SPDX-License-Identifier: BSD-3-Clause
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
const char search[] = "const uint8_t qjsc_";
|
||||
|
||||
int main(int argc, char const *argv[]) {
|
||||
if (argc != 3) {
|
||||
printf("usage %s <bytecode.h> <bytecode_mod.h>\n", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
FILE *input = NULL, *output = NULL;
|
||||
char line[1024];
|
||||
|
||||
input = fopen(argv[1], "rb");
|
||||
if (!input) {
|
||||
printf("Error opening input file %s\n", argv[1]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
output = fopen(argv[2], "wb");
|
||||
if (!output) {
|
||||
printf("Error opening output file %s\n", argv[1]);
|
||||
fclose(input);
|
||||
return 0;
|
||||
}
|
||||
|
||||
fprintf(output, "#ifndef BYTECODE_MOD_H\n");
|
||||
fprintf(output, "#define BYTECODE_MOD_H\n\n");
|
||||
fprintf(output, "/* generated from '%s' */\n\n", argv[1]);
|
||||
fprintf(output, "static inline int js_load_all_modules(JSContext *ctx) {\n");
|
||||
fprintf(output, "\treturn ");
|
||||
int first = 1;
|
||||
|
||||
size_t search_size = sizeof(search) - 1;
|
||||
while (fgets(line, sizeof(line), input)) {
|
||||
if (strncmp(line, search, search_size)) {
|
||||
continue;
|
||||
} else if (!first) {
|
||||
fprintf(output, " &&\n\t\t");
|
||||
}
|
||||
|
||||
char *p = strchr(line + search_size, '[');
|
||||
*p = 0;
|
||||
|
||||
p = line + search_size;
|
||||
fprintf(output, "js_add_module(ctx, qjsc_%s, qjsc_%s_size)", p, p);
|
||||
first = 0;
|
||||
}
|
||||
if (first) {
|
||||
fprintf(output, "1");
|
||||
}
|
||||
fprintf(output, ";\n}\n\n");
|
||||
fprintf(output, "#endif /* BYTECODE_MOD_H */\n");
|
||||
|
||||
fclose(input);
|
||||
fclose(output);
|
||||
return 0;
|
||||
}
|
||||
Reference in new issue
Block a user