Source file test/wasm32arith.go

     1  // run
     2  
     3  // Copyright 2026 The Go Authors. All rights reserved.
     4  // Use of this source code is governed by a BSD-style
     5  // license that can be found in the LICENSE file.
     6  
     7  //go:build wasm32
     8  
     9  package main
    10  
    11  import "unsafe"
    12  
    13  //go:noinline
    14  func indexAfterAdd(a []int, x uint) int {
    15  	return a[x+1]
    16  }
    17  
    18  //go:noinline
    19  func indexAfterMul(a []int, x uint) int {
    20  	return a[x*2]
    21  }
    22  
    23  //go:noinline
    24  func indexAfterSignedAdd(a []int, x, y int) int {
    25  	return a[x+y]
    26  }
    27  
    28  //go:noinline
    29  func indexAfterShift(a []int, x uint) int {
    30  	return a[x<<1]
    31  }
    32  
    33  //go:noinline
    34  func indexAfterComplement(a []int, x uint) int {
    35  	return a[^x]
    36  }
    37  
    38  //go:noinline
    39  func indexAfterTruncate(a []int, x uint64) int {
    40  	return a[uint32(x)]
    41  }
    42  
    43  //go:noinline
    44  func truncateAndExtend(x uint64) ([3]uint64, [3]int64) {
    45  	return [3]uint64{uint64(uint8(x)), uint64(uint16(x)), uint64(uint32(x))},
    46  		[3]int64{int64(int8(x)), int64(int16(x)), int64(int32(x))}
    47  }
    48  
    49  //go:noinline
    50  func pointerAfterAdd(x uintptr) unsafe.Pointer {
    51  	return unsafe.Pointer(x + 1)
    52  }
    53  
    54  //go:noinline
    55  func dereferenceAfterAdd(x uintptr) byte {
    56  	return *(*byte)(unsafe.Pointer(x + 1))
    57  }
    58  
    59  func mustPanic(f func()) {
    60  	defer func() {
    61  		if recover() == nil {
    62  			panic("wrapped nil dereference did not panic")
    63  		}
    64  	}()
    65  	f()
    66  }
    67  
    68  func main() {
    69  	a := []int{42}
    70  	if indexAfterAdd(a, ^uint(0)) != 42 {
    71  		panic("MaxUint+1 did not wrap")
    72  	}
    73  	if indexAfterMul(a, uint(1)<<31) != 42 {
    74  		panic("uint multiplication did not wrap")
    75  	}
    76  	minInt := -int(^uint(0)>>1) - 1
    77  	if indexAfterSignedAdd(a, minInt, minInt) != 42 {
    78  		panic("signed addition did not wrap")
    79  	}
    80  	if indexAfterShift(a, uint(1)<<31) != 42 {
    81  		panic("left shift did not wrap")
    82  	}
    83  	if indexAfterComplement(a, ^uint(0)) != 42 {
    84  		panic("complement did not wrap")
    85  	}
    86  	if indexAfterTruncate(a, 1<<32) != 42 {
    87  		panic("index truncation did not discard the high word")
    88  	}
    89  	u, s := truncateAndExtend(0x0123456780008080)
    90  	if u != [3]uint64{0x80, 0x8080, 0x80008080} || s != [3]int64{-128, -32640, -2147450752} {
    91  		panic("extension after truncation did not preserve the low bits")
    92  	}
    93  	if pointerAfterAdd(^uintptr(0)) != nil {
    94  		panic("pointer arithmetic did not wrap")
    95  	}
    96  	mustPanic(func() {
    97  		dereferenceAfterAdd(^uintptr(0))
    98  	})
    99  }
   100  

View as plain text