runtime/loong64: use ABIInternal convention in cgocallbackg

This change follows from CL 765581 and CL 768780.
As expected, the program panics when an unpinned pointed is passed from Go to C.
The stack trace showing an autogenerated frame by the linker.

    # cat -n main.c
         1  #include <stdio.h>
         2  #include "parse.h"
         3
         4  int main(void) {
         5          GoMap m = GoFoo();
         6          (void)m;
         7          return 0;
         8  }

    # cat -n callbackErr.go
         1  package main
         2
         3  import (
         4          "C"
         5  )
         6
         7  //export GoFoo
         8  func GoFoo() map[int]int {
         9          return map[int]int{0: 1,}
        10  }
        11
        12  func main() {
        13  }

    # go build -buildmode=c-shared -o callbackErr.so callbackErr.go
    # gcc -o main main.c callbackErr.so -Wl,-rpath,'$ORIGIN'
    # ./main
    [...]
    goroutine 17 [running, locked to thread]:
    panic({0x7ffff3811460?, 0x3bbf919e000?})
    	/usr/lib/go-1.26/src/runtime/panic.go:879 +0x160
    runtime.cgoCheckArg(0x7ffff38126e0, 0x3bbf9192000, 0x0?, 0x0, 0x1)
    	/usr/lib/go-1.26/src/runtime/cgocall.go:659 +0x4c4
    runtime.cgoCheckResult({0x7ffff38126e0, 0x3bbf9192000})
    	/usr/lib/go-1.26/src/runtime/cgocall.go:829 +0x50
    _cgoexp_f011903c6073_GoFoo(0x7ffffb8059e8)
    	/home/chenguoqi/temp/callbackErr.go:8 +0x80
    runtime.cgocallbackg1(0x7ffff376e570, 0x7ffffb8059e8, 0x0)
    	/usr/lib/go-1.26/src/runtime/cgocall.go:466 +0x29c
    runtime.cgocallbackg(0x7ffff376e570, 0x7ffffb8059e8, 0x0)
    	/usr/lib/go-1.26/src/runtime/cgocall.go:362 +0x108
    runtime.cgocallbackg(0x7ffff376e570, 0x7ffffb8059e8, 0x0)
    	<autogenerated>:1 +0x1c
    runtime.cgocallback(0x0, 0x0, 0x0)
    	/usr/lib/go-1.26/src/runtime/asm_loong64.s:662 +0x9c
    runtime.goexit({})
    	/usr/lib/go-1.26/src/runtime/asm_loong64.s:752 +0x4
    Aborted                    ./main

Load the cgocallbackg arguments into the registers expected by ABIInternal.
    [...]
    goroutine 17 [running, locked to thread]:
    panic({0x7ffff0356d78?, 0x5eae29a9e000?})
    	/home/chenguoqi/golang/go-upstream/src/runtime/panic.go:878 +0x160
    runtime.cgoCheckArg(0x7ffff0351598, 0x5eae29a92000, 0x0?, 0x0, 0x1)
    	/home/chenguoqi/golang/go-upstream/src/runtime/cgocall.go:667 +0x4a4
    runtime.cgoCheckResult({0x7ffff0351598, 0x5eae29a92000})
    	/home/chenguoqi/golang/go-upstream/src/runtime/cgocall.go:837 +0x50
    _cgoexp_f011903c6073_GoFoo(0x7ffffb817928)
    	/home/chenguoqi/temp/callbackErr.go:8 +0x80
    runtime.cgocallbackg1(0x7ffff02ae500, 0x7ffffb817928, 0x0)
    	/home/chenguoqi/golang/go-upstream/src/runtime/cgocall.go:474 +0x2b0
    runtime.cgocallbackg(0x7ffff02ae500, 0x7ffffb817928, 0x0)
    	/home/chenguoqi/golang/go-upstream/src/runtime/cgocall.go:362 +0x104
    runtime.cgocallback(0x0, 0x0, 0x0)
    	/home/chenguoqi/golang/go-upstream/src/runtime/asm_loong64.s:667 +0x94
    runtime.goexit({})
    	/home/chenguoqi/golang/go-upstream/src/runtime/asm_loong64.s:747 +0x4
    Aborted                    ./main

Change-Id: I8d5693fa582f8692b8f9cb022b82917943eaf30e
Reviewed-on: https://go-review.googlesource.com/c/go/+/779740
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
Reviewed-by: Meidan Li <limeidan@loongson.cn>
Reviewed-by: Junyang Shao <shaojunyang@google.com>
LUCI-TryBot-Result: golang-scoped@luci-project-accounts.iam.gserviceaccount.com <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
This commit is contained in:
Guoqi Chen 2026-05-19 20:33:39 +08:00 committed by abner chenc
parent f93504bfd6
commit 8329d31307

View file

@ -656,20 +656,19 @@ havem:
MOVV m_curg(R12), g
JAL runtime·save_g(SB)
MOVV (g_sched+gobuf_sp)(g), R13 // prepare stack as R13
MOVV (g_sched+gobuf_pc)(g), R4
MOVV R4, -(24+8)(R13) // "saved LR"; must match frame size
MOVV fn+0(FP), R5
MOVV frame+8(FP), R6
MOVV ctxt+16(FP), R7
MOVV $-(24+8)(R13), R3
MOVV R5, 8(R3)
MOVV R6, 16(R3)
MOVV R7, 24(R3)
JAL runtime·cgocallbackg(SB)
MOVV (g_sched+gobuf_pc)(g), R7
MOVV R7, -(24+8)(R13) // "saved LR"; must match frame size
MOVV fn+0(FP), R4
MOVV frame+8(FP), R5
MOVV ctxt+16(FP), R6
SUBV $(24+8), R13 // Allocate the same frame size on the g stack
MOVV R13, R3 // switch stack
JAL runtime·cgocallbackg<ABIInternal>(SB)
// Restore g->sched (== m->curg->sched) from saved values.
MOVV 0(R3), R4
MOVV R4, (g_sched+gobuf_pc)(g)
MOVV 0(R3), R7
MOVV R7, (g_sched+gobuf_pc)(g)
MOVV $(24+8)(R3), R13 // must match frame size
MOVV R13, (g_sched+gobuf_sp)(g)