JIT: fix handling of newarr size (#19633)
authorAndy Ayers <andya@microsoft.com>
Fri, 24 Aug 2018 02:35:33 +0000 (19:35 -0700)
committerGitHub <noreply@github.com>
Fri, 24 Aug 2018 02:35:33 +0000 (19:35 -0700)
`newarr` accepts either an int or native int for array size, and the
newarr helper the jit invokes takes a native int.

If the value on the stack is an int and the jit is running on a platform
where native int is larger than int, the jit must explicitly widen the
int to native int.

Found this while running some code through the interpreter -- the path
the interpreter uses to invoke jitted code doesn't guarantee that int args
are 64 bit clean.

src/jit/importer.cpp

index 5d00c70..7636410 100644 (file)
@@ -14285,6 +14285,23 @@ void Compiler::impImportBlockCode(BasicBlock* block)
                 op2 = impPopStack().val;
                 assertImp(genActualTypeIsIntOrI(op2->gtType));
 
+#ifdef _TARGET_64BIT_
+                // The array helper takes a native int for array length.
+                // So if we have an int, explicitly extend it to be a native int.
+                if (genActualType(op2->TypeGet()) != TYP_I_IMPL)
+                {
+                    if (op2->IsIntegralConst())
+                    {
+                        op2->gtType = TYP_I_IMPL;
+                    }
+                    else
+                    {
+                        bool isUnsigned = false;
+                        op2             = gtNewCastNode(TYP_I_IMPL, op2, isUnsigned, TYP_I_IMPL);
+                    }
+                }
+#endif // _TARGET_64BIT_
+
 #ifdef FEATURE_READYTORUN_COMPILER
                 if (opts.IsReadyToRun())
                 {