Gentoo Archives: gentoo-commits

From: Georgy Yakovlev <gyakovlev@g.o>
To: gentoo-commits@l.g.o
Subject: [gentoo-commits] repo/gentoo:master commit in: dev-lang/rust/files/, dev-lang/rust/
Date: Sun, 18 Apr 2021 01:24:12
Message-Id: 1618709004.65b9e4c1a1c3a2de55637c7977584c5827b66366.gyakovlev@gentoo
1 commit: 65b9e4c1a1c3a2de55637c7977584c5827b66366
2 Author: Georgy Yakovlev <gyakovlev <AT> gentoo <DOT> org>
3 AuthorDate: Sun Apr 18 01:23:09 2021 +0000
4 Commit: Georgy Yakovlev <gyakovlev <AT> gentoo <DOT> org>
5 CommitDate: Sun Apr 18 01:23:24 2021 +0000
6 URL: https://gitweb.gentoo.org/repo/gentoo.git/commit/?id=65b9e4c1
7
8 dev-lang/rust: security revbump of 1.51.0
9
10 Fixes for:
11 CVE-2020-36323
12 CVE-2021-28876
13 CVE-2021-31162
14
15 Bug: https://bugs.gentoo.org/782799
16 Bug: https://bugs.gentoo.org/782367
17 Package-Manager: Portage-3.0.18, Repoman-3.0.3
18 Signed-off-by: Georgy Yakovlev <gyakovlev <AT> gentoo.org>
19
20 dev-lang/rust/files/1.51.0-CVE-2020-36323.patch | 175 +++++++
21 dev-lang/rust/files/1.51.0-CVE-2021-28876.patch | 39 ++
22 dev-lang/rust/files/1.51.0-CVE-2021-28878.patch | 112 +++++
23 dev-lang/rust/files/1.51.0-CVE-2021-28879.patch | 84 ++++
24 dev-lang/rust/files/1.51.0-CVE-2021-31162.patch | 195 ++++++++
25 dev-lang/rust/rust-1.51.0-r1.ebuild | 622 ++++++++++++++++++++++++
26 6 files changed, 1227 insertions(+)
27
28 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
29 new file mode 100644
30 index 00000000000..b4f2215cc23
31 --- /dev/null
32 +++ b/dev-lang/rust/files/1.51.0-CVE-2020-36323.patch
33 @@ -0,0 +1,175 @@
34 +From 6d43225bfb08ec91f7476b76c7fec632c4a096ef Mon Sep 17 00:00:00 2001
35 +From: Yechan Bae <yechan@××××××.edu>
36 +Date: Wed, 3 Feb 2021 16:36:33 -0500
37 +Subject: [PATCH 1/2] Fixes #80335
38 +
39 +---
40 + library/alloc/src/str.rs | 42 ++++++++++++++++++++++----------------
41 + library/alloc/tests/str.rs | 30 +++++++++++++++++++++++++++
42 + 2 files changed, 54 insertions(+), 18 deletions(-)
43 +
44 +diff --git a/library/alloc/src/str.rs b/library/alloc/src/str.rs
45 +index 70e0c7dba5eab..a7584c6b65100 100644
46 +--- a/library/alloc/src/str.rs
47 ++++ b/library/alloc/src/str.rs
48 +@@ -90,8 +90,8 @@ impl<S: Borrow<str>> Join<&str> for [S] {
49 + }
50 + }
51 +
52 +-macro_rules! spezialize_for_lengths {
53 +- ($separator:expr, $target:expr, $iter:expr; $($num:expr),*) => {
54 ++macro_rules! specialize_for_lengths {
55 ++ ($separator:expr, $target:expr, $iter:expr; $($num:expr),*) => {{
56 + let mut target = $target;
57 + let iter = $iter;
58 + let sep_bytes = $separator;
59 +@@ -102,7 +102,8 @@ macro_rules! spezialize_for_lengths {
60 + $num => {
61 + for s in iter {
62 + copy_slice_and_advance!(target, sep_bytes);
63 +- copy_slice_and_advance!(target, s.borrow().as_ref());
64 ++ let content_bytes = s.borrow().as_ref();
65 ++ copy_slice_and_advance!(target, content_bytes);
66 + }
67 + },
68 + )*
69 +@@ -110,11 +111,13 @@ macro_rules! spezialize_for_lengths {
70 + // arbitrary non-zero size fallback
71 + for s in iter {
72 + copy_slice_and_advance!(target, sep_bytes);
73 +- copy_slice_and_advance!(target, s.borrow().as_ref());
74 ++ let content_bytes = s.borrow().as_ref();
75 ++ copy_slice_and_advance!(target, content_bytes);
76 + }
77 + }
78 + }
79 +- };
80 ++ target
81 ++ }}
82 + }
83 +
84 + macro_rules! copy_slice_and_advance {
85 +@@ -153,7 +156,7 @@ where
86 + // if the `len` calculation overflows, we'll panic
87 + // we would have run out of memory anyway and the rest of the function requires
88 + // the entire Vec pre-allocated for safety
89 +- let len = sep_len
90 ++ let reserved_len = sep_len
91 + .checked_mul(iter.len())
92 + .and_then(|n| {
93 + slice.iter().map(|s| s.borrow().as_ref().len()).try_fold(n, usize::checked_add)
94 +@@ -161,22 +164,25 @@ where
95 + .expect("attempt to join into collection with len > usize::MAX");
96 +
97 + // crucial for safety
98 +- let mut result = Vec::with_capacity(len);
99 +- assert!(result.capacity() >= len);
100 ++ let mut result = Vec::with_capacity(reserved_len);
101 ++ debug_assert!(result.capacity() >= reserved_len);
102 +
103 + result.extend_from_slice(first.borrow().as_ref());
104 +
105 + unsafe {
106 +- {
107 +- let pos = result.len();
108 +- let target = result.get_unchecked_mut(pos..len);
109 +-
110 +- // copy separator and slices over without bounds checks
111 +- // generate loops with hardcoded offsets for small separators
112 +- // massive improvements possible (~ x2)
113 +- spezialize_for_lengths!(sep, target, iter; 0, 1, 2, 3, 4);
114 +- }
115 +- result.set_len(len);
116 ++ let pos = result.len();
117 ++ let target = result.get_unchecked_mut(pos..reserved_len);
118 ++
119 ++ // copy separator and slices over without bounds checks
120 ++ // generate loops with hardcoded offsets for small separators
121 ++ // massive improvements possible (~ x2)
122 ++ let remain = specialize_for_lengths!(sep, target, iter; 0, 1, 2, 3, 4);
123 ++
124 ++ // issue #80335: A weird borrow implementation can return different
125 ++ // slices for the length calculation and the actual copy, so
126 ++ // `remain.len()` might be non-zero.
127 ++ let result_len = reserved_len - remain.len();
128 ++ result.set_len(result_len);
129 + }
130 + result
131 + }
132 +diff --git a/library/alloc/tests/str.rs b/library/alloc/tests/str.rs
133 +index 604835e6cc4a6..6df8d8c2f354f 100644
134 +--- a/library/alloc/tests/str.rs
135 ++++ b/library/alloc/tests/str.rs
136 +@@ -160,6 +160,36 @@ fn test_join_for_different_lengths_with_long_separator() {
137 + test_join!("~~~~~a~~~~~bc", ["", "a", "bc"], "~~~~~");
138 + }
139 +
140 ++#[test]
141 ++fn test_join_isue_80335() {
142 ++ use core::{borrow::Borrow, cell::Cell};
143 ++
144 ++ struct WeirdBorrow {
145 ++ state: Cell<bool>,
146 ++ }
147 ++
148 ++ impl Default for WeirdBorrow {
149 ++ fn default() -> Self {
150 ++ WeirdBorrow { state: Cell::new(false) }
151 ++ }
152 ++ }
153 ++
154 ++ impl Borrow<str> for WeirdBorrow {
155 ++ fn borrow(&self) -> &str {
156 ++ let state = self.state.get();
157 ++ if state {
158 ++ "0"
159 ++ } else {
160 ++ self.state.set(true);
161 ++ "123456"
162 ++ }
163 ++ }
164 ++ }
165 ++
166 ++ let arr: [WeirdBorrow; 3] = Default::default();
167 ++ test_join!("0-0-0", arr, "-");
168 ++}
169 ++
170 + #[test]
171 + #[cfg_attr(miri, ignore)] // Miri is too slow
172 + fn test_unsafe_slice() {
173 +
174 +From 26a62701e42d10c03ce5f2f911e7d5edeefa2f0f Mon Sep 17 00:00:00 2001
175 +From: Yechan Bae <yechan@××××××.edu>
176 +Date: Sat, 20 Mar 2021 13:42:54 -0400
177 +Subject: [PATCH 2/2] Update the comment
178 +
179 +---
180 + library/alloc/src/str.rs | 8 ++++----
181 + 1 file changed, 4 insertions(+), 4 deletions(-)
182 +
183 +diff --git a/library/alloc/src/str.rs b/library/alloc/src/str.rs
184 +index a7584c6b65100..4d1e876457b8e 100644
185 +--- a/library/alloc/src/str.rs
186 ++++ b/library/alloc/src/str.rs
187 +@@ -163,7 +163,7 @@ where
188 + })
189 + .expect("attempt to join into collection with len > usize::MAX");
190 +
191 +- // crucial for safety
192 ++ // prepare an uninitialized buffer
193 + let mut result = Vec::with_capacity(reserved_len);
194 + debug_assert!(result.capacity() >= reserved_len);
195 +
196 +@@ -178,9 +178,9 @@ where
197 + // massive improvements possible (~ x2)
198 + let remain = specialize_for_lengths!(sep, target, iter; 0, 1, 2, 3, 4);
199 +
200 +- // issue #80335: A weird borrow implementation can return different
201 +- // slices for the length calculation and the actual copy, so
202 +- // `remain.len()` might be non-zero.
203 ++ // A weird borrow implementation may return different
204 ++ // slices for the length calculation and the actual copy.
205 ++ // Make sure we don't expose uninitialized bytes to the caller.
206 + let result_len = reserved_len - remain.len();
207 + result.set_len(result_len);
208 + }
209
210 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
211 new file mode 100644
212 index 00000000000..3a0af024143
213 --- /dev/null
214 +++ b/dev-lang/rust/files/1.51.0-CVE-2021-28876.patch
215 @@ -0,0 +1,39 @@
216 +From 86a4b27475aab52b998c15f5758540697cc9cff0 Mon Sep 17 00:00:00 2001
217 +From: =?UTF-8?q?Sebastian=20Dr=C3=B6ge?= <sebastian@×××××××××××.com>
218 +Date: Thu, 4 Feb 2021 10:23:01 +0200
219 +Subject: [PATCH] Increment `self.index` before calling
220 + `Iterator::self.a.__iterator_get_unchecked` in `Zip` `TrustedRandomAccess`
221 + specialization
222 +
223 +Otherwise if `Iterator::self.a.__iterator_get_unchecked` panics the
224 +index would not have been incremented yet and another call to
225 +`Iterator::next` would read from the same index again, which is not
226 +allowed according to the API contract of `TrustedRandomAccess` for
227 +`!Clone`.
228 +
229 +Fixes https://github.com/rust-lang/rust/issues/81740
230 +---
231 + library/core/src/iter/adapters/zip.rs | 7 ++++---
232 + 1 file changed, 4 insertions(+), 3 deletions(-)
233 +
234 +diff --git a/library/core/src/iter/adapters/zip.rs b/library/core/src/iter/adapters/zip.rs
235 +index 98b8dca961407..9f98353452006 100644
236 +--- a/library/core/src/iter/adapters/zip.rs
237 ++++ b/library/core/src/iter/adapters/zip.rs
238 +@@ -198,12 +198,13 @@ where
239 + Some((self.a.__iterator_get_unchecked(i), self.b.__iterator_get_unchecked(i)))
240 + }
241 + } else if A::may_have_side_effect() && self.index < self.a.size() {
242 ++ let i = self.index;
243 ++ self.index += 1;
244 + // match the base implementation's potential side effects
245 +- // SAFETY: we just checked that `self.index` < `self.a.len()`
246 ++ // SAFETY: we just checked that `i` < `self.a.len()`
247 + unsafe {
248 +- self.a.__iterator_get_unchecked(self.index);
249 ++ self.a.__iterator_get_unchecked(i);
250 + }
251 +- self.index += 1;
252 + None
253 + } else {
254 + None
255
256 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
257 new file mode 100644
258 index 00000000000..f319ab5e8c4
259 --- /dev/null
260 +++ b/dev-lang/rust/files/1.51.0-CVE-2021-28878.patch
261 @@ -0,0 +1,112 @@
262 +From 2371914a05f8f2763dffe6e2511d0870bcd6b461 Mon Sep 17 00:00:00 2001
263 +From: Giacomo Stevanato <giaco.stevanato@×××××.com>
264 +Date: Wed, 3 Mar 2021 21:09:01 +0100
265 +Subject: [PATCH 1/2] Prevent Zip specialization from calling
266 + __iterator_get_unchecked twice with the same index after calling next_back
267 +
268 +---
269 + library/core/src/iter/adapters/zip.rs | 13 +++++++++----
270 + 1 file changed, 9 insertions(+), 4 deletions(-)
271 +
272 +diff --git a/library/core/src/iter/adapters/zip.rs b/library/core/src/iter/adapters/zip.rs
273 +index 817fc2a51e981..ea7a809c6badb 100644
274 +--- a/library/core/src/iter/adapters/zip.rs
275 ++++ b/library/core/src/iter/adapters/zip.rs
276 +@@ -13,9 +13,10 @@ use crate::iter::{InPlaceIterable, SourceIter, TrustedLen};
277 + pub struct Zip<A, B> {
278 + a: A,
279 + b: B,
280 +- // index and len are only used by the specialized version of zip
281 ++ // index, len and a_len are only used by the specialized version of zip
282 + index: usize,
283 + len: usize,
284 ++ a_len: usize,
285 + }
286 + impl<A: Iterator, B: Iterator> Zip<A, B> {
287 + pub(in crate::iter) fn new(a: A, b: B) -> Zip<A, B> {
288 +@@ -110,6 +111,7 @@ where
289 + b,
290 + index: 0, // unused
291 + len: 0, // unused
292 ++ a_len: 0, // unused
293 + }
294 + }
295 +
296 +@@ -184,8 +186,9 @@ where
297 + B: TrustedRandomAccess + Iterator,
298 + {
299 + fn new(a: A, b: B) -> Self {
300 +- let len = cmp::min(a.size(), b.size());
301 +- Zip { a, b, index: 0, len }
302 ++ let a_len = a.size();
303 ++ let len = cmp::min(a_len, b.size());
304 ++ Zip { a, b, index: 0, len, a_len }
305 + }
306 +
307 + #[inline]
308 +@@ -197,7 +200,7 @@ where
309 + unsafe {
310 + Some((self.a.__iterator_get_unchecked(i), self.b.__iterator_get_unchecked(i)))
311 + }
312 +- } else if A::MAY_HAVE_SIDE_EFFECT && self.index < self.a.size() {
313 ++ } else if A::MAY_HAVE_SIDE_EFFECT && self.index < self.a_len {
314 + let i = self.index;
315 + self.index += 1;
316 + self.len += 1;
317 +@@ -262,6 +265,7 @@ where
318 + for _ in 0..sz_a - self.len {
319 + self.a.next_back();
320 + }
321 ++ self.a_len = self.len;
322 + }
323 + let sz_b = self.b.size();
324 + if B::MAY_HAVE_SIDE_EFFECT && sz_b > self.len {
325 +@@ -273,6 +277,7 @@ where
326 + }
327 + if self.index < self.len {
328 + self.len -= 1;
329 ++ self.a_len -= 1;
330 + let i = self.len;
331 + // SAFETY: `i` is smaller than the previous value of `self.len`,
332 + // which is also smaller than or equal to `self.a.len()` and `self.b.len()`
333 +
334 +From c1bfb9a78db6d481be1d03355672712c766e20b0 Mon Sep 17 00:00:00 2001
335 +From: Giacomo Stevanato <giaco.stevanato@×××××.com>
336 +Date: Fri, 19 Feb 2021 15:25:09 +0100
337 +Subject: [PATCH 2/2] Add relevant test
338 +
339 +---
340 + library/core/tests/iter/adapters/zip.rs | 23 +++++++++++++++++++++++
341 + 1 file changed, 23 insertions(+)
342 +
343 +diff --git a/library/core/tests/iter/adapters/zip.rs b/library/core/tests/iter/adapters/zip.rs
344 +index a597710392952..000c15f72c886 100644
345 +--- a/library/core/tests/iter/adapters/zip.rs
346 ++++ b/library/core/tests/iter/adapters/zip.rs
347 +@@ -265,3 +265,26 @@ fn test_issue_82282() {
348 + panic!();
349 + }
350 + }
351 ++
352 ++#[test]
353 ++fn test_issue_82291() {
354 ++ use std::cell::Cell;
355 ++
356 ++ let mut v1 = [()];
357 ++ let v2 = [()];
358 ++
359 ++ let called = Cell::new(0);
360 ++
361 ++ let mut zip = v1
362 ++ .iter_mut()
363 ++ .map(|r| {
364 ++ called.set(called.get() + 1);
365 ++ r
366 ++ })
367 ++ .zip(&v2);
368 ++
369 ++ zip.next_back();
370 ++ assert_eq!(called.get(), 1);
371 ++ zip.next();
372 ++ assert_eq!(called.get(), 1);
373 ++}
374
375 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
376 new file mode 100644
377 index 00000000000..3407a2ddf2f
378 --- /dev/null
379 +++ b/dev-lang/rust/files/1.51.0-CVE-2021-28879.patch
380 @@ -0,0 +1,84 @@
381 +From 66a260617a88ed1ad55a46f03c5a90d5ad3004d3 Mon Sep 17 00:00:00 2001
382 +From: Giacomo Stevanato <giaco.stevanato@×××××.com>
383 +Date: Fri, 19 Feb 2021 12:15:37 +0100
384 +Subject: [PATCH 1/3] Increment self.len in specialized ZipImpl to avoid
385 + underflow in size_hint
386 +
387 +---
388 + library/core/src/iter/adapters/zip.rs | 1 +
389 + 1 file changed, 1 insertion(+)
390 +
391 +diff --git a/library/core/src/iter/adapters/zip.rs b/library/core/src/iter/adapters/zip.rs
392 +index 9d0f4e3618fc5..ce48016afcd50 100644
393 +--- a/library/core/src/iter/adapters/zip.rs
394 ++++ b/library/core/src/iter/adapters/zip.rs
395 +@@ -200,6 +200,7 @@ where
396 + } else if A::MAY_HAVE_SIDE_EFFECT && self.index < self.a.size() {
397 + let i = self.index;
398 + self.index += 1;
399 ++ self.len += 1;
400 + // match the base implementation's potential side effects
401 + // SAFETY: we just checked that `i` < `self.a.len()`
402 + unsafe {
403 +
404 +From 8b9ac4d4155c74db5b317046033ab9c05a09e351 Mon Sep 17 00:00:00 2001
405 +From: Giacomo Stevanato <giaco.stevanato@×××××.com>
406 +Date: Fri, 19 Feb 2021 12:16:12 +0100
407 +Subject: [PATCH 2/3] Add test for underflow in specialized Zip's size_hint
408 +
409 +---
410 + library/core/tests/iter/adapters/zip.rs | 20 ++++++++++++++++++++
411 + 1 file changed, 20 insertions(+)
412 +
413 +diff --git a/library/core/tests/iter/adapters/zip.rs b/library/core/tests/iter/adapters/zip.rs
414 +index 1fce0951e365e..a597710392952 100644
415 +--- a/library/core/tests/iter/adapters/zip.rs
416 ++++ b/library/core/tests/iter/adapters/zip.rs
417 +@@ -245,3 +245,23 @@ fn test_double_ended_zip() {
418 + assert_eq!(it.next_back(), Some((3, 3)));
419 + assert_eq!(it.next(), None);
420 + }
421 ++
422 ++#[test]
423 ++fn test_issue_82282() {
424 ++ fn overflowed_zip(arr: &[i32]) -> impl Iterator<Item = (i32, &())> {
425 ++ static UNIT_EMPTY_ARR: [(); 0] = [];
426 ++
427 ++ let mapped = arr.into_iter().map(|i| *i);
428 ++ let mut zipped = mapped.zip(UNIT_EMPTY_ARR.iter());
429 ++ zipped.next();
430 ++ zipped
431 ++ }
432 ++
433 ++ let arr = [1, 2, 3];
434 ++ let zip = overflowed_zip(&arr).zip(overflowed_zip(&arr));
435 ++
436 ++ assert_eq!(zip.size_hint(), (0, Some(0)));
437 ++ for _ in zip {
438 ++ panic!();
439 ++ }
440 ++}
441 +
442 +From aeb4ea739efb70e0002a4a9c4c7b8027dd0620b3 Mon Sep 17 00:00:00 2001
443 +From: Giacomo Stevanato <giaco.stevanato@×××××.com>
444 +Date: Fri, 19 Feb 2021 12:17:48 +0100
445 +Subject: [PATCH 3/3] Remove useless comparison since now self.index <=
446 + self.len is an invariant
447 +
448 +---
449 + library/core/src/iter/adapters/zip.rs | 2 +-
450 + 1 file changed, 1 insertion(+), 1 deletion(-)
451 +
452 +diff --git a/library/core/src/iter/adapters/zip.rs b/library/core/src/iter/adapters/zip.rs
453 +index ce48016afcd50..817fc2a51e981 100644
454 +--- a/library/core/src/iter/adapters/zip.rs
455 ++++ b/library/core/src/iter/adapters/zip.rs
456 +@@ -259,7 +259,7 @@ where
457 + if sz_a != sz_b {
458 + let sz_a = self.a.size();
459 + if A::MAY_HAVE_SIDE_EFFECT && sz_a > self.len {
460 +- for _ in 0..sz_a - cmp::max(self.len, self.index) {
461 ++ for _ in 0..sz_a - self.len {
462 + self.a.next_back();
463 + }
464 + }
465
466 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
467 new file mode 100644
468 index 00000000000..fd9165ea3c5
469 --- /dev/null
470 +++ b/dev-lang/rust/files/1.51.0-CVE-2021-31162.patch
471 @@ -0,0 +1,195 @@
472 +From fa89c0fbcfa8f4d44f153b1195ec5a305540ffc4 Mon Sep 17 00:00:00 2001
473 +From: The8472 <git@×××××××××××××××.de>
474 +Date: Mon, 29 Mar 2021 04:22:34 +0200
475 +Subject: [PATCH 1/3] add testcase for double-drop during Vec in-place
476 + collection
477 +
478 +---
479 + library/alloc/tests/vec.rs | 38 +++++++++++++++++++++++++++++++++++++-
480 + 1 file changed, 37 insertions(+), 1 deletion(-)
481 +
482 +diff --git a/library/alloc/tests/vec.rs b/library/alloc/tests/vec.rs
483 +index c142536cd2dfb..b926c697d58ab 100644
484 +--- a/library/alloc/tests/vec.rs
485 ++++ b/library/alloc/tests/vec.rs
486 +@@ -1027,7 +1027,7 @@ fn test_from_iter_specialization_head_tail_drop() {
487 + }
488 +
489 + #[test]
490 +-fn test_from_iter_specialization_panic_drop() {
491 ++fn test_from_iter_specialization_panic_during_iteration_drops() {
492 + let drop_count: Vec<_> = (0..=2).map(|_| Rc::new(())).collect();
493 + let src: Vec<_> = drop_count.iter().cloned().collect();
494 + let iter = src.into_iter();
495 +@@ -1050,6 +1050,42 @@ fn test_from_iter_specialization_panic_drop() {
496 + );
497 + }
498 +
499 ++#[test]
500 ++fn test_from_iter_specialization_panic_during_drop_leaks() {
501 ++ static mut DROP_COUNTER: usize = 0;
502 ++
503 ++ #[derive(Debug)]
504 ++ enum Droppable {
505 ++ DroppedTwice(Box<i32>),
506 ++ PanicOnDrop,
507 ++ }
508 ++
509 ++ impl Drop for Droppable {
510 ++ fn drop(&mut self) {
511 ++ match self {
512 ++ Droppable::DroppedTwice(_) => {
513 ++ unsafe {
514 ++ DROP_COUNTER += 1;
515 ++ }
516 ++ println!("Dropping!")
517 ++ }
518 ++ Droppable::PanicOnDrop => {
519 ++ if !std::thread::panicking() {
520 ++ panic!();
521 ++ }
522 ++ }
523 ++ }
524 ++ }
525 ++ }
526 ++
527 ++ let _ = std::panic::catch_unwind(AssertUnwindSafe(|| {
528 ++ let v = vec![Droppable::DroppedTwice(Box::new(123)), Droppable::PanicOnDrop];
529 ++ let _ = v.into_iter().take(0).collect::<Vec<_>>();
530 ++ }));
531 ++
532 ++ assert_eq!(unsafe { DROP_COUNTER }, 1);
533 ++}
534 ++
535 + #[test]
536 + fn test_cow_from() {
537 + let borrowed: &[_] = &["borrowed", "(slice)"];
538 +
539 +From 421f5d282a51e130d3ca7c4524d8ad6753437da9 Mon Sep 17 00:00:00 2001
540 +From: The8472 <git@×××××××××××××××.de>
541 +Date: Mon, 29 Mar 2021 04:22:48 +0200
542 +Subject: [PATCH 2/3] fix double-drop in in-place collect specialization
543 +
544 +---
545 + library/alloc/src/vec/into_iter.rs | 27 ++++++++++++++-------
546 + library/alloc/src/vec/source_iter_marker.rs | 4 +--
547 + 2 files changed, 20 insertions(+), 11 deletions(-)
548 +
549 +diff --git a/library/alloc/src/vec/into_iter.rs b/library/alloc/src/vec/into_iter.rs
550 +index bcbdffabc7fbe..324e894bafd23 100644
551 +--- a/library/alloc/src/vec/into_iter.rs
552 ++++ b/library/alloc/src/vec/into_iter.rs
553 +@@ -85,20 +85,29 @@ impl<T, A: Allocator> IntoIter<T, A> {
554 + ptr::slice_from_raw_parts_mut(self.ptr as *mut T, self.len())
555 + }
556 +
557 +- pub(super) fn drop_remaining(&mut self) {
558 +- unsafe {
559 +- ptr::drop_in_place(self.as_mut_slice());
560 +- }
561 +- self.ptr = self.end;
562 +- }
563 ++ /// Drops remaining elements and relinquishes the backing allocation.
564 ++ ///
565 ++ /// This is roughly equivalent to the following, but more efficient
566 ++ ///
567 ++ /// ```
568 ++ /// # let mut into_iter = Vec::<u8>::with_capacity(10).into_iter();
569 ++ /// (&mut into_iter).for_each(core::mem::drop);
570 ++ /// unsafe { core::ptr::write(&mut into_iter, Vec::new().into_iter()); }
571 ++ /// ```
572 ++ pub(super) fn forget_allocation_drop_remaining(&mut self) {
573 ++ let remaining = self.as_raw_mut_slice();
574 +
575 +- /// Relinquishes the backing allocation, equivalent to
576 +- /// `ptr::write(&mut self, Vec::new().into_iter())`
577 +- pub(super) fn forget_allocation(&mut self) {
578 ++ // overwrite the individual fields instead of creating a new
579 ++ // struct and then overwriting &mut self.
580 ++ // this creates less assembly
581 + self.cap = 0;
582 + self.buf = unsafe { NonNull::new_unchecked(RawVec::NEW.ptr()) };
583 + self.ptr = self.buf.as_ptr();
584 + self.end = self.buf.as_ptr();
585 ++
586 ++ unsafe {
587 ++ ptr::drop_in_place(remaining);
588 ++ }
589 + }
590 + }
591 +
592 +diff --git a/library/alloc/src/vec/source_iter_marker.rs b/library/alloc/src/vec/source_iter_marker.rs
593 +index 50882fc17673e..e857d284d3ab6 100644
594 +--- a/library/alloc/src/vec/source_iter_marker.rs
595 ++++ b/library/alloc/src/vec/source_iter_marker.rs
596 +@@ -69,9 +69,9 @@ where
597 + }
598 +
599 + // drop any remaining values at the tail of the source
600 +- src.drop_remaining();
601 + // but prevent drop of the allocation itself once IntoIter goes out of scope
602 +- src.forget_allocation();
603 ++ // if the drop panics then we also leak any elements collected into dst_buf
604 ++ src.forget_allocation_drop_remaining();
605 +
606 + let vec = unsafe { Vec::from_raw_parts(dst_buf, len, cap) };
607 +
608 +
609 +From 328a5e040780984c60dde2db300dad4f1323c39d Mon Sep 17 00:00:00 2001
610 +From: The8472 <git@×××××××××××××××.de>
611 +Date: Fri, 2 Apr 2021 23:06:05 +0200
612 +Subject: [PATCH 3/3] cleanup leak after test to make miri happy
613 +
614 +---
615 + library/alloc/tests/vec.rs | 16 +++++++++++++---
616 + 1 file changed, 13 insertions(+), 3 deletions(-)
617 +
618 +diff --git a/library/alloc/tests/vec.rs b/library/alloc/tests/vec.rs
619 +index b926c697d58ab..b9fe07c73e55e 100644
620 +--- a/library/alloc/tests/vec.rs
621 ++++ b/library/alloc/tests/vec.rs
622 +@@ -1,3 +1,4 @@
623 ++use alloc::boxed::Box;
624 + use std::borrow::Cow;
625 + use std::cell::Cell;
626 + use std::collections::TryReserveError::*;
627 +@@ -1056,14 +1057,14 @@ fn test_from_iter_specialization_panic_during_drop_leaks() {
628 +
629 + #[derive(Debug)]
630 + enum Droppable {
631 +- DroppedTwice(Box<i32>),
632 ++ DroppedTwice,
633 + PanicOnDrop,
634 + }
635 +
636 + impl Drop for Droppable {
637 + fn drop(&mut self) {
638 + match self {
639 +- Droppable::DroppedTwice(_) => {
640 ++ Droppable::DroppedTwice => {
641 + unsafe {
642 + DROP_COUNTER += 1;
643 + }
644 +@@ -1078,12 +1079,21 @@ fn test_from_iter_specialization_panic_during_drop_leaks() {
645 + }
646 + }
647 +
648 ++ let mut to_free: *mut Droppable = core::ptr::null_mut();
649 ++ let mut cap = 0;
650 ++
651 + let _ = std::panic::catch_unwind(AssertUnwindSafe(|| {
652 +- let v = vec![Droppable::DroppedTwice(Box::new(123)), Droppable::PanicOnDrop];
653 ++ let mut v = vec![Droppable::DroppedTwice, Droppable::PanicOnDrop];
654 ++ to_free = v.as_mut_ptr();
655 ++ cap = v.capacity();
656 + let _ = v.into_iter().take(0).collect::<Vec<_>>();
657 + }));
658 +
659 + assert_eq!(unsafe { DROP_COUNTER }, 1);
660 ++ // clean up the leak to keep miri happy
661 ++ unsafe {
662 ++ Vec::from_raw_parts(to_free, 0, cap);
663 ++ }
664 + }
665 +
666 + #[test]
667
668 diff --git a/dev-lang/rust/rust-1.51.0-r1.ebuild b/dev-lang/rust/rust-1.51.0-r1.ebuild
669 new file mode 100644
670 index 00000000000..e2f25109d3e
671 --- /dev/null
672 +++ b/dev-lang/rust/rust-1.51.0-r1.ebuild
673 @@ -0,0 +1,622 @@
674 +# Copyright 1999-2021 Gentoo Authors
675 +# Distributed under the terms of the GNU General Public License v2
676 +
677 +EAPI=7
678 +
679 +PYTHON_COMPAT=( python3_{7..9} )
680 +
681 +inherit bash-completion-r1 check-reqs estack flag-o-matic llvm multiprocessing multilib-build python-any-r1 rust-toolchain toolchain-funcs
682 +
683 +if [[ ${PV} = *beta* ]]; then
684 + betaver=${PV//*beta}
685 + BETA_SNAPSHOT="${betaver:0:4}-${betaver:4:2}-${betaver:6:2}"
686 + MY_P="rustc-beta"
687 + SLOT="beta/${PV}"
688 + SRC="${BETA_SNAPSHOT}/rustc-beta-src.tar.xz -> rustc-${PV}-src.tar.xz"
689 +else
690 + ABI_VER="$(ver_cut 1-2)"
691 + SLOT="stable/${ABI_VER}"
692 + MY_P="rustc-${PV}"
693 + SRC="${MY_P}-src.tar.xz"
694 + KEYWORDS="~amd64 ~arm ~arm64 ~ppc64 ~x86"
695 +fi
696 +
697 +RUST_STAGE0_VERSION="1.$(($(ver_cut 2) - 1)).0"
698 +
699 +DESCRIPTION="Systems programming language from Mozilla"
700 +HOMEPAGE="https://www.rust-lang.org/"
701 +
702 +SRC_URI="
703 + https://static.rust-lang.org/dist/${SRC}
704 + !system-bootstrap? ( $(rust_all_arch_uris rust-${RUST_STAGE0_VERSION}) )
705 +"
706 +
707 +# keep in sync with llvm ebuild of the same version as bundled one.
708 +ALL_LLVM_TARGETS=( AArch64 AMDGPU ARM AVR BPF Hexagon Lanai Mips MSP430
709 + NVPTX PowerPC RISCV Sparc SystemZ WebAssembly X86 XCore )
710 +ALL_LLVM_TARGETS=( "${ALL_LLVM_TARGETS[@]/#/llvm_targets_}" )
711 +LLVM_TARGET_USEDEPS=${ALL_LLVM_TARGETS[@]/%/?}
712 +
713 +LICENSE="|| ( MIT Apache-2.0 ) BSD-1 BSD-2 BSD-4 UoI-NCSA"
714 +
715 +IUSE="clippy cpu_flags_x86_sse2 debug doc libressl miri nightly parallel-compiler rls rustfmt system-bootstrap system-llvm test wasm ${ALL_LLVM_TARGETS[*]}"
716 +
717 +# Please keep the LLVM dependency block separate. Since LLVM is slotted,
718 +# we need to *really* make sure we're not pulling more than one slot
719 +# simultaneously.
720 +
721 +# How to use it:
722 +# 1. List all the working slots (with min versions) in ||, newest first.
723 +# 2. Update the := to specify *max* version, e.g. < 12.
724 +# 3. Specify LLVM_MAX_SLOT, e.g. 11.
725 +LLVM_DEPEND="
726 + || (
727 + sys-devel/llvm:11[${LLVM_TARGET_USEDEPS// /,}]
728 + )
729 + <sys-devel/llvm-12:=
730 + wasm? ( sys-devel/lld )
731 +"
732 +LLVM_MAX_SLOT=11
733 +
734 +# to bootstrap we need at least exactly previous version, or same.
735 +# most of the time previous versions fail to bootstrap with newer
736 +# for example 1.47.x, requires at least 1.46.x, 1.47.x is ok,
737 +# but it fails to bootstrap with 1.48.x
738 +# https://github.com/rust-lang/rust/blob/${PV}/src/stage0.txt
739 +BOOTSTRAP_DEPEND="||
740 + (
741 + =dev-lang/rust-$(ver_cut 1).$(($(ver_cut 2) - 1))*
742 + =dev-lang/rust-bin-$(ver_cut 1).$(($(ver_cut 2) - 1))*
743 + =dev-lang/rust-$(ver_cut 1).$(ver_cut 2)*
744 + =dev-lang/rust-bin-$(ver_cut 1).$(ver_cut 2)*
745 + )
746 +"
747 +
748 +BDEPEND="${PYTHON_DEPS}
749 + app-eselect/eselect-rust
750 + || (
751 + >=sys-devel/gcc-4.7
752 + >=sys-devel/clang-3.5
753 + )
754 + system-bootstrap? ( ${BOOTSTRAP_DEPEND} )
755 + !system-llvm? (
756 + dev-util/cmake
757 + dev-util/ninja
758 + )
759 +"
760 +
761 +DEPEND="
762 + >=app-arch/xz-utils-5.2
763 + net-misc/curl:=[http2,ssl]
764 + sys-libs/zlib:=
765 + !libressl? ( dev-libs/openssl:0= )
766 + libressl? ( dev-libs/libressl:0= )
767 + elibc_musl? ( sys-libs/libunwind:= )
768 + system-llvm? (
769 + ${LLVM_DEPEND}
770 + )
771 +"
772 +
773 +# we need to block older versions due to layout changes.
774 +RDEPEND="${DEPEND}
775 + app-eselect/eselect-rust
776 + !<dev-lang/rust-1.47.0-r1
777 + !<dev-lang/rust-bin-1.47.0-r1
778 +"
779 +
780 +REQUIRED_USE="|| ( ${ALL_LLVM_TARGETS[*]} )
781 + miri? ( nightly )
782 + parallel-compiler? ( nightly )
783 + test? ( ${ALL_LLVM_TARGETS[*]} )
784 + wasm? ( llvm_targets_WebAssembly )
785 + x86? ( cpu_flags_x86_sse2 )
786 +"
787 +
788 +# we don't use cmake.eclass, but can get a warnings
789 +CMAKE_WARN_UNUSED_CLI=no
790 +
791 +QA_FLAGS_IGNORED="
792 + usr/lib/${PN}/${PV}/bin/.*
793 + usr/lib/${PN}/${PV}/libexec/.*
794 + usr/lib/${PN}/${PV}/lib/lib.*.so
795 + usr/lib/${PN}/${PV}/lib/rustlib/.*/bin/.*
796 + usr/lib/${PN}/${PV}/lib/rustlib/.*/lib/lib.*.so
797 +"
798 +
799 +QA_SONAME="
800 + usr/lib/${PN}/${PV}/lib/lib.*.so.*
801 + usr/lib/${PN}/${PV}/lib/rustlib/.*/lib/lib.*.so
802 +"
803 +
804 +# causes double bootstrap
805 +RESTRICT="test"
806 +
807 +PATCHES=(
808 + "${FILESDIR}"/1.47.0-libressl.patch
809 + "${FILESDIR}"/1.47.0-ignore-broken-and-non-applicable-tests.patch
810 + "${FILESDIR}"/1.49.0-gentoo-musl-target-specs.patch
811 + "${FILESDIR}"/1.51.0-bootstrap-panic.patch
812 + "${FILESDIR}"/1.51.0-CVE-2020-36323.patch
813 + "${FILESDIR}"/1.51.0-CVE-2021-28876.patch
814 + #"${FILESDIR}"/1.51.0-CVE-2021-28878.patch
815 + #"${FILESDIR}"/1.51.0-CVE-2021-28879.patch
816 + "${FILESDIR}"/1.51.0-CVE-2021-31162.patch
817 +)
818 +
819 +S="${WORKDIR}/${MY_P}-src"
820 +
821 +toml_usex() {
822 + usex "${1}" true false
823 +}
824 +
825 +boostrap_rust_version_check() {
826 + # never call from pkg_pretend. eselect-rust may be not installed yet.
827 + [[ ${MERGE_TYPE} == binary ]] && return
828 + local rustc_wanted="$(ver_cut 1).$(($(ver_cut 2) - 1))"
829 + local rustc_toonew="$(ver_cut 1).$(($(ver_cut 2) + 1))"
830 + local rustc_version=( $(eselect --brief rust show 2>/dev/null) )
831 + rustc_version=${rustc_version[0]#rust-bin-}
832 + rustc_version=${rustc_version#rust-}
833 +
834 + [[ -z "${rustc_version}" ]] && die "Failed to determine rust version, check 'eselect rust' output"
835 +
836 + if ver_test "${rustc_version}" -lt "${rustc_wanted}" ; then
837 + eerror "Rust >=${rustc_wanted} is required"
838 + eerror "please run 'eselect rust' and set correct rust version"
839 + die "selected rust version is too old"
840 + elif ver_test "${rustc_version}" -ge "${rustc_toonew}" ; then
841 + eerror "Rust <${rustc_toonew} is required"
842 + eerror "please run 'eselect rust' and set correct rust version"
843 + die "selected rust version is too new"
844 + else
845 + einfo "Using rust ${rustc_version} to build"
846 + fi
847 +}
848 +
849 +pre_build_checks() {
850 + local M=6144
851 + M=$(( $(usex clippy 128 0) + ${M} ))
852 + M=$(( $(usex miri 128 0) + ${M} ))
853 + M=$(( $(usex rls 512 0) + ${M} ))
854 + M=$(( $(usex rustfmt 256 0) + ${M} ))
855 + M=$(( $(usex system-llvm 0 2048) + ${M} ))
856 + M=$(( $(usex wasm 256 0) + ${M} ))
857 + M=$(( $(usex debug 15 10) * ${M} / 10 ))
858 + eshopts_push -s extglob
859 + if is-flagq '-g?(gdb)?([1-9])'; then
860 + M=$(( 15 * ${M} / 10 ))
861 + fi
862 + eshopts_pop
863 + M=$(( $(usex system-bootstrap 0 1024) + ${M} ))
864 + M=$(( $(usex doc 256 0) + ${M} ))
865 + CHECKREQS_DISK_BUILD=${M}M check-reqs_pkg_${EBUILD_PHASE}
866 +}
867 +
868 +pkg_pretend() {
869 + pre_build_checks
870 +}
871 +
872 +pkg_setup() {
873 + pre_build_checks
874 + python-any-r1_pkg_setup
875 +
876 + export LIBGIT2_NO_PKG_CONFIG=1 #749381
877 +
878 + use system-bootstrap && boostrap_rust_version_check
879 +
880 + if use system-llvm; then
881 + llvm_pkg_setup
882 +
883 + local llvm_config="$(get_llvm_prefix "$LLVM_MAX_SLOT")/bin/llvm-config"
884 + export LLVM_LINK_SHARED=1
885 + export RUSTFLAGS="${RUSTFLAGS} -Lnative=$("${llvm_config}" --libdir)"
886 + fi
887 +}
888 +
889 +src_prepare() {
890 + if ! use system-bootstrap; then
891 + local rust_stage0_root="${WORKDIR}"/rust-stage0
892 + local rust_stage0="rust-${RUST_STAGE0_VERSION}-$(rust_abi)"
893 +
894 + "${WORKDIR}/${rust_stage0}"/install.sh --disable-ldconfig \
895 + --destdir="${rust_stage0_root}" --prefix=/ || die
896 + fi
897 +
898 + default
899 +}
900 +
901 +src_configure() {
902 + local rust_target="" rust_targets="" arch_cflags
903 +
904 + # Collect rust target names to compile standard libs for all ABIs.
905 + for v in $(multilib_get_enabled_abi_pairs); do
906 + rust_targets="${rust_targets},\"$(rust_abi $(get_abi_CHOST ${v##*.}))\""
907 + done
908 + if use wasm; then
909 + rust_targets="${rust_targets},\"wasm32-unknown-unknown\""
910 + if use system-llvm; then
911 + # un-hardcode rust-lld linker for this target
912 + # https://bugs.gentoo.org/715348
913 + sed -i '/linker:/ s/rust-lld/wasm-ld/' compiler/rustc_target/src/spec/wasm32_base.rs || die
914 + fi
915 + fi
916 + rust_targets="${rust_targets#,}"
917 +
918 + local tools="\"cargo\","
919 + if use clippy; then
920 + tools="\"clippy\",$tools"
921 + fi
922 + if use miri; then
923 + tools="\"miri\",$tools"
924 + fi
925 + if use rls; then
926 + tools="\"rls\",\"analysis\",\"src\",$tools"
927 + fi
928 + if use rustfmt; then
929 + tools="\"rustfmt\",$tools"
930 + fi
931 +
932 + local rust_stage0_root
933 + if use system-bootstrap; then
934 + rust_stage0_root="$(rustc --print sysroot)"
935 + else
936 + rust_stage0_root="${WORKDIR}"/rust-stage0
937 + fi
938 +
939 + rust_target="$(rust_abi)"
940 +
941 + cat <<- _EOF_ > "${S}"/config.toml
942 + [llvm]
943 + download-ci-llvm = false
944 + optimize = $(toml_usex !debug)
945 + release-debuginfo = $(toml_usex debug)
946 + assertions = $(toml_usex debug)
947 + ninja = true
948 + targets = "${LLVM_TARGETS// /;}"
949 + experimental-targets = ""
950 + link-shared = $(toml_usex system-llvm)
951 + [build]
952 + build = "${rust_target}"
953 + host = ["${rust_target}"]
954 + target = [${rust_targets}]
955 + cargo = "${rust_stage0_root}/bin/cargo"
956 + rustc = "${rust_stage0_root}/bin/rustc"
957 + docs = $(toml_usex doc)
958 + compiler-docs = $(toml_usex doc)
959 + submodules = false
960 + python = "${EPYTHON}"
961 + locked-deps = true
962 + vendor = true
963 + extended = true
964 + tools = [${tools}]
965 + verbose = 2
966 + sanitizers = false
967 + profiler = false
968 + cargo-native-static = false
969 + [install]
970 + prefix = "${EPREFIX}/usr/lib/${PN}/${PV}"
971 + sysconfdir = "etc"
972 + docdir = "share/doc/rust"
973 + bindir = "bin"
974 + libdir = "lib"
975 + mandir = "share/man"
976 + [rust]
977 + # https://github.com/rust-lang/rust/issues/54872
978 + codegen-units-std = 1
979 + optimize = true
980 + debug = $(toml_usex debug)
981 + debug-assertions = $(toml_usex debug)
982 + debuginfo-level-rustc = 0
983 + backtrace = true
984 + incremental = false
985 + default-linker = "$(tc-getCC)"
986 + parallel-compiler = $(toml_usex parallel-compiler)
987 + channel = "$(usex nightly nightly stable)"
988 + description = "gentoo"
989 + rpath = false
990 + verbose-tests = true
991 + optimize-tests = $(toml_usex !debug)
992 + codegen-tests = true
993 + dist-src = false
994 + remap-debuginfo = true
995 + lld = $(usex system-llvm false $(toml_usex wasm))
996 + backtrace-on-ice = true
997 + jemalloc = false
998 + [dist]
999 + src-tarball = false
1000 + _EOF_
1001 +
1002 + for v in $(multilib_get_enabled_abi_pairs); do
1003 + rust_target=$(rust_abi $(get_abi_CHOST ${v##*.}))
1004 + arch_cflags="$(get_abi_CFLAGS ${v##*.})"
1005 +
1006 + cat <<- _EOF_ >> "${S}"/config.env
1007 + CFLAGS_${rust_target}=${arch_cflags}
1008 + _EOF_
1009 +
1010 + cat <<- _EOF_ >> "${S}"/config.toml
1011 + [target.${rust_target}]
1012 + cc = "$(tc-getBUILD_CC)"
1013 + cxx = "$(tc-getBUILD_CXX)"
1014 + linker = "$(tc-getCC)"
1015 + ar = "$(tc-getAR)"
1016 + _EOF_
1017 + # librustc_target/spec/linux_musl_base.rs sets base.crt_static_default = true;
1018 + if use elibc_musl; then
1019 + cat <<- _EOF_ >> "${S}"/config.toml
1020 + crt-static = false
1021 + _EOF_
1022 + fi
1023 + if use system-llvm; then
1024 + cat <<- _EOF_ >> "${S}"/config.toml
1025 + llvm-config = "$(get_llvm_prefix "${LLVM_MAX_SLOT}")/bin/llvm-config"
1026 + _EOF_
1027 + fi
1028 + done
1029 + if use wasm; then
1030 + cat <<- _EOF_ >> "${S}"/config.toml
1031 + [target.wasm32-unknown-unknown]
1032 + linker = "$(usex system-llvm lld rust-lld)"
1033 + _EOF_
1034 + fi
1035 +
1036 + if [[ -n ${I_KNOW_WHAT_I_AM_DOING_CROSS} ]]; then # whitespace intentionally shifted below
1037 + # experimental cross support
1038 + # discussion: https://bugs.gentoo.org/679878
1039 + # TODO: c*flags, clang, system-llvm, cargo.eclass target support
1040 + # it would be much better if we could split out stdlib
1041 + # complilation to separate ebuild and abuse CATEGORY to
1042 + # just install to /usr/lib/rustlib/<target>
1043 +
1044 + # extra targets defined as a bash array
1045 + # spec format: <LLVM target>:<rust-target>:<CTARGET>
1046 + # best place would be /etc/portage/env/dev-lang/rust
1047 + # Example:
1048 + # RUST_CROSS_TARGETS=(
1049 + # "AArch64:aarch64-unknown-linux-gnu:aarch64-unknown-linux-gnu"
1050 + # )
1051 + # no extra hand holding is done, no target transformations, all
1052 + # values are passed as-is with just basic checks, so it's up to user to supply correct values
1053 + # valid rust targets can be obtained with
1054 + # rustc --print target-list
1055 + # matching cross toolchain has to be installed
1056 + # matching LLVM_TARGET has to be enabled for both rust and llvm (if using system one)
1057 + # only gcc toolchains installed with crossdev are checked for now.
1058 +
1059 + # BUG: we can't pass host flags to cross compiler, so just filter for now
1060 + # BUG: this should be more fine-grained.
1061 + filter-flags '-mcpu=*' '-march=*' '-mtune=*'
1062 +
1063 + local cross_target_spec
1064 + for cross_target_spec in "${RUST_CROSS_TARGETS[@]}";do
1065 + # extracts first element form <LLVM target>:<rust-target>:<CTARGET>
1066 + local cross_llvm_target="${cross_target_spec%%:*}"
1067 + # extracts toolchain triples, <rust-target>:<CTARGET>
1068 + local cross_triples="${cross_target_spec#*:}"
1069 + # extracts first element after before : separator
1070 + local cross_rust_target="${cross_triples%%:*}"
1071 + # extracts last element after : separator
1072 + local cross_toolchain="${cross_triples##*:}"
1073 + use llvm_targets_${cross_llvm_target} || die "need llvm_targets_${cross_llvm_target} target enabled"
1074 + command -v ${cross_toolchain}-gcc > /dev/null 2>&1 || die "need ${cross_toolchain} cross toolchain"
1075 +
1076 + cat <<- _EOF_ >> "${S}"/config.toml
1077 + [target.${cross_rust_target}]
1078 + cc = "${cross_toolchain}-gcc"
1079 + cxx = "${cross_toolchain}-g++"
1080 + linker = "${cross_toolchain}-gcc"
1081 + ar = "${cross_toolchain}-ar"
1082 + _EOF_
1083 + if use system-llvm; then
1084 + cat <<- _EOF_ >> "${S}"/config.toml
1085 + llvm-config = "$(get_llvm_prefix "${LLVM_MAX_SLOT}")/bin/llvm-config"
1086 + _EOF_
1087 + fi
1088 +
1089 + # append cross target to "normal" target list
1090 + # example 'target = ["powerpc64le-unknown-linux-gnu"]'
1091 + # becomes 'target = ["powerpc64le-unknown-linux-gnu","aarch64-unknown-linux-gnu"]'
1092 +
1093 + rust_targets="${rust_targets},\"${cross_rust_target}\""
1094 + sed -i "/^target = \[/ s#\[.*\]#\[${rust_targets}\]#" config.toml || die
1095 +
1096 + ewarn
1097 + ewarn "Enabled ${cross_rust_target} rust target"
1098 + ewarn "Using ${cross_toolchain} cross toolchain"
1099 + ewarn
1100 + if ! has_version -b 'sys-devel/binutils[multitarget]' ; then
1101 + ewarn "'sys-devel/binutils[multitarget]' is not installed"
1102 + ewarn "'strip' will be unable to strip cross libraries"
1103 + ewarn "cross targets will be installed with full debug information"
1104 + ewarn "enable 'multitarget' USE flag for binutils to be able to strip object files"
1105 + ewarn
1106 + ewarn "Alternatively llvm-strip can be used, it supports stripping any target"
1107 + ewarn "define STRIP=\"llvm-strip\" to use it (experimental)"
1108 + ewarn
1109 + fi
1110 + done
1111 + fi # I_KNOW_WHAT_I_AM_DOING_CROSS
1112 +
1113 + einfo "Rust configured with the following settings:"
1114 + cat "${S}"/config.toml || die
1115 +}
1116 +
1117 +src_compile() {
1118 + # we need \n IFS to have config.env with spaces loaded properly. #734018
1119 + (
1120 + IFS=$'\n'
1121 + env $(cat "${S}"/config.env) RUST_BACKTRACE=1\
1122 + "${EPYTHON}" ./x.py dist -vv --config="${S}"/config.toml -j$(makeopts_jobs) || die
1123 + )
1124 +}
1125 +
1126 +src_test() {
1127 + # https://rustc-dev-guide.rust-lang.org/tests/intro.html
1128 +
1129 + # those are basic and codegen tests.
1130 + local tests=(
1131 + codegen
1132 + codegen-units
1133 + compile-fail
1134 + incremental
1135 + mir-opt
1136 + pretty
1137 + run-make
1138 + )
1139 +
1140 + # fails if llvm is not built with ALL targets.
1141 + # and known to fail with system llvm sometimes.
1142 + use system-llvm || tests+=( assembly )
1143 +
1144 + # fragile/expensive/less important tests
1145 + # or tests that require extra builds
1146 + # TODO: instead of skipping, just make some nonfatal.
1147 + if [[ ${ERUST_RUN_EXTRA_TESTS:-no} != no ]]; then
1148 + tests+=(
1149 + rustdoc
1150 + rustdoc-js
1151 + rustdoc-js-std
1152 + rustdoc-ui
1153 + run-make-fulldeps
1154 + ui
1155 + ui-fulldeps
1156 + )
1157 + fi
1158 +
1159 + local i failed=()
1160 + einfo "rust_src_test: enabled tests ${tests[@]/#/src/test/}"
1161 + for i in "${tests[@]}"; do
1162 + local t="src/test/${i}"
1163 + einfo "rust_src_test: running ${t}"
1164 + if ! (
1165 + IFS=$'\n'
1166 + env $(cat "${S}"/config.env) RUST_BACKTRACE=1 \
1167 + "${EPYTHON}" ./x.py test -vv --config="${S}"/config.toml \
1168 + -j$(makeopts_jobs) --no-doc --no-fail-fast "${t}"
1169 + )
1170 + then
1171 + failed+=( "${t}" )
1172 + eerror "rust_src_test: ${t} failed"
1173 + fi
1174 + done
1175 +
1176 + if [[ ${#failed[@]} -ne 0 ]]; then
1177 + eerror "rust_src_test: failure summary: ${failed[@]}"
1178 + die "aborting due to test failures"
1179 + fi
1180 +}
1181 +
1182 +src_install() {
1183 + (
1184 + IFS=$'\n'
1185 + env $(cat "${S}"/config.env) DESTDIR="${D}" \
1186 + "${EPYTHON}" ./x.py install -vv --config="${S}"/config.toml || die
1187 + )
1188 +
1189 + # bug #689562, #689160
1190 + rm -v "${ED}/usr/lib/${PN}/${PV}/etc/bash_completion.d/cargo" || die
1191 + rmdir -v "${ED}/usr/lib/${PN}/${PV}"/etc{/bash_completion.d,} || die
1192 + newbashcomp src/tools/cargo/src/etc/cargo.bashcomp.sh cargo
1193 +
1194 + local symlinks=(
1195 + cargo
1196 + rustc
1197 + rustdoc
1198 + rust-gdb
1199 + rust-gdbgui
1200 + rust-lldb
1201 + )
1202 +
1203 + use clippy && symlinks+=( clippy-driver cargo-clippy )
1204 + use miri && symlinks+=( miri cargo-miri )
1205 + use rls && symlinks+=( rls )
1206 + use rustfmt && symlinks+=( rustfmt cargo-fmt )
1207 +
1208 + einfo "installing eselect-rust symlinks and paths: ${symlinks[@]}"
1209 + local i
1210 + for i in "${symlinks[@]}"; do
1211 + # we need realpath on /usr/bin/* symlink return version-appended binary path.
1212 + # so /usr/bin/rustc should point to /usr/lib/rust/<ver>/bin/rustc-<ver>
1213 + # need to fix eselect-rust to remove this hack.
1214 + local ver_i="${i}-${PV}"
1215 + if [[ -f "${ED}/usr/lib/${PN}/${PV}/bin/${i}" ]]; then
1216 + einfo "Installing ${i} symlink"
1217 + ln -v "${ED}/usr/lib/${PN}/${PV}/bin/${i}" "${ED}/usr/lib/${PN}/${PV}/bin/${ver_i}" || die
1218 + else
1219 + ewarn "${i} symlink requested, but source file not found"
1220 + ewarn "please report this"
1221 + fi
1222 + dosym "../lib/${PN}/${PV}/bin/${ver_i}" "/usr/bin/${ver_i}"
1223 + done
1224 +
1225 + # symlinks to switch components to active rust in eselect
1226 + dosym "${PV}/lib" "/usr/lib/${PN}/lib-${PV}"
1227 + dosym "${PV}/libexec" "/usr/lib/${PN}/libexec-${PV}"
1228 + dosym "${PV}/share/man" "/usr/lib/${PN}/man-${PV}"
1229 + dosym "rust/${PV}/lib/rustlib" "/usr/lib/rustlib-${PV}"
1230 + dosym "../../lib/${PN}/${PV}/share/doc/rust" "/usr/share/doc/${P}"
1231 +
1232 + newenvd - "50${P}" <<-_EOF_
1233 + LDPATH="${EPREFIX}/usr/lib/rust/lib"
1234 + MANPATH="${EPREFIX}/usr/lib/rust/man"
1235 + $(use amd64 && usex elibc_musl 'CARGO_TARGET_X86_64_UNKNOWN_LINUX_MUSL_RUSTFLAGS="-C target-feature=-crt-static"' '')
1236 + $(use arm64 && usex elibc_musl 'CARGO_TARGET_AARCH64_UNKNOWN_LINUX_MUSL_RUSTFLAGS="-C target-feature=-crt-static"' '')
1237 + _EOF_
1238 +
1239 + rm -rf "${ED}/usr/lib/${PN}/${PV}"/*.old || die
1240 + rm -rf "${ED}/usr/lib/${PN}/${PV}/doc"/*.old || die
1241 +
1242 + # note: eselect-rust adds EROOT to all paths below
1243 + cat <<-_EOF_ > "${T}/provider-${P}"
1244 + /usr/bin/cargo
1245 + /usr/bin/rustdoc
1246 + /usr/bin/rust-gdb
1247 + /usr/bin/rust-gdbgui
1248 + /usr/bin/rust-lldb
1249 + /usr/lib/rustlib
1250 + /usr/lib/rust/lib
1251 + /usr/lib/rust/libexec
1252 + /usr/lib/rust/man
1253 + /usr/share/doc/rust
1254 + _EOF_
1255 +
1256 + if use clippy; then
1257 + echo /usr/bin/clippy-driver >> "${T}/provider-${P}"
1258 + echo /usr/bin/cargo-clippy >> "${T}/provider-${P}"
1259 + fi
1260 + if use miri; then
1261 + echo /usr/bin/miri >> "${T}/provider-${P}"
1262 + echo /usr/bin/cargo-miri >> "${T}/provider-${P}"
1263 + fi
1264 + if use rls; then
1265 + echo /usr/bin/rls >> "${T}/provider-${P}"
1266 + fi
1267 + if use rustfmt; then
1268 + echo /usr/bin/rustfmt >> "${T}/provider-${P}"
1269 + echo /usr/bin/cargo-fmt >> "${T}/provider-${P}"
1270 + fi
1271 +
1272 + insinto /etc/env.d/rust
1273 + doins "${T}/provider-${P}"
1274 +}
1275 +
1276 +pkg_postinst() {
1277 + eselect rust update
1278 +
1279 + if has_version sys-devel/gdb || has_version dev-util/lldb; then
1280 + elog "Rust installs a helper script for calling GDB and LLDB,"
1281 + elog "for your convenience it is installed under /usr/bin/rust-{gdb,lldb}-${PV}."
1282 + fi
1283 +
1284 + if has_version app-editors/emacs; then
1285 + elog "install app-emacs/rust-mode to get emacs support for rust."
1286 + fi
1287 +
1288 + if has_version app-editors/gvim || has_version app-editors/vim; then
1289 + elog "install app-vim/rust-vim to get vim support for rust."
1290 + fi
1291 +}
1292 +
1293 +pkg_postrm() {
1294 + eselect rust cleanup
1295 +}