government / program / program.go

@ 21ba553f2ac9aed7435e6a7744c2a33c37c9dec8 | history


// Package program is the whole path from a .gov file to something the
// evaluator can run: ratify the chain, rebuild the policy graph, decode it
// into a tree, and turn the tree into values.
//
// It is also the trust boundary. Everything before Load returns is checking
// untrusted bytes; everything after it runs code that passed every check.
package program

import (
	"fmt"
	"io"
	"time"

	"government/ast"
	"government/eval"
	"government/policy"
	"government/ratify"
	"government/sexpr"
)

// ErrDecode: the policy graph is valid but does not decode to a program,
// e.g. a list node with no elements.
const ErrDecode = "E-DECODE"

type Error struct {
	Code string
	Msg  string
}

func (e *Error) Error() string { return e.Code + ": " + e.Msg }

type Options struct {
	At       time.Time
	Pin      ratify.Pin
	MaxNodes int
}

// Program is a ratified, decoded program.
type Program struct {
	Forms   []sexpr.Value
	origins ast.Origins
	depth   map[*ast.Node]int
}

// Load runs every check on a .gov file and decodes it. Any error it returns
// means the program may not run: a ratify, policy or decode error.
func Load(data []byte, opts Options) (*Program, error) {
	certs, err := ratify.Ratify(data, ratify.Options{At: opts.At, Pin: opts.Pin})
	if err != nil {
		return nil, err
	}
	g, err := policy.Program(certs, policy.Options{MaxNodes: opts.MaxNodes})
	if err != nil {
		return nil, err
	}
	layers, err := g.Layers()
	if err != nil {
		return nil, &Error{ErrDecode, err.Error()}
	}
	tree, err := ast.Unlayer(layers)
	if err != nil {
		return nil, &Error{ErrDecode, err.Error()}
	}
	forms, origins, err := ast.FromTree(tree)
	if err != nil {
		return nil, &Error{ErrDecode, err.Error()}
	}
	p := &Program{Forms: forms, origins: origins, depth: map[*ast.Node]int{}}
	var walk func(n *ast.Node, d int)
	walk = func(n *ast.Node, d int) {
		p.depth[n] = d
		for _, c := range n.Children {
			walk(c, d+1)
		}
	}
	for _, f := range tree.Forms {
		walk(f, 1)
	}
	return p, nil
}

// Locate names where an evaluation error happened: the OID of the
// innermost list being evaluated and its depth, which is the cert that
// holds it. A .gov program has no line numbers; this is its equivalent.
func (p *Program) Locate(err error) string {
	e, ok := err.(*eval.Error)
	if !ok {
		return ""
	}
	pair, ok := e.Expr.(*sexpr.Pair)
	if !ok {
		return ""
	}
	n, ok := p.origins[pair]
	if !ok {
		return ""
	}
	return fmt.Sprintf("%s (depth %d)", n, p.depth[n])
}

// Run evaluates the forms in order, printing the value of each one that is
// not a define, and stops at the first error.
func (p *Program) Run(out io.Writer) error {
	top := eval.NewTopLevel()
	for _, form := range p.Forms {
		v, err := top.Eval(form)
		if err != nil {
			return err
		}
		if !IsDefine(form) {
			fmt.Fprintln(out, sexpr.Print(v))
		}
	}
	return nil
}

// Describe formats an evaluation error with its location, as `government
// run` prints it.
func (p *Program) Describe(err error) string {
	if where := p.Locate(err); where != "" {
		return err.Error() + " at " + where
	}
	return err.Error()
}

func IsDefine(form sexpr.Value) bool {
	pair, ok := form.(*sexpr.Pair)
	return ok && pair.Car == sexpr.Value(sexpr.Symbol("define"))
}