Skip to content

Commit

Permalink
Merge pull request #190 from microsoft-golang-bot/auto-merge/microsof…
Browse files Browse the repository at this point in the history
…t/main

Merge upstream `master` into `microsoft/main`
  • Loading branch information
microsoft-golang-review-bot authored Aug 23, 2021
2 parents 09dfeef + ec3d52c commit 54b86ea
Show file tree
Hide file tree
Showing 60 changed files with 746 additions and 398 deletions.
2 changes: 1 addition & 1 deletion src/archive/zip/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func (z *Reader) init(r io.ReaderAt, size int64) error {
// indicate it contains up to 1 << 128 - 1 files. Since each file has a
// header which will be _at least_ 30 bytes we can safely preallocate
// if (data size / 30) >= end.directoryRecords.
if (uint64(size)-end.directorySize)/30 >= end.directoryRecords {
if end.directorySize < uint64(size) && (uint64(size)-end.directorySize)/30 >= end.directoryRecords {
z.File = make([]*File, 0, end.directoryRecords)
}
z.Comment = end.comment
Expand Down
18 changes: 18 additions & 0 deletions src/archive/zip/reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1384,3 +1384,21 @@ func TestCVE202133196(t *testing.T) {
t.Errorf("Archive has unexpected number of files, got %d, want 5", len(r.File))
}
}

func TestCVE202139293(t *testing.T) {
// directory size is so large, that the check in Reader.init
// overflows when subtracting from the archive size, causing
// the pre-allocation check to be bypassed.
data := []byte{
0x50, 0x4b, 0x06, 0x06, 0x05, 0x06, 0x31, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x50, 0x4b,
0x06, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
0x00, 0x00, 0x50, 0x4b, 0x05, 0x06, 0x00, 0x1a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x50, 0x4b,
0x06, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
0x00, 0x00, 0x00, 0x50, 0x4b, 0x05, 0x06, 0x00, 0x31, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff,
0xff, 0x50, 0xfe, 0x00, 0xff, 0x00, 0x3a, 0x00, 0x00, 0x00, 0xff,
}
_, err := NewReader(bytes.NewReader(data), int64(len(data)))
if err != ErrFormat {
t.Fatalf("unexpected error, got: %v, want: %v", err, ErrFormat)
}
}
1 change: 1 addition & 0 deletions src/cmd/compile/internal/base/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ type CmdFlags struct {

// ParseFlags parses the command-line flags into Flag.
func ParseFlags() {
Flag.G = 3
Flag.I = addImportDir

Flag.LowerC = 1
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/compile/internal/importer/iimport.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ func (p *iimporter) posBaseAt(off uint64) *syntax.PosBase {
return posBase
}
filename := p.stringAt(off)
posBase := syntax.NewFileBase(filename)
posBase := syntax.NewTrimmedFileBase(filename, true)
p.posBaseCache[off] = posBase
return posBase
}
Expand Down
26 changes: 18 additions & 8 deletions src/cmd/compile/internal/noder/noder.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,13 +191,23 @@ func (p *noder) errorAt(pos syntax.Pos, format string, args ...interface{}) {
base.ErrorfAt(p.makeXPos(pos), format, args...)
}

// TODO(gri) Can we eliminate fileh in favor of absFilename?
func fileh(name string) string {
return objabi.AbsFile("", name, base.Flag.TrimPath)
}

func absFilename(name string) string {
return objabi.AbsFile(base.Ctxt.Pathname, name, base.Flag.TrimPath)
// trimFilename returns the "trimmed" filename of b, which is the
// absolute filename after applying -trimpath processing. This
// filename form is suitable for use in object files and export data.
//
// If b's filename has already been trimmed (i.e., because it was read
// in from an imported package's export data), then the filename is
// returned unchanged.
func trimFilename(b *syntax.PosBase) string {
filename := b.Filename()
if !b.Trimmed() {
dir := ""
if b.IsFileBase() {
dir = base.Ctxt.Pathname
}
filename = objabi.AbsFile(dir, filename, base.Flag.TrimPath)
}
return filename
}

// noder transforms package syntax's AST into a Node tree.
Expand Down Expand Up @@ -1723,7 +1733,7 @@ func (p *noder) pragma(pos syntax.Pos, blankLine bool, text string, old syntax.P
// (primarily misuse of linker flags), other files are not.
// See golang.org/issue/23672.
func isCgoGeneratedFile(pos syntax.Pos) bool {
return strings.HasPrefix(filepath.Base(filepath.Clean(fileh(pos.Base().Filename()))), "_cgo_")
return strings.HasPrefix(filepath.Base(trimFilename(pos.Base())), "_cgo_")
}

// safeArg reports whether arg is a "safe" command-line argument,
Expand Down
6 changes: 4 additions & 2 deletions src/cmd/compile/internal/noder/posmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,10 @@ func (m *posMap) makeSrcPosBase(b0 *syntax.PosBase) *src.PosBase {
b1, ok := m.bases[b0]
if !ok {
fn := b0.Filename()
absfn := trimFilename(b0)

if b0.IsFileBase() {
b1 = src.NewFileBase(fn, absFilename(fn))
b1 = src.NewFileBase(fn, absfn)
} else {
// line directive base
p0 := b0.Pos()
Expand All @@ -55,7 +57,7 @@ func (m *posMap) makeSrcPosBase(b0 *syntax.PosBase) *src.PosBase {
panic("infinite recursion in makeSrcPosBase")
}
p1 := src.MakePos(m.makeSrcPosBase(p0b), p0.Line(), p0.Col())
b1 = src.NewLinePragmaBase(p1, fn, fileh(fn), b0.Line(), b0.Col())
b1 = src.NewLinePragmaBase(p1, fn, absfn, b0.Line(), b0.Col())
}
if m.bases == nil {
m.bases = make(map[*syntax.PosBase]*src.PosBase)
Expand Down
7 changes: 3 additions & 4 deletions src/cmd/compile/internal/noder/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,16 +194,15 @@ func (pr *pkgReader) posBaseIdx(idx int) *src.PosBase {
r := pr.newReader(relocPosBase, idx, syncPosBase)
var b *src.PosBase

fn := r.string()
absfn := r.string()
filename := r.string()

if r.bool() {
b = src.NewFileBase(fn, absfn)
b = src.NewFileBase(filename, filename)
} else {
pos := r.pos0()
line := r.uint()
col := r.uint()
b = src.NewLinePragmaBase(pos, fn, absfn, line, col)
b = src.NewLinePragmaBase(pos, filename, filename, line, col)
}

pr.posBases[idx] = b
Expand Down
5 changes: 2 additions & 3 deletions src/cmd/compile/internal/noder/reader2.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,15 +109,14 @@ func (pr *pkgReader2) posBaseIdx(idx int) *syntax.PosBase {
var b *syntax.PosBase

filename := r.string()
_ = r.string() // absolute file name

if r.bool() {
b = syntax.NewFileBase(filename)
b = syntax.NewTrimmedFileBase(filename, true)
} else {
pos := r.pos()
line := r.uint()
col := r.uint()
b = syntax.NewLineBase(pos, filename, line, col)
b = syntax.NewLineBase(pos, filename, true, line, col)
}

pr.posBases[idx] = b
Expand Down
6 changes: 1 addition & 5 deletions src/cmd/compile/internal/noder/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,11 +189,7 @@ func (pw *pkgWriter) posBaseIdx(b *syntax.PosBase) int {
w := pw.newWriter(relocPosBase, syncPosBase)
w.p.posBasesIdx[b] = w.idx

// TODO(mdempsky): What exactly does "fileh" do anyway? Is writing
// out both of these strings really the right thing to do here?
fn := b.Filename()
w.string(fn)
w.string(fileh(fn))
w.string(trimFilename(b))

if !w.bool(b.IsFileBase()) {
w.pos(b)
Expand Down
18 changes: 7 additions & 11 deletions src/cmd/compile/internal/reflectdata/reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ var (
// Tracking which types need runtime type descriptor
signatset = make(map[*types.Type]struct{})
// Queue of types wait to be generated runtime type descriptor
signatslice []*types.Type
signatslice []typeAndStr

gcsymmu sync.Mutex // protects gcsymset and gcsymslice
gcsymset = make(map[*types.Type]struct{})
Expand Down Expand Up @@ -1238,21 +1238,16 @@ func NeedRuntimeType(t *types.Type) {
}
if _, ok := signatset[t]; !ok {
signatset[t] = struct{}{}
signatslice = append(signatslice, t)
signatslice = append(signatslice, typeAndStr{t: t, short: types.TypeSymName(t), regular: t.String()})
}
}

func WriteRuntimeTypes() {
// Process signatset. Use a loop, as writeType adds
// entries to signatset while it is being processed.
signats := make([]typeAndStr, len(signatslice))
// Process signatslice. Use a loop, as writeType adds
// entries to signatslice while it is being processed.
for len(signatslice) > 0 {
signats = signats[:0]
// Transfer entries to a slice and sort, for reproducible builds.
for _, t := range signatslice {
signats = append(signats, typeAndStr{t: t, short: types.TypeSymName(t), regular: t.String()})
}
signatslice = signatslice[:0]
signats := signatslice
// Sort for reproducible builds.
sort.Sort(typesByString(signats))
for _, ts := range signats {
t := ts.t
Expand All @@ -1261,6 +1256,7 @@ func WriteRuntimeTypes() {
writeType(types.NewPtr(t))
}
}
signatslice = signatslice[len(signats):]
}

// Emit GC data symbols.
Expand Down
10 changes: 9 additions & 1 deletion src/cmd/compile/internal/ssa/gen/RISCV64.rules
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,10 @@
(BNEZ (SEQZ x) yes no) => (BEQZ x yes no)
(BNEZ (SNEZ x) yes no) => (BNEZ x yes no)

// Absorb NEG into branch when possible.
(BEQZ x:(NEG y) yes no) && x.Uses == 1 => (BEQZ y yes no)
(BNEZ x:(NEG y) yes no) && x.Uses == 1 => (BNEZ y yes no)

// Convert BEQZ/BNEZ into more optimal branch conditions.
(BEQZ (SUB x y) yes no) => (BEQ x y yes no)
(BNEZ (SUB x y) yes no) => (BNE x y yes no)
Expand All @@ -596,11 +600,15 @@
(BEQZ (SLTU x y) yes no) => (BGEU x y yes no)
(BNEZ (SLTU x y) yes no) => (BLTU x y yes no)

// Convert branch with zero to BEQZ/BNEZ.
// Convert branch with zero to more optimal branch zero.
(BEQ (MOVDconst [0]) cond yes no) => (BEQZ cond yes no)
(BEQ cond (MOVDconst [0]) yes no) => (BEQZ cond yes no)
(BNE (MOVDconst [0]) cond yes no) => (BNEZ cond yes no)
(BNE cond (MOVDconst [0]) yes no) => (BNEZ cond yes no)
(BLT (MOVDconst [0]) cond yes no) => (BGTZ cond yes no)
(BLT cond (MOVDconst [0]) yes no) => (BLTZ cond yes no)
(BGE (MOVDconst [0]) cond yes no) => (BLEZ cond yes no)
(BGE cond (MOVDconst [0]) yes no) => (BGEZ cond yes no)

// Store zero
(MOVBstore [off] {sym} ptr (MOVDconst [0]) mem) => (MOVBstorezero [off] {sym} ptr mem)
Expand Down
16 changes: 8 additions & 8 deletions src/cmd/compile/internal/ssa/regalloc.go
Original file line number Diff line number Diff line change
Expand Up @@ -620,20 +620,20 @@ func (s *regAllocState) init(f *Func) {
}
if s.f.Config.ctxt.Flag_dynlink {
switch s.f.Config.arch {
case "amd64":
s.allocatable &^= 1 << 15 // R15
case "arm":
s.allocatable &^= 1 << 9 // R9
case "ppc64le": // R2 already reserved.
// nothing to do
case "arm64":
// nothing to do?
case "386":
// nothing to do.
// Note that for Flag_shared (position independent code)
// we do need to be careful, but that carefulness is hidden
// in the rewrite rules so we always have a free register
// available for global load/stores. See gen/386.rules (search for Flag_shared).
case "amd64":
s.allocatable &^= 1 << 15 // R15
case "arm":
s.allocatable &^= 1 << 9 // R9
case "arm64":
// nothing to do
case "ppc64le": // R2 already reserved.
// nothing to do
case "s390x":
s.allocatable &^= 1 << 11 // R11
default:
Expand Down
70 changes: 70 additions & 0 deletions src/cmd/compile/internal/ssa/rewriteRISCV64.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion src/cmd/compile/internal/syntax/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,13 @@ func (p *parser) updateBase(pos Pos, tline, tcol uint, text string) {
// If we have a column (//line filename:line:col form),
// an empty filename means to use the previous filename.
filename := text[:i-1] // lop off ":line"
trimmed := false
if filename == "" && ok2 {
filename = p.base.Filename()
trimmed = p.base.Trimmed()
}

p.base = NewLineBase(pos, filename, line, col)
p.base = NewLineBase(pos, filename, trimmed, line, col)
}

func commentText(s string) string {
Expand Down
Loading

0 comments on commit 54b86ea

Please sign in to comment.