audit.go raw

   1  package iskra
   2  
   3  import (
   4  	"os"
   5  	"path/filepath"
   6  	"sort"
   7  	"strings"
   8  )
   9  
  10  // AuditReport is the isomorphism audit of one corpus.
  11  type AuditReport struct {
  12  	Functions       int
  13  	SelfIso         int
  14  	TotalLocals     int64
  15  	TotalExternals  int64
  16  	DistinctHash24  int
  17  	IsoCollisions   int
  18  	PairsChecked    int
  19  	LargestLocals   int
  20  	LargestFuncName string
  21  }
  22  
  23  // AuditCorpus walks the ast/ directory of a corpus and measures, per function
  24  // AST dump, the self-isomorphism and the cross-function selectivity of the
  25  // signatures.
  26  //
  27  // The moxie audit tests each signature against itself, which cannot fail for
  28  // any function whose extraction produced a signature; the number is 100% by
  29  // construction. The measurement that carries information is the last two
  30  // fields: how many distinct 24-bit hashes the signatures produce, and how many
  31  // of the compared pairs are fully isomorphic for different functions.
  32  func AuditCorpus(corpusDir string) (*AuditReport, error) {
  33  	astDir := filepath.Join(corpusDir, "ast")
  34  	entries, err := os.ReadDir(astDir)
  35  	if err != nil {
  36  		return nil, err
  37  	}
  38  	rep := &AuditReport{}
  39  	type since struct {
  40  		name string
  41  		sig  *BindingSignature
  42  	}
  43  	var sigs []since
  44  	for _, e := range entries {
  45  		if e.IsDir() {
  46  			continue
  47  		}
  48  		name := e.Name()
  49  		if !strings.Contains(name, "func_") && !strings.Contains(name, "metho") {
  50  			continue
  51  		}
  52  		data, err := os.ReadFile(filepath.Join(astDir, name))
  53  		if err != nil {
  54  			return nil, err
  55  		}
  56  		st := ExtractSymbols(string(data))
  57  		bg := ExtractBindings(st)
  58  		sig := SignatureWithTypes(st)
  59  		rep.Functions++
  60  		if sig.IsIsomorphic(sig) {
  61  			rep.SelfIso++
  62  		}
  63  		rep.TotalLocals += int64(len(bg.Bindings))
  64  		rep.TotalExternals += int64(len(bg.Externals))
  65  		if len(bg.Bindings) > rep.LargestLocals {
  66  			rep.LargestLocals = len(bg.Bindings)
  67  			rep.LargestFuncName = name
  68  		}
  69  		sigs = append(sigs, since{name: name, sig: sig})
  70  	}
  71  	hashes := make(map[uint32]struct{}, len(sigs))
  72  	for _, s := range sigs {
  73  		hashes[SignatureHash24(s.sig)] = struct{}{}
  74  	}
  75  	rep.DistinctHash24 = len(hashes)
  76  	sort.Slice(sigs, func(i, j int) bool { return sigs[i].name < sigs[j].name })
  77  	n := len(sigs)
  78  	for i := 0; i < n; i++ {
  79  		limit := i + 51
  80  		if limit > n {
  81  			limit = n
  82  		}
  83  		for j := i + 1; j < limit; j++ {
  84  			rep.PairsChecked++
  85  			if sigs[i].sig.IsIsomorphic(sigs[j].sig) {
  86  				rep.IsoCollisions++
  87  			}
  88  		}
  89  	}
  90  	return rep, nil
  91  }
  92  
  93  // SelfIsoRate is the percentage of functions whose signature is isomorphic to
  94  // itself.
  95  func (r *AuditReport) SelfIsoRate() int {
  96  	if r.Functions == 0 {
  97  		return 0
  98  	}
  99  	return r.SelfIso * 100 / r.Functions
 100  }
 101  
 102  // HashUniqueness is the percentage of functions with a distinct 24-bit hash.
 103  func (r *AuditReport) HashUniqueness() int {
 104  	if r.Functions == 0 {
 105  		return 0
 106  	}
 107  	return r.DistinctHash24 * 100 / r.Functions
 108  }
 109