Skip to content

Keep text pointers valid across garbage collection when rendering expressions - #1872

Open
Wiljea wants to merge 2 commits into
c3d:devfrom
Wiljea:fix-symbol-concat-gc
Open

Wiljea wants to merge 2 commits into
c3d:devfrom
Wiljea:fix-symbol-concat-gc

Conversation

@Wiljea

@Wiljea Wiljea commented Nov 2, 2026

Copy link
Copy Markdown

Problem

Two places keep a raw pointer to text while the garbage collector may run and move it, so expressions can be displayed with damaged characters, depending on when a collection happens:

  1. operator+ on symbols, src/symbol.cc, holds raw pointers to its arguments while allocating the result. Rendering an expression goes through this operator for each term. The example suite gave A14+^A+ followed by a line break instead of A14+A15+.
  2. renderer::put(text, length) and its variant with a format, src/renderer.cc, read the source through a raw pointer while writing one character at a time. Each character may run the collector. The source is then read from where the output was just written: 'A' 'B' + gave 'AAA', and 'A' 'B' ↑ gave 'AAAAA'.

operator+ on text and the rendering of text objects already protect themselves against this. The first failure appeared on a branch that only adds library files, in the test Numerical evaluation of a sum with many terms.

This is separate from #1858 and its fix #1868: the failure is the same with or without that fix.

Fix

Use gcutf8 in these places, as text.cc does. The fix in renderer::put covers all its callers.

How I found it: forcing collections

Forcing a collection at every allocation exposes this kind of problem whatever the memory layout. I added this temporarily to runtime::available(size_t) on the simulator, and it is not part of this PR:

#ifdef SIMULATOR
    {
        static int period = getenv("DB48X_GC_STRESS") ? atoi(getenv("DB48X_GC_STRESS")) : 0;
        static int count  = 0;
        if (period && ++count >= period)
        {
            count = 0;
            gc();
        }
    }
#endif

Then run the tests with DB48X_GC_STRESS=5 ./db48x -H -Z -T'(symbolic)'. Many steps fail with "RPL command was not processed", simply because the run is so slow. What matters is any got with damaged text.

Period Before After
17 AA 3=B÷3 for A÷3=B÷3 no damaged text
5 AAA for A+B, AAAAA for A↑B no damaged text
2 not compared no damaged text
1 not compared cos (sin A) for tan (cos (sin A)), probably the previous result, because the test did not wait long enough; not verified

These forced runs stopped on their time limits after fewer than 35 steps of symbolic. Other groups were not run this way, so similar problems may remain elsewhere. It may be worth doing a wider run, with longer test delays.

Tests

  • Full suite on this branch, DB48x and DB50x: no failure.
  • The branch where the sum test failed, with the first fix: no failure, in two runs.
  • No new test: the failure depends on when a collection happens, and I have no simple stable test without the forcing above.
  • Not tried on a calculator, and I did not measure the cost of the added gcutf8.

Author & design: Jean Wilson
🤖 Generated with Claude Code

Wiljea and others added 2 commits November 2, 2026 17:34
operator+ on symbols kept raw pointers to the text of its arguments while
allocating the result, which may run the garbage collector and move them.
The text of the result then came from stale addresses. Rendering expressions
goes through this operator for every term, so an expression could be displayed
with damaged characters, for instance A14+^A+ followed by a line break in place
of A14+A15+, depending on when a collection happened.

Use gcutf8, as the same operator on text already does.

Author & design: Jean Wilson
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
renderer::put(text, length) and its variant with a format read the source
through a raw pointer while writing one character at a time. Each character
written to the scratchpad may run the garbage collector and move the source,
which is then read from the place where the output was just written. For
instance, 'A' 'B' + was displayed as 'AAA'.

Use gcutf8, as the rendering of text objects already does.

Author & design: Jean Wilson
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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