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

     1  // Copyright 2020 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  // Test passing C struct to exported Go function.
     8  
     9  /*
    10  #include <stdint.h>
    11  #include <stdlib.h>
    12  
    13  // T is a C struct with alignment padding after b.
    14  // The padding bytes are not considered initialized by MSAN.
    15  // It is big enough to be passed on stack in C ABI (and least
    16  // on AMD64).
    17  typedef struct { char b; uintptr_t x, y; } T;
    18  
    19  extern void F(T);
    20  
    21  // Use weak as a hack to permit defining a function even though we use export.
    22  void CF(int x) __attribute__ ((weak));
    23  void CF(int x) {
    24  	T *t = malloc(sizeof(T));
    25  	t->b = (char)x;
    26  	t->x = x;
    27  	t->y = x;
    28  	F(*t);
    29  }
    30  */
    31  import "C"
    32  
    33  //export F
    34  func F(t C.T) { println(t.b, t.x, t.y) }
    35  
    36  func main() {
    37  	C.CF(C.int(0))
    38  }
    39  

View as plain text