fixes bug 337492 "xpcom proxies may release proxied object on random threads" r=bsmedberg

git-svn-id: svn://10.0.0.236/trunk@198691 18797224-902f-48f8-a5cc-f745e15eee43
This commit is contained in:
darin%meer.net 2006-05-30 23:07:13 +00:00
parent f1f5e46226
commit c6462b5272
5 changed files with 95 additions and 15 deletions

View File

@ -52,6 +52,7 @@
#include "nsProxyEvent.h"
#include "nsProxyEventPrivate.h"
#include "nsProxyRelease.h"
#include "nsIProxyObjectManager.h"
#include "nsCRT.h"
@ -409,20 +410,22 @@ nsProxyObjectCallInfo::SetCallersTarget(nsIEventTarget* target)
}
nsProxyObject::nsProxyObject(nsIEventTarget *target, PRInt32 proxyType,
nsISupports *realObject)
nsISupports *realObject,
nsISupports *rootObject)
{
mRealObject = realObject;
mRootObject = rootObject;
mTarget = target;
mProxyType = proxyType;
}
nsProxyObject::~nsProxyObject()
{
// I am worried about order of destruction here.
// do not remove assignments.
mRealObject = 0;
mTarget = 0;
// Proxy the release of mRealObject to protect against it being deleted on
// the wrong thread.
nsISupports *doomed = nsnull;
mRealObject.swap(doomed);
NS_ProxyRelease(mTarget, doomed);
}
void

View File

@ -84,7 +84,7 @@ class nsProxyObject
{
public:
nsProxyObject(nsIEventTarget *destQueue, PRInt32 proxyType,
nsISupports *realObject);
nsISupports *realObject, nsISupports *rootObject);
void AddRef();
void Release();
@ -112,6 +112,7 @@ private:
nsCOMPtr<nsISupports> mRealObject; /* the non-proxy object that this event is referring to.
This is a strong ref. */
nsISupports *mRootObject; /* weak pointer to identity object */
nsresult convertMiniVariantToVariant(nsXPTMethodInfo * methodInfo,
nsXPTCMiniVariant * params,

View File

@ -296,6 +296,7 @@ nsProxyEventObject::GetNewOrUsedProxy(nsIEventTarget *target,
peo = new nsProxyEventObject(target,
proxyType,
rootObject,
rootObject,
rootClazz,
nsnull);
if(!peo) {
@ -345,10 +346,11 @@ nsProxyEventObject::GetNewOrUsedProxy(nsIEventTarget *target,
return nsnull;
}
peo = new nsProxyEventObject(target,
proxyType,
rawInterface,
proxyClazz,
peo = new nsProxyEventObject(target,
proxyType,
rawInterface,
rootObject,
proxyClazz,
rootProxy);
if (!peo) {
// Ouch... Out of memory!
@ -397,6 +399,7 @@ nsProxyEventObject::nsProxyEventObject()
nsProxyEventObject::nsProxyEventObject(nsIEventTarget *target,
PRInt32 proxyType,
nsISupports* aObj,
nsISupports* aRootObj,
nsProxyEventClass* aClass,
nsProxyEventObject* root)
: mClass(aClass),
@ -405,7 +408,8 @@ nsProxyEventObject::nsProxyEventObject(nsIEventTarget *target,
{
NS_IF_ADDREF(mRoot);
mProxyObject = new nsProxyObject(target, proxyType, aObj);
// XXX protect against OOM errors
mProxyObject = new nsProxyObject(target, proxyType, aObj, aRootObj);
#ifdef DEBUG_xpcom_proxy
DebugDump("Create", 0);
@ -446,9 +450,10 @@ nsProxyEventObject::~nsProxyEventObject()
NS_ASSERTION(!mNext, "There are still proxies in the chain!");
if (realToProxyMap != nsnull) {
nsCOMPtr<nsISupports> rootObject = do_QueryInterface(mProxyObject->mRealObject);
nsCOMPtr<nsISupports> rootQueue = do_QueryInterface(mProxyObject->mTarget);
nsProxyEventKey key(rootObject, rootQueue, mProxyObject->mProxyType);
nsCOMPtr<nsISupports> rootTarget =
do_QueryInterface(mProxyObject->mTarget);
nsProxyEventKey key(mProxyObject->mRootObject, rootTarget,
mProxyObject->mProxyType);
#ifdef DEBUG_dougt
void* value =
#endif

View File

@ -133,6 +133,7 @@ public:
nsProxyEventObject(nsIEventTarget *target,
PRInt32 proxyType,
nsISupports* aObj,
nsISupports* aRootObj, // result of QI(nsISupports)
nsProxyEventClass* aClass,
nsProxyEventObject* root);

View File

@ -55,6 +55,8 @@
#include "nsIRunnable.h"
#include "nsIProxyObjectManager.h"
#include "nsIThreadPool.h"
#include "nsXPCOMCIDInternal.h"
#include "nsComponentManagerUtils.h"
#include "nsServiceManagerUtils.h"
#include "nsThreadUtils.h"
@ -419,6 +421,72 @@ public:
};
NS_IMPL_THREADSAFE_ISUPPORTS1(TestSyncProxyToSelf, nsIRunnable)
//---------------------------------------------------------------------------
// Test to make sure we can call methods on a "main thread only" object from
// a background thread.
class MainThreadOnly : public nsIRunnable {
public:
NS_DECL_ISUPPORTS
NS_IMETHOD Run() {
NS_ASSERTION(NS_IsMainThread(), "method called on wrong thread");
*mNumRuns -= 1;
return NS_OK;
}
MainThreadOnly(PRUint32 *numRuns) : mNumRuns(numRuns) {}
~MainThreadOnly() {
NS_ASSERTION(NS_IsMainThread(), "method called on wrong thread");
}
PRBool IsDone() { return mNumRuns == 0; }
private:
PRUint32 *mNumRuns;
};
NS_IMPL_ISUPPORTS1(MainThreadOnly, nsIRunnable) // not threadsafe!
static nsresult
RunApartmentTest()
{
LOG(("RunApartmentTest: start\n"));
const PRUint32 numDispatched = 160;
PRUint32 numCompleted = 0;
nsCOMPtr<nsIRunnable> obj = new MainThreadOnly(&numCompleted);
nsCOMPtr<nsIProxyObjectManager> manager =
do_GetService(NS_XPCOMPROXY_CONTRACTID);
nsCOMPtr<nsIRunnable> objProxy;
manager->GetProxyForObject(NS_PROXY_TO_CURRENT_THREAD,
NS_GET_IID(nsIRunnable),
obj,
NS_PROXY_ASYNC,
getter_AddRefs(objProxy));
nsCOMPtr<nsIThread> thread;
NS_NewThread(getter_AddRefs(thread));
obj = nsnull;
nsCOMPtr<nsIThreadPool> pool = do_CreateInstance(NS_THREADPOOL_CONTRACTID);
pool->SetThreadLimit(8);
for (PRUint32 i = 0; i < numDispatched; ++i)
pool->Dispatch(objProxy, NS_DISPATCH_NORMAL);
objProxy = nsnull;
nsCOMPtr<nsIThread> curThread = do_GetCurrentThread();
while (numCompleted < numDispatched) {
NS_ProcessNextEvent(curThread);
}
pool->Shutdown();
LOG(("RunApartmentTest: end\n"));
return NS_OK;
}
int
main(int argc, char **argv)
{
@ -435,6 +503,8 @@ main(int argc, char **argv)
NS_GetComponentRegistrar(getter_AddRefs(registrar));
registrar->AutoRegister(nsnull);
RunApartmentTest();
nsCOMPtr<nsIThread> eventLoopThread;
NS_NewThread(getter_AddRefs(eventLoopThread));