Skip to content
Browse files

Fix #241: type analysis causes truncation of integer literals when ca…

…lculating with types smaller than int32.
  • Loading branch information...
1 parent a318ce6 commit 2783b020079a26e8e82ad503a3d5a6143fb8d55b @dgrunwald dgrunwald committed
View
38 ICSharpCode.Decompiler/ILAst/InitializerPeepholeTransforms.cs
@@ -93,9 +93,9 @@ bool TransformMultidimensionalArrayInitializers(List<ILNode> body, ILExpression
List<ILExpression> ctorArgs;
ArrayType arrayType;
if (expr.Match(ILCode.Stloc, out v, out newarrExpr) &&
- newarrExpr.Match(ILCode.Newobj, out ctor, out ctorArgs) &&
- (arrayType = (ctor.DeclaringType as ArrayType)) != null &&
- arrayType.Rank == ctorArgs.Count) {
+ newarrExpr.Match(ILCode.Newobj, out ctor, out ctorArgs) &&
+ (arrayType = (ctor.DeclaringType as ArrayType)) != null &&
+ arrayType.Rank == ctorArgs.Count) {
// Clone the type, so we can muck about with the Dimensions
arrayType = new ArrayType(arrayType.ElementType, arrayType.Rank);
var arrayLengths = new int[arrayType.Rank];
@@ -126,12 +126,12 @@ bool ForwardScanInitializeArrayRuntimeHelper(List<ILNode> body, int pos, ILVaria
ILExpression methodArg2;
FieldDefinition field;
if (body.ElementAtOrDefault(pos).Match(ILCode.Call, out methodRef, out methodArg1, out methodArg2) &&
- methodRef.DeclaringType.FullName == "System.Runtime.CompilerServices.RuntimeHelpers" &&
- methodRef.Name == "InitializeArray" &&
- methodArg1.Match(ILCode.Ldloc, out v2) &&
- array == v2 &&
- methodArg2.Match(ILCode.Ldtoken, out field) &&
- field != null && field.InitialValue != null) {
+ methodRef.DeclaringType.FullName == "System.Runtime.CompilerServices.RuntimeHelpers" &&
+ methodRef.Name == "InitializeArray" &&
+ methodArg1.Match(ILCode.Ldloc, out v2) &&
+ array == v2 &&
+ methodArg2.Match(ILCode.Ldtoken, out field) &&
+ field != null && field.InitialValue != null) {
ILExpression[] newArr = new ILExpression[arrayLength];
if (DecodeArrayInitializer(TypeAnalysis.GetTypeCode(arrayType.GetElementType()), field.InitialValue, newArr)) {
values = newArr;
@@ -148,7 +148,6 @@ static bool DecodeArrayInitializer(TypeCode elementType, byte[] initialValue, IL
{
switch (elementType) {
case TypeCode.Boolean:
- case TypeCode.SByte:
case TypeCode.Byte:
if (initialValue.Length == output.Length) {
for (int j = 0; j < output.Length; j++) {
@@ -157,9 +156,15 @@ static bool DecodeArrayInitializer(TypeCode elementType, byte[] initialValue, IL
return true;
}
return false;
- case TypeCode.Char:
+ case TypeCode.SByte:
+ if (initialValue.Length == output.Length) {
+ for (int j = 0; j < output.Length; j++) {
+ output[j] = new ILExpression(ILCode.Ldc_I4, (int)unchecked((sbyte)initialValue[j]));
+ }
+ return true;
+ }
+ return false;
case TypeCode.Int16:
- case TypeCode.UInt16:
if (initialValue.Length == output.Length * 2) {
for (int j = 0; j < output.Length; j++) {
output[j] = new ILExpression(ILCode.Ldc_I4, (int)BitConverter.ToInt16(initialValue, j * 2));
@@ -167,6 +172,15 @@ static bool DecodeArrayInitializer(TypeCode elementType, byte[] initialValue, IL
return true;
}
return false;
+ case TypeCode.Char:
+ case TypeCode.UInt16:
+ if (initialValue.Length == output.Length * 2) {
+ for (int j = 0; j < output.Length; j++) {
+ output[j] = new ILExpression(ILCode.Ldc_I4, (int)BitConverter.ToUInt16(initialValue, j * 2));
+ }
+ return true;
+ }
+ return false;
case TypeCode.Int32:
case TypeCode.UInt32:
if (initialValue.Length == output.Length * 4) {
View
37 ICSharpCode.Decompiler/ILAst/TypeAnalysis.cs
@@ -543,9 +543,19 @@ TypeReference DoInferTypeForExpression(ILExpression expr, TypeReference expected
case ILCode.Ldc_I4:
if (IsBoolean(expectedType) && ((int)expr.Operand == 0 || (int)expr.Operand == 1))
return typeSystem.Boolean;
- return (IsIntegerOrEnum(expectedType) || expectedType is PointerType) ? expectedType : typeSystem.Int32;
+ if (expectedType is PointerType && (int)expr.Operand == 0)
+ return expectedType;
+ if (IsIntegerOrEnum(expectedType) && OperandFitsInType(expectedType, (int)expr.Operand))
+ return expectedType;
+ else
+ return typeSystem.Int32;
case ILCode.Ldc_I8:
- return (IsIntegerOrEnum(expectedType) || expectedType is PointerType) ? expectedType : typeSystem.Int64;
+ if (expectedType is PointerType && (long)expr.Operand == 0)
+ return expectedType;
+ if (IsIntegerOrEnum(expectedType) && GetInformationAmount(expectedType) >= NativeInt)
+ return expectedType;
+ else
+ return typeSystem.Int64;
case ILCode.Ldc_R4:
return typeSystem.Single;
case ILCode.Ldc_R8:
@@ -1043,6 +1053,29 @@ public static bool IsEnum(TypeReference type)
}
}
+ static bool OperandFitsInType(TypeReference type, int num)
+ {
+ TypeDefinition typeDef = type.Resolve() as TypeDefinition;
+ if (typeDef != null && typeDef.IsEnum) {
+ type = typeDef.Fields.Single(f => f.IsRuntimeSpecialName && !f.IsStatic).FieldType;
+ }
+ switch (type.MetadataType) {
+ case MetadataType.SByte:
+ return sbyte.MinValue <= num && num <= sbyte.MaxValue;
+ case MetadataType.Int16:
+ return short.MinValue <= num && num <= short.MaxValue;
+ case MetadataType.Byte:
+ return byte.MinValue <= num && num <= byte.MaxValue;
+ case MetadataType.Char:
+ return char.MinValue <= num && num <= char.MaxValue;
+ case MetadataType.UInt16:
+ return ushort.MinValue <= num && num <= ushort.MaxValue;
+ break;
+ default:
+ return true;
+ }
+ }
+
static bool IsArrayPointerOrReference(TypeReference type)
{
TypeSpecification typeSpec = type as TypeSpecification;
View
1 ICSharpCode.Decompiler/Tests/ICSharpCode.Decompiler.Tests.csproj
@@ -63,6 +63,7 @@
<Compile Include="PInvoke.cs" />
<Compile Include="QueryExpressions.cs" />
<Compile Include="Switch.cs" />
+ <Compile Include="TypeAnalysisTests.cs" />
<Compile Include="UndocumentedExpressions.cs" />
<Compile Include="UnsafeCode.cs" />
<Compile Include="Types\S_TypeDeclarations.cs" />
View
6 ICSharpCode.Decompiler/Tests/TestRunner.cs
@@ -142,6 +142,12 @@ public void YieldReturn()
TestFile(@"..\..\Tests\YieldReturn.cs");
}
+ [Test]
+ public void TypeAnalysis()
+ {
+ TestFile(@"..\..\Tests\TypeAnalysisTests.cs");
+ }
+
static void TestFile(string fileName)
{
string code = File.ReadAllText(fileName);
View
27 ICSharpCode.Decompiler/Tests/TypeAnalysisTests.cs
@@ -0,0 +1,27 @@
+// Copyright (c) AlphaSierraPapa for the SharpDevelop Team
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy of this
+// software and associated documentation files (the "Software"), to deal in the Software
+// without restriction, including without limitation the rights to use, copy, modify, merge,
+// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
+// to whom the Software is furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in all copies or
+// substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
+// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
+// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
+// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+// DEALINGS IN THE SOFTWARE.
+
+using System;
+
+public class TypeAnalysisTests
+{
+ public byte SubtractFrom256(byte b)
+ {
+ return (byte)(256 - (int)b);
+ }
+}

0 comments on commit 2783b02

Please sign in to comment.
Something went wrong with that request. Please try again.