Source file src/plugin/plugin.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 plugin implements loading and symbol resolution of Go plugins. 6 // 7 // A plugin is a Go main package with exported functions and variables that 8 // has been built with: 9 // 10 // go build -buildmode=plugin 11 // 12 // When a plugin is first opened, the init functions of all packages not 13 // already part of the program are called. The main function is not run. 14 // A plugin is only initialized once, and cannot be closed. 15 // 16 // # Warnings 17 // 18 // The ability to dynamically load parts of an application during 19 // execution, perhaps based on user-defined configuration, may be a 20 // useful building block in some designs. In particular, because 21 // applications and dynamically loaded functions can share data 22 // structures directly, plugins may enable very high-performance 23 // integration of separate parts. 24 // 25 // However, the plugin mechanism has many significant drawbacks that 26 // should be considered carefully during the design. For example: 27 // 28 // - Plugins are currently supported only on Linux, FreeBSD, and 29 // macOS, making them unsuitable for applications intended to be 30 // portable. 31 // 32 // - Plugins are poorly supported by the Go race detector. Even simple 33 // race conditions may not be automatically detected. See 34 // https://go.dev/issue/24245 for more information. 35 // 36 // - Applications that use plugins may require careful configuration 37 // to ensure that the various parts of the program be made available 38 // in the correct location in the file system (or container image). 39 // By contrast, deploying an application consisting of a single static 40 // executable is straightforward. 41 // 42 // - Reasoning about program initialization is more difficult when 43 // some packages may not be initialized until long after the 44 // application has started running. 45 // 46 // - Bugs in applications that load plugins could be exploited by 47 // an attacker to load dangerous or untrusted libraries. 48 // 49 // - Runtime crashes are likely to occur unless all parts of the 50 // program (the application and all its plugins) are compiled 51 // using exactly the same version of the toolchain, the same build 52 // tags, and the same values of certain flags and environment 53 // variables. 54 // 55 // - Similar crashing problems are likely to arise unless all common 56 // dependencies of the application and its plugins are built from 57 // exactly the same source code. 58 // 59 // - Together, these restrictions mean that, in practice, the 60 // application and its plugins must all be built together by a 61 // single person or component of a system. In that case, it may 62 // be simpler for that person or component to generate Go source 63 // files that blank-import the desired set of plugins and then 64 // compile a static executable in the usual way. 65 // 66 // For these reasons, many users decide that traditional interprocess 67 // communication (IPC) mechanisms such as sockets, pipes, remote 68 // procedure call (RPC), shared memory mappings, or file system 69 // operations may be more suitable despite the performance overheads. 70 package plugin 71 72 // Plugin is a loaded Go plugin. 73 type Plugin struct { 74 pluginpath string 75 err string // set if plugin failed to load 76 loaded chan struct{} // closed when loaded 77 syms map[string]any 78 } 79 80 // Open opens a Go plugin. 81 // If a path has already been opened, then the existing *[Plugin] is returned. 82 // It is safe for concurrent use by multiple goroutines. 83 func Open(path string) (*Plugin, error) { 84 return open(path) 85 } 86 87 // Lookup searches for a symbol named symName in plugin p. 88 // A symbol is any exported variable or function. 89 // It reports an error if the symbol is not found. 90 // It is safe for concurrent use by multiple goroutines. 91 func (p *Plugin) Lookup(symName string) (Symbol, error) { 92 return lookup(p, symName) 93 } 94 95 // A Symbol is a pointer to a variable or function. 96 // 97 // For example, a plugin defined as 98 // 99 // package main 100 // 101 // import "fmt" 102 // 103 // var V int 104 // 105 // func F() { fmt.Printf("Hello, number %d\n", V) } 106 // 107 // may be loaded with the [Open] function and then the exported package 108 // symbols V and F can be accessed 109 // 110 // p, err := plugin.Open("plugin_name.so") 111 // if err != nil { 112 // panic(err) 113 // } 114 // v, err := p.Lookup("V") 115 // if err != nil { 116 // panic(err) 117 // } 118 // f, err := p.Lookup("F") 119 // if err != nil { 120 // panic(err) 121 // } 122 // *v.(*int) = 7 123 // f.(func())() // prints "Hello, number 7" 124 type Symbol any 125