← Back to Pull requests

Emit UTF-8 unescaped in string section #9259

Open
stevenfontanella opened this pull request with 102 lines diff across 5 files
View on GitHub
stevenfontanella stevenfontanella commented on Oct 13, 2026 23:01 UTC
Author

Saves some bytes in the string section when non-magic string lowering is used.

Also resolves some TODOs, and fixes unescapeUTF8JSONtoWTF16 which previously didn't handle UTF-8 sequences.

Diff: 102 lines (+62 / -40) across 5 files Reviewer: tlively Review duration: 13720m
All Vouch review confidence checks passed
Residual risk 0.05 = Change Risk (0.37) × (1 − Review Confidence (0.85)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.37 · 102 lines diff across 5 files
0.37
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.89 · 3 substantive comment(s)
0.89
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
85%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size diff_size
0.277
Real PR Conversation Timeline
5 items
tlively tlively commented on code on Oct 14, 2026 21:07 UTC
Code Review
src/passes/Print.cpp:1
@@ -3709,24 +3709,9 @@ void PrintSExpression::visitModule(Module* curr) {
     doIndent(o, indent);
     o << ";; custom section \"" << section.name << "\", size "
       << section.data.size();
-    bool isPrintable = true;
-    for (auto c : section.data) {
-      if (!isprint(static_cast<unsigned char>(c))) {
-        isPrintable = false;
-        break;
-      }
-    }
-    if (isPrintable) {
-      o << ", contents: ";
-      // std::quoted is not available in all the supported compilers yet.
-      o << '"';
-      for (auto c : section.data) {
-        if (c == '\\' || c == '"') {
-          o << '\\';
-        }
-        o << c;
-      }
-      o << '"';
+    std::string_view sectionData(section.data.data(), section.data.size());
+    if (String::isPrintableUTF8(sectionData)) {

When printing text modules, where size doesn't matter, but readability does, I would err on the side of using more escapes to avoid accidentally emitting unprintable unicode characters, so I wouldn't want to make the change here unless we were really confident that isPrintableUTF8 was exhaustive.

tlively tlively commented on code on Oct 14, 2026 21:13 UTC
Code Review
src/support/string.cpp:1
@@ -478,13 +484,34 @@ bool isUTF8(std::string_view str) {
   return true;
 }
 
+bool isPrintableUTF8(std::string_view str) {
+  while (!str.empty()) {
+    auto u = takeWTF8CodePoint(str);
+    if (!u) {
+      return false; // Invalid encoding
+    }
+    // Reject ASCII control chars (0x00-0x1F), DEL (0x7F), and surrogates
+    // (0xD800-0xDFFF)
+    if (*u < 0x20 || *u == 0x7F || (0xD800 <= *u && *u < 0xE000)) {
+      return false;
+    }

There are many additional kinds of unicode control characters: https://en.wikipedia.org/wiki/Unicode_control_characters.

Trying to implement our own correct version of this function is probably not worth the effort, and we don't want to pull in a library like ICU, so we should at least default to saying a character is not printable unless we know for sure that it is.

tlively tlively commented on code on Oct 14, 2026 21:18 UTC
Code Review
src/support/string.cpp:1
@@ -435,17 +436,22 @@ std::ostream& printEscapedJSON(std::ostream& os, std::string_view str) {
         break;
     }
 
-    // TODO: To minimize size, consider additionally escaping only other control
-    // characters (u <= 0x1F) and surrogates, emitting everything else directly
-    // assuming a UTF-8 encoding of the JSON text. We don't do this now because
-    // Print.cpp would consider the contents unprintable, messing up our test.
     bool isNaivelyPrintable = 32 <= u && u < 127;
     if (isNaivelyPrintable) {
       assert(u < 0x80 && "need additional logic to emit valid UTF-8");
       os << uint8_t(u);
       continue;
     }
 
+    // In raw UTF-8 mode, emit non-ASCII code points directly. Lone surrogates
+    // have no UTF-8 encoding and must still be escaped. (Paired surrogates have
+    // already been combined into a single code point >= 0x10000 above.)
+    bool isLoneSurrogate = 0xD800 <= u && u < 0xE000;
+    if (rawUTF8 && u >= 0x80 && !isLoneSurrogate) {

If we're writing raw UTF-8 and don't care about writing control characters, then why check for u >= 0x80 here?

tlively tlively commented on Oct 14, 2026 21:21 UTC
Reviewer

It looks like there a few separate changes here: fixing JSON decoding to handle arbitrary UTF-8 (uncontroversial), emitting raw UTF-8 in the string section (probably ok, but hopefully nobody uses the string section), and printing more UTF-8 in wat custom sections (dangerous).

It might be helpful to separate them into separate PRs so we can discuss them individually.

Reviewers
Assignees
stevenfontanella stevenfontanella
Labels
risk: low confidence: 85.4% reviewed
Vouch Risk Breakdown
AI-powered review risk analysis
Change Risk
Change Risk · Model 1 (XGBoost)
Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
Score: 36.6% (MEDIUM) · Higher = larger blast radius
36.6%
Review Confidence
Review Confidence · Models 2 & 3
Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
Score: 85.4% · Lower = rushed or shallow review
85.4%
Residual Risk
Residual Risk · Core Equation
Residual = Change × (1 − Confidence)
Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
Score: 5.4% · Safe to merge
5.4%