<commit_before># Fuchsia support

For information about checking out and building Fuchsia see
[Getting Started](https://fuchsia.dev/fuchsia-src/get-started)
and [Source Code](https://fuchsia.dev/fuchsia-src/get-started/get_fuchsia_source).

## Prerequisites

To run syzkaller with a fuchsia target, you will need:

* A fuchsia checkout.

The rest of the document will use the following environment variables:

* `SOURCEDIR` path of your fuchsia checkout.

## Building Fuchsia

NOTE: Inside `${SOURCEDIR}/src/testing/fuzzing/syzkaller/BUILD.gnsrc/testing/fuzzing/syzkaller/BUILD.gn`
you need to replace the line with `"$(src)/executor/kvm.S.h"` by `"${src}/executor/kvm_amd64.S.h"`

To build fuchsia run:

```shell
$ fx --dir "out/arm64" set core.arm64 \
  --with-base "//bundles:tools" \
  --with-base "//src/testing/fuzzing/syzkaller" \
  --args=syzkaller_dir='"/full/path/to/syzkaller"' \
  --variant=kasan
$ fx clean-build
```

And

```shell
$ fx --dir "out/x64" set core.x64 \
  --with-base "//bundles:tools" \
  --with-base "//src/testing/fuzzing/syzkaller" \
  --args=syzkaller_dir='"/full/path/to/syzkaller"' \
  --variant=kasan
$ fx clean-build
```

## Building binaries for fuchsia

To build all the binaries required for running syzkaller in fuchsia, run:

```
$ make TARGETOS=fuchsia TARGETARCH=amd64 \
    SOURCEDIR=path/to/fuchsia/checkout
```

## Running syz-manager

Running syz-manager requires you to have built fuchsia previously, and added the ssh keys to the fuchsia.zbi image:

```
$ ${SOURCEDIR}/out/x64/host_x64/zbi -o ${SOURCEDIR}/out/x64/fuchsia-ssh.zbi ${SOURCEDIR}/out/x64/fuchsia.zbi --entry "data/ssh/authorized_keys=${SOURCEDIR}/.ssh/authorized_keys"
```

You will also need to extend the `fvm` image:

```
$ cp "${SOURCEDIR}/out/x64/obj/build/images/fuchsia/fuchsia/fvm.blk" "${SOURCEDIR}/out/x64/obj/build/images/fuchsia/fuchsia/fvm-extended.blk"
$ ${SOURCEDIR}/out/x64/host_x64/fvm "${SOURCEDIR}/out/x64/obj/build/images/fuchsia/fuchsia/fvm-extended.blk" extend --length 3G
```

Note: This needs to be repeated after each `fx build`.

Run `syz-manager` with a config along the lines of:
```
{
        "name": "fuchsia",
        "target": "fuchsia/amd64",
        "http": ":12345",
        "workdir": "/workdir.fuchsia",
        "kernel_obj": "/fuchsia/out/x64.zircon/kernel-x64-gcc",
        "syzkaller": "/syzkaller",
        "image": "/fuchsia/out/x64/obj/build/images/fuchsia/fuchsia/fvm-extended.blk",
        "sshkey": "/fuchsia/.ssh/pkey",
        "reproduce": false,
        "cover": false,
        "procs": 8,
        "type": "qemu",
        "vm": {
                "count": 10,
                "cpu": 4,
                "mem": 2048,
                "kernel": "/fuchsia/out/x64/multiboot.bin",
                "initrd": "/fuchsia/out/x64/fuchsia-ssh.zbi"
        }
}
```

## Update syscall and fidl definitions

Syscall descriptions live in the `sys/fuchsia` folder. To update a syscall, you need to modify the `.txt` file that contains it, make sure your new definition matches the one in zircon's [syscalls.abigen](https://fuchsia.googlesource.com/fuchsia/+/master/zircon/system/public/zircon/syscalls.abigen) file. **If the syscall was used in `executor/common_fuchsia.h`, you need to update the usages there as well**. FIDL definitions do not need manual updating because they are extracted automatically when you run `make extract`, but they require Fuchsia to be rebuilt for each architecture (see "Building Fuchsia" above).

Once you updated the syscalls definitions, everything can be regenerated by running:

```
make extract TARGETOS=fuchsia SOURCEDIR=/path/to/fuchsia/checkout
make generate
```

## How to generate syscall description for FIDL

TODO: This section is out of date.

Syscall descriptions for FIDL are automatically generated as part of `make extract` as described above.

However, if you wish to manually generate syscall descriptions for a given `.fidl` file, do the following.

FIDL files should first be compiled into FIDL intermediate representation (JSON) files using `fidlc`:

```bash
$SOURCEDIR/out/x64/host_x64/fidlc --json /tmp/io.json --files $SOURCEDIR/zircon/system/fidl/fuchsia-io/io.fidl
```

Then run FIDL compiler backend `fidlgen` with syzkaller generator, which compiles a FIDL IR file into a syscall description file:

```bash
$SOURCEDIR/out/x64/host_x64/fidlgen -generators syzkaller -json /tmp/io.json -output-base fidl_io -include-base fidl_io
```

## Running syz-ci locally

To run `syz-ci` locally for Fuchsia, you need:

- bootstrapped Fuchsia checkout (in `/bootstrap/fuchsia` dir in the example below)
- bootstrap `syz-ci` binary (in the current dir, build with `make ci`)
- `syz-ci` config similar to the one below (in `ci.cfg` file in the current dir)

```
{
	"name": "testci",
	"http": ":50000",
	"manager_port_start": 50001,
	"syzkaller_repo": "https://github.com/google/syzkaller.git",
	"managers": [
		{
			"name": "fuchsia",
			"repo": "https://fuchsia.googlesource.com",
			"manager_config": {
				"target": "fuchsia/amd64",
				"type": "qemu",
				"cover": false,
				"procs": 8,
				"vm": {
					"count": 4,
					"cpu": 4,
					"mem": 1024
				}
			}
		}
	]
}
```

Run `syz-ci` as:
```
SOURCEDIR=/bootstrap/fuchsia ./syz-ci -config ci.cfg
```

## Troubleshooting

While running the `make extract` step, it's possible that the fidl definitions
are not up to date. It could happen that they have been removed or renamed.

If this is the case, you would see an error mentioning that the fidl.json file
could not be found:

```
go generate ./sys/fuchsia
cannot find /path-to-fuchsia/out/x64/fidling/gen/zircon/public/fidl/zircon-ethernet/zircon-ethernet.fidl.json
exit status 1
```

You can search for the string in the fuchsia repos or in the code-review tool to
see what happened to it. If the fidl interface was renamed or removed, you
should update `sys/fuchsia/fidlgen/main.go` to reflect this change, and remove the
stale autogenerated files.<commit_msg>docs/fuchsia: add caveat to Fuchsia readme<commit_after># Fuchsia support

For information about checking out and building Fuchsia see
[Getting Started](https://fuchsia.dev/fuchsia-src/get-started)
and [Source Code](https://fuchsia.dev/fuchsia-src/get-started/get_fuchsia_source).

## Caveat

Please note that Fuchsia support is currently incomplete, and may break at any
time due to changes in Fuchsia and/or Syzkaller.

Some known issues include:
* System call definitions require manual updates.
* Crash parsing does not work reliably.
* Coverage feedback is not supported.

## Prerequisites

To run syzkaller with a Fuchsia target, you will need:

* A Fuchsia checkout.

The rest of the document will use the following environment variables:

* `SOURCEDIR` path of your Fuchsia checkout.

## Building Fuchsia

To build Fuchsia for x64, run:

```shell
$ fx --dir "out/x64" set core.x64 \
  --with-base "//bundles:tools" \
  --with-base "//src/testing/fuzzing/syzkaller" \
  --args=syzkaller_dir='"/full/path/to/syzkaller"' \
  --variant=kasan
$ fx build
```

Alternatively, for arm64, run:

```shell
$ fx --dir "out/arm64" set core.arm64 \
  --with-base "//bundles:tools" \
  --with-base "//src/testing/fuzzing/syzkaller" \
  --args=syzkaller_dir='"/full/path/to/syzkaller"' \
  --variant=kasan
$ fx build
```

## Building binaries for Fuchsia

To build all the binaries required for running syzkaller in Fuchsia, run:

```shell
make TARGETOS=fuchsia TARGETARCH=amd64 \
    SOURCEDIR=path/to/fuchsia/checkout
```

## Running syz-manager

Running syz-manager requires you to have built Fuchsia previously, and added the ssh keys to the fuchsia.zbi image:

```shell
${SOURCEDIR}/out/x64/host_x64/zbi -o ${SOURCEDIR}/out/x64/fuchsia-ssh.zbi ${SOURCEDIR}/out/x64/fuchsia.zbi --entry "data/ssh/authorized_keys=${SOURCEDIR}/.ssh/authorized_keys"
```

You will also need to extend the `fvm` image:

```shell
cp "${SOURCEDIR}/out/x64/obj/build/images/fuchsia/fuchsia/fvm.blk" "${SOURCEDIR}/out/x64/obj/build/images/fuchsia/fuchsia/fvm-extended.blk"
${SOURCEDIR}/out/x64/host_x64/fvm "${SOURCEDIR}/out/x64/obj/build/images/fuchsia/fuchsia/fvm-extended.blk" extend --length 3G
```

Note: This needs to be repeated after each `fx build`.

Set up a config file, using the following as a starting point:
```json
{
        "name": "fuchsia",
        "target": "fuchsia/amd64",
        "http": ":12345",
        "workdir": "/workdir.fuchsia",
        "kernel_obj": "/fuchsia/out/x64.zircon/kernel-x64-gcc",
        "syzkaller": "/syzkaller",
        "image": "/fuchsia/out/x64/obj/build/images/fuchsia/fuchsia/fvm-extended.blk",
        "sshkey": "/fuchsia/.ssh/pkey",
        "reproduce": false,
        "cover": false,
        "procs": 8,
        "type": "qemu",
        "vm": {
                "count": 10,
                "cpu": 4,
                "mem": 2048,
                "kernel": "/fuchsia/out/x64/multiboot.bin",
                "initrd": "/fuchsia/out/x64/fuchsia-ssh.zbi"
        }
}
```

Run `syz-manager` with that config:
```shell
bin/syz-manager -config manager.cfg
```

Note: You may need to modify your `PATH` so that qemu can be found, e.g.
`PATH="$SOURCEDIR/prebuilt/third_party/qemu/linux-x64/bin:$PATH"`

## Update syscall and FIDL definitions

Syscall descriptions live in the `sys/fuchsia` folder. To update a syscall, you need to modify the `.txt` file that contains it, make sure your new definition matches the one in zircon's [syscalls.abigen](https://fuchsia.googlesource.com/fuchsia/+/master/zircon/system/public/zircon/syscalls.abigen) file. **If the syscall was used in `executor/common_fuchsia.h`, you need to update the usages there as well**. FIDL definitions do not need manual updating because they are extracted automatically when you run `make extract`, but they require Fuchsia to be rebuilt for each architecture (see "Building Fuchsia" above).

Once you updated the syscalls definitions, everything can be regenerated by running:

```
make extract TARGETOS=fuchsia SOURCEDIR=/path/to/fuchsia/checkout
make generate
```

## How to generate syscall description for FIDL

TODO: This section is out of date.

Syscall descriptions for FIDL are automatically generated as part of `make extract` as described above.

However, if you wish to manually generate syscall descriptions for a given `.fidl` file, do the following.

FIDL files should first be compiled into FIDL intermediate representation (JSON) files using `fidlc`:

```bash
$SOURCEDIR/out/x64/host_x64/fidlc --json /tmp/io.json --files $SOURCEDIR/zircon/system/fidl/fuchsia-io/io.fidl
```

Then run FIDL compiler backend `fidlgen` with syzkaller generator, which compiles a FIDL IR file into a syscall description file:

```bash
$SOURCEDIR/out/x64/host_x64/fidlgen -generators syzkaller -json /tmp/io.json -output-base fidl_io -include-base fidl_io
```

## Running syz-ci locally

To run `syz-ci` locally for Fuchsia, you need:

- bootstrapped Fuchsia checkout (in `/bootstrap/fuchsia` dir in the example below)
- bootstrap `syz-ci` binary (in the current dir, build with `make ci`)
- `syz-ci` config similar to the one below (in `ci.cfg` file in the current dir)

```
{
	"name": "testci",
	"http": ":50000",
	"manager_port_start": 50001,
	"syzkaller_repo": "https://github.com/google/syzkaller.git",
	"managers": [
		{
			"name": "fuchsia",
			"repo": "https://fuchsia.googlesource.com",
			"manager_config": {
				"target": "fuchsia/amd64",
				"type": "qemu",
				"cover": false,
				"procs": 8,
				"vm": {
					"count": 4,
					"cpu": 4,
					"mem": 1024
				}
			}
		}
	]
}
```

Run `syz-ci` as:
```
SOURCEDIR=/bootstrap/fuchsia ./syz-ci -config ci.cfg
```

## Troubleshooting

While running the `make extract` step, it's possible that the fidl definitions
are not up to date. It could happen that they have been removed or renamed.

If this is the case, you would see an error mentioning that the fidl.json file
could not be found:

```
go generate ./sys/fuchsia
cannot find /path-to-fuchsia/out/x64/fidling/gen/zircon/public/fidl/zircon-ethernet/zircon-ethernet.fidl.json
exit status 1
```

You can search for the string in the Fuchsia repos or in the code-review tool to
see what happened to it. If the fidl interface was renamed or removed, you
should update `sys/fuchsia/fidlgen/main.go` to reflect this change, and remove the
stale autogenerated files.
{'fraction_non_alphanumeric': 0.13070674248578393, 'fraction_numerical': 0.01429731925264013, 'mean_word_length': 3.6404070863173765, 'pattern_counts': {'":': 72, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 10, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2605031', 'n_tokens_mistral': 4801, 'n_tokens_neox': 4325, 'n_words': 1259}