fix(rust): duplicate fresh strings bypass Limits::max_heap_bytes

Abierto
#109 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

@brxs ya está trabajando en esto.

Desde el 27/11/2026.

  • #111 de @brxs — cerrado sin fusionar
  • #115 de @brxs — abierto

Evaluación

Dificultad
3/5
Tiempo estimado
1-2 días
Aptitud para principiantes
35/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Estancado
Stack tecnológico
rust
Área
backend

Línea de trabajo

Read rust/crates/psychoscript/src/heap.rs, focusing on heap::Walk::string and how Limits::max_heap_bytes is enforced. Run the minimal Rust test or the crate's memory-limit tests, then verify that separately allocated identical strings trigger DiagnosticKind::MemoryLimitExceeded without unbounded RSS growth.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

Summary

In the Rust crate (psychoscript 0.1.3 / dev), Limits::max_heap_bytes can be bypassed with separately allocated identical strings, allowing unbounded RSS growth under a small heap limit.

Cause

heap::Walk::string (rust/crates/psychoscript/src/heap.rs) deduplicates strings by (r.s, r.units) content via texts: FxHashSet<(&str, Option<&[u16]>)>:

let r = self.vm.atoms.sref(s);
if self.texts.insert((r.s, r.units)) {
    self.bytes += STRING_BYTES + 2 * r.len16() as u64;
}

However, strings constructed dynamically (e.g. 'x'.repeat(10_000) or string concatenation) each allocate their own buffer and StrId in the string arena (vm.atoms.bufs). Because Walk::string only adds bytes for the first occurrence of a given text, holding 10,000 separately allocated copies of a 10,000-char string measures as ~20 KB instead of ~100+ MB and never trips MemoryLimitExceeded.

Minimal Reproduction
use psychoscript::{DiagnosticKind, Limits, Runtime};

#[test]
fn duplicate_fresh_strings_bypass_max_heap_bytes() {
    let rt = Runtime::make().limits(Limits {
        max_heap_bytes: Some(2 * 1024 * 1024),
        ..Limits::default()
    });
    let code = "const a = []; for (let i = 0; i < 10_000; i++) a.push('x'.repeat(10_000)); return a.length";
    let outcome = rt.execute(code);
    assert_eq!(
        outcome.result.unwrap_err().kind,
        DiagnosticKind::MemoryLimitExceeded
    );
}

Observed: Completes with Ok(10000) and grows process RSS by ~103 MiB under a 2 MiB max_heap_bytes limit.
Expected: Stops with DiagnosticKind::MemoryLimitExceeded.

(Split out the separate BigInt max_heap_bytes issue into #113.)

Lenguaje dominante
Rust
Estrellas
470
Forks
11
Merge medio
4 h 10 min
PR fusionados (30 d)
89

Preparar el entorno

Este proyecto no incluye contenedor de desarrollo, Dockerfile ni guía de contribución, así que la configuración corre por tu cuenta: empieza por su README y consulta nuestra guía para la primera contribución para los pasos generales.

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de anomalyco/psychoscript

Todos los issues de anomalyco/psychoscript

Issues similares

Más issues de Rust

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.