WebAssembly / WebAssembly/binaryen

[wasm-ctor-eval] Any way to zero the stack after calling wasm-ctor-eval?

Open
#4,527 8 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
WebAssembly
Stars
8.6k
Forks
885
Avg merge
1d 19h
Merged PRs (30d)
69

Description

I'm using wasm-ctor-eval as a great "extended compile-time evaluation" for when constexpr doesn't suffice. I did notice however that as the memory state gets saved after running, that includes the stack. That clutters (a bit) the resulting binary since now the dirty stack is dutifully recorded, even though nothing should ever depend on its exact contents. (i.e. it saves the contents of the stack after it has been returned from; in the case of partial evaluation of course it should keep the contents of whichever part of the stack hasn't been returned from yet.)

Is there any (easy?) way to have it not do that? I can manually zero out individual stack variables before returning (cast them to a volatile char array, iterate over each one) but this gets quickly tiresome.

EDIT: I realized I can do the following to just zero out the entire stack. Good enough for me!

volatile unsigned char* _start = (volatile unsigned char*)&__data_end;
volatile unsigned char* _end = (volatile unsigned char*)&__heap_base;
for (size_t i = 0; _start + i < _end; ++i) {
    _start[i] = 0;
}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing wasm-ctor-eval's memory-state save step and how it distinguishes returned stack data from the portion still needed for partial evaluation. The issue's desired outcome is to omit or zero the completed stack while preserving any unfinished stack state; the provided C loop is a workaround and can serve as a behavioral reference.

Written by the indexing model from the issue text.

Assessment

Tech stack
c, wasm
Domain
compilers
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.