diff options
author | Michael Anthony Knyszek <mknyszek@google.com> | 2021-06-11 13:58:05 +0000 |
---|---|---|
committer | Michael Knyszek <mknyszek@google.com> | 2021-06-12 00:12:55 +0000 |
commit | 9d46ee5ac4acd6602692f70c5149a3f6db058558 (patch) | |
tree | 74ab00cf909c6f544c8a1f3336a16a0ab8dc8bcb /src/reflect/value.go | |
parent | e552a6d31270c86064632af1d092e0db5a930250 (diff) | |
download | go-9d46ee5ac4acd6602692f70c5149a3f6db058558.tar.gz go-9d46ee5ac4acd6602692f70c5149a3f6db058558.zip |
reflect: handle stack-to-register translation in callMethod
callMethod previously assumed erroneously that between the "value" and
"method" ABIs (that is, the ABI the caller is following to call this
method value and the actual ABI of the method), it could never happen
that an argument passed on the stack in the former could be passed in
registers in the latter. The cited reason was that the latter always
uses strictly more registers.
However, there are situations where the value ABI could pass a value on
the stack, but later is passed in a register. For instance, if the
receiver pushes a value passed in registers that uses multiple registers
to be passed on the stack, later arguments which were passed on the
stack may now be passed in registers.
This change fixes callMethod to no longer makes this assumption, and
handles the stack-to-register translation explicitly.
Fixes #46696.
Change-Id: I7100a664d97bbe401302cc893b3a98b28cdcdfc0
Reviewed-on: https://go-review.googlesource.com/c/go/+/327089
Trust: Michael Knyszek <mknyszek@google.com>
Run-TryBot: Michael Knyszek <mknyszek@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Cherry Mui <cherryyz@google.com>
Diffstat (limited to 'src/reflect/value.go')
-rw-r--r-- | src/reflect/value.go | 42 |
1 files changed, 32 insertions, 10 deletions
diff --git a/src/reflect/value.go b/src/reflect/value.go index 418dff781f..c963a407bc 100644 --- a/src/reflect/value.go +++ b/src/reflect/value.go @@ -952,25 +952,47 @@ func callMethod(ctxt *methodValue, frame unsafe.Pointer, retValid *bool, regs *a continue } - // There are three cases to handle in translating each + // There are four cases to handle in translating each // argument: // 1. Stack -> stack translation. - // 2. Registers -> stack translation. - // 3. Registers -> registers translation. - // The fourth cases can't happen, because a method value - // call uses strictly fewer registers than a method call. + // 2. Stack -> registers translation. + // 3. Registers -> stack translation. + // 4. Registers -> registers translation. + // TODO(mknyszek): Cases 2 and 3 below only work on little endian + // architectures. This is OK for now, but this needs to be fixed + // before supporting the register ABI on big endian architectures. // If the value ABI passes the value on the stack, // then the method ABI does too, because it has strictly // fewer arguments. Simply copy between the two. if vStep := valueSteps[0]; vStep.kind == abiStepStack { mStep := methodSteps[0] - if mStep.kind != abiStepStack || vStep.size != mStep.size { - panic("method ABI and value ABI do not align") + // Handle stack -> stack translation. + if mStep.kind == abiStepStack { + if vStep.size != mStep.size { + panic("method ABI and value ABI do not align") + } + typedmemmove(t, + add(methodFrame, mStep.stkOff, "precomputed stack offset"), + add(valueFrame, vStep.stkOff, "precomputed stack offset")) + continue + } + // Handle stack -> register translation. + for _, mStep := range methodSteps { + from := add(valueFrame, vStep.stkOff+mStep.offset, "precomputed stack offset") + switch mStep.kind { + case abiStepPointer: + // Do the pointer copy directly so we get a write barrier. + methodRegs.Ptrs[mStep.ireg] = *(*unsafe.Pointer)(from) + fallthrough // We need to make sure this ends up in Ints, too. + case abiStepIntReg: + memmove(unsafe.Pointer(&methodRegs.Ints[mStep.ireg]), from, mStep.size) + case abiStepFloatReg: + memmove(unsafe.Pointer(&methodRegs.Floats[mStep.freg]), from, mStep.size) + default: + panic("unexpected method step") + } } - typedmemmove(t, - add(methodFrame, mStep.stkOff, "precomputed stack offset"), - add(valueFrame, vStep.stkOff, "precomputed stack offset")) continue } // Handle register -> stack translation. |