6 Commits

Author SHA1 Message Date
Stephan Renatus 5240170c68 wasm: deal with importing memory in the compiler (#3763)
This sets the stage for eventually allowing OPA wasm modules that do NOT
import memory.

With this change, we add the necessary segments to the wasm module that
declare that memory is to be imported, in the wasm compiler. As far as
LLVM and our C base is concerned, the memory is NOT imported.

The test runners have been adapted, `make wasm-lib-test` works without
having imported memory now. `make wasm-rego-test` can deal with both: it
will provide memory in its `imports` for instantiation, but if the wasm
module happens to not want that import, it'll be ignored. The memory used
in the other host methods is the exported one. (Whether that is exported
or re-exported imported doesn't make a difference.)

Some first steps have been included to make OPA's Wasm SDK work without
imported memory. There are a few loose ends around enforcing memory
limits, to be taken care of later.

----

This also addresses a problem we've seen in the wild before: when our
additions to the wasm modules' data segments exceed the number of pages
needed for the memory import, a "data segment overflowing memory" issue
could have happened. That was because the minimal memory size for the
imported memory was determined by LLVM, and we'd just squeeze our added
data segments in, without adjusting that limit.

Now, the limit will be set properly; and if a too small memory was
provided, a more descriptive failure will happen at instantiation time.
Wasmtime, for example, raises

    incompatible import type for `env::memory`
    Caused by:
        memory types incompatible

Signed-off-by: Stephan Renatus <stephan.renatus@gmail.com>
2021-10-18 14:02:08 +02:00
Stephan Renatus 7fe3ba44f8 wasm: re-enable wasm-lib-test (#3084)
When we've removed the --export-all from the build of opa-test.wasm in
https://github.com/open-policy-agent/opa/pull/3061, we've robbed
wasm/test.js of the ability to see and run them.

Now, the existing tests have been explicitly exported, and the test
runner was adapted to fail if nothing was run at all.

Signed-off-by: Stephan Renatus <stephan.renatus@gmail.com>
2021-01-21 15:08:44 +01:00
Teemu Koponen ee7c3c16cd wasm: Compile the re2 into the library.
Disable all the logging in the library to avoid a dependency to C++
streams of the standard library.

Signed-off-by: Teemu Koponen <koponen@styra.com>
2020-10-28 17:20:13 -04:00
Torin Sandall bcf6bb8f49 wasm: Add support for built-in functions
This commit adds support for built-in functions. Previously there was
no way for the host environment to supply functions that could be
invoked from inside the wasm runtime.

This change updates the wasm library to declare callbacks that can be
invoked by the policy. The host environment should implement the
callbacks by using the first argument to dispatch to appropriate
built-in function implementation. The second callback argument is
reserved for future use. The remaining arguments represent the
operands passed to the call in the policy. This approach is used for
now because it avoids the need to update the function index when
compiling the policy executable which would require relinking the wasm
library object files (because if built-in imports were added
dynamically the function index would be shifted by some number and all
of the call instructions in the wasm library would have to be rewritten.)

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2019-11-17 18:12:39 -05:00
Torin Sandall 581422f041 wasm: Remove memory.grow calls on every malloc
The memory.grow calls were dominating the evaluation time and the
integration tests were beginning to take way too long to run. This
just updates the opa_malloc placeholder to keep track of the heap and
only call memory.grow when needed. We can revisit memory management in
the future but for now this is good enough. The opa_object and opa_set
functions had to be fixed to zero out their members so that the same
memory could be re-used across multiple evals.

Fixes #1121

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2019-10-01 09:08:23 -04:00
Torin Sandall 90bda056b4 Add C library implementing operations for WASM
These changes add a C library that implements low-level data operations
and JSON parsing for WASM policies. The output of the WASM build process
are checked into the repository so that OPA can easily access the
bytecode for test and other purposes.

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2018-10-08 17:29:18 -07:00