Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proxy: ensure Client fields are 64-bit aligned #54

Merged
merged 2 commits into from Mar 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 5 additions & 3 deletions proxy/client.go
Expand Up @@ -39,6 +39,11 @@ type CertSource interface {
// localhost port and tunneling them securely over a TLS connection to a remote
// database instance defined by its PlanetScale unique branch identifier.
type Client struct {
// connectionsCounter is used to enforce the optional maxConnections limit
// NOTE: don't move this field, as we need to make sure the fields are
// 64-bit aligned
connectionsCounter uint64

remoteAddr string
localAddr string
instance string
Expand All @@ -47,9 +52,6 @@ type Client struct {

log *zap.Logger

// connectionsCounter is used to enforce the optional maxConnections limit
connectionsCounter uint64

// configCache contains the TLS certificate chache for each indiviual
// database
configCache *tlsCache
Expand Down
12 changes: 12 additions & 0 deletions proxy/client_test.go
Expand Up @@ -11,6 +11,7 @@ import (
"net"
"path/filepath"
"testing"
"unsafe"

qt "github.com/frankban/quicktest"
"go.uber.org/zap/zaptest"
Expand Down Expand Up @@ -182,6 +183,17 @@ func TestClient_run(t *testing.T) {
<-done
}

func TestClient_SyncAtomicAlignment(t *testing.T) {
c := qt.New(t)

// copied from: https://github.com/GoogleCloudPlatform/cloudsql-proxy/blob/302d5d87ac52d8b814625f7b27344fd9ba6a0348/proxy/proxy/client_test.go#L290
// The sync/atomic pkg has a bug that requires the developer to guarantee
// 64-bit alignment when using 64-bit functions on 32-bit systems.
client := &Client{} //nolint: staticcheck
offset := unsafe.Offsetof(client.connectionsCounter)
c.Assert(int(offset%64), qt.Equals, 0, qt.Commentf("Client.connectionsCounter is not aligned"))
}

type testCert struct {
serverCfg *tls.Config
clientCert tls.Certificate
Expand Down