Bugfix :: Fix F# exception serialization to preserve fields#19342
Open
Bugfix :: Fix F# exception serialization to preserve fields#19342
Conversation
Contributor
❗ Release notes required
|
c9b17c0 to
f6f7424
Compare
Member
Author
|
/azp run fsharp-ci |
|
Azure Pipelines successfully started running 1 pipeline(s). |
f6f7424 to
675d541
Compare
Fixes #878 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
675d541 to
4b0a72e
Compare
The generated GetObjectData override on F# exception types must have the [SecurityCritical] attribute to match the security accessibility of Exception.GetObjectData on .NET Framework. Without this, the CLR rejects the override with 'Inheritance security rules violated', causing FS0193 errors when any project references FSharp.Core (which contains MatchFailureException with the new GetObjectData override). Also update IL baselines and FSharp.Core surface area baseline. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
FSharp.Core has [assembly: SecurityTransparent] which makes all methods transparent. On .NET Framework, transparent methods cannot override SecurityCritical methods (Exception.GetObjectData) nor call SecurityCritical base constructors (Exception(SerializationInfo, StreamingContext)). Skip emitting serialization members for FSharp.Core exceptions. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…, add test FSharp.Core has [assembly: SecurityTransparent]. On .NET Framework, transparent code cannot override SecurityCritical methods (GetObjectData). Without GetObjectData to write fields, the field-restoring ctor would crash. So for FSharp.Core exceptions: - Keep the base-call-only deserialization ctor (status quo, SecuritySafeCritical base) - Skip GetObjectData override (can't override SecurityCritical from transparent) For user exceptions (no SecurityTransparent): - Emit both GetObjectData and field-restoring ctor (full serialization) Add test verifying FSharp.Core MatchFailureException: - Loads without TypeLoadException - Has deserialization ctor - Does NOT have GetObjectData override Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
The compilingFSharpCore guard skips GetObjectData, so the surface area baseline should not include it for MatchFailureException. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…ctors Add deserialization constructor bodies (reading fields from SerializationInfo) and GetObjectData overrides to match the netcore baselines. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes F# exceptions losing their field values during serialization.
Exceptions with fields now emit a
GetObjectDataoverride and a deserialization constructor that save/restore the fields viaSerializationInfo, enabling correctISerializableroundtrips.Note: This fix is primarily relevant for users targeting .NET Framework 4.x or .NET Core ≤7, where
BinaryFormatter/ISerializableserialization is still available. On .NET 8+,StreamingContextis removed and the serialization members are not emitted. The entireISerializableinfrastructure is deprecated (SYSLIB0051) — no modern serializer (System.Text.Json,DataContractSerializer, etc.) usesGetObjectData.FSharp.Core exception caveat: FSharp.Core has
[assembly: SecurityTransparent], which on .NET Framework prevents overriding theSecurityCriticalmethodException.GetObjectData. FSharp.Core exceptions (e.g.MatchFailureException) therefore get only the base-call deserialization constructor (status quo) but not theGetObjectDataoverride. This is tested explicitly. User-defined exceptions are unaffected — they get full serialization support.