Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add virtualization doc #69

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions docs/virtualization.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Virtualization

Virtualization in the context of WASI interfaces is the ability for a Webassembly module to implement a given interface and consumer module to be able to use that implement transparently.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Virtualization in the context of WASI interfaces is the ability for a Webassembly module to implement a given interface and consumer module to be able to use that implement transparently.
Virtualization in the context of WASI interfaces is the ability for a Webassembly module to implement a given interface and for a consumer module to be able to use its implementation transparently.


Furthermore there are two classes of virtualization; static and dynamic. Static virtualization is done before the initialization of a Wasm store and dynamic at runtime.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Furthermore there are two classes of virtualization; static and dynamic. Static virtualization is done before the initialization of a Wasm store and dynamic at runtime.
Furthermore there are two classes of virtualization: static and dynamic. Static virtualization is done before the initialization of a Wasm store and dynamic is performed at runtime.


## Usecase

Roughly taken from [here](https://pdfs.semanticscholar.org/4cce/9abb177cc58c199e13b82d498f37010c2bfc.pdf). Lets say a user wants to encrypt a text file they are editing. One way this could be accomplished is by having composable services. An encryption module could export the interface for a directory that would encrypt any file written to it. Lets consider how this could be done with in the framework of WASI. At runtime the encryption module would given a root directory file descriptor in which it would write encrypted text to, it would also would return a wrapped file descriptor of a directory that would be given to the text editor to write in. This means any WASI `fd` related calls that the text editor makes would handled by the encryption module which would then interact base system. Lets look next at a WASI program might accomplish virtualizing the file descriptor interface.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Roughly taken from [here](https://pdfs.semanticscholar.org/4cce/9abb177cc58c199e13b82d498f37010c2bfc.pdf). Lets say a user wants to encrypt a text file they are editing. One way this could be accomplished is by having composable services. An encryption module could export the interface for a directory that would encrypt any file written to it. Lets consider how this could be done with in the framework of WASI. At runtime the encryption module would given a root directory file descriptor in which it would write encrypted text to, it would also would return a wrapped file descriptor of a directory that would be given to the text editor to write in. This means any WASI `fd` related calls that the text editor makes would handled by the encryption module which would then interact base system. Lets look next at a WASI program might accomplish virtualizing the file descriptor interface.
Roughly taken from [here](https://pdfs.semanticscholar.org/4cce/9abb177cc58c199e13b82d498f37010c2bfc.pdf). Let's say a user wants to encrypt a text file they are editing. One way this could be accomplished is by having composable services. An encryption module could export the interface for a directory that would encrypt any file written to it. Let's consider how this could be done with in the framework of WASI. At runtime the encryption module would given a root directory file descriptor in which it would write encrypted text to, it would also would return a wrapped file descriptor of a directory that would be given to the text editor to write in. This means any WASI `fd`-related calls that the text editor makes would handled by the encryption module which would then interact with the base system. Let's look next at how a WASI program might accomplish virtualizing the file descriptor interface.


## Virtualization with GC

The one possibility would be to use an object oriented approach. This is relies on the [function references](https://github.com/WebAssembly/function-references/blob/master/proposals/function-references/Overview.md) and [GC](https://github.com/WebAssembly/gc/blob/master/proposals/gc/Overview.md) proposals.

We might have the following type definition for a `fd` and its related functions.
```
(type $open_func (func (param i32) (param i32) ... ))
(type $close_func)
...

(type $fd (struct
(field $open (ref $open_func))
(field $close (ref $close_func))
))

```

`func.bind` from function references proposals would be used by the programs implementing the interface to create proxy functions.

```
(func $fd-open-proxy (param $_fd (ref $fd)) (param i32) (param i32) ... )
(local $open_ref (ref $open_func))
...
(call_ref
... ;; other params
(get_field $open_func $open (get_local $_fd)
)

)

(func mk-fd-open-proxy (param (ref $fd) (result (ref $open_func)))
(func.bind $open_func (local.get 0) (func.ref $fd-open-proxy))
)
```

In this scenario no functions are imported, only types. While this is one possible future we don't currently have this functionality. While it maybe be possible to polyfill things like `func.bind` using tables, other things such as full GC would more complicated and may create overhead.

### Dynamic Dispatching - Without GC

Assuming the function reference proposal, another way to implement dynamic dispatching currently would be to create a primitive polyfill for structs. An `fd` would then be represented as a stuct who's fields contained all the associated functions

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Assuming the function reference proposal, another way to implement dynamic dispatching currently would be to create a primitive polyfill for structs. An `fd` would then be represented as a stuct who's fields contained all the associated functions
Assuming the function reference proposal, another way to implement dynamic dispatching currently would be to create a primitive polyfill for structs. An `fd` would then be represented as a struct whose fields contain all the associated functions.


```
struct.create(fields: u32) -> anyRef
```

```
struct.set(index: i32, func: funcRef)
```

```
struct.get(stuct_ref: anyRef, index: u32) -> funcRef
```