|
| 1 | +// Copyright 2025 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | +// |
| 15 | + |
| 16 | +package main |
| 17 | + |
| 18 | +import ( |
| 19 | + "bufio" |
| 20 | + "fmt" |
| 21 | + "log" |
| 22 | + "os" |
| 23 | + "strings" |
| 24 | +) |
| 25 | + |
| 26 | +var ( |
| 27 | + template1 = ` |
| 28 | + { |
| 29 | + 'target_name': '____TARGETNAME____', |
| 30 | + 'type': 'executable', |
| 31 | + 'dependencies': [ |
| 32 | + '<(node_lib_target_name)', |
| 33 | + 'deps/googletest/googletest.gyp:gtest_prod', |
| 34 | + 'deps/histogram/histogram.gyp:histogram', |
| 35 | + 'deps/uvwasi/uvwasi.gyp:uvwasi', |
| 36 | + 'deps/ncrypto/ncrypto.gyp:ncrypto', |
| 37 | + 'deps/nbytes/nbytes.gyp:nbytes', |
| 38 | + 'tools/v8_gypfiles/abseil.gyp:abseil', |
| 39 | + ], |
| 40 | + 'includes': [ |
| 41 | + 'node.gypi' |
| 42 | + ], |
| 43 | + 'include_dirs': [ |
| 44 | + 'src', |
| 45 | + 'tools/msvs/genfiles', |
| 46 | + 'deps/v8/include', |
| 47 | + 'deps/cares/include', |
| 48 | + 'deps/uv/include', |
| 49 | + 'deps/uvwasi/include', |
| 50 | + 'test/cctest', |
| 51 | + 'test/fuzzers', |
| 52 | + ], |
| 53 | + 'defines': [ |
| 54 | + 'NODE_ARCH="<(target_arch)"', |
| 55 | + 'NODE_PLATFORM="<(OS)"', |
| 56 | + 'NODE_WANT_INTERNALS=1', |
| 57 | + 'HAVE_OPENSSL=1', |
| 58 | + 'NAPI_VERSION=10', |
| 59 | + ], |
| 60 | + 'sources': [ |
| 61 | + 'src/node_snapshot_stub.cc', |
| 62 | + 'test/fuzzers/fuzz_common.cc', |
| 63 | + 'test/fuzzers/____TARGETNAME____.cc', |
| 64 | + ], |
| 65 | + 'conditions': [ |
| 66 | + ['OS=="linux"', { |
| 67 | + 'ldflags': [ '-fsanitize=fuzzer' ] |
| 68 | + }], |
| 69 | + # Ensure that ossfuzz flag has been set and that we are on Linux |
| 70 | + [ 'OS!="linux" or ossfuzz!="true"', { |
| 71 | + 'type': 'none', |
| 72 | + }], |
| 73 | + # Avoid excessive LTO |
| 74 | + ['enable_lto=="true"', { |
| 75 | + 'ldflags': [ '-fno-lto' ], |
| 76 | + }], |
| 77 | + ], |
| 78 | + },` |
| 79 | + fuzzers = []string{ |
| 80 | + "fuzz_ClientHelloParser", |
| 81 | + "fuzz_blob", |
| 82 | + "fuzz_buffer_compare", |
| 83 | + "fuzz_buffer_equals", |
| 84 | + "fuzz_buffer_includes", |
| 85 | + "fuzz_cipheriv", |
| 86 | + "fuzz_createPrivateKeyDER", |
| 87 | + "fuzz_createPrivateKeyJWK", |
| 88 | + "fuzz_createPrivateKeyPEM", |
| 89 | + "fuzz_diffieHellmanDER", |
| 90 | + "fuzz_diffieHellmanJWK", |
| 91 | + "fuzz_diffieHellmanPEM", |
| 92 | + "fuzz_fs_write_open_read", |
| 93 | + "fuzz_fs_write_read_append", |
| 94 | + "fuzz_httpparser1", |
| 95 | + "fuzz_path_basename", |
| 96 | + "fuzz_path_dirname", |
| 97 | + "fuzz_path_extname", |
| 98 | + "fuzz_path_format", |
| 99 | + "fuzz_path_isAbsolute", |
| 100 | + "fuzz_path_join", |
| 101 | + "fuzz_path_normalize", |
| 102 | + "fuzz_path_parse", |
| 103 | + "fuzz_path_relative", |
| 104 | + "fuzz_path_resolve", |
| 105 | + "fuzz_path_toNamespacedPath", |
| 106 | + "fuzz_querystring_parse", |
| 107 | + "fuzz_quic_token", |
| 108 | + "fuzz_sign_verify", |
| 109 | + "fuzz_stream1", |
| 110 | + "fuzz_string_decoder", |
| 111 | + "fuzz_strings", |
| 112 | + "fuzz_tls_socket_request", |
| 113 | + "fuzz_v8_deserialize", |
| 114 | + "fuzz_x509", |
| 115 | + "fuzz_zlib_brotliCompress", |
| 116 | + "fuzz_zlib_brotliDecompress", |
| 117 | + "fuzz_zlib_createBrotliDecompress", |
| 118 | + "fuzz_zlib_gzip_createUnzip", |
| 119 | + } |
| 120 | +) |
| 121 | + |
| 122 | +func createGypTargetEntry(fuzzerName string) string { |
| 123 | + templ := template1 |
| 124 | + templWithTargetName := strings.ReplaceAll(templ, "____TARGETNAME____", fuzzerName) |
| 125 | + return templWithTargetName |
| 126 | +} |
| 127 | + |
| 128 | +func main() { |
| 129 | + if len(os.Args) < 3 { |
| 130 | + fmt.Println("Usage: go run main.go <inputfile> <outputfile>") |
| 131 | + return |
| 132 | + } |
| 133 | + |
| 134 | + inputFile := os.Args[1] |
| 135 | + outputFile := os.Args[2] |
| 136 | + |
| 137 | + in, err := os.Open(inputFile) |
| 138 | + if err != nil { |
| 139 | + log.Fatalf("Failed to open input file: %v", err) |
| 140 | + } |
| 141 | + defer in.Close() |
| 142 | + |
| 143 | + out, err := os.Create(outputFile) |
| 144 | + if err != nil { |
| 145 | + log.Fatalf("Failed to create output file: %v", err) |
| 146 | + } |
| 147 | + defer out.Close() |
| 148 | + |
| 149 | + scanner := bufio.NewScanner(in) |
| 150 | + writer := bufio.NewWriter(out) |
| 151 | + |
| 152 | + var ignore bool |
| 153 | + |
| 154 | + for scanner.Scan() { |
| 155 | + line := scanner.Text() |
| 156 | + |
| 157 | + // Remove existing fuzzers in the original node.gyp |
| 158 | + if !ignore && line == " { # fuzz_env" { |
| 159 | + ignore = true |
| 160 | + continue |
| 161 | + } else if ignore && line == " }, # fuzz_env" { |
| 162 | + ignore = false |
| 163 | + continue |
| 164 | + } else if !ignore && line == " { # fuzz_ClientHelloParser.cc" { |
| 165 | + ignore = true |
| 166 | + continue |
| 167 | + } else if ignore && line == " }, # fuzz_ClientHelloParser.cc" { |
| 168 | + ignore = false |
| 169 | + continue |
| 170 | + } else if !ignore && line == " { # fuzz_url" { |
| 171 | + ignore = true |
| 172 | + continue |
| 173 | + } else if ignore && line == " }, # fuzz_url" { |
| 174 | + ignore = false |
| 175 | + continue |
| 176 | + } else if !ignore && line == " { # fuzz_strings" { |
| 177 | + ignore = true |
| 178 | + continue |
| 179 | + } else if ignore && line == " }, # fuzz_strings" { |
| 180 | + ignore = false |
| 181 | + continue |
| 182 | + } |
| 183 | + if ignore { |
| 184 | + continue |
| 185 | + } |
| 186 | + |
| 187 | + var stringToAppend string |
| 188 | + appendTargetsNow := line == " 'targets': [" |
| 189 | + // add new line unless we are adding the fuzzers |
| 190 | + // since the fuzzer template already has new line |
| 191 | + if appendTargetsNow { |
| 192 | + stringToAppend = line |
| 193 | + } else { |
| 194 | + stringToAppend = line + "\n" |
| 195 | + } |
| 196 | + _, err := writer.WriteString(stringToAppend) |
| 197 | + if err != nil { |
| 198 | + log.Fatalf("Failed to write line: %v", err) |
| 199 | + } |
| 200 | + |
| 201 | + // Check for the special line |
| 202 | + if appendTargetsNow { |
| 203 | + for _, fuzzer := range fuzzers { |
| 204 | + fmt.Println("appending ", fuzzer) |
| 205 | + _, err := writer.WriteString(createGypTargetEntry(fuzzer)) |
| 206 | + if err != nil { |
| 207 | + log.Fatalf("Failed to write injected line: %v", err) |
| 208 | + } |
| 209 | + } |
| 210 | + } |
| 211 | + } |
| 212 | + |
| 213 | + if err := scanner.Err(); err != nil { |
| 214 | + log.Fatalf("Error reading input file: %v", err) |
| 215 | + } |
| 216 | + |
| 217 | + if err := writer.Flush(); err != nil { |
| 218 | + log.Fatalf("Error flushing output: %v", err) |
| 219 | + } |
| 220 | +} |
0 commit comments