Optimize local.get
stack occurrences
#809
Labels
optimization
An performance optimization issue.
register-machine
A work item for the register-machine engine.
In the new
wasmi
register-machine translation we have to preserve values oflocal.get x
registers if they are on the stack while an incominglocal.set x
orlocal.tee x
overwrites the value oflocal x
. In order to preserve the value of thelocal x
we have to query the current stack for all occurrences oflocal x
and replace them withlocal y
wherey
refers to a register in the preservation register space.The register preservation is explained a bit here: #808
In order to safe guard
wasmi
against malicious attackers it is important to make this stack query as fast and attacker resistant as possible. A naive implementation would simply query the actual stack forlocal x
and replace them withlocal y
. Note that this requires iteration through the entire value stack which can be big. For some sequences of Wasm bytecodes this falls apart quickly and would deterioratewasmi
compilation performance. I.e. this would be attackable. In order to safeguardwasmi
against those kinds of attack we instead store occurrences of eachlocal x
into a separate data structure which we simply calledLocalRefs
.Link: https://github.com/paritytech/wasmi/blob/80e1d212a26da8950f11fa6c0812bcc70661c3ee/crates/wasmi/src/engine/regmach/translator/stack/provider.rs#L193
Once we want to replace all stack occurrences of
local x
withlocal y
we instead drain the accumulated occurrences viaLocalRefs
which prevent us from having to iterate through the entire value stack but only adjust those items on the value stack that are actually required to be adjusted.While this works this also comes with the downside of having to maintain this additional data structure and keep it in sync with the value stack which is costly.
Ideally we could optimize the
LocalRefs
data structure further since right now it is quite simple or we could use the cheaper attackable algorith up to a point where we deem it "safe" to do so, e.g. when the value stack is still quite small which usually is the case for non malicious Wasm programs.The text was updated successfully, but these errors were encountered: