internal/impl: fix non-atomic access of MessageState.mi

MessageState's mi field must only be accessed via atomic operations.
Rename the field to 'atomicMessageInfo' to make it incorrect access
obvious. Fix one incorrect non-atomic access.

Change-Id: If80343fb1b82186416f007ca5b340a4926b1cd3c
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/221419
Reviewed-by: Joe Tsai <joetsai@google.com>
This commit is contained in:
Damien Neil 2020-02-27 12:33:08 -08:00
parent a786db3a49
commit 075e0741aa
3 changed files with 5 additions and 5 deletions

View File

@ -100,9 +100,9 @@ func (mi *MessageInfo) initOnce() {
func (mi *MessageInfo) getPointer(m pref.Message) (p pointer, ok bool) {
switch m := m.(type) {
case *messageState:
return m.pointer(), m.mi == mi
return m.pointer(), m.messageInfo() == mi
case *messageReflectWrapper:
return m.pointer(), m.mi == mi
return m.pointer(), m.messageInfo() == mi
}
return pointer{}, false
}

View File

@ -256,7 +256,7 @@ type MessageState struct {
pragma.DoNotCompare
pragma.DoNotCopy
mi *MessageInfo
atomicMessageInfo *MessageInfo
}
type messageState MessageState

View File

@ -151,10 +151,10 @@ func (ms *messageState) messageInfo() *MessageInfo {
return ms.LoadMessageInfo()
}
func (ms *messageState) LoadMessageInfo() *MessageInfo {
return (*MessageInfo)(atomic.LoadPointer((*unsafe.Pointer)(unsafe.Pointer(&ms.mi))))
return (*MessageInfo)(atomic.LoadPointer((*unsafe.Pointer)(unsafe.Pointer(&ms.atomicMessageInfo))))
}
func (ms *messageState) StoreMessageInfo(mi *MessageInfo) {
atomic.StorePointer((*unsafe.Pointer)(unsafe.Pointer(&ms.mi)), unsafe.Pointer(mi))
atomic.StorePointer((*unsafe.Pointer)(unsafe.Pointer(&ms.atomicMessageInfo)), unsafe.Pointer(mi))
}
type atomicNilMessage struct{ p unsafe.Pointer } // p is a *messageReflectWrapper