Skip to content

fix(native): keep undecoded swift-demangle echoes out of demangled status - #4287

Closed
viniciusdsandrade wants to merge 3 commits into
morluto:mainfrom
viniciusdsandrade:fix/native-swift-demangle-undecoded-echo
Closed

viniciusdsandrade wants to merge 3 commits into
morluto:mainfrom
viniciusdsandrade:fix/native-swift-demangle-undecoded-echo

Conversation

@viniciusdsandrade

Copy link
Copy Markdown
Contributor

Summary

Fixes #4215

Keep demangle_swift from reporting a name as demangled when swift-demangle only printed its adjusted spelling of a name it could not decode. __T0Bogus now reports invalid, and non-Swift names such as ___stack_chk_fail and strlen report unchanged. Undecoded results keep the caller's input as output.

Problem and expected behavior

parseDemangledSymbols treated any output line that differed from its input as a successful decode. swift-demangle adjusts some names before demangling and prints that adjusted spelling when demangling fails (tools/swift-demangle/swift-demangle.cpp in swiftlang/swift):

  • a name starting with __ loses one underscore (hadLeadingUnderscore, name.substr(1)), so __T0Bogus prints _T0Bogus and ___stack_chk_fail prints __stack_chk_fail;
  • a command-line argument starting with S, s or e gets a $ prefix (correctedName), so strlen prints $strlen.

REA passes symbols as arguments (--compact -- ...symbols), so each of these became demangled with an output that is not a demangling. The issue reproduced __T0Bogus -> _T0Bogus, demangled with Xcode 27. The ___stack_chk_fail and strlen cases follow from the same source and are not separately reproduced here.

Expected behavior: an undecoded name is invalid when it is a Swift mangling and unchanged otherwise, and its output is the caller's input.

Change and scope

  • src/native/parsers/demangle.ts: compare the output with both the input and the spelling swift-demangle prints for an undecoded name. Decide invalid on the name the demangler examined after removing the linkage underscore, using the existing isSwiftMangledSymbolName. Names that genuinely decode, including __T04main3FooV and s4main3FooV, stay demangled.
  • An output equal to the input is still treated as undecoded, so toolchains without either adjustment keep their current results.
  • Not included: other swift-demangle argument handling, such as the tool skipping a lone _ argument.

Contract and boundary impact

  • Semantic owner and earliest changed stage: swift-demangle output interpretation in src/native/parsers/demangle.ts.
  • Contribution scope fit and linked maintainer decision: not applicable (bug fix within the existing native macOS provider).
  • CLI and MCP/tool-catalog contract: unchanged schemas. status now matches the documented demangled, unchanged or invalid meaning for these inputs.
  • Provider, bridge, target-format, or platform compatibility: native macOS provider with Xcode swift-demangle.
  • Evidence, artifact, provenance, or reconstruction contract: undecoded names are no longer reported as decoded. output for undecoded names is the caller's input; the printed spelling is a fixed transform of that input.
  • Target selection, authority, upstream behavior, and cleanup details (if applicable): not applicable.
  • Generated metadata (docs/public/product-catalog.json), package, or installation impact: none.

Expected effects:

  • Read existing files or captured data.
  • Launch or attach to a process.
  • Inject instrumentation or run scripts/code.
  • Change process memory, registers, files, or configuration.
  • Contact network services.
  • Recover protected code or work around protections.
  • Documentation or usability only; no target interaction.
  • Not sure yet; investigation or maintainer guidance needed.

Evidence and regression coverage

  • Tests added or updated:
    • src/native/parsers/demangle.test.ts: the underscore-stripped and $-prefixed spellings for __T0Bogus, __$sBogus, ___stack_chk_fail, strlen, Symbol and entry keep invalid or unchanged with the input as output; __T04main3FooV and s4main3FooV still demangle.
    • tests/boundary/mcp/nativeDemangleMcp.test.ts (darwin): __T0Bogus, ___stack_chk_fail and strlen through the production demangle_swift tool return invalid, unchanged and unchanged with the input as output.
  • Base reproduction or other evidence: the issue's executed CLI matrix shows __T0Bogus -> _T0Bogus, demangled. The other cases are derived from the cited swift-demangle source.
  • User-visible CLI/MCP output (if applicable): {"input":"__T0Bogus","output":"__T0Bogus","status":"invalid"} instead of {"input":"__T0Bogus","output":"_T0Bogus","status":"demangled"}.
  • Remaining proof gaps: swift-demangle was not run locally; the darwin MCP test runs it in CI where available.

For evidence-bearing changes:

  • Observed, derived, and inferred claims remain distinguishable.
  • Artifact identity, source provenance, and failed attempts remain preserved.
  • Unsupported, incomplete, unavailable, or uncertain outcomes remain visible.

Validation performed

  • No local test commands were run: verification is source-derived; CI executes the deterministic suite.

Compatibility, safety, and release

  • Breaking changes or migration steps: none.
  • Real Hopper/Ghidra, browser, or OS coverage: none run locally.
  • Package or release metadata impact: none.
  • Security, privacy, process, or containment review: no change to how swift-demangle is invoked.

Before merge

  • This PR addresses a concrete problem or an agreed enhancement.
  • Material scope and authority questions are resolved, and any required maintainer scope decision covers the final diff.
  • The PR has one focused outcome and the title follows type(scope): outcome.
  • Related issue is linked, or the reason for not linking one is stated above.
  • Tests cover changed observable behavior and meaningful failure paths.
  • Owning docs, contracts, and generated metadata are updated where needed.
  • User-visible CLI/MCP changes include representative output.
  • I checked the final diff for secrets, unrelated cleanup, and unsupported claims.

…atus

swift-demangle removes one underscore from a name starting with __ and
prefixes $ to an argument starting with S, s or e before demangling. A
name it cannot decode is printed in that adjusted spelling, so the
parser saw output that differed from the input and reported it as
demangled: __T0Bogus became _T0Bogus, and ___stack_chk_fail or strlen
were reported as demangled names.

Treat that adjusted spelling as an undecoded echo. Undecoded results
keep the caller's input as output, and invalid is decided on the name
the demangler examined, so __T0Bogus is invalid and non-Swift names
stay unchanged.

Fixes morluto#4215

Signed-off-by: Vinícius DS Andrade <[email protected]>
Assert that the underscore-stripped and $-prefixed spellings
swift-demangle prints for names it cannot decode keep invalid or
unchanged status with the caller's input as output, while a linkage
underscore or a missing $ on a decodable name still demangles.

Signed-off-by: Vinícius DS Andrade <[email protected]>
Send __T0Bogus, ___stack_chk_fail and strlen through the production
demangle_swift tool with the real swift-demangle and require invalid,
unchanged and unchanged with the caller's input as output.

Signed-off-by: Vinícius DS Andrade <[email protected]>
morluto added a commit that referenced this pull request Oct 16, 2026
Recognize known undecoded producer spellings without treating them as successful demangles. Preserve actual producer output and derive symbol types from the domain contract instead of duplicating its validation schema.

Compare retained output with the real demangler through MCP, while preserving CLI malformed-input and line-ending coverage.

Refs: #4215, #4287

Co-authored-by: Vinícius DS Andrade <[email protected]>
@viniciusdsandrade
viniciusdsandrade marked this pull request as ready for review October 16, 2026 02:10
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 16, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-16T02:17:13.801654Z 580c38e Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@greptile-apps

greptile-apps Bot commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge; no actionable defects were found.

Summary

Fixes parseDemangledSymbols so adjusted echoes from swift-demangle do not count as successful decodes.

  • Swift demangling keeps fallback spellings from looking decoded.

Reviews (1) · Last reviewed commit: "test(native): demangle undecoded linkage..." · Reviewed by Greptile

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 580c38e1c1

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

? "invalid"
: "unchanged"
: "demangled",
output: decoded ? value : input,

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Keep the actual undecoded spelling in output

When an undecoded argument is rewritten by the producer (the added cases pass __T0Bogus and receive _T0Bogus, or strlen and $strlen), this ternary replaces value with input. #demangle returns only this parsed result plus invocation metadata, so the actual stdout spelling is no longer present anywhere in the Evidence; callers therefore see an output value the command did not emit, and toolchain-specific behavior cannot be audited. Keep value as output and use decoded only for status classification.

AGENTS.md reference: AGENTS.md:L13-L13

Useful? React with 👍 / 👎.

: "unchanged"
: "demangled",
output: decoded ? value : input,
status: decoded

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Derive parsed symbols from the domain contract

This bug fix changes the adapter's interpretation while continuing to validate and export it through resultSchema, an independent copy of the symbol item in demangleSwiftSchema (src/domain/native/nativeInspection.ts:183-190). Because no type relationship connects them, a domain contract change can leave this parser compiling and publishing a different status/output shape until a runtime parse fails; derive DemangledSymbol and its validation from the canonical domain contract and remove the local schema as part of this producing-boundary fix.

AGENTS.md reference: AGENTS.md:L27-L27

Useful? React with 👍 / 👎.

@kaoru0822-kitauji

Copy link
Copy Markdown
Collaborator

Conflict check from the CI & conflict-fix track: this branch conflicts with main, and main's 808339f ("fix(native): distinguish undecoded Swift spelling adjustments") references #4287 and is co-authored from this PR, so the change has already landed there. Resolving the conflict would reduce this branch to main's version, so I left it untouched. A maintainer may want to close this as superseded.

@kaoru0822-kitauji kaoru0822-kitauji added the ci-fix-wip Claimed by the CI-fix track label Oct 16, 2026
@kaoru0822-kitauji

Copy link
Copy Markdown
Collaborator

Conflict check from the CI/conflict track. I did not push anything.

#4215 was closed by 808339f0b (fix(native): distinguish undecoded Swift spelling adjustments), which changed src/native/parsers/demangle.ts and the same two test files, hence the conflict. I ran this PR's new expectations (the 8 inputs in keeps the demangler's adjusted spelling of an undecoded name out of demangled) against current main (42fc6c53a):

  • Status: matches for all 8. __T0Bogus/__$sBogus → invalid; ___stack_chk_fail, strlen, Symbol, entry → unchanged; the two Swift names → demangled.
  • output: differs for the 6 undecoded names. main reports the demangler's adjusted spelling (_T0Bogus, _$sBogus, __stack_chk_fail, $strlen, $Symbol, $entry), while this PR keeps the caller's input.

So the bug in #4215 is fixed on main. What remains here is a representation choice for output that conflicts with the one main just made. That needs an author or maintainer decision, not a mechanical conflict resolution, so I'm leaving this PR as is.

The Static checks failure is format:check on the old head; the inherited test failures are tracked by #4345.

@kaoru0822-kitauji kaoru0822-kitauji removed the ci-fix-wip Claimed by the CI-fix track label Oct 16, 2026
@viniciusdsandrade

Copy link
Copy Markdown
Contributor Author

Closing as superseded by 808339f on main.

@morluto morluto added area: native Native analysis and decompilation, including Ghidra, IDA, and Hopper analysis behavior bug Something isn't working labels Nov 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: native Native analysis and decompilation, including Ghidra, IDA, and Hopper analysis behavior bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] demangle-swift reports an undecoded linked legacy name as demangled

3 participants