gcwb.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // +build go1.21,!go1.25
  2. /*
  3. * Copyright 2021 ByteDance Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package rt
  18. import (
  19. `sync/atomic`
  20. `unsafe`
  21. `golang.org/x/arch/x86/x86asm`
  22. )
  23. //go:linkname GcWriteBarrier2 runtime.gcWriteBarrier2
  24. func GcWriteBarrier2()
  25. //go:linkname RuntimeWriteBarrier runtime.writeBarrier
  26. var RuntimeWriteBarrier uintptr
  27. const (
  28. _MaxInstr = 15
  29. )
  30. func isvar(arg x86asm.Arg) bool {
  31. v, ok := arg.(x86asm.Mem)
  32. return ok && v.Base == x86asm.RIP
  33. }
  34. func iszero(arg x86asm.Arg) bool {
  35. v, ok := arg.(x86asm.Imm)
  36. return ok && v == 0
  37. }
  38. func GcwbAddr() uintptr {
  39. var err error
  40. var off uintptr
  41. var ins x86asm.Inst
  42. /* get the function address */
  43. pc := uintptr(0)
  44. fp := FuncAddr(atomic.StorePointer)
  45. /* search within the first 16 instructions */
  46. for i := 0; i < 16; i++ {
  47. mem := unsafe.Pointer(uintptr(fp) + pc)
  48. buf := BytesFrom(mem, _MaxInstr, _MaxInstr)
  49. /* disassemble the instruction */
  50. if ins, err = x86asm.Decode(buf, 64); err != nil {
  51. panic("gcwbaddr: " + err.Error())
  52. }
  53. /* check for a byte comparison with zero */
  54. if ins.Op == x86asm.CMP && ins.MemBytes == 1 && isvar(ins.Args[0]) && iszero(ins.Args[1]) {
  55. off = pc + uintptr(ins.Len) + uintptr(ins.Args[0].(x86asm.Mem).Disp)
  56. break
  57. }
  58. /* move to next instruction */
  59. nb := ins.Len
  60. pc += uintptr(nb)
  61. }
  62. /* check for address */
  63. if off == 0 {
  64. panic("gcwbaddr: could not locate the variable `writeBarrier`")
  65. } else {
  66. return uintptr(fp) + off
  67. }
  68. }