Source file src/crypto/internal/nistec/p256_asm_test.go

     1  // Copyright 2024 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  //go:build (amd64 || arm64 || ppc64le || s390x) && !purego && linux
     6  
     7  package nistec
     8  
     9  import (
    10  	"syscall"
    11  	"testing"
    12  	"unsafe"
    13  )
    14  
    15  // Lightly adapted from the bytes test package. Allocate a pair of T one at the start of a page, another at the
    16  // end. Any access beyond or before the page boundary should cause a fault. This is linux specific.
    17  func dangerousObjs[T any](t *testing.T) (start *T, end *T) {
    18  	pagesize := syscall.Getpagesize()
    19  	b, err := syscall.Mmap(0, 0, 3*pagesize, syscall.PROT_READ|syscall.PROT_WRITE, syscall.MAP_ANONYMOUS|syscall.MAP_PRIVATE)
    20  	if err != nil {
    21  		t.Fatalf("mmap failed %s", err)
    22  	}
    23  	err = syscall.Mprotect(b[:pagesize], syscall.PROT_NONE)
    24  	if err != nil {
    25  		t.Fatalf("mprotect low failed %s\n", err)
    26  	}
    27  	err = syscall.Mprotect(b[2*pagesize:], syscall.PROT_NONE)
    28  	if err != nil {
    29  		t.Fatalf("mprotect high failed %s\n", err)
    30  	}
    31  	b = b[pagesize : 2*pagesize]
    32  	end = (*T)(unsafe.Pointer(&b[len(b)-(int)(unsafe.Sizeof(*end))]))
    33  	start = (*T)(unsafe.Pointer(&b[0]))
    34  	return start, end
    35  }
    36  
    37  func TestP256SelectAffinePageBoundary(t *testing.T) {
    38  	var out p256AffinePoint
    39  	begintp, endtp := dangerousObjs[p256AffineTable](t)
    40  	for i := 0; i < 31; i++ {
    41  		p256SelectAffine(&out, begintp, i)
    42  		p256SelectAffine(&out, endtp, i)
    43  	}
    44  }
    45  
    46  func TestP256SelectPageBoundary(t *testing.T) {
    47  	var out P256Point
    48  	begintp, endtp := dangerousObjs[p256Table](t)
    49  	for i := 0; i < 15; i++ {
    50  		p256Select(&out, begintp, i)
    51  		p256Select(&out, endtp, i)
    52  	}
    53  }
    54  

View as plain text