Skip to content
77-webgl-jslib-memory-rules logo

77 Webgl Jslib Memory Rules

77-webgl-jslib-memory-rules

Status: ACTIVE AppliesTo: v10 Type: Policy / Hard Rules

SKILL.md

Full skill instructions

77-webgl-jslib-memory-rules

Status: ACTIVE AppliesTo: v10 Type: Policy / Hard Rules


Purpose

WebGL .jslib ↔ C# 간 ptr/​len/문자열 메모리 수명 규칙을 Hard Rule로 고정한다.

이 규칙을 위반하면 메모리 누수 또는 크래시가 발생한다.


Hard Rules

1. .jslib가 _malloc한 모든 버퍼는 C#이 반드시 Free 호출로 반환해야 함

이벤트포인터Free 함수
MESSAGEdataPtrWS_FreeBuffer(dataPtr)
ERROR/​CLOSEmessagePtrWS_FreeString(messagePtr)

위반 시: WASM heap 메모리 누수 → 장기 실행 시 OOM 크래시

2. C#은 WS_PollEvent로 받은 ptr을 즉시 managed(ArrayPool)로 복사하고 ptr을 보존하지 않는다

// CORRECT
var buffer = ArrayPool<byte>.Shared.Rent(dataLen);
Marshal.Copy(dataPtr, buffer, 0, dataLen);
// ... 사용 ...
ArrayPool<byte>.Shared.Return(buffer);
WS_FreeBuffer(dataPtr);

// WRONG - ptr 보존 금지
_savedPtr = dataPtr; // 이후 Free 시점 불명확 → 누수/​UAF

위반 시: Use-After-Free 또는 메모리 누수

3. 한 Tick에서 처리 가능한 이벤트 개수 상한이 있어야 한다

const int MaxEventsPerTick = 64; // 권장값

for (int i = 0; i < MaxEventsPerTick; i++)
{
    if (WS_PollEvent(...) == 0)
        break;
    // 처리
}

위반 시: 대량 수신 시 프레임 드롭, 메인 스레드 블로킹

4. ToArray() 금지, ArrayPool<byte> 사용 강제

// CORRECT
var buffer = ArrayPool<byte>.Shared.Rent(dataLen);
Marshal.Copy(dataPtr, buffer, 0, dataLen);
// ... 사용 후 ...
ArrayPool<byte>.Shared.Return(buffer);

// WRONG
byte[] buffer = new byte[dataLen]; // GC 압박
Marshal.Copy(dataPtr, buffer, 0, dataLen);

위반 시: GC 압박으로 프레임 스파이크, 핫 경로 allocation


권장 구현 패턴

MESSAGE 처리

case EventType.MESSAGE:
    // 1. ArrayPool에서 버퍼 임대
    var buffer = ArrayPool<byte>.Shared.Rent(dataLen);
    try
    {
        // 2. Marshal.Copy로 복사
        Marshal.Copy(dataPtr, buffer, 0, dataLen);

        // 3. core로 전달
        _core.OnFrame(_sessionId, buffer.AsSpan(0, dataLen));
    }
    finally
    {
        // 4. ArrayPool 반환
        ArrayPool<byte>.Shared.Return(buffer);
    }
    // 5. Free 호출 (반드시)
    WS_FreeBuffer(dataPtr);
    break;

ERROR/​CLOSE 처리

case EventType.CLOSE:
    string reason = "";
    if (messagePtr != IntPtr.Zero)
    {
        reason = Marshal.PtrToStringUTF8(messagePtr);
        WS_FreeString(messagePtr); // 반드시
    }
    EnqueueDispatch(() => OnClose?.Invoke((ushort)code, reason));
    break;

FAIL 조건

아래 패턴이 문서/코드에 등장하면 FAIL:

  1. Free 누락 가능성이 있는 설계

    • 예: dataPtr를 필드에 저장하고 나중에 Free하는 구조
    • 예: 예외 발생 시 Free가 호출되지 않는 코드 경로
  2. ToArray() 또는 new byte[] 사용

    • 핫 경로(수신 루프)에서 allocation
  3. 이벤트 처리량 상한 없음

    • 무한 루프로 PollEvent 호출
  4. ptr 보존

    • WS_PollEvent에서 받은 ptr을 복사 없이 보관

Verification Checklist

  • MESSAGE 수신 후 WS_FreeBuffer(dataPtr) 호출됨
  • ERROR/​CLOSE 수신 후 messagePtr이 non-null이면 WS_FreeString(messagePtr) 호출됨
  • 예외 발생 시에도 Free가 호출되도록 try-finally 또는 동등한 구조 사용
  • ArrayPool<byte>.Shared.Rent()/.Return() 사용
  • ToArray() 또는 new byte[] 미사용
  • Tick당 이벤트 처리 상한 존재 (권장: 64)

Reference

More skills from majiayu000

xiaohongshu logo
majiayu000/claude-arsenal

xiaohongshu

xiaohongshu

286 148
View
agent-task-conductor logo
majiayu000/claude-skill-registry

agent-task-conductor

Conduct multi-agent task orchestration and workflow coordination.

663 1
View
conductor-setup logo
majiayu000/claude-skill-registry

conductor-setup

Initialize project with Conductor artifacts (product definition,

663 1
View
animation-designer logo
majiayu000/claude-skill-registry

animation-designer

Expert in web animations, transitions, and motion design using Framer Motion and CSS

663 1
View
diagramming logo
majiayu000/claude-skill-registry

diagramming

Creates Mermaid and ASCII diagrams for flowcharts, architecture, ERDs, state machines, mindmaps, and more. Use when user mentions diagram, flowchart, mermaid, ASCII diagram, text diagram, terminal diagram, visualize, C4, mindmap, architecture diagram, sequence diagram, ERD, or needs visual docume...

663 1
View
h3-pg logo
majiayu000/claude-skill-registry-data

h3-pg

PostgreSQL bindings for H3 hexagonal grid system. Use when working with H3 cells in Postgres, including spatial indexing, geometry/geography integration, and raster analysis.

23 1
View
conductor-development logo
majiayu000/claude-skill-registry

conductor-development

Context-Driven Development skill for projects using Conductor. Use this skill when you detect a `conductor/` directory in the project, when working on tasks defined in a `plan.md` file, or when the user asks about tracks, specs, or plans. Automatically applies TDD workflow, tracks task completion...

663 1
View
conductor-status logo
majiayu000/claude-skill-registry

conductor-status

Display project status, active tracks, and next actions

663 1
View
dockerization logo
majiayu000/claude-skill-registry

dockerization

Official Stakpak application containerization standard operating procedure, a step-by-step guidline to properly dockerize applications. This is a rule book curated by the Stakpak Team.

663 1
View

Popular AI tools

Kaiber logo
Video

Kaiber

Generate, edit, and beat-sync AI video with leading models in one workspace.

Paid
View
Vimcal logo
Productivity

Vimcal

The world's fastest calendar for remote work

Free
View

Transform Your Design with AI Designer by ImgCreator.ai

Freemium
View
Akool AI logo
Content & writing

Akool AI

Revolutionizing Video Production with AI-Powered Creativity

Paid
View

Extend an image past the frame and let AI fill the new aspect ratio.

Freemium
View
StarByFace logo
Security

StarByFace

Discover your celebrity doppelgänger with StarByFace!

Free
View
C

ChainClarity explains 700+ crypto whitepapers in plain English, with layered summaries, comparisons, research tools, alerts, and a $4.99 Pro plan.

Freemium
View
Opus Clip logo
Coding & apps

Opus Clip

Opus.ai: Revolutionize Your Web Experience

Free
View