github.com/cloudwego/frugal@v0.1.15/internal/atm/rtx/memzero_test.go (about)

     1  /*
     2   * Copyright 2022 ByteDance Inc.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package rtx
    18  
    19  import (
    20      `fmt`
    21      `math/rand`
    22      `testing`
    23      `unsafe`
    24  
    25      `github.com/cloudwego/frugal/internal/rt`
    26      `github.com/davecgh/go-spew/spew`
    27      `golang.org/x/arch/x86/x86asm`
    28  )
    29  
    30  //go:nosplit
    31  //go:noescape
    32  //goland:noinspection GoUnusedParameter
    33  func callnative1(fn unsafe.Pointer, a0 uintptr)
    34  
    35  func disasmfunc(p unsafe.Pointer) {
    36      pc := uintptr(0)
    37      for {
    38          pp := unsafe.Pointer(uintptr(p) + pc)
    39          ins, err := x86asm.Decode(rt.BytesFrom(pp, 15, 15), 64)
    40          if err != nil {
    41              panic(err)
    42          }
    43          fmt.Printf("%#x(%d) %s\n", uintptr(pp), ins.Len, x86asm.GNUSyntax(ins, uint64(uintptr(pp)), nil))
    44          pc += uintptr(ins.Len)
    45          if ins.Op == x86asm.RET {
    46              break
    47          }
    48      }
    49  }
    50  
    51  func zeromemsized(p unsafe.Pointer, n uintptr) {
    52      callnative1(unsafe.Pointer(uintptr(MemZero.Fn) + MemZero.Sz[n / ZeroStep]), uintptr(p))
    53  }
    54  
    55  func TestMemZero_Clear(t *testing.T) {
    56      mm := make([]byte, 256)
    57      rand.Read(mm)
    58      zeromemsized(unsafe.Pointer(&mm[0]), 48)
    59      spew.Dump(mm)
    60      disasmfunc(MemZero.Fn)
    61  }