Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/ifuzz/arm64/arm64.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright 2024 syzkaller project authors. All rights reserved.
// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.

//go:generate bash -c "go run gen/gen.go gen/json/arm64.json | gofmt > generated/insns.go"
//go:generate go run gen/gen.go gen/json/arm64.json generated/insns.go

// Package arm64 allows to generate and mutate arm64 machine code.
package arm64
Expand Down
14 changes: 10 additions & 4 deletions pkg/ifuzz/arm64/gen/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,31 @@
package main

import (
"bytes"
"encoding/json"
"fmt"
"os"
"strconv"
"strings"

"github.com/google/syzkaller/pkg/ifuzz/arm64"
"github.com/google/syzkaller/pkg/osutil"
"github.com/google/syzkaller/pkg/serializer"
"github.com/google/syzkaller/pkg/tool"
)

func main() {
if len(os.Args) != 2 {
tool.Failf("usage: gen arm64.json")
if len(os.Args) != 3 {
tool.Failf("usage: gen arm64.json output.file")
}
jsonStr, err := os.ReadFile(os.Args[1])
if err != nil {
tool.Failf("failed to open input file: %v", err)
}
insns := JSONToInsns(jsonStr)

fmt.Printf(`// Code generated by pkg/ifuzz/gen. DO NOT EDIT.
out := new(bytes.Buffer)
fmt.Fprintf(out, `// Code generated by pkg/ifuzz/gen. DO NOT EDIT.

// go:build !codeanalysis

Expand All @@ -42,7 +45,10 @@ func init() {

var insns_arm64 =
`)
serializer.Write(os.Stdout, insns)
serializer.Write(out, insns)
if err := osutil.WriteFileAtomically(os.Args[2], out.Bytes()); err != nil {
tool.Fail(err)
}

fmt.Fprintf(os.Stderr, "handled %v\n", len(insns))
}
Expand Down
14 changes: 10 additions & 4 deletions pkg/ifuzz/x86/gen/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package main

import (
"bufio"
"bytes"
"errors"
"fmt"
"os"
Expand All @@ -16,14 +17,15 @@ import (

"github.com/google/syzkaller/pkg/ifuzz/iset"
"github.com/google/syzkaller/pkg/ifuzz/x86"
"github.com/google/syzkaller/pkg/osutil"
"github.com/google/syzkaller/pkg/serializer"
"github.com/google/syzkaller/pkg/tool"
)

// nolint: gocyclo, gocognit, funlen, dupl
func main() {
if len(os.Args) != 2 {
tool.Failf("usage: gen instructions.txt")
if len(os.Args) != 3 {
tool.Failf("usage: gen instructions.txt output.file")
}
f, err := os.Open(os.Args[1])
if err != nil {
Expand Down Expand Up @@ -168,7 +170,8 @@ nextInsn:
fmt.Fprintf(os.Stderr, "deduped %v instructions\n", len(insns)-len(deduped))
insns = deduped

fmt.Printf(`
out := new(bytes.Buffer)
fmt.Fprintf(out, `
// Code generated by pkg/ifuzz/x86/gen. DO NOT EDIT.

//go:build !codeanalysis
Expand All @@ -183,7 +186,10 @@ func init() {

var insns =
`)
serializer.Write(os.Stdout, insns)
serializer.Write(out, insns)
if err := osutil.WriteFileAtomically(os.Args[2], out.Bytes()); err != nil {
tool.Fail(err)
}

fmt.Fprintf(os.Stderr, "handled %v, skipped %v\n", len(insns), skipped)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/ifuzz/x86/x86.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright 2017 syzkaller project authors. All rights reserved.
// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.

//go:generate bash -c "go run gen/gen.go gen/all-enc-instructions.txt > generated/insns.go"
//go:generate go run gen/gen.go gen/all-enc-instructions.txt generated/insns.go

// Package x86 allows to generate and mutate x86 machine code.
package x86
Expand Down
13 changes: 13 additions & 0 deletions pkg/osutil/osutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,19 @@ func WriteFile(filename string, data []byte) error {
return os.WriteFile(filename, data, DefaultFilePerm)
}

// WriteFileAtomically writes data to file filename without exposing and empty/partially-written file.
// This is useful for writing generated source files. Exposing an empty file may break tools
// that run on source files in parallel.
func WriteFileAtomically(filename string, data []byte) error {
// We can't use os.CreateTemp b/c it may be on a different mount,
// and Rename can't move across mounts.
tmpFile := filename + ".tmp"
if err := WriteFile(tmpFile, data); err != nil {
return err
}
return os.Rename(tmpFile, filename)
}

func WriteJSON[T any](filename string, obj T) error {
jsonData, err := json.MarshalIndent(obj, "", "\t")
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion sys/fuchsia/fidlgen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func main() {
return pos.File == file
}))

if err := osutil.WriteFile(file, desc); err != nil {
if err := osutil.WriteFileAtomically(file, desc); err != nil {
tool.Fail(err)
}
}
Expand Down