Skip to content

Commit

Permalink
Experimental code to start java class reachability.
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverchang committed Jan 7, 2025
1 parent 71e372e commit 5e4d60b
Show file tree
Hide file tree
Showing 3 changed files with 461 additions and 0 deletions.
61 changes: 61 additions & 0 deletions experimental/javareach/cmd/reachable/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package main

import (
"flag"
"fmt"
"io"
"log"
"os"

"github.com/google/osv-scanner/experimental/javareach"
)

func main() {
flag.Parse()
for _, filename := range flag.Args() {
reader, err := os.Open(filename)
if err != nil {
log.Printf("failed to open %s: %v", filename, err)
continue
}
err = EnumerateReachability(reader)
if err != nil {
log.Printf("failed to enumerate reachability for %s: %v", filename, err)
}
}
}

// TODO:
// - Transitively resolve dependencies and download dependent .jar files.
// - Detect uses of reflection
// - See if we should do a finer grained analysis to only consider referenced
// classes where a method is called/referenced.
func EnumerateReachability(r io.Reader) error {
cf, err := javareach.ParseClass(r)
if err != nil {
return err
}

thisClass, err := cf.ConstantPoolClass(int(cf.ThisClass))
if err != nil {
return err
}
fmt.Printf("this class: %s\n", thisClass)

for i, cp := range cf.ConstantPool {
if int(cf.ThisClass) == i {
// Don't consider the this class itself.
continue
}

if cp.Type() == javareach.ConstantKindClass {
class, err := cf.ConstantPoolClass(i)
if err != nil {
return err
}
fmt.Printf("class: %s\n", class)
}
}

return nil
}
3 changes: 3 additions & 0 deletions experimental/javareach/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/google/osv-scanner/experimental/javareach

go 1.23
Loading

0 comments on commit 5e4d60b

Please sign in to comment.