Source file src/cmd/cgo/internal/testsanitizers/testdata/asan_unsafe_fail1.go

     1  // Copyright 2022 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  package main
     6  
     7  import (
     8  	"fmt"
     9  	"unsafe"
    10  )
    11  
    12  func main() {
    13  	a := 1
    14  	b := 2
    15  	c := add(a, b)
    16  	d := a + b
    17  	fmt.Println(c, d)
    18  }
    19  
    20  //go:noinline
    21  func add(a1, b1 int) int {
    22  	// The arguments.
    23  	// When -asan is enabled, unsafe.Pointer(&a1) conversion is escaping.
    24  	var p *int = (*int)(unsafe.Add(unsafe.Pointer(&a1), 1*unsafe.Sizeof(int(1))))
    25  	*p = 10 // BOOM
    26  	return a1 + b1
    27  }
    28  

View as plain text