Source file src/cmd/cgo/internal/testplugin/testdata/issue44956/main.go

     1  // Copyright 2021 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  // Issue 44956: writable static temp is not exported correctly.
     6  // In the test below, package base is
     7  //
     8  //     X = &map{...}
     9  //
    10  // which compiles to
    11  //
    12  //     X = &stmp           // static
    13  //     stmp = makemap(...) // in init function
    14  //
    15  // plugin1 and plugin2 both import base. plugin1 doesn't use
    16  // base.X, so that symbol is deadcoded in plugin1.
    17  //
    18  // plugin1 is loaded first. base.init runs at that point, which
    19  // initialize base.stmp.
    20  //
    21  // plugin2 is then loaded. base.init already ran, so it doesn't run
    22  // again. When base.stmp is not exported, plugin2's base.X points to
    23  // its own private base.stmp, which is not initialized, fail.
    24  
    25  package main
    26  
    27  import "plugin"
    28  
    29  func main() {
    30  	_, err := plugin.Open("issue44956p1.so")
    31  	if err != nil {
    32  		panic("FAIL")
    33  	}
    34  
    35  	p2, err := plugin.Open("issue44956p2.so")
    36  	if err != nil {
    37  		panic("FAIL")
    38  	}
    39  	f, err := p2.Lookup("F")
    40  	if err != nil {
    41  		panic("FAIL")
    42  	}
    43  	x := f.(func() *map[int]int)()
    44  	if x == nil || (*x)[123] != 456 {
    45  		panic("FAIL")
    46  	}
    47  }
    48  

View as plain text