1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562
|
// Copyright 2013 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package interp
// Emulated functions that we cannot interpret because they are
// external or because they use "unsafe" or "reflect" operations.
import (
"go/types"
"math"
"os"
"runtime"
"strings"
"sync/atomic"
"time"
"unsafe"
"golang.org/x/tools/go/ssa"
)
type externalFn func(fr *frame, args []value) value
// TODO(adonovan): fix: reflect.Value abstracts an lvalue or an
// rvalue; Set() causes mutations that can be observed via aliases.
// We have not captured that correctly here.
// Key strings are from Function.String().
var externals = make(map[string]externalFn)
func init() {
// That little dot ۰ is an Arabic zero numeral (U+06F0), categories [Nd].
for k, v := range map[string]externalFn{
"(*sync.Pool).Get": ext۰sync۰Pool۰Get,
"(*sync.Pool).Put": ext۰nop,
"(reflect.Value).Bool": ext۰reflect۰Value۰Bool,
"(reflect.Value).CanAddr": ext۰reflect۰Value۰CanAddr,
"(reflect.Value).CanInterface": ext۰reflect۰Value۰CanInterface,
"(reflect.Value).Elem": ext۰reflect۰Value۰Elem,
"(reflect.Value).Field": ext۰reflect۰Value۰Field,
"(reflect.Value).Float": ext۰reflect۰Value۰Float,
"(reflect.Value).Index": ext۰reflect۰Value۰Index,
"(reflect.Value).Int": ext۰reflect۰Value۰Int,
"(reflect.Value).Interface": ext۰reflect۰Value۰Interface,
"(reflect.Value).IsNil": ext۰reflect۰Value۰IsNil,
"(reflect.Value).IsValid": ext۰reflect۰Value۰IsValid,
"(reflect.Value).Kind": ext۰reflect۰Value۰Kind,
"(reflect.Value).Len": ext۰reflect۰Value۰Len,
"(reflect.Value).MapIndex": ext۰reflect۰Value۰MapIndex,
"(reflect.Value).MapKeys": ext۰reflect۰Value۰MapKeys,
"(reflect.Value).NumField": ext۰reflect۰Value۰NumField,
"(reflect.Value).NumMethod": ext۰reflect۰Value۰NumMethod,
"(reflect.Value).Pointer": ext۰reflect۰Value۰Pointer,
"(reflect.Value).Set": ext۰reflect۰Value۰Set,
"(reflect.Value).String": ext۰reflect۰Value۰String,
"(reflect.Value).Type": ext۰reflect۰Value۰Type,
"(reflect.Value).Uint": ext۰reflect۰Value۰Uint,
"(reflect.error).Error": ext۰reflect۰error۰Error,
"(reflect.rtype).Bits": ext۰reflect۰rtype۰Bits,
"(reflect.rtype).Elem": ext۰reflect۰rtype۰Elem,
"(reflect.rtype).Field": ext۰reflect۰rtype۰Field,
"(reflect.rtype).In": ext۰reflect۰rtype۰In,
"(reflect.rtype).Kind": ext۰reflect۰rtype۰Kind,
"(reflect.rtype).NumField": ext۰reflect۰rtype۰NumField,
"(reflect.rtype).NumIn": ext۰reflect۰rtype۰NumIn,
"(reflect.rtype).NumMethod": ext۰reflect۰rtype۰NumMethod,
"(reflect.rtype).NumOut": ext۰reflect۰rtype۰NumOut,
"(reflect.rtype).Out": ext۰reflect۰rtype۰Out,
"(reflect.rtype).Size": ext۰reflect۰rtype۰Size,
"(reflect.rtype).String": ext۰reflect۰rtype۰String,
"bytes.init": ext۰nop, // avoid asm dependency
"bytes.Equal": ext۰bytes۰Equal,
"bytes.IndexByte": ext۰bytes۰IndexByte,
"hash/crc32.haveSSE42": ext۰crc32۰haveSSE42,
"internal/cpu.cpuid": ext۰cpu۰cpuid,
"internal/syscall/unix.syscall_fcntl": ext۰syscall۰unix۰syscall_fcntl,
"math.Abs": ext۰math۰Abs,
"math.Exp": ext۰math۰Exp,
"math.Float32bits": ext۰math۰Float32bits,
"math.Float32frombits": ext۰math۰Float32frombits,
"math.Float64bits": ext۰math۰Float64bits,
"math.Float64frombits": ext۰math۰Float64frombits,
"math.Ldexp": ext۰math۰Ldexp,
"math.Log": ext۰math۰Log,
"math.Min": ext۰math۰Min,
"math.hasSSE4": ext۰math۰hasSSE4,
"math.hasVectorFacility": ext۰math۰hasVectorFacility,
"os.runtime_args": ext۰os۰runtime_args,
"os.runtime_beforeExit": ext۰nop,
"os/signal.init": ext۰nop,
"reflect.New": ext۰reflect۰New,
"reflect.SliceOf": ext۰reflect۰SliceOf,
"reflect.TypeOf": ext۰reflect۰TypeOf,
"reflect.ValueOf": ext۰reflect۰ValueOf,
"reflect.Zero": ext۰reflect۰Zero,
"reflect.init": ext۰reflect۰Init,
"reflect.valueInterface": ext۰reflect۰valueInterface,
"runtime.Breakpoint": ext۰runtime۰Breakpoint,
"runtime.Caller": ext۰runtime۰Caller,
"runtime.Callers": ext۰runtime۰Callers,
"runtime.FuncForPC": ext۰runtime۰FuncForPC,
"runtime.GC": ext۰runtime۰GC,
"runtime.GOMAXPROCS": ext۰runtime۰GOMAXPROCS,
"runtime.Goexit": ext۰runtime۰Goexit,
"runtime.Gosched": ext۰runtime۰Gosched,
"runtime.init": ext۰nop,
"runtime.KeepAlive": ext۰nop,
"runtime.NumCPU": ext۰runtime۰NumCPU,
"runtime.NumGoroutine": ext۰runtime۰NumGoroutine,
"runtime.ReadMemStats": ext۰runtime۰ReadMemStats,
"runtime.SetFinalizer": ext۰nop, // ignore
"(*runtime.Func).Entry": ext۰runtime۰Func۰Entry,
"(*runtime.Func).FileLine": ext۰runtime۰Func۰FileLine,
"(*runtime.Func).Name": ext۰runtime۰Func۰Name,
"runtime.environ": ext۰runtime۰environ,
"runtime.getgoroot": ext۰runtime۰getgoroot,
"strings.init": ext۰nop, // avoid asm dependency
"strings.Count": ext۰strings۰Count,
"strings.Index": ext۰strings۰Index,
"strings.IndexByte": ext۰strings۰IndexByte,
"sync.runtime_Semacquire": ext۰nop, // unimplementable
"sync.runtime_Semrelease": ext۰nop, // unimplementable
"sync.runtime_Syncsemcheck": ext۰nop, // unimplementable
"sync.runtime_notifyListCheck": ext۰nop,
"sync.runtime_registerPoolCleanup": ext۰nop,
"sync/atomic.AddInt32": ext۰atomic۰AddInt32,
"sync/atomic.AddUint32": ext۰atomic۰AddUint32,
"sync/atomic.CompareAndSwapInt32": ext۰atomic۰CompareAndSwapInt32,
"sync/atomic.CompareAndSwapUint32": ext۰atomic۰CompareAndSwapUint32,
"sync/atomic.LoadInt32": ext۰atomic۰LoadInt32,
"sync/atomic.LoadUint32": ext۰atomic۰LoadUint32,
"sync/atomic.StoreInt32": ext۰atomic۰StoreInt32,
"sync/atomic.StoreUint32": ext۰atomic۰StoreUint32,
"sync/atomic.AddInt64": ext۰atomic۰AddInt64,
"sync/atomic.AddUint64": ext۰atomic۰AddUint64,
"sync/atomic.CompareAndSwapInt64": ext۰atomic۰CompareAndSwapInt64,
"sync/atomic.CompareAndSwapUint64": ext۰atomic۰CompareAndSwapUint64,
"sync/atomic.LoadInt64": ext۰atomic۰LoadInt64,
"sync/atomic.LoadUint64": ext۰atomic۰LoadUint64,
"sync/atomic.StoreInt64": ext۰atomic۰StoreInt64,
"sync/atomic.StoreUint64": ext۰atomic۰StoreUint64,
"(*sync/atomic.Value).Load": ext۰atomic۰ValueLoad,
"(*sync/atomic.Value).Store": ext۰atomic۰ValueStore,
"testing.MainStart": ext۰testing۰MainStart,
"time.Sleep": ext۰time۰Sleep,
"time.now": ext۰time۰now,
} {
externals[k] = v
}
}
// wrapError returns an interpreted 'error' interface value for err.
func wrapError(err error) value {
if err == nil {
return iface{}
}
return iface{t: errorType, v: err.Error()}
}
func ext۰nop(fr *frame, args []value) value { return nil }
func ext۰sync۰Pool۰Get(fr *frame, args []value) value {
Pool := fr.i.prog.ImportedPackage("sync").Type("Pool").Object()
_, newIndex, _ := types.LookupFieldOrMethod(Pool.Type(), false, Pool.Pkg(), "New")
if New := (*args[0].(*value)).(structure)[newIndex[0]]; New != nil {
return call(fr.i, fr, 0, New, nil)
}
return nil
}
func ext۰bytes۰Equal(fr *frame, args []value) value {
// func Equal(a, b []byte) bool
a := args[0].([]value)
b := args[1].([]value)
if len(a) != len(b) {
return false
}
for i := range a {
if a[i] != b[i] {
return false
}
}
return true
}
func ext۰bytes۰IndexByte(fr *frame, args []value) value {
// func IndexByte(s []byte, c byte) int
s := args[0].([]value)
c := args[1].(byte)
for i, b := range s {
if b.(byte) == c {
return i
}
}
return -1
}
func ext۰crc32۰haveSSE42(fr *frame, args []value) value {
return false
}
func ext۰math۰Float64frombits(fr *frame, args []value) value {
return math.Float64frombits(args[0].(uint64))
}
func ext۰math۰Float64bits(fr *frame, args []value) value {
return math.Float64bits(args[0].(float64))
}
func ext۰math۰Float32frombits(fr *frame, args []value) value {
return math.Float32frombits(args[0].(uint32))
}
func ext۰math۰Abs(fr *frame, args []value) value {
return math.Abs(args[0].(float64))
}
func ext۰math۰Exp(fr *frame, args []value) value {
return math.Exp(args[0].(float64))
}
func ext۰math۰Float32bits(fr *frame, args []value) value {
return math.Float32bits(args[0].(float32))
}
func ext۰math۰Min(fr *frame, args []value) value {
return math.Min(args[0].(float64), args[1].(float64))
}
func ext۰math۰hasSSE4(fr *frame, args []value) value {
return false
}
func ext۰math۰hasVectorFacility(fr *frame, args []value) value {
return false
}
func ext۰math۰Ldexp(fr *frame, args []value) value {
return math.Ldexp(args[0].(float64), args[1].(int))
}
func ext۰math۰Log(fr *frame, args []value) value {
return math.Log(args[0].(float64))
}
func ext۰os۰runtime_args(fr *frame, args []value) value {
return fr.i.osArgs
}
func ext۰runtime۰Breakpoint(fr *frame, args []value) value {
runtime.Breakpoint()
return nil
}
func ext۰runtime۰Caller(fr *frame, args []value) value {
// func Caller(skip int) (pc uintptr, file string, line int, ok bool)
skip := 1 + args[0].(int)
for i := 0; i < skip; i++ {
if fr != nil {
fr = fr.caller
}
}
var pc uintptr
var file string
var line int
var ok bool
if fr != nil {
fn := fr.fn
// TODO(adonovan): use pc/posn of current instruction, not start of fn.
// (Required to interpret the log package's tests.)
pc = uintptr(unsafe.Pointer(fn))
posn := fn.Prog.Fset.Position(fn.Pos())
file = posn.Filename
line = posn.Line
ok = true
}
return tuple{pc, file, line, ok}
}
func ext۰runtime۰Callers(fr *frame, args []value) value {
// Callers(skip int, pc []uintptr) int
skip := args[0].(int)
pc := args[1].([]value)
for i := 0; i < skip; i++ {
if fr != nil {
fr = fr.caller
}
}
i := 0
for fr != nil && i < len(pc) {
pc[i] = uintptr(unsafe.Pointer(fr.fn))
i++
fr = fr.caller
}
return i
}
func ext۰runtime۰FuncForPC(fr *frame, args []value) value {
// FuncForPC(pc uintptr) *Func
pc := args[0].(uintptr)
var fn *ssa.Function
if pc != 0 {
fn = (*ssa.Function)(unsafe.Pointer(pc)) // indeed unsafe!
}
var Func value
Func = structure{fn} // a runtime.Func
return &Func
}
func ext۰runtime۰environ(fr *frame, args []value) value {
// This function also implements syscall.runtime_envs.
return environ
}
func ext۰runtime۰getgoroot(fr *frame, args []value) value {
return os.Getenv("GOROOT")
}
func ext۰strings۰Count(fr *frame, args []value) value {
// Call compiled version to avoid asm dependency.
return strings.Count(args[0].(string), args[1].(string))
}
func ext۰strings۰IndexByte(fr *frame, args []value) value {
// Call compiled version to avoid asm dependency.
return strings.IndexByte(args[0].(string), args[1].(byte))
}
func ext۰strings۰Index(fr *frame, args []value) value {
// Call compiled version to avoid asm dependency.
return strings.Index(args[0].(string), args[1].(string))
}
func ext۰runtime۰GOMAXPROCS(fr *frame, args []value) value {
// Ignore args[0]; don't let the interpreted program
// set the interpreter's GOMAXPROCS!
return runtime.GOMAXPROCS(0)
}
func ext۰runtime۰Goexit(fr *frame, args []value) value {
// TODO(adonovan): don't kill the interpreter's main goroutine.
runtime.Goexit()
return nil
}
func ext۰runtime۰GC(fr *frame, args []value) value {
runtime.GC()
return nil
}
func ext۰runtime۰Gosched(fr *frame, args []value) value {
runtime.Gosched()
return nil
}
func ext۰runtime۰NumCPU(fr *frame, args []value) value {
return runtime.NumCPU()
}
func ext۰runtime۰NumGoroutine(fr *frame, args []value) value {
return int(atomic.LoadInt32(&fr.i.goroutines))
}
func ext۰runtime۰ReadMemStats(fr *frame, args []value) value {
// TODO(adonovan): populate args[0].(Struct)
return nil
}
func ext۰atomic۰LoadUint32(fr *frame, args []value) value {
// TODO(adonovan): fix: not atomic!
return (*args[0].(*value)).(uint32)
}
func ext۰atomic۰StoreUint32(fr *frame, args []value) value {
// TODO(adonovan): fix: not atomic!
*args[0].(*value) = args[1].(uint32)
return nil
}
func ext۰atomic۰LoadInt32(fr *frame, args []value) value {
// TODO(adonovan): fix: not atomic!
return (*args[0].(*value)).(int32)
}
func ext۰atomic۰StoreInt32(fr *frame, args []value) value {
// TODO(adonovan): fix: not atomic!
*args[0].(*value) = args[1].(int32)
return nil
}
func ext۰atomic۰CompareAndSwapInt32(fr *frame, args []value) value {
// TODO(adonovan): fix: not atomic!
p := args[0].(*value)
if (*p).(int32) == args[1].(int32) {
*p = args[2].(int32)
return true
}
return false
}
func ext۰atomic۰CompareAndSwapUint32(fr *frame, args []value) value {
// TODO(adonovan): fix: not atomic!
p := args[0].(*value)
if (*p).(uint32) == args[1].(uint32) {
*p = args[2].(uint32)
return true
}
return false
}
func ext۰atomic۰AddInt32(fr *frame, args []value) value {
// TODO(adonovan): fix: not atomic!
p := args[0].(*value)
newv := (*p).(int32) + args[1].(int32)
*p = newv
return newv
}
func ext۰atomic۰AddUint32(fr *frame, args []value) value {
// TODO(adonovan): fix: not atomic!
p := args[0].(*value)
newv := (*p).(uint32) + args[1].(uint32)
*p = newv
return newv
}
func ext۰atomic۰LoadUint64(fr *frame, args []value) value {
// TODO(adonovan): fix: not atomic!
return (*args[0].(*value)).(uint64)
}
func ext۰atomic۰StoreUint64(fr *frame, args []value) value {
// TODO(adonovan): fix: not atomic!
*args[0].(*value) = args[1].(uint64)
return nil
}
func ext۰atomic۰LoadInt64(fr *frame, args []value) value {
// TODO(adonovan): fix: not atomic!
return (*args[0].(*value)).(int64)
}
func ext۰atomic۰StoreInt64(fr *frame, args []value) value {
// TODO(adonovan): fix: not atomic!
*args[0].(*value) = args[1].(int64)
return nil
}
func ext۰atomic۰CompareAndSwapInt64(fr *frame, args []value) value {
// TODO(adonovan): fix: not atomic!
p := args[0].(*value)
if (*p).(int64) == args[1].(int64) {
*p = args[2].(int64)
return true
}
return false
}
func ext۰atomic۰CompareAndSwapUint64(fr *frame, args []value) value {
// TODO(adonovan): fix: not atomic!
p := args[0].(*value)
if (*p).(uint64) == args[1].(uint64) {
*p = args[2].(uint64)
return true
}
return false
}
func ext۰atomic۰AddInt64(fr *frame, args []value) value {
// TODO(adonovan): fix: not atomic!
p := args[0].(*value)
newv := (*p).(int64) + args[1].(int64)
*p = newv
return newv
}
func ext۰atomic۰AddUint64(fr *frame, args []value) value {
// TODO(adonovan): fix: not atomic!
p := args[0].(*value)
newv := (*p).(uint64) + args[1].(uint64)
*p = newv
return newv
}
func ext۰atomic۰ValueLoad(fr *frame, args []value) value {
// TODO(adonovan): fix: not atomic!
// Receiver is *struct{v interface{}}.
return (*args[0].(*value)).(structure)[0]
}
func ext۰atomic۰ValueStore(fr *frame, args []value) value {
// TODO(adonovan): fix: not atomic!
// Receiver is *struct{v interface{}}.
(*args[0].(*value)).(structure)[0] = args[1]
return nil
}
func ext۰cpu۰cpuid(fr *frame, args []value) value {
return tuple{uint32(0), uint32(0), uint32(0), uint32(0)}
}
func ext۰syscall۰unix۰syscall_fcntl(fr *frame, args []value) value {
return tuple{int(0), wrapError(nil)}
}
// Pretend: type runtime.Func struct { entry *ssa.Function }
func ext۰runtime۰Func۰FileLine(fr *frame, args []value) value {
// func (*runtime.Func) FileLine(uintptr) (string, int)
f, _ := (*args[0].(*value)).(structure)[0].(*ssa.Function)
pc := args[1].(uintptr)
_ = pc
if f != nil {
// TODO(adonovan): use position of current instruction, not fn.
posn := f.Prog.Fset.Position(f.Pos())
return tuple{posn.Filename, posn.Line}
}
return tuple{"", 0}
}
func ext۰runtime۰Func۰Name(fr *frame, args []value) value {
// func (*runtime.Func) Name() string
f, _ := (*args[0].(*value)).(structure)[0].(*ssa.Function)
if f != nil {
return f.String()
}
return ""
}
func ext۰runtime۰Func۰Entry(fr *frame, args []value) value {
// func (*runtime.Func) Entry() uintptr
f, _ := (*args[0].(*value)).(structure)[0].(*ssa.Function)
return uintptr(unsafe.Pointer(f))
}
func ext۰time۰now(fr *frame, args []value) value {
nano := time.Now().UnixNano()
return tuple{int64(nano / 1e9), int32(nano % 1e9), int64(0)}
}
func ext۰time۰Sleep(fr *frame, args []value) value {
time.Sleep(time.Duration(args[0].(int64)))
return nil
}
func valueToBytes(v value) []byte {
in := v.([]value)
b := make([]byte, len(in))
for i := range in {
b[i] = in[i].(byte)
}
return b
}
func ext۰testing۰MainStart(fr *frame, args []value) value {
// We no longer support interpretation of the "testing" package
// because it changes too often and uses low-level features that
// are a pain to emulate.
panic(`interpretation of the "testing" package is no longer supported`)
}
|