Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | #include "TestInterruptErrorCleanup.h" |
michael@0 | 2 | |
michael@0 | 3 | #include "mozilla/CondVar.h" |
michael@0 | 4 | #include "mozilla/Mutex.h" |
michael@0 | 5 | |
michael@0 | 6 | #include "IPDLUnitTests.h" // fail etc. |
michael@0 | 7 | #include "IPDLUnitTestSubprocess.h" |
michael@0 | 8 | |
michael@0 | 9 | using mozilla::CondVar; |
michael@0 | 10 | using mozilla::Mutex; |
michael@0 | 11 | using mozilla::MutexAutoLock; |
michael@0 | 12 | |
michael@0 | 13 | namespace mozilla { |
michael@0 | 14 | namespace _ipdltest { |
michael@0 | 15 | |
michael@0 | 16 | //----------------------------------------------------------------------------- |
michael@0 | 17 | // parent |
michael@0 | 18 | |
michael@0 | 19 | namespace { |
michael@0 | 20 | |
michael@0 | 21 | // NB: this test does its own shutdown, rather than going through |
michael@0 | 22 | // QuitParent(), because it's testing degenerate edge cases |
michael@0 | 23 | |
michael@0 | 24 | void DeleteSubprocess(Mutex* mutex, CondVar* cvar) |
michael@0 | 25 | { |
michael@0 | 26 | MutexAutoLock lock(*mutex); |
michael@0 | 27 | |
michael@0 | 28 | delete gSubprocess; |
michael@0 | 29 | gSubprocess = nullptr; |
michael@0 | 30 | |
michael@0 | 31 | cvar->Notify(); |
michael@0 | 32 | } |
michael@0 | 33 | |
michael@0 | 34 | void DeleteTheWorld() |
michael@0 | 35 | { |
michael@0 | 36 | delete static_cast<TestInterruptErrorCleanupParent*>(gParentActor); |
michael@0 | 37 | gParentActor = nullptr; |
michael@0 | 38 | |
michael@0 | 39 | // needs to be synchronous to avoid affecting event ordering on |
michael@0 | 40 | // the main thread |
michael@0 | 41 | Mutex mutex("TestInterruptErrorCleanup.DeleteTheWorld.mutex"); |
michael@0 | 42 | CondVar cvar(mutex, "TestInterruptErrorCleanup.DeleteTheWorld.cvar"); |
michael@0 | 43 | |
michael@0 | 44 | MutexAutoLock lock(mutex); |
michael@0 | 45 | |
michael@0 | 46 | XRE_GetIOMessageLoop()->PostTask( |
michael@0 | 47 | FROM_HERE, |
michael@0 | 48 | NewRunnableFunction(DeleteSubprocess, &mutex, &cvar)); |
michael@0 | 49 | |
michael@0 | 50 | cvar.Wait(); |
michael@0 | 51 | } |
michael@0 | 52 | |
michael@0 | 53 | void Done() |
michael@0 | 54 | { |
michael@0 | 55 | static NS_DEFINE_CID(kAppShellCID, NS_APPSHELL_CID); |
michael@0 | 56 | nsCOMPtr<nsIAppShell> appShell (do_GetService(kAppShellCID)); |
michael@0 | 57 | appShell->Exit(); |
michael@0 | 58 | |
michael@0 | 59 | passed(__FILE__); |
michael@0 | 60 | } |
michael@0 | 61 | |
michael@0 | 62 | } // namespace <anon> |
michael@0 | 63 | |
michael@0 | 64 | TestInterruptErrorCleanupParent::TestInterruptErrorCleanupParent() |
michael@0 | 65 | : mGotProcessingError(false) |
michael@0 | 66 | { |
michael@0 | 67 | MOZ_COUNT_CTOR(TestInterruptErrorCleanupParent); |
michael@0 | 68 | } |
michael@0 | 69 | |
michael@0 | 70 | TestInterruptErrorCleanupParent::~TestInterruptErrorCleanupParent() |
michael@0 | 71 | { |
michael@0 | 72 | MOZ_COUNT_DTOR(TestInterruptErrorCleanupParent); |
michael@0 | 73 | } |
michael@0 | 74 | |
michael@0 | 75 | void |
michael@0 | 76 | TestInterruptErrorCleanupParent::Main() |
michael@0 | 77 | { |
michael@0 | 78 | // This test models the following sequence of events |
michael@0 | 79 | // |
michael@0 | 80 | // (1) Parent: Interrupt out-call |
michael@0 | 81 | // (2) Child: crash |
michael@0 | 82 | // --[Parent-only hereafter]-- |
michael@0 | 83 | // (3) Interrupt out-call return false |
michael@0 | 84 | // (4) Close() |
michael@0 | 85 | // --[event loop]-- |
michael@0 | 86 | // (5) delete parentActor |
michael@0 | 87 | // (6) delete childProcess |
michael@0 | 88 | // --[event loop]-- |
michael@0 | 89 | // (7) Channel::OnError notification |
michael@0 | 90 | // --[event loop]-- |
michael@0 | 91 | // (8) Done, quit |
michael@0 | 92 | // |
michael@0 | 93 | // See bug 535298 and friends; this seqeunce of events captures |
michael@0 | 94 | // three differnent potential errors |
michael@0 | 95 | // - Close()-after-error (semantic error previously) |
michael@0 | 96 | // - use-after-free of parentActor |
michael@0 | 97 | // - use-after-free of channel |
michael@0 | 98 | // |
michael@0 | 99 | // Because of legacy constraints related to nsNPAPI* code, we need |
michael@0 | 100 | // to ensure that this sequence of events can occur without |
michael@0 | 101 | // errors/crashes. |
michael@0 | 102 | |
michael@0 | 103 | MessageLoop::current()->PostTask( |
michael@0 | 104 | FROM_HERE, NewRunnableFunction(DeleteTheWorld)); |
michael@0 | 105 | |
michael@0 | 106 | // it's a failure if this *succeeds* |
michael@0 | 107 | if (CallError()) |
michael@0 | 108 | fail("expected an error!"); |
michael@0 | 109 | |
michael@0 | 110 | if (!mGotProcessingError) |
michael@0 | 111 | fail("expected a ProcessingError() notification"); |
michael@0 | 112 | |
michael@0 | 113 | // it's OK to Close() a channel after an error, because nsNPAPI* |
michael@0 | 114 | // wants to do this |
michael@0 | 115 | Close(); |
michael@0 | 116 | |
michael@0 | 117 | // we know that this event *must* be after the MaybeError |
michael@0 | 118 | // notification enqueued by AsyncChannel, because that event is |
michael@0 | 119 | // enqueued within the same mutex that ends up signaling the |
michael@0 | 120 | // wakeup-on-error of |CallError()| above |
michael@0 | 121 | MessageLoop::current()->PostTask(FROM_HERE, NewRunnableFunction(Done)); |
michael@0 | 122 | } |
michael@0 | 123 | |
michael@0 | 124 | void |
michael@0 | 125 | TestInterruptErrorCleanupParent::ProcessingError(Result what) |
michael@0 | 126 | { |
michael@0 | 127 | if (what != MsgDropped) |
michael@0 | 128 | fail("unexpected processing error"); |
michael@0 | 129 | mGotProcessingError = true; |
michael@0 | 130 | } |
michael@0 | 131 | |
michael@0 | 132 | //----------------------------------------------------------------------------- |
michael@0 | 133 | // child |
michael@0 | 134 | |
michael@0 | 135 | TestInterruptErrorCleanupChild::TestInterruptErrorCleanupChild() |
michael@0 | 136 | { |
michael@0 | 137 | MOZ_COUNT_CTOR(TestInterruptErrorCleanupChild); |
michael@0 | 138 | } |
michael@0 | 139 | |
michael@0 | 140 | TestInterruptErrorCleanupChild::~TestInterruptErrorCleanupChild() |
michael@0 | 141 | { |
michael@0 | 142 | MOZ_COUNT_DTOR(TestInterruptErrorCleanupChild); |
michael@0 | 143 | } |
michael@0 | 144 | |
michael@0 | 145 | bool |
michael@0 | 146 | TestInterruptErrorCleanupChild::AnswerError() |
michael@0 | 147 | { |
michael@0 | 148 | _exit(0); |
michael@0 | 149 | NS_RUNTIMEABORT("unreached"); |
michael@0 | 150 | return false; |
michael@0 | 151 | } |
michael@0 | 152 | |
michael@0 | 153 | |
michael@0 | 154 | } // namespace _ipdltest |
michael@0 | 155 | } // namespace mozilla |