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

Unified Diff: webrtc/rtc_tools/sanitizers_unittest.cc

Issue 3005273002: Add a check that sanitizers cause a fatal failure (Closed)
Patch Set: Rework Created 3 years, 3 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
« webrtc/rtc_tools/BUILD.gn ('K') | « webrtc/rtc_tools/BUILD.gn ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: webrtc/rtc_tools/sanitizers_unittest.cc
diff --git a/webrtc/rtc_tools/sanitizers_unittest.cc b/webrtc/rtc_tools/sanitizers_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..1e8bc6e1305e89b022a09ed6836d0fe9aa9c41c2
--- /dev/null
+++ b/webrtc/rtc_tools/sanitizers_unittest.cc
@@ -0,0 +1,119 @@
+/*
+ * Copyright (c) 2017 The WebRTC project authors. All Rights Reserved.
+ *
+ * Use of this source code is governed by a BSD-style license
+ * that can be found in the LICENSE file in the root of the source
+ * tree. An additional intellectual property rights grant can be found
+ * in the file PATENTS. All contributing project authors may
+ * be found in the AUTHORS file in the root of the source tree.
+ */
+
+#include <stddef.h>
+
+#include "webrtc/rtc_base/checks.h"
+#include "webrtc/rtc_base/nullsocketserver.h"
+#include "webrtc/rtc_base/thread.h"
+#include "webrtc/test/gtest.h"
+
+namespace rtc {
+
+namespace {
+
+#if defined(MEMORY_SANITIZER)
kwiberg-webrtc 2017/09/13 11:59:23 There are preprocessor symbols here [https://chrom
oprypin_webrtc 2017/09/13 14:48:15 I didn't know about these. I got my example from h
kwiberg-webrtc 2017/09/14 02:15:02 OK, FOO_SANITIZER seems to be set by Chromium's GN
+void UseOfUninitializedValue() {
+ int* buf = new int[2];
+ if (buf[1]) {
+ ++buf[0];
+ }
+ delete[] buf;
+}
+
+TEST(SanitizersDeathTest, MemorySanitizer) {
+ UseOfUninitializedValue();
kwiberg-webrtc 2017/09/13 11:59:23 Hmm. Why don't we expect to die here?
oprypin_webrtc 2017/09/13 14:48:15 Hmm indeed. It was incorrect. Had to rework this.
+}
+#endif
+
+#if defined(ADDRESS_SANITIZER)
+void HeapUseAfterFree() {
+ char *buf = new char[2];
+ delete[] buf;
+ buf[0] = buf[1];
+}
+
+TEST(SanitizersDeathTest, AddressSanitizer) {
+ EXPECT_DEATH(HeapUseAfterFree(), "heap-use-after-free");
+}
+#endif
+
+#if defined(UNDEFINED_SANITIZER)
+// For ubsan:
+void SignedIntegerOverflow() {
+ int32_t x = 1234567890;
+ x *= 2;
+}
+
+// For ubsan_vptr:
+struct Base {
+ virtual void f() = 0;
+ virtual ~Base() {}
+};
+struct Derived : public Base {
+ virtual void f() {}
+};
kwiberg-webrtc 2017/09/14 02:15:02 IIUC, you don't need the subclass or a virtual des
oprypin_webrtc 2017/09/14 06:48:33 OK, I used the alternative implementation. It's ni
kwiberg-webrtc 2017/09/14 08:23:22 Acknowledged.
+
+void InvalidVptr() {
+ Base* ptr = new Derived;
+ delete ptr;
+ ptr->f();
+}
+
+TEST(SanitizersDeathTest, UndefinedSanitizer) {
+ EXPECT_DEATH({ SignedIntegerOverflow(); InvalidVptr(); },
kwiberg-webrtc 2017/09/13 11:59:23 Why are you doing two lethal things here?
oprypin_webrtc 2017/09/13 14:48:15 ubsan fails on only one of them, ubsan_vptr only f
kwiberg-webrtc 2017/09/14 02:15:02 Hmm, yeah, I see. And it looks like it'll be messy
+ "runtime error");
+}
+#endif
+
+#if defined(THREAD_SANITIZER)
+class IncrementThread : public Thread {
+ public:
+ explicit IncrementThread(int* value)
+ : Thread(std::unique_ptr<SocketServer>(new NullSocketServer())),
kwiberg-webrtc 2017/09/13 11:59:23 rtc::MakeUnique<NullSocketServer>()
oprypin_webrtc 2017/09/13 14:48:15 Done.
+ value_(value) {}
+
+ void Run() override {
+ ++*value_;
+ Thread::Current()->SleepMs(100);
+ }
+
+ // Un-protect Thread::Join for the test.
+ void Join() {
+ Thread::Join();
+ }
+
+ private:
+ int* value_;
+
+ RTC_DISALLOW_COPY_AND_ASSIGN(IncrementThread);
+};
+
+void DataRace() {
+ int value = 0;
+ IncrementThread thread1(&value);
+ IncrementThread thread2(&value);
+ thread1.Start();
+ thread2.Start();
+ thread1.Join();
+ thread2.Join();
+ // TSan seems to mess with gtest's death detection.
+ // Fail intentionally, and rely on detecting the error message.
+ RTC_CHECK(false);
+}
+
+TEST(SanitizersDeathTest, ThreadSanitizer) {
+ EXPECT_DEATH(DataRace(), "data race");
+}
+#endif
+
+} // namespace
+
+} // namespace rtc
« webrtc/rtc_tools/BUILD.gn ('K') | « webrtc/rtc_tools/BUILD.gn ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698