radareorg / radare2

UNIX-like reverse engineering framework and command-line toolset
https://www.radare.org/
GNU Lesser General Public License v3.0
20.34k stars 2.97k forks source link

loading a project doesn't restore some evals #13888

Closed a3desu closed 5 years ago

a3desu commented 5 years ago

I'm using r2 to work with avr.

Every time I load a project saved with 'PS' command I have to set asm.cpu by hands even if it present in project file's evals.

Steps to reproduce: $ r2 -a avr any_file

e asm.cpu ATmega8 e asm.cpu=ATmega328 e asm.cpu ATmega328 Ps test_project q

Next load saved project: $ r2 -p test_project

e asm.cpu ATmega8

$ grep asm.cpu ~/.local/share/radare2/projects/test_project/rc "e asm.cpu = ATmega328"

a3desu commented 5 years ago

I investigated futher a bit. Looks like that have no means with "evals" and tightly coupled to asm.cpu config

Here some snippets from libr/core/cconfig.c

    char *asm_cpu = strdup (r_config_get (core->config, "asm.cpu"));
    if (core->assembler->cur) {
        const char *newAsmCPU = core->assembler->cur->cpus;
        if (newAsmCPU) {
            if (*newAsmCPU) {
                char *nac = strdup (newAsmCPU);
                char *comma = strchr (nac, ',');
                if (comma) {
                    *comma = 0;
                    r_config_set (core->config, "asm.cpu", nac);
                }
                free (nac);
            } else {
                r_config_set (core->config, "asm.cpu", "");
            }
        }

Here config(asm.cpu) read to *asm_cpu, and then set to asm plugin default (#0 in cpus).

    r_asm_set_cpu (core->assembler, asm_cpu);
    free (asm_cpu);
    RConfigNode *asmcpu = r_config_node_get (core->config, "asm.cpu");

Here r_asm_cpu set to previously read value. Note that config value differs now. So *asmcpu contains "default" ATmega8 in case of AVR.

I created some dirty patch to test and fix issue. Don't know whether I need to create pull requset.

diff --git a/libr/core/cconfig.c b/libr/core/cconfig.c
index 69f06d25a..3a437e12c 100644
--- a/libr/core/cconfig.c
+++ b/libr/core/cconfig.c
@@ -498,10 +498,16 @@ static int cb_asmarch(void *user, void *data) {
                if (newAsmCPU) {
                        if (*newAsmCPU) {
                                char *nac = strdup (newAsmCPU);
-                               char *comma = strchr (nac, ',');
-                               if (comma) {
-                                       *comma = 0;
-                                       r_config_set (core->config, "asm.cpu", nac);
+                               if(asm_cpu) {
+                                       if(!strstr(nac, asm_cpu)) {
+                                               // set default cpu if asm.cpu is not set or
+                                               // not present in arch cpus
+                                               char *comma = strchr (nac, ',');
+                                               if (comma) {
+                                                       *comma = 0;
+                                                       r_config_set (core->config, "asm.cpu", nac);
+                                               }
+                                       }
                                }
                                free (nac);
                        } else {
radare commented 5 years ago

Yes please, create a PR

On 25 Apr 2019, at 13:47, Наташа notifications@github.com wrote:

I investigated futher a bit. Looks like that have no means with "evals" and tightly coupled to asm.cpu config

Here some snippets from libr/core/cconfig.c

char asm_cpu = strdup (r_config_get (core->config, "asm.cpu")); if (core->assembler->cur) { const char newAsmCPU = core->assembler->cur->cpus; if (newAsmCPU) { if (newAsmCPU) { char nac = strdup (newAsmCPU); char comma = strchr (nac, ','); if (comma) { comma = 0; r_config_set (core->config, "asm.cpu", nac); } free (nac); } else { r_config_set (core->config, "asm.cpu", ""); } } Here config(asm.cpu) read to *asm_cpu, and then set to asm plugin default (#0 in cpus).

r_asm_set_cpu (core->assembler, asm_cpu); free (asm_cpu); RConfigNode asmcpu = r_config_node_get (core->config, "asm.cpu"); Here r_asm_cpu set to previously read value. Note that config value differs now. So asmcpu contains "default" ATmega8 in case of AVR.

I created some dirty patch to test and fix issue. Don't know whether I need to create pull requset.

diff --git a/libr/core/cconfig.c b/libr/core/cconfig.c index 69f06d25a..3a437e12c 100644 --- a/libr/core/cconfig.c +++ b/libr/core/cconfig.c @@ -498,10 +498,16 @@ static int cb_asmarch(void user, void data) { if (newAsmCPU) { if (newAsmCPU) { char nac = strdup (newAsmCPU);

  • char *comma = strchr (nac, ',');
  • if (comma) {
  • *comma = 0;
  • r_config_set (core->config, "asm.cpu", nac);
  • if(asm_cpu) {
  • if(!strstr(nac, asm_cpu)) {
  • // set default cpu if asm.cpu is not set or
  • // not present in arch cpus
  • char *comma = strchr (nac, ',');
  • if (comma) {
  • *comma = 0;
  • r_config_set (core->config, "asm.cpu", nac);
  • }
  • } } free (nac); } else { — You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or mute the thread.