1// Copyright 2016 The Go Authors. All rights reserved.2// Use of this source code is governed by a BSD-style3// license that can be found in the LICENSE file.45package main67import (8 "fmt"9 "log"10 "path/filepath"11 "plugin"12 "strings"1314 "testplugin/common"15)1617func init() {18 common.X *= 519}2021// testUnnamed tests that two plugins built with .go files passed on22// the command line do not have overlapping symbols. That is,23// unnamed1.so/FuncInt and unnamed2.so/FuncInt should be distinct functions.24func 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 }3637 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}4950func 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 }5455 p, err := plugin.Open("plugin1.so")56 if err != nil {57 log.Fatalf("plugin.Open failed: %v", err)58 }5960 const wantX = 3 * 5 * 761 if got := common.X; got != wantX {62 log.Fatalf("after plugin load common.X=%d, want %d", got, wantX)63 }6465 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 }7273 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 }8081 // sub/plugin1.so is a different plugin with the same name as82 // the already loaded plugin. It also depends on common. Test83 // that we can load the different plugin, it is actually84 // 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 }9394 funcVar, err := subp.Lookup("FuncVar")95 if err != nil {96 log.Fatalf(`sub/plugin1.Lookup("FuncVar") failed: %v`, err)97 }98 called := false99 *funcVar.(*func()) = func() {100 called = true101 }102103 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 }113114 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 }128129 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, and134 // 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 }138139 _, 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 }146147 _, 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 }154155 _, 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 }163164 // Test that unexported types with the same names in165 // 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())()172173 testUnnamed()174175 fmt.Println("PASS")176}
Findings
✓ No findings reported for this file.