Krates (Validate & Security)
Krates is the validation and security layer of LightVM. It checks selected bytecode structures and configured resource restrictions before and during execution.
How Krates Works
Krates combines structural validation, security checks, and execution tick monitoring. These checks do not prove complete bytecode safety, determinism, or function reachability.
- Bounds Verification:
validate_bytecodechecks that targets ofJump,IfFalse, andBreakare below the bytecode length. - Variable Validation:
validate_varschecks indices inValIdx,GetIdx,SetIdx,IncIdx, andDecIdxagainstvar_count. - Function Start Bounds: Checks that each function start address is below the bytecode length.
- Feature Gating:
has_nightly_opcodesdetectsinstantiate,import, andexportin serialized source; the loading interface rejects them when nightly support is disabled. - Resource Quotas:
validate_securitycounts I/O, imports, object/array creation, calls, and jumps in bytecode against configured limits. The execution loop also counts these operations at runtime. - Module Whitelisting: Checks import module names against
SecurityConfig.allowed_importsduring security validation. - Nop Padding: Rejects bytecode longer than ten instructions when the
Nopcount exceeds the total instruction count divided by ten using integer division. - Bypass Capability:
unsafe_modebypassesvalidate_security(static quotas, import whitelisting, andNoppadding checks) and runtime I/O, import, allocation, call, and jump quotas. Structural validation, feature gating, stack limits, and gas checks remain active. - Gas Monitoring (Tick Control):
GasMonitorchecks the tick limit on every execution-loop iteration. Ticks count loop iterations, not elapsed time or hardware processing cycles. - Tick Validation: Rejects a zero
max_ticksduring initialization and reports an error when the tick count reaches the limit.