File size: 1,963 Bytes
4bcc2be | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | // Copyright 2024 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 subtle
import (
"internal/cpu"
"internal/runtime/sys"
"testing"
)
func TestWithDataIndependentTiming(t *testing.T) {
if !cpu.ARM64.HasDIT {
t.Skip("CPU does not support DIT")
}
ditAlreadyEnabled := sys.DITEnabled()
WithDataIndependentTiming(func() {
if !sys.DITEnabled() {
t.Fatal("dit not enabled within WithDataIndependentTiming closure")
}
WithDataIndependentTiming(func() {
if !sys.DITEnabled() {
t.Fatal("dit not enabled within nested WithDataIndependentTiming closure")
}
})
if !sys.DITEnabled() {
t.Fatal("dit not enabled after return from nested WithDataIndependentTiming closure")
}
})
if !ditAlreadyEnabled && sys.DITEnabled() {
t.Fatal("dit not unset after returning from WithDataIndependentTiming closure")
}
}
func TestDITPanic(t *testing.T) {
if !cpu.ARM64.HasDIT {
t.Skip("CPU does not support DIT")
}
ditAlreadyEnabled := sys.DITEnabled()
defer func() {
e := recover()
if e == nil {
t.Fatal("didn't panic")
}
if !ditAlreadyEnabled && sys.DITEnabled() {
t.Error("DIT still enabled after panic inside of WithDataIndependentTiming closure")
}
}()
WithDataIndependentTiming(func() {
if !sys.DITEnabled() {
t.Fatal("dit not enabled within WithDataIndependentTiming closure")
}
panic("bad")
})
}
func TestDITGoroutineInheritance(t *testing.T) {
if !cpu.ARM64.HasDIT {
t.Skip("CPU does not support DIT")
}
ditAlreadyEnabled := sys.DITEnabled()
WithDataIndependentTiming(func() {
done := make(chan struct{})
go func() {
if !sys.DITEnabled() {
t.Error("DIT not enabled in new goroutine")
}
close(done)
}()
<-done
if !ditAlreadyEnabled && !sys.DITEnabled() {
t.Fatal("dit unset after returning from goroutine started in WithDataIndependentTiming closure")
}
})
}
|