Fix memory leaks from mono_type_full_name in marshal-shared.c error paths#125616
Fix memory leaks from mono_type_full_name in marshal-shared.c error paths#125616
Conversation
…aths Co-authored-by: lewing <24063+lewing@users.noreply.github.com>
…red.c Co-authored-by: lewing <24063+lewing@users.noreply.github.com>
|
@copilot take a look at the failures |
…inline form Co-authored-by: lewing <24063+lewing@users.noreply.github.com>
The build was failing with Root cause: when Fixed in commit Warning Firewall rules blocked me from connecting to one or more addresses (expand for details)I tried to connect to the following addresses, but was blocked by firewall rules:
If you need me to access, download, or install something from one of these locations, you can either:
|
There was a problem hiding this comment.
Pull request overview
Fixes repeated heap leaks in Mono marshaling error paths by ensuring mono_type_full_name() results are freed after being used to format exception messages during IL generation (notably during AOT compilation when many invalid marshal configurations are encountered).
Changes:
- Store
mono_type_full_name(...)results into locals beforeg_strdup_printf(...)andg_free(...)them afterward. - Apply the pattern to the auto-layout struct check, non-blittable generic field check, and struct field type check (two type-name allocations).
You can also share your feedback on Copilot code review. Take the survey.
mono_type_full_name()returns a heap-allocated string, but several call sites inmarshal-shared.cpassed the result directly intog_strdup_printfwithout freeing it — leaking on every hit. These paths can fire repeatedly during AOT compilation against assemblies with many invalid marshal configurations.Description
Store each
mono_type_full_name()result in a local, use it, theng_freeit:Fixed sites:
g_strdup_printfwith singlemono_type_full_name; result stored, used, and freedg_errorpath — left as-is (inlinemono_type_full_namecall);g_erroris fatal and never returns, and inDISABLE_ASSERT_MESSAGESbuilds its variadic args are discarded entirely — introducing a local variable causes an unused-variable build error in WASM/WASI configurationsg_strdup_printfwith singlemono_type_full_name; result stored, used, and freedg_strdup_printfwith twomono_type_full_namecalls; both results stored, used, and freedCustomer Impact
Cumulative memory growth during AOT compilation of assemblies containing types with invalid marshal configurations (auto-layout structs, non-blittable generic fields, reference-typed struct fields).
Regression
No — pre-existing leak, not a regression.
Testing
Validated by code inspection and CI. These are error paths with no dedicated test infrastructure in the repo; the fix is mechanical and low-risk.
Risk
Very low. Changes are confined to error paths that emit a marshal exception. No logic changes — only introduction of local variables and paired
g_freecalls before strings go out of scope. Theg_errorpath is intentionally left unchanged to avoid a-Werror,-Wunused-variablebuild failure in WASM/WASI configurations whereg_errordiscards its variadic arguments.Package authoring no longer needed in .NET 9
IMPORTANT: Starting with .NET 9, you no longer need to edit a NuGet package's csproj to enable building and bump the version.
Keep in mind that we still need package authoring in .NET 8 and older versions.
Original prompt
🔒 GitHub Advanced Security automatically protects Copilot coding agent pull requests. You can protect all pull requests by enabling Advanced Security for your repositories. Learn more about Advanced Security.