Skip to content

Instantly share code, notes, and snippets.

@methane
Created December 27, 2018 00:02
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save methane/b61dcfb504d54de5bced1c6e3209a91d to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"runtime"
"sync"
"unsafe"
)
var stopC = make(chan struct{})
// stress starts GC repeatedly to enable write barrier.
func stress() {
for {
select {
case <-stopC:
return
default:
runtime.GC()
}
}
}
var wg sync.WaitGroup
var throttle = make(chan struct{}, 1)
//go:noinline
func noPointerArgs(a, b, c, d uint64) {
<-throttle
wg.Done()
}
func main() {
// make large heap to make write barrier longer
x := make(map[string]string)
for i := 0; i < 10000; i++ {
x[fmt.Sprintf("k%v", i)] = fmt.Sprintf("v%v", i)
}
// enable write barrier.
go stress()
// build integer looks like bad pointer. amd64 specific.
y := uint64(uintptr(unsafe.Pointer(&x)))
fmt.Printf("%x -> ", y)
y |= 0x3000000
fmt.Printf("%x\n", y)
// run go func()
wg.Add(10000000)
for i := 0; i < 10000000; i++ {
throttle <- struct{}{}
go noPointerArgs(y, y, y, y)
}
wg.Wait()
close(stopC)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment