|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +// A command line executable that generates a bunch of valid IPC files |
| 19 | +// containing example record batches. Those are used as fuzzing seeds |
| 20 | +// to make fuzzing more efficient. |
| 21 | + |
| 22 | +#include <cstdlib> |
| 23 | +#include <iostream> |
| 24 | +#include <memory> |
| 25 | +#include <string> |
| 26 | +#include <vector> |
| 27 | + |
| 28 | +#include "arrow/io/file.h" |
| 29 | +#include "arrow/io/memory.h" |
| 30 | +#include "arrow/ipc/test_common.h" |
| 31 | +#include "arrow/ipc/writer.h" |
| 32 | +#include "arrow/record_batch.h" |
| 33 | +#include "arrow/result.h" |
| 34 | +#include "arrow/util/io_util.h" |
| 35 | + |
| 36 | +namespace arrow { |
| 37 | +namespace ipc { |
| 38 | + |
| 39 | +using ::arrow::internal::CreateDir; |
| 40 | +using ::arrow::internal::PlatformFilename; |
| 41 | + |
| 42 | +Result<std::vector<std::shared_ptr<RecordBatch>>> Batches() { |
| 43 | + std::vector<std::shared_ptr<RecordBatch>> batches; |
| 44 | + std::shared_ptr<RecordBatch> batch; |
| 45 | + RETURN_NOT_OK(test::MakeNullRecordBatch(&batch)); |
| 46 | + batches.push_back(batch); |
| 47 | + RETURN_NOT_OK(test::MakeListRecordBatch(&batch)); |
| 48 | + batches.push_back(batch); |
| 49 | + RETURN_NOT_OK(test::MakeDictionary(&batch)); |
| 50 | + batches.push_back(batch); |
| 51 | + RETURN_NOT_OK(test::MakeTimestamps(&batch)); |
| 52 | + batches.push_back(batch); |
| 53 | + RETURN_NOT_OK(test::MakeFWBinary(&batch)); |
| 54 | + batches.push_back(batch); |
| 55 | + RETURN_NOT_OK(test::MakeStruct(&batch)); |
| 56 | + batches.push_back(batch); |
| 57 | + RETURN_NOT_OK(test::MakeUnion(&batch)); |
| 58 | + batches.push_back(batch); |
| 59 | + RETURN_NOT_OK(test::MakeFixedSizeListRecordBatch(&batch)); |
| 60 | + batches.push_back(batch); |
| 61 | + return batches; |
| 62 | +} |
| 63 | + |
| 64 | +template <typename RecordBatchWriterClass> |
| 65 | +Result<std::shared_ptr<Buffer>> SerializeRecordBatch( |
| 66 | + const std::shared_ptr<RecordBatch>& batch) { |
| 67 | + ARROW_ASSIGN_OR_RAISE(auto sink, io::BufferOutputStream::Create(1024)); |
| 68 | + ARROW_ASSIGN_OR_RAISE(auto writer, |
| 69 | + RecordBatchWriterClass::Open(sink.get(), batch->schema())); |
| 70 | + RETURN_NOT_OK(writer->WriteRecordBatch(*batch)); |
| 71 | + RETURN_NOT_OK(writer->Close()); |
| 72 | + return sink->Finish(); |
| 73 | +} |
| 74 | + |
| 75 | +Status DoMain(bool is_stream_format, const std::string& out_dir) { |
| 76 | + ARROW_ASSIGN_OR_RAISE(auto dir_fn, PlatformFilename::FromString(out_dir)); |
| 77 | + RETURN_NOT_OK(CreateDir(dir_fn)); |
| 78 | + |
| 79 | + auto serialize_func = is_stream_format ? SerializeRecordBatch<RecordBatchStreamWriter> |
| 80 | + : SerializeRecordBatch<RecordBatchFileWriter>; |
| 81 | + |
| 82 | + ARROW_ASSIGN_OR_RAISE(auto batches, Batches()); |
| 83 | + int batch_num = 1; |
| 84 | + for (const auto& batch : batches) { |
| 85 | + RETURN_NOT_OK(batch->ValidateFull()); |
| 86 | + ARROW_ASSIGN_OR_RAISE(auto buf, serialize_func(batch)); |
| 87 | + auto name = "batch-" + std::to_string(batch_num++); |
| 88 | + |
| 89 | + ARROW_ASSIGN_OR_RAISE(auto batch_fn, dir_fn.Join(name)); |
| 90 | + std::cerr << batch_fn.ToString() << std::endl; |
| 91 | + ARROW_ASSIGN_OR_RAISE(auto file, io::FileOutputStream::Open(batch_fn.ToString())); |
| 92 | + RETURN_NOT_OK(file->Write(buf)); |
| 93 | + RETURN_NOT_OK(file->Close()); |
| 94 | + } |
| 95 | + return Status::OK(); |
| 96 | +} |
| 97 | + |
| 98 | +ARROW_NORETURN void Usage() { |
| 99 | + std::cerr << "Usage: arrow-ipc-generate-fuzz-corpus " |
| 100 | + << "[-stream|-file] <output directory>" << std::endl; |
| 101 | + std::exit(2); |
| 102 | +} |
| 103 | + |
| 104 | +int Main(int argc, char** argv) { |
| 105 | + if (argc != 3) { |
| 106 | + Usage(); |
| 107 | + } |
| 108 | + auto opt = std::string(argv[1]); |
| 109 | + if (opt != "-stream" && opt != "-file") { |
| 110 | + Usage(); |
| 111 | + } |
| 112 | + auto out_dir = std::string(argv[2]); |
| 113 | + |
| 114 | + Status st = DoMain(opt == "-stream", out_dir); |
| 115 | + if (!st.ok()) { |
| 116 | + std::cerr << st.ToString() << std::endl; |
| 117 | + return 1; |
| 118 | + } |
| 119 | + return 0; |
| 120 | +} |
| 121 | + |
| 122 | +} // namespace ipc |
| 123 | +} // namespace arrow |
| 124 | + |
| 125 | +int main(int argc, char** argv) { return arrow::ipc::Main(argc, argv); } |
0 commit comments