Documentation
¶
Overview ¶
Package nvdisasmoracle runs NVIDIA's nvdisasm disassembler as an independent ground-truth oracle for SASS decoders, micro-assemblers, and static analyses.
Callers configure the executable or remote/VM command prefix through the EnvCommand ("NVDISASM_CMD") environment variable and optionally persist SHA-256-keyed results in EnvCacheDir ("NVDISASM_CACHE"). Cache keys are scoped to the full command invocation and a verified or caller-pinned tool identity (Oracle.ToolIdentity), preventing silent reuse across toolkits, command changes, or in-place executable replacements. When NVDISASM_CMD is unset, FromEnv reports false so gated tests skip cleanly.
WrapCUBIN packages raw 16-byte instruction words into a minimal 64-bit EM_CUDA ELF image (OSABI 0x41, ABI version 1) so nvdisasm modes that require an ELF container (--print-life-ranges, --output-control-flow-graph-with-basic-blocks) can inspect assembled instruction sequences without nvcc or ptxas.
Index ¶
Examples ¶
Constants ¶
const ( OSABICUDA uint8 = 0x41 ABIVersion1 uint8 = 1 EFlagsSM121a uint32 = 0x7908 )
CUDA ELF64 header constants accepted by nvdisasm for sm_121a.
const ( EnvCommand = "NVDISASM_CMD" EnvCacheDir = "NVDISASM_CACHE" )
Environment variable names configuring FromEnv.
Variables ¶
This section is empty.
Functions ¶
func EncodeWord ¶
EncodeWord packs Lo and Hi into a 16-byte little-endian SASS instruction word.
Example ¶
package main
import (
"fmt"
"github.com/tmc/lib/nvidia/internal/nvdisasmoracle"
)
func main() {
w := nvdisasmoracle.EncodeWord(0x0000000000007918, 0x000fc00000000000)
fmt.Printf("%x %x\n", w[:2], w[13:15])
}
Output: 1879 c00f
func WrapCUBIN ¶
WrapCUBIN wraps 16-byte-aligned raw SASS instruction bytes into a minimal 64-bit little-endian EM_CUDA ELF image (OSABI 0x41, ABI 1, e_flags eflags) containing a single global STT_FUNC kernel section ".text.<kernel>".
Example ¶
package main
import (
"bytes"
"encoding/binary"
"fmt"
"github.com/tmc/lib/nvidia/internal/nvdisasmoracle"
elfinspect "github.com/tmc/lib/nvidia/x/host/elf"
)
func main() {
nop := nvdisasmoracle.EncodeWord(0x0000000000007918, 0x000fc00000000000)
exit := nvdisasmoracle.EncodeWord(0x000000000000794d, 0x000fea0003800000)
var code []byte
code = append(code, nop[:]...)
code = append(code, exit[:]...)
cubin, err := nvdisasmoracle.WrapCUBIN(nvdisasmoracle.EFlagsSM121a, "probe", code)
if err != nil {
fmt.Println("error:", err)
return
}
ef, err := elfinspect.OpenCubin(bytes.NewReader(cubin))
if err != nil {
fmt.Println("elf error:", err)
return
}
defer ef.Close()
sec := ef.Section(".text.probe")
eflags := binary.LittleEndian.Uint32(cubin[48:52])
fmt.Printf("machine=%v flags=%#x text=%d\n", ef.Machine, eflags, sec.Size)
}
Output: machine=EM_CUDA flags=0x7908 text=32
Types ¶
type Oracle ¶
type Oracle struct {
Command []string
CacheDir string
TempDir string
// ToolID optionally pins the tool identity used to scope cache keys.
// When empty, [Oracle.ToolIdentity] hashes the resolved executable (and
// any regular-file arguments) and records nvdisasm --version output.
ToolID string
// contains filtered or unexported fields
}
An Oracle runs nvdisasm through Command (a local binary, a Lima VM prefix such as "limactl shell okcnvcc -- nvdisasm", or an SSH prefix such as "ssh gpu-host /usr/local/cuda/bin/nvdisasm") with optional SHA-256 caching in CacheDir.
When CacheDir is non-empty, cache keys include the full Command slice and Oracle.ToolIdentity (either the caller-pinned ToolID or a verified executable digest and nvdisasm version) so cached results cannot leak across different toolkits, command changes, or in-place executable replacements.
func FromEnv ¶
FromEnv constructs an Oracle from EnvCommand and EnvCacheDir. It reports false when EnvCommand is unset or blank so gated tests can skip.
Example ¶
package main
import (
"fmt"
"os"
"github.com/tmc/lib/nvidia/internal/nvdisasmoracle"
)
func main() {
os.Unsetenv(nvdisasmoracle.EnvCommand)
_, ok := nvdisasmoracle.FromEnv()
fmt.Println("configured:", ok)
}
Output: configured: false
func (*Oracle) RunCUBIN ¶
RunCUBIN runs nvdisasm with flags on cubin and returns its raw stdout bytes. When CacheDir is set, successful outputs are keyed by SHA-256 of Command, Oracle.ToolIdentity, flags, and cubin bytes.
func (*Oracle) ToolIdentity ¶
ToolIdentity returns the tool identity string used to scope cache keys for o. If o.ToolID is set, it returns that pinned identity. Otherwise it verifies the configured executable (locally, over SSH, or inside a Lima VM) by hashing the resolved binary and any regular-file arguments, plus capturing nvdisasm --version output when invoking nvdisasm.
func (*Oracle) Words ¶
Words disassembles a slice of 16-byte little-endian instruction words via "nvdisasm -b <arch> -json", placing word i at byte offset 16*i. Words that nvdisasm rejects (either via an illegal-encoding error on stderr or an empty JSON instruction record) return WordResult{OK: false}.
type WordResult ¶
type WordResult struct {
OK bool `json:"ok"`
Predicate string `json:"predicate,omitempty"`
Opcode string `json:"opcode,omitempty"`
Operands string `json:"operands,omitempty"`
}
WordResult is nvdisasm's JSON disassembly of one 128-bit instruction word. OK is false when nvdisasm rejects the word as an illegal encoding or emits an empty JSON instruction stub.
func (WordResult) Normalized ¶
func (w WordResult) Normalized() (predicate, opcode, operands string)
Normalized adjusts WordResult for nvdisasm's JSON formatting quirk on P2R and R2P, where the destination/source PR token is placed in Predicate without "@" while any guarding "@Pn" predicate appears in Operands.
Example ¶
package main
import (
"fmt"
"github.com/tmc/lib/nvidia/internal/nvdisasmoracle"
)
func main() {
r := nvdisasmoracle.WordResult{
OK: true,
Predicate: "PR",
Opcode: "P2R",
Operands: "@P0,R11,R0,RZ",
}
pred, op, args := r.Normalized()
fmt.Printf("%s | %s | %s\n", pred, op, args)
fmt.Println(r.String())
}
Output: @P0 | P2R | PR,R11,R0,RZ @P0 P2R PR,R11,R0,RZ
func (WordResult) String ¶
func (w WordResult) String() string