Source file test/wasmfuncpc.go

     1  // run
     2  
     3  // Copyright 2026 The Go Authors. All rights reserved.
     4  // Use of this source code is governed by a BSD-style
     5  // license that can be found in the LICENSE file.
     6  
     7  //go:build wasm || wasm32
     8  
     9  package main
    10  
    11  import (
    12  	"reflect"
    13  	"runtime"
    14  	"unsafe"
    15  )
    16  
    17  // A runtime method has a low logical PC. Its code pointer must remain valid
    18  // when stack growth adjusts pointers in the caller's frame.
    19  func mapError() (err any) {
    20  	defer func() { err = recover() }()
    21  	var m map[any]int
    22  	_ = m[[]int{}]
    23  	return
    24  }
    25  
    26  //go:noinline
    27  func grow(n int, p unsafe.Pointer) {
    28  	var pad [512]byte
    29  	pad[n%len(pad)] = byte(n)
    30  	if n > 0 {
    31  		grow(n-1, p)
    32  	}
    33  	runtime.KeepAlive(p)
    34  	runtime.KeepAlive(&pad)
    35  }
    36  
    37  func main() {
    38  	m, ok := reflect.TypeOf(mapError()).MethodByName("Error")
    39  	if !ok {
    40  		panic("missing Error method")
    41  	}
    42  	p := m.Func.UnsafePointer()
    43  	if runtime.FuncForPC(uintptr(p)) == nil {
    44  		panic("missing function for method PC")
    45  	}
    46  	grow(1000, p)
    47  	runtime.KeepAlive(p)
    48  }
    49  

View as plain text