Bounds check "if" statements.

This commit is contained in:
Nigel Tao committed 2017-06-06 19:01:48 +10:00
1 parent f686adc3c0
commit 84bf437013
4 files changed
+149 -5

No files matched your search

+30 -1
View File
@@ -596,7 +596,36 @@ func (g *gen) writeStatement(n *a.Node, depth uint32) error {
return nil
case a.KIf:
// TODO, including considering suspendible calls.
// TODO: consider suspendible calls.
n := n.If()
for {
g.writes("if (")
if err := g.writeExpr(n.Condition(), replaceCallSuspendibles, 0); err != nil {
return err
}
g.writes(") {\n")
for _, o := range n.BodyIfTrue() {
if err := g.writeStatement(o, depth); err != nil {
return err
}
}
if bif := n.BodyIfFalse(); len(bif) > 0 {
g.writes("} else {")
for _, o := range bif {
if err := g.writeStatement(o, depth); err != nil {
return err
}
}
break
}
n = n.ElseIf()
if n == nil {
break
}
g.writes("} else ")
}
g.writes("}\n")
return nil
case a.KJump:
n := n.Jump()
+5
View File
@@ -209,10 +209,12 @@ puffs_gif_status puffs_gif_lzw_decoder_decode(puffs_gif_lzw_decoder* self,
uint32_t v_bits;
uint32_t v_n_bits;
uint32_t v_clear_code;
uint32_t v_width;
v_bits = 0;
v_n_bits = 0;
v_clear_code = (((uint32_t)(1)) << self->f_literal_width);
v_width = (self->f_literal_width + 1);
while (1) {
while ((v_n_bits < v_width)) {
@@ -226,6 +228,9 @@ puffs_gif_status puffs_gif_lzw_decoder_decode(puffs_gif_lzw_decoder* self,
}
v_bits >>= v_width;
v_n_bits -= v_width;
if ((v_bits < v_clear_code)) {
goto label_0_break;
}
goto label_0_break;
}
label_0_break:;
+107 -1
View File
@@ -157,7 +157,7 @@ func (q *checker) bcheckStatement(n *a.Node) error {
return q.bcheckAssignment(n.LHS(), n.Operator(), n.RHS())
case a.KIf:
// TODO.
return q.bcheckIf(n.If())
case a.KJump:
// No-op.
@@ -306,6 +306,112 @@ func (q *checker) bcheckAssignment1(lhs *a.Expr, op t.ID, rhs *a.Expr) error {
return nil
}
// terminates returns whether a block of statements terminates. In other words,
// whether the block is non-empty and its final statement is a "return",
// "break", "continue" or an "if-else" chain where all branches terminate.
//
// TODO: strengthen this to include "while" statements? For inspiration, the Go
// spec has https://golang.org/ref/spec#Terminating_statements
func terminates(body []*a.Node) bool {
if len(body) > 0 {
n := body[len(body)-1]
switch n.Kind() {
case a.KReturn, a.KJump:
return true
case a.KIf:
n := n.If()
for {
if !terminates(n.BodyIfTrue()) {
return false
}
if bif := n.BodyIfFalse(); len(bif) > 0 && !terminates(bif) {
return false
}
n = n.ElseIf()
if n == nil {
return true
}
}
}
return false
}
return false
}
func snapshot(facts []*a.Expr) []*a.Expr {
return append([]*a.Expr(nil), facts...)
}
func unify(dst []*a.Expr, branches [][]*a.Expr) ([]*a.Expr, error) {
switch len(branches) {
case 0:
// No-op.
case 1:
dst = append(dst, branches[0]...)
default:
return nil, fmt.Errorf("TODO: unify multiple if branches")
}
return dst, nil
}
func (q *checker) bcheckIf(n *a.If) error {
branches := [][]*a.Expr(nil)
for n != nil {
snap := snapshot(q.facts)
// Check the if condition.
//
// TODO: check that n.Condition() has no side effects.
if _, _, err := q.bcheckExpr(n.Condition(), 0); err != nil {
return err
}
if cv := n.Condition().ConstValue(); cv != nil {
return fmt.Errorf("TODO: bcheckIf for \"if true\" or \"if false\"")
}
// Check the if-true branch, assuming the if condition.
q.facts.appendFact(n.Condition())
for _, o := range n.BodyIfTrue() {
if err := q.bcheckStatement(o); err != nil {
return err
}
}
if !terminates(n.BodyIfTrue()) {
branches = append(branches, snapshot(q.facts))
}
// Check the if-false branch, assuming the inverted if condition.
q.facts = append(q.facts[:0], snap...)
if inverse, err := invert(q.idMap, n.Condition()); err != nil {
return err
} else {
q.facts.appendFact(inverse)
}
if bif := n.BodyIfFalse(); len(bif) > 0 {
for _, o := range bif {
if err := q.bcheckStatement(o); err != nil {
return err
}
}
if !terminates(bif) {
branches = append(branches, snapshot(q.facts))
}
break
}
n = n.ElseIf()
if n == nil {
branches = append(branches, snapshot(q.facts))
break
}
}
u, err := unify(q.facts[:0], branches)
if err != nil {
return err
}
q.facts = u
return nil
}
func (q *checker) bcheckWhile(n *a.While) error {
// Check the pre and inv conditions on entry.
for _, o := range n.Asserts() {
+7 -3
View File
@@ -8,9 +8,8 @@ pub struct lzw_decoder?(
pub func lzw_decoder.decode?(dst ptr buf1, src ptr buf1, src_final bool)() {
var bits u32
var n_bits u32
// TODO: set clear_code, end_code, etc.
var clear_code u32 = (1 as u32) << this.literal_width
// TODO: set end_code.
var width u32[..12] = this.literal_width + 1
// TODO: implement.
@@ -31,6 +30,11 @@ pub func lzw_decoder.decode?(dst ptr buf1, src ptr buf1, src_final bool)() {
bits >>= width
n_bits -= width
// TODO: s/bits/code/
if bits < clear_code {
break
}
// TODO: implement.
break
}