nested-array-field-bug.mod 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. MODULE NestedArrayFieldBug;
  2. (* Repro: a RECORD field that is a nested ARRAY OF ARRAY OF CHAR, accessed
  3. as `arr[i].field[j][k]`, is miscompiled by V3 -> segfault (rc 139).
  4. Observed 2026-10-05. Expected exit 42; observed 139.
  5. Found while adding Lower's procedure-formal name table (a module-level
  6. ARRAY OF RECORD with a `pname : ARRAY [0..31] OF ARRAY [0..63] OF
  7. CHAR` field): the V3-built compiler crashed (SIGABRT) on every input.
  8. Flattening the field to `ARRAY [0..2047] OF CHAR` fixed that crash.
  9. STATUS (2026-10-05):
  10. - The *stride* half is FIXED (commit v3-fix-arrayrec-stride):
  11. QbeGen.ArrBodyItems now uses SymTab.TypeSize for record/class/set
  12. array elements, so an ARRAY OF record has correct element size and
  13. flat field access works (see tests/t_recarr.mod, exit 42).
  14. - This nested case STILL crashes: a record element inside an array
  15. is emitted as a zero blob, and its nested-array field's
  16. sub-descriptors (`$arr_i_field_k`) are never emitted, so the
  17. field's pointers are null. Fixing it needs per-element
  18. RecStatics/RecItems in ArrBodyItems/ArrData.
  19. Compare with docs/wip/local-array-bug.mod (local arrays) and the
  20. Stage-A array-of-record stride fix (QbeGen.ElemSize). *)
  21. VAR ExitCode : INTEGER;
  22. CONST MaxName = 63; MaxProc = 4;
  23. TYPE PE = RECORD
  24. name : ARRAY [0..MaxName] OF CHAR;
  25. npar : CARDINAL;
  26. pname : ARRAY [0..3] OF ARRAY [0..MaxName] OF CHAR; (* nested *)
  27. END;
  28. VAR procs : ARRAY [0..MaxProc-1] OF PE;
  29. nProcs : CARDINAL;
  30. PROCEDURE Reset;
  31. BEGIN nProcs := 0 END Reset;
  32. PROCEDURE NoteProc (name : ARRAY OF CHAR);
  33. VAR i : CARDINAL;
  34. BEGIN
  35. IF nProcs >= MaxProc THEN RETURN END;
  36. i := 0;
  37. WHILE (i < MaxName) AND (name[i] # CHR(0)) DO
  38. procs[nProcs].name[i] := name[i]; INC(i)
  39. END;
  40. procs[nProcs].name[i] := CHR(0);
  41. procs[nProcs].npar := 0; INC(nProcs)
  42. END NoteProc;
  43. PROCEDURE NoteParam (name : ARRAY OF CHAR);
  44. VAR j, i : CARDINAL;
  45. BEGIN
  46. IF nProcs = 0 THEN RETURN END;
  47. j := procs[nProcs-1].npar;
  48. i := 0;
  49. WHILE (i < MaxName) AND (name[i] # CHR(0)) DO
  50. procs[nProcs-1].pname[j][i] := name[i]; INC(i)
  51. END;
  52. procs[nProcs-1].pname[j][i] := CHR(0);
  53. procs[nProcs-1].npar := j + 1
  54. END NoteParam;
  55. BEGIN
  56. Reset; NoteProc("Add"); NoteParam("a");
  57. IF (nProcs = 1) AND (procs[0].pname[0][0] = "a")
  58. THEN ExitCode := 42 ELSE ExitCode := 1 END
  59. END NestedArrayFieldBug.