Skip to content

Instantly share code, notes, and snippets.

@bzz

bzz/README.md Secret

Forked from juanjux/py2ast2pb.go
Last active July 5, 2017 12:34
Show Gist options
  • Save bzz/c0c3dbcab5fecbe48e22167e2ad78595 to your computer and use it in GitHub Desktop.
Save bzz/c0c3dbcab5fecbe48e22167e2ad78595 to your computer and use it in GitHub Desktop.

This is example of reproducing a panic on reading UAST from client.ParseUAST(), while iterating over files in Git repo using go-git.

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x8 pc=0x1186147]

goroutine 1 [running]:
github.com/bblfsh/sdk/uast.(*Node).ProtoSize(0x0, 0xc4201ddda0)

Steps to reproduce:

git clone https://github.com/damoeb/kalipo.git
go run pyFromGit2ast2pb.go

You can change "java" to "python" (L42 and L60) back and forth you can get more interesting results.

I.e

package main
import (
"context"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"time"
"github.com/bblfsh/sdk/protocol"
"gopkg.in/src-d/go-git.v4"
"gopkg.in/src-d/go-git.v4/plumbing/object"
"google.golang.org/grpc"
)
// Note: start the server with:
// docker run --privileged -p 9432:9432 --name bblfsh bblfsh/server
func main() {
if len(os.Args) < 2 {
fmt.Printf("Use %s <path-to-clone>\n", os.Args[0])
os.Exit(1)
}
// Connect to the running server
conn, err := grpc.Dial("0.0.0.0:9432", grpc.WithTimeout(time.Second*2), grpc.WithInsecure())
checkIfError(err)
client := protocol.NewProtocolServiceClient(conn)
r, err := git.PlainOpen(os.Args[1])
checkIfError(err)
ref, err := r.Head()
checkIfError(err)
commit, err := r.CommitObject(ref.Hash())
checkIfError(err)
tree, err := commit.Tree()
checkIfError(err)
err = tree.Files().ForEach(func(f *object.File) error {
if !strings.HasSuffix(f.Name, ".py") {
return nil
}
contentstr, err := f.Contents()
if err != nil {
fmt.Printf("\t\tReading file:%s failed, reason: %s\n", f.Name, err)
return err
}
fmt.Printf("100644 blob %s %s\n", f.Hash, f.Name)
fmt.Printf("\tParsing file:%s, size:%d\n", f.Name, len(contentstr))
if err != nil {
fmt.Printf("\t\tGeting content for %s failed, reason: %s\n", f.Name, err)
return err
}
req := &protocol.ParseUASTRequest{Filename: f.Name,
Content: contentstr,
Language: "python"}
resp, err := client.ParseUAST(context.TODO(), req)
if err != nil {
fmt.Printf("\t\tError - ParseUAST failed, reposne is nil, error:%v for %s\n", err, f.Name)
return err
} else if resp == nil {
fmt.Printf("\t\tNo error, but - ParseUAST failed, response is nil\n")
return err
} else if (len(resp.Errors) != 0) || (resp.Status != protocol.Ok) {
fmt.Printf("\t\tNo error, but - ParseUAST status:%s, error:%v for %s\n", resp.Status, resp.Errors, f.Name)
return err
}
// FIXME: Crashes sometimes here, check resp.UAST? :(
data, err := resp.UAST.Marshal()
if err != nil {
fmt.Println("]\t\tError: failed to save UAST to .pb for file: ", f.Name)
return err
}
outFile := "out/" + filepath.Base(f.Name) + ".pb"
err = ioutil.WriteFile(outFile, data, 00644)
checkIfError(err)
fmt.Printf("\tUAST for %s, saved to %s\n", f.Name, outFile)
return nil
})
}
func checkIfError(err error) {
if err == nil {
return
}
fmt.Printf("\x1b[31;1m%s\x1b[0m\n", fmt.Sprintf("error: %s", err))
os.Exit(1)
}
@juanjux
Copy link

juanjux commented Jun 21, 2017

Could you try again with the new ~master of the Python driver? Anyway it seems there is an issue with the SDK not checking nil pointers somewhere.

@bzz
Copy link
Author

bzz commented Jun 23, 2017

Thank you! Will follow your instructions to get everything latest locally bblfsh/bblfshd#34 (comment) and report issues on relevant repositories (server/python)!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment