Skip to content

VM: (EXPERIMENTAL) Change to a capabilities-based system from feature flags. - #1148

Open
schungx wants to merge 6 commits into
mainfrom
vm-capabilities
Open

VM: (EXPERIMENTAL) Change to a capabilities-based system from feature flags.#1148
schungx wants to merge 6 commits into
mainfrom
vm-capabilities

Conversation

@schungx

@schungx schungx commented Aug 21, 2026

Copy link
Copy Markdown
Collaborator

This PR is an experiment to replace the current feature flags-based fingerprint for artifacts with one based on the caps supported by a particular host build, as well as the caps required to run an artifact.

Caps

The Caps bit-flags contains a bunch of capabilities for running an artifact inside the VM.

Host Caps

Based on the build's feature flags, a set of caps for the host VM can be obtained (together with other info such as the width of integers and floats etc.).

Compilation

During compilation, when the compiler lowers from AST nodes, the caps are collected. In the end, there is a set of caps required for running that artifact, which is stored as a fingerprint together with the artifact.

Verification

The verifier supports caps as well. When an artifact is being verified, it goes through all the instructions and checks that they never require caps not available in the supported set.

Reading

Reading from a stored artifact compares the required caps (stored as fingerprint) with what the host supports. This is quite versatile.

For example, assume that an artifact is built without no_index, but the script does not use any arrays or indexing. Then the artifact would not require the INDEXING or ARRAY caps. Such an artifact can successfully run on a VM built with no_index. That's because, even though the host caps do not contain INDEXING nor ARRAY, they are not needed to run the artifact and therefore the load is allowed to succeed.

Safeguard

When the artifact is loaded, the verifying process checks the actual instruction caps with the stored fingerprint to make sure that nothing has been mucked with. That is to safeguard the situation that the fingerprint is manually edited afterwards to drop a necessary cap, which may result in errors when it is run on the VM.

@schungx
schungx requested a review from ImTheSquid August 21, 2026 09:32
@schungx schungx self-assigned this Aug 21, 2026
@schungx schungx added enhancement vm Issues related to the Rhai Grain bytecodes compiler and VM. labels Aug 21, 2026
Comment thread src/grain/format/abi.rs
/// The script uses floating-point numbers, which are not available under `no_float`.
const FLOAT = 0b0000_0000_0000_0000_0001;
/// The script uses arrays, which are not available under `no_index`.
const ARRAY = 0b0000_0000_0000_0000_0010;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Bit of a nit but I think it's better to use 1 << N instead of writing out the entire binary number, it's hard to find the one here

Comment thread src/grain/format/abi.rs
/// The script is built with `sync`.
const SYNC = 0b0100_0000_0000_0000_0000;
/// The script contains unsupported syntax.
const UNSUPPORTED = 0b1000_0000_0000_0000_0000;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I don't really see why this is necessary, the program can't be exported if it has unsupported items so this will never be written.

Comment thread src/grain/format/abi.rs
const CUSTOM_SYNTAX = 0b0000_0010_0000_0000_0000;

/// The script is built with `sync`.
const SYNC = 0b0100_0000_0000_0000_0000;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This is more of a question, but what does sync change here? What capabilities does that restrict inside the binary?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement vm Issues related to the Rhai Grain bytecodes compiler and VM.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants