Add simple hashlink fuzzers.

This could be extended in the future by adding more methods.

Bug: 172599195
Test: Run fuzzers.
Change-Id: I4e594489553d79d90fa670215e33a1d8f6d2b231
diff --git a/fuzzing/orphans/hashlink/Android.bp b/fuzzing/orphans/hashlink/Android.bp
new file mode 100644
index 0000000..5fa6927
--- /dev/null
+++ b/fuzzing/orphans/hashlink/Android.bp
@@ -0,0 +1,52 @@
+// Copyright 2021, The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+rust_fuzz {
+    name: "hashlink_linkedhashmap_fuzzer",
+    srcs: ["hashlink_linkedhashmap_fuzzer.rs"],
+    rustlibs: [
+        "libarbitrary",
+        "libhashlink",
+    ],
+    fuzz_config: {
+        fuzz_on_haiku_device: true,
+        fuzz_on_haiku_host: true,
+    },
+}
+
+rust_fuzz {
+    name: "hashlink_linkedhashset_fuzzer",
+    srcs: ["hashlink_linkedhashset_fuzzer.rs"],
+    rustlibs: [
+        "libarbitrary",
+        "libhashlink",
+    ],
+    fuzz_config: {
+        fuzz_on_haiku_device: true,
+        fuzz_on_haiku_host: true,
+    },
+}
+
+rust_fuzz {
+    name: "hashlink_lrucache_fuzzer",
+    srcs: ["hashlink_lrucache_fuzzer.rs"],
+    rustlibs: [
+        "libarbitrary",
+        "libhashlink",
+    ],
+    fuzz_config: {
+        fuzz_on_haiku_device: true,
+        fuzz_on_haiku_host: true,
+    },
+}
\ No newline at end of file
diff --git a/fuzzing/orphans/hashlink/hashlink_linkedhashmap_fuzzer.rs b/fuzzing/orphans/hashlink/hashlink_linkedhashmap_fuzzer.rs
new file mode 100644
index 0000000..65ea227
--- /dev/null
+++ b/fuzzing/orphans/hashlink/hashlink_linkedhashmap_fuzzer.rs
@@ -0,0 +1,83 @@
+// Copyright 2021, The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#![allow(missing_docs)]
+#![no_main]
+#![feature(bench_black_box)]
+
+use hashlink::LinkedHashMap;
+use libfuzzer_sys::arbitrary::Arbitrary;
+use libfuzzer_sys::fuzz_target;
+
+const MAX_RESERVE: usize = 1024;
+
+#[derive(Arbitrary, Debug, Eq, Hash, PartialEq)]
+enum Data {
+    A,
+    B,
+    Int { val: u8 },
+}
+
+#[derive(Arbitrary, Debug)]
+enum LinkedHashMapMethods {
+    Insert { key: Data, value: Data },
+    Remove { key: Data },
+    ContainsKey { key: Data },
+    Get { key: Data },
+    EntryOrInsert { key: Data, value: Data },
+    Iter,
+    Drain,
+    Clear,
+    Reserve { additional: usize },
+    ShrinkToFit,
+}
+
+fuzz_target!(|commands: Vec<LinkedHashMapMethods>| {
+    let mut map = LinkedHashMap::new();
+    for command in commands {
+        match command {
+            LinkedHashMapMethods::Insert { key, value } => {
+                map.insert(key, value);
+            }
+            LinkedHashMapMethods::Remove { key } => {
+                map.remove(&key);
+            }
+            LinkedHashMapMethods::ContainsKey { key } => {
+                map.contains_key(&key);
+            }
+            LinkedHashMapMethods::Get { key } => {
+                map.get(&key);
+            }
+            LinkedHashMapMethods::EntryOrInsert { key, value } => {
+                map.entry(key).or_insert(value);
+            }
+            LinkedHashMapMethods::Iter => {
+                std::hint::black_box(map.iter().count());
+            }
+            LinkedHashMapMethods::Drain => {
+                std::hint::black_box(map.drain().count());
+            }
+            LinkedHashMapMethods::Clear => {
+                map.clear();
+            }
+            LinkedHashMapMethods::Reserve { additional } => {
+                // Avoid allocating too much memory and crashing the fuzzer.
+                map.reserve(additional % MAX_RESERVE);
+            }
+            LinkedHashMapMethods::ShrinkToFit => {
+                map.shrink_to_fit();
+            }
+        }
+    }
+});
diff --git a/fuzzing/orphans/hashlink/hashlink_linkedhashset_fuzzer.rs b/fuzzing/orphans/hashlink/hashlink_linkedhashset_fuzzer.rs
new file mode 100644
index 0000000..0135a9d
--- /dev/null
+++ b/fuzzing/orphans/hashlink/hashlink_linkedhashset_fuzzer.rs
@@ -0,0 +1,83 @@
+// Copyright 2021, The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#![allow(missing_docs)]
+#![no_main]
+#![feature(bench_black_box)]
+
+use hashlink::LinkedHashSet;
+use libfuzzer_sys::arbitrary::Arbitrary;
+use libfuzzer_sys::fuzz_target;
+
+const MAX_RESERVE: usize = 1024;
+
+#[derive(Arbitrary, Debug, Eq, Hash, PartialEq)]
+enum Data {
+    A,
+    B,
+    Int { val: u8 },
+}
+
+#[derive(Arbitrary, Debug)]
+enum LinkedHashSetMethods {
+    Insert { value: Data },
+    Remove { value: Data },
+    Contains { value: Data },
+    Get { value: Data },
+    GetOrInsert { value: Data },
+    Iter,
+    Drain,
+    Clear,
+    Reserve { additional: usize },
+    ShrinkToFit,
+}
+
+fuzz_target!(|commands: Vec<LinkedHashSetMethods>| {
+    let mut set = LinkedHashSet::new();
+    for command in commands {
+        match command {
+            LinkedHashSetMethods::Insert { value } => {
+                set.insert(value);
+            }
+            LinkedHashSetMethods::Remove { value } => {
+                set.remove(&value);
+            }
+            LinkedHashSetMethods::Contains { value } => {
+                set.contains(&value);
+            }
+            LinkedHashSetMethods::Get { value } => {
+                set.get(&value);
+            }
+            LinkedHashSetMethods::GetOrInsert { value } => {
+                set.get_or_insert(value);
+            }
+            LinkedHashSetMethods::Iter => {
+                std::hint::black_box(set.iter().count());
+            }
+            LinkedHashSetMethods::Drain => {
+                std::hint::black_box(set.drain().count());
+            }
+            LinkedHashSetMethods::Clear => {
+                set.clear();
+            }
+            LinkedHashSetMethods::Reserve { additional } => {
+                // Avoid allocating too much memory and crashing the fuzzer.
+                set.reserve(additional % MAX_RESERVE);
+            }
+            LinkedHashSetMethods::ShrinkToFit => {
+                set.shrink_to_fit();
+            }
+        }
+    }
+});
diff --git a/fuzzing/orphans/hashlink/hashlink_lrucache_fuzzer.rs b/fuzzing/orphans/hashlink/hashlink_lrucache_fuzzer.rs
new file mode 100644
index 0000000..a5cc957
--- /dev/null
+++ b/fuzzing/orphans/hashlink/hashlink_lrucache_fuzzer.rs
@@ -0,0 +1,86 @@
+// Copyright 2021, The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#![allow(missing_docs)]
+#![no_main]
+#![feature(bench_black_box)]
+
+use hashlink::LruCache;
+use libfuzzer_sys::arbitrary::Arbitrary;
+use libfuzzer_sys::fuzz_target;
+
+#[derive(Arbitrary, Debug, Eq, Hash, PartialEq)]
+enum Data {
+    A,
+    B,
+    Int { val: u8 },
+}
+
+#[derive(Arbitrary, Debug)]
+struct LruCacheFuzzInfo {
+    capacity: u8,
+    commands: Vec<LruCacheMethods>,
+}
+
+#[derive(Arbitrary, Debug)]
+enum LruCacheMethods {
+    Insert { key: Data, value: Data },
+    Remove { key: Data },
+    ContainsKey { key: Data },
+    Get { key: Data },
+    EntryOrInsert { key: Data, value: Data },
+    Iter,
+    Drain,
+    Clear,
+    Peek { key: Data },
+    RemoveLru,
+}
+
+fuzz_target!(|info: LruCacheFuzzInfo| {
+    let mut cache = LruCache::new(info.capacity.into());
+    for command in info.commands {
+        match command {
+            LruCacheMethods::Insert { key, value } => {
+                cache.insert(key, value);
+            }
+            LruCacheMethods::Remove { key } => {
+                cache.remove(&key);
+            }
+            LruCacheMethods::ContainsKey { key } => {
+                cache.contains_key(&key);
+            }
+            LruCacheMethods::Get { key } => {
+                cache.get(&key);
+            }
+            LruCacheMethods::EntryOrInsert { key, value } => {
+                cache.entry(key).or_insert(value);
+            }
+            LruCacheMethods::Iter => {
+                std::hint::black_box(cache.iter().count());
+            }
+            LruCacheMethods::Drain => {
+                std::hint::black_box(cache.drain().count());
+            }
+            LruCacheMethods::Clear => {
+                cache.clear();
+            }
+            LruCacheMethods::Peek { key } => {
+                cache.peek(&key);
+            }
+            LruCacheMethods::RemoveLru => {
+                cache.remove_lru();
+            }
+        }
+    }
+});