Bug 1466475 - Make mozilla::Span produce aligned bogus pointers per new Rust rules. r?froydnj draft
authorHenri Sivonen <hsivonen@hsivonen.fi>
Mon, 04 Jun 2018 12:59:46 +0300
changeset 803507 457bf45abecdef51af73c39571ffe6ebcec5e377
parent 803506 d87bf0bd52e2398d47501e88abdf7b290b4f424c
push id112122
push userbmo:hsivonen@hsivonen.fi
push dateMon, 04 Jun 2018 10:01:17 +0000
reviewersfroydnj
bugs1466475
milestone62.0a1
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());
   }