bug 1300152 - Add nsIDebug2::rustPanic to allow triggering Rust panic for testing. r=froydnj
authorTed Mielczarek <ted@mielczarek.org>
Fri, 02 Sep 2016 13:22:21 -0400
changeset 313086 8c9117de1e7f40af42b7cbce25bc3780c032fe45
parent 313085 c2115d200aebfcf50507302c26e6ffc3c9ba12ec
child 313087 319788a816f33f7275ba129977cb0378b26ed6a5
push id20479
push userkwierso@gmail.com
push dateThu, 08 Sep 2016 01:08:46 +0000
treeherderfx-team@fb7c6b034329 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersfroydnj
bugs1300152, 2
milestone51.0a1
bug 1300152 - Add nsIDebug2::rustPanic to allow triggering Rust panic for testing. r=froydnj For testing purposes it will be useful to be able to trigger crashes in Rust code. Being able to trigger a panic seems like a good place to start. This will make it easier to validate improvements in crash reporting. MozReview-Commit-ID: Bh5rBieLLWW
toolkit/crashreporter/test/unit/test_crash_rust_panic.js
toolkit/crashreporter/test/unit/xpcshell.ini
toolkit/library/rust/lib.rs
xpcom/base/moz.build
xpcom/base/nsDebugImpl.cpp
xpcom/base/nsIDebug2.idl
copy from toolkit/crashreporter/test/unit/test_crash_moz_crash.js
copy to toolkit/crashreporter/test/unit/test_crash_rust_panic.js
--- a/toolkit/crashreporter/test/unit/test_crash_moz_crash.js
+++ b/toolkit/crashreporter/test/unit/test_crash_rust_panic.js
@@ -1,16 +1,12 @@
 function run_test()
 {
-  // Try crashing with a runtime abort
+  // Try crashing with a Rust panic
   do_crash(function() {
-             crashType = CrashTestUtils.CRASH_MOZ_CRASH;
-             crashReporter.annotateCrashReport("TestKey", "TestValue");
+             Components.classes["@mozilla.org/xpcom/debug;1"].getService(Components.interfaces.nsIDebug2).rustPanic("OH NO");
            },
            function(mdump, extra) {
-             do_check_eq(extra.TestKey, "TestValue");
-             do_check_false("OOMAllocationSize" in extra);
-             do_check_false("JSOutOfMemory" in extra);
-             do_check_false("JSLargeAllocationFailure" in extra);
+             //TODO: check some extra things?
            },
           // process will exit with a zero exit status
           true);
 }
--- a/toolkit/crashreporter/test/unit/xpcshell.ini
+++ b/toolkit/crashreporter/test/unit/xpcshell.ini
@@ -4,16 +4,18 @@ tail =
 skip-if = toolkit == 'android' || toolkit == 'gonk'
 support-files =
   crasher_subprocess_head.js
   crasher_subprocess_tail.js
 
 [test_crash_moz_crash.js]
 [test_crash_purevirtual.js]
 [test_crash_runtimeabort.js]
+[test_crash_rust_panic.js]
+skip-if = !rust
 [test_crash_after_js_oom_reported.js]
 [test_crash_after_js_oom_recovered.js]
 [test_crash_after_js_oom_reported_2.js]
 [test_crash_after_js_large_allocation_failure.js]
 [test_crash_after_js_large_allocation_failure_reporting.js]
 [test_crash_oom.js]
 [test_oom_annotation_windows.js]
 skip-if = os != 'win'
--- a/toolkit/library/rust/lib.rs
+++ b/toolkit/library/rust/lib.rs
@@ -1,5 +1,14 @@
 // This Source Code Form is subject to the terms of the Mozilla Public
 // License, v. 2.0. If a copy of the MPL was not distributed with this
 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
 
 extern crate mp4parse_capi;
+
+use std::ffi::CStr;
+use std::os::raw::c_char;
+
+/// Used to implement `nsIDebug2::RustPanic` for testing purposes.
+#[no_mangle]
+pub extern fn intentional_panic(message: *const c_char) {
+    panic!("{}", unsafe { CStr::from_ptr(message) }.to_string_lossy());
+}
--- a/xpcom/base/moz.build
+++ b/xpcom/base/moz.build
@@ -152,8 +152,11 @@ FINAL_LIBRARY = 'xul'
 
 LOCAL_INCLUDES += [
     '../build',
     '/xpcom/ds',
 ]
 
 if 'gtk' in CONFIG['MOZ_WIDGET_TOOLKIT']:
     CXXFLAGS += CONFIG['TK_CFLAGS']
+
+if CONFIG['MOZ_RUST']:
+    DEFINES['MOZ_RUST'] = True
--- a/xpcom/base/nsDebugImpl.cpp
+++ b/xpcom/base/nsDebugImpl.cpp
@@ -142,16 +142,32 @@ nsDebugImpl::Break(const char* aFile, in
 
 NS_IMETHODIMP
 nsDebugImpl::Abort(const char* aFile, int32_t aLine)
 {
   NS_DebugBreak(NS_DEBUG_ABORT, nullptr, nullptr, aFile, aLine);
   return NS_OK;
 }
 
+#ifdef MOZ_RUST
+// From toolkit/library/rust/lib.rs
+extern "C" void intentional_panic(const char* message);
+#endif
+
+NS_IMETHODIMP
+nsDebugImpl::RustPanic(const char* aMessage)
+{
+#ifdef MOZ_RUST
+  intentional_panic(aMessage);
+  return NS_OK;
+#else
+  return NS_ERROR_NOT_IMPLEMENTED;
+#endif
+}
+
 NS_IMETHODIMP
 nsDebugImpl::GetIsDebugBuild(bool* aResult)
 {
 #ifdef DEBUG
   *aResult = true;
 #else
   *aResult = false;
 #endif
--- a/xpcom/base/nsIDebug2.idl
+++ b/xpcom/base/nsIDebug2.idl
@@ -74,9 +74,16 @@ interface nsIDebug2 : nsISupports
     /**
      * Request the process to trigger a fatal abort.
      *
      * @param aFile file containing abort request
      * @param aLine line number of abort request
      */
     void abort(in string aFile, 
                in long aLine);
+
+    /**
+     * Request the process to trigger a fatal panic!() from Rust code.
+     *
+     * @param aMessage the string to pass to panic!().
+     */
+    void rustPanic(in string aMessage);
 };