Jakob Degen
    • 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
# Opsem for `async` blocks #### Dependencies Besides all of the "standard" parts of our current working semantics, this proposal depends on two non-standard parts: 1. `AliasCell<T>`. This type has been proposed a number of times, the intent is that `AliasCell<T>` is to `&mut` as `UnsafeCell<T>` is to `&`. (I have not thought about whether there are more subtleties to this, so if there are that's probably a bug in what I'm saying here) 2. [Overlapping allocations]. See [UCG#328] for context. We will actually need to adjust this proposal a bit, more below. [Overlapping allocations]: https://hackmd.io/@2S4Crel_Q9OwC_vamlwXmw/HkQaMnB49 [UCG#328]: https://github.com/rust-lang/unsafe-code-guidelines/issues/328 ### StorageLive modifications One key thing that we need to justify is that locals in generators have addresses that overlap with their enclosing type. We achieve this by adding a field `generator_info: Option<(AllocId, Range<Address>)>` to stack frames. This value is set to `Some` for all stack frames that are associated with generator bodies. When performing allocations for a StorageLive, we search the entire call stack for frames in which this is set to `Some`. We then explicitly permit the address chosen for the allocation to overlap the given allocations at the given address range, even if both allocations are forced. *We previously proved that the scheme laid out in UCG#328 was implementable wrt ptr2int rountrips by arguing that forced allocations never overlap. This obviously breaks that. However, implementability of this scheme follows from the generator lowering pass being correct, and so there should be no "real" problems here.* ### Generator Types Generator types are always of the form `AliasCell<GenType>` where `GenType` is freshly generated for each generator and of the form: ```rust= #[repr(Rust)] enum GenType { Unstarted, Finished, } ``` ### Generator Table The key idea of this proposal is to store all info about generator state in a "generator table." The GT is a `Map<(AllocId, Address), GeneratorSuspendState>` for: ```rust= struct GeneratorSuspendState { frame: StackFrame, resume_block: BbName, resume_place: Place, unwind_block: BbName, } ``` There is exactly one entry in the GT for each currently suspended generator. The gist of the idea is that all execution basically proceeds as expected, and when hitting a yield point, instead of returning completely, we stash the current stack frame in the generator table so that we can resume execution later. Concretely, this means that `resume` looks like this (in MiniRust): ```rust= impl Machine { // Interface depends on how we decide to set up shims. For now, we assume // that we call this shim instead of pushing a stack frame. fn builtin_generator_resume_shim( &mut self, // Magic type that happens to contain all the information we need generator_info: GeneratorInfo, self_pointer: Pointer, argument: Value, ) -> Result<()> { // First, retag the `Pin<&mut Self>` pointer like for any call let self_pointer = self.mem.retag(self_pointer); let address = self_pointer.address; // Next, check if there is already an in-progress execution let key = (self_pointer.alloc_id, address); if let Some(suspend_state) = self.generator_table.remove(key) { // Resuming with the wrong type is UB if suspend_state.frame.func != generator_info.body { throw_ub!(); } self.mem.typed_store( suspend_state.resume_place, argument, generator_info.resume_ty ); suspend_state.frame.next = (suspend_state.resume_block, 0); // FIXME: Fails to correcty set the return place self.push_stack_frame(suspend_state.frame); return Ok(()); } let start_new = evaluate!( let self_pointer = self_pointer.get().cast<GenTy>(); match *self_pointer { GenTy::Unstarted => true, GenTy::Finished => false, } ); if start_new { // Won't re-write everything here, none of this is interesting let frame = self.make_frame_for_call(generator_info.body, ...); frame.generator_key = Some(key); // The only thing that is different from a normal function call // is that we must retag the self pointer with a protector // belonging to the generator frame. self.mem.retag(self_pointer, protector = frame.call_id); self.push_stack_frame(frame); } else { self.panic_shim() } } } ``` The `drop` impl for `GenType` is very similar but simpler because there are fewer cases we have to care about: ```rust= impl Machine { // Interface depends on how we decide to set up shims. For now, we assume // that we call this shim instead of pushing a stack frame. fn builtin_generator_drop_shim( &mut self, // Magic type that happens to contain all the information we need generator_info: GeneratorInfo, self_pointer: Pointer, argument: Value, ) -> Result<()> { // First, retag the `Pin<&mut Self>` pointer like for any call let self_pointer = self.mem.retag(self_pointer); // Next, check if there is already an in-progress execution let key = (self_pointer.alloc_id, self_pointer.address); if let Some(suspend_state) = self.generator_table.remove(key) { // Resuming with the wrong type is UB if suspend_state.frame.func != generator_info.body { throw_ub!(); } suspend_state.frame.next = (suspend_state.unwind_block, 0); self.push_stack_frame(suspend_state.frame); return Ok(()); } // Drops are a nop before execution has started or after it has ended } } ``` ### Yield and Return At MIR building time, MIR inside generator bodies is modified so that `return foo;` actually does `return GeneratorState::Complete(foo);` and `yield foo;` does `yield GeneratorState::Yielded(foo);`. The implementation of yield is then extremely simple: Copy the yielded value similarly to what happens when returning, and push the top frame onto the generator table. FIXME: The return place vs yield place thing kind of makes a mess here. Might have to jump through some hoops as long as we don't have return by value. ## Correctness These are just outlines, but should get the point across. ### Optimizations The only change to the way that functions normally execute is the change to StorageLive (and having more overlapping allocations). With this exception, the proofs of correctness for optimizations before this proposal are expected to continue to go through after this proposal. ### Generator lowering We show correctness by doing the lowering in four phases: #### Storing yield point in memory Number the yield points 1..Y. We introduce a new local to the function that is always set to N immediately before yield point N. #### Using generator memory as an allocator When we created the stack frame for the generator, its memory was retagged with Unique permissions and we included a protector. We now store the resulting pointer (in the stack frame or something), and replace all locals with places that are derived from that pointer. We assume that the generator was large enough so that it is possible to find offsets for all locals such that simultaneously storage-live locals get non-overlapping memory. As a part of this change, we also stop including the generator info in the generator's stack frame. This change is correct wrt code observing addresses because we explicitly allowed ahead of time for these allocations to be within the given range. They are still non-overlapping wrt each other. They are also non-overlapping wrt other allocations - this is true by induction hypothesis (or something) when the generator frame is created, and because we now no longer insert the generator info into the frame, it is not possible for any other allocation to overlap with the ones that are local to this body. This change is also hopefully correct wrt SB. #### Removing the protected retag We now replace all uses of the pointer that is created by the protected retag with the pointer that is passed in to the `poll` or `drop` call. I don't think this is actually strictly speaking sound, specifically I have concerns about all the pointers not being in a contiguous SRW block; however, I think the bug here is that we have insufficiently strong requirements for the pointers used before this transformation. #### Match + Inlining The remainder of the generator transform now just replaces what is left of the shim with a `match` on the local we introduced in the first step and inlines the relevant section of the generator body into each arm of that match. (I'm blindly assuming that correctness would trivially follow if we rigorously wrote down what this transform actually was)

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