Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
e9a70c7
Update dependencies in Cargo.lock and add Cargo.lock to .gitignore
shawhanken Dec 18, 2025
68da611
Merge branch 'main' of https://github.com/yusufyian/RustPython
shawhanken Dec 18, 2025
1bf2bdf
Update dependencies in Cargo.lock and add Cargo.lock to .gitignore
shawhanken Dec 19, 2025
1726b36
Add additional reference files to .gitignore
shawhanken Dec 19, 2025
fd57302
Update .gitignore to exclude additional files and improve dependency …
shawhanken Dec 19, 2025
ecfabb8
Merge branch 'RustPython:main' into main
yusufyian Dec 19, 2025
bc9b80a
Merge branch 'main' of https://github.com/yusufyian/RustPython
shawhanken Dec 19, 2025
1a1c97a
Add checkpoint functionality to VirtualMachine
shawhanken Dec 19, 2025
3997507
Add checkpoint request handling and update checkpoint functionality
shawhanken Dec 19, 2025
551d025
Enhance demo script with additional print statements for debugging
shawhanken Dec 19, 2025
665790f
Update demo user and enhance README with testing instructions
shawhanken Dec 24, 2025
c2edc6b
Update .gitignore to include demo files
shawhanken Dec 24, 2025
4dc6120
Update .gitignore and demo script for type checking
shawhanken Dec 25, 2025
9ac5e54
Rename RustPython binary to "pvm" in Cargo.toml and update demo.py fo…
yusufyian Dec 26, 2025
8ea2822
Rename RustPython binary to 'pvm' in Cargo.toml and update demo.py fo…
yusufyian Dec 26, 2025
66dc584
Update README and test script to reflect binary name change from 'rus…
yusufyian Dec 29, 2025
6c26391
Refactor demo.py for improved clarity and structure in checkpointing …
yusufyian Dec 29, 2025
a1c1891
Update demo.py and README for financial trading scenario simulation
yusufyian Dec 29, 2025
f41b080
Enhance demo.py with additional trading scenario features and update …
yusufyian Dec 29, 2025
5f547a5
Implement PVM host and runtime modules with initial configurations
yusufyian Dec 29, 2025
3ed0799
Update various files for improved functionality and clarity
yusufyian Dec 29, 2025
9704677
Enhance VM functionality and code clarity
yusufyian Dec 29, 2025
965b201
Enhance demo script and update .gitignore
yusufyian Dec 30, 2025
9f354c2
Update .gitignore to include all reference files and remove specific …
yusufyian Dec 30, 2025
b50f1f3
Remove obsolete reference files for PVM integration and continuation …
yusufyian Dec 30, 2025
f8fc889
Remove obsolete demo files for checkpoint/resume functionality
yusufyian Dec 30, 2025
26ac488
Remove obsolete binary snapshot file for demo functionality
yusufyian Dec 30, 2025
b2ba6ea
Remove obsolete test script for checkpoint/resume functionality
yusufyian Dec 30, 2025
2dfed2d
Refactor comments in state_store.py for clarity and consistency
yusufyian Dec 30, 2025
d8aabdd
Enhance checkpoint functionality and update .gitignore
yusufyian Dec 30, 2025
6d54427
Update source location handling in compiler-source
yusufyian Dec 30, 2025
a1a7ec6
Refactor checkpoint and snapshot handling for improved functionality
yusufyian Dec 30, 2025
3620c13
Update version formatting in version.rs to reflect PVM 0.0.2 integration
yusufyian Dec 31, 2025
fe31a3d
Add PVM versioning support and update build script
yusufyian Dec 31, 2025
8eaf89f
Implement multi-frame checkpoint support and enhance stack management
yusufyian Dec 31, 2025
5544761
Enhance checkpoint functionality and update demo scripts
yusufyian Dec 31, 2025
13117d6
Add support for enumerate, zip, map, and filter in snapshot handling
yusufyian Dec 31, 2025
63c44b5
Enhance checkpoint and block stack management in VM
yusufyian Dec 31, 2025
c6806af
Add support for ListIterator and RangeIterator in snapshot handling
yusufyian Jan 3, 2026
9844d0d
Add comprehensive demo snapshot to .gitignore
yusufyian Jan 3, 2026
a07fed6
Remove Cargo.lock file to prevent unnecessary tracking of dependencies
yusufyian Jan 5, 2026
06e9e6a
Add error handling and execution options in PVM runtime
yusufyian Jan 5, 2026
072c0cc
Add determinism support and enhance error handling in PVM runtime
yusufyian Jan 8, 2026
5e01466
Enhance PVM runtime with import tracing and version updates
yusufyian Jan 8, 2026
b9dc39d
Add new business scenario demos to README
yusufyian Jan 8, 2026
d5afdde
Fix formatting issue in escrow_marketplace_demo.py by adding a newlin…
yusufyian Jan 12, 2026
9f3c462
Enhance PVM runtime with continuation support and new features
yusufyian Jan 18, 2026
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
Prev Previous commit
Next Next commit
Add determinism support and enhance error handling in PVM runtime
- Introduced `DeterminismOptions` to the `ExecutionOptions` struct for improved execution control.
- Added new error types: `DeterministicValidationError`, `NonDeterministicError`, and `OutOfGasError` to enhance error reporting.
- Updated `execute_tx_with_options` to handle determinism settings and associated error handling.
- Enhanced demo scripts to showcase determinism features and provide clearer usage instructions.
- Refactored exception mapping to include determinism-related errors for better debugging and user feedback.
  • Loading branch information
yusufyian committed Jan 8, 2026
commit 072c0ccba9e154317cd3c9d92aaa75c56f203f4a
5 changes: 5 additions & 0 deletions Lib/pvm_sdk/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from . import pvm_random
from . import pvm_sys
from . import pvm_time

__all__ = ["pvm_random", "pvm_sys", "pvm_time"]
23 changes: 23 additions & 0 deletions Lib/pvm_sdk/pvm_random.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import pvm_host

_counter = 0


def _next_block():
global _counter
domain = b"random" + _counter.to_bytes(8, "little")
_counter += 1
return pvm_host.randomness(domain)


def random():
block = _next_block()
value = int.from_bytes(block[:8], "little") >> 11
return value / (1 << 53)


def randbytes(n):
out = bytearray()
while len(out) < n:
out.extend(_next_block())
return bytes(out[:n])
7 changes: 7 additions & 0 deletions Lib/pvm_sdk/pvm_sys.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import pvm_host

_ctx = pvm_host.context()

chain_id = _ctx.get("chain_id")
pvm_version = _ctx.get("pvm_version")
stdlib_hash = _ctx.get("stdlib_hash")
9 changes: 9 additions & 0 deletions Lib/pvm_sdk/pvm_time.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import pvm_host


def time():
return pvm_host.context()["timestamp_ms"] / 1000.0


def time_ns():
return pvm_host.context()["timestamp_ms"] * 1_000_000
134 changes: 134 additions & 0 deletions crates/pvm-runtime/src/determinism.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
#[derive(Clone, Debug)]
pub struct DeterminismOptions {
pub enabled: bool,
pub hash_seed: u32,
pub stdlib_whitelist: Vec<String>,
pub stdlib_blacklist: Vec<String>,
pub stdlib_hash: Option<String>,
pub enable_softfloat: bool,
pub enable_gas: bool,
}

impl DeterminismOptions {
pub fn deterministic(hash_seed: Option<u32>) -> Self {
let mut options = Self::default();
options.enabled = true;
options.hash_seed = hash_seed.unwrap_or(0);
options
}

pub fn default_whitelist() -> Vec<String> {
vec![
"builtins",
"types",
"collections",
"collections.abc",
"abc",
"enum",
"dataclasses",
"typing",
"functools",
"itertools",
"operator",
"re",
"sre_compile",
"sre_parse",
"sre_constants",
"_sre",
"string",
"codecs",
"encodings",
"unicodedata",
"math",
"keyword",
"reprlib",
"json",
"copyreg",
"base64",
"binascii",
"struct",
"hashlib",
"hmac",
"warnings",
"heapq",
"bisect",
"_collections",
"_collections_abc",
"_functools",
"_abc",
"_py_abc",
"_struct",
"_weakrefset",
"_weakref",
"_thread",
"_json",
"_hashlib",
"_md5",
"_sha1",
"_sha256",
"_sha512",
"_sha3",
"_blake2",
"_bisect",
"_heapq",
"_warnings",
"_operator",
"pvm_host",
"pvm_sdk",
"pvm_sdk.pvm_time",
"pvm_sdk.pvm_random",
"pvm_sdk.pvm_sys",
"pvm_time",
"pvm_random",
"pvm_sys",
]
.into_iter()
.map(|item| item.to_owned())
.collect()
}
Comment on lines +23 to +99

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🔴 Critical

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Search for _thread module usage and references
rg '_thread' --type=rs -C3 | head -150

# Check for threading stubs or controls in the runtime
rg 'thread' crates/pvm-runtime/ --type=rs -C2 | head -100

# Look for how the whitelist is actually used/enforced
rg 'default_whitelist\|PythonAllowList' --type=rs -B2 -A5

Repository: RustPython/RustPython

Length of output: 145


🏁 Script executed:

#!/bin/bash
# Search for _thread usage in Rust files
rg '_thread' -C3 crates/ | grep -E "\.rs:|_thread" | head -100

# Look for threading-related code and stubs
rg 'thread|Thread' crates/pvm-runtime/ -C2 | head -150

# Check how the whitelist is used
rg 'default_whitelist' crates/ -B2 -A8 | head -100

# Look at the determinism module structure
cat -n crates/pvm-runtime/src/determinism.rs | head -120

Repository: RustPython/RustPython

Length of output: 16273


🏁 Script executed:

#!/bin/bash
# Look at the _thread module implementation in detail
cat -n crates/vm/src/stdlib/thread.rs | head -200

# Search for how whitelist is enforced - look for import restrictions or allowed checks
rg 'stdlib_whitelist|allowed|import_check' crates/pvm-runtime/ -B2 -A3

# Check if there's any PVM-specific handling of _thread
rg 'import.*_thread|_thread.*stub|thread.*determinism' crates/pvm-runtime/ -C3

Repository: RustPython/RustPython

Length of output: 13917


🏁 Script executed:

#!/bin/bash
# Look for functions exported from _thread module
sed -n '1,300p' crates/vm/src/stdlib/thread.rs | grep -E '#\[pyfunction\]|fn start_new_thread|fn run_thread' -A5

# Check the full _thread module exports/functions
rg '#\[pyfunction\]' crates/vm/src/stdlib/thread.rs -A5

# Look for any PVM-specific restrictions on start_new_thread
rg 'start_new_thread' crates/pvm-runtime/ -C5

# Check if there are any tests or documentation about determinism
cat -n crates/pvm-runtime/src/guard.rs | head -80

Repository: RustPython/RustPython

Length of output: 4816


🏁 Script executed:

#!/bin/bash
# Check if there's any runtime interception or stubbing of _thread.start_new_thread
rg 'start_new_thread|allocate_lock' crates/pvm-runtime/ -C5

# Look for any PVM thread management that might control actual threading
rg 'new_thread|make_spawn_func|std::thread' crates/pvm-runtime/ -C3

# Check if there's any documentation about threading in PVM
find crates/pvm-runtime -name "*.md" -o -name "*.rs" | xargs grep -l "thread\|determinism" | head -5

# Look at the complete start_new_thread implementation
sed -n '230,280p' crates/vm/src/stdlib/thread.rs

Repository: RustPython/RustPython

Length of output: 1545


🏁 Script executed:

#!/bin/bash
# Look for any tests verifying determinism
find crates/pvm-runtime -name "*.rs" -exec grep -l "determinism.*test\|test.*determinism\|_thread" {} \;

# Check if there's any documentation or comments about _thread and determinism
rg 'thread.*determinism|determinism.*thread' crates/pvm-runtime/ -C3

# Look at how start_new_thread actually works - does it use vm context
sed -n '250,350p' crates/vm/src/stdlib/thread.rs

# Check if there's initialization code that might control threading behavior
rg 'setup_determinism|init.*determinism' crates/ -C3 | head -50

Repository: RustPython/RustPython

Length of output: 3193


Reconsider including _thread in the determinism whitelist.

The _thread module (line 67) is whitelisted but provides start_new_thread(), which spawns real OS threads with non-deterministic scheduling. No PVM-specific stubbing or controls were found to restrict this behavior. While threading is correctly blacklisted, _thread provides equivalent functionality at a lower level.

Either:

  1. Remove _thread from the whitelist (and validate no SDK modules depend on it)
  2. Document why thread operations are acceptable in your determinism model
  3. Implement PVM-specific controls to stub or isolate thread operations
🤖 Prompt for AI Agents
In `@crates/pvm-runtime/src/determinism.rs` around lines 23 - 99, In
default_whitelist() remove the "_thread" entry from the returned Vec<String> and
re-run/validate that no pvm_sdk modules (e.g., pvm_sdk, pvm_sdk.runtime,
pvm_sdk.runner, pvm_sdk.actor, pvm_sdk.verify, pvm_sdk.types) or other allowed
modules depend on low-level threading; if any depend, either add a documented
justification for keeping "_thread" in default_whitelist() or replace the
dependency with a PVM-controlled stub (implementing a controlled API in pvm_sdk
like pvm_sdk.pvm_thread that disables real OS thread spawning) before keeping
it.


pub fn default_blacklist() -> Vec<String> {
vec![
"time",
"datetime",
"random",
"secrets",
"uuid",
"os",
"sys",
"socket",
"ssl",
"subprocess",
"ctypes",
"threading",
"multiprocessing",
"signal",
"select",
"asyncio",
"pathlib",
"glob",
"tempfile",
"shutil",
"zipfile",
"inspect",
"traceback",
]
.into_iter()
.map(|item| item.to_owned())
.collect()
}
}

impl Default for DeterminismOptions {
fn default() -> Self {
Self {
enabled: false,
hash_seed: 0,
stdlib_whitelist: Self::default_whitelist(),
stdlib_blacklist: Self::default_blacklist(),
stdlib_hash: None,
enable_softfloat: false,
enable_gas: false,
}
}
}
191 changes: 191 additions & 0 deletions crates/pvm-runtime/src/guard.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
use crate::determinism::DeterminismOptions;
use rustpython_vm::{
PyObjectRef, PyResult, VirtualMachine,
builtins::PyListRef,
compiler::Mode,
};

const GUARD_SOURCE: &str = r#"
import builtins
import sys

_ALLOW = set(PVM_WHITELIST)
_DENY = set(PVM_BLACKLIST)
_REAL_IMPORT = builtins.__import__
_HOST = _REAL_IMPORT(PVM_HOST_MODULE, None, None, (), 0)

_ALIAS = {
"time": "pvm_sdk.pvm_time",
"random": "pvm_sdk.pvm_random",
"pvm_time": "pvm_sdk.pvm_time",
"pvm_random": "pvm_sdk.pvm_random",
"pvm_sys": "pvm_sdk.pvm_sys",
}


def _resolve_name(name, globals, level):
if level and globals:
pkg = globals.get("__package__") or globals.get("__name__")
if pkg:
parts = pkg.split(".")
if level <= len(parts):
base = ".".join(parts[: len(parts) - level + 1])
return base + ("." + name if name else "")
return name


def _is_allowed(name, globals=None, level=0):
resolved = _resolve_name(name, globals, level)
if resolved == "sys" and globals:
importer = globals.get("__package__") or globals.get("__name__")
if importer and _allowed_by_whitelist(importer):
return True
parts = resolved.split(".") if resolved else []
for i in range(1, len(parts) + 1):
prefix = ".".join(parts[:i])
if prefix in _DENY:
return False
if resolved in _DENY:
return False
if resolved in _ALLOW:
return True
for i in range(1, len(parts) + 1):
prefix = ".".join(parts[:i])
if prefix in _ALLOW:
return True
if resolved:
prefix = resolved + "."
for item in _ALLOW:
if item.startswith(prefix):
return True
return False


def _allowed_by_whitelist(name):
parts = name.split(".") if name else []
if name in _ALLOW:
return True
for i in range(1, len(parts) + 1):
prefix = ".".join(parts[:i])
if prefix in _ALLOW:
return True
if name:
prefix = name + "."
for item in _ALLOW:
if item.startswith(prefix):
return True
return False


def _alias(name, target):
try:
if "." in target:
leaf = target.rsplit(".", 1)[-1]
mod = _REAL_IMPORT(target, None, None, (leaf,), 0)
else:
mod = _REAL_IMPORT(target, None, None, (), 0)
except Exception:
return
sys.modules[name] = mod


if PVM_SYS_PATH is not None:
sys.path[:] = PVM_SYS_PATH
try:
sys.path_importer_cache.clear()
except Exception:
sys.path_importer_cache = {}

for _name, _target in _ALIAS.items():
_alias(_name, _target)


def _pvm_import(name, globals=None, locals=None, fromlist=(), level=0):
resolved = _resolve_name(name, globals, level)
if resolved in _ALIAS:
mod = sys.modules.get(resolved)
if mod is None:
raise _HOST.DeterministicValidationError("alias module missing: " + resolved)
return mod
if not _is_allowed(name, globals, level):
raise _HOST.NonDeterministicError("module not allowed: " + name)
return _REAL_IMPORT(name, globals, locals, fromlist, level)


builtins.__import__ = _pvm_import


class _PvmImportGuard:
def find_spec(self, fullname, path=None,>
if not _is_allowed(fullname):
raise _HOST.NonDeterministicError("module not allowed: " + fullname)
return None


sys.meta_path.insert(0, _PvmImportGuard())


def _blocked_open(*_args, **_kwargs):
raise _HOST.DeterministicValidationError(
"file IO is disabled in deterministic mode"
)


builtins.open = _blocked_open
try:
import io as _io
_io.open = _blocked_open
except Exception:
pass

if hasattr(builtins, "execfile"):
builtins.execfile = _blocked_open
"#;

pub(crate) fn install(
vm: &VirtualMachine,
options: &DeterminismOptions,
host_module_name: &str,
) -> PyResult<()> {
if !options.enabled {
return Ok(());
}

let scope = vm.new_scope_with_builtins();
let mut whitelist_items = options.stdlib_whitelist.clone();
if !whitelist_items.iter().any(|item| item == host_module_name) {
whitelist_items.push(host_module_name.to_owned());
}
let whitelist = to_pylist(vm, &whitelist_items);
scope
.globals
.set_item("PVM_WHITELIST", whitelist.into(), vm)?;
let blacklist = to_pylist(vm, &options.stdlib_blacklist);
scope
.globals
.set_item("PVM_BLACKLIST", blacklist.into(), vm)?;
let sys_paths = vm.state.config.paths.module_search_paths.clone();
let sys_paths_list = to_pylist(vm, &sys_paths);
scope
.globals
.set_item("PVM_SYS_PATH", sys_paths_list.into(), vm)?;
scope.globals.set_item(
"PVM_HOST_MODULE",
vm.ctx.new_str(host_module_name).into(),
vm,
)?;

let code = vm
.compile(GUARD_SOURCE, Mode::Exec, "<pvm_guard>".to_owned())
.map_err(|err| vm.new_syntax_error(&err, Some(GUARD_SOURCE)))?;
vm.run_code_obj(code, scope)?;
Ok(())
}

fn to_pylist(vm: &VirtualMachine, items: &[String]) -> PyListRef {
let entries: Vec<PyObjectRef> = items
.iter()
.map(|item| vm.ctx.new_str(item.as_str()).into())
.collect();
vm.ctx.new_list(entries)
}
Loading