Skip to content

Commit

Permalink
misc/spectre: add spectre index test
Browse files Browse the repository at this point in the history
Test for CL 222660.

Change-Id: I1dae41a9746dfc4144a0d29c02201de8ecd216fd
Reviewed-on: https://go-review.googlesource.com/c/go/+/222978
Reviewed-by: Keith Randall <[email protected]>
  • Loading branch information
rsc committed Mar 13, 2020
1 parent fc8a633 commit be72e3c
Show file tree
Hide file tree
Showing 5 changed files with 422 additions and 1 deletion.
51 changes: 51 additions & 0 deletions misc/spectre/asm_amd64.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright 2020 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

#include "textflag.h"

TEXT ·clflush(SB),NOSPLIT,$0-8
MOVQ arg+0(FP), AX
CLFLUSH 0(AX)
RET

TEXT ·rdtscp(SB),NOSPLIT,$0-8
RDTSCP
SHLQ $32, DX
ORQ DX, AX
MOVQ AX, ret+0(FP)
RET

TEXT ·nop(SB),NOSPLIT,$0-0
RET

TEXT ·cpuid(SB),NOSPLIT,$0-0
CPUID
RET

TEXT ·features(SB),NOSPLIT,$0-2
MOVL $0, AX
MOVL $0, CX
CPUID
CMPL AX, $1
JLT none

MOVL $1, AX
MOVL $0, CX
CPUID
SHRL $19, DX
ANDL $1, DX
MOVB DX, hasCLFLUSH+0(FP)

MOVL $0x80000001, AX
MOVL $0, CX
CPUID
SHRL $27, DX
ANDL $1, DX
MOVB DX, hasRDTSCP+0(FP)
RET

none:
MOVB $0, hasCLFLUSH+0(FP)
MOVB $0, hasRDTSCP+1(FP)
RET
7 changes: 7 additions & 0 deletions misc/spectre/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Copyright 2020 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Package spectre contains a Spectre test.
// It only runs on certain architectures.
package spectre
59 changes: 59 additions & 0 deletions misc/spectre/index_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright 2020 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package spectre

import "testing"

func shouldPanic(f func()) {
defer func() {
if recover() == nil {
panic("index did not panic")
}
}()
f()
}

var (
Zero = 0
One = 1
Two = 2
Three = 3
Four = 4
Five = 5
)

func TestIndex(t *testing.T) {
xs := "hello"
xi := []int{10, 20, 30, 40, 50}
xf := []float64{10, 20, 30, 40, 50}

xs = xs[Zero:Five]
xi = xi[Zero:Five]
xf = xf[Zero:Five]

if xs[Four] != 'o' {
t.Errorf("xs[4] = %q, want %q", xs[Four], 'o')
}
if xi[Four] != 50 {
t.Errorf("xi[4] = %d, want 50", xi[Four])
}
if xf[Four] != 50 {
t.Errorf("xf[4] = %v, want 50", xf[Four])
}

xs1 := xs[One:]
xi1 := xi[One:]
xf1 := xf[One:]

if xs1[Three] != 'o' {
t.Errorf("xs1[3] = %q, want %q", xs1[Three], 'o')
}
if xi1[Three] != 50 {
t.Errorf("xi1[3] = %d, want 50", xi1[Three])
}
if xf1[Three] != 50 {
t.Errorf("xf1[3] = %v, want 50", xf1[Three])
}
}
Loading

0 comments on commit be72e3c

Please sign in to comment.