| 1 | package interp |
| 2 | |
| 3 | import ( |
| 4 | "flag" |
| 5 | "fmt" |
| 6 | "go/constant" |
| 7 | "log" |
| 8 | "math/bits" |
| 9 | "os" |
| 10 | "path" |
| 11 | "reflect" |
| 12 | |
| 13 | gen "github.com/traefik/yaegi/stdlib/generic" |
| 14 | ) |
| 15 | |
| 16 | // Symbols returns a map of interpreter exported symbol values for the given |
| 17 | // import path. If the argument is the empty string, all known symbols are |
| 18 | // returned. |
| 19 | func (interp *Interpreter) Symbols(importPath string) Exports { |
| 20 | m := map[string]map[string]reflect.Value{} |
| 21 | interp.mutex.RLock() |
| 22 | defer interp.mutex.RUnlock() |
| 23 | |
| 24 | for k, v := range interp.srcPkg { |
| 25 | if importPath != "" && k != importPath { |
| 26 | continue |
| 27 | } |
| 28 | syms := map[string]reflect.Value{} |
| 29 | for n, s := range v { |
| 30 | if !canExport(n) { |
| 31 | // Skip private non-exported symbols. |
| 32 | continue |
| 33 | } |
| 34 | switch s.kind { |
| 35 | case constSym: |
| 36 | syms[n] = s.rval |
| 37 | case funcSym: |
| 38 | syms[n] = genFunctionWrapper(s.node)(interp.frame) |
| 39 | case varSym: |
| 40 | syms[n] = interp.frame.data[s.index] |
| 41 | case typeSym: |
| 42 | syms[n] = reflect.New(s.typ.TypeOf()) |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | if len(syms) > 0 { |
| 47 | m[k] = syms |
| 48 | } |
| 49 | |
| 50 | if importPath != "" { |
| 51 | return m |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | if importPath != "" && len(m) > 0 { |
| 56 | return m |
| 57 | } |
| 58 | |
| 59 | for k, v := range interp.binPkg { |
| 60 | if importPath != "" && k != importPath { |
| 61 | continue |
| 62 | } |
| 63 | m[k] = v |
| 64 | if importPath != "" { |
| 65 | return m |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | return m |
| 70 | } |
| 71 | |
| 72 | // getWrapper returns the wrapper type of the corresponding interface, trying |
| 73 | // first the composed ones, or nil if not found. |
| 74 | func getWrapper(n *node, t reflect.Type) reflect.Type { |
| 75 | p, ok := n.interp.binPkg[t.PkgPath()] |
| 76 | if !ok { |
| 77 | return nil |
| 78 | } |
| 79 | w := p["_"+t.Name()] |
| 80 | lm := n.typ.methods() |
| 81 | |
| 82 | // mapTypes may contain composed interfaces wrappers to test against, from |
| 83 | // most complex to simplest (guaranteed by construction of mapTypes). Find the |
| 84 | // first for which the interpreter type has all the methods. |
| 85 | for _, rt := range n.interp.mapTypes[w] { |
| 86 | match := true |
| 87 | for i := 1; i < rt.NumField(); i++ { |
| 88 | // The interpreter type must have all required wrapper methods. |
| 89 | if _, ok := lm[rt.Field(i).Name[1:]]; !ok { |
| 90 | match = false |
| 91 | break |
| 92 | } |
| 93 | } |
| 94 | if match { |
| 95 | return rt |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | // Otherwise return the direct "non-composed" interface. |
| 100 | return w.Type().Elem() |
| 101 | } |
| 102 | |
| 103 | // Use loads binary runtime symbols in the interpreter context so |
| 104 | // they can be used in interpreted code. |
| 105 | func (interp *Interpreter) Use(values Exports) error { |
| 106 | for k, v := range values { |
| 107 | importPath := path.Dir(k) |
| 108 | packageName := path.Base(k) |
| 109 | |
| 110 | if k == "." && v["MapTypes"].IsValid() { |
| 111 | // Use mapping for special interface wrappers. |
| 112 | for kk, vv := range v["MapTypes"].Interface().(map[reflect.Value][]reflect.Type) { |
| 113 | interp.mapTypes[kk] = vv |
| 114 | } |
| 115 | continue |
| 116 | } |
| 117 | |
| 118 | if importPath == "." { |
| 119 | return fmt.Errorf("export path %[1]q is missing a package name; did you mean '%[1]s/%[1]s'?", k) |
| 120 | } |
| 121 | |
| 122 | if importPath == selfPrefix { |
| 123 | interp.hooks.Parse(v) |
| 124 | continue |
| 125 | } |
| 126 | |
| 127 | if interp.binPkg[importPath] == nil { |
| 128 | interp.binPkg[importPath] = make(map[string]reflect.Value) |
| 129 | interp.pkgNames[importPath] = packageName |
| 130 | } |
| 131 | |
| 132 | for s, sym := range v { |
| 133 | interp.binPkg[importPath][s] = sym |
| 134 | } |
| 135 | if k == selfPath { |
| 136 | interp.binPkg[importPath]["Self"] = reflect.ValueOf(interp) |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | // Checks if input values correspond to stdlib packages by looking for one |
| 141 | // well known stdlib package path. |
| 142 | if _, ok := values["fmt/fmt"]; ok { |
| 143 | fixStdlib(interp) |
| 144 | |
| 145 | // Load stdlib generic source. |
| 146 | for _, s := range gen.Sources { |
| 147 | if _, err := interp.Compile(s); err != nil { |
| 148 | return err |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | return nil |
| 153 | } |
| 154 | |
| 155 | // fixStdlib redefines interpreter stdlib symbols to use the standard input, |
| 156 | // output and errror assigned to the interpreter. The changes are limited to |
| 157 | // the interpreter only. |
| 158 | // Note that it is possible to escape the virtualized stdio by |
| 159 | // read/write directly to file descriptors 0, 1, 2. |
| 160 | func fixStdlib(interp *Interpreter) { |
| 161 | p := interp.binPkg["fmt"] |
| 162 | if p == nil { |
| 163 | return |
| 164 | } |
| 165 | |
| 166 | stdin, stdout, stderr := interp.stdin, interp.stdout, interp.stderr |
| 167 | |
| 168 | p["Print"] = reflect.ValueOf(func(a ...interface{}) (n int, err error) { return fmt.Fprint(stdout, a...) }) |
| 169 | p["Printf"] = reflect.ValueOf(func(f string, a ...interface{}) (n int, err error) { return fmt.Fprintf(stdout, f, a...) }) |
| 170 | p["Println"] = reflect.ValueOf(func(a ...interface{}) (n int, err error) { return fmt.Fprintln(stdout, a...) }) |
| 171 | |
| 172 | p["Scan"] = reflect.ValueOf(func(a ...interface{}) (n int, err error) { return fmt.Fscan(stdin, a...) }) |
| 173 | p["Scanf"] = reflect.ValueOf(func(f string, a ...interface{}) (n int, err error) { return fmt.Fscanf(stdin, f, a...) }) |
| 174 | p["Scanln"] = reflect.ValueOf(func(a ...interface{}) (n int, err error) { return fmt.Fscanln(stdin, a...) }) |
| 175 | |
| 176 | // Update mapTypes to virtualized symbols as well. |
| 177 | interp.mapTypes[p["Print"]] = interp.mapTypes[reflect.ValueOf(fmt.Print)] |
| 178 | interp.mapTypes[p["Printf"]] = interp.mapTypes[reflect.ValueOf(fmt.Printf)] |
| 179 | interp.mapTypes[p["Println"]] = interp.mapTypes[reflect.ValueOf(fmt.Println)] |
| 180 | interp.mapTypes[p["Scan"]] = interp.mapTypes[reflect.ValueOf(fmt.Scan)] |
| 181 | interp.mapTypes[p["Scanf"]] = interp.mapTypes[reflect.ValueOf(fmt.Scanf)] |
| 182 | interp.mapTypes[p["Scanln"]] = interp.mapTypes[reflect.ValueOf(fmt.Scanln)] |
| 183 | |
| 184 | if p = interp.binPkg["flag"]; p != nil { |
| 185 | c := flag.NewFlagSet(os.Args[0], flag.PanicOnError) |
| 186 | c.SetOutput(stderr) |
| 187 | p["CommandLine"] = reflect.ValueOf(&c).Elem() |
| 188 | } |
| 189 | |
| 190 | if p = interp.binPkg["log"]; p != nil { |
| 191 | l := log.New(stderr, "", log.LstdFlags) |
| 192 | // Restrict Fatal symbols to panic instead of exit. |
| 193 | p["Fatal"] = reflect.ValueOf(l.Panic) |
| 194 | p["Fatalf"] = reflect.ValueOf(l.Panicf) |
| 195 | p["Fatalln"] = reflect.ValueOf(l.Panicln) |
| 196 | |
| 197 | p["Flags"] = reflect.ValueOf(l.Flags) |
| 198 | p["Output"] = reflect.ValueOf(l.Output) |
| 199 | p["Panic"] = reflect.ValueOf(l.Panic) |
| 200 | p["Panicf"] = reflect.ValueOf(l.Panicf) |
| 201 | p["Panicln"] = reflect.ValueOf(l.Panicln) |
| 202 | p["Prefix"] = reflect.ValueOf(l.Prefix) |
| 203 | p["Print"] = reflect.ValueOf(l.Print) |
| 204 | p["Printf"] = reflect.ValueOf(l.Printf) |
| 205 | p["Println"] = reflect.ValueOf(l.Println) |
| 206 | p["SetFlags"] = reflect.ValueOf(l.SetFlags) |
| 207 | p["SetOutput"] = reflect.ValueOf(l.SetOutput) |
| 208 | p["SetPrefix"] = reflect.ValueOf(l.SetPrefix) |
| 209 | p["Writer"] = reflect.ValueOf(l.Writer) |
| 210 | |
| 211 | // Update mapTypes to virtualized symbols as well. |
| 212 | interp.mapTypes[p["Print"]] = interp.mapTypes[reflect.ValueOf(log.Print)] |
| 213 | interp.mapTypes[p["Printf"]] = interp.mapTypes[reflect.ValueOf(log.Printf)] |
| 214 | interp.mapTypes[p["Println"]] = interp.mapTypes[reflect.ValueOf(log.Println)] |
| 215 | interp.mapTypes[p["Panic"]] = interp.mapTypes[reflect.ValueOf(log.Panic)] |
| 216 | interp.mapTypes[p["Panicf"]] = interp.mapTypes[reflect.ValueOf(log.Panicf)] |
| 217 | interp.mapTypes[p["Panicln"]] = interp.mapTypes[reflect.ValueOf(log.Panicln)] |
| 218 | } |
| 219 | |
| 220 | if p = interp.binPkg["os"]; p != nil { |
| 221 | p["Args"] = reflect.ValueOf(&interp.args).Elem() |
| 222 | if interp.specialStdio { |
| 223 | // Inherit streams from interpreter even if they do not have a file descriptor. |
| 224 | p["Stdin"] = reflect.ValueOf(&stdin).Elem() |
| 225 | p["Stdout"] = reflect.ValueOf(&stdout).Elem() |
| 226 | p["Stderr"] = reflect.ValueOf(&stderr).Elem() |
| 227 | } else { |
| 228 | // Inherits streams from interpreter only if they have a file descriptor and preserve original type. |
| 229 | if s, ok := stdin.(*os.File); ok { |
| 230 | p["Stdin"] = reflect.ValueOf(&s).Elem() |
| 231 | } |
| 232 | if s, ok := stdout.(*os.File); ok { |
| 233 | p["Stdout"] = reflect.ValueOf(&s).Elem() |
| 234 | } |
| 235 | if s, ok := stderr.(*os.File); ok { |
| 236 | p["Stderr"] = reflect.ValueOf(&s).Elem() |
| 237 | } |
| 238 | } |
| 239 | if !interp.unrestricted { |
| 240 | // In restricted mode, scripts can only access to a passed virtualized env, and can not write the real one. |
| 241 | getenv := func(key string) string { return interp.env[key] } |
| 242 | p["Clearenv"] = reflect.ValueOf(func() { interp.env = map[string]string{} }) |
| 243 | p["ExpandEnv"] = reflect.ValueOf(func(s string) string { return os.Expand(s, getenv) }) |
| 244 | p["Getenv"] = reflect.ValueOf(getenv) |
| 245 | p["LookupEnv"] = reflect.ValueOf(func(key string) (s string, ok bool) { s, ok = interp.env[key]; return }) |
| 246 | p["Setenv"] = reflect.ValueOf(func(key, value string) error { interp.env[key] = value; return nil }) |
| 247 | p["Unsetenv"] = reflect.ValueOf(func(key string) error { delete(interp.env, key); return nil }) |
| 248 | p["Environ"] = reflect.ValueOf(func() (a []string) { |
| 249 | for k, v := range interp.env { |
| 250 | a = append(a, k+"="+v) |
| 251 | } |
| 252 | return |
| 253 | }) |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | if p = interp.binPkg["math/bits"]; p != nil { |
| 258 | // Do not trust extracted value maybe from another arch. |
| 259 | p["UintSize"] = reflect.ValueOf(constant.MakeInt64(bits.UintSize)) |
| 260 | } |
| 261 | } |