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