2019-09-17 14:15:22 +00:00
|
|
|
---
|
|
|
|
layout: default
|
|
|
|
title: Integrating a Go project
|
|
|
|
parent: Setting up a new project
|
|
|
|
grand_parent: Getting started
|
|
|
|
nav_order: 1
|
|
|
|
permalink: /getting-started/new-project-guide/go-lang/
|
|
|
|
---
|
|
|
|
|
|
|
|
# Integrating a Go project
|
|
|
|
{: .no_toc}
|
|
|
|
|
|
|
|
- TOC
|
|
|
|
{:toc}
|
|
|
|
---
|
|
|
|
|
|
|
|
The process of integrating a project written in Go with OSS-Fuzz is very similar
|
|
|
|
to the general
|
|
|
|
[Setting up a new project]({{ site.baseurl }}/getting-started/new-project-guide/)
|
|
|
|
process. The key specifics of integrating a Go project are outlined below.
|
|
|
|
|
|
|
|
## Go-fuzz support
|
|
|
|
|
|
|
|
OSS-Fuzz supports **go-fuzz** in the
|
2020-04-14 04:57:30 +00:00
|
|
|
[libFuzzer compatible mode](https://github.com/mdempsky/go114-fuzz-build)
|
2019-09-17 14:15:22 +00:00
|
|
|
only. In that mode, fuzz targets for Go use the libFuzzer engine with native Go
|
|
|
|
coverage instrumentation. Binaries compiled in this mode provide the same
|
|
|
|
libFuzzer command line interface as non-Go fuzz targets.
|
|
|
|
|
|
|
|
## Project files
|
|
|
|
|
2020-04-14 04:57:30 +00:00
|
|
|
First, you need to write a Go fuzz target that accepts a stream of bytes and
|
|
|
|
calls the program API with that. This fuzz target should reside in your project
|
|
|
|
repository
|
|
|
|
([example](https://github.com/golang/go/blob/4ad13555184eb0697c2e92c64c1b0bdb287ccc10/src/html/fuzz.go#L13)).
|
|
|
|
|
2019-09-17 14:15:22 +00:00
|
|
|
The structure of the project directory in OSS-Fuzz repository doesn't differ for
|
2020-01-30 23:36:44 +00:00
|
|
|
projects written in Go. The project files have the following Go specific
|
|
|
|
aspects.
|
2019-09-17 14:15:22 +00:00
|
|
|
|
|
|
|
### project.yaml
|
|
|
|
|
2020-01-30 23:36:44 +00:00
|
|
|
The `language` attribute must be specified.
|
|
|
|
|
|
|
|
```yaml
|
|
|
|
language: go
|
|
|
|
```
|
|
|
|
|
|
|
|
The only supported fuzzing engine and sanitizer are `libfuzzer` and `address`,
|
|
|
|
respectively.
|
2019-09-17 14:15:22 +00:00
|
|
|
[Example](https://github.com/google/oss-fuzz/blob/356f2b947670b7eb33a1f535c71bc5c87a60b0d1/projects/syzkaller/project.yaml#L7):
|
|
|
|
|
|
|
|
```yaml
|
|
|
|
fuzzing_engines:
|
|
|
|
- libfuzzer
|
|
|
|
sanitizers:
|
|
|
|
- address
|
|
|
|
```
|
|
|
|
|
|
|
|
### Dockerfile
|
|
|
|
|
2021-09-08 16:13:52 +00:00
|
|
|
The Dockerfile should start by `FROM gcr.io/oss-fuzz-base/base-builder-go`
|
|
|
|
|
2019-09-17 14:15:22 +00:00
|
|
|
The OSS-Fuzz builder image has the latest stable release of Golang installed. In
|
2021-02-22 23:25:47 +00:00
|
|
|
order to install dependencies of your project, add `RUN git clone ...` command to
|
2019-09-17 14:15:22 +00:00
|
|
|
your Dockerfile.
|
|
|
|
[Example](https://github.com/google/oss-fuzz/blob/356f2b947670b7eb33a1f535c71bc5c87a60b0d1/projects/syzkaller/Dockerfile#L23):
|
|
|
|
|
|
|
|
```dockerfile
|
|
|
|
# Dependency for one of the fuzz targets.
|
2021-02-22 23:25:47 +00:00
|
|
|
RUN git clone --depth 1 https://github.com/ianlancetaylor/demangle
|
2019-09-17 14:15:22 +00:00
|
|
|
```
|
|
|
|
|
2021-02-22 23:25:47 +00:00
|
|
|
go-fuzz will then automatically download the dependencies based on the go.mod file
|
2020-11-25 15:40:15 +00:00
|
|
|
|
2019-09-17 14:15:22 +00:00
|
|
|
### build.sh
|
|
|
|
|
2020-04-14 04:57:30 +00:00
|
|
|
In order to build a Go fuzz target, you need to call `go-fuzz`
|
2019-09-17 14:15:22 +00:00
|
|
|
command first, and then link the resulting `.a` file against
|
|
|
|
`$LIB_FUZZING_ENGINE` using the `$CXX $CXXFLAGS ...` command.
|
|
|
|
|
2020-11-20 18:55:33 +00:00
|
|
|
The best way to do this is by using a `compile_go_fuzzer` script,
|
|
|
|
as it also supports coverage builds.
|
2019-09-17 14:15:22 +00:00
|
|
|
|
2020-11-20 18:55:33 +00:00
|
|
|
A usage example from go-dns project is
|
2019-09-17 14:15:22 +00:00
|
|
|
|
2020-11-20 18:55:33 +00:00
|
|
|
```sh
|
|
|
|
compile_go_fuzzer github.com/miekg/dns FuzzNewRR fuzz_newrr fuzz
|
2019-09-17 14:15:22 +00:00
|
|
|
```
|
2020-11-20 18:55:33 +00:00
|
|
|
|
|
|
|
Arguments are :
|
|
|
|
* path of the package with the fuzz target
|
|
|
|
* name of the fuzz function
|
|
|
|
* name of the fuzzer to be built
|
|
|
|
* optional tag to be used by `go build` and such
|