This repository has been archived on 2025-08-04. You can view files and clone it, but cannot push or open issues or pull requests.
rhaj/rhai_engine/rhaibook/safety/memory.md
2025-04-03 09:18:05 +02:00

1.5 KiB

Limiting Memory Usage

{{#include ../links.md}}

During Evaluation

To prevent out-of-memory failures, provide a closure to [Engine::on_progress][progress] to track memory usage and force-terminate a malicious script before it can bring down the host system.

Most O/S provides system calls to obtain the current memory usage of the process.

let mut engine = Engine::new();

const MAX_MEMORY: usize = 10 * 1024 * 1024;   // 10MB

engine.on_progress(|_| {
    // Call a system function to obtain the current memory usage
    let memory_usage = get_current_progress_memory_usage();

    if memory_usage > MAX_MEMORY {
        // Terminate the script
        Some(Dynamic::UNIT)
    } else {
        // Continue
        None
    }
});

During Parsing

A malicious script can be carefully crafted such that it consumes all available memory during the parsing stage.

Protect against this by via a closure to [Engine::on_parse_token][token remap filter].

let mut engine = Engine::new();

const MAX_MEMORY: usize = 10 * 1024 * 1024;   // 10MB

engine.on_parse_token(|token, _, _| {
    // Call a system function to obtain the current memory usage
    let memory_usage = get_current_progress_memory_usage();

    if memory_usage > MAX_MEMORY {
        // Terminate parsing
        Token::LexError(
            LexError::Runtime("out of memory".into()).into()
        )
    } else {
        // Continue
        token
    }
});