Disallow sending JS functions to a different thread (608142, r=jst). a=beta7+
authorAndreas Gal <gal@mozilla.com>
Fri, 29 Oct 2010 15:03:44 -0700
changeset 56710 05e775f368f8
parent 56709 de32b6e1ca00
child 56711 67dd5fbeebaa
push id16663
push userjst@mozilla.com
push dateSun, 31 Oct 2010 05:12:52 +0000
treeherdermozilla-central@67dd5fbeebaa [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjst, beta7
bugs608142
milestone2.0b8pre
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Disallow sending JS functions to a different thread (608142, r=jst). a=beta7+
js/src/xpconnect/src/xpc.msg
js/src/xpconnect/src/xpcprivate.h
js/src/xpconnect/src/xpcwrappedjs.cpp
js/src/xpconnect/tests/unit/test_bug608142.js
xpcom/base/nsError.h
--- a/js/src/xpconnect/src/xpc.msg
+++ b/js/src/xpconnect/src/xpc.msg
@@ -210,14 +210,15 @@ XPC_MSG_DEF(NS_ERROR_CACHE_DATA_IS_NOT_S
 XPC_MSG_DEF(NS_ERROR_CACHE_WAIT_FOR_VALIDATION      , "Cache entry exists but needs to be validated first")
 XPC_MSG_DEF(NS_ERROR_CACHE_ENTRY_DOOMED             , "Cache entry has been  doomed")
 XPC_MSG_DEF(NS_ERROR_CACHE_READ_ACCESS_DENIED       , "Read access to cache denied")
 XPC_MSG_DEF(NS_ERROR_CACHE_WRITE_ACCESS_DENIED      , "Write access to cache denied")
 XPC_MSG_DEF(NS_ERROR_CACHE_IN_USE                   , "Cache is currently in use")
 XPC_MSG_DEF(NS_ERROR_DOCUMENT_NOT_CACHED            , "Document does not exist in cache")
 XPC_MSG_DEF(NS_ERROR_INSUFFICIENT_DOMAIN_LEVELS     , "The requested number of domain levels exceeds those present in the host string")
 XPC_MSG_DEF(NS_ERROR_HOST_IS_IP_ADDRESS             , "The host string is an IP address")
+XPC_MSG_DEF(NS_ERROR_NOT_SAME_THREAD                , "Can't access a wrapped JS object from a different thread")
 
 /* storage related codes (from mozStorage.h) */
 XPC_MSG_DEF(NS_ERROR_STORAGE_BUSY                   , "SQLite database connection is busy")
 XPC_MSG_DEF(NS_ERROR_STORAGE_IOERR                  , "SQLite encountered an IO error")
 XPC_MSG_DEF(NS_ERROR_STORAGE_CONSTRAINT             , "SQLite database operation failed because a constraint was violated")
 
--- a/js/src/xpconnect/src/xpcprivate.h
+++ b/js/src/xpconnect/src/xpcprivate.h
@@ -3066,16 +3066,17 @@ protected:
    void Unlink();
 
 private:
     JSObject* mJSObj;
     nsXPCWrappedJSClass* mClass;
     nsXPCWrappedJS* mRoot;
     nsXPCWrappedJS* mNext;
     nsISupports* mOuter;    // only set in root
+    nsCOMPtr<nsIThread> mThread;
 };
 
 /***************************************************************************/
 
 class XPCJSObjectHolder : public nsIXPConnectJSObjectHolder,
                           public XPCRootSetElem
 {
 public:
--- a/js/src/xpconnect/src/xpcwrappedjs.cpp
+++ b/js/src/xpconnect/src/xpcwrappedjs.cpp
@@ -38,16 +38,17 @@
  * the terms of any one of the MPL, the GPL or the LGPL.
  *
  * ***** END LICENSE BLOCK ***** */
 
 /* Class that wraps JS objects to appear as XPCOM objects. */
 
 #include "xpcprivate.h"
 #include "nsAtomicRefcnt.h"
+#include "nsThreadUtils.h"
 
 // NOTE: much of the fancy footwork is done in xpcstubs.cpp
 
 NS_IMPL_CYCLE_COLLECTION_CLASS(nsXPCWrappedJS)
 
 NS_IMETHODIMP
 NS_CYCLE_COLLECTION_CLASSNAME(nsXPCWrappedJS)::Traverse
    (void *p, nsCycleCollectionTraversalCallback &cb)
@@ -285,16 +286,17 @@ nsXPCWrappedJS::GetWeakReference(nsIWeak
     return nsSupportsWeakReference::GetWeakReference(aInstancePtr);
 }
 
 NS_IMETHODIMP
 nsXPCWrappedJS::GetJSObject(JSObject** aJSObj)
 {
     NS_PRECONDITION(aJSObj, "bad param");
     NS_PRECONDITION(mJSObj, "bad wrapper");
+
     if(!(*aJSObj = mJSObj))
         return NS_ERROR_OUT_OF_MEMORY;
     return NS_OK;
 }
 
 // static
 nsresult
 nsXPCWrappedJS::GetNewOrUsed(XPCCallContext& ccx,
@@ -428,17 +430,18 @@ nsXPCWrappedJS::nsXPCWrappedJS(XPCCallCo
                                JSObject* aJSObj,
                                nsXPCWrappedJSClass* aClass,
                                nsXPCWrappedJS* root,
                                nsISupports* aOuter)
     : mJSObj(aJSObj),
       mClass(aClass),
       mRoot(root ? root : this),
       mNext(nsnull),
-      mOuter(root ? nsnull : aOuter)
+      mOuter(root ? nsnull : aOuter),
+      mThread(do_GetCurrentThread())
 {
 #ifdef DEBUG_stats_jband
     static int count = 0;
     static const int interval = 10;
     if(0 == (++count % interval))
         printf("//////// %d instances of nsXPCWrappedJS created\n", count);
 #endif
 
@@ -563,16 +566,18 @@ nsXPCWrappedJS::GetInterfaceInfo(nsIInte
 
 NS_IMETHODIMP
 nsXPCWrappedJS::CallMethod(PRUint16 methodIndex,
                            const XPTMethodDescriptor* info,
                            nsXPTCMiniVariant* params)
 {
     if(!IsValid())
         return NS_ERROR_UNEXPECTED;
+    if (NS_GetCurrentThread() != mThread)
+        return NS_ERROR_NOT_SAME_THREAD;
     return GetClass()->CallMethod(this, methodIndex, info, params);
 }
 
 NS_IMETHODIMP
 nsXPCWrappedJS::GetInterfaceIID(nsIID** iid)
 {
     NS_PRECONDITION(iid, "bad param");
 
new file mode 100644
--- /dev/null
+++ b/js/src/xpconnect/tests/unit/test_bug608142.js
@@ -0,0 +1,51 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
+ * vim: sw=4 ts=4 sts=4 et
+ * ***** BEGIN LICENSE BLOCK *****
+ * Version: MPL 1.1/GPL 2.0/LGPL 2.1
+ *
+ * The contents of this file are subject to the Mozilla Public License Version
+ * 1.1 (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.mozilla.org/MPL/
+ *
+ * Software distributed under the License is distributed on an "AS IS" basis,
+ * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
+ * for the specific language governing rights and limitations under the
+ * License.
+ *
+ * The Original Code is Necko Test Code.
+ *
+ * The Initial Developer of the Original Code is the Mozilla Foundation.
+ * Portions created by the Initial Developer are Copyright (C) 2009
+ * the Initial Developer. All Rights Reserved.
+ *
+ * Contributor(s):
+ *   Andreas Gal <gal@uci.edu>
+ *
+ * Alternatively, the contents of this file may be used under the terms of
+ * either the GNU General Public License Version 2 or later (the "GPL"), or
+ * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
+ * in which case the provisions of the GPL or the LGPL are applicable instead
+ * of those above. If you wish to allow use of your version of this file only
+ * under the terms of either the GPL or the LGPL, and not to allow others to
+ * use your version of this file under the terms of the MPL, indicate your
+ * decision by deleting the provisions above and replace them with the notice
+ * and other provisions required by the GPL or the LGPL. If you do not delete
+ * the provisions above, a recipient may use your version of this file under
+ * the terms of any one of the MPL, the GPL or the LGPL.
+ *
+ * ***** END LICENSE BLOCK ***** */
+
+function run_test() { 
+    var tm = Components.classes["@mozilla.org/thread-manager;1"].getService();
+    var thr = tm.newThread(0);
+
+    thr.dispatch({
+        run: function() {
+            do_check_true(false);
+        }
+    }, Components.interfaces.nsIThread.DISPATCH_NORMAL);
+
+    thr.shutdown();
+}
+
--- a/xpcom/base/nsError.h
+++ b/xpcom/base/nsError.h
@@ -302,16 +302,20 @@ inline int NS_SUCCEEDED(nsresult _nsresu
  /* Result codes used by nsIVariant */
 
 #define NS_ERROR_CANNOT_CONVERT_DATA            NS_ERROR_GENERATE_FAILURE(NS_ERROR_MODULE_XPCOM,  1)
 #define NS_ERROR_OBJECT_IS_IMMUTABLE            NS_ERROR_GENERATE_FAILURE(NS_ERROR_MODULE_XPCOM,  2)
 #define NS_ERROR_LOSS_OF_SIGNIFICANT_DATA       NS_ERROR_GENERATE_FAILURE(NS_ERROR_MODULE_XPCOM,  3)
 
 #define NS_SUCCESS_LOSS_OF_INSIGNIFICANT_DATA   NS_ERROR_GENERATE_SUCCESS(NS_ERROR_MODULE_XPCOM,  1)
 
+/* Result codes used by nsIThreadManager */
+
+#define NS_ERROR_NOT_SAME_THREAD                NS_ERROR_GENERATE_FAILURE(NS_ERROR_MODULE_XPCOM,  4)
+
 /**
  * Various operations are not permitted during XPCOM shutdown and will fail
  * with this exception.
  */
 #define NS_ERROR_ILLEGAL_DURING_SHUTDOWN        NS_ERROR_GENERATE_FAILURE(NS_ERROR_MODULE_XPCOM, 30)
 
  /*
   * This will return the nsresult corresponding to the most recent NSPR failure