Rizin
unix-like reverse engineering framework and cli tools
core_java.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2021 deroad <wargio@libero.it>
2 // SPDX-License-Identifier: LGPL-3.0-only
3 
4 #include <rz_types.h>
5 #include <rz_lib.h>
6 #include <rz_demangler.h>
7 #include <rz_cmd.h>
8 #include <rz_core.h>
9 #include <rz_cons.h>
10 #include <string.h>
11 #include <rz_analysis.h>
12 
13 #include "../format/java/class_bin.h"
14 
15 #define name_args(name) (cmd_##name##_args)
16 #define name_help(name) (cmd_##name##_help)
17 #define name_handler(name) (rz_cmd_##name##_handler)
18 #define static_description_without_args(command, summ) \
19  static const RzCmdDescArg name_args(command)[] = { \
20  { 0 }, \
21  }; \
22  static const RzCmdDescHelp name_help(command) = { \
23  .summary = summ, \
24  .args = name_args(command), \
25  }
26 #define rz_cmd_desc_argv_modes_new_warn(rcmd, root, cmd, flags) \
27  rz_warn_if_fail(rz_cmd_desc_argv_modes_new(rcmd, root, #cmd, flags, name_handler(cmd), &name_help(cmd)))
28 
29 #define rz_cmd_desc_argv_new_warn(rcmd, root, cmd) \
30  rz_warn_if_fail(rz_cmd_desc_argv_new(rcmd, root, #cmd, name_handler(cmd), &name_help(cmd)))
31 
33  if (!core) {
34  return NULL;
35  }
36  RzAnalysis *analysis = core->analysis;
37  if (!analysis || !analysis->binb.bin) {
38  return NULL;
39  }
40  RzBin *b = analysis->binb.bin;
41  if (!b->cur || !b->cur->o) {
42  return NULL;
43  }
44  RzBinPlugin *plugin = b->cur->o->plugin;
45  return plugin && !strcmp(plugin->name, "java") ? (RzBinJavaClass *)b->cur->o->bin_obj : NULL;
46 }
47 
49  if (argc != 1) {
51  }
52 
54  if (!jclass) {
55  return RZ_CMD_STATUS_ERROR;
56  }
57 
58  if (mode & RZ_OUTPUT_MODE_JSON) {
59  PJ *pj = pj_new();
60  if (!pj) {
61  return RZ_CMD_STATUS_ERROR;
62  }
63 
66  pj_free(pj);
67  } else {
68  RzStrBuf *sb = rz_strbuf_new("");
69  if (!sb) {
70  return RZ_CMD_STATUS_ERROR;
71  }
72 
74  rz_cons_print(rz_strbuf_get(sb));
76  }
77 
78  return RZ_CMD_STATUS_OK;
79 }
80 
82  if (argc != 1) {
84  }
85 
87  if (!jclass) {
88  return RZ_CMD_STATUS_ERROR;
89  }
90 
91  if (mode & RZ_OUTPUT_MODE_JSON) {
92  PJ *pj = pj_new();
93  if (!pj) {
94  return RZ_CMD_STATUS_ERROR;
95  }
96 
99  pj_free(pj);
100  } else {
101  RzStrBuf *sb = rz_strbuf_new("");
102  if (!sb) {
103  return RZ_CMD_STATUS_ERROR;
104  }
105 
107  rz_cons_print(rz_strbuf_get(sb));
109  }
110 
111  return RZ_CMD_STATUS_OK;
112 }
113 
115  if (argc != 1) {
117  }
118 
120  if (!jclass) {
121  return RZ_CMD_STATUS_ERROR;
122  }
123 
124  if (mode & RZ_OUTPUT_MODE_JSON) {
125  PJ *pj = pj_new();
126  if (!pj) {
127  return RZ_CMD_STATUS_ERROR;
128  }
129 
132  pj_free(pj);
133  } else {
134  RzStrBuf *sb = rz_strbuf_new("");
135  if (!sb) {
136  return RZ_CMD_STATUS_ERROR;
137  }
138 
140  rz_cons_print(rz_strbuf_get(sb));
142  }
143 
144  return RZ_CMD_STATUS_OK;
145 }
146 
148  if (argc != 1) {
150  }
151 
153  if (!jclass) {
154  return RZ_CMD_STATUS_ERROR;
155  }
156 
157  if (mode & RZ_OUTPUT_MODE_JSON) {
158  PJ *pj = pj_new();
159  if (!pj) {
160  return RZ_CMD_STATUS_ERROR;
161  }
162 
165  pj_free(pj);
166  } else {
167  RzStrBuf *sb = rz_strbuf_new("");
168  if (!sb) {
169  return RZ_CMD_STATUS_ERROR;
170  }
171 
173  rz_cons_print(rz_strbuf_get(sb));
175  }
176 
177  return RZ_CMD_STATUS_OK;
178 }
179 
181  if (argc != 1) {
183  }
184 
186  if (!jclass) {
187  return RZ_CMD_STATUS_ERROR;
188  }
189 
190  if (mode & RZ_OUTPUT_MODE_JSON) {
191  PJ *pj = pj_new();
192  if (!pj) {
193  return RZ_CMD_STATUS_ERROR;
194  }
195 
198  pj_free(pj);
199  } else {
200  RzStrBuf *sb = rz_strbuf_new("");
201  if (!sb) {
202  return RZ_CMD_STATUS_ERROR;
203  }
204 
206  rz_cons_print(rz_strbuf_get(sb));
208  }
209 
210  return RZ_CMD_STATUS_OK;
211 }
212 
213 RZ_IPI RzCmdStatus rz_cmd_javas_handler(RzCore *core, int argc, const char **argv) {
214  if (argc != 1) {
216  }
217 
219  if (!jclass) {
220  return RZ_CMD_STATUS_ERROR;
221  }
222 
223  RzStrBuf *sb = rz_strbuf_new("");
224  if (!sb) {
225  return RZ_CMD_STATUS_ERROR;
226  }
227 
229  rz_cons_print(rz_strbuf_get(sb));
231  return RZ_CMD_STATUS_OK;
232 }
233 
234 RZ_IPI RzCmdStatus rz_cmd_javar_handler(RzCore *core, int argc, const char **argv) {
235  if (argc != 2) {
237  }
238 
240  if (!jclass) {
241  return RZ_CMD_STATUS_ERROR;
242  }
243 
244  st32 index = rz_num_math(core->num, argv[1]);
245 
246  if (index < 1) {
247  RZ_LOG_ERROR("can't resolve constant pool index %d\n", index);
248  return RZ_CMD_STATUS_INVALID;
249  }
250 
251  char *resolved = rz_bin_java_class_const_pool_resolve_index(jclass, index);
252  if (!resolved) {
253  RZ_LOG_ERROR("can't resolve constant pool index %d\n", index);
254  return RZ_CMD_STATUS_INVALID;
255  }
256 
257  char *demangled = rz_demangler_java(resolved);
258  if (demangled) {
259  rz_cons_println(demangled);
260  } else {
261  rz_cons_println(resolved);
262  }
263 
264  free(resolved);
265  free(demangled);
266  return RZ_CMD_STATUS_OK;
267 }
268 
269 static const RzCmdDescHelp java_usage = {
270  .summary = "Core plugin to visualize java class information",
271 };
272 
273 static_description_without_args(javac, "prints the class structure");
274 static_description_without_args(javaf, "prints the class fields");
275 static_description_without_args(javai, "prints the class interfaces");
276 static_description_without_args(javam, "prints the class methods");
277 static_description_without_args(javap, "prints the class constant pool");
278 static_description_without_args(javas, "prints the class like a java source code");
279 
280 static const RzCmdDescArg name_args(javar)[] = {
281  {
282  .name = "index",
283  .type = RZ_CMD_ARG_TYPE_NUM,
284  },
285  { 0 },
286 };
287 
288 static const RzCmdDescHelp name_help(javar) = {
289  .summary = "resolves the class constant pool value at a given index",
290  .args = name_args(javar),
291 };
292 
293 static bool rz_cmd_java_init_handler(RzCore *core) {
294  RzCmd *rcmd = core->rcmd;
296  if (!root_cd) {
297  return false;
298  }
299 
300  RzCmdDesc *java = rz_cmd_desc_group_new(rcmd, root_cd, "java", NULL, NULL, &java_usage);
301  if (!java) {
303  return false;
304  }
305 
311  rz_cmd_desc_argv_new_warn(rcmd, java, javas);
312  rz_cmd_desc_argv_new_warn(rcmd, java, javar);
313 
314  return true;
315 }
316 
318  .name = "java",
319  .desc = "Suite of java commands, type `java` for more info",
320  .license = "LGPL-3.0-only",
321  .author = "deroad",
322  .version = "1.0",
323  .init = rz_cmd_java_init_handler,
324 };
325 
326 #ifndef RZ_PLUGIN_INCORE
329  .data = &rz_core_plugin_java,
331 };
332 #endif
#define RZ_IPI
Definition: analysis_wasm.c:11
static SblHeader sb
Definition: bin_mbn.c:26
RZ_API void rz_bin_java_class_const_pool_as_json(RZ_NONNULL RzBinJavaClass *bin, RZ_NONNULL PJ *j)
Returns the class const pool in json format via PJ arg.
Definition: class_bin.c:1650
RZ_API void rz_bin_java_class_interfaces_as_text(RZ_NONNULL RzBinJavaClass *bin, RZ_NONNULL RzStrBuf *sb)
Returns the class interfaces as text via RzStrBuf arg.
Definition: class_bin.c:1898
RZ_API void rz_bin_java_class_as_json(RZ_NONNULL RzBinJavaClass *bin, RZ_NONNULL PJ *j)
Returns the class info as json.
Definition: class_bin.c:521
RZ_API void rz_bin_java_class_fields_as_json(RZ_NONNULL RzBinJavaClass *bin, RZ_NONNULL PJ *j)
Returns the fields in json format via PJ arg.
Definition: class_bin.c:1366
RZ_API RZ_OWN char * rz_bin_java_class_const_pool_resolve_index(RZ_NONNULL RzBinJavaClass *bin, st32 index)
Returns the string linked to the class const pool index.
Definition: class_bin.c:644
RZ_API void rz_bin_java_class_as_text(RZ_NONNULL RzBinJavaClass *bin, RZ_NONNULL RzStrBuf *sb)
Returns the class info as text.
Definition: class_bin.c:591
RZ_API void rz_bin_java_class_as_source_code(RZ_NONNULL RzBinJavaClass *bin, RZ_NONNULL RzStrBuf *sb)
Returns the class info as text source code.
Definition: class_bin.c:708
RZ_API void rz_bin_java_class_const_pool_as_text(RZ_NONNULL RzBinJavaClass *bin, RZ_NONNULL RzStrBuf *sb)
Returns the class const pool in text format via RzStrBuf arg.
Definition: class_bin.c:1605
RZ_API void rz_bin_java_class_interfaces_as_json(RZ_NONNULL RzBinJavaClass *bin, RZ_NONNULL PJ *j)
Returns the class interfaces as json via PJ arg.
Definition: class_bin.c:1928
RZ_API void rz_bin_java_class_methods_as_text(RZ_NONNULL RzBinJavaClass *bin, RZ_NONNULL RzStrBuf *sb)
Returns the methods in text format via RzStrBuf arg.
Definition: class_bin.c:1121
RZ_API void rz_bin_java_class_fields_as_text(RZ_NONNULL RzBinJavaClass *bin, RZ_NONNULL RzStrBuf *sb)
Returns the fields in text format via RzStrBuf arg.
Definition: class_bin.c:1318
RZ_API void rz_bin_java_class_methods_as_json(RZ_NONNULL RzBinJavaClass *bin, RZ_NONNULL PJ *j)
Returns the methods in json format via PJ arg.
Definition: class_bin.c:1169
RZ_API RzCmdDesc * rz_cmd_get_root(RzCmd *cmd)
Get the root command descriptor.
Definition: cmd_api.c:230
RZ_API RzCmdDesc * rz_cmd_desc_group_new(RzCmd *cmd, RzCmdDesc *parent, const char *name, RzCmdArgvCb cb, const RzCmdDescHelp *help, const RzCmdDescHelp *group_help)
Create a new command descriptor for a name that is used both as a group but that has a sub-command wi...
Definition: cmd_api.c:2233
RZ_API void rz_cons_println(const char *str)
Definition: cons.c:233
RZ_IPI RzCmdStatus rz_cmd_javar_handler(RzCore *core, int argc, const char **argv)
Definition: core_java.c:234
#define rz_cmd_desc_argv_new_warn(rcmd, root, cmd)
Definition: core_java.c:29
#define name_help(name)
Definition: core_java.c:16
static RzBinJavaClass * core_java_get_class(RzCore *core)
Definition: core_java.c:32
RZ_API RzLibStruct rizin_plugin
Definition: core_java.c:327
RZ_IPI RzCmdStatus rz_cmd_javas_handler(RzCore *core, int argc, const char **argv)
Definition: core_java.c:213
RZ_IPI RzCmdStatus rz_cmd_javai_handler(RzCore *core, int argc, const char **argv, RzOutputMode mode)
Definition: core_java.c:114
#define name_args(name)
Definition: core_java.c:15
RZ_IPI RzCmdStatus rz_cmd_javap_handler(RzCore *core, int argc, const char **argv, RzOutputMode mode)
Definition: core_java.c:81
static const RzCmdDescHelp java_usage
Definition: core_java.c:269
#define rz_cmd_desc_argv_modes_new_warn(rcmd, root, cmd, flags)
Definition: core_java.c:26
RZ_IPI RzCmdStatus rz_cmd_javam_handler(RzCore *core, int argc, const char **argv, RzOutputMode mode)
Definition: core_java.c:147
RZ_IPI RzCmdStatus rz_cmd_javac_handler(RzCore *core, int argc, const char **argv, RzOutputMode mode)
Definition: core_java.c:48
static bool rz_cmd_java_init_handler(RzCore *core)
Definition: core_java.c:293
RzCorePlugin rz_core_plugin_java
Definition: core_java.c:317
RZ_IPI RzCmdStatus rz_cmd_javaf_handler(RzCore *core, int argc, const char **argv, RzOutputMode mode)
Definition: core_java.c:180
#define static_description_without_args(command, summ)
Definition: core_java.c:18
#define RZ_API
#define NULL
Definition: cris-opc.c:27
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
const char int mode
Definition: ioapi.h:137
jobject jclass
Definition: jni.h:104
RZ_API RZ_OWN char * rz_demangler_java(RZ_NULLABLE const char *symbol)
Demangles java symbols.
Definition: demangler.c:38
static static fork const void static count static fd const char const char static newpath char char argv
Definition: sflib.h:40
#define rz_warn_if_reached()
Definition: rz_assert.h:29
enum rz_cmd_status_t RzCmdStatus
@ RZ_CMD_ARG_TYPE_NUM
Argument is a number.
Definition: rz_cmd.h:38
@ RZ_CMD_STATUS_OK
command handler exited in the right way
Definition: rz_cmd.h:24
@ RZ_CMD_STATUS_WRONG_ARGS
command handler could not handle the arguments passed to it
Definition: rz_cmd.h:25
@ RZ_CMD_STATUS_INVALID
command could not be executed (e.g. shell level error, bad expression, etc.)
Definition: rz_cmd.h:27
@ RZ_CMD_STATUS_ERROR
command handler had issues while running (e.g. allocation error, etc.)
Definition: rz_cmd.h:26
@ RZ_LIB_TYPE_CORE
Definition: rz_lib.h:83
#define RZ_LOG_ERROR(fmtstr,...)
Definition: rz_log.h:58
RZ_API ut64 rz_num_math(RzNum *num, const char *str)
Definition: unum.c:456
RZ_API PJ * pj_new(void)
Definition: pj.c:25
RZ_API const char * pj_string(PJ *pj)
Definition: pj.c:57
RZ_API void pj_free(PJ *j)
Definition: pj.c:34
RZ_API char * rz_strbuf_get(RzStrBuf *sb)
Definition: strbuf.c:321
RZ_API RzStrBuf * rz_strbuf_new(const char *s)
Definition: strbuf.c:8
RZ_API void rz_strbuf_free(RzStrBuf *sb)
Definition: strbuf.c:358
RzOutputMode
Enum to describe the way data are printed.
Definition: rz_types.h:38
@ RZ_OUTPUT_MODE_JSON
Definition: rz_types.h:40
@ RZ_OUTPUT_MODE_STANDARD
Definition: rz_types.h:39
#define st32
Definition: rz_types_base.h:12
#define RZ_VERSION
Definition: rz_version.h:8
#define b(i)
Definition: sha256.c:42
Definition: rz_pj.h:12
RzBinBind binb
Definition: rz_analysis.h:579
RzBin * bin
Definition: rz_bin.h:807
char * name
Definition: rz_bin.h:509
const char * summary
Definition: rz_cmd.h:289
const char * version
Definition: rz_core.h:128
const char * name
Definition: rz_core.h:124
RzCmd * rcmd
Definition: rz_core.h:319
RzAnalysis * analysis
Definition: rz_core.h:322
RzNum * num
Definition: rz_core.h:316