Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 19 additions & 9 deletions cmd/orchestratorvet/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ the package under analysis passes it to one of
- `(*task.TaskRegistry).AddOrchestratorVersion`
- `(*task.TaskRegistry).AddOrchestratorNVersion`

From each of those roots it follows the whole-package call graph through
same-package named functions, methods, function variables whose target can be
From each of those roots it follows the call graph through named functions,
concrete methods, function variables whose target can be
proven, and the nested function literals whose bodies are proven to execute --
through a direct call, a call through a single-assignment function variable,
a raw `go` or `defer` call,
Expand All @@ -51,6 +51,11 @@ terminates because each function is visited once, and a helper reached from
several orchestrators is reported once. Activity bodies, entity bodies, and any
function not reachable from a registered orchestrator are never reported.

The handler itself or a helper it calls can live in another package in the
same Go module. An imported hazard is reported at the local registration or
call site, with a representative call path and no automatic fix. Dependencies
provide summaries even when only the registration package is named in `go vet`.

## Checks

### Wall clock
Expand Down Expand Up @@ -224,14 +229,19 @@ The analyzer reports only what it can prove, and stays silent otherwise.
- A random generator is only reported when its seed provably reaches a source
that varies between runs. A constant seed, a seed taken from orchestration
input, and a seed the analyzer cannot follow are all left alone.
- Every diagnostic is deduplicated by source position and category, so a shared
helper is reported once no matter how many orchestrators reach it.
- Diagnostics are deduplicated by source position and category. For an imported
call, the analyzer shows one witness per hazard category at that call site.

## Limitations

- The analyzer runs one package at a time. Helpers, activity registrations, and
orchestrator bodies in other packages are invisible to it, so hazards there
are not reported.
- Imported bodies are summarized through Go analysis facts. The analyzer does
not follow bodies across module boundaries, inside the standard library or
the SDK's `task` package, or when module metadata is unavailable. Known
hazardous APIs are still checked at their call sites. Summaries do not trust
the initial value of an exported package variable, which an importer can
reassign.
- Registry-name completeness and proofs about non-progressing loops do not
use imported summaries.
- Calls through interface values, struct fields, and function parameters cannot
be resolved to a body and are not followed.
- Version-aware name resolution is approximated: a task name is considered
Expand Down Expand Up @@ -261,7 +271,7 @@ cd cmd/orchestratorvet && go test ./...
cd cmd/orchestratorvet && go test -run '^$' -bench . ./analysis/orchestratorgo/...
```

The benchmarks measure orchestrator-count scaling, call-graph depth, and the
early exit taken by the overwhelming majority of packages, which register no
The benchmarks measure orchestrator-count scaling, call-graph depth, and
summary construction for packages that register no
orchestrators at all. They type-check against the same durable task stub the
fixtures use, so the two cannot drift apart.
39 changes: 28 additions & 11 deletions cmd/orchestratorvet/analysis/orchestratorgo/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// package registers with task.TaskRegistry.
//
// The analyzer starts from every proven registration in the package under
// analysis, follows the call graph through same-package named functions,
// methods, resolvable function variables, and the nested function literals
// analysis, follows the call graph through named functions, methods,
// resolvable function variables, and the nested function literals
// whose bodies are proven to execute -- through a direct call whose callee
// resolves to the literal, a call through a single-assignment variable, a raw go or
// defer call, or the callback argument of an explicitly modeled invoker such
Expand Down Expand Up @@ -32,24 +32,27 @@
// - registration forms that task.TaskRegistry rejects or that derive unstable
// names, and registered orchestrators invoked as plain Go calls.
//
// The analyzer stays silent whenever proof is unavailable. Dynamic registration,
// wildcard registration, cross-package registration, functions reached only
// through values it cannot resolve, and code outside orchestrator reachability
// are all left alone.
// Same-module imports contribute replay-hazard summaries via analysis facts.
// These summaries do not establish registry completeness or loop progress.
// The analyzer stays silent when it cannot resolve a call or registration to
// a body or a known hazard, and outside registered orchestrator reachability.
package orchestratorgo

import (
"go/token"

"golang.org/x/tools/go/analysis"
)

var includeTestFiles bool

// Analyzer reports replay hazards in registered durable task orchestrators.
var Analyzer = &analysis.Analyzer{
Name: "orchestratorgo",
Doc: "report nondeterministic and replay-unsafe code in durable task orchestrators",
URL: "https://github.com/microsoft/durabletask-go/tree/main/cmd/orchestratorvet/analysis/orchestratorgo",
Run: run,
Name: "orchestratorgo",
Doc: "report nondeterministic and replay-unsafe code in durable task orchestrators",
URL: "https://github.com/microsoft/durabletask-go/tree/main/cmd/orchestratorvet/analysis/orchestratorgo",
Run: run,
FactTypes: []analysis.Fact{new(hazardFact)},
}

func init() {
Expand All @@ -63,8 +66,12 @@ func init() {

func run(pass *analysis.Pass) (any, error) {
index := newPackageIndex(pass)
facts := newHazardFacts(index)
if facts != nil {
facts.export()
}
registry := collectRegistrations(pass, index)
if len(registry.roots) == 0 {
if len(registry.roots) == 0 && len(registry.importedRoots) == 0 {
return nil, nil
}
reach := reachableFunctions(index, registry.roots)
Expand All @@ -75,8 +82,18 @@ func run(pass *analysis.Pass) (any, error) {
registry: registry,
reported: make(map[reportKey]bool),
}
if facts != nil {
for _, root := range registry.importedRoots {
facts.report(check, root.pos, root.function)
}
}
for _, node := range reach.order {
check.checkFunction(node)
if facts != nil {
index.forEachTarget(node, func(pos token.Pos, target functionTarget) {
facts.report(check, pos, target.imported)
})
}
}
check.reportClockDiagnostics()
return nil, nil
Expand Down
31 changes: 21 additions & 10 deletions cmd/orchestratorvet/analysis/orchestratorgo/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package orchestratorgo_test

import (
"fmt"
"go/types"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -85,6 +86,13 @@ func helper%d_%d(value int) int {
}
}
builder.WriteString("}\n")
if !register {
builder.WriteString("\nfunc Exported(ctx *task.OrchestrationContext) {\n")
for i := 0; i < orchestrators; i++ {
fmt.Fprintf(&builder, "\t_, _ = orchestrator%d(ctx)\n", i)
}
builder.WriteString("}\n")
}
return builder.String()
}

Expand Down Expand Up @@ -115,14 +123,17 @@ func loadBenchmarkPass(b *testing.B, source string) *analysis.Pass {
}
pkg := loaded[0]
return &analysis.Pass{
Analyzer: orchestratorgo.Analyzer,
Fset: pkg.Fset,
Files: pkg.Syntax,
Pkg: pkg.Types,
TypesInfo: pkg.TypesInfo,
TypesSizes: pkg.TypesSizes,
Report: func(analysis.Diagnostic) {},
ResultOf: map[*analysis.Analyzer]any{},
Analyzer: orchestratorgo.Analyzer,
Fset: pkg.Fset,
Files: pkg.Syntax,
Pkg: pkg.Types,
TypesInfo: pkg.TypesInfo,
TypesSizes: pkg.TypesSizes,
Report: func(analysis.Diagnostic) {},
ResultOf: map[*analysis.Analyzer]any{},
Module: &analysis.Module{Path: "bench"},
ImportObjectFact: func(types.Object, analysis.Fact) bool { return false },
ExportObjectFact: func(types.Object, analysis.Fact) {},
}
}

Expand Down Expand Up @@ -157,8 +168,8 @@ func BenchmarkAnalyzerCallGraphDepth(b *testing.B) {
}
}

// BenchmarkAnalyzerNoOrchestrators measures the cost of the early exit taken by
// the overwhelming majority of packages, which register nothing at all.
// BenchmarkAnalyzerNoOrchestrators measures summary construction without
// registrations.
func BenchmarkAnalyzerNoOrchestrators(b *testing.B) {
runAnalyzerBenchmark(b, generateBenchmarkPackage(8, 4, false))
}
29 changes: 24 additions & 5 deletions cmd/orchestratorvet/analysis/orchestratorgo/checks.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ type checker struct {
registry *registrySet
reported map[reportKey]bool

// In summary mode the checker collects only hazards proved within a body.
collectHazard func(category, message string)

// Clock fixes are reported after all reachable functions have been checked,
// so each file's replacements and import cleanup form one atomic fix.
clockDiagnostics []analysis.Diagnostic
Expand All @@ -48,6 +51,10 @@ func (c *checker) report(
format string,
args ...any,
) {
if c.collectHazard != nil {
c.collectHazard(category, fmt.Sprintf(format, args...))
return
}
key := reportKey{pos: pos, category: category}
if c.reported[key] {
return
Expand Down Expand Up @@ -134,17 +141,23 @@ func (c *checker) inspect(node ast.Node, stack []ast.Node, file *ast.File) {
)
}
case *ast.ForStmt:
c.checkUnboundedLoop(node)
if c.registry != nil {
c.checkUnboundedLoop(node)
}
case *ast.CallExpr:
c.checkCall(node, stack, file)
}
}

func (c *checker) checkGoStmt(node *ast.GoStmt, stack []ast.Node, file *ast.File) {
var fixes []analysis.SuggestedFix
if c.collectHazard == nil {
fixes = c.goStatementFix(node, contextParameter(c.pass, stack), file)
}
c.report(
node.Go,
categoryGoroutine,
c.goStatementFix(node, contextParameter(c.pass, stack), file),
fixes,
"raw go statement is not deterministic in an orchestrator; "+
"use (*task.OrchestrationContext).Go",
)
Expand All @@ -158,7 +171,9 @@ func (c *checker) checkCall(call *ast.CallExpr, stack []ast.Node, file *ast.File
if function == nil {
return
}
c.checkDirectOrchestratorCall(call, function)
if c.registry != nil {
c.checkDirectOrchestratorCall(call, function)
}
if owner, name, ok := methodOwner(function); ok {
c.checkMethodCall(call, function, owner, name)
return
Expand Down Expand Up @@ -215,10 +230,14 @@ func (c *checker) checkPackageCall(
switch path {
case "time":
if replacement, ok := wallClockFunctions[name]; ok {
var fixes []analysis.SuggestedFix
if c.collectHazard == nil {
fixes = c.currentTimeFix(call, name, stack, file)
}
c.report(
call.Lparen,
categoryWallClock,
c.currentTimeFix(call, name, stack, file),
fixes,
"time.%s is not deterministic in an orchestrator; "+
"use (*task.OrchestrationContext).%s",
name,
Expand Down Expand Up @@ -334,7 +353,7 @@ func (c *checker) checkMethodCall(
name := function.Name()
switch ownerPath {
case taskPackagePath:
if ownerName == "OrchestrationContext" {
if ownerName == "OrchestrationContext" && c.registry != nil {
c.checkOrchestrationContextCall(call, name)
}
return
Expand Down
Loading