Source file src/runtime/gobuf_wasm32.go

     1  // Copyright 2026 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  //go:build wasm32
     6  
     7  package runtime
     8  
     9  import (
    10  	"internal/abi"
    11  	"internal/runtime/sys"
    12  	"unsafe"
    13  )
    14  
    15  func wasmFuncEntryAddr() uintptr
    16  func wasmFuncCount() uintptr
    17  func wasmGoexitHandle() uintptr
    18  
    19  //go:linkname reflect_wasm32FuncHandleToPC reflect.wasm32FuncHandleToPC
    20  func reflect_wasm32FuncHandleToPC(handle uintptr) uintptr {
    21  	return funcValuePC(handle)
    22  }
    23  
    24  //go:linkname reflect_wasm32FuncPCToHandle reflect.wasm32FuncPCToHandle
    25  func reflect_wasm32FuncPCToHandle(pc uintptr) uintptr {
    26  	return funcHandle(pc)
    27  }
    28  
    29  // funcValuePC returns the logical entry PC for a call_indirect handle by
    30  // indexing runtime.wasmFuncEntry, a linker-generated table of uint32 entry PCs.
    31  // Handle 0 is nil and maps to entry 0, which is zero.
    32  //
    33  //go:nosplit
    34  func funcValuePC(handle uintptr) uintptr {
    35  	if handle > wasmFuncCount() {
    36  		throw("invalid wasm32 function handle")
    37  	}
    38  	base := wasmFuncEntryAddr()
    39  	return uintptr(*(*uint32)(unsafe.Pointer(base + handle*4)))
    40  }
    41  
    42  // funcHandle returns the call_indirect handle for the function containing pc.
    43  // PC 0 maps to handle 0. Otherwise, funcHandle calls findfunc to get the
    44  // containing function's entry PC and binary-searches runtime.wasmFuncEntry,
    45  // which is ordered by entry PC.
    46  //
    47  //go:nosplit
    48  func funcHandle(pc uintptr) uintptr {
    49  	if pc == 0 {
    50  		return 0
    51  	}
    52  	f := findfunc(pc)
    53  	if !f.valid() {
    54  		throw("invalid wasm32 function PC")
    55  	}
    56  	entry := f.entry()
    57  
    58  	count := wasmFuncCount()
    59  	base := wasmFuncEntryAddr()
    60  	lo, hi := uintptr(1), count+1
    61  	for lo < hi {
    62  		mid := lo + (hi-lo)/2
    63  		midPC := uintptr(*(*uint32)(unsafe.Pointer(base + mid*4)))
    64  		if midPC < entry {
    65  			lo = mid + 1
    66  		} else {
    67  			hi = mid
    68  		}
    69  	}
    70  	if lo > count || uintptr(*(*uint32)(unsafe.Pointer(base + lo*4))) != entry {
    71  		throw("missing wasm32 function handle")
    72  	}
    73  	return lo
    74  }
    75  
    76  // gobufSetPC sets a wasm32 continuation. gobuf.lr is the PC_F handle paired
    77  // with gobuf.pc, not a link register. Assembly paths that copy an existing
    78  // continuation copy both fields directly.
    79  //
    80  //go:nosplit
    81  func gobufSetPC(buf *gobuf, pc uintptr) {
    82  	buf.pc = pc
    83  	buf.lr = funcHandle(pc)
    84  }
    85  
    86  // gobufSetGoexit leaves PC-1 within goexit for stack unwinding and uses
    87  // the static handle instead of looking up a known PC.
    88  //
    89  //go:nosplit
    90  func gobufSetGoexit(buf *gobuf) {
    91  	buf.pc = abi.FuncPCABI0(goexit) + sys.PCQuantum
    92  	buf.lr = wasmGoexitHandle()
    93  }
    94  

View as plain text