From df1d4f64e2c5f1dee5bc2bf98e06034b550b3a11 Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Sat, 4 Nov 2023 17:26:40 +1100 Subject: [PATCH] check: ban recursive function calls --- doc/changelog.md | 1 + lang/check/check.go | 104 ++++++++++++++++++++++++++++++++++++++++++++ lang/token/list.go | 4 ++ 3 files changed, 109 insertions(+) diff --git a/doc/changelog.md b/doc/changelog.md index 3554b812..a9d96ebe 100644 --- a/doc/changelog.md +++ b/doc/changelog.md @@ -23,6 +23,7 @@ The LICENSE has changed from a single license (Apache 2) to a dual license - Added `std/xxhash32`. - Added `std/xxhash64`. - Added `wuffs_base__status__is_truncated_input_error`. +- Ban recursive function calls. - Changed `lzw.set_literal_width` to `lzw.set_quirk`. - Changed `set_quirk_enabled!(quirk: u32, enabled: bool)` to `set_quirk!(key: u32, value: u64) status`. diff --git a/lang/check/check.go b/lang/check/check.go index 8dc1776f..42ea6159 100644 --- a/lang/check/check.go +++ b/lang/check/check.go @@ -98,6 +98,9 @@ func Check(tm *t.Map, files []*a.File, resolveUse func(usePath string) ([]byte, builtInInterfaces: map[t.QID][]t.QQID{}, builtInInterfaceFuncs: map[t.QQID]*a.Func{}, unseenInterfaceImpls: map[t.QQID]*a.Func{}, + + chooseAlternatives: map[t.QID][]t.ID{}, + noRecursiveMarks: map[t.QID]uint8{}, } for _, funcs := range builtin.Funcs { @@ -199,10 +202,12 @@ var phases = [...]struct { {a.KStruct, (*Checker).checkStructDecl}, {a.KInvalid, (*Checker).checkStructCycles}, {a.KStruct, (*Checker).checkStructFields}, + {a.KFunc, (*Checker).gatherChooseAlternatives}, {a.KFunc, (*Checker).checkFuncSignature}, {a.KFunc, (*Checker).checkFuncContract}, {a.KFunc, (*Checker).checkFuncImplements}, {a.KFunc, (*Checker).checkFuncBody}, + {a.KFunc, (*Checker).checkNoRecursiveFuncs}, {a.KInvalid, (*Checker).checkInterfacesSatisfied}, {a.KStruct, (*Checker).checkFieldMethodCollisions}, {a.KInvalid, (*Checker).checkAllTypeChecked}, @@ -243,6 +248,9 @@ type Checker struct { builtInInterfaceFuncs map[t.QQID]*a.Func unseenInterfaceImpls map[t.QQID]*a.Func + chooseAlternatives map[t.QID][]t.ID + noRecursiveMarks map[t.QID]uint8 + unsortedStructs []*a.Struct } @@ -563,6 +571,27 @@ func checkTypeExpr(q *checker, n *a.TypeExpr) error { return nil } +func (c *Checker) gatherChooseAlternatives(node *a.Node) error { + n := node.AsFunc() + return node.Walk(func(innerNode *a.Node) error { + if innerNode.Kind() == a.KChoose { + o := innerNode.AsChoose() + receiverType := n.Receiver()[1] + oQID := t.QID{receiverType, o.Name()} + chooseAlternatives := c.chooseAlternatives[oQID] + for _, p := range o.Args() { + p := p.AsExpr() + pQID := t.QID{receiverType, p.Ident()} + if oQID[1] != pQID[1] { + chooseAlternatives = append(chooseAlternatives, pQID[1]) + } + } + c.chooseAlternatives[oQID] = chooseAlternatives + } + return nil + }) +} + func (c *Checker) checkFuncSignature(node *a.Node) error { return c.checkFuncSignature1(node, true) } @@ -760,6 +789,81 @@ func (c *Checker) checkFuncBody(node *a.Node) error { return nil } +// checkNoRecursiveFuncs essentially performs a topological sort (via a +// depth-first search), ordering bar and baz functions on whether bar's body +// (or asserts) calls the this.baz method. +// +// https://en.wikipedia.org/wiki/Topological_sorting#Depth-first_search +func (c *Checker) checkNoRecursiveFuncs(node *a.Node) error { + _, err := c.checkNoRecursiveFuncs1(nil, node.AsFunc()) + return err +} + +func (c *Checker) checkNoRecursiveFuncs1(dst []t.QID, n *a.Func) ([]t.QID, error) { + nQID := n.QQID().QIDSuffix() + dst = append(dst, nQID) + + const ( + unmarked = 0 + temporary = 1 + permanent = 2 + ) + switch c.noRecursiveMarks[nQID] { + case temporary: + hidden, names := true, make([]string, 0, len(dst)) + for _, qid := range dst { + if hidden { + if qid != nQID { + continue + } + hidden = false + } + names = append(names, qid.Str(c.tm)) + } + return nil, fmt.Errorf("check: recursive call chain: %s", names) + case permanent: + return dst, nil + } + c.noRecursiveMarks[nQID] = temporary + + if err := n.AsNode().Walk(func(innerNode *a.Node) error { + if innerNode.Kind() != a.KExpr { + return nil + } + o := innerNode.AsExpr() + if o.Operator() != t.IDOpenParen { + return nil + } + foo := o.LHS().AsExpr().IsThisDotFoo() + if foo == 0 { + return nil + } + qqid := n.QQID() + qqid[2] = foo + + for _, altID := range c.chooseAlternatives[qqid.QIDSuffix()] { + altQQID := t.QQID{qqid[0], qqid[1], altID} + dst1, err1 := c.checkNoRecursiveFuncs1(dst, c.funcs[altQQID]) + if err1 != nil { + return err1 + } + dst = dst1 + } + + dst1, err1 := c.checkNoRecursiveFuncs1(dst, c.funcs[qqid]) + if err1 != nil { + return err1 + } + dst = dst1 + return nil + }); err != nil { + return nil, err + } + + c.noRecursiveMarks[nQID] = permanent + return dst[:len(dst)-1], nil +} + func (c *Checker) checkInterfacesSatisfied(node *a.Node) error { if len(c.unseenInterfaceImpls) == 0 { return nil diff --git a/lang/token/list.go b/lang/token/list.go index 3994ccd0..4569406f 100644 --- a/lang/token/list.go +++ b/lang/token/list.go @@ -192,6 +192,10 @@ func (x QQID) Str(m *Map) string { return m.ByID(x[2]) } +func (x QQID) QIDSuffix() QID { + return QID{x[1], x[2]} +} + // Token combines an ID and the line number it was seen. type Token struct { ID ID