Files
wuffs/internal/cgen/var.go
T
Nigel Tao b0e9efff3f Avoid (NULL + 0) in derived io_buffer variables
This addresses a "runtime error: applying zero offset to null pointer"
UBSAN (Undefined Behavior Sanitizer) warning using clang 15.0.1:
https://logs.chromium.org/logs/skia/5e005cc1b1981011/+/steps/dm/0/stdout

The offending line of code was "io1_a_dst = io0_a_dst + a_dst->meta.wi"
in wuffs_lzw__decoder__write_to and "io1_a_dst = NULL + 0" was Undefined
Behavior even though io1_a_dst was never dereferenced.

This commit avoids initializing io1_a_dst to (NULL + 0) when io0_a_dst
is NULL, falling back to initializing it with just NULL.

As discussed in https://reviews.llvm.org/D67122 while (nullptr + 0) is
Defined Behavior according to the C++ spec, (NULL + 0) is Undefined
Behavior (by omission) in C11 6.5.6/8: "If both the pointer operand and
the result point to elements of the same array object, or one past the
last element of the array object, the evaluation shall not produce an
overflow; otherwise, the behavior is undefined."

The benchmarks (an excerpt of the full suite is below) seem quite
sensitive to this simple change that's outside of hot loops, sometimes
much better and sometimes much worse. I don't know why.

name                                               old speed       new speed      delta

wuffs_bzip2_decode_10k/clang11                    63.1MB/s ± 0%   58.4MB/s ± 0%   -7.43%  (p=0.008 n=5+5)
wuffs_bzip2_decode_100k/clang11                   49.4MB/s ± 0%   46.2MB/s ± 0%   -6.35%  (p=0.008 n=5+5)

wuffs_bzip2_decode_10k/gcc10                      60.9MB/s ± 0%   56.3MB/s ± 0%   -7.59%  (p=0.016 n=5+4)
wuffs_bzip2_decode_100k/gcc10                     49.6MB/s ± 0%   47.0MB/s ± 0%   -5.15%  (p=0.008 n=5+5)

wuffs_deflate_decode_1k_full_init/clang11          196MB/s ± 0%    195MB/s ± 1%   -0.63%  (p=0.008 n=5+5)
wuffs_deflate_decode_1k_part_init/clang11          226MB/s ± 0%    224MB/s ± 0%   -0.95%  (p=0.008 n=5+5)
wuffs_deflate_decode_10k_full_init/clang11         409MB/s ± 0%    420MB/s ± 0%   +2.84%  (p=0.008 n=5+5)
wuffs_deflate_decode_10k_part_init/clang11         418MB/s ± 0%    431MB/s ± 0%   +2.97%  (p=0.008 n=5+5)
wuffs_deflate_decode_100k_just_one_read/clang11    517MB/s ± 1%    542MB/s ± 0%   +4.78%  (p=0.008 n=5+5)
wuffs_deflate_decode_100k_many_big_reads/clang11   330MB/s ± 0%    338MB/s ± 0%   +2.45%  (p=0.008 n=5+5)

wuffs_deflate_decode_1k_full_init/gcc10            188MB/s ± 0%    177MB/s ± 0%   -5.38%  (p=0.008 n=5+5)
wuffs_deflate_decode_1k_part_init/gcc10            218MB/s ± 0%    209MB/s ± 0%   -4.11%  (p=0.016 n=4+5)
wuffs_deflate_decode_10k_full_init/gcc10           402MB/s ± 0%    407MB/s ± 1%   +1.19%  (p=0.008 n=5+5)
wuffs_deflate_decode_10k_part_init/gcc10           413MB/s ± 0%    419MB/s ± 1%   +1.64%  (p=0.008 n=5+5)
wuffs_deflate_decode_100k_just_one_read/gcc10      520MB/s ± 0%    532MB/s ± 0%   +2.25%  (p=0.008 n=5+5)
wuffs_deflate_decode_100k_many_big_reads/gcc10     330MB/s ± 0%    335MB/s ± 0%   +1.56%  (p=0.008 n=5+5)

wuffs_gif_decode_1k_bw/clang11                     781MB/s ± 0%    645MB/s ± 0%  -17.46%  (p=0.008 n=5+5)
wuffs_gif_decode_1k_color_full_init/clang11        176MB/s ± 0%    181MB/s ± 0%   +3.11%  (p=0.008 n=5+5)
wuffs_gif_decode_1k_color_part_init/clang11        215MB/s ± 0%    223MB/s ± 0%   +3.46%  (p=0.008 n=5+5)
wuffs_gif_decode_10k_bgra/clang11                  786MB/s ± 0%    801MB/s ± 0%   +1.94%  (p=0.008 n=5+5)
wuffs_gif_decode_10k_indexed/clang11               208MB/s ± 0%    211MB/s ± 0%   +1.44%  (p=0.008 n=5+5)
wuffs_gif_decode_20k/clang11                       261MB/s ± 0%    252MB/s ± 0%   -3.43%  (p=0.008 n=5+5)
wuffs_gif_decode_100k_artificial/clang11           578MB/s ± 0%    583MB/s ± 0%   +0.86%  (p=0.008 n=5+5)
wuffs_gif_decode_100k_realistic/clang11            224MB/s ± 0%    223MB/s ± 0%   -0.79%  (p=0.008 n=5+5)
wuffs_gif_decode_1000k_full_init/clang11           229MB/s ± 0%    225MB/s ± 0%   -1.73%  (p=0.008 n=5+5)
wuffs_gif_decode_1000k_part_init/clang11           229MB/s ± 0%    225MB/s ± 0%   -1.69%  (p=0.008 n=5+5)
wuffs_gif_decode_anim_screencap/clang11           1.31GB/s ± 0%   1.29GB/s ± 0%   -1.06%  (p=0.008 n=5+5)

wuffs_gif_decode_1k_bw/gcc10                       650MB/s ± 0%    631MB/s ± 0%   -3.03%  (p=0.008 n=5+5)
wuffs_gif_decode_1k_color_full_init/gcc10          169MB/s ± 0%    168MB/s ± 0%   -0.51%  (p=0.008 n=5+5)
wuffs_gif_decode_1k_color_part_init/gcc10          202MB/s ± 0%    202MB/s ± 0%   -0.18%  (p=0.008 n=5+5)
wuffs_gif_decode_10k_bgra/gcc10                    766MB/s ± 0%    790MB/s ± 0%   +3.08%  (p=0.008 n=5+5)
wuffs_gif_decode_10k_indexed/gcc10                 202MB/s ± 0%    208MB/s ± 0%   +3.16%  (p=0.008 n=5+5)
wuffs_gif_decode_20k/gcc10                         250MB/s ± 0%    258MB/s ± 0%   +3.20%  (p=0.008 n=5+5)
wuffs_gif_decode_100k_artificial/gcc10             563MB/s ± 0%    566MB/s ± 0%   +0.57%  (p=0.008 n=5+5)
wuffs_gif_decode_100k_realistic/gcc10              217MB/s ± 0%    220MB/s ± 0%   +1.49%  (p=0.008 n=5+5)
wuffs_gif_decode_1000k_full_init/gcc10             220MB/s ± 0%    224MB/s ± 0%   +1.49%  (p=0.008 n=5+5)
wuffs_gif_decode_1000k_part_init/gcc10             220MB/s ± 0%    224MB/s ± 0%   +1.43%  (p=0.008 n=5+5)
wuffs_gif_decode_anim_screencap/gcc10             1.30GB/s ± 0%   1.30GB/s ± 0%   +0.34%  (p=0.008 n=5+5)

wuffs_lzw_decode_20k/clang11                       293MB/s ± 0%    292MB/s ± 0%   -0.41%  (p=0.008 n=5+5)
wuffs_lzw_decode_100k/clang11                      486MB/s ± 0%    537MB/s ± 0%  +10.49%  (p=0.008 n=5+5)

wuffs_lzw_decode_20k/gcc10                         258MB/s ± 0%    276MB/s ± 0%   +7.19%  (p=0.016 n=4+5)
wuffs_lzw_decode_100k/gcc10                        512MB/s ± 0%    527MB/s ± 0%   +3.08%  (p=0.016 n=4+5)
2022-10-20 11:18:09 +11:00

378 lines
10 KiB
Go

// Copyright 2017 The Wuffs Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cgen
import (
"errors"
"fmt"
a "github.com/google/wuffs/lang/ast"
t "github.com/google/wuffs/lang/token"
)
var errNeedDerivedVar = errors.New("cgen: internal error: need derived var")
// argsContainsArgsDotFoo returns whether the argNodes, a slice of *ast.Node
// where each ast.Node is an ast.Arg, contains a "name: value" element whose
// value is "args.foo".
func argsContainsArgsDotFoo(argNodes []*a.Node, foo t.ID) bool {
for _, o := range argNodes {
if foo == o.AsArg().Value().IsArgsDotFoo() {
return true
}
}
return false
}
func (g *gen) needDerivedVar(name t.ID) bool {
if name == 0 {
return false
}
for _, o := range g.currFunk.astFunc.Body() {
err := o.Walk(func(p *a.Node) error {
switch p.Kind() {
case a.KExpr:
// Look for p matching "args.name.etc(etc)".
recv, meth, args, ok := p.AsExpr().IsMethodCall()
if !ok {
return nil
}
if recv.IsArgsDotFoo() == name {
return errNeedDerivedVar
}
// Some built-in methods will also need a derived var for their
// arguments.
//
// TODO: use a comprehensive list of such methods.
switch meth {
case t.IDLimitedSwizzleU32InterleavedFromReader,
t.IDSwizzleInterleavedFromReader:
if recv.MType().Eq(typeExprPixelSwizzler) && argsContainsArgsDotFoo(args, name) {
return errNeedDerivedVar
}
}
case a.KIOManip:
if p.AsIOManip().IO().IsArgsDotFoo() == name {
return errNeedDerivedVar
}
}
return nil
})
if err == errNeedDerivedVar {
return true
}
}
return false
}
func (g *gen) findDerivedVars() {
for _, o := range g.currFunk.astFunc.In().Fields() {
o := o.AsField()
if !o.XType().IsIOTokenType() || !g.needDerivedVar(o.Name()) {
continue
}
if g.currFunk.derivedVars == nil {
g.currFunk.derivedVars = map[t.ID]struct{}{}
}
g.currFunk.derivedVars[o.Name()] = struct{}{}
}
}
func (g *gen) derivedVarCNames(typ *a.TypeExpr) (elem string, i1 string, i2 string, isWriter bool, retErr error) {
if typ.Decorator() == 0 {
if qid := typ.QID(); qid[0] == t.IDBase {
switch qid[1] {
case t.IDIOReader:
return "uint8_t", "meta.ri", "meta.wi", false, nil
case t.IDIOWriter:
return "uint8_t", "meta.wi", "data.len", true, nil
case t.IDTokenReader:
return "wuffs_base__token", "meta.ri", "meta.wi", false, nil
case t.IDTokenWriter:
return "wuffs_base__token", "meta.wi", "data.len", true, nil
}
}
}
return "", "", "", false, fmt.Errorf("unsupported derivedVarCNames type %q", typ.Str(g.tm))
}
func (g *gen) writeInitialLoadDerivedVar(b *buffer, n *a.Field) error {
elem, i1, i2, isWriter, err := g.derivedVarCNames(n.XType())
if err != nil {
return err
}
preName := aPrefix + n.Name().Str(g.tm)
c := ""
if !isWriter {
c = "const "
}
b.printf("%s%s* %s%s = NULL;\n", c, elem, iopPrefix, preName)
b.printf("%s%s* %s%s WUFFS_BASE__POTENTIALLY_UNUSED = NULL;\n", c, elem, io0Prefix, preName)
b.printf("%s%s* %s%s WUFFS_BASE__POTENTIALLY_UNUSED = NULL;\n", c, elem, io1Prefix, preName)
b.printf("%s%s* %s%s WUFFS_BASE__POTENTIALLY_UNUSED = NULL;\n", c, elem, io2Prefix, preName)
b.printf("if (%s && %s->data.ptr) {\n", preName, preName)
b.printf("%s%s = %s->data.ptr;\n", io0Prefix, preName, preName)
b.printf("%s%s = %s%s + %s->%s;\n", io1Prefix, preName, io0Prefix, preName, preName, i1)
b.printf("%s%s = %s%s;\n", iopPrefix, preName, io1Prefix, preName)
b.printf("%s%s = %s%s + %s->%s;\n", io2Prefix, preName, io0Prefix, preName, preName, i2)
if isWriter {
b.printf("if (%s->meta.closed) {\n", preName)
b.printf("%s%s = %s%s;\n", io2Prefix, preName, iopPrefix, preName)
b.printf("}\n")
}
b.printf("}\n")
return nil
}
func (g *gen) writeFinalSaveDerivedVar(b *buffer, n *a.Field) error {
_, i1, _, _, err := g.derivedVarCNames(n.XType())
if err != nil {
return err
}
preName := aPrefix + n.Name().Str(g.tm)
b.printf("if (%s && %s->data.ptr) {\n%s->%s = ((size_t)(%s%s - %s->data.ptr));\n}\n",
preName, preName, preName, i1, iopPrefix, preName, preName)
return nil
}
func (g *gen) writeLoadDerivedVar(b *buffer, n *a.Expr) error {
_, i1, _, _, err := g.derivedVarCNames(n.MType())
if err != nil {
return err
}
switch n.Operator() {
case 0:
name := n.Ident().Str(g.tm)
b.printf("%s%s%s = %s%s.data.ptr + %s%s.%s;\n",
iopPrefix, vPrefix, name, uPrefix, name, uPrefix, name, i1)
return nil
case t.IDDot:
if lhs := n.LHS().AsExpr(); (lhs.Operator() == 0) && (lhs.Ident() == t.IDArgs) {
name := n.Ident().Str(g.tm)
b.printf("if (%s%s) {\n", aPrefix, name)
b.printf("%s%s%s = %s%s->data.ptr + %s%s->%s;\n",
iopPrefix, aPrefix, name, aPrefix, name, aPrefix, name, i1)
b.printf("}\n")
return nil
}
}
return fmt.Errorf("could not determine derived variables for %q", n.Str(g.tm))
}
func (g *gen) writeSaveDerivedVar(b *buffer, n *a.Expr) error {
_, i1, _, _, err := g.derivedVarCNames(n.MType())
if err != nil {
return err
}
switch n.Operator() {
case 0:
name := n.Ident().Str(g.tm)
b.printf("%s%s.%s = ((size_t)(%s%s%s - %s%s.data.ptr));\n",
uPrefix, name, i1, iopPrefix, vPrefix, name, uPrefix, name)
return nil
case t.IDDot:
if lhs := n.LHS().AsExpr(); (lhs.Operator() == 0) && (lhs.Ident() == t.IDArgs) {
name := n.Ident().Str(g.tm)
b.printf("if (%s%s) {\n", aPrefix, name)
b.printf("%s%s->%s = ((size_t)(%s%s%s - %s%s->data.ptr));\n",
aPrefix, name, i1, iopPrefix, aPrefix, name, aPrefix, name)
b.printf("}\n")
return nil
}
}
return fmt.Errorf("could not determine derived variables for %q", n.Str(g.tm))
}
func (g *gen) couldHaveDerivedVar(n *a.Expr) bool {
typ := n.MType()
if typ.Decorator() != 0 {
return false
}
qid := typ.QID()
if qid[0] != t.IDBase {
return false
}
switch qid[1] {
case t.IDIOReader, t.IDIOWriter, t.IDTokenReader, t.IDTokenWriter:
// No-op.
default:
return false
}
switch n.Operator() {
case t.IDDot:
if lhs := n.LHS().AsExpr(); (lhs.Operator() == 0) && (lhs.Ident() == t.IDArgs) {
if g.currFunk.derivedVars == nil {
return false
} else if _, ok := g.currFunk.derivedVars[n.Ident()]; !ok {
return false
}
}
case t.IDOpenParen:
switch n.LHS().AsExpr().Ident() {
case t.IDEmptyIOReader, t.IDEmptyIOWriter:
if n.LHS().AsExpr().LHS().AsExpr().MType().IsEtcUtilityType() {
return false
}
}
}
return true
}
func (g *gen) writeLoadExprDerivedVars(b *buffer, n *a.Expr) error {
if (g.currFunk.derivedVars != nil) && (n.Operator() == a.ExprOperatorCall) {
for _, o := range n.Args() {
if v := o.AsArg().Value(); g.couldHaveDerivedVar(v) {
if err := g.writeLoadDerivedVar(b, v); err != nil {
return err
}
}
}
}
return nil
}
func (g *gen) writeSaveExprDerivedVars(b *buffer, n *a.Expr) error {
if (g.currFunk.derivedVars != nil) && (n.Operator() == a.ExprOperatorCall) {
for _, o := range n.Args() {
if v := o.AsArg().Value(); g.couldHaveDerivedVar(v) {
if err := g.writeSaveDerivedVar(b, v); err != nil {
return err
}
}
}
}
return nil
}
func (g *gen) writeResumeSuspend1(b *buffer, f *funk, n *a.Var, suspend bool) error {
if typ := n.XType(); typ.HasPointers() {
return nil
} else if f.varResumables == nil || !f.varResumables[n.Name()] {
return nil
} else {
local := fmt.Sprintf("%s%s", vPrefix, n.Name().Str(g.tm))
lhs := local
// TODO: don't hard-code [0], and allow recursive coroutines.
rhs := fmt.Sprintf("self->private_data.%s%s[0].%s", sPrefix, g.currFunk.astFunc.FuncName().Str(g.tm), lhs)
if suspend {
lhs, rhs = rhs, lhs
}
switch typ.Decorator() {
case 0:
b.printf("%s = %s;\n", lhs, rhs)
return nil
case t.IDArray:
inner := typ.Inner()
if inner.Decorator() != 0 {
break
}
qid := inner.QID()
if qid[0] != t.IDBase {
break
}
switch qid[1] {
case t.IDU8, t.IDU16, t.IDU32, t.IDU64:
b.printf("memcpy(%s, %s, sizeof(%s));\n", lhs, rhs, local)
return nil
}
}
}
return fmt.Errorf("cannot resume or suspend a local variable %q of type %q",
n.Name().Str(g.tm), n.XType().Str(g.tm))
}
func (g *gen) writeResumeSuspend(b *buffer, f *funk, suspend bool) error {
for _, n := range f.varList {
if err := g.writeResumeSuspend1(b, f, n, suspend); err != nil {
return err
}
}
return nil
}
func (g *gen) writeVars(b *buffer, f *funk, inStructDecl bool) error {
for _, n := range f.varList {
typ := n.XType()
if typ.Innermost().IsEtcUtilityType() {
continue
} else if inStructDecl &&
(typ.HasPointers() || f.varResumables == nil || !f.varResumables[n.Name()]) {
continue
}
name := n.Name().Str(g.tm)
if typ.IsIOType() {
b.printf("wuffs_base__io_buffer %s%s = wuffs_base__empty_io_buffer();\n", uPrefix, name)
} else if typ.IsTokenType() {
return fmt.Errorf("TODO: support token_{reader,writer} typed variables")
}
if err := g.writeCTypeName(b, typ, vPrefix, name); err != nil {
return err
}
if inStructDecl {
b.writes(";\n")
} else if typ.IsNumType() {
b.writes(" = 0;\n")
} else if typ.IsBool() {
b.writes(" = false;\n")
} else if typ.IsStatus() {
b.writes(" = wuffs_base__make_status(NULL);\n")
} else if typ.IsIOType() {
b.printf(" = &%s%s;\n", uPrefix, name)
} else if typ.Eq(typeExprARMCRC32U32) {
b.writes(" = 0;\n")
} else {
b.writes(" = {0};\n")
}
if typ.IsIOType() {
qualifier := ""
if typ.QID()[1] == t.IDIOReader {
qualifier = "const "
}
b.printf("%suint8_t* %s%s%s WUFFS_BASE__POTENTIALLY_UNUSED = NULL;\n",
qualifier, iopPrefix, vPrefix, name)
b.printf("%suint8_t* %s%s%s WUFFS_BASE__POTENTIALLY_UNUSED = NULL;\n",
qualifier, io0Prefix, vPrefix, name)
b.printf("%suint8_t* %s%s%s WUFFS_BASE__POTENTIALLY_UNUSED = NULL;\n",
qualifier, io1Prefix, vPrefix, name)
b.printf("%suint8_t* %s%s%s WUFFS_BASE__POTENTIALLY_UNUSED = NULL;\n",
qualifier, io2Prefix, vPrefix, name)
}
}
return nil
}