Skip to content

1after909-doit-73591e: recover break/continue for loop-exit gotos (loopbreak_recovery, DIV-7) - #55

Merged
mahaloz merged 4 commits into
mainfrom
feat/angr-1after909-doit-73591e
Jun 25, 2026
Merged

mahaloz merged 4 commits into
mainfrom
feat/angr-1after909-doit-73591e

Conversation

@mahaloz

@mahaloz mahaloz commented Jun 25, 2026 •

Copy link
Copy Markdown
Contributor

loopbreak_recovery: recover break; for loop-exit gotos (port of Ghidra scopeBreak)

Closes the angr-vs-kuna gap on test_decompiling_1after909_doit::doit. angr renders the
command-processing while loop with 0 gotos / 0 labels — every error/quit path is a
structured break;. kuna emitted 10 gotos / 2 labels, nine of them goto label_239f; to
the loop's shared cleanup successor (the semantic break;) plus a synthesized label_239f:.

See analysis.md and the side-by-side angr-vs-kuna.txt.

Mechanism

angr's Phoenix/SAILR structurer runs loop-successor refinement (break/continue recovery). The
upstream equivalent is Ghidra BlockGraph::scopeBreak(-1,-1), called in ActionFinalStructure
between finalizePrinting and markUnstructured — which kuna's port had left an explicit SEAM
stub (docs/rust-port/losses.md).

This PR ports it as a new module s8_structure/kuna_loopbreak_recovery.rs: a single recursive
walk over the already-collapsed structured tree (BlockGraph/BlockGoto/BlockIf/BlockSwitch/
BlockWhileDo/BlockDoWhile/BlockInfLoop/BlockMultiGoto/BlockCondition), carrying curexit
(the fall-through block) and curloopexit (the innermost loop's successor). A goto whose target
equals curloopexit is retagged f_goto_goto → f_break_goto. The printer already emits break;
for that flag, and markUnstructured (run after) then suppresses the now-dead successor label.
BlockId equality is the faithful analog of the C++ getIndex() identity. It only flips
gototype flags on already-structured nodes — no CFG/SSA/type change.

The whole pass is gated by Architecture::recover_loop_break, called from
ActionFinalStructure::apply; the only ported-core edits are that one gated call plus the
flag/option wiring.

Option

option loopbreak_recovery on|off (ElementId 4091). DIV-7 default-on;
option loopbreak_recovery off restores the byte-identical raw-goto rendering.

Ablation / parity

  • 0 of 675 datatest assertions change with the flip on — make test stays PARITY OK
    without regeneration.
  • make rust-test (cargo unit + golden differential + .sla parity) green with the flip on.
  • Speed +0.12% (440.2 → 440.7 ms median on doit, within the 5% budget).
  • Clean on all gates, and it converges kuna toward upstream Ghidra (which runs scopeBreak
    unconditionally) ⇒ shipped default-on (DIV-7).

On doit the nine goto label_239f; become break; and label_239f: disappears (10/2 → 1/1
gotos/labels; the lone remaining goto label_1dca is a forward jump into the loop head, a
separate structural gap, correctly left untouched).

Stage test: tests/stages/ghangr-1after909-doit-73591e.xml (two-pass off/on).

🤖 Generated with Claude Code


Before / after — doit in 1after909

Real captured kuna output on the function this feature was built for, with option loopbreak_recovery flipped. Ported from angr — test_decompiling_1after909_doit; Phoenix/SAILR loop-successor break/continue recovery (Ghidra BlockGraph::scopeBreak); doit.

metric loopbreak_recovery off loopbreak_recovery on (default)
gotos 10 1 ⬇️
labels 2 1 ⬇️
loc 258 257 ⬇️
switches 0 0
speed 440.22 ms 440.73 ms — Δ 0.12% (budget 5.0%)
kuna — before (option loopbreak_recovery off)
unsigned long doit(void)

{
  int8 v1;
  char *v10; // rax
  int8 v11; // fs_offset
  int4 v12;
  void *v13; // stack - 0x103a50
  char v14; // stack - 0x101d68
  char v2; // al
  int4 v3; // eax
  int4 v4; // eax
  int4 v5; // eax
  unsigned long v6; // rax
  void *v7; // rax
  unsigned long v8; // rax
  uint8 v9; // rax
  
  v1 = *(int8 *)(v11 + 0x28);
  strcpy(PTRSUB(RSP,-0x100818),s_203020);
  v6 = strdup("fc6b25dc07595b11f307d79cca713a332b784cbf2e52e6e30a372e7bd4935ebd");
  puts("1 AFTER 909: ASCII MUSIC LYRICS PREPARATION SERVICE");
  fflush(dat_203848);
  for (v12 = 0; v12 <= 0x38c; v12 = v12 + 1) {
    v13 = (void *)0x0;
  }
  printf("Enter username: ");
  fflush(dat_203848);
  v12 = read_str(PTRSUB(RSP,-0x101de8),0x40);
  if (v12 <= -1) {
    puts("Failed to parse username.");
    fflush(dat_203848);
    v6 = 0xffffffff;
  }
  else if (v12 == 0) {
    puts("String is empty.");
    fflush(dat_203848);
    v6 = 0xffffffff;
  }
  else {
    printf("Enter password: ");
    fflush(dat_203848);
    v12 = read_str(PTRSUB(RSP,-0x101da8),0x40);
    if (v12 <= -1) {
      puts("Failed to parse password.");
      fflush(dat_203848);
      v6 = 0xffffffff;
    }
    else if (v12 == 0) {
      puts("String is empty.");
      fflush(dat_203848);
      v6 = 0xffffffff;
    }
    else {
      printf("Enter document id: ");
      fflush(dat_203848);
      v12 = read_str(PTRSUB(RSP,-0x101d68),0x40);
      if (v12 <= -1) {
        puts("Failed to parse document id.");
        fflush(dat_203848);
        v6 = 0xffffffff;
      }
      else if (v14 == '\0') {
        v12 = 0;
        while( true ) {
          sprintf(PTRSUB(RSP,-0x101d68),"%lx",&PTRSUB(RSP,-0x101d68)[v12]);
          create_filename(PTRSUB(RSP,-0x1014a8),0x48e,PTRSUB(RSP,-0x101de8),PTRSUB(RSP,-0x101da8),PTRSUB(RSP,-0x101d68));
          v3 = access(PTRSUB(RSP,-0x1014a8),0);
          if (v3 == -1) break;
          v12 = v12 + 1;
        }
        printf("Created new document id: %s\n",PTRSUB(RSP,-0x101d68));
        fflush(dat_203848);
        v7 = fopen(PTRSUB(RSP,-0x1014a8),"a+");
        if (v7 == (void *)0x0) {
          puts("Cannot create document.");
          fflush(dat_203848);
          v6 = 0xffffffff;
        }
        else {
          printf("Enter title: ");
          fflush(dat_203848);
          read_str(PTRSUB(RSP,-0x101ca8),0x400);
          fprintf(v7,"%s\n---\n",PTRSUB(RSP,-0x101ca8));
          fflush(v7);
label_1dca:
          v12 = 0;
          while (v12 <= 0x38e) {
            printf("\n%s","Enter command: ");
            fflush(dat_203848);
            do {
              v3 = read_str(PTRSUB(RSP,-0x101d28),0x40);
            } while (v3 <= 0);
            v3 = strcmp(PTRSUB(RSP,-0x101d28),"quit");
            if (v3 == 0) goto label_239f;
            v3 = strcmp(PTRSUB(RSP,-0x101d28),"list");
            if (v3 == 0) {
              printf("This command requires admin privileges. Please authenticate: ");
              fflush(dat_203848);
              v3 = read_str(PTRSUB(RSP,-0x101ce8),0x40);
              if (v3 <= -1) {
                puts("Error reading string.");
                goto label_239f;
              }
              v3 = verify_password(PTRSUB(RSP,-0x101ce8),v6);
              if (v3 == 0) {
                puts("Admin access granted.");
                fflush(dat_203848);
                v8 = popen("/bin/ls",0x28ff);
                while (v2 = fgetc(v8), v2 != '\xff') {
                  putchar((int4)v2);
                }
                fflush(dat_203848);
              }
              else {
                puts("Credentials verification failed.");
              }
            }
            else {
              v3 = strcmp(PTRSUB(RSP,-0x101d28),"text");
              if (v3 == 0) {
                printf("Enter text: ");
                fflush(dat_203848);
                v3 = read_str(PTRSUB(RSP,-0x101018),0x800);
                if (v3 <= -1) {
                  puts("Error reading string.");
                  goto label_239f;
                }
                v9 = strlen(PTRSUB(RSP,-0x101018));
                fwrite(PTRSUB(RSP,-0x101018),1,v9,v7);
                fwrite(0x291b,1,1,v7);
                v12 = v12 + 1;
              }
              else {
                v3 = strcmp(PTRSUB(RSP,-0x101d28),"image");
                if (v3 == 0) {
                  printf("Enter image width: ");
                  fflush(dat_203848);
                  v3 = read_str(PTRSUB(RSP,-0x101018),0x800);
                  if (v3 <= -1) {
                    puts("Error reading string.");
                    goto label_239f;
                  }
                  v3 = atoi(PTRSUB(RSP,-0x101018));
                  printf("Enter image height: ");
                  fflush(dat_203848);
                  v4 = read_str(PTRSUB(RSP,-0x101018),0x800);
                  if (v4 <= -1) {
                    puts("Error reading string.");
                    goto label_239f;
                  }
                  v4 = atoi(PTRSUB(RSP,-0x101018));
                  if ((v3 == 0) || (v4 == 0)) {
                    puts("Illegal width or height");
                    goto label_239f;
                  }
                  puts("Enter JPEG data");
                  fflush(dat_203848);
                  v5 = read_bin(PTRSUB(RSP,-0x100018),0x100000);
                  if (v5 <= 0) {
                    puts("Could not receive image.");
                    goto label_239f;
                  }
                  v10 = (char *)convert(PTRSUB(RSP,-0x100018),v5,PTRSUB(RSP,-0x100818),v3,v4);
                  if (v10 == (char *)0x0) {
                    puts("Image conversion failed.");
                    goto label_239f;
                  }
                  fwrite("\n-------------------\n",1,0x15,v7);
                  v9 = strlen(v10);
                  fwrite(v10,1,v9,v7);
                  fwrite("\n-------------------\n",1,0x15,v7);
                  *(char **)&PTRSUB(RSP,-0x103a58)[(int8)v12 * 8] = v10;
                  v12 = v12 + 1;
                }
                else {
                  v3 = strcmp(PTRSUB(RSP,-0x101d28),"print");
                  if (v3 == 0) {
                    fseek(v7,0,0);
                    do {
                      v3 = fread(PTRSUB(RSP,-0x1018a8),1,0x400,v7);
                      fwrite(PTRSUB(RSP,-0x1018a8),1,(int8)v3,dat_203848);
                    } while (0x400 <= v3);
                  }
                  else {
                    v3 = strcmp(PTRSUB(RSP,-0x101d28),"palette");
                    if (v3 == 0) {
                      puts("Enter palette data");
                      fflush(dat_203848);
                      v3 = read_bin(PTRSUB(RSP,-0x100818),0x800);
                      if (v3 <= 0) {
                        puts("Failed to read palette.");
                        goto label_239f;
                      }
                      PTRSUB(RSP,-0x100818)[v3] = 0;
                    }
                    else {
                      fflush(dat_203848);
                    }
                  }
                }
              }
            }
          }
label_239f:
          fflush(dat_203848);
          for (v12 = 0; v12 <= 0x38c; v12 = v12 + 1) {
            if (v13 != (void *)0x0) {
              free(v13);
            }
          }
          fclose(v7);
          puts("Bye!");
          fflush(dat_203848);
          v6 = 0;
        }
      }
      else {
        create_filename(PTRSUB(RSP,-0x1014a8),0x48e,PTRSUB(RSP,-0x101de8),PTRSUB(RSP,-0x101da8),PTRSUB(RSP,-0x101d68));
        v12 = access(PTRSUB(RSP,-0x1014a8),0);
        if (v12 == 0) {
          v7 = fopen(PTRSUB(RSP,-0x1014a8),"a+");
          if (v7 == (void *)0x0) {
            puts("Cannot open document.");
            fflush(dat_203848);
            v6 = 0xffffffff;
          }
          else {
            printf("Title: ");
            fflush(dat_203848);
            fseek(v7,0,0);
            do {
              v2 = fgetc(v7);
              putchar((int4)v2);
              if (v2 == '\n') {
                fflush(dat_203848);
                fseek(v7,0,2);
                goto label_1dca;
              }
            } while (v2 != '\xff');
            puts("Empty title");
            fflush(dat_203848);
            v6 = 0xffffffff;
          }
        }
        else {
          puts("A document with the specified id does not exist.");
          fflush(dat_203848);
          sleep(1);
          v6 = 0xffffffff;
        }
      }
    }
  }
  if (v1 == *(int8 *)(v11 + 0x28)) {
    return v6;
  }
                    /* WARNING: Subroutine does not return */
  __stack_chk_fail();
}
kuna — after (option loopbreak_recovery on)
unsigned long doit(void)

{
  int8 v1;
  char *v10; // rax
  int8 v11; // fs_offset
  int4 v12;
  void *v13; // stack - 0x103a50
  char v14; // stack - 0x101d68
  char v2; // al
  int4 v3; // eax
  int4 v4; // eax
  int4 v5; // eax
  unsigned long v6; // rax
  void *v7; // rax
  unsigned long v8; // rax
  uint8 v9; // rax
  
  v1 = *(int8 *)(v11 + 0x28);
  strcpy(PTRSUB(RSP,-0x100818),s_203020);
  v6 = strdup("fc6b25dc07595b11f307d79cca713a332b784cbf2e52e6e30a372e7bd4935ebd");
  puts("1 AFTER 909: ASCII MUSIC LYRICS PREPARATION SERVICE");
  fflush(dat_203848);
  for (v12 = 0; v12 <= 0x38c; v12 = v12 + 1) {
    v13 = (void *)0x0;
  }
  printf("Enter username: ");
  fflush(dat_203848);
  v12 = read_str(PTRSUB(RSP,-0x101de8),0x40);
  if (v12 <= -1) {
    puts("Failed to parse username.");
    fflush(dat_203848);
    v6 = 0xffffffff;
  }
  else if (v12 == 0) {
    puts("String is empty.");
    fflush(dat_203848);
    v6 = 0xffffffff;
  }
  else {
    printf("Enter password: ");
    fflush(dat_203848);
    v12 = read_str(PTRSUB(RSP,-0x101da8),0x40);
    if (v12 <= -1) {
      puts("Failed to parse password.");
      fflush(dat_203848);
      v6 = 0xffffffff;
    }
    else if (v12 == 0) {
      puts("String is empty.");
      fflush(dat_203848);
      v6 = 0xffffffff;
    }
    else {
      printf("Enter document id: ");
      fflush(dat_203848);
      v12 = read_str(PTRSUB(RSP,-0x101d68),0x40);
      if (v12 <= -1) {
        puts("Failed to parse document id.");
        fflush(dat_203848);
        v6 = 0xffffffff;
      }
      else if (v14 == '\0') {
        v12 = 0;
        while( true ) {
          sprintf(PTRSUB(RSP,-0x101d68),"%lx",&PTRSUB(RSP,-0x101d68)[v12]);
          create_filename(PTRSUB(RSP,-0x1014a8),0x48e,PTRSUB(RSP,-0x101de8),PTRSUB(RSP,-0x101da8),PTRSUB(RSP,-0x101d68));
          v3 = access(PTRSUB(RSP,-0x1014a8),0);
          if (v3 == -1) break;
          v12 = v12 + 1;
        }
        printf("Created new document id: %s\n",PTRSUB(RSP,-0x101d68));
        fflush(dat_203848);
        v7 = fopen(PTRSUB(RSP,-0x1014a8),"a+");
        if (v7 == (void *)0x0) {
          puts("Cannot create document.");
          fflush(dat_203848);
          v6 = 0xffffffff;
        }
        else {
          printf("Enter title: ");
          fflush(dat_203848);
          read_str(PTRSUB(RSP,-0x101ca8),0x400);
          fprintf(v7,"%s\n---\n",PTRSUB(RSP,-0x101ca8));
          fflush(v7);
label_1dca:
          v12 = 0;
          while (v12 <= 0x38e) {
            printf("\n%s","Enter command: ");
            fflush(dat_203848);
            do {
              v3 = read_str(PTRSUB(RSP,-0x101d28),0x40);
            } while (v3 <= 0);
            v3 = strcmp(PTRSUB(RSP,-0x101d28),"quit");
            if (v3 == 0) break;
            v3 = strcmp(PTRSUB(RSP,-0x101d28),"list");
            if (v3 == 0) {
              printf("This command requires admin privileges. Please authenticate: ");
              fflush(dat_203848);
              v3 = read_str(PTRSUB(RSP,-0x101ce8),0x40);
              if (v3 <= -1) {
                puts("Error reading string.");
                break;
              }
              v3 = verify_password(PTRSUB(RSP,-0x101ce8),v6);
              if (v3 == 0) {
                puts("Admin access granted.");
                fflush(dat_203848);
                v8 = popen("/bin/ls",0x28ff);
                while (v2 = fgetc(v8), v2 != '\xff') {
                  putchar((int4)v2);
                }
                fflush(dat_203848);
              }
              else {
                puts("Credentials verification failed.");
              }
            }
            else {
              v3 = strcmp(PTRSUB(RSP,-0x101d28),"text");
              if (v3 == 0) {
                printf("Enter text: ");
                fflush(dat_203848);
                v3 = read_str(PTRSUB(RSP,-0x101018),0x800);
                if (v3 <= -1) {
                  puts("Error reading string.");
                  break;
                }
                v9 = strlen(PTRSUB(RSP,-0x101018));
                fwrite(PTRSUB(RSP,-0x101018),1,v9,v7);
                fwrite(0x291b,1,1,v7);
                v12 = v12 + 1;
              }
              else {
                v3 = strcmp(PTRSUB(RSP,-0x101d28),"image");
                if (v3 == 0) {
                  printf("Enter image width: ");
                  fflush(dat_203848);
                  v3 = read_str(PTRSUB(RSP,-0x101018),0x800);
                  if (v3 <= -1) {
                    puts("Error reading string.");
                    break;
                  }
                  v3 = atoi(PTRSUB(RSP,-0x101018));
                  printf("Enter image height: ");
                  fflush(dat_203848);
                  v4 = read_str(PTRSUB(RSP,-0x101018),0x800);
                  if (v4 <= -1) {
                    puts("Error reading string.");
                    break;
                  }
                  v4 = atoi(PTRSUB(RSP,-0x101018));
                  if ((v3 == 0) || (v4 == 0)) {
                    puts("Illegal width or height");
                    break;
                  }
                  puts("Enter JPEG data");
                  fflush(dat_203848);
                  v5 = read_bin(PTRSUB(RSP,-0x100018),0x100000);
                  if (v5 <= 0) {
                    puts("Could not receive image.");
                    break;
                  }
                  v10 = (char *)convert(PTRSUB(RSP,-0x100018),v5,PTRSUB(RSP,-0x100818),v3,v4);
                  if (v10 == (char *)0x0) {
                    puts("Image conversion failed.");
                    break;
                  }
                  fwrite("\n-------------------\n",1,0x15,v7);
                  v9 = strlen(v10);
                  fwrite(v10,1,v9,v7);
                  fwrite("\n-------------------\n",1,0x15,v7);
                  *(char **)&PTRSUB(RSP,-0x103a58)[(int8)v12 * 8] = v10;
                  v12 = v12 + 1;
                }
                else {
                  v3 = strcmp(PTRSUB(RSP,-0x101d28),"print");
                  if (v3 == 0) {
                    fseek(v7,0,0);
                    do {
                      v3 = fread(PTRSUB(RSP,-0x1018a8),1,0x400,v7);
                      fwrite(PTRSUB(RSP,-0x1018a8),1,(int8)v3,dat_203848);
                    } while (0x400 <= v3);
                  }
                  else {
                    v3 = strcmp(PTRSUB(RSP,-0x101d28),"palette");
                    if (v3 == 0) {
                      puts("Enter palette data");
                      fflush(dat_203848);
                      v3 = read_bin(PTRSUB(RSP,-0x100818),0x800);
                      if (v3 <= 0) {
                        puts("Failed to read palette.");
                        break;
                      }
                      PTRSUB(RSP,-0x100818)[v3] = 0;
                    }
                    else {
                      fflush(dat_203848);
                    }
                  }
                }
              }
            }
          }
          fflush(dat_203848);
          for (v12 = 0; v12 <= 0x38c; v12 = v12 + 1) {
            if (v13 != (void *)0x0) {
              free(v13);
            }
          }
          fclose(v7);
          puts("Bye!");
          fflush(dat_203848);
          v6 = 0;
        }
      }
      else {
        create_filename(PTRSUB(RSP,-0x1014a8),0x48e,PTRSUB(RSP,-0x101de8),PTRSUB(RSP,-0x101da8),PTRSUB(RSP,-0x101d68));
        v12 = access(PTRSUB(RSP,-0x1014a8),0);
        if (v12 == 0) {
          v7 = fopen(PTRSUB(RSP,-0x1014a8),"a+");
          if (v7 == (void *)0x0) {
            puts("Cannot open document.");
            fflush(dat_203848);
            v6 = 0xffffffff;
          }
          else {
            printf("Title: ");
            fflush(dat_203848);
            fseek(v7,0,0);
            do {
              v2 = fgetc(v7);
              putchar((int4)v2);
              if (v2 == '\n') {
                fflush(dat_203848);
                fseek(v7,0,2);
                goto label_1dca;
              }
            } while (v2 != '\xff');
            puts("Empty title");
            fflush(dat_203848);
            v6 = 0xffffffff;
          }
        }
        else {
          puts("A document with the specified id does not exist.");
          fflush(dat_203848);
          sleep(1);
          v6 = 0xffffffff;
        }
      }
    }
  }
  if (v1 == *(int8 *)(v11 + 0x28)) {
    return v6;
  }
                    /* WARNING: Subroutine does not return */
  __stack_chk_fail();
}
angr reference (the goal, v9.2.213)
typedef struct FILE {
} FILE;

extern FILE *stdout;

unsigned int doit(void)
{
    char v0;  // [bp-0x103a95]
    int iter;  // [bp-0x103a94], Other Possible Types: unsigned int
    unsigned int v2;  // [bp-0x103a90]
    int cur;  // [bp-0x103a8c]
    unsigned int v4;  // [bp-0x103a88]
    unsigned int v5;  // [bp-0x103a84]
    unsigned int l;  // [bp-0x103a80]
    unsigned int k;  // [bp-0x103a7c]
    unsigned int v8;  // [bp-0x103a78]
    unsigned int v9;  // [bp-0x103a74]
    FILE *fp;  // [bp-0x103a70], Other Possible Types: unsigned long long
    void* p;  // [bp-0x103a68], Other Possible Types: unsigned long long
    FILE *v12;  // [bp-0x103a60], Other Possible Types: unsigned long long
    char v13;  // [bp-0x103a58]
    void* v14;  // [bp-0x103a50]
    unsigned long str;  // [bp-0x101df0]
    char v16;  // [bp-0x101de8]
    char v17;  // [bp-0x101da8]
    char v18[64];  // [bp-0x101d68]
    char v19[64];  // [bp-0x101d28]
    char v20;  // [bp-0x101ce8]
    char v21[1024];  // [bp-0x101ca8]
    char v22;  // [bp-0x1018a8]
    char v23[1168];  // [bp-0x1014a8]
    char v24[2048];  // [bp-0x101018]
    char v25[2048];  // [bp-0x100818]
    char v26;  // [bp-0x100018]

    fp = 0;
    v4 = 0;
    p = 0;
    v5 = 0;
    l = 0;
    iter = 0;
    v12 = 0;
    strcpy(&v25, "   ...',;:clodxkO0KXNWM");
    str = strdup("fc6b25dc07595b11f307d79cca713a332b784cbf2e52e6e30a372e7bd4935ebd");
    puts("1 AFTER 909: ASCII MUSIC LYRICS PREPARATION SERVICE");
    fflush(stdout);
    for (cur = 0; cur <= 908; cur += 1)
    {
        v14 = NULL;
    }
    printf("Enter username: ");
    fflush(stdout);
    k = read_str(&v16, 64);
    if (k < 0)
    {
        puts("Failed to parse username.");
        fflush(stdout);
        return 4294967295;
    }
    else if (!k)
    {
        puts("String is empty.");
        fflush(stdout);
        return 4294967295;
    }
    else
    {
        printf("Enter password: ");
        fflush(stdout);
        k = read_str(&v17, 64);
        if (k < 0)
        {
            puts("Failed to parse password.");
            fflush(stdout);
            return 4294967295;
        }
        else if (!k)
        {
            puts("String is empty.");
            fflush(stdout);
            return 4294967295;
        }
        else
        {
            printf("Enter document id: ");
            fflush(stdout);
            k = read_str(&v18, 64);
            if (k < 0)
            {
                puts("Failed to parse document id.");
                fflush(stdout);
                return 4294967295;
            }
            if (!v18)
            {
                v2 = 0;
                while (true)
                {
                    sprintf(&v18, "%lx", (char [64])&v18[v2]);
                    create_filename(&v23, 1166, &v16, &v17, &v18);
                    if (access(&v23, 0) == 4294967295)
                        break;
                    v2 += 1;
                }
                printf("Created new document id: %s\n", &v18);
                fflush(stdout);
                fp = fopen(&v23, "a+");
                if (!fp)
                {
                    puts("Cannot create document.");
                    fflush(stdout);
                    return 4294967295;
                }
                printf("Enter title: ");
                fflush(stdout);
                k = read_str(&v21, 0x400);
                fprintf(fp, "%s\n---\n", &v21);
                fflush(fp);
            }
            else
            {
                create_filename(&v23, 1166, &v16, &v17, &v18);
                if (access(&v23, 0))
                {
                    puts("A document with the specified id does not exist.");
                    fflush(stdout);
                    sleep(1);
                    return 4294967295;
                }
                fp = fopen(&v23, "a+");
                if (!fp)
                {
                    puts("Cannot open document.");
                    fflush(stdout);
                    return 4294967295;
                }
                printf("Title: ");
                fflush(stdout);
                fseek(fp, 0, 0);
                while (true)
                {
                    v0 = fgetc(fp);
                    putchar(v0);
                    if (v0 == 10)
                        break;
                    if (v0 == 255)
                    {
                        puts("Empty title");
                        fflush(stdout);
                        return 4294967295;
                    }
                }
                fflush(stdout);
                fseek(fp, 0, 2);
            }
            iter = 0;
            while (iter <= 910)
            {
                printf("\n%s", "Enter command: ");
                fflush(stdout);
                do
                {
                    k = read_str(&v19, 64);
                } while (k <= 0);
                if (!strcmp(&v19, "quit"))
                    break;
                if (!strcmp(&v19, "list"))
                {
                    printf("This command requires admin privileges. Please authenticate: ");
                    fflush(stdout);
                    k = read_str(&v20, 64);
                    if (k < 0)
                    {
                        puts("Error reading string.");
                        break;
                    }
                    else
                    {
                        if ((int)verify_password(&v20, str, str))
                        {
                            puts("Credentials verification failed.");
                        }
                        else
                        {
                            puts("Admin access granted.");
                            fflush(stdout);
                            v12 = popen("/bin/ls", "r");
                            while (true)
                            {
                                v0 = fgetc(v12);
                                if (v0 == 255)
                                    break;
                                putchar(v0);
                            }
                            fflush(stdout);
                        }
                    }
                }
                else if (!strcmp(&v19, "text"))
                {
                    printf("Enter text: ");
                    fflush(stdout);
                    k = read_str(&v24, 0x800);
                    if (k < 0)
                    {
                        puts("Error reading string.");
                        break;
                    }
                    else
                    {
                        fwrite(&v24, 1, strlen(&v24), fp);
                        fwrite("\n", 1, 1, fp);
                        iter += 1;
                    }
                }
                else if (!strcmp(&v19, "image"))
                {
                    printf("Enter image width: ");
                    fflush(stdout);
                    k = read_str(&v24, 0x800);
                    if (k < 0)
                    {
                        puts("Error reading string.");
                        break;
                    }
                    else
                    {
                        v8 = atoi(&v24);
                        printf("Enter image height: ");
                        fflush(stdout);
                        k = read_str(&v24, 0x800);
                        if (k < 0)
                        {
                            puts("Error reading string.");
                            break;
                        }
                        else
                        {
                            v9 = atoi(&v24);
                            if (!v8 || !v9)
                            {
                                puts("Illegal width or height");
                                break;
                            }
                            else
                            {
                                puts("Enter JPEG data");
                                fflush(stdout);
                                v5 = read_bin(&v26, 0x100000);
                                if (v5 <= 0)
                                {
                                    puts("Could not receive image.");
                                    break;
                                }
                                else
                                {
                                    p = convert(&v26, v5, &v25, v8, v9);
                                    if (!p)
                                    {
                                        puts("Image conversion failed.");
                                        break;
                                    }
                                    else
                                    {
                                        fwrite("\n-------------------\n", 1, 21, fp);
                                        fwrite(p, 1, strlen(p), fp);
                                        fwrite("\n-------------------\n", 1, 21, fp);
                                        *((void* *)&(&v13)[8 * iter]) = p;
                                        iter += 1;
                                    }
                                }
                            }
                        }
                    }
                }
                else if (!strcmp(&v19, "print"))
                {
                    fseek(fp, 0, 0);
                    do
                    {
                        l = fread(&v22, 1, 0x400, fp);
                        fwrite(&v22, 1, l, stdout);
                    } while (l > 1023);
                }
                else if (!strcmp(&v19, "palette"))
                {
                    puts("Enter palette data");
                    fflush(stdout);
                    v4 = read_bin(&v25, 0x800);
                    if (v4 <= 0)
                    {
                        puts("Failed to read palette.");
                        break;
                    }
                    else
                    {
                        v25[v4] = 0;
                    }
                }
                else
                {
                    fflush(stdout);
                }
            }
            fflush(stdout);
            for (cur = 0; cur <= 908; cur += 1)
            {
                if (v14)
                    free(v14);
            }
            fclose(fp);
            puts("Bye!");
            fflush(stdout);
            return 0;
        }
    }
}

…t gotos (loopbreak_recovery)

angr renders 1after909::doit with 0 gotos; kuna emits 10 (9 are `goto label_239f`,
which are really `break;` out of the command loop). Closing this requires porting
Ghidra's BlockGraph::scopeBreak (a recorded SEAM stub in blockaction.rs / losses.md)
plus a printer change -- S8 structuring, LARGE per Hard Rule 7. Proposal only.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@mahaloz

mahaloz commented Jun 25, 2026

Copy link
Copy Markdown
Contributor Author

APPROVE

…tion loopbreak_recovery, DIV-7)

Port Ghidra BlockGraph::scopeBreak into a new s8_structure/kuna_loopbreak_recovery.rs
pass: a recursive walk over the structured tree that retags loop-exit (and
switch-exit) goto edges f_goto_goto -> f_break_goto, so the printer emits break;
instead of `goto <successor-label>;` + a synthesized label. Closes the angr gap on
test_decompiling_1after909_doit::doit (10/2 -> 1/1 gotos/labels).

Gated by Architecture::recover_loop_break (option loopbreak_recovery, ElementId 4091),
called from ActionFinalStructure::apply between finalizePrinting and markUnstructured.
Ablation clean (0/675 datatest assertions change; make rust-test green), speed +0.12%
-> shipped DIV-7 default-on. The printer already emitted break; for f_break_goto and
block.rs already had next_flow_after/get_exit_leaf, so no printc.rs or block.rs edit
was needed.

Tests: tests/stages/ghangr-1after909-doit-73591e.xml (two-pass off/on), baseline-stages
+2; kuna-catalog.xml provenance counts bumped for the new 24th settable.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@mahaloz
mahaloz marked this pull request as ready for review June 25, 2026 15:50
@mahaloz mahaloz changed the title [PROPOSAL] angr-1after909-doit-73591e: recover break/continue for loop-exit gotos (scopeBreak) 1after909-doit-73591e: recover break/continue for loop-exit gotos (loopbreak_recovery, DIV-7) Jun 25, 2026
mahaloz and others added 2 commits June 25, 2026 16:30
Union-resolve the divergence collisions for the loopbreak_recovery
structure-recovery option (DIV-7 on the branch, renumbered to DIV-10 since
main's max is DIV-9):
- divergences.md: keep main's DIV-7..9, add loopbreak as DIV-10
- architecture.rs / seams.rs: union recover_loop_break + fold_call_returns
  fields/ctors/resets/seam-copies; DIV-7 -> DIV-10 in comments
- loopbreakrecovery ElementId 4091 -> 4092 (4091 now held by dedupvardecls)
- stages.toml / catalog fixtures / counts: 43 settables, 91 surfaces;
  catalog provenance counts angr 9, structure-recovery 5
- regenerate assertions.md + baseline-stages.json (165/165)
- UPSTREAM.md / PROGRESS.md union

Gates: catalog OK, make test PARITY OK (675/675), make test-stages PARITY OK (165/165).

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01PBrckq3YYAzBLq5k4KpWac
…3591e (#55)

Re-merge after main advanced (PR #67 added the macho-arm64e loader-tier
settable). Union the count drift:
- 44 settables now (26 stage-model knobs + 15 analysis gates + 3 loader caps:
  relocobjects/i386_pie_plt/macho-arm64e), surface=91
- kuna_stages/tests.rs + catalog_bytecompat.rs count assertions -> 44;
  trailing-comma count -> 43 (last row now macho-arm64e)
- loopbreak_recovery (DIV-10) provenance unchanged; angr=9, structure-recovery=5
- regenerate stage_catalog fixture, assertions.md, baseline-stages.json (165/165)

Gates: catalog OK, make test PARITY OK (675/675), make test-stages PARITY OK (165/165).

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01PBrckq3YYAzBLq5k4KpWac
@mahaloz
mahaloz merged commit 7db9d17 into main Jun 25, 2026
mahaloz added a commit that referenced this pull request Jun 25, 2026
…57)

Union-resolve the registrations for the gotoreduce opt-in option (angr SAILR
ReturnDuplicator goto-reduction) against main, which had advanced with the #55
(loopbreak_recovery/DIV-10) and #67 (macho-arm64e) settables:
- architecture.rs / seams.rs / options.rs: union reduce_return_gotos with
  recover_loop_break/fold_call_returns fields, ctors, resets, set_kuna_option
  arms, KUNA_OPTION_NAMES, seam-copies
- s8_structure/mod.rs: union kuna_gotoreduce + kuna_loopbreak_recovery modules
- stages.toml: union the gotoreduce settable; 45 settables now
  (27 stage-model + 15 analysis + 3 loader caps), surface=91
- gotoreduce ElementId 4100 (no collision: dedup 4091, loopbreak 4092)
- catalog provenance counts: angr 10, structure-recovery 6 (gotoreduce both)
- count assertions (kuna_stages/tests.rs, catalog_bytecompat.rs, xml.rs corpus
  137 files) bumped; regenerate stage_catalog fixture, assertions.md,
  baseline-stages.json (168/168)
- gotoreduce is opt-in (default-off) -> no DIV entry

Gates: catalog OK, make test PARITY OK (675/675), make test-stages PARITY OK (168/168).

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01PBrckq3YYAzBLq5k4KpWac
mahaloz added a commit that referenced this pull request Jun 25, 2026
Region-structurer foundation (Inc 0) — analysis-only, no new --option. Main had
advanced with #55 (loopbreak_recovery/DIV-10), #57 (gotoreduce), #67 (macho-arm64e);
the only conflict was docs/baseline-stages.json (data_footer counts), all other
region code (s7_regions/kuna_regiongraph.rs, kuna_regionid.rs, kuna_console.rs) and
the kuna-regions.xml assertions merged cleanly. Regenerated assertions.md (45
settables, unchanged) and baseline-stages.json (171/171, incl. the 8 KUNA-REGIONS
assertions plus main's gotoreduce/loopbreak/catalog ones).

Gates: catalog OK, make test PARITY OK (675/675), make test-stages PARITY OK (171/171).
Region unit tests: regionid 6/6, regiongraph 18/18 (incl. new build_from_block_graph_*).

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01PBrckq3YYAzBLq5k4KpWac
@mahaloz
mahaloz deleted the feat/angr-1after909-doit-73591e branch July 29, 2026 08:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant