Skip to content

Commit

Permalink
starlark: use portable syscall wrapper for mmap (google#321)
Browse files Browse the repository at this point in the history
Also, add test to ensure that new dependencies (such as golang.org/x/sys/unix)
are not added casually.

Fixes google#320
  • Loading branch information
adonovan authored Nov 18, 2020
1 parent a5c0cc4 commit e55f603
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 12 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ require (
github.com/chzyer/logex v1.1.10 // indirect
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1 // indirect
golang.org/x/sys v0.0.0-20200803210538-64077c9b5642 // indirect
golang.org/x/sys v0.0.0-20200803210538-64077c9b5642
)
23 changes: 23 additions & 0 deletions starlark/eval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"bytes"
"fmt"
"math"
"os/exec"
"path/filepath"
"sort"
"strings"
Expand Down Expand Up @@ -891,3 +892,25 @@ func TestExecutionSteps(t *testing.T) {
t.Errorf("execution returned error %q, want cancellation", err)
}
}

// TestDeps fails if the interpreter proper (not the REPL, etc) sprouts new external dependencies.
// We may expand the list of permitted dependencies, but should do so deliberately, not casually.
func TestDeps(t *testing.T) {
cmd := exec.Command("go", "list", "-deps")
out, err := cmd.Output()
if err != nil {
t.Skipf("'go list' failed: %s", err)
}
for _, pkg := range strings.Split(string(out), "\n") {
// Does pkg have form "domain.name/dir"?
slash := strings.IndexByte(pkg, '/')
dot := strings.IndexByte(pkg, '.')
if 0 < dot && dot < slash {
if strings.HasPrefix(pkg, "go.starlark.net/") ||
strings.HasPrefix(pkg, "golang.org/x/sys/") {
continue // permitted dependencies
}
t.Errorf("new interpreter dependency: %s", pkg)
}
}
}
14 changes: 3 additions & 11 deletions starlark/int_posix64.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ import (
"log"
"math"
"math/big"
"runtime"
"syscall"
"unsafe"

"golang.org/x/sys/unix"
)

// intImpl represents a union of (int32, *big.Int) in a single pointer,
Expand Down Expand Up @@ -59,15 +59,7 @@ func makeBigInt(x *big.Int) Int { return Int{intImpl(x)} }
var smallints = reserveAddresses(1 << 32)

func reserveAddresses(len int) uintptr {
// Use syscall to avoid golang.org/x/sys/unix dependency.
MAP_ANON := 0x1000 // darwin (and all BSDs)
switch runtime.GOOS {
case "linux", "android":
MAP_ANON = 0x20
case "solaris":
MAP_ANON = 0x100
}
b, err := syscall.Mmap(-1, 0, len, syscall.PROT_READ, syscall.MAP_PRIVATE|MAP_ANON)
b, err := unix.Mmap(-1, 0, len, unix.PROT_READ, unix.MAP_PRIVATE|unix.MAP_ANON)
if err != nil {
log.Fatalf("mmap: %v", err)
}
Expand Down

0 comments on commit e55f603

Please sign in to comment.