Vishnunarayan K. I.
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
      • Invitee
    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Versions and GitHub Sync Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
Invitee
Publish Note

Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

Your note will be visible on your profile and discoverable by anyone.
Your note is now live.
This note is visible on your profile and discoverable online.
Everyone on the web can find and read all notes of this public team.
See published notes
Unpublish note
Please check the box to agree to the Community Guidelines.
View profile
Engagement control
Commenting
Permission
Disabled Forbidden Owners Signed-in users Everyone
Enable
Permission
  • Forbidden
  • Owners
  • Signed-in users
  • Everyone
Suggest edit
Permission
Disabled Forbidden Owners Signed-in users Everyone
Enable
Permission
  • Forbidden
  • Owners
  • Signed-in users
Emoji Reply
Enable
Import from Dropbox Google Drive Gist Clipboard
   owned this note    owned this note      
Published Linked with GitHub
Subscribed
  • Any changes
    Be notified of any changes
  • Mention me
    Be notified of mention me
  • Unsubscribe
Subscribe
# Heap allocation in const eval: Design doc ## Prelude Before we dig into actual allocations, we'll introduce some examples that motivate the implementation. First of all, ```rust const FOO: Vec<i32> = Vec::new(); ``` works just fine on the stable compiler, and we'll want this to keep working forever. The reason this is sound right now ([full discussion on zulip](https://rust-lang.zulipchat.com/#narrow/stream/146212-t-compiler.2Fconst-eval/topic/drop.20vs.20construction.20from.20const/near/216729919)) is that since there are no heap alloctions possible at all in const contexts, we know that `FOO` cannot contain any heap allocations and thus know that invoking `drop(FOO)` will not actually deallocate anything, because nothign was allocated. Now, since we want to actually allow heap allocations, we need to consider the difference between transient and non-transient allocations. Transient allocations are alloations only used for computing the final value of a constant, but are not part of the final constant itself. One example would be ```rust const FOO: Vec<i32> = { let foo = "foo".to_string(); assert!(foo.len() == 3); Vec::new() }; ``` The allocation created by `to_string()` is deallocated before the evaluation of the constant is done, and even if we used during the evaluation itself. Non-transient allocations are allocations that end up in the final constant. An illegal example would be ```rust const FOO: Vec<i32> = vec![42]; ``` This is illegal because invoking `drop(FOO)` at runtime would drop an allocation created during const eval. and `drop(FOO); drop(FOO);` would drop it twice, causing a double-free, even if we somehow managed to make a single `drop(FOO)` legal. A legal way to put a heap allocation into a constant would be ```rust const FOO: &[i32] = vec![42].leak(); ``` since there is no way to ever deallocate the resulting constant, so it is indistinguishable from ```rust const FOO: &[i32] = &[42]; ``` which already works on the stable compiler today. ### Non-transient allocations We'll start out with non-transient allocations, as these are the actually problematic ones. First of all, `Box<T>` actually has a second generic parameter: `Box<T, A>`, but the `A` is defaulted to `std::alloc::Global`. This means if you use `Box::new(42)`, you actually get `Box::<i32, Global>::new(42)` which will then invoke `Global::alloc` internally $somehow (the details are not important). This gets very interesting with `Vec`, because while `Vec` is defined as `struct Vec<T, A = Global>`, the implementation of `new` is ```rust impl<T> Vec<T> { fn new() -> Self { /* code goes here */ } } ``` which is actually ```rust impl<T> Vec<T, Global> { fn new() -> Self { /* code goes here */ } } ``` This means you can invoke `Vec::new()`, even though it references `Global`, which does not implement `const AllocRef`. But you won't be able to do `x.push(42)` anymore, because that is defined in a different impl block: ```rust impl<T, A: AllocRef> Vec<T, A> { fn push(t: T) { /* code goes here */ } } ``` which requires that the allocator implements `const AllocRef`. ## `AllocRef`, `alloc::Global` and `alloc::ConstGlobal` The general scheme for heap allocations in const contexts will use a custom allocator which never deallocates. So, even if you have a `Box<i32, ConstGlobal>` and you `drop` it, no deallocation will happen. This means that we'll be able to write ```rust const FOO: Box<i32, ConstGlobal> = Box::new_in(42, ConstGlobal); let x = FOO; let y = FOO; ``` without causing any heap allocations whatsoever. When used with reallocating datastructures like `Vec`, there can be unexpected behaviour though: ```rust const FOO: Vec<i32, ConstGlobal> = Vec::new_in(ConstGlobal); FOO.push(42); // Panic due to "out of memory" in the given allocator ``` It's not too problematic, since the above code will cause the `const_item_mutation` lint to trigger. We may create additional lints for ```rust let mut x = FOO; x.push(42); // Panics ``` and suggest ```rust let mut x: Vec<i32> = FOO.into_iter().collect(); ``` or whatever API will be the default for moving between different allocators. The same issue occurs when attempting to use `ConstGlobal` at runtime ```rust let x = Box::new_in(42, ConstGlobal); // Panic due to "out of memory" ``` but such situations should be easy to lint against and are not a soundness problem. While we could just make `ConstGlobal` actually heap allocate at runtime, but never free, @oli-obk is of the opinion that erroring (or panicking) is better than silently causing memory leaks. Users who want leaking allocations can use a leaking allocator defined in user space. ### `impl const AllocRef for ConstGlobal` Since we'll want to have different behaviour between runtime and compile-time, we'll need to introduce a new `const` intrinsic for allocating. There is no requirement to have anything for deallocating, as deallocation will just do nothing. We could consider adding a deallocation intrinsic anyway, in order to ensure that long running const evaluations don't blow up memory beyond what they actually still can access. This will allow users to develop an "oracle" which decides whether we're at runtime or at compile-time by using `catch_unwind`. Since `catch_unwind` is not `const fn`, this is a non-issue at this time. But it still is a first for successful compile-time behaviour with panicking runtime behaviour. An alternative would be to have an additional way to do `impl !AllocRef for ConstGlobal` even though there is a `impl const AllocRef for ConstGlobal`, but @oli-obk has no clue if we really want to open this can of worms. It should be considered before stabilization, but does not block an initial impl. ## Transient heap allocations via `Global` We may still want to support `Global` allocations at some point, since that will make it easier to interact with code that is not generic over allocators. This has various problems though, as discussed in the prelude. These problems can mostly be resolved by pushing the proof obligation of correctness to the user via auto traits similar to `Send` and `Sync` and post-monomorphization errors. Note that the post monomorphization errors only guarantee soundness if the users correctly specified the unsafe trait impls for their types. ### `ConstSafe` and `ConstRefSafe` auto traits We add (names bikesheddable!) `ConstSafe` and `ConstRefSafe` unsafe auto traits. `ConstSafe` types may appear in constants directly. This includes all types except * `&T: ConstSafe where T: ConstRefSafe` * `&mut T: !ConstSafe` Other types may (or may not) appear behind references by implementing the `ConstRefSafe` trait (or not) * `*const T: !ConstRefSafe` * `*mut T: !ConstRefSafe` * `String: ConstRefSafe` * `UnsafeCell<T>: !ConstRefSafe` * `i32: ConstRefSafe + ConstSafe`. * the same for other primitives * `[T]: ConstRefSafe where T: ConstRefSafe` * the data pointer of a fat pointer follows the same rules as the root value of an allocation * rationale: the value itself could be on the heap, but you can't do anything bad with it since trait methods at worst can get a `&self` if you start with a `&Trait`. Further heap pointers inside the are forbidden, just like in root values of constants. * ... and so on (needs full list and rationale before stabilization) Additionally values that contain no pointers to heap allocations are allowed as the final value of a constant. Our rationale is that 1. we want to forbid types like ```rust struct Foo(*mut ()); ``` whose methods convert the raw pointer to a raw pointer to the actual type (which might contain an unsafe cell) and the modify that value. 2. we want to allow types like `String` (at least behind references), since we know the user can't do anything bad with them as they have no interior mutability. `String` is pretty much equivalent to ```rust struct String(*mut u8, usize, usize); ``` Which is indistinguishable from the `Foo` type via pure type based analysis. In order to distinguish these two types, we need to get some information from the user. The user can write ```rust unsafe impl ConstRefSafe for String {} ``` and declare that they have read and understood the `ConstRefSafe` documentation and solemly swear that `String` is only up to good things. #### Backcompat issue 1 Now one issue with this is that we'd suddenly forbid ```rust struct Foo(*mut ()); const FOO: Foo = Foo(std::ptr::null_mut()); ``` which is perfectly sane and legal on stable Rust. The problems only happen once there are pointers to actual heap allocations or to mutable statics in the pointer field. Thus we allow any type directly in the root of a constant, as long as there are none such pointers in there. #### Backcompat issue 2 Another issue is that ```rust struct Foo(*mut ()); const FOO: &'static Foo = &Foo(std::ptr::null_mut()); ``` is also perfectly sane and legal on stable Rust. Basically as long as there are no heap pointers, we'll just allow any value, but if there are heap pointers, we require `ConstSafe` and `ConstRefSafe` ## Related links Proposal: https://github.com/rust-lang/const-eval/issues/20 Discussion on zulip: * https://rust-lang.zulipchat.com/#narrow/stream/146212-t-compiler.2Fconst-eval/topic/.60ConstSafe.60.20and.20bitwise.20copies/near/180012229 * https://rust-lang.zulipchat.com/#narrow/stream/146212-t-compiler.2Fconst-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval/near/216728136 ## Prior art - C++20 allows heap allocation in `constexpr` [link to proposal](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p0784r7.html) - Dlang has very powerful compile time function evaluation(CTFE).

Import from clipboard

Paste your markdown or webpage here...

Advanced permission required

Your current role can only read. Ask the system administrator to acquire write and comment permission.

This team is disabled

Sorry, this team is disabled. You can't edit this note.

This note is locked

Sorry, only owner can edit this note.

Reach the limit

Sorry, you've reached the max length this note can be.
Please reduce the content or divide it to more notes, thank you!

Import from Gist

Import from Snippet

or

Export to Snippet

Are you sure?

Do you really want to delete this note?
All users will lose their connection.

Create a note from template

Create a note from template

Oops...
This template has been removed or transferred.
Upgrade
All
  • All
  • Team
No template.

Create a template

Upgrade

Delete template

Do you really want to delete this template?
Turn this template into a regular note and keep its content, versions, and comments.

This page need refresh

You have an incompatible client version.
Refresh to update.
New version available!
See releases notes here
Refresh to enjoy new features.
Your user state has changed.
Refresh to load new user state.

Sign in

Forgot password

or

By clicking below, you agree to our terms of service.

Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
Wallet ( )
Connect another wallet

New to HackMD? Sign up

Help

  • English
  • 中文
  • Français
  • Deutsch
  • 日本語
  • Español
  • Català
  • Ελληνικά
  • Português
  • italiano
  • Türkçe
  • Русский
  • Nederlands
  • hrvatski jezik
  • język polski
  • Українська
  • हिन्दी
  • svenska
  • Esperanto
  • dansk

Documents

Help & Tutorial

How to use Book mode

Slide Example

API Docs

Edit in VSCode

Install browser extension

Contacts

Feedback

Discord

Send us email

Resources

Releases

Pricing

Blog

Policy

Terms

Privacy

Cheatsheet

Syntax Example Reference
# Header Header 基本排版
- Unordered List
  • Unordered List
1. Ordered List
  1. Ordered List
- [ ] Todo List
  • Todo List
> Blockquote
Blockquote
**Bold font** Bold font
*Italics font* Italics font
~~Strikethrough~~ Strikethrough
19^th^ 19th
H~2~O H2O
++Inserted text++ Inserted text
==Marked text== Marked text
[link text](https:// "title") Link
![image alt](https:// "title") Image
`Code` Code 在筆記中貼入程式碼
```javascript
var i = 0;
```
var i = 0;
:smile: :smile: Emoji list
{%youtube youtube_id %} Externals
$L^aT_eX$ LaTeX
:::info
This is a alert area.
:::

This is a alert area.

Versions and GitHub Sync
Get Full History Access

  • Edit version name
  • Delete

revision author avatar     named on  

More Less

Note content is identical to the latest version.
Compare
    Choose a version
    No search result
    Version not found
Sign in to link this note to GitHub
Learn more
This note is not linked with GitHub
 

Feedback

Submission failed, please try again

Thanks for your support.

On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

Please give us some advice and help us improve HackMD.

 

Thanks for your feedback

Remove version name

Do you want to remove this version name and description?

Transfer ownership

Transfer to
    Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

      Link with GitHub

      Please authorize HackMD on GitHub
      • Please sign in to GitHub and install the HackMD app on your GitHub repo.
      • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
      Learn more  Sign in to GitHub

      Push the note to GitHub Push to GitHub Pull a file from GitHub

        Authorize again
       

      Choose which file to push to

      Select repo
      Refresh Authorize more repos
      Select branch
      Select file
      Select branch
      Choose version(s) to push
      • Save a new version and push
      • Choose from existing versions
      Include title and tags
      Available push count

      Pull from GitHub

       
      File from GitHub
      File from HackMD

      GitHub Link Settings

      File linked

      Linked by
      File path
      Last synced branch
      Available push count

      Danger Zone

      Unlink
      You will no longer receive notification when GitHub file changes after unlink.

      Syncing

      Push failed

      Push successfully