-
Notifications
You must be signed in to change notification settings - Fork 568
Fix XA3006: Sanitize whitespace-only parameter names in marshal methods LLVM IR generation #10835
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
dcf5e10
Initial plan
Copilot 480d726
Fix empty parameter name causing invalid LLVM IR in marshal methods
Copilot 0240be1
Use IsNullOrWhiteSpace to handle whitespace-only parameter names
Copilot cfde3c1
Add regression tests for whitespace parameter names in LLVM IR genera…
Copilot cfb863b
Fix test: correct issue reference and use whitespace-only parameter n…
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
83 changes: 83 additions & 0 deletions
83
...marin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/Tasks/LlvmIrGeneratorTests.cs
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.IO; | ||
| using Microsoft.Build.Utilities; | ||
| using NUnit.Framework; | ||
| using Xamarin.Android.Tasks.LLVMIR; | ||
| using Xamarin.Android.Tools; | ||
|
|
||
| namespace Xamarin.Android.Build.Tests.Tasks | ||
| { | ||
| [TestFixture] | ||
| public class LlvmIrGeneratorTests | ||
| { | ||
| /// <summary> | ||
| /// Regression test for https://github.com/dotnet/android/issues/10679 | ||
| /// | ||
| /// When a managed method parameter has a whitespace-only name, the LLVM IR | ||
| /// generator would produce invalid IR like: | ||
| /// define void @test(ptr noundef % ) | ||
| /// | ||
| /// The fix ensures whitespace-only names are normalized to null before | ||
| /// creating LlvmIrFunctionParameter instances, so that LlvmIrFunction | ||
| /// assigns them valid numeric names (e.g., %0, %1). | ||
| /// </summary> | ||
| [Test] | ||
| [TestCase (null, Description = "Null parameter name")] | ||
| [TestCase ("", Description = "Empty parameter name")] | ||
| [TestCase (" ", Description = "Space-only parameter name")] | ||
| [TestCase (" ", Description = "Multiple spaces parameter name")] | ||
| [TestCase ("\t", Description = "Tab-only parameter name")] | ||
| public void FunctionParameterWithInvalidName_GetsNumericName (string? paramName) | ||
| { | ||
| var parameters = new List<LlvmIrFunctionParameter> { | ||
| new LlvmIrFunctionParameter (typeof (IntPtr), "env"), | ||
| new LlvmIrFunctionParameter (typeof (IntPtr), "klass"), | ||
| new LlvmIrFunctionParameter (typeof (IntPtr), paramName), | ||
| }; | ||
|
|
||
| var func = new LlvmIrFunction ("test_function", typeof (void), parameters); | ||
|
|
||
| // The third parameter should have been assigned a numeric name | ||
| var thirdParam = func.Signature.Parameters[2]; | ||
| Assert.IsNotNull (thirdParam.Name, "Parameter name should not be null after LlvmIrFunction construction"); | ||
| Assert.IsNotEmpty (thirdParam.Name, "Parameter name should not be empty after LlvmIrFunction construction"); | ||
| Assert.That (thirdParam.Name.Trim (), Is.Not.Empty, "Parameter name should not be whitespace-only after LlvmIrFunction construction"); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Verifies that the LLVM IR generator produces valid function signatures | ||
| /// when parameters have whitespace-only names that would have caused | ||
| /// invalid IR like "ptr noundef % )" before the fix. | ||
| /// </summary> | ||
| [Test] | ||
| [TestCase (" ", Description = "Space-only parameter name")] | ||
| [TestCase ("\t", Description = "Tab-only parameter name")] | ||
| public void GeneratedIR_FunctionWithWhitespaceParameterName_ProducesValidOutput (string paramName) | ||
| { | ||
| var parameters = new List<LlvmIrFunctionParameter> { | ||
| new LlvmIrFunctionParameter (typeof (IntPtr), "env"), | ||
| new LlvmIrFunctionParameter (typeof (IntPtr), "klass"), | ||
| new LlvmIrFunctionParameter (typeof (IntPtr), paramName), | ||
| }; | ||
|
|
||
| var func = new LlvmIrFunction ("test_function", typeof (void), parameters); | ||
|
|
||
| var log = new TaskLoggingHelper (new MockBuildEngine (TestContext.Out, [], [], []), "test"); | ||
| var module = new LlvmIrModule (new LlvmIrTypeCache (), log); | ||
| func.Body.Ret (typeof (void)); | ||
| module.Add (func); | ||
| module.AfterConstruction (); | ||
|
|
||
| var generator = LlvmIrGenerator.Create (AndroidTargetArch.Arm64, "test.ll"); | ||
| using var writer = new StringWriter (); | ||
| generator.Generate (writer, module); | ||
|
|
||
| string output = writer.ToString (); | ||
| // The output should contain valid parameter declarations - no whitespace-only names after % | ||
| Assert.That (output, Does.Not.Contain ("% )"), "Generated LLVM IR should not contain 'ptr noundef % )' pattern"); | ||
| Assert.That (output, Does.Not.Contain ("%\t)"), "Generated LLVM IR should not contain 'ptr noundef %\\t)' pattern"); | ||
| Assert.That (output, Does.Contain ("@test_function"), "Generated LLVM IR should contain the function name"); | ||
| } | ||
| } | ||
| } | ||
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
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The regression test comment links to issue #10086, but the PR description/title indicates this change fixes XA3006 whitespace-only parameter names (#10679). Please update the comment to reference the correct issue so future readers can trace the right bug report.