1//===-- cas-fuzzer.cpp - Fuzzer for CAS ObjectStore::validate() -----------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// Fuzzer for on-disk CAS validation. Creates a valid CAS database, stores
10// objects, corrupts the on-disk files using fuzzer-provided bytes, then calls
11// validate(). The invariant: validate() must either succeed or return an error,
12// never crash.
13//
14//===----------------------------------------------------------------------===//
15
16#include "llvm/ADT/ScopeExit.h"
17#include "llvm/CAS/ActionCache.h"
18#include "llvm/CAS/BuiltinUnifiedCASDatabases.h"
19#include "llvm/CAS/ObjectStore.h"
20#include "llvm/Support/Error.h"
21#include "llvm/Support/FileSystem.h"
22#include "llvm/Support/MemoryBuffer.h"
23#include "llvm/Support/Path.h"
24#include "llvm/Support/raw_ostream.h"
25#include <cstdint>
26#include <cstring>
27
28using namespace llvm;
29using namespace llvm::cas;
30
31namespace {
32
33/// Read a little-endian uint32 from Data, or 0 if not enough bytes.
34static uint32_t readU32(ArrayRef<uint8_t> Data, size_t Offset) {
35 if (Offset + sizeof(uint32_t) > Data.size())
36 return 0;
37 return support::endian::read32le(P: Data.data() + Offset);
38}
39
40/// Read a little-endian uint16 from Data, or 0 if not enough bytes.
41static uint16_t readU16(ArrayRef<uint8_t> Data, size_t Offset) {
42 if (Offset + sizeof(uint16_t) > Data.size())
43 return 0;
44 return support::endian::read16le(P: Data.data() + Offset);
45}
46
47/// Find the versioned subdirectory (v1.N) inside the CAS root.
48static std::string findVersionedSubdir(StringRef CASDir) {
49 std::error_code EC;
50 std::string Best;
51 uint64_t BestOrder = 0;
52 for (sys::fs::directory_iterator DirI(CASDir, EC), DirE; !EC && DirI != DirE;
53 DirI.increment(ec&: EC)) {
54 if (DirI->type() != sys::fs::file_type::directory_file)
55 continue;
56 StringRef Name = sys::path::filename(path: DirI->path());
57 if (!Name.starts_with(Prefix: "v1."))
58 continue;
59 uint64_t Order;
60 if (Name.substr(Start: 3).getAsInteger(Radix: 10, Result&: Order))
61 continue;
62 if (Best.empty() || Order > BestOrder) {
63 Best = DirI->path();
64 BestOrder = Order;
65 }
66 }
67 return Best;
68}
69
70/// Collect paths of files matching a prefix in a directory.
71static void collectFilesWithPrefix(StringRef Dir, StringRef Prefix,
72 SmallVectorImpl<std::string> &Results) {
73 std::error_code EC;
74 for (sys::fs::directory_iterator DirI(Dir, EC), DirE; !EC && DirI != DirE;
75 DirI.increment(ec&: EC)) {
76 StringRef Name = sys::path::filename(path: DirI->path());
77 if (Name.starts_with(Prefix))
78 Results.push_back(Elt: DirI->path());
79 }
80}
81
82/// Read an entire file into a buffer.
83static bool readFileBytes(StringRef Path, SmallVectorImpl<char> &Buf) {
84 auto MBOrErr = MemoryBuffer::getFile(Filename: Path, /*IsText=*/false,
85 /*RequiresNullTerminator=*/false);
86 if (!MBOrErr)
87 return false;
88 Buf.assign(in_start: (*MBOrErr)->getBufferStart(), in_end: (*MBOrErr)->getBufferEnd());
89 return true;
90}
91
92/// Write buffer contents to a file, replacing it entirely.
93static bool writeFileBytes(StringRef Path, ArrayRef<char> Buf) {
94 std::error_code EC;
95 raw_fd_ostream OS(Path, EC, sys::fs::OF_None);
96 if (EC)
97 return false;
98 OS.write(Ptr: Buf.data(), Size: Buf.size());
99 return !OS.has_error();
100}
101
102/// Create a CAS database and store some baseline objects.
103/// Returns true on success, populating CAS and AC via output parameters.
104static bool createAndPopulateCAS(StringRef TmpDir,
105 std::unique_ptr<ObjectStore> &CAS,
106 std::unique_ptr<ActionCache> &AC) {
107 auto Result = createOnDiskUnifiedCASDatabases(Path: TmpDir);
108 if (!Result) {
109 consumeError(Err: Result.takeError());
110 return false;
111 }
112 CAS = std::move(Result->first);
113 AC = std::move(Result->second);
114
115 // Store a leaf node (no refs, small data).
116 const char LeafData[] = "hello-cas-fuzzer-leaf-data";
117 auto Leaf = CAS->store(Refs: {}, Data: arrayRefFromStringRef<char>(Input: LeafData));
118 if (!Leaf) {
119 consumeError(Err: Leaf.takeError());
120 return false;
121 }
122
123 // Store a node with a ref to the leaf.
124 const char NodeData[] = "node-with-one-ref";
125 auto Node1 = CAS->store(Refs: {*Leaf}, Data: arrayRefFromStringRef<char>(Input: NodeData));
126 if (!Node1) {
127 consumeError(Err: Node1.takeError());
128 return false;
129 }
130
131 // Store a node referencing both previous nodes.
132 const char Node2Data[] = "node-with-two-refs";
133 auto Node2 =
134 CAS->store(Refs: {*Leaf, *Node1}, Data: arrayRefFromStringRef<char>(Input: Node2Data));
135 if (!Node2) {
136 consumeError(Err: Node2.takeError());
137 return false;
138 }
139
140 // Store a larger data node to potentially exercise different size encodings.
141 std::string LargeData(4096, 'X');
142 auto LargeNode =
143 CAS->store(Refs: {}, Data: arrayRefFromStringRef<char>(Input: StringRef(LargeData)));
144 if (!LargeNode) {
145 consumeError(Err: LargeNode.takeError());
146 return false;
147 }
148
149 return true;
150}
151
152/// Apply byte-level mutations to a file.
153static void applyByteMutations(StringRef Path, ArrayRef<uint8_t> Data) {
154 SmallVector<char> Buf;
155 if (!readFileBytes(Path, Buf) || Buf.empty())
156 return;
157
158 // Parse as 7-byte chunks: [offset(4)][op(1)][value(1)][unused(1)]
159 for (size_t I = 0; I + 6 <= Data.size(); I += 7) {
160 uint32_t Offset = readU32(Data, Offset: I) % Buf.size();
161 uint8_t Op = Data[I + 4] % 3;
162 uint8_t Value = Data[I + 5];
163 switch (Op) {
164 case 0: // XOR
165 Buf[Offset] ^= Value;
166 break;
167 case 1: // SET
168 Buf[Offset] = Value;
169 break;
170 case 2: // Zero
171 Buf[Offset] = 0;
172 break;
173 }
174 }
175 writeFileBytes(Path, Buf);
176}
177
178/// Truncate a file to a given fraction of its size.
179static void truncateFile(StringRef Path, uint8_t Fraction) {
180 SmallVector<char> Buf;
181 if (!readFileBytes(Path, Buf) || Buf.empty())
182 return;
183 // Fraction is 0-255, map to 0-100% of file size.
184 size_t NewSize =
185 static_cast<size_t>(static_cast<uint64_t>(Buf.size()) * Fraction / 255);
186 // Don't zero out the size.
187 if (NewSize == 0)
188 NewSize = 1;
189 Buf.resize(N: NewSize);
190 writeFileBytes(Path, Buf);
191}
192
193/// Append garbage bytes to a file.
194static void appendGarbage(StringRef Path, ArrayRef<uint8_t> Data) {
195 SmallVector<char> Buf;
196 if (!readFileBytes(Path, Buf))
197 return;
198 Buf.append(in_start: Data.begin(), in_end: Data.end());
199 writeFileBytes(Path, Buf);
200}
201
202/// Zero out a range in a file.
203static void zeroRange(StringRef Path, uint32_t Offset, uint16_t Length) {
204 SmallVector<char> Buf;
205 if (!readFileBytes(Path, Buf) || Buf.empty())
206 return;
207 size_t Start = Offset % Buf.size();
208 size_t End = std::min(a: Start + static_cast<size_t>(Length), b: Buf.size());
209 std::memset(s: Buf.data() + Start, c: 0, n: End - Start);
210 writeFileBytes(Path, Buf);
211}
212
213/// Corrupt standalone files (obj.*, leaf.*, leaf+0.*).
214static void corruptStandaloneFiles(StringRef SubDir, ArrayRef<uint8_t> Data) {
215 SmallVector<std::string> StandaloneFiles;
216 collectFilesWithPrefix(Dir: SubDir, Prefix: "obj.", Results&: StandaloneFiles);
217 collectFilesWithPrefix(Dir: SubDir, Prefix: "leaf.", Results&: StandaloneFiles);
218 collectFilesWithPrefix(Dir: SubDir, Prefix: "leaf+0.", Results&: StandaloneFiles);
219
220 if (StandaloneFiles.empty())
221 return;
222
223 for (size_t I = 0; I < Data.size() && !StandaloneFiles.empty(); I += 3) {
224 size_t FileIdx = Data[I] % StandaloneFiles.size();
225 uint8_t Action = (I + 1 < Data.size()) ? Data[I + 1] % 4 : 0;
226 uint8_t Param = (I + 2 < Data.size()) ? Data[I + 2] : 128;
227
228 StringRef FilePath = StandaloneFiles[FileIdx];
229 switch (Action) {
230 case 0: // Delete the file
231 sys::fs::remove(path: FilePath);
232 break;
233 case 1: // Truncate
234 truncateFile(Path: FilePath, Fraction: Param);
235 break;
236 case 2: // Corrupt bytes
237 if (I + 3 < Data.size())
238 applyByteMutations(
239 Path: FilePath, Data: Data.slice(N: I + 3, M: std::min(a: Data.size() - I - 3,
240 b: static_cast<size_t>(21))));
241 break;
242 case 3: // Zero out beginning
243 zeroRange(Path: FilePath, Offset: 0, Length: Param);
244 break;
245 }
246 }
247}
248
249/// Select which data file to target (index.v1 or data.v1).
250static std::string selectTargetFile(StringRef SubDir, uint8_t Selector) {
251 SmallString<256> Path(SubDir);
252 if (Selector % 2 == 0)
253 sys::path::append(path&: Path, a: "index.v1");
254 else
255 sys::path::append(path&: Path, a: "data.v1");
256 return std::string(Path);
257}
258
259/// Try to exercise the CAS after corruption: store and load.
260static void exerciseCAS(ObjectStore &CAS) {
261 // Try storing a new object.
262 const char NewData[] = "post-corruption-data";
263 auto NewObj = CAS.store(Refs: {}, Data: arrayRefFromStringRef<char>(Input: NewData));
264 if (!NewObj)
265 consumeError(Err: NewObj.takeError());
266
267 // Try validate again with CheckHash=false.
268 if (auto E = CAS.validate(/*CheckHash=*/false))
269 consumeError(Err: std::move(E));
270}
271
272} // end anonymous namespace
273
274extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
275 if (Size == 0)
276 return 0;
277
278 // Create a unique temp directory for this fuzzer run.
279 SmallString<256> TmpDir;
280 if (sys::fs::createUniqueDirectory(Prefix: "cas-fuzzer", ResultPath&: TmpDir))
281 return 0;
282
283 // Ensure cleanup on exit.
284 auto Cleanup = scope_exit([&]() { sys::fs::remove_directories(path: TmpDir); });
285
286 // Step 1: Create and populate a valid CAS.
287 std::unique_ptr<ObjectStore> CAS;
288 std::unique_ptr<ActionCache> AC;
289 if (!createAndPopulateCAS(TmpDir, CAS, AC))
290 return 0;
291
292 // Step 2: Validate baseline - should succeed.
293 if (auto E = CAS->validate(/*CheckHash=*/true)) {
294 // If baseline validation fails, something is wrong with the setup.
295 consumeError(Err: std::move(E));
296 return 0;
297 }
298
299 // Step 3: Close the CAS so files are unmapped.
300 CAS.reset();
301 AC.reset();
302
303 // Step 4: Find the versioned subdirectory.
304 std::string SubDir = findVersionedSubdir(CASDir: TmpDir);
305 if (SubDir.empty())
306 return 0;
307
308 // Step 5: Apply corruption based on mode selector (first byte).
309 ArrayRef<uint8_t> Input(Data, Size);
310 uint8_t Mode = Input[0] % 6;
311 ArrayRef<uint8_t> Rest = Input.drop_front(N: 1);
312
313 switch (Mode) {
314 case 0: { // Byte-level mutations
315 if (Rest.empty())
316 break;
317 std::string Target = selectTargetFile(SubDir, Selector: Rest[0]);
318 if (Rest.size() > 1)
319 applyByteMutations(Path: Target, Data: Rest.drop_front(N: 1));
320 break;
321 }
322 case 1: { // File truncation
323 if (Rest.size() < 2)
324 break;
325 std::string Target = selectTargetFile(SubDir, Selector: Rest[0]);
326 truncateFile(Path: Target, Fraction: Rest[1]);
327 break;
328 }
329 case 2: { // Append garbage
330 if (Rest.empty())
331 break;
332 std::string Target = selectTargetFile(SubDir, Selector: Rest[0]);
333 if (Rest.size() > 1)
334 appendGarbage(Path: Target, Data: Rest.drop_front(N: 1));
335 break;
336 }
337 case 3: { // Zero out a range
338 if (Rest.size() < 7)
339 break;
340 std::string Target = selectTargetFile(SubDir, Selector: Rest[0]);
341 uint32_t Offset = readU32(Data: Rest, Offset: 1);
342 uint16_t Length = readU16(Data: Rest, Offset: 5);
343 zeroRange(Path: Target, Offset, Length);
344 break;
345 }
346 case 4: { // Standalone file corruption
347 corruptStandaloneFiles(SubDir, Data: Rest);
348 break;
349 }
350 case 5: { // Combined: byte mutations + exercise CAS
351 if (Rest.empty())
352 break;
353 std::string Target = selectTargetFile(SubDir, Selector: Rest[0]);
354 if (Rest.size() > 1)
355 applyByteMutations(Path: Target, Data: Rest.drop_front(N: 1));
356 break;
357 }
358 }
359
360 // Step 6: Reopen the CAS after corruption.
361 auto Reopened = createOnDiskUnifiedCASDatabases(Path: TmpDir);
362 if (!Reopened) {
363 // Reopen failing is acceptable — corruption may have broken the format.
364 consumeError(Err: Reopened.takeError());
365 return 0;
366 }
367 CAS = std::move(Reopened->first);
368 AC = std::move(Reopened->second);
369
370 // Step 7: Validate — must not crash.
371 bool ValidationFailed = false;
372 if (auto E = CAS->validate(/*CheckHash=*/true)) {
373 consumeError(Err: std::move(E));
374 ValidationFailed = true;
375 }
376 if (auto E = CAS->validate(/*CheckHash=*/false)) {
377 consumeError(Err: std::move(E));
378 ValidationFailed = true;
379 }
380
381 // Step 8: For mode 5, exercise the CAS only if validation passed.
382 if (Mode == 5 && !ValidationFailed)
383 exerciseCAS(CAS&: *CAS);
384
385 return 0;
386}
387