near / nearcore

Reference client for NEAR Protocol
https://near.org
GNU General Public License v3.0
2.32k stars 622 forks source link

Implement correct and maintainable WASM instrumentation #6659

Closed matklad closed 1 year ago

matklad commented 2 years ago

Today, we rely on wasm instrumentation for:

This code historically caused trouble for us (bugs/performance issues). The issues here are structural – the code is based on pwasm_utils crate, which, at the moment, is deprecated and replaced with wasm-instrumentation crate upstream. Additionally, there's a feeling that we should build, rather than buy, this code, as it's fine details matter for the definition of the protocol.

There's a different angle of consideration here. Historically, parity's stuff was based on parity's implementation of wasm (pwasm). Since then, bytecodealliance produced an array of useful and well maintained tools for working with WASM. The obvious idea is to use that to implement instrumentation. However , there's a certain misalignment of initiatives here, as bytecodealliance aims only at 99% backwards compatibility, and in general intends to support only the latest version of WASM.

We discussed the following design space

I think there's consensus that we should start wit 3, vendoring instrumentation and using parity_wasm.

Note: here's an example of patch we might want to apply to pwasm_utils: https://github.com/near/wasm-utils/commit/b967faf1aa43c0c8125b529b30806738ae82ccd0


Additional related issues to check out:

nagisa commented 2 years ago

There are some additional concerns with the direct use of wasmparser here. For example we document that a module must not have any more than 100k imports total. Ignoring the fact that other limits such as for the overall contract size would kick in first, it is important to remember this check is gone in the most recent version of the wasmparser crate, and we'd lose this check if we did upgrade for any reason.

While I think we should continue using wasmparser, we probably should not be relying on it to imposing any specific limits (that is – we should have our own limits that are more reasonable for our contract size of 4MiB.) Doing so would also enable us to reduce the dependency on having multiple distinct versions of wasmparser as the protocol evolves.

matklad commented 2 years ago

Yup, that's https://github.com/near/nearcore/issues/6456

matklad commented 2 years ago

And related older discussion: https://github.com/near/nearcore/issues/3191

matklad commented 2 years ago

https://github.com/near/nearcore/pull/6774/ vendors pwasm_utils. Potential follow-ups there:

matklad commented 2 years ago

@nagisa I think the original intention for this issue was to be a tracking issue for finit-wasm related work. Should we tag it as such, or do we have a trcking issue for that work on some other repo?

nagisa commented 2 years ago

I do have a tracking issue for specifically finite-wasm work (spec, analysis, tests) at https://github.com/near/finite-wasm/issues/16 (private repository for the time being.)

You will notice, however, that this tracking issue does not track the implementation work for anything past analysis (that is, the instrumentation of a given blob of WebAssembly code is not included.)

I figured that given recent performance investigation and conclusions, it might not make sense to spend time on implementing something that doesn’t suit our needs super well either way. So for overall work, yes, having this be the overall tracking issue makes sense to me.

nagisa commented 1 year ago

We have largely finished the implementation of finite-wasm a while ago. Completion of this is pending:

jakmeier commented 1 year ago

@nagisa since you just publicized finite-wasm, is it time to close this issue as done?