Revision: 9390
Author: gwt.mirror...@gmail.com
Date: Thu Dec  9 09:46:57 2010
Log: Guard against invalid dispIds in JavaDispatchImpl

Also, rather than write an invalid dispId and wait for things to blow up, throw
a HostedModeException if we hit a JSNI ref that shouldn't be there

Review at http://gwt-code-reviews.appspot.com/1172801

Review by: sco...@google.com
http://code.google.com/p/google-web-toolkit/source/detail?r=9390

Added:
/trunk/user/src/com/google/gwt/user/client/rpc/core/java/util/ArraysViolator.java
 /trunk/user/test/com/google/gwt/emultest/java/math/BigIntegerViolator.java
 /trunk/user/test/com/google/gwt/emultest/java/util/TreeMapViolator.java
Modified:
 /trunk/dev/core/src/com/google/gwt/dev/shell/JavaDispatchImpl.java
 /trunk/dev/core/src/com/google/gwt/dev/shell/Jsni.java
 /trunk/user/src/com/google/gwt/user/client/rpc/core/java/util/Arrays.java
 /trunk/user/super/com/google/gwt/emul/java/util/Random.java
/trunk/user/test/com/google/gwt/emultest/java/math/BigIntegerConstructorsTest.java
 /trunk/user/test/com/google/gwt/emultest/java/util/TreeMapTest.java

=======================================
--- /dev/null
+++ /trunk/user/src/com/google/gwt/user/client/rpc/core/java/util/ArraysViolator.java Thu Dec 9 09:46:57 2010
@@ -0,0 +1,28 @@
+/*
+ * Copyright 2010 Google Inc.
+ *
+ * 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 com.google.gwt.user.client.rpc.core.java.util;
+
+import java.util.List;
+
+/**
+ * Used to delay referencing methods only present in the emulated JRE until they
+ * are actually used.
+ */
+public class ArraysViolator {
+  public static native Object[] getArray0(List<?> instance) /*-{
+    return instan...@java.util.arrays$arraylist::array;
+  }-*/;
+}
=======================================
--- /dev/null
+++ /trunk/user/test/com/google/gwt/emultest/java/math/BigIntegerViolator.java Thu Dec 9 09:46:57 2010
@@ -0,0 +1,29 @@
+/*
+ * Copyright 2010 Google Inc.
+ *
+ * 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 com.google.gwt.emultest.java.math;
+
+import java.math.BigInteger;
+
+/**
+ * Used to delay referencing methods only present in the emulated JRE until they
+ * are actually used.
+ */
+public class BigIntegerViolator {
+
+  public static native BigInteger fromDouble(double v) /*-{
+    return @java.math.BigInteger::valueOf(D)(v);
+  }-*/;
+}
=======================================
--- /dev/null
+++ /trunk/user/test/com/google/gwt/emultest/java/util/TreeMapViolator.java Thu Dec 9 09:46:57 2010
@@ -0,0 +1,30 @@
+/*
+ * Copyright 2010 Google Inc.
+ *
+ * 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 com.google.gwt.emultest.java.util;
+
+import java.util.Map;
+
+/**
+ * Used to delay referencing methods only present in the emulated JRE until they
+ * are actually used.
+ */
+public class TreeMapViolator {
+  // Use JSNI to call a special method on our implementation of TreeMap.
+  @SuppressWarnings("unchecked") // raw Map
+  public static native void callAssertCorrectness(Map map) /*-{
+    m...@java.util.treemap::assertCorrectness()();
+  }-*/;
+}
=======================================
--- /trunk/dev/core/src/com/google/gwt/dev/shell/JavaDispatchImpl.java Tue Jun 22 06:26:45 2010 +++ /trunk/dev/core/src/com/google/gwt/dev/shell/JavaDispatchImpl.java Thu Dec 9 09:46:57 2010
@@ -61,6 +61,9 @@
    * @return the field
    */
   public Field getField(int dispId) {
+    if (dispId < 0) {
+      throw new RuntimeException("Field does not exist.");
+    }
     Member member = getMember(dispId);

     if (member instanceof SyntheticClassMember) {
@@ -82,6 +85,10 @@
    * @throws IllegalArgumentException
    */
   public Object getFieldValue(int dispId) {
+    if (dispId < 0) {
+      throw new RuntimeException("Field does not exist.");
+    }
+
     Member member = getMember(dispId);

     if (member instanceof SyntheticClassMember) {
@@ -103,6 +110,10 @@
    * @return the method
    */
   public MethodAdaptor getMethod(int dispId) {
+    if (dispId < 0) {
+      throw new RuntimeException("Method does not exist.");
+    }
+
     Member m = getMember(dispId);
     if (m instanceof Method) {
       return new MethodAdaptor((Method) m);
=======================================
--- /trunk/dev/core/src/com/google/gwt/dev/shell/Jsni.java Tue Jun 22 06:26:45 2010 +++ /trunk/dev/core/src/com/google/gwt/dev/shell/Jsni.java Thu Dec 9 09:46:57 2010
@@ -106,13 +106,18 @@

         Member member;
         if (dispId < 0) {
- // We've already emitted a warning from getDispId; just fake the jsni
           member = null;
         } else {
member = dispatchInfo.getClassInfoByDispId(dispId).getMember(dispId);
         }
-
-        if (member == null || member instanceof Field
+
+        if (member == null) {
+          throw new HostedModeException(
+ "JSNI rewriter found reference to non-existent field in a field reference or java method tear-off: "
+                  + ident + " at " + x.getSourceInfo());
+        }
+
+        if (member instanceof Field
             || member instanceof SyntheticClassMember) {
           if (q != null) {
             accept(q);
@@ -176,18 +181,21 @@
member = dispatchInfo.getClassInfoByDispId(dispId).getMember(dispId);
           }

+          if (member == null) {
+            throw new HostedModeException(
+ "JSNI rewriter found reference to non-existent field in a method invocation: "
+                    + ref.getIdent() + " at " + ref.getSourceInfo());
+          }
+
           /*
* Make sure the ident is a reference to a method or constructor and
            * not a reference to a field whose contents (e.g. a Function) we
            * intend to immediately invoke.
            *
            * p.C::method()(); versus p.C::field();
-           *
- * Also, if the reference was to a non-existent field, we'll go ahead
-           * and rewrite the call site as though -1 is a valid dispid.
+           *
            */
-          if (member == null || member instanceof Method
-              || member instanceof Constructor<?>) {
+ if (member instanceof Method || member instanceof Constructor<?>) {

             // Use a clone instead of modifying the original JSNI
             // __gwt_makeJavaInvoke(paramCount)(obj, dispId, args)
=======================================
--- /trunk/user/src/com/google/gwt/user/client/rpc/core/java/util/Arrays.java Mon Sep 20 15:25:50 2010 +++ /trunk/user/src/com/google/gwt/user/client/rpc/core/java/util/Arrays.java Thu Dec 9 09:46:57 2010
@@ -60,16 +60,12 @@
       Object[] array;
       if (GWT.isScript()) {
         // Violator pattern.
-        array = getArray0(instance);
+        array = ArraysViolator.getArray0(instance);
       } else {
         // Clone the underlying array.
         array = instance.toArray();
       }
       streamWriter.writeObject(array);
     }
-
-    private static native Object[] getArray0(List<?> instance) /*-{
-      return instan...@java.util.arrays$arraylist::array;
-    }-*/;
   }
 }
=======================================
--- /trunk/user/super/com/google/gwt/emul/java/util/Random.java Fri Jan 15 12:29:09 2010 +++ /trunk/user/super/com/google/gwt/emul/java/util/Random.java Thu Dec 9 05:31:18 2010
@@ -34,14 +34,14 @@
  */
 package java.util;

-import java.io.Serializable;
-
 /**
* This class provides methods that generates pseudo-random numbers of different * types, such as {...@code int}, {...@code long}, {...@code double}, and {...@code float}.
  * It follows the algorithms specified in the JRE javadoc.
+ *
+ * This emulated version of Random is not serializable.
  */
-public class Random implements Serializable {
+public class Random {

   private static final double multiplierHi = 0x5de;
   private static final double multiplierLo = 0xece66d;
@@ -90,29 +90,21 @@

   /**
* The boolean value indicating if the second Gaussian number is available.
-   *
-   * @serial
    */
   private boolean haveNextNextGaussian = false;

   /**
    * The second Gaussian generated number.
-   *
-   * @serial
    */
   private double nextNextGaussian;

   /**
    * The high 24 bits of the 48=bit seed value.
-   *
-   * @serial It is associated with the internal state of this generator.
    */
   private double seedhi;

   /**
    * The low 24 bits of the 48=bit seed value.
-   *
-   * @serial It is associated with the internal state of this generator.
    */
   private double seedlo;

=======================================
--- /trunk/user/test/com/google/gwt/emultest/java/math/BigIntegerConstructorsTest.java Mon Jun 7 12:20:31 2010 +++ /trunk/user/test/com/google/gwt/emultest/java/math/BigIntegerConstructorsTest.java Thu Dec 9 05:31:18 2010
@@ -786,29 +786,25 @@
       // JRE implementation doesn't have the method tested here
       return;
     }
-    BigInteger val = fromDouble(1.0);
+    BigInteger val = BigIntegerViolator.fromDouble(1.0);
     assertEquals("1", val.toString());
-    val = fromDouble(100.0);
+    val = BigIntegerViolator.fromDouble(100.0);
     assertEquals("100", val.toString());
-    val = fromDouble(2147483647.0);
+    val = BigIntegerViolator.fromDouble(2147483647.0);
     assertEquals("2147483647", val.toString());
-    val = fromDouble(-2147483647.0);
+    val = BigIntegerViolator.fromDouble(-2147483647.0);
     assertEquals("-2147483647", val.toString());
-    val = fromDouble(2147483648.0);
+    val = BigIntegerViolator.fromDouble(2147483648.0);
     assertEquals("2147483648", val.toString());
-    val = fromDouble(-2147483648.0);
+    val = BigIntegerViolator.fromDouble(-2147483648.0);
     assertEquals("-2147483648", val.toString());
-    val = fromDouble(4294967295.0);
+    val = BigIntegerViolator.fromDouble(4294967295.0);
     assertEquals("4294967295", val.toString());
-    val = fromDouble(-4294967295.0);
+    val = BigIntegerViolator.fromDouble(-4294967295.0);
     assertEquals("-4294967295", val.toString());
-    val = fromDouble(4294967296.0);
+    val = BigIntegerViolator.fromDouble(4294967296.0);
     assertEquals("4294967296", val.toString());
-    val = fromDouble(-4294967296.0);
+    val = BigIntegerViolator.fromDouble(-4294967296.0);
     assertEquals("-4294967296", val.toString());
   }
-
-  private native BigInteger fromDouble(double v) /*-{
-    return @java.math.BigInteger::valueOf(D)(v);
-  }-*/;
-}
+}
=======================================
--- /trunk/user/test/com/google/gwt/emultest/java/util/TreeMapTest.java Fri Apr 2 04:07:16 2010 +++ /trunk/user/test/com/google/gwt/emultest/java/util/TreeMapTest.java Thu Dec 9 05:31:18 2010
@@ -135,12 +135,6 @@
assertEquals(expected.entrySet().toArray(), actual.entrySet().toArray());
     assertEquals(expected.values().toArray(), actual.values().toArray());
   }
-
-  // Use JSNI to call a special method on our implementation of TreeMap.
-  @SuppressWarnings("unchecked") // raw Map
-  private static native void callAssertCorrectness(Map map) /*-{
-    m...@java.util.treemap::assertCorrectness()();
-  }-*/;

   /**
* Create the expected return of toString for a Map containing only the passed
@@ -1868,7 +1862,7 @@
   protected void verifyMap() {
     if (GWT.isScript()) {
       // Verify red-black correctness in our implementation
-      callAssertCorrectness(map);
+      TreeMapViolator.callAssertCorrectness(map);
     }
     super.verifyMap();
   }

--
http://groups.google.com/group/Google-Web-Toolkit-Contributors

Reply via email to