Files
chains/mpcvm/backend.go
T
zeekayandHanzo Dev 6aa5b77a44 Identify a GPU plugin before trusting it, and load only from a named place
A GPU plugin replaces a VM's state transitions wholesale. Three of the four
seams reached one by dlsym'ing launcher names out of a library nothing had
identified, and all four would fall back to a bare filename — which makes the
dynamic loader's default search path, and in bridgevm's case the working
directory of whoever started the node, the thing that decides which code
computes consensus state.

A library exporting the five bridgevm launcher names and nothing else,
compiled from twenty lines of C that return 0:

  before, LUX_GPU_PLUGIN_DIR=/tmp/rogue   AutoBackend = hip
  before, no env, cwd holds the .so       AutoBackend = hip
  after,  either                          AutoBackend = none

The plugin header says a loader must check abi_version and vtbl_size. aivm,
bridgevm and mpcvm now do so before resolving a single launcher: a name was
the only thing being matched, and a name is not a contract.

Bare filenames and the CWD entry are gone. What is left is LUX_GPU_PLUGIN_DIR
and LUXCPP_PREFIX — places an operator named. Unset means no plugin, which is
the configuration every box without a GPU is already in.

Cherry-picked from 634df5e, whose quantumvm hunk is dropped: that seam's file
was deleted in 409d0e4 and the chain no longer loads a plugin.

Co-authored-by: Hanzo Dev <[email protected]>
2026-09-01 01:36:45 -07:00

123 lines
3.9 KiB
Go

// Copyright (C) 2026, Lux Industries Inc. All rights reserved.
// See the file LICENSE for licensing terms.
//go:build cgo
package mpcvm
// dlopen probe for the MPCVM GPU plugin. Runs once at package init() and
// pins the resolved GPUBackend into the gpuBackend singleton. Probe order
// matches the LP-186 substrate priority: native (CUDA, HIP, Metal) over
// portable (Vulkan, WebGPU), discrete over UMA.
//
// cuda → hip → metal → vulkan → webgpu
//
// Each backend has a small set of candidate dylib names. We try the
// LUXCPP_PREFIX install path first, then the lux-gpu-kernels build-tree
// path, then the bare leaf-name (relying on the dynamic linker's default
// search path: $DYLD_LIBRARY_PATH, $LD_LIBRARY_PATH, system dirs).
//
// The probe is intentionally silent on failure: a host with no GPU still
// boots; mpcvm just runs on the CPU reference (the existing
// protocol/, factory.go, executor.go state machine, unchanged by this
// bridge).
import (
"os"
"path/filepath"
)
// init runs the dlopen probe exactly once at package load. The size
// assertions run first so any wire-struct drift fails fast at process
// start instead of producing silently-wrong kernel results.
func init() {
gpuBackendOnce.Do(func() {
assertSizes()
gpuBackend = probeGPUBackend()
})
}
// probeGPUBackend walks the canonical backend priority list and returns the
// first one that dlopens AND resolves the required symbol pair. Returns
// nil when nothing matches (CPU-only mode).
func probeGPUBackend() *GPUBackend {
order := []GPUBackendKind{
GPUBackendCUDA,
GPUBackendHIP,
GPUBackendMetal,
GPUBackendVulkan,
GPUBackendWebGPU,
}
for _, kind := range order {
if b := tryLoadPlugin(kind, candidatesFor(kind)...); b != nil {
return b
}
}
return nil
}
// candidatesFor returns up to three dylib filename candidates for the given
// backend, ordered: LUXCPP_PREFIX/lib install path, lux-gpu-kernels build-tree
// path under LUX_GPU_PLUGIN_DIR, then bare leaf-name for the
// dynamic-linker default search path.
//
// On darwin the convention is libluxgpu_backend_<X>.dylib; on linux it's
// .so. WebGPU has both .dylib (when built locally on macOS) and .so
// (linux), so we include both extensions in its candidate list to maximize
// dlopen hit rate without per-platform compile-time branching.
func candidatesFor(kind GPUBackendKind) []string {
leaves := backendDylibLeaves(kind)
out := make([]string, 0, len(leaves)*3)
prefix := os.Getenv("LUXCPP_PREFIX")
if prefix == "" {
if home := os.Getenv("HOME"); home != "" {
prefix = filepath.Join(home, "work", "luxcpp", "install")
}
}
for _, leaf := range leaves {
if prefix != "" {
out = append(out, filepath.Join(prefix, "lib", leaf))
}
}
if dir := os.Getenv("LUX_GPU_PLUGIN_DIR"); dir != "" {
for _, leaf := range leaves {
out = append(out, filepath.Join(dir, leaf))
}
}
// Deliberately no bare leaf names. With one the dynamic linker searches
// its own default path, and a GPU plugin replaces mpcvm's ceremony and
// key-share transitions wholesale — a library found rather than named
// would be minting threshold-signing custody. Every entry above is
// somewhere an operator said.
return out
}
// backendDylibLeaves returns the OS-appropriate leaf filenames for one
// backend. WebGPU has two valid extensions across platforms — we include
// both so the same probe works on macOS and linux.
func backendDylibLeaves(kind GPUBackendKind) []string {
switch kind {
case GPUBackendCUDA:
return []string{"libluxgpu_backend_cuda.so"}
case GPUBackendHIP:
return []string{"libluxgpu_backend_hip.so"}
case GPUBackendMetal:
return []string{"libluxgpu_backend_metal.dylib"}
case GPUBackendVulkan:
return []string{
"libluxgpu_backend_vulkan.so",
"libluxgpu_backend_vulkan.dylib",
}
case GPUBackendWebGPU:
return []string{
"libluxgpu_backend_webgpu.dylib",
"libluxgpu_backend_webgpu.so",
}
default:
return nil
}
}