memguard

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 11, 2026 License: MIT Imports: 3 Imported by: 0

README

mem-guard

Static analysis for unbounded memory growth in Go programs.

mem-guard detects loop and recursion patterns that can grow data structures without bound — the kind of latent bugs that cause OOMs in production after weeks of uptime.

Install

go install github.com/ProfHercules/mem-guard/cmd/mem-guard@latest

Or download a pre-built binary from the releases page.

Usage

mem-guard ./...
mem-guard -json ./...
mem-guard -no-exit-code ./pkg/server

Exit codes: 0 clean, 1 error-severity findings, 2 usage or load failure.

Patterns detected

  • AppendInLoopappend() inside an unbounded loop
  • MapGrowInLoop — map writes inside an unbounded loop
  • UnboundedConcat — string concatenation inside an unbounded loop
  • GoroutineSpawnInLoopgo func() inside an unbounded loop
  • RecursiveAccumulator — recursive function that grows a *[]T or map parameter
  • CrossBoundaryGrowth — unbounded loop that calls a function which grows a passed-by-reference collection
  • ChannelDrivenGrowth — collecting channel values into a growing data structure

A loop is considered unbounded unless it is provably bounded (counted for, range over a collection, or len()-bounded exit).

Library usage

import memguard "github.com/ProfHercules/mem-guard"

findings, err := memguard.Analyze([]string{"./..."}, memguard.Options{})
if err != nil {
    log.Fatal(err)
}
for _, f := range findings {
    fmt.Println(f)
}

License

MIT

Documentation

Overview

Package memguard detects unbounded memory growth patterns in Go programs.

It loads packages with golang.org/x/tools/go/packages, builds the SSA representation, and runs heuristics that flag append/map-grow/string-concat/ goroutine-spawn inside unbounded loops, recursive accumulators, cross-package growth via passed-by-reference collections, and channel-driven growth.

The library entry point is Analyze. The CLI lives at cmd/mem-guard.

Index

Examples

Constants

View Source
const (
	SeverityInfo    = finding.SeverityInfo
	SeverityWarning = finding.SeverityWarning
	SeverityError   = finding.SeverityError
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Finding

type Finding = finding.Finding

Re-exports so callers can use a single package import.

func Analyze

func Analyze(patterns []string, opts Options) ([]Finding, error)

Analyze loads the given Go package patterns and runs the configured heuristics. Findings are returned sorted (errors first, then file/line).

Example
package main

import (
	"fmt"

	memguard "github.com/ProfHercules/mem-guard"
)

func main() {
	findings, err := memguard.Analyze([]string{"./testdata/bounded"}, memguard.Options{})
	if err != nil {
		fmt.Println("error:", err)
		return
	}
	fmt.Println("findings:", len(findings))
}
Output:
findings: 0

type Heuristic

type Heuristic = heuristics.Heuristic

Re-exports so callers can use a single package import.

func DefaultHeuristics

func DefaultHeuristics() []Heuristic

DefaultHeuristics returns a fresh slice of the built-in heuristics.

type Options

type Options struct {
	// Dir is the working directory for package loading. Empty means cwd.
	Dir string
	// Heuristics overrides the built-in heuristic set. Nil means
	// DefaultHeuristics().
	Heuristics []Heuristic
}

Options controls an Analyze call.

type Severity

type Severity = finding.Severity

Re-exports so callers can use a single package import.

Directories

Path Synopsis
Package analysis loads Go packages, builds the SSA program, constructs the call graph, and pre-computes loop information that mem-guard heuristics consume.
Package analysis loads Go packages, builds the SSA program, constructs the call graph, and pre-computes loop information that mem-guard heuristics consume.
cmd
mem-guard command
Command mem-guard is the CLI for github.com/ProfHercules/mem-guard.
Command mem-guard is the CLI for github.com/ProfHercules/mem-guard.
Package finding defines the Finding type produced by mem-guard heuristics along with severity levels and human/JSON printers.
Package finding defines the Finding type produced by mem-guard heuristics along with severity levels and human/JSON printers.
Package heuristics implements the built-in mem-guard checks.
Package heuristics implements the built-in mem-guard checks.
internal
cli
Package cli implements the mem-guard command-line interface.
Package cli implements the mem-guard command-line interface.

Jump to

Keyboard shortcuts

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