summaryrefslogtreecommitdiffstats
path: root/ipc/ipdl/test/cxx/TestInterruptErrorCleanup.cpp
blob: 3af7ad620d92522b756fd25d07d8a124f7b2a652 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include "TestInterruptErrorCleanup.h"

#include "base/task.h"
#include "mozilla/CondVar.h"
#include "mozilla/Mutex.h"

#include "IPDLUnitTests.h"  // fail etc.
#include "IPDLUnitTestSubprocess.h"

using mozilla::CondVar;
using mozilla::Mutex;
using mozilla::MutexAutoLock;

namespace mozilla {
namespace _ipdltest {

//-----------------------------------------------------------------------------
// parent

namespace {

// NB: this test does its own shutdown, rather than going through
// QuitParent(), because it's testing degenerate edge cases

void DeleteSubprocess(Mutex* mutex, CondVar* cvar) {
  MutexAutoLock lock(*mutex);

  gSubprocess->Destroy();
  gSubprocess = nullptr;

  cvar->Notify();
}

void DeleteTheWorld() {
  delete static_cast<TestInterruptErrorCleanupParent*>(gParentActor);
  gParentActor = nullptr;

  // needs to be synchronous to avoid affecting event ordering on
  // the main thread
  Mutex mutex MOZ_UNANNOTATED("TestInterruptErrorCleanup.DeleteTheWorld.mutex");
  CondVar cvar(mutex, "TestInterruptErrorCleanup.DeleteTheWorld.cvar");

  MutexAutoLock lock(mutex);

  XRE_GetIOMessageLoop()->PostTask(
      NewRunnableFunction("DeleteSubprocess", DeleteSubprocess, &mutex, &cvar));

  cvar.Wait();
}

void Done() {
  static NS_DEFINE_CID(kAppShellCID, NS_APPSHELL_CID);
  nsCOMPtr<nsIAppShell> appShell(do_GetService(kAppShellCID));
  appShell->Exit();

  passed(__FILE__);
}

}  // namespace

TestInterruptErrorCleanupParent::TestInterruptErrorCleanupParent()
    : mGotProcessingError(false) {
  MOZ_COUNT_CTOR(TestInterruptErrorCleanupParent);
}

TestInterruptErrorCleanupParent::~TestInterruptErrorCleanupParent() {
  MOZ_COUNT_DTOR(TestInterruptErrorCleanupParent);
}

void TestInterruptErrorCleanupParent::Main() {
  // This test models the following sequence of events
  //
  // (1) Parent: Interrupt out-call
  // (2) Child: crash
  // --[Parent-only hereafter]--
  // (3) Interrupt out-call return false
  // (4) Close()
  // --[event loop]--
  // (5) delete parentActor
  // (6) delete childProcess
  // --[event loop]--
  // (7) Channel::OnError notification
  // --[event loop]--
  // (8) Done, quit
  //
  // See bug 535298 and friends; this seqeunce of events captures
  // three differnent potential errors
  //  - Close()-after-error (semantic error previously)
  //  - use-after-free of parentActor
  //  - use-after-free of channel
  //
  // Because of legacy constraints related to nsNPAPI* code, we need
  // to ensure that this sequence of events can occur without
  // errors/crashes.

  MessageLoop::current()->PostTask(
      NewRunnableFunction("DeleteTheWorld", DeleteTheWorld));

  // it's a failure if this *succeeds*
  if (CallError()) fail("expected an error!");

  if (!mGotProcessingError) fail("expected a ProcessingError() notification");

  // it's OK to Close() a channel after an error, because nsNPAPI*
  // wants to do this
  Close();

  // we know that this event *must* be after the MaybeError
  // notification enqueued by AsyncChannel, because that event is
  // enqueued within the same mutex that ends up signaling the
  // wakeup-on-error of |CallError()| above
  MessageLoop::current()->PostTask(NewRunnableFunction("Done", Done));
}

void TestInterruptErrorCleanupParent::ProcessingError(Result aCode,
                                                      const char* aReason) {
  if (aCode != MsgDropped) fail("unexpected processing error");
  mGotProcessingError = true;
}

//-----------------------------------------------------------------------------
// child

TestInterruptErrorCleanupChild::TestInterruptErrorCleanupChild() {
  MOZ_COUNT_CTOR(TestInterruptErrorCleanupChild);
}

TestInterruptErrorCleanupChild::~TestInterruptErrorCleanupChild() {
  MOZ_COUNT_DTOR(TestInterruptErrorCleanupChild);
}

mozilla::ipc::IPCResult TestInterruptErrorCleanupChild::AnswerError() {
  _exit(0);
  MOZ_CRASH("unreached");
}

}  // namespace _ipdltest
}  // namespace mozilla