-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of #92274 - woppopo:const_deallocate, r=oli-obk
Add `intrinsics::const_deallocate` Tracking issue: #79597 Related: #91884 This allows deallocation of a memory allocated by `intrinsics::const_allocate`. At the moment, this can be only used to reduce memory usage, but in the future this may be useful to detect memory leaks (If an allocated memory remains after evaluation, raise an error...?).
- Loading branch information
Showing
18 changed files
with
274 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
src/test/ui/consts/const-eval/heap/alloc_intrinsic_zero_sized.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// run-pass | ||
#![feature(core_intrinsics)] | ||
#![feature(const_heap)] | ||
#![feature(inline_const)] | ||
|
||
use std::intrinsics; | ||
|
||
struct ZST; | ||
|
||
fn main() { | ||
const { | ||
unsafe { | ||
let _ = intrinsics::const_allocate(0, 0) as *mut ZST; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// run-pass | ||
#![feature(core_intrinsics)] | ||
#![feature(const_heap)] | ||
#![feature(const_mut_refs)] | ||
|
||
use std::intrinsics; | ||
|
||
const _X: () = unsafe { | ||
let ptr = intrinsics::const_allocate(4, 4); | ||
intrinsics::const_deallocate(ptr, 4, 4); | ||
}; | ||
|
||
const Y: &u32 = unsafe { | ||
let ptr = intrinsics::const_allocate(4, 4) as *mut u32; | ||
*ptr = 42; | ||
&*ptr | ||
}; | ||
|
||
const Z: &u32 = &42; | ||
|
||
const _Z: () = unsafe { | ||
let ptr1 = Y as *const _ as *mut u8; | ||
intrinsics::const_deallocate(ptr1, 4, 4); // nop | ||
intrinsics::const_deallocate(ptr1, 2, 4); // nop | ||
intrinsics::const_deallocate(ptr1, 4, 2); // nop | ||
|
||
let ptr2 = Z as *const _ as *mut u8; | ||
intrinsics::const_deallocate(ptr2, 4, 4); // nop | ||
intrinsics::const_deallocate(ptr2, 2, 4); // nop | ||
intrinsics::const_deallocate(ptr2, 4, 2); // nop | ||
}; | ||
|
||
fn main() { | ||
assert_eq!(*Y, 42); | ||
assert_eq!(*Z, 42); | ||
} |
22 changes: 22 additions & 0 deletions
22
src/test/ui/consts/const-eval/heap/dealloc_intrinsic_dangling.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#![feature(core_intrinsics)] | ||
#![feature(const_heap)] | ||
#![feature(const_mut_refs)] | ||
|
||
use std::intrinsics; | ||
|
||
const _X: &'static u8 = unsafe { | ||
let ptr = intrinsics::const_allocate(4, 4); | ||
intrinsics::const_deallocate(ptr, 4, 4); | ||
&*ptr | ||
//~^ error: evaluation of constant value failed | ||
}; | ||
|
||
const _Y: u8 = unsafe { | ||
let ptr = intrinsics::const_allocate(4, 4); | ||
let reference = &*ptr; | ||
intrinsics::const_deallocate(ptr, 4, 4); | ||
*reference | ||
//~^ error: evaluation of constant value failed | ||
}; | ||
|
||
fn main() {} |
15 changes: 15 additions & 0 deletions
15
src/test/ui/consts/const-eval/heap/dealloc_intrinsic_dangling.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
error[E0080]: evaluation of constant value failed | ||
--> $DIR/dealloc_intrinsic_dangling.rs:10:5 | ||
| | ||
LL | &*ptr | ||
| ^^^^^ pointer to alloc2 was dereferenced after this allocation got freed | ||
|
||
error[E0080]: evaluation of constant value failed | ||
--> $DIR/dealloc_intrinsic_dangling.rs:18:5 | ||
| | ||
LL | *reference | ||
| ^^^^^^^^^^ pointer to alloc4 was dereferenced after this allocation got freed | ||
|
||
error: aborting due to 2 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0080`. |
13 changes: 13 additions & 0 deletions
13
src/test/ui/consts/const-eval/heap/dealloc_intrinsic_duplicate.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#![feature(core_intrinsics)] | ||
#![feature(const_heap)] | ||
|
||
use std::intrinsics; | ||
|
||
const _X: () = unsafe { | ||
let ptr = intrinsics::const_allocate(4, 4); | ||
intrinsics::const_deallocate(ptr, 4, 4); | ||
intrinsics::const_deallocate(ptr, 4, 4); | ||
//~^ error: evaluation of constant value failed | ||
}; | ||
|
||
fn main() {} |
9 changes: 9 additions & 0 deletions
9
src/test/ui/consts/const-eval/heap/dealloc_intrinsic_duplicate.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
error[E0080]: evaluation of constant value failed | ||
--> $DIR/dealloc_intrinsic_duplicate.rs:9:5 | ||
| | ||
LL | intrinsics::const_deallocate(ptr, 4, 4); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ pointer to alloc2 was dereferenced after this allocation got freed | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0080`. |
29 changes: 29 additions & 0 deletions
29
src/test/ui/consts/const-eval/heap/dealloc_intrinsic_incorrect_layout.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#![feature(core_intrinsics)] | ||
#![feature(const_heap)] | ||
|
||
use std::intrinsics; | ||
|
||
const _X: () = unsafe { | ||
let ptr = intrinsics::const_allocate(4, 4); | ||
intrinsics::const_deallocate(ptr, 4, 2); | ||
//~^ error: evaluation of constant value failed | ||
}; | ||
const _Y: () = unsafe { | ||
let ptr = intrinsics::const_allocate(4, 4); | ||
intrinsics::const_deallocate(ptr, 2, 4); | ||
//~^ error: evaluation of constant value failed | ||
}; | ||
|
||
const _Z: () = unsafe { | ||
let ptr = intrinsics::const_allocate(4, 4); | ||
intrinsics::const_deallocate(ptr, 3, 4); | ||
//~^ error: evaluation of constant value failed | ||
}; | ||
|
||
const _W: () = unsafe { | ||
let ptr = intrinsics::const_allocate(4, 4); | ||
intrinsics::const_deallocate(ptr, 4, 3); | ||
//~^ error: evaluation of constant value failed | ||
}; | ||
|
||
fn main() {} |
27 changes: 27 additions & 0 deletions
27
src/test/ui/consts/const-eval/heap/dealloc_intrinsic_incorrect_layout.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
error[E0080]: evaluation of constant value failed | ||
--> $DIR/dealloc_intrinsic_incorrect_layout.rs:8:5 | ||
| | ||
LL | intrinsics::const_deallocate(ptr, 4, 2); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ incorrect layout on deallocation: alloc2 has size 4 and alignment 4, but gave size 4 and alignment 2 | ||
|
||
error[E0080]: evaluation of constant value failed | ||
--> $DIR/dealloc_intrinsic_incorrect_layout.rs:13:5 | ||
| | ||
LL | intrinsics::const_deallocate(ptr, 2, 4); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ incorrect layout on deallocation: alloc4 has size 4 and alignment 4, but gave size 2 and alignment 4 | ||
|
||
error[E0080]: evaluation of constant value failed | ||
--> $DIR/dealloc_intrinsic_incorrect_layout.rs:19:5 | ||
| | ||
LL | intrinsics::const_deallocate(ptr, 3, 4); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ incorrect layout on deallocation: alloc6 has size 4 and alignment 4, but gave size 3 and alignment 4 | ||
|
||
error[E0080]: evaluation of constant value failed | ||
--> $DIR/dealloc_intrinsic_incorrect_layout.rs:25:5 | ||
| | ||
LL | intrinsics::const_deallocate(ptr, 4, 3); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ align has to be a power of 2, `3` is not a power of 2 | ||
|
||
error: aborting due to 4 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0080`. |
17 changes: 17 additions & 0 deletions
17
src/test/ui/consts/const-eval/heap/dealloc_intrinsic_zero_sized.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// run-pass | ||
#![feature(core_intrinsics)] | ||
#![feature(const_heap)] | ||
#![feature(inline_const)] | ||
|
||
use std::intrinsics; | ||
|
||
fn main() { | ||
const { | ||
unsafe { | ||
let ptr1 = intrinsics::const_allocate(0, 0); | ||
let ptr2 = intrinsics::const_allocate(0, 0); | ||
intrinsics::const_deallocate(ptr1, 0, 0); | ||
intrinsics::const_deallocate(ptr2, 0, 0); | ||
} | ||
} | ||
} |