Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 578
[TrimmableTypeMap] Fix UCO boolean return type mismatch causing n_* callback trimming#11168
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -585,6 +585,154 @@ public void Generate_AcwProxy_HasUnmanagedCallersOnlyAttribute () | ||
| Assert.Contains (methodDefs, name => name.Contains ("_uco_")); | ||
| } | ||
| [Theory] | ||
| [InlineData (1, 0x05)] // Boolean → byte (unsigned) for JNI ABI | ||
| [InlineData (2, 0x04)] // Byte → sbyte | ||
| [InlineData (3, 0x03)] // Char → char | ||
| [InlineData (4, 0x06)] // Short → int16 | ||
| [InlineData (5, 0x08)] // Int → int32 | ||
| [InlineData (6, 0x0A)] // Long → int64 | ||
| [InlineData (7, 0x0C)] // Float → float32 | ||
| [InlineData (8, 0x0D)] // Double → float64 | ||
| [InlineData (9, 0x18)] // Object → IntPtr | ||
| public void EncodeClrType_ProducesCorrectPrimitiveTypeCode (int kindValue, byte expectedCode) | ||
| { | ||
| var kind = (JniParamKind) kindValue; | ||
| var blob = new BlobBuilder (); | ||
| JniSignatureHelper.EncodeClrType (new SignatureTypeEncoder (blob), kind); | ||
| Assert.Equal (expectedCode, blob.ToArray () [0]); | ||
| } | ||
| [Theory] | ||
| [InlineData (1, 0x04)] // Boolean → sbyte — matches MCW n_* callbacks | ||
| [InlineData (2, 0x04)] // Byte → sbyte | ||
| [InlineData (3, 0x03)] // Char → char | ||
| [InlineData (4, 0x06)] // Short → int16 | ||
| [InlineData (5, 0x08)] // Int → int32 | ||
| [InlineData (6, 0x0A)] // Long → int64 | ||
| [InlineData (7, 0x0C)] // Float → float32 | ||
| [InlineData (8, 0x0D)] // Double → float64 | ||
| [InlineData (9, 0x18)] // Object → IntPtr | ||
| public void EncodeClrTypeForCallback_ProducesCorrectPrimitiveTypeCode (int kindValue, byte expectedCode) | ||
| { | ||
| var kind = (JniParamKind) kindValue; | ||
| var blob = new BlobBuilder (); | ||
| JniSignatureHelper.EncodeClrTypeForCallback (new SignatureTypeEncoder (blob), kind); | ||
| Assert.Equal (expectedCode, blob.ToArray () [0]); | ||
| } | ||
| [Fact] | ||
| public void EncodeClrType_Boolean_DiffersFromCallback () | ||
| { | ||
| var ucoBlob = new BlobBuilder (); | ||
| JniSignatureHelper.EncodeClrType (new SignatureTypeEncoder (ucoBlob), JniParamKind.Boolean); | ||
| var cbBlob = new BlobBuilder (); | ||
| JniSignatureHelper.EncodeClrTypeForCallback (new SignatureTypeEncoder (cbBlob), JniParamKind.Boolean); | ||
| var ucoBytes = ucoBlob.ToArray (); | ||
| var cbBytes = cbBlob.ToArray (); | ||
| Assert.NotEqual (ucoBytes, cbBytes); | ||
| Assert.Equal (0x05, ucoBytes [0]); // byte (unsigned) | ||
| Assert.Equal (0x04, cbBytes [0]); // sbyte (signed) | ||
| } | ||
| [Fact] | ||
| public void EncodeClrType_Void_Throws () | ||
| { | ||
| var blob = new BlobBuilder (); | ||
| Assert.ThrowsAny<ArgumentException> (() => | ||
| JniSignatureHelper.EncodeClrType (new SignatureTypeEncoder (blob), JniParamKind.Void)); | ||
| } | ||
| [Fact] | ||
| public void EncodeClrTypeForCallback_Void_Throws () | ||
| { | ||
| var blob = new BlobBuilder (); | ||
| Assert.ThrowsAny<ArgumentException> (() => | ||
| JniSignatureHelper.EncodeClrTypeForCallback (new SignatureTypeEncoder (blob), JniParamKind.Void)); | ||
| } | ||
| [Theory] | ||
| [InlineData (2)] // Byte | ||
| [InlineData (3)] // Char | ||
| [InlineData (4)] // Short | ||
| [InlineData (5)] // Int | ||
| [InlineData (6)] // Long | ||
| [InlineData (7)] // Float | ||
| [InlineData (8)] // Double | ||
| [InlineData (9)] // Object | ||
| public void EncodeClrType_NonBooleanTypes_IdenticalToCallback (int kindValue) | ||
| { | ||
| var kind = (JniParamKind) kindValue; | ||
| var ucoBlob = new BlobBuilder (); | ||
| JniSignatureHelper.EncodeClrType (new SignatureTypeEncoder (ucoBlob), kind); | ||
| var cbBlob = new BlobBuilder (); | ||
| JniSignatureHelper.EncodeClrTypeForCallback (new SignatureTypeEncoder (cbBlob), kind); | ||
| Assert.Equal (ucoBlob.ToArray (), cbBlob.ToArray ()); | ||
| } | ||
| [Fact] | ||
| public void Generate_UcoMethod_BooleanReturn_WrapperUsesByte_CallbackUsesSByte () | ||
| { | ||
| // Regression test: the UCO wrapper must use byte (unsigned, JNI ABI) for boolean, | ||
| // but the callback MemberRef must use sbyte (signed, MCW convention). | ||
| // A mismatch caused ILLink to fail resolving the member reference and trim n_* methods. | ||
| var peer = FindFixtureByJavaName ("my/app/TouchHandler"); | ||
| using var stream = GenerateAssembly (new [] { peer }, "BoolReturnTest"); | ||
| using var pe = new PEReader (stream); | ||
| var reader = pe.GetMetadataReader (); | ||
| // Find the UCO wrapper method for onTouch (returns Z → boolean) | ||
| var ucoMethod = reader.MethodDefinitions | ||
| .Select (h => reader.GetMethodDefinition (h)) | ||
| .First (m => reader.GetString (m.Name).Contains ("onTouch") && | ||
| reader.GetString (m.Name).Contains ("_uco_")); | ||
| var ucoSig = ucoMethod.DecodeSignature (SignatureTypeProvider.Instance, null); | ||
| Assert.Equal ("System.Byte", ucoSig.ReturnType); | ||
| // Find the callback MemberRef that the UCO wrapper calls (n_OnTouch on the TouchHandler type) | ||
| var callbackRef = FindCallbackMemberRef (reader, "n_OnTouch"); | ||
| var callbackSig = callbackRef.DecodeMethodSignature (SignatureTypeProvider.Instance, null); | ||
| Assert.Equal ("System.SByte", callbackSig.ReturnType); | ||
| } | ||
| [Fact] | ||
| public void Generate_UcoMethod_BooleanParam_WrapperUsesByte_CallbackUsesSByte () | ||
| { | ||
| // Regression test: boolean parameters must also use the correct encoding. | ||
| var peer = FindFixtureByJavaName ("my/app/TouchHandler"); | ||
| using var stream = GenerateAssembly (new [] { peer }, "BoolParamTest"); | ||
| using var pe = new PEReader (stream); | ||
| var reader = pe.GetMetadataReader (); | ||
| // Find the UCO wrapper for onFocusChange (takes Z as 3rd param → boolean parameter) | ||
| var ucoMethod = reader.MethodDefinitions | ||
| .Select (h => reader.GetMethodDefinition (h)) | ||
| .First (m => reader.GetString (m.Name).Contains ("onFocusChange") && | ||
| reader.GetString (m.Name).Contains ("_uco_")); | ||
| var ucoSig = ucoMethod.DecodeSignature (SignatureTypeProvider.Instance, null); | ||
| // Params: IntPtr (jnienv), IntPtr (self), IntPtr (View object), byte (boolean) | ||
| Assert.Equal ("System.Byte", ucoSig.ParameterTypes.Last ()); | ||
| // Find the callback MemberRef | ||
| var callbackRef = FindCallbackMemberRef (reader, "n_OnFocusChange"); | ||
| var callbackSig = callbackRef.DecodeMethodSignature (SignatureTypeProvider.Instance, null); | ||
| Assert.Equal ("System.SByte", callbackSig.ParameterTypes.Last ()); | ||
| } | ||
| static MemberReference FindCallbackMemberRef (MetadataReader reader, string methodName) | ||
| { | ||
| var refs = Enumerable.Range (1, reader.GetTableRowCount (TableIndex.MemberRef)) | ||
| .Select (i => reader.GetMemberReference (MetadataTokens.MemberReferenceHandle (i))) | ||
| .Where (m => reader.GetString (m.Name) == methodName) | ||
MemberAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💡 Testing — Rule: Test assertions must be specific | ||
| .ToList (); | ||
| Assert.Single (refs); | ||
| return refs [0]; | ||
| } | ||
| [Theory] | ||
| [InlineData ("()V", 0)] | ||
| [InlineData ("(I)V", 1)] | ||
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.
💡 Documentation — Now that
EncodeClrTypeForCallbackexists withsbytefor boolean, this inline comment could be expanded to mention the dual encoding: e.g.,// JNI jboolean: byte (UCO wrapper) vs sbyte (callback MemberRef). Helps future readers understand why there are two methods.Rule: Comments explain "why", not "what" (Postmortem
#59)