ART: Correctly make methods preverified

Bug: 16828525

(cherry picked from commit df1532b9ba0cda2d00b78fbdef461f8a6cf8a737)

Change-Id: I66756348b2aa50e41dacca59769b6810a91c73b0
diff --git a/runtime/class_linker.cc b/runtime/class_linker.cc
index 4342234..42e0899 100644
--- a/runtime/class_linker.cc
+++ b/runtime/class_linker.cc
@@ -3532,6 +3532,7 @@
   // Don't attempt to re-verify if already sufficiently verified.
   if (klass->IsVerified() ||
       (klass->IsCompileTimeVerified() && Runtime::Current()->IsCompiler())) {
+    EnsurePreverifiedMethods(klass);
     return;
   }
 
@@ -3554,6 +3555,7 @@
   // Skip verification if disabled.
   if (!Runtime::Current()->IsVerificationEnabled()) {
     klass->SetStatus(mirror::Class::kStatusVerified, self);
+    EnsurePreverifiedMethods(klass);
     return;
   }
 
@@ -3656,7 +3658,14 @@
     // Note: we're going here during compilation and at runtime. When we set the
     // kAccPreverified flag when compiling image classes, the flag is recorded
     // in the image and is set when loading the image.
+    EnsurePreverifiedMethods(klass);
+  }
+}
+
+void ClassLinker::EnsurePreverifiedMethods(ConstHandle<mirror::Class> klass) {
+  if ((klass->GetAccessFlags() & kAccPreverified) == 0) {
     klass->SetPreverifiedFlagOnAllMethods();
+    klass->SetAccessFlags(klass->GetAccessFlags() | kAccPreverified);
   }
 }
 
@@ -3790,7 +3799,8 @@
   }
   DCHECK(klass->GetClass() != NULL);
   klass->SetObjectSize(sizeof(mirror::Proxy));
-  klass->SetAccessFlags(kAccClassIsProxy | kAccPublic | kAccFinal);
+  // Set the class access flags incl. preverified, so we do not try to set the flag on the methods.
+  klass->SetAccessFlags(kAccClassIsProxy | kAccPublic | kAccFinal | kAccPreverified);
   klass->SetClassLoader(soa.Decode<mirror::ClassLoader*>(loader));
   DCHECK_EQ(klass->GetPrimitiveType(), Primitive::kPrimNot);
   klass->SetName(soa.Decode<mirror::String*>(name));
@@ -4356,6 +4366,7 @@
                                     bool can_init_parents) {
   DCHECK(c.Get() != nullptr);
   if (c->IsInitialized()) {
+    EnsurePreverifiedMethods(c);
     return true;
   }
   const bool success = InitializeClass(c, can_init_fields, can_init_parents);