sha256, AES-GCM and chacha20poly1305 allocate on every call

未关闭
#6,158 4 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
45/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
活跃
技术栈
go

调研方向

Start by reproducing the allocation counts with the supplied test under go test, go test -tags purego, and tinygo test; use TinyGo's -print-allocs output to locate the reported escape sites. The issue names allocation sites in crypto/sha256/sha256.go, crypto/internal/fips140/sha256/sha256.go, crypto/internal/fips140/aes/gcm/gcm_generic.go, and the x/crypto chacha files. Done means the listed TinyGo per-call allocations are reduced to match Go where feasible, with the allocation test confirming the results.

由索引模型根据 Issue 内容生成。

描述

On linux/amd64 these allocate on every call with TinyGo dev (3de4a3a8) and 0.44.0, where Go allocates nothing. Go still allocates nothing with -tags purego, so it is not the assembly.

per call Go Go purego TinyGo
sha256.Sum256 0 0 3
sha256 Reset, Write, Sum on a reused hash 0 0 1
AES-GCM Seal or Open 0 0 6
x/crypto chacha20poly1305 Seal or Open 0 1 6

The counts are the same at -opt=z and -opt=2. -print-allocs names these sites.

crypto/sha256/sha256.go:59:6: object allocated on the heap: escapes at line 12
crypto/internal/fips140/sha256/sha256.go:150:10: object allocated on the heap: escapes at line 152
crypto/internal/fips140/aes/gcm/gcm_generic.go:60:19: object allocated on the heap: escapes at line 62
golang.org/x/crypto/chacha20/chacha_generic.go:76:14: object allocated on the heap: escapes at line 77
golang.org/x/crypto/chacha20poly1305/chacha20poly1305_generic.go:18:7: object allocated on the heap: escapes at unknown line
golang.org/x/crypto/chacha20poly1305/chacha20poly1305_generic.go:25:6: object allocated on the heap: escapes at unknown line
golang.org/x/crypto/chacha20poly1305/chacha20poly1305_generic.go:40:6: object allocated on the heap: escapes at unknown line
golang.org/x/crypto/chacha20poly1305/chacha20poly1305_generic.go:60:6: object allocated on the heap: escapes at unknown line

Most are small local arrays or a value from a constructor such as sha256.New or chacha20.NewUnauthenticatedCipher, which Go keeps on the stack. They sit on the per packet path of anything doing TLS or Noise. We found them profiling a large network server built with TinyGo, where they were a large share of its allocation rate.

Test
package allocpar

import (
	"crypto/aes"
	"crypto/cipher"
	"crypto/sha256"
	"testing"
	"time"

	"golang.org/x/crypto/chacha20poly1305"
)

var sink [32]byte

func report(t *testing.T, name string, f func()) {
	t.Helper()
	t.Logf("%-28s %.1f allocs/op", name, testing.AllocsPerRun(200, f))
}

func TestAllocs(t *testing.T) {
	data := make([]byte, 1400)

	report(t, "sha256.Sum256", func() { sink = sha256.Sum256(data) })

	h := sha256.New()
	report(t, "sha256 Reset+Write+Sum", func() {
		h.Reset()
		h.Write(data)
		h.Sum(sink[:0])
	})

	key := make([]byte, 32)
	aead, err := chacha20poly1305.New(key)
	if err != nil {
		t.Fatal(err)
	}
	nonce := make([]byte, aead.NonceSize())
	sealed := make([]byte, 0, len(data)+aead.Overhead())
	opened := make([]byte, 0, len(data))
	report(t, "chacha20poly1305 Seal", func() { sealed = aead.Seal(sealed[:0], nonce, data, nil) })
	report(t, "chacha20poly1305 Open", func() { opened, _ = aead.Open(opened[:0], nonce, sealed, nil) })

	block, _ := aes.NewCipher(key)
	gcm, _ := cipher.NewGCM(block)
	gnonce := make([]byte, gcm.NonceSize())
	report(t, "AES-GCM Seal", func() { sealed = gcm.Seal(sealed[:0], gnonce, data, nil) })
	report(t, "AES-GCM Open", func() { opened, _ = gcm.Open(opened[:0], gnonce, sealed, nil) })

	tm := time.NewTimer(time.Hour)
	report(t, "Timer.Reset", func() { tm.Reset(time.Hour) })
	tm.Stop()
}

Run with go test -v, go test -tags purego -v and tinygo test -v.

主要语言
Go
星标
17.8k
派生
1.1k
平均合并
1 天 4 小时
30 天内合并 PR
380

环境准备

这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

tinygo-org/tinygo 的其他 Issue

查看 tinygo-org/tinygo 的全部 Issue

相似的 Issue

更多 Go Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。