pii: a CPU without AVX2 runs pf-serve-base, and Rosetta Apple silicon's pf-serve
echo_yip on X: Settings → Privacy's model never started, "pf-serve
(Metal): signal: illegal instruction" and the same on the CPU. x86's
pf-serve needs AVX2, FMA, F16C and BMI2, which a Celeron, a Pentium or
a Mac before Haswell lacks, and so does Rosetta before macOS 15, where
the Intel DMG on Apple silicon ran the Intel pf-serve.
- arch.go: target() is darwin-arm64 for magpie's Intel build under
Rosetta (sysctl.proc_translated, rosetta_darwin.go), so Apple
silicon's pf-serve, with Metal, is fetched and run. The Intel one
already on disk is kept until the arm64 one is (tagDirs; start
fetches it first, for at most 5 minutes, and falls back), then
removed with the old tags.
- fullX86 asks golang.org/x/sys/cpu for AVX, AVX2, FMA and BMI2. Where
one is missing, candidates() runs only pf-serve-base, the SSE4.2
build the previous commit adds, when it is on disk.
- pf-v3 (privacy-filter run 41256502056 at 697335d8e) is pinned;
pf-v2 and pf-v1 are old tags, so pf-v2 runs until pf-v3 is
downloaded. On a CPU without AVX2 that first start still fails, and
the retry after the download runs pf-serve-base.
Run at this commit: go test ./internal/pii/... on mote, ok; go build
./... ok; go vet ./internal/pii/... for linux, windows and darwin/amd64
ok; gofmt -l clean. Broken by hand: with candidates()'s fullX86 branch
and start()'s fetch turned off, TestBaseWhereTheCPULacksAVX2 ("starts =
[start pf-serve-vulkan MTL0]") and TestRosettaRunsAppleSiliconsServe
("started from [path pf-v9-darwin-amd64]") go red.
pf-v3's five zips, downloaded from the release and hashed here, match
its SHA256SUMS and the pins. A throwaway probe (not committed) set a
temp dir as pii's folder with the pinned model, fetched pf-v3 through
ensureServe from GitHub and masked an English and a Chinese text:
Linux (magpie-desktop), x86 with AVX2: pf-v3-linux-amd64, Vulkan
first, name/email/phone found
mote, Apple M5 Pro: pf-v3-darwin-arm64, Metal, the same spans
this Mac under Rosetta (arch -x86_64, the darwin/amd64 test binary,
sysctl.proc_translated 1): target darwin-arm64, fetched
pf-v3-darwin-arm64 and masked with Metal, the same scores as mote
Windows (xiaomi-gpu): pf-v3-windows-amd64, Vulkan, name/email/phone
found
The previous commit's qemu -cpu Nehalem run is where pf-serve-base
starts and pf-serve dies with SIGILL.
Not tried: the full go test ./... (only internal/pii changed); a real
CPU without AVX2; a Mac before macOS 15, where Rosetta has no AVX;
the GUI's Settings → Privacy page on a pf-v2 to pf-v3 upgrade.
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
package pii
|
||||
|
||||
import (
|
||||
"runtime"
|
||||
|
||||
"golang.org/x/sys/cpu"
|
||||
)
|
||||
|
||||
// osArch is the system magpie was built for.
|
||||
var osArch = runtime.GOOS + "-" + runtime.GOARCH
|
||||
|
||||
// target is this system's name in serveSHA: the one magpie was built
|
||||
// for, but Apple silicon's where an Intel magpie runs under Rosetta.
|
||||
// Rosetta before macOS 15 has no AVX, so the Intel pf-serve ended with
|
||||
// SIGILL on its first instruction of it, on Metal and on the CPU alike
|
||||
// (echo_yip on X); Apple silicon's runs there natively, on Metal.
|
||||
func target() string {
|
||||
if osArch == "darwin-amd64" && translated() {
|
||||
return "darwin-arm64"
|
||||
}
|
||||
return osArch
|
||||
}
|
||||
|
||||
// fullX86 says the CPU has what pf-serve is built for on x86 (build.sh's
|
||||
// x86): AVX, AVX2, FMA and BMI2, with F16C, which every CPU with AVX2
|
||||
// has. Without them pf-serve ends with SIGILL (a Celeron, a Pentium, a
|
||||
// Mac before Haswell), and pf-serve-base, built for SSE4.2 alone, is run
|
||||
// instead. Off x86 it is true: no other system has a pf-serve-base.
|
||||
var fullX86 = func() bool {
|
||||
if runtime.GOARCH != "amd64" {
|
||||
return true
|
||||
}
|
||||
return cpu.X86.HasAVX && cpu.X86.HasAVX2 && cpu.X86.HasFMA && cpu.X86.HasBMI2
|
||||
}
|
||||
@@ -0,0 +1,148 @@
|
||||
package pii
|
||||
|
||||
import (
|
||||
"archive/zip"
|
||||
"bytes"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// noFullX86 holds fullX86 at have for the test.
|
||||
func noFullX86(t *testing.T, have bool) {
|
||||
was := fullX86
|
||||
fullX86 = func() bool { return have }
|
||||
t.Cleanup(func() { fullX86 = was })
|
||||
}
|
||||
|
||||
// withBase puts the stand-in as pf-serve-base too.
|
||||
func withBase(t *testing.T) {
|
||||
t.Helper()
|
||||
b, err := os.ReadFile(exe("pf-serve"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(exe("pf-serve-base"), b, 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
// A CPU without AVX2, FMA or BMI2 ended pf-serve with SIGILL on the GPU
|
||||
// and on the CPU (echo_yip on X): pf-serve-base runs there, on the CPU
|
||||
// alone, since pf-serve-vulkan is built like pf-serve.
|
||||
func TestBaseWhereTheCPULacksAVX2(t *testing.T) {
|
||||
log := fakeInstall(t, "ok", true)
|
||||
withBase(t)
|
||||
noFullX86(t, false)
|
||||
if _, err := Find([]string{"hi Alice"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if starts := fakeLines(t, log, "start"); !reflect.DeepEqual(starts, []string{"start pf-serve-base CPU"}) {
|
||||
t.Fatalf("starts = %q", starts)
|
||||
}
|
||||
}
|
||||
|
||||
// A CPU with all of them never runs pf-serve-base; and a pf-serve from
|
||||
// before pf-serve-base was built is run as it was.
|
||||
func TestBaseOnlyWhereNeeded(t *testing.T) {
|
||||
t.Run("full CPU", func(t *testing.T) {
|
||||
log := fakeInstall(t, "novulkan", true)
|
||||
withBase(t)
|
||||
noFullX86(t, true)
|
||||
if _, err := Find([]string{"hi Alice"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if starts := fakeLines(t, log, "start"); !reflect.DeepEqual(starts, []string{"start pf-serve-vulkan MTL0", "start pf-serve CPU"}) {
|
||||
t.Fatalf("starts = %q", starts)
|
||||
}
|
||||
})
|
||||
t.Run("no base on disk", func(t *testing.T) {
|
||||
log := fakeInstall(t, "novulkan", true)
|
||||
noFullX86(t, false)
|
||||
if _, err := Find([]string{"hi Alice"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if starts := fakeLines(t, log, "start"); !reflect.DeepEqual(starts, []string{"start pf-serve-vulkan MTL0", "start pf-serve CPU"}) {
|
||||
t.Fatalf("starts = %q", starts)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// rosettaInstall is an Intel magpie's pf-serve of serveTag (pf-v9), as
|
||||
// fetched before magpie knew it ran under Rosetta, and from then on under
|
||||
// Rosetta, Apple silicon's served at want's paths.
|
||||
func rosettaInstall(t *testing.T, want map[string][]byte) string {
|
||||
t.Helper()
|
||||
wasArch, wasTr := osArch, translated
|
||||
wasTag, wasOld, wasBase, wasServe := serveTag, oldTags, serveBase, serveSHA
|
||||
t.Cleanup(func() {
|
||||
upgrades.Wait()
|
||||
osArch, translated = wasArch, wasTr
|
||||
serveTag, oldTags, serveBase, serveSHA = wasTag, wasOld, wasBase, wasServe
|
||||
})
|
||||
osArch, translated = "darwin-amd64", func() bool { return false }
|
||||
serveTag, oldTags = "pf-v9", nil
|
||||
log := fakeInstall(t, "ok", false)
|
||||
translated = func() bool { return true }
|
||||
if target() != "darwin-arm64" {
|
||||
t.Fatalf("target = %s under Rosetta", target())
|
||||
}
|
||||
s := httptest.NewServer(&files{m: want})
|
||||
t.Cleanup(s.Close)
|
||||
zipSum := sum([]byte("nothing"))
|
||||
if z, ok := want["/rel/pf-v9/pf-serve-darwin-arm64.zip"]; ok {
|
||||
zipSum = sum(z)
|
||||
}
|
||||
serveBase, serveSHA = s.URL+"/rel/", map[string]string{"darwin-arm64": zipSum, "darwin-amd64": "x"}
|
||||
return log
|
||||
}
|
||||
|
||||
// Under Rosetta, Apple silicon's pf-serve is fetched before the first
|
||||
// start, run, and the Intel one deleted.
|
||||
func TestRosettaRunsAppleSiliconsServe(t *testing.T) {
|
||||
self, err := os.Executable()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
b, err := os.ReadFile(self)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var zb bytes.Buffer
|
||||
zw := zip.NewWriter(&zb)
|
||||
w, _ := zw.Create(filepath.Base(exeIn("", "pf-serve")))
|
||||
w.Write(b)
|
||||
zw.Close()
|
||||
log := rosettaInstall(t, map[string][]byte{"/rel/pf-v9/pf-serve-darwin-arm64.zip": zb.Bytes()})
|
||||
intel := filepath.Join(dir(), "pf-v9-darwin-amd64")
|
||||
if !Installed() {
|
||||
t.Fatal("the Intel pf-serve wasn't taken for installed")
|
||||
}
|
||||
if _, err := Find([]string{"hi Alice"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := fakeLines(t, log, "path "); !reflect.DeepEqual(got, []string{"path pf-v9-darwin-arm64"}) {
|
||||
t.Fatalf("started from %q", got)
|
||||
}
|
||||
upgrades.Wait()
|
||||
if _, err := os.Stat(intel); !os.IsNotExist(err) {
|
||||
t.Fatalf("the Intel pf-serve was left: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// One that can't be fetched leaves the Intel one to run.
|
||||
func TestRosettaKeepsIntelsWhereNoneIsFetched(t *testing.T) {
|
||||
log := rosettaInstall(t, map[string][]byte{})
|
||||
if _, err := Find([]string{"hi Alice"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
upgrades.Wait()
|
||||
if got := fakeLines(t, log, "path "); !reflect.DeepEqual(got, []string{"path pf-v9-darwin-amd64"}) {
|
||||
t.Fatalf("started from %q", got)
|
||||
}
|
||||
if _, err := os.Stat(exeIn(filepath.Join(dir(), "pf-v9-darwin-amd64"), "pf-serve")); err != nil {
|
||||
t.Fatalf("the Intel pf-serve was deleted: %v", err)
|
||||
}
|
||||
}
|
||||
@@ -169,10 +169,16 @@ func upgrade() {
|
||||
removeOld()
|
||||
}
|
||||
|
||||
// removeOld deletes oldTags' pf-serve.
|
||||
// removeOld deletes oldTags' pf-serve, and serveTag's for a system
|
||||
// other than target (an Intel one under Rosetta).
|
||||
func removeOld() {
|
||||
for _, tag := range oldTags {
|
||||
os.RemoveAll(tagDir(tag))
|
||||
for _, d := range tagDirs(tag) {
|
||||
os.RemoveAll(d)
|
||||
}
|
||||
}
|
||||
for _, d := range tagDirs(serveTag)[1:] {
|
||||
os.RemoveAll(d)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+22
-12
@@ -52,21 +52,18 @@ var (
|
||||
// oldTags are the tags pinned before, newest first: one of theirs on disk
|
||||
// is run until serveTag's is downloaded (upgrade).
|
||||
var (
|
||||
serveTag = "pf-v2"
|
||||
oldTags = []string{"pf-v1"}
|
||||
serveTag = "pf-v3"
|
||||
oldTags = []string{"pf-v2", "pf-v1"}
|
||||
serveBase = "https://github.com/yetone/magpie/releases/download/"
|
||||
serveSHA = map[string]string{
|
||||
"darwin-amd64": "4b68c9f06b14b909ec35e491ba58fcb06df500a5a0985465dd1115f9e7a5d753",
|
||||
"darwin-arm64": "13ea258cc139dd0fc024f8eab70ee3566aa12ce6b5ab71f890b68bc8507159d3",
|
||||
"linux-amd64": "ff0af5cd1a3d350e7c93ca45c0b5285088c9666340826e87fbb14e5dcb23f772",
|
||||
"linux-arm64": "05970f7c5ba202e0e577cb22e0d12f009789c321583574a1ae1196d9522f7181",
|
||||
"windows-amd64": "f4993000cc09500c907d3dd7042d783fa0b9310f272bc0bdf323ba9d012556f0",
|
||||
"darwin-amd64": "5d9277d6852b7e3dcc30d102f43c21bf54a5c3199e0f9f998d965ec7a30ea6d7",
|
||||
"darwin-arm64": "b45951751bb26633a5ca3827fbf3718f10dfd5c313da6b4f6ba786755353cb4b",
|
||||
"linux-amd64": "b2b231209d8806648ec8dfe2d7b1a06c4cc273d1de4d50325e7a1638e63c7cc4",
|
||||
"linux-arm64": "9812356fcb311c351c094aa9a587da3d54166382699b59e9f1c92ab08abae792",
|
||||
"windows-amd64": "8fd8f7295743197311d13ed34aa9fb7be891f4e2586dfda6c4a3335e6cef52f3",
|
||||
}
|
||||
)
|
||||
|
||||
// target is this system's name in serveSHA.
|
||||
func target() string { return runtime.GOOS + "-" + runtime.GOARCH }
|
||||
|
||||
// Supported says pf-serve is built for this system.
|
||||
func Supported() bool { return serveSHA[target()] != "" }
|
||||
|
||||
@@ -88,6 +85,17 @@ func serveDir() string { return tagDir(serveTag) }
|
||||
|
||||
func tagDir(tag string) string { return filepath.Join(dir(), tag+"-"+target()) }
|
||||
|
||||
// tagDirs are where tag's pf-serve may be on disk: tagDir, and under
|
||||
// Rosetta also the Intel one's, which magpie fetched there before it
|
||||
// fetched Apple silicon's (target).
|
||||
func tagDirs(tag string) []string {
|
||||
ds := []string{tagDir(tag)}
|
||||
if osArch != target() {
|
||||
ds = append(ds, filepath.Join(dir(), tag+"-"+osArch))
|
||||
}
|
||||
return ds
|
||||
}
|
||||
|
||||
// exe is a program of pf-serve's: serveTag's, or, until that is
|
||||
// downloaded, the newest of oldTags' on disk.
|
||||
func exe(name string) string { return exeIn(binDir(), name) }
|
||||
@@ -101,8 +109,10 @@ func exeIn(d, name string) string {
|
||||
|
||||
func binDir() string {
|
||||
for _, tag := range append([]string{serveTag}, oldTags...) {
|
||||
if _, err := os.Stat(exeIn(tagDir(tag), "pf-serve")); err == nil {
|
||||
return tagDir(tag)
|
||||
for _, d := range tagDirs(tag) {
|
||||
if _, err := os.Stat(exeIn(d, "pf-serve")); err == nil {
|
||||
return d
|
||||
}
|
||||
}
|
||||
}
|
||||
return serveDir()
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
package pii
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
// translated says magpie runs under Rosetta: an Intel build on Apple
|
||||
// silicon.
|
||||
var translated = sync.OnceValue(func() bool {
|
||||
v, err := unix.SysctlUint32("sysctl.proc_translated")
|
||||
return err == nil && v == 1
|
||||
})
|
||||
@@ -0,0 +1,6 @@
|
||||
//go:build !darwin
|
||||
|
||||
package pii
|
||||
|
||||
// translated says magpie runs under Rosetta, which only macOS has.
|
||||
var translated = func() bool { return false }
|
||||
@@ -3,6 +3,7 @@ package pii
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/binary"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
@@ -165,7 +166,15 @@ type candidate struct {
|
||||
// which runs on Metal; then pf-serve on the CPU, which runs anywhere.
|
||||
// macOS's was tried on Metal alone: one whose load hung there, or ended,
|
||||
// was tried again on Metal by every request (lazyeo, an M2).
|
||||
// A CPU without what pf-serve is built for runs pf-serve-base, on the
|
||||
// CPU alone: pf-serve-vulkan is built like pf-serve, and would end the
|
||||
// same way.
|
||||
func candidates(cpu bool) []candidate {
|
||||
if !fullX86() {
|
||||
if _, err := os.Stat(exe("pf-serve-base")); err == nil {
|
||||
return []candidate{{exe("pf-serve-base"), true}}
|
||||
}
|
||||
}
|
||||
var out []candidate
|
||||
if !cpu {
|
||||
if _, err := os.Stat(exe("pf-serve-vulkan")); err == nil {
|
||||
@@ -183,6 +192,21 @@ func start(cpu bool, stop <-chan struct{}) (*helper, error) {
|
||||
if !Installed() {
|
||||
return nil, ErrNotInstalled
|
||||
}
|
||||
if !strings.HasSuffix(binDir(), "-"+target()) {
|
||||
// only the Intel pf-serve under Rosetta, which may not start at
|
||||
// all: Apple silicon's is fetched first, and one that can't be
|
||||
// leaves the Intel one to try
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
|
||||
go func() {
|
||||
select {
|
||||
case <-stop:
|
||||
cancel()
|
||||
case <-ctx.Done():
|
||||
}
|
||||
}()
|
||||
ensureServe(ctx)
|
||||
cancel()
|
||||
}
|
||||
upgrades.Add(1)
|
||||
go func() {
|
||||
defer upgrades.Done()
|
||||
|
||||
Reference in New Issue
Block a user