Avoid method overriding if its super method is inaccessible.

A call to package private method could be redirected to subclass which
was not in the same package.
Modified vtable to retain virtual super methods which cannot be accessed.

This change affects vtable index in optimized dex.

Change-Id: I9cc7e309c305bca12e5061009c4245fb70014681
Signed-off-by: Im Sooin <ciecet@gmail.com>
diff --git a/tests/300-package-override/expected.txt b/tests/300-package-override/expected.txt
new file mode 100644
index 0000000..b0aad4d
--- /dev/null
+++ b/tests/300-package-override/expected.txt
@@ -0,0 +1 @@
+passed
diff --git a/tests/300-package-override/info.txt b/tests/300-package-override/info.txt
new file mode 100644
index 0000000..0ed59eb
--- /dev/null
+++ b/tests/300-package-override/info.txt
@@ -0,0 +1,2 @@
+Tests a dalvik bug where we'd allow subclasses to override package-protected
+methods.
\ No newline at end of file
diff --git a/tests/300-package-override/src/Main.java b/tests/300-package-override/src/Main.java
new file mode 100644
index 0000000..ad7eaaf
--- /dev/null
+++ b/tests/300-package-override/src/Main.java
@@ -0,0 +1,22 @@
+/*
+ * Copyright (C) 2012 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.
+ */
+
+public class Main {
+  public static void main(String args[]) throws Exception {
+    p1.BaseClass c = new p2.DerivedClass();
+    c.run();
+  }
+}
diff --git a/tests/300-package-override/src/p1/BaseClass.java b/tests/300-package-override/src/p1/BaseClass.java
new file mode 100644
index 0000000..1c048ac
--- /dev/null
+++ b/tests/300-package-override/src/p1/BaseClass.java
@@ -0,0 +1,22 @@
+/*
+ * Copyright (C) 2012 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 BaseClass {
+  public void run() { foo(); }
+  void foo() { System.out.println("passed"); } // It should not be possible to override this.
+}
diff --git a/tests/300-package-override/src/p2/DerivedClass.java b/tests/300-package-override/src/p2/DerivedClass.java
new file mode 100644
index 0000000..860f50c
--- /dev/null
+++ b/tests/300-package-override/src/p2/DerivedClass.java
@@ -0,0 +1,21 @@
+/*
+ * Copyright (C) 2012 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 p2;
+
+public class DerivedClass extends p1.BaseClass {
+  void foo() { System.out.println("DerivedClass overrode package-private method!"); } // This should not override BaseClass.foo.
+}
diff --git a/vm/oo/Class.cpp b/vm/oo/Class.cpp
index 3a721b6..cc31e4e 100644
--- a/vm/oo/Class.cpp
+++ b/vm/oo/Class.cpp
@@ -2913,8 +2913,8 @@
             for (si = 0; si < clazz->super->vtableCount; si++) {
                 Method* superMeth = clazz->vtable[si];
 
-                if (dvmCompareMethodNamesAndProtos(localMeth, superMeth) == 0)
-                {
+                if (dvmCompareMethodNamesAndProtos(localMeth, superMeth) == 0 &&
+                        dvmCheckMethodAccess(clazz, superMeth)) {
                     /* verify */
                     if (dvmIsFinalMethod(superMeth)) {
                         ALOGW("Method %s.%s overrides final %s.%s",