ejrgilbert / whamm

5 stars 2 forks source link

Global state initialization #56

Open ejrgilbert opened 2 weeks ago

ejrgilbert commented 2 weeks ago

Right now, the language supports global state declaration, but no global state initialization, e.g.:

i32 count; // totally fine
count = 20; // WILL NOT WORK

<rules>

This is because some modules do not have Wasm start functions defined. If a start function doesn't exist, there is no obvious place to emit the global state initialization logic. Declarations are easy since it's just putting a new global into the module!

Possible workarounds:

  1. If no start function exists, emit one! It will be run on startup and initialize global state. It's possible that some use cases won't support a start function...so this may not be a solution for "all programs we instrument".
  2. Instrument all functions in the module to check if global state has been initialized. If it hasn't yet, call the function that we emit to initialize the global state! This would work anywhere, but it would add a bool check to the start of every function, so it would have a higher performance impact.

Solution number 1 is clearly the best choice, if it's possible for the domain we're instrumenting within.

ejrgilbert commented 2 weeks ago

Dfinity confirmed that the "insert a start function" approach should work for their use case. Will work toward that design goal.