From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <gentoo-commits+bounces-1272163-garchives=archives.gentoo.org@lists.gentoo.org>
Received: from lists.gentoo.org (pigeon.gentoo.org [208.92.234.80])
	(using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits))
	(No client certificate requested)
	by finch.gentoo.org (Postfix) with ESMTPS id 6F1EE1382C5
	for <garchives@archives.gentoo.org>; Sun, 18 Apr 2021 01:24:12 +0000 (UTC)
Received: from pigeon.gentoo.org (localhost [127.0.0.1])
	by pigeon.gentoo.org (Postfix) with SMTP id 7AFF1E0801;
	Sun, 18 Apr 2021 01:24:11 +0000 (UTC)
Received: from smtp.gentoo.org (smtp.gentoo.org [140.211.166.183])
	(using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits))
	(No client certificate requested)
	by pigeon.gentoo.org (Postfix) with ESMTPS id 453AAE0815
	for <gentoo-commits@lists.gentoo.org>; Sun, 18 Apr 2021 01:24:11 +0000 (UTC)
Received: from oystercatcher.gentoo.org (unknown [IPv6:2a01:4f8:202:4333:225:90ff:fed9:fc84])
	(using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits))
	(No client certificate requested)
	by smtp.gentoo.org (Postfix) with ESMTPS id 92DAE340AB3
	for <gentoo-commits@lists.gentoo.org>; Sun, 18 Apr 2021 01:24:09 +0000 (UTC)
Received: from localhost.localdomain (localhost [IPv6:::1])
	by oystercatcher.gentoo.org (Postfix) with ESMTP id 454096F4
	for <gentoo-commits@lists.gentoo.org>; Sun, 18 Apr 2021 01:24:08 +0000 (UTC)
From: "Georgy Yakovlev" <gyakovlev@gentoo.org>
To: gentoo-commits@lists.gentoo.org
Content-Transfer-Encoding: 8bit
Content-type: text/plain; charset=UTF-8
Reply-To: gentoo-dev@lists.gentoo.org, "Georgy Yakovlev" <gyakovlev@gentoo.org>
Message-ID: <1618709004.65b9e4c1a1c3a2de55637c7977584c5827b66366.gyakovlev@gentoo>
Subject: [gentoo-commits] repo/gentoo:master commit in: dev-lang/rust/files/, dev-lang/rust/
X-VCS-Repository: repo/gentoo
X-VCS-Files: dev-lang/rust/files/1.51.0-CVE-2020-36323.patch dev-lang/rust/files/1.51.0-CVE-2021-28876.patch dev-lang/rust/files/1.51.0-CVE-2021-28878.patch dev-lang/rust/files/1.51.0-CVE-2021-28879.patch dev-lang/rust/files/1.51.0-CVE-2021-31162.patch dev-lang/rust/rust-1.51.0-r1.ebuild
X-VCS-Directories: dev-lang/rust/ dev-lang/rust/files/
X-VCS-Committer: gyakovlev
X-VCS-Committer-Name: Georgy Yakovlev
X-VCS-Revision: 65b9e4c1a1c3a2de55637c7977584c5827b66366
X-VCS-Branch: master
Date: Sun, 18 Apr 2021 01:24:08 +0000 (UTC)
Precedence: bulk
List-Post: <mailto:gentoo-commits@lists.gentoo.org>
List-Help: <mailto:gentoo-commits+help@lists.gentoo.org>
List-Unsubscribe: <mailto:gentoo-commits+unsubscribe@lists.gentoo.org>
List-Subscribe: <mailto:gentoo-commits+subscribe@lists.gentoo.org>
List-Id: Gentoo Linux mail <gentoo-commits.gentoo.org>
X-BeenThere: gentoo-commits@lists.gentoo.org
X-Auto-Response-Suppress: DR, RN, NRN, OOF, AutoReply
X-Archives-Salt: 34ddd8b0-ae0a-447e-95e1-87f2f122666d
X-Archives-Hash: b46ab101c5b2669a418e43c543b24268

commit:     65b9e4c1a1c3a2de55637c7977584c5827b66366
Author:     Georgy Yakovlev <gyakovlev <AT> gentoo <DOT> org>
AuthorDate: Sun Apr 18 01:23:09 2021 +0000
Commit:     Georgy Yakovlev <gyakovlev <AT> gentoo <DOT> org>
CommitDate: Sun Apr 18 01:23:24 2021 +0000
URL:        https://gitweb.gentoo.org/repo/gentoo.git/commit/?id=65b9e4c1

dev-lang/rust: security revbump of 1.51.0

Fixes for:
CVE-2020-36323
CVE-2021-28876
CVE-2021-31162

Bug: https://bugs.gentoo.org/782799
Bug: https://bugs.gentoo.org/782367
Package-Manager: Portage-3.0.18, Repoman-3.0.3
Signed-off-by: Georgy Yakovlev <gyakovlev <AT> gentoo.org>

 dev-lang/rust/files/1.51.0-CVE-2020-36323.patch | 175 +++++++
 dev-lang/rust/files/1.51.0-CVE-2021-28876.patch |  39 ++
 dev-lang/rust/files/1.51.0-CVE-2021-28878.patch | 112 +++++
 dev-lang/rust/files/1.51.0-CVE-2021-28879.patch |  84 ++++
 dev-lang/rust/files/1.51.0-CVE-2021-31162.patch | 195 ++++++++
 dev-lang/rust/rust-1.51.0-r1.ebuild             | 622 ++++++++++++++++++++++++
 6 files changed, 1227 insertions(+)

diff --git a/dev-lang/rust/files/1.51.0-CVE-2020-36323.patch b/dev-lang/rust/files/1.51.0-CVE-2020-36323.patch
new file mode 100644
index 00000000000..b4f2215cc23
--- /dev/null
+++ b/dev-lang/rust/files/1.51.0-CVE-2020-36323.patch
@@ -0,0 +1,175 @@
+From 6d43225bfb08ec91f7476b76c7fec632c4a096ef Mon Sep 17 00:00:00 2001
+From: Yechan Bae <yechan@gatech.edu>
+Date: Wed, 3 Feb 2021 16:36:33 -0500
+Subject: [PATCH 1/2] Fixes #80335
+
+---
+ library/alloc/src/str.rs   | 42 ++++++++++++++++++++++----------------
+ library/alloc/tests/str.rs | 30 +++++++++++++++++++++++++++
+ 2 files changed, 54 insertions(+), 18 deletions(-)
+
+diff --git a/library/alloc/src/str.rs b/library/alloc/src/str.rs
+index 70e0c7dba5eab..a7584c6b65100 100644
+--- a/library/alloc/src/str.rs
++++ b/library/alloc/src/str.rs
+@@ -90,8 +90,8 @@ impl<S: Borrow<str>> Join<&str> for [S] {
+     }
+ }
+ 
+-macro_rules! spezialize_for_lengths {
+-    ($separator:expr, $target:expr, $iter:expr; $($num:expr),*) => {
++macro_rules! specialize_for_lengths {
++    ($separator:expr, $target:expr, $iter:expr; $($num:expr),*) => {{
+         let mut target = $target;
+         let iter = $iter;
+         let sep_bytes = $separator;
+@@ -102,7 +102,8 @@ macro_rules! spezialize_for_lengths {
+                 $num => {
+                     for s in iter {
+                         copy_slice_and_advance!(target, sep_bytes);
+-                        copy_slice_and_advance!(target, s.borrow().as_ref());
++                        let content_bytes = s.borrow().as_ref();
++                        copy_slice_and_advance!(target, content_bytes);
+                     }
+                 },
+             )*
+@@ -110,11 +111,13 @@ macro_rules! spezialize_for_lengths {
+                 // arbitrary non-zero size fallback
+                 for s in iter {
+                     copy_slice_and_advance!(target, sep_bytes);
+-                    copy_slice_and_advance!(target, s.borrow().as_ref());
++                    let content_bytes = s.borrow().as_ref();
++                    copy_slice_and_advance!(target, content_bytes);
+                 }
+             }
+         }
+-    };
++        target
++    }}
+ }
+ 
+ macro_rules! copy_slice_and_advance {
+@@ -153,7 +156,7 @@ where
+     // if the `len` calculation overflows, we'll panic
+     // we would have run out of memory anyway and the rest of the function requires
+     // the entire Vec pre-allocated for safety
+-    let len = sep_len
++    let reserved_len = sep_len
+         .checked_mul(iter.len())
+         .and_then(|n| {
+             slice.iter().map(|s| s.borrow().as_ref().len()).try_fold(n, usize::checked_add)
+@@ -161,22 +164,25 @@ where
+         .expect("attempt to join into collection with len > usize::MAX");
+ 
+     // crucial for safety
+-    let mut result = Vec::with_capacity(len);
+-    assert!(result.capacity() >= len);
++    let mut result = Vec::with_capacity(reserved_len);
++    debug_assert!(result.capacity() >= reserved_len);
+ 
+     result.extend_from_slice(first.borrow().as_ref());
+ 
+     unsafe {
+-        {
+-            let pos = result.len();
+-            let target = result.get_unchecked_mut(pos..len);
+-
+-            // copy separator and slices over without bounds checks
+-            // generate loops with hardcoded offsets for small separators
+-            // massive improvements possible (~ x2)
+-            spezialize_for_lengths!(sep, target, iter; 0, 1, 2, 3, 4);
+-        }
+-        result.set_len(len);
++        let pos = result.len();
++        let target = result.get_unchecked_mut(pos..reserved_len);
++
++        // copy separator and slices over without bounds checks
++        // generate loops with hardcoded offsets for small separators
++        // massive improvements possible (~ x2)
++        let remain = specialize_for_lengths!(sep, target, iter; 0, 1, 2, 3, 4);
++
++        // issue #80335: A weird borrow implementation can return different
++        // slices for the length calculation and the actual copy, so
++        // `remain.len()` might be non-zero.
++        let result_len = reserved_len - remain.len();
++        result.set_len(result_len);
+     }
+     result
+ }
+diff --git a/library/alloc/tests/str.rs b/library/alloc/tests/str.rs
+index 604835e6cc4a6..6df8d8c2f354f 100644
+--- a/library/alloc/tests/str.rs
++++ b/library/alloc/tests/str.rs
+@@ -160,6 +160,36 @@ fn test_join_for_different_lengths_with_long_separator() {
+     test_join!("~~~~~a~~~~~bc", ["", "a", "bc"], "~~~~~");
+ }
+ 
++#[test]
++fn test_join_isue_80335() {
++    use core::{borrow::Borrow, cell::Cell};
++
++    struct WeirdBorrow {
++        state: Cell<bool>,
++    }
++
++    impl Default for WeirdBorrow {
++        fn default() -> Self {
++            WeirdBorrow { state: Cell::new(false) }
++        }
++    }
++
++    impl Borrow<str> for WeirdBorrow {
++        fn borrow(&self) -> &str {
++            let state = self.state.get();
++            if state {
++                "0"
++            } else {
++                self.state.set(true);
++                "123456"
++            }
++        }
++    }
++
++    let arr: [WeirdBorrow; 3] = Default::default();
++    test_join!("0-0-0", arr, "-");
++}
++
+ #[test]
+ #[cfg_attr(miri, ignore)] // Miri is too slow
+ fn test_unsafe_slice() {
+
+From 26a62701e42d10c03ce5f2f911e7d5edeefa2f0f Mon Sep 17 00:00:00 2001
+From: Yechan Bae <yechan@gatech.edu>
+Date: Sat, 20 Mar 2021 13:42:54 -0400
+Subject: [PATCH 2/2] Update the comment
+
+---
+ library/alloc/src/str.rs | 8 ++++----
+ 1 file changed, 4 insertions(+), 4 deletions(-)
+
+diff --git a/library/alloc/src/str.rs b/library/alloc/src/str.rs
+index a7584c6b65100..4d1e876457b8e 100644
+--- a/library/alloc/src/str.rs
++++ b/library/alloc/src/str.rs
+@@ -163,7 +163,7 @@ where
+         })
+         .expect("attempt to join into collection with len > usize::MAX");
+ 
+-    // crucial for safety
++    // prepare an uninitialized buffer
+     let mut result = Vec::with_capacity(reserved_len);
+     debug_assert!(result.capacity() >= reserved_len);
+ 
+@@ -178,9 +178,9 @@ where
+         // massive improvements possible (~ x2)
+         let remain = specialize_for_lengths!(sep, target, iter; 0, 1, 2, 3, 4);
+ 
+-        // issue #80335: A weird borrow implementation can return different
+-        // slices for the length calculation and the actual copy, so
+-        // `remain.len()` might be non-zero.
++        // A weird borrow implementation may return different
++        // slices for the length calculation and the actual copy.
++        // Make sure we don't expose uninitialized bytes to the caller.
+         let result_len = reserved_len - remain.len();
+         result.set_len(result_len);
+     }

diff --git a/dev-lang/rust/files/1.51.0-CVE-2021-28876.patch b/dev-lang/rust/files/1.51.0-CVE-2021-28876.patch
new file mode 100644
index 00000000000..3a0af024143
--- /dev/null
+++ b/dev-lang/rust/files/1.51.0-CVE-2021-28876.patch
@@ -0,0 +1,39 @@
+From 86a4b27475aab52b998c15f5758540697cc9cff0 Mon Sep 17 00:00:00 2001
+From: =?UTF-8?q?Sebastian=20Dr=C3=B6ge?= <sebastian@centricular.com>
+Date: Thu, 4 Feb 2021 10:23:01 +0200
+Subject: [PATCH] Increment `self.index` before calling
+ `Iterator::self.a.__iterator_get_unchecked` in `Zip` `TrustedRandomAccess`
+ specialization
+
+Otherwise if `Iterator::self.a.__iterator_get_unchecked` panics the
+index would not have been incremented yet and another call to
+`Iterator::next` would read from the same index again, which is not
+allowed according to the API contract of `TrustedRandomAccess` for
+`!Clone`.
+
+Fixes https://github.com/rust-lang/rust/issues/81740
+---
+ library/core/src/iter/adapters/zip.rs | 7 ++++---
+ 1 file changed, 4 insertions(+), 3 deletions(-)
+
+diff --git a/library/core/src/iter/adapters/zip.rs b/library/core/src/iter/adapters/zip.rs
+index 98b8dca961407..9f98353452006 100644
+--- a/library/core/src/iter/adapters/zip.rs
++++ b/library/core/src/iter/adapters/zip.rs
+@@ -198,12 +198,13 @@ where
+                 Some((self.a.__iterator_get_unchecked(i), self.b.__iterator_get_unchecked(i)))
+             }
+         } else if A::may_have_side_effect() && self.index < self.a.size() {
++            let i = self.index;
++            self.index += 1;
+             // match the base implementation's potential side effects
+-            // SAFETY: we just checked that `self.index` < `self.a.len()`
++            // SAFETY: we just checked that `i` < `self.a.len()`
+             unsafe {
+-                self.a.__iterator_get_unchecked(self.index);
++                self.a.__iterator_get_unchecked(i);
+             }
+-            self.index += 1;
+             None
+         } else {
+             None

diff --git a/dev-lang/rust/files/1.51.0-CVE-2021-28878.patch b/dev-lang/rust/files/1.51.0-CVE-2021-28878.patch
new file mode 100644
index 00000000000..f319ab5e8c4
--- /dev/null
+++ b/dev-lang/rust/files/1.51.0-CVE-2021-28878.patch
@@ -0,0 +1,112 @@
+From 2371914a05f8f2763dffe6e2511d0870bcd6b461 Mon Sep 17 00:00:00 2001
+From: Giacomo Stevanato <giaco.stevanato@gmail.com>
+Date: Wed, 3 Mar 2021 21:09:01 +0100
+Subject: [PATCH 1/2] Prevent Zip specialization from calling
+ __iterator_get_unchecked twice with the same index after calling next_back
+
+---
+ library/core/src/iter/adapters/zip.rs | 13 +++++++++----
+ 1 file changed, 9 insertions(+), 4 deletions(-)
+
+diff --git a/library/core/src/iter/adapters/zip.rs b/library/core/src/iter/adapters/zip.rs
+index 817fc2a51e981..ea7a809c6badb 100644
+--- a/library/core/src/iter/adapters/zip.rs
++++ b/library/core/src/iter/adapters/zip.rs
+@@ -13,9 +13,10 @@ use crate::iter::{InPlaceIterable, SourceIter, TrustedLen};
+ pub struct Zip<A, B> {
+     a: A,
+     b: B,
+-    // index and len are only used by the specialized version of zip
++    // index, len and a_len are only used by the specialized version of zip
+     index: usize,
+     len: usize,
++    a_len: usize,
+ }
+ impl<A: Iterator, B: Iterator> Zip<A, B> {
+     pub(in crate::iter) fn new(a: A, b: B) -> Zip<A, B> {
+@@ -110,6 +111,7 @@ where
+             b,
+             index: 0, // unused
+             len: 0,   // unused
++            a_len: 0, // unused
+         }
+     }
+ 
+@@ -184,8 +186,9 @@ where
+     B: TrustedRandomAccess + Iterator,
+ {
+     fn new(a: A, b: B) -> Self {
+-        let len = cmp::min(a.size(), b.size());
+-        Zip { a, b, index: 0, len }
++        let a_len = a.size();
++        let len = cmp::min(a_len, b.size());
++        Zip { a, b, index: 0, len, a_len }
+     }
+ 
+     #[inline]
+@@ -197,7 +200,7 @@ where
+             unsafe {
+                 Some((self.a.__iterator_get_unchecked(i), self.b.__iterator_get_unchecked(i)))
+             }
+-        } else if A::MAY_HAVE_SIDE_EFFECT && self.index < self.a.size() {
++        } else if A::MAY_HAVE_SIDE_EFFECT && self.index < self.a_len {
+             let i = self.index;
+             self.index += 1;
+             self.len += 1;
+@@ -262,6 +265,7 @@ where
+                     for _ in 0..sz_a - self.len {
+                         self.a.next_back();
+                     }
++                    self.a_len = self.len;
+                 }
+                 let sz_b = self.b.size();
+                 if B::MAY_HAVE_SIDE_EFFECT && sz_b > self.len {
+@@ -273,6 +277,7 @@ where
+         }
+         if self.index < self.len {
+             self.len -= 1;
++            self.a_len -= 1;
+             let i = self.len;
+             // SAFETY: `i` is smaller than the previous value of `self.len`,
+             // which is also smaller than or equal to `self.a.len()` and `self.b.len()`
+
+From c1bfb9a78db6d481be1d03355672712c766e20b0 Mon Sep 17 00:00:00 2001
+From: Giacomo Stevanato <giaco.stevanato@gmail.com>
+Date: Fri, 19 Feb 2021 15:25:09 +0100
+Subject: [PATCH 2/2] Add relevant test
+
+---
+ library/core/tests/iter/adapters/zip.rs | 23 +++++++++++++++++++++++
+ 1 file changed, 23 insertions(+)
+
+diff --git a/library/core/tests/iter/adapters/zip.rs b/library/core/tests/iter/adapters/zip.rs
+index a597710392952..000c15f72c886 100644
+--- a/library/core/tests/iter/adapters/zip.rs
++++ b/library/core/tests/iter/adapters/zip.rs
+@@ -265,3 +265,26 @@ fn test_issue_82282() {
+         panic!();
+     }
+ }
++
++#[test]
++fn test_issue_82291() {
++    use std::cell::Cell;
++
++    let mut v1 = [()];
++    let v2 = [()];
++
++    let called = Cell::new(0);
++
++    let mut zip = v1
++        .iter_mut()
++        .map(|r| {
++            called.set(called.get() + 1);
++            r
++        })
++        .zip(&v2);
++
++    zip.next_back();
++    assert_eq!(called.get(), 1);
++    zip.next();
++    assert_eq!(called.get(), 1);
++}

diff --git a/dev-lang/rust/files/1.51.0-CVE-2021-28879.patch b/dev-lang/rust/files/1.51.0-CVE-2021-28879.patch
new file mode 100644
index 00000000000..3407a2ddf2f
--- /dev/null
+++ b/dev-lang/rust/files/1.51.0-CVE-2021-28879.patch
@@ -0,0 +1,84 @@
+From 66a260617a88ed1ad55a46f03c5a90d5ad3004d3 Mon Sep 17 00:00:00 2001
+From: Giacomo Stevanato <giaco.stevanato@gmail.com>
+Date: Fri, 19 Feb 2021 12:15:37 +0100
+Subject: [PATCH 1/3] Increment self.len in specialized ZipImpl to avoid
+ underflow in size_hint
+
+---
+ library/core/src/iter/adapters/zip.rs | 1 +
+ 1 file changed, 1 insertion(+)
+
+diff --git a/library/core/src/iter/adapters/zip.rs b/library/core/src/iter/adapters/zip.rs
+index 9d0f4e3618fc5..ce48016afcd50 100644
+--- a/library/core/src/iter/adapters/zip.rs
++++ b/library/core/src/iter/adapters/zip.rs
+@@ -200,6 +200,7 @@ where
+         } else if A::MAY_HAVE_SIDE_EFFECT && self.index < self.a.size() {
+             let i = self.index;
+             self.index += 1;
++            self.len += 1;
+             // match the base implementation's potential side effects
+             // SAFETY: we just checked that `i` < `self.a.len()`
+             unsafe {
+
+From 8b9ac4d4155c74db5b317046033ab9c05a09e351 Mon Sep 17 00:00:00 2001
+From: Giacomo Stevanato <giaco.stevanato@gmail.com>
+Date: Fri, 19 Feb 2021 12:16:12 +0100
+Subject: [PATCH 2/3] Add test for underflow in specialized Zip's size_hint
+
+---
+ library/core/tests/iter/adapters/zip.rs | 20 ++++++++++++++++++++
+ 1 file changed, 20 insertions(+)
+
+diff --git a/library/core/tests/iter/adapters/zip.rs b/library/core/tests/iter/adapters/zip.rs
+index 1fce0951e365e..a597710392952 100644
+--- a/library/core/tests/iter/adapters/zip.rs
++++ b/library/core/tests/iter/adapters/zip.rs
+@@ -245,3 +245,23 @@ fn test_double_ended_zip() {
+     assert_eq!(it.next_back(), Some((3, 3)));
+     assert_eq!(it.next(), None);
+ }
++
++#[test]
++fn test_issue_82282() {
++    fn overflowed_zip(arr: &[i32]) -> impl Iterator<Item = (i32, &())> {
++        static UNIT_EMPTY_ARR: [(); 0] = [];
++
++        let mapped = arr.into_iter().map(|i| *i);
++        let mut zipped = mapped.zip(UNIT_EMPTY_ARR.iter());
++        zipped.next();
++        zipped
++    }
++
++    let arr = [1, 2, 3];
++    let zip = overflowed_zip(&arr).zip(overflowed_zip(&arr));
++
++    assert_eq!(zip.size_hint(), (0, Some(0)));
++    for _ in zip {
++        panic!();
++    }
++}
+
+From aeb4ea739efb70e0002a4a9c4c7b8027dd0620b3 Mon Sep 17 00:00:00 2001
+From: Giacomo Stevanato <giaco.stevanato@gmail.com>
+Date: Fri, 19 Feb 2021 12:17:48 +0100
+Subject: [PATCH 3/3] Remove useless comparison since now self.index <=
+ self.len is an invariant
+
+---
+ library/core/src/iter/adapters/zip.rs | 2 +-
+ 1 file changed, 1 insertion(+), 1 deletion(-)
+
+diff --git a/library/core/src/iter/adapters/zip.rs b/library/core/src/iter/adapters/zip.rs
+index ce48016afcd50..817fc2a51e981 100644
+--- a/library/core/src/iter/adapters/zip.rs
++++ b/library/core/src/iter/adapters/zip.rs
+@@ -259,7 +259,7 @@ where
+             if sz_a != sz_b {
+                 let sz_a = self.a.size();
+                 if A::MAY_HAVE_SIDE_EFFECT && sz_a > self.len {
+-                    for _ in 0..sz_a - cmp::max(self.len, self.index) {
++                    for _ in 0..sz_a - self.len {
+                         self.a.next_back();
+                     }
+                 }

diff --git a/dev-lang/rust/files/1.51.0-CVE-2021-31162.patch b/dev-lang/rust/files/1.51.0-CVE-2021-31162.patch
new file mode 100644
index 00000000000..fd9165ea3c5
--- /dev/null
+++ b/dev-lang/rust/files/1.51.0-CVE-2021-31162.patch
@@ -0,0 +1,195 @@
+From fa89c0fbcfa8f4d44f153b1195ec5a305540ffc4 Mon Sep 17 00:00:00 2001
+From: The8472 <git@infinite-source.de>
+Date: Mon, 29 Mar 2021 04:22:34 +0200
+Subject: [PATCH 1/3] add testcase for double-drop during Vec in-place
+ collection
+
+---
+ library/alloc/tests/vec.rs | 38 +++++++++++++++++++++++++++++++++++++-
+ 1 file changed, 37 insertions(+), 1 deletion(-)
+
+diff --git a/library/alloc/tests/vec.rs b/library/alloc/tests/vec.rs
+index c142536cd2dfb..b926c697d58ab 100644
+--- a/library/alloc/tests/vec.rs
++++ b/library/alloc/tests/vec.rs
+@@ -1027,7 +1027,7 @@ fn test_from_iter_specialization_head_tail_drop() {
+ }
+ 
+ #[test]
+-fn test_from_iter_specialization_panic_drop() {
++fn test_from_iter_specialization_panic_during_iteration_drops() {
+     let drop_count: Vec<_> = (0..=2).map(|_| Rc::new(())).collect();
+     let src: Vec<_> = drop_count.iter().cloned().collect();
+     let iter = src.into_iter();
+@@ -1050,6 +1050,42 @@ fn test_from_iter_specialization_panic_drop() {
+     );
+ }
+ 
++#[test]
++fn test_from_iter_specialization_panic_during_drop_leaks() {
++    static mut DROP_COUNTER: usize = 0;
++
++    #[derive(Debug)]
++    enum Droppable {
++        DroppedTwice(Box<i32>),
++        PanicOnDrop,
++    }
++
++    impl Drop for Droppable {
++        fn drop(&mut self) {
++            match self {
++                Droppable::DroppedTwice(_) => {
++                    unsafe {
++                        DROP_COUNTER += 1;
++                    }
++                    println!("Dropping!")
++                }
++                Droppable::PanicOnDrop => {
++                    if !std::thread::panicking() {
++                        panic!();
++                    }
++                }
++            }
++        }
++    }
++
++    let _ = std::panic::catch_unwind(AssertUnwindSafe(|| {
++        let v = vec![Droppable::DroppedTwice(Box::new(123)), Droppable::PanicOnDrop];
++        let _ = v.into_iter().take(0).collect::<Vec<_>>();
++    }));
++
++    assert_eq!(unsafe { DROP_COUNTER }, 1);
++}
++
+ #[test]
+ fn test_cow_from() {
+     let borrowed: &[_] = &["borrowed", "(slice)"];
+
+From 421f5d282a51e130d3ca7c4524d8ad6753437da9 Mon Sep 17 00:00:00 2001
+From: The8472 <git@infinite-source.de>
+Date: Mon, 29 Mar 2021 04:22:48 +0200
+Subject: [PATCH 2/3] fix double-drop in in-place collect specialization
+
+---
+ library/alloc/src/vec/into_iter.rs          | 27 ++++++++++++++-------
+ library/alloc/src/vec/source_iter_marker.rs |  4 +--
+ 2 files changed, 20 insertions(+), 11 deletions(-)
+
+diff --git a/library/alloc/src/vec/into_iter.rs b/library/alloc/src/vec/into_iter.rs
+index bcbdffabc7fbe..324e894bafd23 100644
+--- a/library/alloc/src/vec/into_iter.rs
++++ b/library/alloc/src/vec/into_iter.rs
+@@ -85,20 +85,29 @@ impl<T, A: Allocator> IntoIter<T, A> {
+         ptr::slice_from_raw_parts_mut(self.ptr as *mut T, self.len())
+     }
+ 
+-    pub(super) fn drop_remaining(&mut self) {
+-        unsafe {
+-            ptr::drop_in_place(self.as_mut_slice());
+-        }
+-        self.ptr = self.end;
+-    }
++    /// Drops remaining elements and relinquishes the backing allocation.
++    ///
++    /// This is roughly equivalent to the following, but more efficient
++    ///
++    /// ```
++    /// # let mut into_iter = Vec::<u8>::with_capacity(10).into_iter();
++    /// (&mut into_iter).for_each(core::mem::drop);
++    /// unsafe { core::ptr::write(&mut into_iter, Vec::new().into_iter()); }
++    /// ```
++    pub(super) fn forget_allocation_drop_remaining(&mut self) {
++        let remaining = self.as_raw_mut_slice();
+ 
+-    /// Relinquishes the backing allocation, equivalent to
+-    /// `ptr::write(&mut self, Vec::new().into_iter())`
+-    pub(super) fn forget_allocation(&mut self) {
++        // overwrite the individual fields instead of creating a new
++        // struct and then overwriting &mut self.
++        // this creates less assembly
+         self.cap = 0;
+         self.buf = unsafe { NonNull::new_unchecked(RawVec::NEW.ptr()) };
+         self.ptr = self.buf.as_ptr();
+         self.end = self.buf.as_ptr();
++
++        unsafe {
++            ptr::drop_in_place(remaining);
++        }
+     }
+ }
+ 
+diff --git a/library/alloc/src/vec/source_iter_marker.rs b/library/alloc/src/vec/source_iter_marker.rs
+index 50882fc17673e..e857d284d3ab6 100644
+--- a/library/alloc/src/vec/source_iter_marker.rs
++++ b/library/alloc/src/vec/source_iter_marker.rs
+@@ -69,9 +69,9 @@ where
+         }
+ 
+         // drop any remaining values at the tail of the source
+-        src.drop_remaining();
+         // but prevent drop of the allocation itself once IntoIter goes out of scope
+-        src.forget_allocation();
++        // if the drop panics then we also leak any elements collected into dst_buf
++        src.forget_allocation_drop_remaining();
+ 
+         let vec = unsafe { Vec::from_raw_parts(dst_buf, len, cap) };
+ 
+
+From 328a5e040780984c60dde2db300dad4f1323c39d Mon Sep 17 00:00:00 2001
+From: The8472 <git@infinite-source.de>
+Date: Fri, 2 Apr 2021 23:06:05 +0200
+Subject: [PATCH 3/3] cleanup leak after test to make miri happy
+
+---
+ library/alloc/tests/vec.rs | 16 +++++++++++++---
+ 1 file changed, 13 insertions(+), 3 deletions(-)
+
+diff --git a/library/alloc/tests/vec.rs b/library/alloc/tests/vec.rs
+index b926c697d58ab..b9fe07c73e55e 100644
+--- a/library/alloc/tests/vec.rs
++++ b/library/alloc/tests/vec.rs
+@@ -1,3 +1,4 @@
++use alloc::boxed::Box;
+ use std::borrow::Cow;
+ use std::cell::Cell;
+ use std::collections::TryReserveError::*;
+@@ -1056,14 +1057,14 @@ fn test_from_iter_specialization_panic_during_drop_leaks() {
+ 
+     #[derive(Debug)]
+     enum Droppable {
+-        DroppedTwice(Box<i32>),
++        DroppedTwice,
+         PanicOnDrop,
+     }
+ 
+     impl Drop for Droppable {
+         fn drop(&mut self) {
+             match self {
+-                Droppable::DroppedTwice(_) => {
++                Droppable::DroppedTwice => {
+                     unsafe {
+                         DROP_COUNTER += 1;
+                     }
+@@ -1078,12 +1079,21 @@ fn test_from_iter_specialization_panic_during_drop_leaks() {
+         }
+     }
+ 
++    let mut to_free: *mut Droppable = core::ptr::null_mut();
++    let mut cap = 0;
++
+     let _ = std::panic::catch_unwind(AssertUnwindSafe(|| {
+-        let v = vec![Droppable::DroppedTwice(Box::new(123)), Droppable::PanicOnDrop];
++        let mut v = vec![Droppable::DroppedTwice, Droppable::PanicOnDrop];
++        to_free = v.as_mut_ptr();
++        cap = v.capacity();
+         let _ = v.into_iter().take(0).collect::<Vec<_>>();
+     }));
+ 
+     assert_eq!(unsafe { DROP_COUNTER }, 1);
++    // clean up the leak to keep miri happy
++    unsafe {
++        Vec::from_raw_parts(to_free, 0, cap);
++    }
+ }
+ 
+ #[test]

diff --git a/dev-lang/rust/rust-1.51.0-r1.ebuild b/dev-lang/rust/rust-1.51.0-r1.ebuild
new file mode 100644
index 00000000000..e2f25109d3e
--- /dev/null
+++ b/dev-lang/rust/rust-1.51.0-r1.ebuild
@@ -0,0 +1,622 @@
+# Copyright 1999-2021 Gentoo Authors
+# Distributed under the terms of the GNU General Public License v2
+
+EAPI=7
+
+PYTHON_COMPAT=( python3_{7..9} )
+
+inherit bash-completion-r1 check-reqs estack flag-o-matic llvm multiprocessing multilib-build python-any-r1 rust-toolchain toolchain-funcs
+
+if [[ ${PV} = *beta* ]]; then
+	betaver=${PV//*beta}
+	BETA_SNAPSHOT="${betaver:0:4}-${betaver:4:2}-${betaver:6:2}"
+	MY_P="rustc-beta"
+	SLOT="beta/${PV}"
+	SRC="${BETA_SNAPSHOT}/rustc-beta-src.tar.xz -> rustc-${PV}-src.tar.xz"
+else
+	ABI_VER="$(ver_cut 1-2)"
+	SLOT="stable/${ABI_VER}"
+	MY_P="rustc-${PV}"
+	SRC="${MY_P}-src.tar.xz"
+	KEYWORDS="~amd64 ~arm ~arm64 ~ppc64 ~x86"
+fi
+
+RUST_STAGE0_VERSION="1.$(($(ver_cut 2) - 1)).0"
+
+DESCRIPTION="Systems programming language from Mozilla"
+HOMEPAGE="https://www.rust-lang.org/"
+
+SRC_URI="
+	https://static.rust-lang.org/dist/${SRC}
+	!system-bootstrap? ( $(rust_all_arch_uris rust-${RUST_STAGE0_VERSION}) )
+"
+
+# keep in sync with llvm ebuild of the same version as bundled one.
+ALL_LLVM_TARGETS=( AArch64 AMDGPU ARM AVR BPF Hexagon Lanai Mips MSP430
+	NVPTX PowerPC RISCV Sparc SystemZ WebAssembly X86 XCore )
+ALL_LLVM_TARGETS=( "${ALL_LLVM_TARGETS[@]/#/llvm_targets_}" )
+LLVM_TARGET_USEDEPS=${ALL_LLVM_TARGETS[@]/%/?}
+
+LICENSE="|| ( MIT Apache-2.0 ) BSD-1 BSD-2 BSD-4 UoI-NCSA"
+
+IUSE="clippy cpu_flags_x86_sse2 debug doc libressl miri nightly parallel-compiler rls rustfmt system-bootstrap system-llvm test wasm ${ALL_LLVM_TARGETS[*]}"
+
+# Please keep the LLVM dependency block separate. Since LLVM is slotted,
+# we need to *really* make sure we're not pulling more than one slot
+# simultaneously.
+
+# How to use it:
+# 1. List all the working slots (with min versions) in ||, newest first.
+# 2. Update the := to specify *max* version, e.g. < 12.
+# 3. Specify LLVM_MAX_SLOT, e.g. 11.
+LLVM_DEPEND="
+	|| (
+		sys-devel/llvm:11[${LLVM_TARGET_USEDEPS// /,}]
+	)
+	<sys-devel/llvm-12:=
+	wasm? ( sys-devel/lld )
+"
+LLVM_MAX_SLOT=11
+
+# to bootstrap we need at least exactly previous version, or same.
+# most of the time previous versions fail to bootstrap with newer
+# for example 1.47.x, requires at least 1.46.x, 1.47.x is ok,
+# but it fails to bootstrap with 1.48.x
+# https://github.com/rust-lang/rust/blob/${PV}/src/stage0.txt
+BOOTSTRAP_DEPEND="||
+	(
+		=dev-lang/rust-$(ver_cut 1).$(($(ver_cut 2) - 1))*
+		=dev-lang/rust-bin-$(ver_cut 1).$(($(ver_cut 2) - 1))*
+		=dev-lang/rust-$(ver_cut 1).$(ver_cut 2)*
+		=dev-lang/rust-bin-$(ver_cut 1).$(ver_cut 2)*
+	)
+"
+
+BDEPEND="${PYTHON_DEPS}
+	app-eselect/eselect-rust
+	|| (
+		>=sys-devel/gcc-4.7
+		>=sys-devel/clang-3.5
+	)
+	system-bootstrap? ( ${BOOTSTRAP_DEPEND} )
+	!system-llvm? (
+		dev-util/cmake
+		dev-util/ninja
+	)
+"
+
+DEPEND="
+	>=app-arch/xz-utils-5.2
+	net-misc/curl:=[http2,ssl]
+	sys-libs/zlib:=
+	!libressl? ( dev-libs/openssl:0= )
+	libressl? ( dev-libs/libressl:0= )
+	elibc_musl? ( sys-libs/libunwind:= )
+	system-llvm? (
+		${LLVM_DEPEND}
+	)
+"
+
+# we need to block older versions due to layout changes.
+RDEPEND="${DEPEND}
+	app-eselect/eselect-rust
+	!<dev-lang/rust-1.47.0-r1
+	!<dev-lang/rust-bin-1.47.0-r1
+"
+
+REQUIRED_USE="|| ( ${ALL_LLVM_TARGETS[*]} )
+	miri? ( nightly )
+	parallel-compiler? ( nightly )
+	test? ( ${ALL_LLVM_TARGETS[*]} )
+	wasm? ( llvm_targets_WebAssembly )
+	x86? ( cpu_flags_x86_sse2 )
+"
+
+# we don't use cmake.eclass, but can get a warnings
+CMAKE_WARN_UNUSED_CLI=no
+
+QA_FLAGS_IGNORED="
+	usr/lib/${PN}/${PV}/bin/.*
+	usr/lib/${PN}/${PV}/libexec/.*
+	usr/lib/${PN}/${PV}/lib/lib.*.so
+	usr/lib/${PN}/${PV}/lib/rustlib/.*/bin/.*
+	usr/lib/${PN}/${PV}/lib/rustlib/.*/lib/lib.*.so
+"
+
+QA_SONAME="
+	usr/lib/${PN}/${PV}/lib/lib.*.so.*
+	usr/lib/${PN}/${PV}/lib/rustlib/.*/lib/lib.*.so
+"
+
+# causes double bootstrap
+RESTRICT="test"
+
+PATCHES=(
+	"${FILESDIR}"/1.47.0-libressl.patch
+	"${FILESDIR}"/1.47.0-ignore-broken-and-non-applicable-tests.patch
+	"${FILESDIR}"/1.49.0-gentoo-musl-target-specs.patch
+	"${FILESDIR}"/1.51.0-bootstrap-panic.patch
+	"${FILESDIR}"/1.51.0-CVE-2020-36323.patch
+	"${FILESDIR}"/1.51.0-CVE-2021-28876.patch
+	#"${FILESDIR}"/1.51.0-CVE-2021-28878.patch
+	#"${FILESDIR}"/1.51.0-CVE-2021-28879.patch
+	"${FILESDIR}"/1.51.0-CVE-2021-31162.patch
+)
+
+S="${WORKDIR}/${MY_P}-src"
+
+toml_usex() {
+	usex "${1}" true false
+}
+
+boostrap_rust_version_check() {
+	# never call from pkg_pretend. eselect-rust may be not installed yet.
+	[[ ${MERGE_TYPE} == binary ]] && return
+	local rustc_wanted="$(ver_cut 1).$(($(ver_cut 2) - 1))"
+	local rustc_toonew="$(ver_cut 1).$(($(ver_cut 2) + 1))"
+	local rustc_version=( $(eselect --brief rust show 2>/dev/null) )
+	rustc_version=${rustc_version[0]#rust-bin-}
+	rustc_version=${rustc_version#rust-}
+
+	[[ -z "${rustc_version}" ]] && die "Failed to determine rust version, check 'eselect rust' output"
+
+	if ver_test "${rustc_version}" -lt "${rustc_wanted}" ; then
+		eerror "Rust >=${rustc_wanted} is required"
+		eerror "please run 'eselect rust' and set correct rust version"
+		die "selected rust version is too old"
+	elif ver_test "${rustc_version}" -ge "${rustc_toonew}" ; then
+		eerror "Rust <${rustc_toonew} is required"
+		eerror "please run 'eselect rust' and set correct rust version"
+		die "selected rust version is too new"
+	else
+		einfo "Using rust ${rustc_version} to build"
+	fi
+}
+
+pre_build_checks() {
+	local M=6144
+	M=$(( $(usex clippy 128 0) + ${M} ))
+	M=$(( $(usex miri 128 0) + ${M} ))
+	M=$(( $(usex rls 512 0) + ${M} ))
+	M=$(( $(usex rustfmt 256 0) + ${M} ))
+	M=$(( $(usex system-llvm 0 2048) + ${M} ))
+	M=$(( $(usex wasm 256 0) + ${M} ))
+	M=$(( $(usex debug 15 10) * ${M} / 10 ))
+	eshopts_push -s extglob
+	if is-flagq '-g?(gdb)?([1-9])'; then
+		M=$(( 15 * ${M} / 10 ))
+	fi
+	eshopts_pop
+	M=$(( $(usex system-bootstrap 0 1024) + ${M} ))
+	M=$(( $(usex doc 256 0) + ${M} ))
+	CHECKREQS_DISK_BUILD=${M}M check-reqs_pkg_${EBUILD_PHASE}
+}
+
+pkg_pretend() {
+	pre_build_checks
+}
+
+pkg_setup() {
+	pre_build_checks
+	python-any-r1_pkg_setup
+
+	export LIBGIT2_NO_PKG_CONFIG=1 #749381
+
+	use system-bootstrap && boostrap_rust_version_check
+
+	if use system-llvm; then
+		llvm_pkg_setup
+
+		local llvm_config="$(get_llvm_prefix "$LLVM_MAX_SLOT")/bin/llvm-config"
+		export LLVM_LINK_SHARED=1
+		export RUSTFLAGS="${RUSTFLAGS} -Lnative=$("${llvm_config}" --libdir)"
+	fi
+}
+
+src_prepare() {
+	if ! use system-bootstrap; then
+		local rust_stage0_root="${WORKDIR}"/rust-stage0
+		local rust_stage0="rust-${RUST_STAGE0_VERSION}-$(rust_abi)"
+
+		"${WORKDIR}/${rust_stage0}"/install.sh --disable-ldconfig \
+			--destdir="${rust_stage0_root}" --prefix=/ || die
+	fi
+
+	default
+}
+
+src_configure() {
+	local rust_target="" rust_targets="" arch_cflags
+
+	# Collect rust target names to compile standard libs for all ABIs.
+	for v in $(multilib_get_enabled_abi_pairs); do
+		rust_targets="${rust_targets},\"$(rust_abi $(get_abi_CHOST ${v##*.}))\""
+	done
+	if use wasm; then
+		rust_targets="${rust_targets},\"wasm32-unknown-unknown\""
+		if use system-llvm; then
+			# un-hardcode rust-lld linker for this target
+			# https://bugs.gentoo.org/715348
+			sed -i '/linker:/ s/rust-lld/wasm-ld/' compiler/rustc_target/src/spec/wasm32_base.rs || die
+		fi
+	fi
+	rust_targets="${rust_targets#,}"
+
+	local tools="\"cargo\","
+	if use clippy; then
+		tools="\"clippy\",$tools"
+	fi
+	if use miri; then
+		tools="\"miri\",$tools"
+	fi
+	if use rls; then
+		tools="\"rls\",\"analysis\",\"src\",$tools"
+	fi
+	if use rustfmt; then
+		tools="\"rustfmt\",$tools"
+	fi
+
+	local rust_stage0_root
+	if use system-bootstrap; then
+		rust_stage0_root="$(rustc --print sysroot)"
+	else
+		rust_stage0_root="${WORKDIR}"/rust-stage0
+	fi
+
+	rust_target="$(rust_abi)"
+
+	cat <<- _EOF_ > "${S}"/config.toml
+		[llvm]
+		download-ci-llvm = false
+		optimize = $(toml_usex !debug)
+		release-debuginfo = $(toml_usex debug)
+		assertions = $(toml_usex debug)
+		ninja = true
+		targets = "${LLVM_TARGETS// /;}"
+		experimental-targets = ""
+		link-shared = $(toml_usex system-llvm)
+		[build]
+		build = "${rust_target}"
+		host = ["${rust_target}"]
+		target = [${rust_targets}]
+		cargo = "${rust_stage0_root}/bin/cargo"
+		rustc = "${rust_stage0_root}/bin/rustc"
+		docs = $(toml_usex doc)
+		compiler-docs = $(toml_usex doc)
+		submodules = false
+		python = "${EPYTHON}"
+		locked-deps = true
+		vendor = true
+		extended = true
+		tools = [${tools}]
+		verbose = 2
+		sanitizers = false
+		profiler = false
+		cargo-native-static = false
+		[install]
+		prefix = "${EPREFIX}/usr/lib/${PN}/${PV}"
+		sysconfdir = "etc"
+		docdir = "share/doc/rust"
+		bindir = "bin"
+		libdir = "lib"
+		mandir = "share/man"
+		[rust]
+		# https://github.com/rust-lang/rust/issues/54872
+		codegen-units-std = 1
+		optimize = true
+		debug = $(toml_usex debug)
+		debug-assertions = $(toml_usex debug)
+		debuginfo-level-rustc = 0
+		backtrace = true
+		incremental = false
+		default-linker = "$(tc-getCC)"
+		parallel-compiler = $(toml_usex parallel-compiler)
+		channel = "$(usex nightly nightly stable)"
+		description = "gentoo"
+		rpath = false
+		verbose-tests = true
+		optimize-tests = $(toml_usex !debug)
+		codegen-tests = true
+		dist-src = false
+		remap-debuginfo = true
+		lld = $(usex system-llvm false $(toml_usex wasm))
+		backtrace-on-ice = true
+		jemalloc = false
+		[dist]
+		src-tarball = false
+	_EOF_
+
+	for v in $(multilib_get_enabled_abi_pairs); do
+		rust_target=$(rust_abi $(get_abi_CHOST ${v##*.}))
+		arch_cflags="$(get_abi_CFLAGS ${v##*.})"
+
+		cat <<- _EOF_ >> "${S}"/config.env
+			CFLAGS_${rust_target}=${arch_cflags}
+		_EOF_
+
+		cat <<- _EOF_ >> "${S}"/config.toml
+			[target.${rust_target}]
+			cc = "$(tc-getBUILD_CC)"
+			cxx = "$(tc-getBUILD_CXX)"
+			linker = "$(tc-getCC)"
+			ar = "$(tc-getAR)"
+		_EOF_
+		# librustc_target/spec/linux_musl_base.rs sets base.crt_static_default = true;
+		if use elibc_musl; then
+			cat <<- _EOF_ >> "${S}"/config.toml
+				crt-static = false
+			_EOF_
+		fi
+		if use system-llvm; then
+			cat <<- _EOF_ >> "${S}"/config.toml
+				llvm-config = "$(get_llvm_prefix "${LLVM_MAX_SLOT}")/bin/llvm-config"
+			_EOF_
+		fi
+	done
+	if use wasm; then
+		cat <<- _EOF_ >> "${S}"/config.toml
+			[target.wasm32-unknown-unknown]
+			linker = "$(usex system-llvm lld rust-lld)"
+		_EOF_
+	fi
+
+	if [[ -n ${I_KNOW_WHAT_I_AM_DOING_CROSS} ]]; then # whitespace intentionally shifted below
+	# experimental cross support
+	# discussion: https://bugs.gentoo.org/679878
+	# TODO: c*flags, clang, system-llvm, cargo.eclass target support
+	# it would be much better if we could split out stdlib
+	# complilation to separate ebuild and abuse CATEGORY to
+	# just install to /usr/lib/rustlib/<target>
+
+	# extra targets defined as a bash array
+	# spec format:  <LLVM target>:<rust-target>:<CTARGET>
+	# best place would be /etc/portage/env/dev-lang/rust
+	# Example:
+	# RUST_CROSS_TARGETS=(
+	#	"AArch64:aarch64-unknown-linux-gnu:aarch64-unknown-linux-gnu"
+	# )
+	# no extra hand holding is done, no target transformations, all
+	# values are passed as-is with just basic checks, so it's up to user to supply correct values
+	# valid rust targets can be obtained with
+	# 	rustc --print target-list
+	# matching cross toolchain has to be installed
+	# matching LLVM_TARGET has to be enabled for both rust and llvm (if using system one)
+	# only gcc toolchains installed with crossdev are checked for now.
+
+	# BUG: we can't pass host flags to cross compiler, so just filter for now
+	# BUG: this should be more fine-grained.
+	filter-flags '-mcpu=*' '-march=*' '-mtune=*'
+
+	local cross_target_spec
+	for cross_target_spec in "${RUST_CROSS_TARGETS[@]}";do
+		# extracts first element form <LLVM target>:<rust-target>:<CTARGET>
+		local cross_llvm_target="${cross_target_spec%%:*}"
+		# extracts toolchain triples, <rust-target>:<CTARGET>
+		local cross_triples="${cross_target_spec#*:}"
+		# extracts first element after before : separator
+		local cross_rust_target="${cross_triples%%:*}"
+		# extracts last element after : separator
+		local cross_toolchain="${cross_triples##*:}"
+		use llvm_targets_${cross_llvm_target} || die "need llvm_targets_${cross_llvm_target} target enabled"
+		command -v ${cross_toolchain}-gcc > /dev/null 2>&1 || die "need ${cross_toolchain} cross toolchain"
+
+		cat <<- _EOF_ >> "${S}"/config.toml
+			[target.${cross_rust_target}]
+			cc = "${cross_toolchain}-gcc"
+			cxx = "${cross_toolchain}-g++"
+			linker = "${cross_toolchain}-gcc"
+			ar = "${cross_toolchain}-ar"
+		_EOF_
+		if use system-llvm; then
+			cat <<- _EOF_ >> "${S}"/config.toml
+				llvm-config = "$(get_llvm_prefix "${LLVM_MAX_SLOT}")/bin/llvm-config"
+			_EOF_
+		fi
+
+		# append cross target to "normal" target list
+		# example 'target = ["powerpc64le-unknown-linux-gnu"]'
+		# becomes 'target = ["powerpc64le-unknown-linux-gnu","aarch64-unknown-linux-gnu"]'
+
+		rust_targets="${rust_targets},\"${cross_rust_target}\""
+		sed -i "/^target = \[/ s#\[.*\]#\[${rust_targets}\]#" config.toml || die
+
+		ewarn
+		ewarn "Enabled ${cross_rust_target} rust target"
+		ewarn "Using ${cross_toolchain} cross toolchain"
+		ewarn
+		if ! has_version -b 'sys-devel/binutils[multitarget]' ; then
+			ewarn "'sys-devel/binutils[multitarget]' is not installed"
+			ewarn "'strip' will be unable to strip cross libraries"
+			ewarn "cross targets will be installed with full debug information"
+			ewarn "enable 'multitarget' USE flag for binutils to be able to strip object files"
+			ewarn
+			ewarn "Alternatively llvm-strip can be used, it supports stripping any target"
+			ewarn "define STRIP=\"llvm-strip\" to use it (experimental)"
+			ewarn
+		fi
+	done
+	fi # I_KNOW_WHAT_I_AM_DOING_CROSS
+
+	einfo "Rust configured with the following settings:"
+	cat "${S}"/config.toml || die
+}
+
+src_compile() {
+	# we need \n IFS to have config.env with spaces loaded properly. #734018
+	(
+	IFS=$'\n'
+	env $(cat "${S}"/config.env) RUST_BACKTRACE=1\
+		"${EPYTHON}" ./x.py dist -vv --config="${S}"/config.toml -j$(makeopts_jobs) || die
+	)
+}
+
+src_test() {
+	# https://rustc-dev-guide.rust-lang.org/tests/intro.html
+
+	# those are basic and codegen tests.
+	local tests=(
+		codegen
+		codegen-units
+		compile-fail
+		incremental
+		mir-opt
+		pretty
+		run-make
+	)
+
+	# fails if llvm is not built with ALL targets.
+	# and known to fail with system llvm sometimes.
+	use system-llvm || tests+=( assembly )
+
+	# fragile/expensive/less important tests
+	# or tests that require extra builds
+	# TODO: instead of skipping, just make some nonfatal.
+	if [[ ${ERUST_RUN_EXTRA_TESTS:-no} != no ]]; then
+		tests+=(
+			rustdoc
+			rustdoc-js
+			rustdoc-js-std
+			rustdoc-ui
+			run-make-fulldeps
+			ui
+			ui-fulldeps
+		)
+	fi
+
+	local i failed=()
+	einfo "rust_src_test: enabled tests ${tests[@]/#/src/test/}"
+	for i in "${tests[@]}"; do
+		local t="src/test/${i}"
+		einfo "rust_src_test: running ${t}"
+		if ! (
+				IFS=$'\n'
+				env $(cat "${S}"/config.env) RUST_BACKTRACE=1 \
+				"${EPYTHON}" ./x.py test -vv --config="${S}"/config.toml \
+				-j$(makeopts_jobs) --no-doc --no-fail-fast "${t}"
+			)
+		then
+				failed+=( "${t}" )
+				eerror "rust_src_test: ${t} failed"
+		fi
+	done
+
+	if [[ ${#failed[@]} -ne 0 ]]; then
+		eerror "rust_src_test: failure summary: ${failed[@]}"
+		die "aborting due to test failures"
+	fi
+}
+
+src_install() {
+	(
+	IFS=$'\n'
+	env $(cat "${S}"/config.env) DESTDIR="${D}" \
+		"${EPYTHON}" ./x.py install -vv --config="${S}"/config.toml || die
+	)
+
+	# bug #689562, #689160
+	rm -v "${ED}/usr/lib/${PN}/${PV}/etc/bash_completion.d/cargo" || die
+	rmdir -v "${ED}/usr/lib/${PN}/${PV}"/etc{/bash_completion.d,} || die
+	newbashcomp src/tools/cargo/src/etc/cargo.bashcomp.sh cargo
+
+	local symlinks=(
+		cargo
+		rustc
+		rustdoc
+		rust-gdb
+		rust-gdbgui
+		rust-lldb
+	)
+
+	use clippy && symlinks+=( clippy-driver cargo-clippy )
+	use miri && symlinks+=( miri cargo-miri )
+	use rls && symlinks+=( rls )
+	use rustfmt && symlinks+=( rustfmt cargo-fmt )
+
+	einfo "installing eselect-rust symlinks and paths: ${symlinks[@]}"
+	local i
+	for i in "${symlinks[@]}"; do
+		# we need realpath on /usr/bin/* symlink return version-appended binary path.
+		# so /usr/bin/rustc should point to /usr/lib/rust/<ver>/bin/rustc-<ver>
+		# need to fix eselect-rust to remove this hack.
+		local ver_i="${i}-${PV}"
+		if [[ -f "${ED}/usr/lib/${PN}/${PV}/bin/${i}" ]]; then
+			einfo "Installing ${i} symlink"
+			ln -v "${ED}/usr/lib/${PN}/${PV}/bin/${i}" "${ED}/usr/lib/${PN}/${PV}/bin/${ver_i}" || die
+		else
+			ewarn "${i} symlink requested, but source file not found"
+			ewarn "please report this"
+		fi
+		dosym "../lib/${PN}/${PV}/bin/${ver_i}" "/usr/bin/${ver_i}"
+	done
+
+	# symlinks to switch components to active rust in eselect
+	dosym "${PV}/lib" "/usr/lib/${PN}/lib-${PV}"
+	dosym "${PV}/libexec" "/usr/lib/${PN}/libexec-${PV}"
+	dosym "${PV}/share/man" "/usr/lib/${PN}/man-${PV}"
+	dosym "rust/${PV}/lib/rustlib" "/usr/lib/rustlib-${PV}"
+	dosym "../../lib/${PN}/${PV}/share/doc/rust" "/usr/share/doc/${P}"
+
+	newenvd - "50${P}" <<-_EOF_
+		LDPATH="${EPREFIX}/usr/lib/rust/lib"
+		MANPATH="${EPREFIX}/usr/lib/rust/man"
+		$(use amd64 && usex elibc_musl 'CARGO_TARGET_X86_64_UNKNOWN_LINUX_MUSL_RUSTFLAGS="-C target-feature=-crt-static"' '')
+		$(use arm64 && usex elibc_musl 'CARGO_TARGET_AARCH64_UNKNOWN_LINUX_MUSL_RUSTFLAGS="-C target-feature=-crt-static"' '')
+	_EOF_
+
+	rm -rf "${ED}/usr/lib/${PN}/${PV}"/*.old || die
+	rm -rf "${ED}/usr/lib/${PN}/${PV}/doc"/*.old || die
+
+	# note: eselect-rust adds EROOT to all paths below
+	cat <<-_EOF_ > "${T}/provider-${P}"
+		/usr/bin/cargo
+		/usr/bin/rustdoc
+		/usr/bin/rust-gdb
+		/usr/bin/rust-gdbgui
+		/usr/bin/rust-lldb
+		/usr/lib/rustlib
+		/usr/lib/rust/lib
+		/usr/lib/rust/libexec
+		/usr/lib/rust/man
+		/usr/share/doc/rust
+	_EOF_
+
+	if use clippy; then
+		echo /usr/bin/clippy-driver >> "${T}/provider-${P}"
+		echo /usr/bin/cargo-clippy >> "${T}/provider-${P}"
+	fi
+	if use miri; then
+		echo /usr/bin/miri >> "${T}/provider-${P}"
+		echo /usr/bin/cargo-miri >> "${T}/provider-${P}"
+	fi
+	if use rls; then
+		echo /usr/bin/rls >> "${T}/provider-${P}"
+	fi
+	if use rustfmt; then
+		echo /usr/bin/rustfmt >> "${T}/provider-${P}"
+		echo /usr/bin/cargo-fmt >> "${T}/provider-${P}"
+	fi
+
+	insinto /etc/env.d/rust
+	doins "${T}/provider-${P}"
+}
+
+pkg_postinst() {
+	eselect rust update
+
+	if has_version sys-devel/gdb || has_version dev-util/lldb; then
+		elog "Rust installs a helper script for calling GDB and LLDB,"
+		elog "for your convenience it is installed under /usr/bin/rust-{gdb,lldb}-${PV}."
+	fi
+
+	if has_version app-editors/emacs; then
+		elog "install app-emacs/rust-mode to get emacs support for rust."
+	fi
+
+	if has_version app-editors/gvim || has_version app-editors/vim; then
+		elog "install app-vim/rust-vim to get vim support for rust."
+	fi
+}
+
+pkg_postrm() {
+	eselect rust cleanup
+}