Files
magpie/internal/pii/arch_test.go
T
yetone 4179cf2d8f 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.
2026-11-01 08:46:20 +08:00

149 lines
4.6 KiB
Go

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)
}
}