Skip to content

Commit

Permalink
Fix unsafe usage in Decode (#1097)
Browse files Browse the repository at this point in the history
We use unsafe pointers to convert slices to structs. It might be
possible that the struct and byte slice are not aligned in the
same way. This PR fixes the alignment issues

The command
```
gotip test -gcflags=all=-d=checkptr
```
no longer complains about unsafe pointer conversion.
The new implementation is as fast as the old one.
Fixes #1096
  • Loading branch information
Ibrahim Jarif authored Nov 11, 2019
1 parent 10155ab commit 8a93a41
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 13 deletions.
25 changes: 22 additions & 3 deletions structs.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* Copyright 2019 Dgraph Labs, Inc. and Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package badger

import (
Expand All @@ -13,6 +29,8 @@ type valuePointer struct {
Offset uint32
}

const vptrSize = unsafe.Sizeof(valuePointer{})

func (p valuePointer) Less(o valuePointer) bool {
if p.Fid != o.Fid {
return p.Fid < o.Fid
Expand All @@ -27,8 +45,6 @@ func (p valuePointer) IsZero() bool {
return p.Fid == 0 && p.Offset == 0 && p.Len == 0
}

const vptrSize = 12

// Encode encodes Pointer into byte buffer.
func (p valuePointer) Encode() []byte {
b := make([]byte, vptrSize)
Expand All @@ -39,7 +55,10 @@ func (p valuePointer) Encode() []byte {

// Decode decodes the value pointer into the provided byte buffer.
func (p *valuePointer) Decode(b []byte) {
*p = *(*valuePointer)(unsafe.Pointer(&b[0]))
// Copy over data from b into p. Using *p=unsafe.pointer(...) leads to
// pointer alignment issues. See https://github.com/dgraph-io/badger/issues/1096
// and comment https://github.com/dgraph-io/badger/pull/1097#pullrequestreview-307361714
copy(((*[vptrSize]byte)(unsafe.Pointer(p))[:]), b[:vptrSize])
}

// header is used in value log as a header before Entry.
Expand Down
15 changes: 7 additions & 8 deletions table/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ type header struct {
diff uint16 // Length of the diff.
}

const headerSize = uint16(unsafe.Sizeof(header{}))

// Encode encodes the header.
func (h header) Encode() []byte {
var b [4]byte
Expand All @@ -52,16 +54,13 @@ func (h header) Encode() []byte {
}

// Decode decodes the header.
func (h *header) Decode(buf []byte) int {
*h = *(*header)(unsafe.Pointer(&buf[0]))
return h.Size()
func (h *header) Decode(buf []byte) {
// Copy over data from buf into h. Using *h=unsafe.pointer(...) leads to
// pointer alignment issues. See https://github.com/dgraph-io/badger/issues/1096
// and comment https://github.com/dgraph-io/badger/pull/1097#pullrequestreview-307361714
copy(((*[headerSize]byte)(unsafe.Pointer(h))[:]), buf[:headerSize])
}

const headerSize = 4

// Size returns size of the header. Currently it's just a constant.
func (h header) Size() int { return headerSize }

// Builder is used in building a table.
type Builder struct {
// Typically tens or hundreds of meg. This is for one single file.
Expand Down
2 changes: 1 addition & 1 deletion table/iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func (itr *blockIterator) setIdx(i int) {
itr.key = append(itr.key[:itr.prevOverlap], itr.baseKey[itr.prevOverlap:h.overlap]...)
}
itr.prevOverlap = h.overlap
valueOff := headerSize + int(h.diff)
valueOff := headerSize + h.diff
diffKey := entryData[headerSize:valueOff]
itr.key = append(itr.key[:h.overlap], diffKey...)
itr.val = entryData[valueOff:]
Expand Down
2 changes: 1 addition & 1 deletion value_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1017,7 +1017,7 @@ func TestTruncatedDiscardStat(t *testing.T) {
entries := []*Entry{{
Key: y.KeyWithTs(lfDiscardStatsKey, 1),
// Insert truncated discard stats. This is important.
Value: encodedDS[:10],
Value: encodedDS[:13],
}}
// Push discard stats entry to the write channel.
req, err := db.sendToWriteCh(entries)
Expand Down

0 comments on commit 8a93a41

Please sign in to comment.