Skip to content

Remove PAL FormatMessageW - #137598

Merged
jkotas merged 12 commits into
dotnet:mainfrom
am11:chore/remove-pal-formatmessage
Nov 18, 2026
Merged

jkotas merged 12 commits into
dotnet:mainfrom
am11:chore/remove-pal-formatmessage

Conversation

@am11

@am11 am11 commented Nov 15, 2026

Copy link
Copy Markdown
Member

Split out of #137579.

All Unix callers of the PAL FormatMessageW are in utilcode, so this handles them there with plain #ifdef TARGET_UNIX branches:

  • SString::FormatMessage expands %1..%10 inserts itself for FORMAT_MESSAGE_FROM_STRING (bindertracing, assembly load errors, EEMessageException, fail-fast and event-reporter messages), and uses the error message table for FORMAT_MESSAGE_FROM_SYSTEM.
  • FormatRuntimeErrorVA (posterror.cpp) uses the error message table.
  • The error message table moves from pal/src/misc/errorstrings.cpp to utilcode/errorstrings.cpp as GetSystemErrorMessage, keeping the HRESULT_FROM_WIN32 unwrapping and the Error <n> fallback.

pal/src/misc/fmtmessage.cpp, the FormatMessageW declaration, the three flags only it used, and its PAL tests are deleted.

Where the Win32 error message table is user-facing

On Unix, FORMAT_MESSAGE_FROM_SYSTEM is reached through GetHRMsg and FormatRuntimeErrorVA. The user-visible case is exception messages for HRESULT_FROM_WIN32 codes that have no CoreCLR resource string, for example the "The system cannot find the file specified." part of FileNotFoundException from Assembly.Load, and the messages of Marshal.GetExceptionForHR(0x80070005) and similar. Without the table, these would show only the HRESULT, as for other codes. This PR keeps them unchanged; removing the table can be a follow-up if we are fine with that.

Testing

Linux x64: native exception messages from Assembly.Load, Type.GetType, Assembly.LoadFrom of a bad image and Marshal.GetExceptionForHR for eight HRESULTs are identical to main. PAL tests pass.

All Unix callers are in utilcode, so handle them there directly:

- `SString::FormatMessage` expands `%1`..`%10` inserts itself for
  `FORMAT_MESSAGE_FROM_STRING`, and uses the error message table for
  `FORMAT_MESSAGE_FROM_SYSTEM`.
- `FormatRuntimeErrorVA` (`posterror.cpp`) uses the error message table.
- Move the error message table from `pal/src/misc/errorstrings.cpp` to
  `utilcode/errorstrings.cpp` as `GetSystemErrorMessage`.

Delete `pal/src/misc/fmtmessage.cpp`, the `FormatMessageW` declaration and
the flags only it used, and its PAL tests.

Co-authored-by: Cursor <[email protected]>
@github-actions github-actions Bot added the area-PAL-coreclr only for closed issues label Nov 15, 2026
@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Nov 15, 2026
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 4 pipeline(s).
11 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

These replace a host API, so a Windows-hosted cross DAC keeps using
`FormatMessage`.

Co-authored-by: Cursor <[email protected]>
Comment thread src/coreclr/inc/utilcode.h Resolved
Comment thread src/coreclr/utilcode/sstring.cpp Resolved
Comment thread src/coreclr/utilcode/sstring.cpp Resolved
am11 and others added 2 commits November 16, 2026 20:55
- Implement `GetSystemErrorMessage` on Windows too and use it in
  `FormatRuntimeErrorVA` on all platforms.
- Handle `%b`, `%r` and `%t` in `SString::FormatMessage` on Unix.
- Assert if the system error message does not fit in the buffer.

Co-authored-by: Cursor <[email protected]>
Comment thread src/coreclr/utilcode/posterror.cpp Resolved
…atmessage

Co-authored-by: Cursor <[email protected]>

# Conflicts:
#	src/coreclr/dlls/mscordac/mscordac_unixexports.src
#	src/coreclr/pal/tests/palsuite/CMakeLists.txt
#	src/coreclr/pal/tests/palsuite/compilableTests.txt
#	src/coreclr/pal/tests/palsuite/paltestlist.txt
Comment thread src/coreclr/utilcode/errorstrings.cpp Resolved
Comment thread src/coreclr/utilcode/sstring.cpp Resolved
Comment thread src/coreclr/utilcode/posterror.cpp Resolved
am11 and others added 2 commits November 17, 2026 01:47
- Drop the 40 error codes that nothing produces from the message table and
  from `pal_error.h`.
- `SString::FormatMessage` on Unix only supports inserts, which is all the
  runtime messages use.

Co-authored-by: Cursor <[email protected]>
Comment thread src/coreclr/utilcode/errorstrings.cpp Resolved

@jkotas jkotas left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thank you!

@AaronRobinsonMSFT

Copy link
Copy Markdown
Member

@am11 Looks like we got some conflicts.

am11 and others added 2 commits November 18, 2026 01:38
…atmessage

Co-authored-by: Cursor <[email protected]>

# Conflicts:
#	src/coreclr/inc/utilcode.h
@AaronRobinsonMSFT
AaronRobinsonMSFT enabled auto-merge (squash) November 17, 2026 23:42
@jkotas

jkotas commented Nov 18, 2026

Copy link
Copy Markdown
Member

The test is failing with

ASSERT FAILED
	Expression: !"Unsupported escape sequence in message"
	Location:   /__w/1/s/src/coreclr/utilcode/sstring.cpp:1826
	Function:   FormatMessage
	Process:    17

@jkotas
jkotas disabled auto-merge November 18, 2026 03:26
`%1%'` relied on `%'` being formatted as `'`.

Co-authored-by: Cursor <[email protected]>
@am11

am11 commented Nov 18, 2026

Copy link
Copy Markdown
Member Author

Fixed. IDS_EE_CANNOT_SET_INITONLY_STATIC_FIELD had a typo ('%1%') that only worked because %' was formatted as '.

@jkotas
jkotas merged commit d8280fb into dotnet:main Nov 18, 2026
100 of 102 checks passed
@am11
am11 deleted the chore/remove-pal-formatmessage branch November 18, 2026 12:22
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Nov 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-PAL-coreclr only for closed issues community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants