Skip to content

Skia text: ICU's bidi object reads a pooled array after it was returned to the pool #26723

Backlog

Description

@mikernet

Current behavior 🐛

UnicodeText (the Skia text engine of TextBlock, TextBox, PasswordBox and RichEditBox) can draw bidi text in the wrong visual order when something rents a pooled array during layout.

ubidi_setPara keeps the pointers it is given rather than copying the data. Its ICU documentation says the text pointer "is stored in the UBiDi object" and, for embeddingLevels, that "a copy of this pointer, not of the levels, will be stored in the UBiDi object; the embeddingLevels array must not be deallocated before the UBiDi structure is destroyed or reused". ICU also writes the resolved levels back into that array, and reads them again in ubidi_setLine and ubidi_getLevels for every line.

UnicodeText breaks both rules:

  • it pins the text and the level array only for the duration of the ubidi_setPara call (fixed in ICU.CreateBiDiAndSetPara), so the GC may move them afterwards;
  • the level array is rented from ArrayPool<byte>.Shared (Uno's pool) and returned at the end of the paragraph step, before the line loop calls ubidi_setLine and ubidi_getLevels.

Anything that rents that array between the two, and writes to it, changes the levels ICU reads. That is the visible bug.

The text half is latent with the options Uno uses. Per ICU's source, after ubidi_setPara the text is read again only to count and skip bidi control characters (controlCount in ubidi_setLine, ubidi_getRuns, ubidi_getLogicalIndex, ubidi_getVisualMap and friends), and that count is kept only when UBIDI_OPTION_REMOVE_CONTROLS is set (getDirProps in ubidi.cpp). Uno doesn't set it, so a moved string isn't read; it would be as soon as that option is used.

Expected behavior 🎯

The buffers ICU keeps pointers to stay valid, unmoved and unshared until ubidi_close.

How to reproduce it (as minimally and precisely as possible) 🔬

It depends on what else rents a byte array of the same size class during layout, so it doesn't reproduce on demand from an app. A runtime test reproduces it by renting the array right after the paragraph bidi step and clearing it: a TextBlock with two right-to-left Runs "ab" and "!!" in a left-to-right control is then drawn "ab!!" instead of "!!ab". Inside that window, the pooled array still held ICU's resolved levels (2, 2, 1, 1), which shows ICU had written to it and was still pointing at it.

A forced compacting GC in the same window alone did not reproduce it in three runs, as expected from the above: only the level array is read again.

Workaround 🛠️

None.

Affected platforms 📱💻🖥️

All platforms 🌍 (every Skia target)

Uno.Sdk version (and other relevant versions) 📦

master (7.x).

IDE version 🧑‍💻

Not relevant.

Anything else we need to know? 💬

ICU API reference: ubidi_setPara in ubidi.h.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions