Bug 1466475 - Make mozilla::Span produce aligned bogus pointers per new Rust rules. r=froydnj
authorHenri Sivonen <hsivonen@hsivonen.fi>
Mon, 04 Jun 2018 12:59:46 +0300
changeset 421345 7910d46af606344992f232d710794e5a7c686daa
parent 421344 752465b44c793318cef36df46ca5ff00c3d8854a
child 421346 b24796357d7795310b17e7502b02728b2dd34630
push id34092
push useraiakab@mozilla.com
push dateTue, 05 Jun 2018 21:57:17 +0000
treeherdermozilla-central@0bb149ce1d1a [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersfroydnj
bugs1466475
milestone62.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1466475 - Make mozilla::Span produce aligned bogus pointers per new Rust rules. r=froydnj MozReview-Commit-ID: JFVSRu53Geh
mfbt/Span.h
mfbt/tests/gtest/TestSpan.cpp
--- a/mfbt/Span.h
+++ b/mfbt/Span.h
@@ -834,19 +834,20 @@ private:
   template<class ExtentType>
   class storage_type : public ExtentType
   {
   public:
     template<class OtherExtentType>
     constexpr storage_type(pointer elements,
                                               OtherExtentType ext)
       : ExtentType(ext)
-      // Replace nullptr with 0x1 for Rust slice compatibility. See
+      // Replace nullptr with aligned bogus pointer for Rust slice
+      // compatibility. See
       // https://doc.rust-lang.org/std/slice/fn.from_raw_parts.html
-      , data_(elements ? elements : reinterpret_cast<pointer>(0x1))
+      , data_(elements ? elements : reinterpret_cast<pointer>(alignof(element_type)))
     {
       const size_t extentSize = ExtentType::size();
       MOZ_RELEASE_ASSERT(
         (!elements && extentSize == 0) ||
         (elements && extentSize != mozilla::MaxValue<size_t>::value));
     }
 
     constexpr pointer data() const { return data_; }
--- a/mfbt/tests/gtest/TestSpan.cpp
+++ b/mfbt/tests/gtest/TestSpan.cpp
@@ -58,32 +58,32 @@ static_assert(!IsConvertible<const nsTAr
 static_assert(IsConvertible<nsTArray<const int>, Span<const int>>::value,
               "nsTArray should convert into const");
 static_assert(!IsConvertible<nsTArray<const int>, Span<int>>::value,
               "nsTArray should not drop const in conversion");
 
 /**
  * Rust slice-compatible nullptr replacement value.
  */
-#define SLICE_CONST_INT_PTR reinterpret_cast<const int*>(0x1)
+#define SLICE_CONST_INT_PTR reinterpret_cast<const int*>(alignof(const int))
 
 /**
  * Rust slice-compatible nullptr replacement value.
  */
-#define SLICE_INT_PTR reinterpret_cast<int*>(0x1)
+#define SLICE_INT_PTR reinterpret_cast<int*>(alignof(int))
 
 /**
  * Rust slice-compatible nullptr replacement value.
  */
-#define SLICE_CONST_INT_PTR_PTR reinterpret_cast<const int**>(0x1)
+#define SLICE_CONST_INT_PTR_PTR reinterpret_cast<const int**>(alignof(const int*))
 
 /**
  * Rust slice-compatible nullptr replacement value.
  */
-#define SLICE_INT_PTR_PTR reinterpret_cast<int**>(0x1)
+#define SLICE_INT_PTR_PTR reinterpret_cast<int**>(alignof(int *))
 
 namespace {
 struct BaseClass
 {
 };
 struct DerivedClass : BaseClass
 {
 };
@@ -1992,17 +1992,17 @@ SPAN_TEST(as_bytes)
   {
     Span<int> s;
     auto bs = AsBytes(s);
     ASSERT_EQ(bs.Length(), s.Length());
     ASSERT_EQ(bs.Length(), 0U);
     ASSERT_EQ(bs.size_bytes(), 0U);
     ASSERT_EQ(static_cast<const void*>(bs.data()),
               static_cast<const void*>(s.data()));
-    ASSERT_EQ(bs.data(), reinterpret_cast<const uint8_t*>(0x1));
+    ASSERT_EQ(bs.data(), reinterpret_cast<const uint8_t*>(SLICE_INT_PTR));
   }
 
   {
     Span<int> s = a;
     auto bs = AsBytes(s);
     ASSERT_EQ(static_cast<const void*>(bs.data()),
               static_cast<const void*>(s.data()));
     ASSERT_EQ(bs.Length(), s.LengthBytes());
@@ -2026,17 +2026,17 @@ SPAN_TEST(as_writable_bytes)
 
   {
     Span<int> s;
     auto bs = AsWritableBytes(s);
     ASSERT_EQ(bs.Length(), s.Length());
     ASSERT_EQ(bs.Length(), 0U);
     ASSERT_EQ(bs.size_bytes(), 0U);
     ASSERT_EQ(static_cast<void*>(bs.data()), static_cast<void*>(s.data()));
-    ASSERT_EQ(bs.data(), reinterpret_cast<uint8_t*>(0x1));
+    ASSERT_EQ(bs.data(), reinterpret_cast<uint8_t*>(SLICE_INT_PTR));
   }
 
   {
     Span<int> s = a;
     auto bs = AsWritableBytes(s);
     ASSERT_EQ(static_cast<void*>(bs.data()), static_cast<void*>(s.data()));
     ASSERT_EQ(bs.Length(), s.LengthBytes());
   }