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

オープン
#109 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

@brxs がすでに取り組んでいます。

2026年11月27日 から。

  • #111 @brxs による — マージされずにクローズ
  • #115 @brxs による — オープン

評価

難易度
3/5
見積もり時間
1〜2日
初心者へのやさしさ
35/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
停滞
技術スタック
rust
領域
backend

調査の方向性

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.

索引モデルが issue の本文から書いたものです。

説明

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.)

主要言語
Rust
スター
470
フォーク
11
平均マージ
4時間 10分
マージ済み PR(30日)
89

環境構築

このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

anomalyco/psychoscript のほかの issue

anomalyco/psychoscript の issue をすべて見る

似ている issue

Rust の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。