Merge "Fix tests flakiness with jit when using Proxy classes."
diff --git a/compiler/optimizing/inliner.cc b/compiler/optimizing/inliner.cc
index 0aaa6b3..353881e 100644
--- a/compiler/optimizing/inliner.cc
+++ b/compiler/optimizing/inliner.cc
@@ -494,6 +494,26 @@
                        << " it is in a different dex file and requires access to the dex cache";
         return false;
       }
+
+      if (current->IsNewInstance() &&
+          (current->AsNewInstance()->GetEntrypoint() == kQuickAllocObjectWithAccessCheck)) {
+        // Allocation entrypoint does not handle inlined frames.
+        return false;
+      }
+
+      if (current->IsNewArray() &&
+          (current->AsNewArray()->GetEntrypoint() == kQuickAllocArrayWithAccessCheck)) {
+        // Allocation entrypoint does not handle inlined frames.
+        return false;
+      }
+
+      if (current->IsUnresolvedStaticFieldGet() ||
+          current->IsUnresolvedInstanceFieldGet() ||
+          current->IsUnresolvedStaticFieldSet() ||
+          current->IsUnresolvedInstanceFieldSet()) {
+        // Entrypoint for unresolved fields does not handle inlined frames.
+        return false;
+      }
     }
   }
   number_of_inlined_instructions_ += number_of_instructions;
diff --git a/compiler/optimizing/load_store_elimination.cc b/compiler/optimizing/load_store_elimination.cc
index 90f28e5..6fbb682 100644
--- a/compiler/optimizing/load_store_elimination.cc
+++ b/compiler/optimizing/load_store_elimination.cc
@@ -59,7 +59,7 @@
           (use->IsInstanceFieldSet() && (reference_ == use->InputAt(1))) ||
           (use->IsUnresolvedInstanceFieldSet() && (reference_ == use->InputAt(1))) ||
           (use->IsStaticFieldSet() && (reference_ == use->InputAt(1))) ||
-          (use->IsUnresolvedStaticFieldSet() && (reference_ == use->InputAt(1))) ||
+          (use->IsUnresolvedStaticFieldSet() && (reference_ == use->InputAt(0))) ||
           (use->IsArraySet() && (reference_ == use->InputAt(2)))) {
         // reference_ is merged to a phi, passed to a callee, or stored to heap.
         // reference_ isn't the only name that can refer to its value anymore.
diff --git a/compiler/optimizing/nodes.h b/compiler/optimizing/nodes.h
index 7df5866..0f2c1cf 100644
--- a/compiler/optimizing/nodes.h
+++ b/compiler/optimizing/nodes.h
@@ -4750,6 +4750,9 @@
     return generate_clinit_check_;
   }
   void SetMustGenerateClinitCheck(bool generate_clinit_check) {
+    // The entrypoint the code generator is going to call does not do
+    // clinit of the class.
+    DCHECK(!NeedsAccessCheck());
     generate_clinit_check_ = generate_clinit_check;
   }
 
diff --git a/test/542-unresolved-access-check/expected.txt b/test/542-unresolved-access-check/expected.txt
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/test/542-unresolved-access-check/expected.txt
diff --git a/test/542-unresolved-access-check/info.txt b/test/542-unresolved-access-check/info.txt
new file mode 100644
index 0000000..30d45b8
--- /dev/null
+++ b/test/542-unresolved-access-check/info.txt
@@ -0,0 +1 @@
+Test unresolved/access checks entry points with the JIT.
diff --git a/test/542-unresolved-access-check/src/Main.java b/test/542-unresolved-access-check/src/Main.java
new file mode 100644
index 0000000..2bdf47f
--- /dev/null
+++ b/test/542-unresolved-access-check/src/Main.java
@@ -0,0 +1,104 @@
+/*
+ * Copyright (C) 2015 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+import java.util.List;
+import p1.InP1;
+import p1.PlaceHolder;
+
+
+// Custom class loader to prevent loading while verifying.
+class MyClassLoader extends ClassLoader {
+  MyClassLoader() throws Exception {
+    super(MyClassLoader.class.getClassLoader());
+
+    // Some magic to get access to the pathList field of BaseDexClassLoader.
+    ClassLoader loader = getClass().getClassLoader();
+    Class<?> baseDexClassLoader = loader.getClass().getSuperclass();
+    Field f = baseDexClassLoader.getDeclaredField("pathList");
+    f.setAccessible(true);
+    Object pathList = f.get(loader);
+
+    // Some magic to get access to the dexField field of pathList.
+    f = pathList.getClass().getDeclaredField("dexElements");
+    f.setAccessible(true);
+    dexElements = (Object[]) f.get(pathList);
+    dexFileField = dexElements[0].getClass().getDeclaredField("dexFile");
+    dexFileField.setAccessible(true);
+  }
+
+  Object[] dexElements;
+  Field dexFileField;
+
+  protected Class<?> loadClass(String className, boolean resolve) throws ClassNotFoundException {
+    if (className.equals("p1.OtherInP1") && !p1.PlaceHolder.entered) {
+      // The request comes from the verifier. Return null to get the access check entry
+      // point in the compiled code.
+      return null;
+    }
+    // Mimic what DexPathList.findClass is doing.
+    try {
+      for (Object element : dexElements) {
+        Object dex = dexFileField.get(element);
+        Method method = dex.getClass().getDeclaredMethod(
+            "loadClassBinaryName", String.class, ClassLoader.class, List.class);
+
+        if (dex != null) {
+          Class clazz = (Class)method.invoke(dex, className, this, null);
+          if (clazz != null) {
+            return clazz;
+          }
+        }
+      }
+    } catch (Exception e) { /* Ignore */ }
+    return getParent().loadClass(className);
+  }
+}
+
+public class Main {
+    public static void main(String[] args) throws Exception {
+      MyClassLoader o = new MyClassLoader();
+      Class foo = o.loadClass("LoadedByMyClassLoader");
+      Method m = foo.getDeclaredMethod("main");
+      m.invoke(null);
+    }
+}
+
+class LoadedByMyClassLoader {
+    public static void main() throws Exception {
+      for (int i = 0; i < 10000; ++i) {
+        // Warm up the JIT.
+        doTheCall(i);
+      }
+      // Sleep a while to let the JIT compile things.
+      // TODO(ngeoffray): Remove the sleep. b/25414532
+      Thread.sleep(2000);
+      doTheCall(10001);
+    }
+
+    public static void doTheCall(int i) {
+      InP1.$inline$AllocateOtherInP1(i);
+      InP1.$inline$AllocateArrayOtherInP1(i);
+      InP1.$inline$UseStaticFieldOtherInP1(i);
+      InP1.$inline$SetStaticFieldOtherInP1(i);
+      InP1.$inline$UseInstanceFieldOtherInP1(i);
+      InP1.$inline$SetInstanceFieldOtherInP1(i);
+      InP1.$inline$LoadOtherInP1(i);
+      InP1.$inline$StaticCallOtherInP1(i);
+      InP1.$inline$InstanceCallOtherInP1(i);
+    }
+}
diff --git a/test/542-unresolved-access-check/src/p1/InP1.java b/test/542-unresolved-access-check/src/p1/InP1.java
new file mode 100644
index 0000000..3516c72
--- /dev/null
+++ b/test/542-unresolved-access-check/src/p1/InP1.java
@@ -0,0 +1,93 @@
+/*
+ * Copyright (C) 2015 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package p1;
+
+public class InP1 {
+    public static Object $inline$AllocateOtherInP1(int i) {
+      // Let this method execute a while to make sure the JIT sees it hot.
+      if (i <= 10000) {
+        return null;
+      }
+      // Set the flag that we have entered InP1 code to get OtherInP1 loaded.
+      PlaceHolder.entered = true;
+      return new OtherInP1();
+    }
+
+    public static Object $inline$AllocateArrayOtherInP1(int i) {
+      if (i <= 10000) {
+        return null;
+      }
+      return new OtherInP1[10];
+    }
+
+    public static Object $inline$UseStaticFieldOtherInP1(int i) {
+      if (i <= 10000) {
+        return null;
+      }
+      return OtherInP1.staticField;
+    }
+
+    public static void $inline$SetStaticFieldOtherInP1(int i) {
+      if (i <= 10000) {
+        return;
+      }
+      OtherInP1.staticField = new Object();
+    }
+
+    public static Object $inline$UseInstanceFieldOtherInP1(int i) {
+      if (i <= 10000) {
+        return null;
+      }
+      return $noinline$AllocateOtherInP1().instanceField;
+    }
+
+    public static void $inline$SetInstanceFieldOtherInP1(int i) {
+      if (i <= 10000) {
+        return;
+      }
+      $noinline$AllocateOtherInP1().instanceField = new Object();
+    }
+
+    public static OtherInP1 $noinline$AllocateOtherInP1() {
+      try {
+        return new OtherInP1();
+      } catch (Exception e) {
+        throw new Error(e);
+      }
+    }
+
+    public static Object $inline$LoadOtherInP1(int i) {
+      if (i <= 10000) {
+        return null;
+      }
+      return OtherInP1.class;
+    }
+
+    public static Object $inline$StaticCallOtherInP1(int i) {
+      if (i <= 10000) {
+        return null;
+      }
+      return OtherInP1.doTheStaticCall();
+    }
+
+    public static Object $inline$InstanceCallOtherInP1(int i) {
+      if (i <= 10000) {
+        return null;
+      }
+      return $noinline$AllocateOtherInP1().doTheInstanceCall();
+    }
+}
diff --git a/test/542-unresolved-access-check/src/p1/OtherInP1.java b/test/542-unresolved-access-check/src/p1/OtherInP1.java
new file mode 100644
index 0000000..adc1ce1
--- /dev/null
+++ b/test/542-unresolved-access-check/src/p1/OtherInP1.java
@@ -0,0 +1,32 @@
+/*
+ * Copyright (C) 2015 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package p1;
+
+class OtherInP1 {
+  OtherInP1() {
+  }
+  static Object staticField = new Object();
+  Object instanceField = new Object();
+
+  static Object doTheStaticCall() {
+    return null;
+  }
+
+  Object doTheInstanceCall() {
+    return null;
+  }
+}
diff --git a/test/542-unresolved-access-check/src/p1/PlaceHolder.java b/test/542-unresolved-access-check/src/p1/PlaceHolder.java
new file mode 100644
index 0000000..2bf4bdf
--- /dev/null
+++ b/test/542-unresolved-access-check/src/p1/PlaceHolder.java
@@ -0,0 +1,24 @@
+/*
+ * Copyright (C) 2015 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package p1;
+
+// Specific class for putting the 'entered' marker. If we were to put the marker
+// in InP1 or in OtherInP1, the code in MyClassLoader using that marker would load
+// InP1 or OtherInP1 in the system class loader, and not in MyClassLoader.
+public class PlaceHolder {
+  public static boolean entered = false;
+}