Upgrade dependent version: github.com/open-policy-agent/opa (#5315)

Upgrade dependent version: github.com/open-policy-agent/opa v0.18.0 -> v0.45.0

Signed-off-by: hongzhouzi <hongzhouzi@kubesphere.io>

Signed-off-by: hongzhouzi <hongzhouzi@kubesphere.io>
This commit is contained in:
hongzhouzi
2022-10-31 10:58:55 +08:00
committed by GitHub
parent 668fca1773
commit ef03b1e3df
363 changed files with 277341 additions and 13544 deletions

View File

@@ -65,3 +65,13 @@ const (
Const byte = iota
Mutable
)
// NameSectionCustomID is the ID of the "Name" section Custom Section
const NameSectionCustomID = "name"
// Subtypes of the 'name' custom section
const (
NameSectionModuleType byte = iota
NameSectionFunctionsType
NameSectionLocalsType
)

View File

@@ -9,8 +9,7 @@ import (
"encoding/binary"
"fmt"
"io"
"github.com/pkg/errors"
"io/ioutil"
"github.com/open-policy-agent/opa/internal/leb128"
"github.com/open-policy-agent/opa/internal/wasm/constant"
@@ -26,7 +25,7 @@ func ReadModule(r io.Reader) (*module.Module, error) {
wr := &reader{r: r, n: 0}
module, err := readModule(wr)
if err != nil {
return nil, errors.Wrapf(err, "offset 0x%x", wr.n)
return nil, fmt.Errorf("offset 0x%x: %w", wr.n, err)
}
return module, nil
@@ -38,7 +37,7 @@ func ReadCodeEntry(r io.Reader) (*module.CodeEntry, error) {
wr := &reader{r: r, n: 0}
entry, err := readCodeEntry(wr)
if err != nil {
return nil, errors.Wrapf(err, "offset 0x%x", wr.n)
return nil, fmt.Errorf("offset 0x%x: %w", wr.n, err)
}
return entry, nil
@@ -96,7 +95,7 @@ func readCodeEntry(r io.Reader) (*module.CodeEntry, error) {
var entry module.CodeEntry
if err := readLocals(r, &entry.Func.Locals); err != nil {
return nil, errors.Wrapf(err, "local declarations")
return nil, fmt.Errorf("local declarations: %w", err)
}
return &entry, readExpr(r, &entry.Func.Expr)
@@ -142,43 +141,63 @@ func readSections(r io.Reader, m *module.Module) error {
bufr := bytes.NewReader(buf)
switch id {
case constant.CustomSectionID, constant.StartSectionID, constant.MemorySectionID:
continue
case constant.StartSectionID:
if err := readStartSection(bufr, &m.Start); err != nil {
return fmt.Errorf("start section: %w", err)
}
case constant.CustomSectionID:
var name string
if err := readByteVectorString(bufr, &name); err != nil {
return fmt.Errorf("read custom section type: %w", err)
}
if name == "name" {
if err := readCustomNameSections(bufr, &m.Names); err != nil {
return fmt.Errorf("custom 'name' section: %w", err)
}
} else {
if err := readCustomSection(bufr, name, &m.Customs); err != nil {
return fmt.Errorf("custom section: %w", err)
}
}
case constant.TypeSectionID:
if err := readTypeSection(bufr, &m.Type); err != nil {
return errors.Wrap(err, "type section")
return fmt.Errorf("type section: %w", err)
}
case constant.ImportSectionID:
if err := readImportSection(bufr, &m.Import); err != nil {
return errors.Wrap(err, "import section")
}
case constant.GlobalSectionID:
if err := readGlobalSection(bufr, &m.Global); err != nil {
return errors.Wrap(err, "global section")
return fmt.Errorf("import section: %w", err)
}
case constant.TableSectionID:
if err := readTableSection(bufr, &m.Table); err != nil {
return errors.Wrap(err, "table section")
return fmt.Errorf("table section: %w", err)
}
case constant.MemorySectionID:
if err := readMemorySection(bufr, &m.Memory); err != nil {
return fmt.Errorf("memory section: %w", err)
}
case constant.GlobalSectionID:
if err := readGlobalSection(bufr, &m.Global); err != nil {
return fmt.Errorf("global section: %w", err)
}
case constant.FunctionSectionID:
if err := readFunctionSection(bufr, &m.Function); err != nil {
return errors.Wrap(err, "function section")
return fmt.Errorf("function section: %w", err)
}
case constant.ExportSectionID:
if err := readExportSection(bufr, &m.Export); err != nil {
return errors.Wrap(err, "export section")
return fmt.Errorf("export section: %w", err)
}
case constant.ElementSectionID:
if err := readElementSection(bufr, &m.Element); err != nil {
return errors.Wrap(err, "element section")
return fmt.Errorf("element section: %w", err)
}
case constant.DataSectionID:
if err := readDataSection(bufr, &m.Data); err != nil {
return errors.Wrap(err, "data section")
return fmt.Errorf("data section: %w", err)
}
case constant.CodeSectionID:
if err := readRawCodeSection(bufr, &m.Code); err != nil {
return errors.Wrap(err, "code section")
return fmt.Errorf("code section: %w", err)
}
default:
return fmt.Errorf("illegal section id")
@@ -186,6 +205,121 @@ func readSections(r io.Reader, m *module.Module) error {
}
}
func readCustomSection(r io.Reader, name string, s *[]module.CustomSection) error {
buf, err := ioutil.ReadAll(r)
if err != nil {
return err
}
*s = append(*s, module.CustomSection{
Name: name,
Data: buf,
})
return nil
}
func readCustomNameSections(r io.Reader, s *module.NameSection) error {
for {
id, err := readByte(r)
if err != nil {
if err == io.EOF {
break
}
return err
}
n, err := leb128.ReadVarUint32(r)
if err != nil {
return err
}
buf := make([]byte, n)
if _, err := io.ReadFull(r, buf); err != nil {
return err
}
bufr := bytes.NewReader(buf)
switch id {
case constant.NameSectionModuleType:
err = readNameSectionModule(bufr, s)
case constant.NameSectionFunctionsType:
err = readNameSectionFunctions(bufr, s)
case constant.NameSectionLocalsType:
err = readNameSectionLocals(bufr, s)
}
if err != nil {
return err
}
}
return nil
}
func readNameSectionModule(r io.Reader, s *module.NameSection) error {
return readByteVectorString(r, &s.Module)
}
func readNameSectionFunctions(r io.Reader, s *module.NameSection) error {
nm, err := readNameMap(r)
if err != nil {
return err
}
s.Functions = nm
return nil
}
func readNameMap(r io.Reader) ([]module.NameMap, error) {
n, err := leb128.ReadVarUint32(r)
if err != nil {
return nil, err
}
nm := make([]module.NameMap, n)
for i := uint32(0); i < n; i++ {
var name string
id, err := leb128.ReadVarUint32(r)
if err != nil {
return nil, err
}
if err := readByteVectorString(r, &name); err != nil {
return nil, err
}
nm[i] = module.NameMap{Index: id, Name: name}
}
return nm, nil
}
func readNameSectionLocals(r io.Reader, s *module.NameSection) error {
n, err := leb128.ReadVarUint32(r) // length of vec(indirectnameassoc)
if err != nil {
return err
}
for i := uint32(0); i < n; i++ {
id, err := leb128.ReadVarUint32(r) // func index
if err != nil {
return err
}
nm, err := readNameMap(r)
if err != nil {
return err
}
for _, m := range nm {
s.Locals = append(s.Locals, module.LocalNameMap{
FuncIndex: id,
NameMap: module.NameMap{
Index: m.Index,
Name: m.Name,
}})
}
}
return nil
}
func readStartSection(r io.Reader, s *module.StartSection) error {
idx, err := leb128.ReadVarUint32(r)
if err != nil {
return err
}
s.FuncIndex = &idx
return nil
}
func readTypeSection(r io.Reader, s *module.TypeSection) error {
n, err := leb128.ReadVarUint32(r)
@@ -256,6 +390,27 @@ func readTableSection(r io.Reader, s *module.TableSection) error {
return nil
}
func readMemorySection(r io.Reader, s *module.MemorySection) error {
n, err := leb128.ReadVarUint32(r)
if err != nil {
return err
}
for i := uint32(0); i < n; i++ {
var mem module.Memory
if err := readLimits(r, &mem.Lim); err != nil {
return err
}
s.Memories = append(s.Memories, mem)
}
return nil
}
func readGlobalSection(r io.Reader, s *module.GlobalSection) error {
n, err := leb128.ReadVarUint32(r)
@@ -396,11 +551,7 @@ func readGlobal(r io.Reader, global *module.Global) error {
return fmt.Errorf("illegal mutability flag")
}
if err := readConstantExpr(r, &global.Init); err != nil {
return err
}
return nil
return readConstantExpr(r, &global.Init)
}
func readImport(r io.Reader, imp *module.Import) error {
@@ -517,11 +668,7 @@ func readElementSegment(r io.Reader, seg *module.ElementSegment) error {
return err
}
if err := readVarUint32Vector(r, &seg.Indices); err != nil {
return err
}
return nil
return readVarUint32Vector(r, &seg.Indices)
}
func readDataSegment(r io.Reader, seg *module.DataSegment) error {
@@ -534,11 +681,7 @@ func readDataSegment(r io.Reader, seg *module.DataSegment) error {
return err
}
if err := readByteVector(r, &seg.Init); err != nil {
return err
}
return nil
return readByteVector(r, &seg.Init)
}
func readRawCodeSegment(r io.Reader, seg *module.RawCodeSegment) error {
@@ -616,6 +759,11 @@ func readInstructions(r io.Reader, instrs *[]instruction.Instruction) error {
ret = append(ret, instruction.SetLocal{Index: leb128.MustReadVarUint32(r)})
case opcode.Call:
ret = append(ret, instruction.Call{Index: leb128.MustReadVarUint32(r)})
case opcode.CallIndirect:
ret = append(ret, instruction.CallIndirect{
Index: leb128.MustReadVarUint32(r),
Reserved: mustReadByte(r),
})
case opcode.BrIf:
ret = append(ret, instruction.BrIf{Index: leb128.MustReadVarUint32(r)})
case opcode.Return:
@@ -647,6 +795,14 @@ func readInstructions(r io.Reader, instrs *[]instruction.Instruction) error {
}
}
func mustReadByte(r io.Reader) byte {
b, err := readByte(r)
if err != nil {
panic(err)
}
return b
}
func readLimits(r io.Reader, l *module.Limit) error {
b, err := readByte(r)
@@ -804,6 +960,6 @@ func readBlockValueType(r io.Reader, v *types.ValueType) error {
func readByte(r io.Reader) (byte, error) {
buf := make([]byte, 1)
_, err := r.Read(buf)
_, err := io.ReadFull(r, buf)
return buf[0], err
}

View File

@@ -50,6 +50,10 @@ func WriteModule(w io.Writer, module *module.Module) error {
return err
}
if err := writeMemorySection(w, module.Memory); err != nil {
return err
}
if err := writeGlobalSection(w, module.Global); err != nil {
return err
}
@@ -58,6 +62,10 @@ func WriteModule(w io.Writer, module *module.Module) error {
return err
}
if err := writeStartSection(w, module.Start); err != nil {
return err
}
if err := writeElementSection(w, module.Element); err != nil {
return err
}
@@ -70,6 +78,16 @@ func WriteModule(w io.Writer, module *module.Module) error {
return err
}
if err := writeNameSection(w, module.Names); err != nil {
return err
}
for _, custom := range module.Customs {
if err := writeCustomSection(w, custom); err != nil {
return err
}
}
return nil
}
@@ -102,6 +120,23 @@ func writeVersion(w io.Writer) error {
return binary.Write(w, binary.LittleEndian, constant.Version)
}
func writeStartSection(w io.Writer, s module.StartSection) error {
if s.FuncIndex == nil {
return nil
}
if err := writeByte(w, constant.StartSectionID); err != nil {
return err
}
var buf bytes.Buffer
if err := leb128.WriteVarUint32(&buf, *s.FuncIndex); err != nil {
return err
}
return writeRawSection(w, &buf)
}
func writeTypeSection(w io.Writer, s module.TypeSection) error {
if len(s.Functions) == 0 {
@@ -233,7 +268,31 @@ func writeTableSection(w io.Writer, s module.TableSection) error {
}
return writeRawSection(w, &buf)
}
func writeMemorySection(w io.Writer, s module.MemorySection) error {
if len(s.Memories) == 0 {
return nil
}
if err := writeByte(w, constant.MemorySectionID); err != nil {
return err
}
var buf bytes.Buffer
if err := leb128.WriteVarUint32(&buf, uint32(len(s.Memories))); err != nil {
return err
}
for _, mem := range s.Memories {
if err := writeLimits(&buf, mem.Lim); err != nil {
return err
}
}
return writeRawSection(w, &buf)
}
func writeExportSection(w io.Writer, s module.ExportSection) error {
@@ -370,6 +429,111 @@ func writeDataSection(w io.Writer, s module.DataSection) error {
return writeRawSection(w, &buf)
}
func writeNameSection(w io.Writer, s module.NameSection) error {
if s.Module == "" && len(s.Functions) == 0 && len(s.Locals) == 0 {
return nil
}
if err := writeByte(w, constant.CustomSectionID); err != nil {
return err
}
var buf bytes.Buffer
if err := writeByteVector(&buf, []byte(constant.NameSectionCustomID)); err != nil {
return err
}
// "module" subsection
if s.Module != "" {
if err := writeByte(&buf, constant.NameSectionModuleType); err != nil {
return err
}
var mbuf bytes.Buffer
if err := writeByteVector(&mbuf, []byte(s.Module)); err != nil {
return err
}
if err := writeRawSection(&buf, &mbuf); err != nil {
return err
}
}
// "functions" subsection
if len(s.Functions) != 0 {
if err := writeByte(&buf, constant.NameSectionFunctionsType); err != nil {
return err
}
var fbuf bytes.Buffer
if err := writeNameMap(&fbuf, s.Functions); err != nil {
return err
}
if err := writeRawSection(&buf, &fbuf); err != nil {
return err
}
}
// "locals" subsection
if len(s.Locals) != 0 {
if err := writeByte(&buf, constant.NameSectionLocalsType); err != nil {
return err
}
funs := map[uint32][]module.NameMap{}
for _, e := range s.Locals {
funs[e.FuncIndex] = append(funs[e.FuncIndex], module.NameMap{Index: e.Index, Name: e.Name})
}
var lbuf bytes.Buffer
if err := leb128.WriteVarUint32(&lbuf, uint32(len(funs))); err != nil {
return err
}
for fidx, e := range funs {
if err := leb128.WriteVarUint32(&lbuf, fidx); err != nil {
return err
}
if err := writeNameMap(&lbuf, e); err != nil {
return err
}
}
if err := writeRawSection(&buf, &lbuf); err != nil {
return err
}
}
return writeRawSection(w, &buf)
}
func writeNameMap(buf io.Writer, nm []module.NameMap) error {
if err := leb128.WriteVarUint32(buf, uint32(len(nm))); err != nil {
return err
}
for _, m := range nm {
if err := leb128.WriteVarUint32(buf, m.Index); err != nil {
return err
}
if err := writeByteVector(buf, []byte(m.Name)); err != nil {
return err
}
}
return nil
}
func writeCustomSection(w io.Writer, s module.CustomSection) error {
if err := writeByte(w, constant.CustomSectionID); err != nil {
return err
}
var buf bytes.Buffer
if err := writeByteVector(&buf, []byte(s.Name)); err != nil {
return err
}
if _, err := io.Copy(&buf, bytes.NewReader(s.Data)); err != nil {
return err
}
return writeRawSection(w, &buf)
}
func writeFunctionType(w io.Writer, fsig module.FunctionType) error {
if err := writeByte(w, constant.FunctionTypeID); err != nil {
@@ -446,11 +610,7 @@ func writeGlobal(w io.Writer, global module.Global) error {
return err
}
if err := writeInstructions(w, global.Init.Instrs); err != nil {
return err
}
return nil
return writeInstructions(w, global.Init.Instrs)
}
func writeInstructions(w io.Writer, instrs []instruction.Instruction) error {
@@ -479,6 +639,8 @@ func writeInstructions(w io.Writer, instrs []instruction.Instruction) error {
case float64:
u64 := math.Float64bits(arg)
err = binary.Write(w, binary.LittleEndian, u64)
case byte:
_, err = w.Write([]byte{arg})
default:
return fmt.Errorf("illegal immediate argument type on instruction %d", i)
}

View File

@@ -9,7 +9,12 @@ import (
"github.com/open-policy-agent/opa/internal/wasm/types"
)
// Unreachable reprsents an unreachable opcode.
// !!! If you find yourself adding support for more control
// instructions (br_table, if, ...), please adapt the
// `withControlInstr` functions of
// `compiler/wasm/optimizations.go`
// Unreachable represents a WASM unreachable instruction.
type Unreachable struct {
NoImmediateArgs
}
@@ -51,6 +56,29 @@ func (i Block) Instructions() []Instruction {
return i.Instrs
}
// If represents a WASM if instruction.
// NOTE(sr): we only use if with one branch so far!
type If struct {
NoImmediateArgs
Type *types.ValueType
Instrs []Instruction
}
// Op returns the opcode of the instruction.
func (If) Op() opcode.Opcode {
return opcode.If
}
// BlockType returns the type of the if's THEN branch.
func (i If) BlockType() *types.ValueType {
return i.Type
}
// Instructions represents the instructions contained in the if's THEN branch.
func (i If) Instructions() []Instruction {
return i.Instrs
}
// Loop represents a WASM loop instruction.
type Loop struct {
NoImmediateArgs
@@ -118,6 +146,22 @@ func (i Call) ImmediateArgs() []interface{} {
return []interface{}{i.Index}
}
// CallIndirect represents a WASM call_indirect instruction.
type CallIndirect struct {
Index uint32 // type index
Reserved byte // zero for now
}
// Op returns the opcode of the instruction.
func (CallIndirect) Op() opcode.Opcode {
return opcode.CallIndirect
}
// ImmediateArgs returns the function index.
func (i CallIndirect) ImmediateArgs() []interface{} {
return []interface{}{i.Index, i.Reserved}
}
// Return represents a WASM return instruction.
type Return struct {
NoImmediateArgs

View File

@@ -137,3 +137,63 @@ type I32LeS struct {
func (I32LeS) Op() opcode.Opcode {
return opcode.I32LeS
}
// I32Add represents the WASM i32.add instruction.
type I32Add struct {
NoImmediateArgs
}
// Op returns the opcode of the instruction.
func (I32Add) Op() opcode.Opcode {
return opcode.I32Add
}
// I64Add represents the WASM i64.add instruction.
type I64Add struct {
NoImmediateArgs
}
// Op returns the opcode of the instruction.
func (I64Add) Op() opcode.Opcode {
return opcode.I64Add
}
// F32Add represents the WASM f32.add instruction.
type F32Add struct {
NoImmediateArgs
}
// Op returns the opcode of the instruction.
func (F32Add) Op() opcode.Opcode {
return opcode.F32Add
}
// F64Add represents the WASM f64.add instruction.
type F64Add struct {
NoImmediateArgs
}
// Op returns the opcode of the instruction.
func (F64Add) Op() opcode.Opcode {
return opcode.F64Add
}
// I32Mul represents the WASM i32.mul instruction.
type I32Mul struct {
NoImmediateArgs
}
// Op returns the opcode of the instruction.
func (I32Mul) Op() opcode.Opcode {
return opcode.I32Mul
}
// I32Sub represents the WASM i32.sub instruction.
type I32Sub struct {
NoImmediateArgs
}
// Op returns the opcode of the instruction.
func (I32Sub) Op() opcode.Opcode {
return opcode.I32Sub
}

View File

@@ -0,0 +1,29 @@
// Copyright 2021 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 instruction
import (
"github.com/open-policy-agent/opa/internal/wasm/opcode"
)
// Drop reprsents a WASM drop instruction.
type Drop struct {
NoImmediateArgs
}
// Op returns the opcode of the instruction.
func (Drop) Op() opcode.Opcode {
return opcode.Drop
}
// Select reprsents a WASM select instruction.
type Select struct {
NoImmediateArgs
}
// Op returns the opcode of the instruction.
func (Select) Op() opcode.Opcode {
return opcode.Select
}

View File

@@ -36,3 +36,19 @@ func (SetLocal) Op() opcode.Opcode {
func (i SetLocal) ImmediateArgs() []interface{} {
return []interface{}{i.Index}
}
// TeeLocal represents the WASM tee_local instruction.
type TeeLocal struct {
Index uint32
}
// Op returns the opcode of the instruction.
func (TeeLocal) Op() opcode.Opcode {
return opcode.TeeLocal
}
// ImmediateArgs returns the index of the local variable to "tee" with the top of
// the stack (like set, but retaining the top of the stack).
func (i TeeLocal) ImmediateArgs() []interface{} {
return []interface{}{i.Index}
}

View File

@@ -16,15 +16,24 @@ type (
// Module represents a WASM module.
Module struct {
Version uint32
Start StartSection
Type TypeSection
Import ImportSection
Function FunctionSection
Table TableSection
Memory MemorySection
Element ElementSection
Global GlobalSection
Export ExportSection
Code RawCodeSection
Data DataSection
Customs []CustomSection
Names NameSection
}
// StartSection represents a WASM start section.
StartSection struct {
FuncIndex *uint32
}
// TypeSection represents a WASM type section.
@@ -47,6 +56,11 @@ type (
Tables []Table
}
// MemorySection represents a Wasm memory section.
MemorySection struct {
Memories []Memory
}
// ElementSection represents a WASM element section.
ElementSection struct {
Segments []ElementSegment
@@ -63,7 +77,7 @@ type (
}
// RawCodeSection represents a WASM code section. The code section is left as a
// raw byte sequence. See CodeSection for the decoded version.
// raw byte sequence.
RawCodeSection struct {
Segments []RawCodeSegment
}
@@ -73,6 +87,32 @@ type (
Segments []DataSegment
}
// CustomSection represents a WASM custom section.
CustomSection struct {
Name string
Data []byte
}
// NameSection represents the WASM custom section "name".
NameSection struct {
Module string
Functions []NameMap
Locals []LocalNameMap
}
// NameMap maps function or local arg indices to their names.
NameMap struct {
Index uint32
Name string
}
// LocalNameMap maps function indices, and argument indices for the args
// of the indexed function to their names.
LocalNameMap struct {
FuncIndex uint32
NameMap
}
// FunctionType represents a WASM function type definition.
FunctionType struct {
Params []types.ValueType
@@ -141,6 +181,11 @@ type (
Lim Limit
}
// Memory represents a Wasm memory statement.
Memory struct {
Lim Limit
}
// Global represents a WASM global statement.
Global struct {
Type types.ValueType

View File

@@ -0,0 +1,12 @@
// Copyright 2021 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.
// +build opa_wasm generate
package capabilities
// ABIVersions returns the ABI versions that this SDK supports
func ABIVersions() [][2]int {
return [][2]int{{1, 1}, {1, 2}}
}

View File

@@ -0,0 +1,14 @@
// Copyright 2021 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.
//go:build !opa_wasm && !generate
// +build !opa_wasm,!generate
package capabilities
// ABIVersions returns the supported Wasm ABI versions for this
// build: none
func ABIVersions() [][2]int {
return nil
}

View File

@@ -0,0 +1,18 @@
// Copyright 2020 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 util
// PageSize represents the WASM page size in bytes.
const PageSize = 65535
// Pages converts a byte size to Pages, rounding up as necessary.
func Pages(n uint32) uint32 {
pages := n / PageSize
if pages*PageSize == n {
return pages
}
return pages + 1
}