]> Cypherpunks.ru repositories - gostls13.git/blob - src/cmd/cgo/internal/testplugin/testdata/host/host.go
runtime: remove crash_cgo_test CgoRaceSignal timeout
[gostls13.git] / src / cmd / cgo / internal / testplugin / testdata / host / host.go
1 // Copyright 2016 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         "log"
10         "path/filepath"
11         "plugin"
12         "strings"
13
14         "testplugin/common"
15 )
16
17 func init() {
18         common.X *= 5
19 }
20
21 // testUnnamed tests that two plugins built with .go files passed on
22 // the command line do not have overlapping symbols. That is,
23 // unnamed1.so/FuncInt and unnamed2.so/FuncInt should be distinct functions.
24 func testUnnamed() {
25         p, err := plugin.Open("unnamed1.so")
26         if err != nil {
27                 log.Fatalf(`plugin.Open("unnamed1.so"): %v`, err)
28         }
29         fn, err := p.Lookup("FuncInt")
30         if err != nil {
31                 log.Fatalf(`unnamed1.so: Lookup("FuncInt") failed: %v`, err)
32         }
33         if got, want := fn.(func() int)(), 1; got != want {
34                 log.Fatalf("unnamed1.so: FuncInt()=%d, want %d", got, want)
35         }
36
37         p, err = plugin.Open("unnamed2.so")
38         if err != nil {
39                 log.Fatalf(`plugin.Open("unnamed2.so"): %v`, err)
40         }
41         fn, err = p.Lookup("FuncInt")
42         if err != nil {
43                 log.Fatalf(`unnamed2.so: Lookup("FuncInt") failed: %v`, err)
44         }
45         if got, want := fn.(func() int)(), 2; got != want {
46                 log.Fatalf("unnamed2.so: FuncInt()=%d, want %d", got, want)
47         }
48 }
49
50 func main() {
51         if got, want := common.X, 3*5; got != want {
52                 log.Fatalf("before plugin load common.X=%d, want %d", got, want)
53         }
54
55         p, err := plugin.Open("plugin1.so")
56         if err != nil {
57                 log.Fatalf("plugin.Open failed: %v", err)
58         }
59
60         const wantX = 3 * 5 * 7
61         if got := common.X; got != wantX {
62                 log.Fatalf("after plugin load common.X=%d, want %d", got, wantX)
63         }
64
65         seven, err := p.Lookup("Seven")
66         if err != nil {
67                 log.Fatalf(`Lookup("Seven") failed: %v`, err)
68         }
69         if got, want := *seven.(*int), 7; got != want {
70                 log.Fatalf("plugin1.Seven=%d, want %d", got, want)
71         }
72
73         readFunc, err := p.Lookup("ReadCommonX")
74         if err != nil {
75                 log.Fatalf(`plugin1.Lookup("ReadCommonX") failed: %v`, err)
76         }
77         if got := readFunc.(func() int)(); got != wantX {
78                 log.Fatalf("plugin1.ReadCommonX()=%d, want %d", got, wantX)
79         }
80
81         // sub/plugin1.so is a different plugin with the same name as
82         // the already loaded plugin. It also depends on common. Test
83         // that we can load the different plugin, it is actually
84         // different, and that it sees the same common package.
85         subpPath, err := filepath.Abs("sub/plugin1.so")
86         if err != nil {
87                 log.Fatalf("filepath.Abs(%q) failed: %v", subpPath, err)
88         }
89         subp, err := plugin.Open(subpPath)
90         if err != nil {
91                 log.Fatalf("plugin.Open(%q) failed: %v", subpPath, err)
92         }
93
94         funcVar, err := subp.Lookup("FuncVar")
95         if err != nil {
96                 log.Fatalf(`sub/plugin1.Lookup("FuncVar") failed: %v`, err)
97         }
98         called := false
99         *funcVar.(*func()) = func() {
100                 called = true
101         }
102
103         readFunc, err = subp.Lookup("ReadCommonX")
104         if err != nil {
105                 log.Fatalf(`sub/plugin1.Lookup("ReadCommonX") failed: %v`, err)
106         }
107         if got := readFunc.(func() int)(); got != wantX {
108                 log.Fatalf("sub/plugin1.ReadCommonX()=%d, want %d", got, wantX)
109         }
110         if !called {
111                 log.Fatal("calling ReadCommonX did not call FuncVar")
112         }
113
114         subf, err := subp.Lookup("F")
115         if err != nil {
116                 log.Fatalf(`sub/plugin1.Lookup("F") failed: %v`, err)
117         }
118         if gotf := subf.(func() int)(); gotf != 17 {
119                 log.Fatalf(`sub/plugin1.F()=%d, want 17`, gotf)
120         }
121         f, err := p.Lookup("F")
122         if err != nil {
123                 log.Fatalf(`plugin1.Lookup("F") failed: %v`, err)
124         }
125         if gotf := f.(func() int)(); gotf != 3 {
126                 log.Fatalf(`plugin1.F()=%d, want 17`, gotf)
127         }
128
129         p2, err := plugin.Open("plugin2.so")
130         if err != nil {
131                 log.Fatalf("plugin.Open failed: %v", err)
132         }
133         // Check that plugin2's init function was called, and
134         // that it modifies the same global variable as the host.
135         if got, want := common.X, 2; got != want {
136                 log.Fatalf("after loading plugin2, common.X=%d, want %d", got, want)
137         }
138
139         _, err = plugin.Open("plugin2-dup.so")
140         if err == nil {
141                 log.Fatal(`plugin.Open("plugin2-dup.so"): duplicate open should have failed`)
142         }
143         if s := err.Error(); !strings.Contains(s, "already loaded") {
144                 log.Fatal(`plugin.Open("plugin2.so"): error does not mention "already loaded"`)
145         }
146
147         _, err = plugin.Open("plugin-mismatch.so")
148         if err == nil {
149                 log.Fatal(`plugin.Open("plugin-mismatch.so"): should have failed`)
150         }
151         if s := err.Error(); !strings.Contains(s, "different version") {
152                 log.Fatalf(`plugin.Open("plugin-mismatch.so"): error does not mention "different version": %v`, s)
153         }
154
155         _, err = plugin.Open("plugin2-dup.so")
156         if err == nil {
157                 log.Fatal(`plugin.Open("plugin2-dup.so"): duplicate open after bad plugin should have failed`)
158         }
159         _, err = plugin.Open("plugin2.so")
160         if err != nil {
161                 log.Fatalf(`plugin.Open("plugin2.so"): second open with same name failed: %v`, err)
162         }
163
164         // Test that unexported types with the same names in
165         // different plugins do not interfere with each other.
166         //
167         // See Issue #21386.
168         UnexportedNameReuse, _ := p.Lookup("UnexportedNameReuse")
169         UnexportedNameReuse.(func())()
170         UnexportedNameReuse, _ = p2.Lookup("UnexportedNameReuse")
171         UnexportedNameReuse.(func())()
172
173         testUnnamed()
174
175         fmt.Println("PASS")
176 }