mirror of
https://github.com/open-policy-agent/opa.git
synced 2026-08-13 03:42:35 -06:00
2e122c9e69
* wasm: read/write custom sections (incl. dwarf)
I've extended the roundtrip OPA test to have some assertions
on custom sections that work for both debug and non-debug
wasm builds.
Without debugging enabled, we had still been stripping the
`names` and `producers` sections. This changes therefore
increases the size of wasm modules. For a simple policy,
package test
default allow = false
allow {
input.foo == "bar"
}
the size generated is 544K (master) vs 620K (this commit).
With debugging enabled (and the opa binary rebuilt), the
size becomes 6.1M.
* wasm/encoding: treat 'name' custom section separately
This roundtrips module, functions, and locals, as per
https://webassembly.github.io/spec/core/appendix/custom.html#name-section
There is currently no use of module and locals, afaict.
* compiler/wasm: record function names in 'name' custom section
Quality-of-life improvement when dealing with our generated WASM
code.
Before:
local.get 2
local.get 3
call 1172
local.set 6
After:
local.get 2
local.get 3
call $g0.data.foo.p
local.set 6
Signed-off-by: Stephan Renatus <stephan.renatus@gmail.com>
115 lines
2.7 KiB
Go
115 lines
2.7 KiB
Go
// Copyright 2018 The OPA Authors. All rights reserved.
|
|
// Use of this source code is governed by an Apache2
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package encoding
|
|
|
|
import (
|
|
"bytes"
|
|
"io/ioutil"
|
|
"path/filepath"
|
|
"reflect"
|
|
"testing"
|
|
|
|
"github.com/open-policy-agent/opa/internal/compiler/wasm/opa"
|
|
"github.com/open-policy-agent/opa/internal/wasm/module"
|
|
)
|
|
|
|
func TestRoundtrip(t *testing.T) {
|
|
|
|
bs, err := ioutil.ReadFile(filepath.Join("testdata", "test1.wasm"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
module, err := ReadModule(bytes.NewBuffer(bs))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
entries, err := CodeEntries(module)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
for i, e := range entries {
|
|
|
|
var buf3 bytes.Buffer
|
|
|
|
if err := WriteCodeEntry(&buf3, e); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
module.Code.Segments[i].Code = buf3.Bytes()
|
|
}
|
|
|
|
var buf2 bytes.Buffer
|
|
|
|
if err := WriteModule(&buf2, module); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
module2, err := ReadModule(&buf2)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
// TODO(tsandall): how to make this more debuggable
|
|
if !reflect.DeepEqual(module, module2) {
|
|
t.Fatal("modules are not equal")
|
|
}
|
|
|
|
}
|
|
|
|
func TestRoundtripOPA(t *testing.T) {
|
|
|
|
bs, err := opa.Bytes()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
module1, err := ReadModule(bytes.NewBuffer(bs))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
// When using a WASM module with or without debug, the custom sections differ.
|
|
// Both variants have 'producers'.
|
|
customSections := map[string]int{}
|
|
for _, s := range module1.Customs {
|
|
customSections[s.Name]++
|
|
}
|
|
if expected, actual := 1, customSections["producers"]; expected != actual {
|
|
t.Errorf("expected %d 'producers' custom sections, found %d", expected, actual)
|
|
}
|
|
if len(module1.Names.Functions) == 0 {
|
|
t.Errorf("expected non-zero function names in 'name' custom sections")
|
|
}
|
|
|
|
// Note(sr): We don't have this set by any other means, so manually set it, and
|
|
// check the write->read roundtrip at least.
|
|
module1.Names.Module = "foo"
|
|
module1.Names.Locals = []module.LocalNameMap{{FuncIndex: 1172, NameMap: module.NameMap{Index: 0, Name: "data"}}}
|
|
|
|
// TODO(tsandall): when all instructions are handled by reader, add logic to
|
|
// check code section contents.
|
|
|
|
var buf2 bytes.Buffer
|
|
if err := WriteModule(&buf2, module1); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
module2, err := ReadModule(&buf2)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if expected, actual := len(module1.Names.Functions), len(module2.Names.Functions); expected != actual {
|
|
t.Errorf("expected %d function names in 'name' custom sections, found %d", expected, actual)
|
|
}
|
|
|
|
// TODO(tsandall): how to make this more debuggable
|
|
if !reflect.DeepEqual(module1, module2) {
|
|
t.Fatal("modules are not equal")
|
|
}
|
|
}
|