Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(143)

Unified Diff: third_party/WebKit/Source/platform/heap/HeapTest.cpp

Issue 2785123004: Oilpan: Add death tests for same thread checks (Closed)
Patch Set: fix Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: third_party/WebKit/Source/platform/heap/HeapTest.cpp
diff --git a/third_party/WebKit/Source/platform/heap/HeapTest.cpp b/third_party/WebKit/Source/platform/heap/HeapTest.cpp
index 7affa63f7f28076e9a6cf007f64aab1f254a43bc..32dee5fdf72377572c8fadbb1f39cd277538d535 100644
--- a/third_party/WebKit/Source/platform/heap/HeapTest.cpp
+++ b/third_party/WebKit/Source/platform/heap/HeapTest.cpp
@@ -5335,6 +5335,136 @@ TEST(HeapTest, ThreadedStrongification) {
ThreadedStrongificationTester::test();
}
+class MemberSameThreadCheckTester {
+ public:
+ void test() {
+ IntWrapper::s_destructorCalls = 0;
+
+ MutexLocker locker(mainThreadMutex());
+ std::unique_ptr<WebThread> workerThread = WTF::wrapUnique(
+ Platform::current()->createThread("Test Worker Thread"));
+ workerThread->getWebTaskRunner()->postTask(
+ BLINK_FROM_HERE,
+ crossThreadBind(&MemberSameThreadCheckTester::workerThreadMain,
+ crossThreadUnretained(this)));
+
+ parkMainThread();
+ }
+
+ private:
+ Member<IntWrapper> m_wrapper;
+
+ void workerThreadMain() {
+ MutexLocker locker(workerThreadMutex());
+
+ ThreadState::attachCurrentThread();
+
+ // Setting an object created on the worker thread to a Member allocated on
+ // the main thread is not allowed.
+ m_wrapper = IntWrapper::create(42);
+
+ wakeMainThread();
+ ThreadState::detachCurrentThread();
+ }
+};
+
+#if DCHECK_IS_ON()
+TEST(HeapTest, MemberSameThreadCheck) {
+ EXPECT_DEATH(MemberSameThreadCheckTester().test(), "");
+}
+#endif
+
+class PersistentSameThreadCheckTester {
+ public:
+ void test() {
+ IntWrapper::s_destructorCalls = 0;
+
+ MutexLocker locker(mainThreadMutex());
+ std::unique_ptr<WebThread> workerThread = WTF::wrapUnique(
+ Platform::current()->createThread("Test Worker Thread"));
+ workerThread->getWebTaskRunner()->postTask(
+ BLINK_FROM_HERE,
+ crossThreadBind(&PersistentSameThreadCheckTester::workerThreadMain,
+ crossThreadUnretained(this)));
+
+ parkMainThread();
+ }
+
+ private:
+ Persistent<IntWrapper> m_wrapper;
+
+ void workerThreadMain() {
+ MutexLocker locker(workerThreadMutex());
+
+ ThreadState::attachCurrentThread();
+
+ // Setting an object created on the worker thread to a Persistent allocated
+ // on the main thread is not allowed.
+ m_wrapper = IntWrapper::create(42);
+
+ wakeMainThread();
+ ThreadState::detachCurrentThread();
+ }
+};
+
+#if DCHECK_IS_ON()
+TEST(HeapTest, PersistentSameThreadCheck) {
+ EXPECT_DEATH(PersistentSameThreadCheckTester().test(), "");
+}
+#endif
+
+class MarkingSameThreadCheckTester {
+ public:
+ void test() {
+ IntWrapper::s_destructorCalls = 0;
+
+ MutexLocker locker(mainThreadMutex());
+ std::unique_ptr<WebThread> workerThread = WTF::wrapUnique(
+ Platform::current()->createThread("Test Worker Thread"));
+ Persistent<MainThreadObject> m_mainThreadObject = new MainThreadObject();
+ workerThread->getWebTaskRunner()->postTask(
+ BLINK_FROM_HERE,
+ crossThreadBind(&MarkingSameThreadCheckTester::workerThreadMain,
+ crossThreadUnretained(this),
+ wrapCrossThreadPersistent(m_mainThreadObject.get())));
+ parkMainThread();
+ // This will try to mark MainThreadObject when it tries to mark IntWrapper
+ // it should crash.
+ preciselyCollectGarbage();
+ }
+
+ private:
+ class MainThreadObject : public GarbageCollectedFinalized<MainThreadObject> {
+ public:
+ DEFINE_INLINE_TRACE() { visitor->trace(m_wrapperSet); }
+ void addToSet(IntWrapper* wrapper) { m_wrapperSet.insert(42, wrapper); }
+
+ private:
+ HeapHashMap<int, Member<IntWrapper>> m_wrapperSet;
+ };
+
+ void workerThreadMain(MainThreadObject* mainThreadObject) {
+ MutexLocker locker(workerThreadMutex());
+
+ ThreadState::attachCurrentThread();
+
+ // Adding a reference to an object created on the worker thread to a
+ // HeapHashMap created on the main thread is not allowed.
+ mainThreadObject->addToSet(IntWrapper::create(42));
+
+ wakeMainThread();
+ ThreadState::detachCurrentThread();
+ }
+};
+
+#if DCHECK_IS_ON()
+TEST(HeapTest, MarkingSameThreadCheck) {
+ // This will crash during marking, at the DCHECK in Visitor::markHeader() or
+ // earlier.
+ EXPECT_DEATH(MarkingSameThreadCheckTester().test(), "");
+}
+#endif
+
static bool allocateAndReturnBool() {
conservativelyCollectGarbage();
return true;
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698