nvdisasmoracle

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 4, 2026 License: Apache-2.0 Imports: 18 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
const (
	OSABICUDA    uint8  = 0x41
	ABIVersion1  uint8  = 1
	EFlagsSM121a uint32 = 0x7908
)

CUDA ELF64 header constants accepted by nvdisasm for sm_121a.

View Source
const (
	EnvCommand  = "NVDISASM_CMD"
	EnvCacheDir = "NVDISASM_CACHE"
)

Environment variable names configuring FromEnv.

Variables

This section is empty.

Functions

func EncodeWord

func EncodeWord(lo, hi uint64) [16]byte

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

func WrapCUBIN(eflags uint32, kernel string, code []byte) ([]byte, error)

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

func FromEnv() (*Oracle, bool)

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

func (o *Oracle) RunCUBIN(ctx context.Context, cubin []byte, flags ...string) ([]byte, error)

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

func (o *Oracle) ToolIdentity(ctx context.Context) (string, error)

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

func (o *Oracle) Words(ctx context.Context, arch string, words [][16]byte) ([]WordResult, error)

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

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL