Submitted By: Ken Moffat <ken at linuxfromscratch dot org>
Date: 2023-05-26
Initial Package Version: 113.0
Upstream Status: Applied for 114.0b7
Origin: Upstream, backported to 113.0 by self because of context differences in toml files.
Description: Fixes segfault with llvm-16.

Original header of hg patch:

# HG changeset patch
# User Mike Hommey <mh+mozilla@glandium.org>
# Date 1684442080 0
# Node ID 424a213f8a13af9e43cde21b7a70370433120869
# Parent  3436c899285c18d00293cf5c4b2703a9a355fce4
Bug 1831242 - Patch bindgen to work around issues with some unsound transmutes when compiling with LLVM 16+. r=supply-chain-reviewers, a=dmeehan

Differential Revision: https://phabricator.services.mozilla.com/D178251

diff -Naur firefox-113.0.orig/Cargo.lock firefox-113.0/Cargo.lock
--- firefox-113.0.orig/Cargo.lock	2023-05-04 23:09:09.000000000 +0100
+++ firefox-113.0/Cargo.lock	2023-05-26 19:15:39.741526488 +0100
@@ -417,8 +417,6 @@
 [[package]]
 name = "bindgen"
 version = "0.64.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "c4243e6031260db77ede97ad86c27e501d646a27ab57b59a574f725d98ab1fb4"
 dependencies = [
  "bitflags 1.3.2",
  "cexpr",
diff -Naur firefox-113.0.orig/Cargo.toml firefox-113.0/Cargo.toml
--- firefox-113.0.orig/Cargo.toml	2023-05-04 23:09:09.000000000 +0100
+++ firefox-113.0/Cargo.toml	2023-05-26 19:16:47.794821340 +0100
@@ -119,7 +119,7 @@
 wasi = { path = "build/rust/wasi" }
 
 # Patch bindgen 0.63 to 0.64
-bindgen = { path = "build/rust/bindgen" }
+bindgen_0_63 = { package = "bindgen", path = "build/rust/bindgen" }
 
 # Patch memoffset 0.6 to 0.8
 memoffset = { path = "build/rust/memoffset" }
@@ -145,6 +145,9 @@
 # Overrides to allow easier use of common internal crates.
 moz_asserts = { path = "mozglue/static/rust/moz_asserts" }
 
+# Patch bindgen to work around issues with some unsound transmutes when compiling with LLVM 16+.
+bindgen = { path = "third_party/rust/bindgen" }
+
 # Patch `rure` to disable building the cdylib and staticlib targets
 # Cargo has no way to disable building targets your dependencies provide which
 # you don't depend on, and linking the cdylib breaks during instrumentation
diff -Naur firefox-113.0.orig/supply-chain/config.toml firefox-113.0/supply-chain/config.toml
--- firefox-113.0.orig/supply-chain/config.toml	2023-05-04 23:09:18.000000000 +0100
+++ firefox-113.0/supply-chain/config.toml	2023-05-26 19:17:39.735809873 +0100
@@ -23,6 +23,10 @@
 audit-as-crates-io = true
 notes = "This is the upstream code plus a few local fixes, see bug 1685697."
 
+[policy."bindgen:0.64.0"]
+audit-as-crates-io = true
+notes = "This is a local override of the bindgen crate from crates.io, with a small local patch."
+
 [policy.chardetng]
 audit-as-crates-io = true
 notes = "This is a crate Henri wrote which is also published. We should probably update Firefox to tip and certify that."
diff -Naur firefox-113.0.orig/third_party/rust/bindgen/codegen/mod.rs firefox-113.0/third_party/rust/bindgen/codegen/mod.rs
--- firefox-113.0.orig/third_party/rust/bindgen/codegen/mod.rs	2023-05-04 23:09:24.000000000 +0100
+++ firefox-113.0/third_party/rust/bindgen/codegen/mod.rs	2023-05-26 19:15:39.741526488 +0100
@@ -141,12 +141,13 @@
     item: &Item,
     ctx: &BindgenContext,
     packed: bool,
+    forward_decl: bool,
 ) -> DerivableTraits {
     let mut derivable_traits = DerivableTraits::empty();
 
     let all_template_params = item.all_template_params(ctx);
 
-    if item.can_derive_copy(ctx) && !item.annotations().disallow_copy() {
+    if item.can_derive_copy(ctx) && !item.annotations().disallow_copy() && !forward_decl {
         derivable_traits |= DerivableTraits::COPY;
 
         if ctx.options().rust_features().builtin_clone_impls ||
@@ -991,7 +992,7 @@
                             vec![attributes::repr("transparent")];
                         let packed = false; // Types can't be packed in Rust.
                         let derivable_traits =
-                            derives_of_item(item, ctx, packed);
+                            derives_of_item(item, ctx, packed, false);
                         if !derivable_traits.is_empty() {
                             let derives: Vec<_> = derivable_traits.into();
                             attributes.push(attributes::derives(&derives))
@@ -2032,8 +2033,9 @@
         }
 
         if forward_decl {
+            let prefix = ctx.trait_prefix();
             fields.push(quote! {
-                _unused: [u8; 0],
+                _unused: ::#prefix::cell::UnsafeCell<[u8; 0]>,
             });
         }
 
@@ -2095,7 +2097,7 @@
             }
         }
 
-        let derivable_traits = derives_of_item(item, ctx, packed);
+        let derivable_traits = derives_of_item(item, ctx, packed, self.is_forward_declaration());
         if !derivable_traits.contains(DerivableTraits::DEBUG) {
             needs_debug_impl = ctx.options().derive_debug &&
                 ctx.options().impl_debug &&
@@ -3127,7 +3129,7 @@
 
         if !variation.is_const() {
             let packed = false; // Enums can't be packed in Rust.
-            let mut derives = derives_of_item(item, ctx, packed);
+            let mut derives = derives_of_item(item, ctx, packed, false);
             // For backwards compat, enums always derive
             // Clone/Eq/PartialEq/Hash, even if we don't generate those by
             // default.
