diff --git a/projects/hamcrest/Dockerfile b/projects/hamcrest/Dockerfile new file mode 100644 index 000000000..5d4caf894 --- /dev/null +++ b/projects/hamcrest/Dockerfile @@ -0,0 +1,23 @@ +# Copyright 2022 Google LLC +# +# 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. +# +################################################################################ + +FROM gcr.io/oss-fuzz-base/base-builder-jvm + +RUN git clone https://github.com/hamcrest/JavaHamcrest.git hamcrest + +COPY build.sh $SRC/ +COPY *.java $SRC/ +WORKDIR $SRC/hamcrest diff --git a/projects/hamcrest/HamcrestFuzzer.java b/projects/hamcrest/HamcrestFuzzer.java new file mode 100644 index 000000000..a8be620f7 --- /dev/null +++ b/projects/hamcrest/HamcrestFuzzer.java @@ -0,0 +1,295 @@ + +// Copyright 2021 Google LLC +// +// 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. +// +//////////////////////////////////////////////////////////////////////////////// +import com.code_intelligence.jazzer.api.FuzzedDataProvider; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.*; +import java.util.*; +import java.util.regex.*; +import javax.xml.xpath.*; +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerException; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.dom.DOMSource; +import org.w3c.dom.Attr; +import org.w3c.dom.DOMException; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import java.util.HashMap; + +public class HamcrestFuzzer { + + HashMap hashMap = new HashMap(); + HashMap hashMap2 = new HashMap(); + + public HamcrestFuzzer(FuzzedDataProvider data) { + } + + public void runTest(FuzzedDataProvider data) { + + hashMap.put(data.consumeString(10), data.consumeString(10)); + hashMap2.put(data.consumeString(10), data.consumeString(10)); + + try { + assertThat(data.consumeString(10), containsString(data.consumeString(10))); + } catch (AssertionError e) { + // documented ignore + } + + try { + assertThat(data.consumeString(10),allOf(startsWith(data.consumeString(10)), containsString(data.consumeString(10)))); + } catch (AssertionError e) { + // documented ignore + } + + try { + assertThat(hashMap, is(hashMap2)); + } catch (AssertionError e) { + // documented ignore + } + + try { + assertThat(hashMap2, is(aMapWithSize(2))); + } catch (AssertionError e) { + // documented ignore + } + + try { + assertThat(hashMap2, is(anEmptyMap())); + } catch (AssertionError e) { + // documented ignore + } + + try { + assertThat(Arrays.asList(data.consumeString(10), data.consumeString(10)), hasSize(equalTo(2))); + } catch (AssertionError e) { + // documented ignore + } + + try { + assertThat(new ArrayList() { { add(data.consumeString(10)); add(data.consumeString(10));}}, is(empty())); + } catch (AssertionError e) { + // documented ignore + } + + try { + assertThat(new ArrayList() {{ add(data.consumeString(10)); add(data.consumeString(10));}}, is(emptyCollectionOf(String.class))); + } catch (AssertionError e) { + // documented ignore + } + + try { + assertThat(new ArrayList() {{add(data.consumeString(10));add(data.consumeString(10));}}, is(emptyIterable())); + } catch (AssertionError e) { + // documented ignore + } + + try { + assertThat(new ArrayList() {{add(data.consumeString(10));add(data.consumeString(10));}}, is(emptyIterableOf(String.class))); + } catch (AssertionError e) { + // documented ignore + } + + try { + assertThat(Arrays.asList(data.consumeString(10), data.consumeString(10)),contains(equalTo(data.consumeString(10)), equalTo(data.consumeString(10)))); + } catch (AssertionError e) { + // documented ignore + } + + try { + assertThat(Arrays.asList(data.consumeString(10), data.consumeString(10)),containsInAnyOrder(Arrays.asList(equalTo(data.consumeString(10)), equalTo(data.consumeString(10))))); + } catch (AssertionError e) { + // documented ignore + } + + try { + assertThat(hashMap, hasEntry(equalTo(data.consumeString(10)), equalTo(data.consumeString(10)))); + } catch (AssertionError e) { + // documented ignore + } + + try { + assertThat(hashMap, hasKey(equalTo(data.consumeString(10)))); + } catch (AssertionError e) { + // documented ignore + } + + try { + assertThat(hashMap, hasValue(data.consumeString(10))); + } catch (AssertionError e) { + // documented ignore + } + + try { + assertThat(data.consumeString(10), is(in(Arrays.asList(data.consumeString(10), data.consumeString(10))))); + } catch (AssertionError e) { + // documented ignore + } + + try { + assertThat(data.consumeString(10), isOneOf(data.consumeString(10), data.consumeString(10))); + } catch (AssertionError e) { + // documented ignore + } + + try { + assertThat(data.consumeString(10), is(oneOf(data.consumeString(10), data.consumeString(10)))); + } catch (AssertionError e) { + // documented ignore + } + + try { + assertThat(data.consumeDouble(), is(closeTo(data.consumeDouble(), data.consumeDouble()))); + } catch (AssertionError e) { + // documented ignore + } + + try { + assertThat(data.consumeDouble(), is(notANumber())); + } catch (AssertionError e) { + // documented ignore + } + + try { + assertThat(data.consumeInt(), comparesEqualTo(data.consumeInt())); + } catch (AssertionError e) { + // documented ignore + } + + try { + assertThat(data.consumeInt(), greaterThan(data.consumeInt())); + } catch (AssertionError e) { + // documented ignore + } + + try { + assertThat(data.consumeInt(), greaterThanOrEqualTo(data.consumeInt())); + } catch (AssertionError e) { + // documented ignore + } + + try { + assertThat(data.consumeInt(), lessThan(data.consumeInt())); + } catch (AssertionError e) { + // documented ignore + } + + try { + assertThat(data.consumeInt(), lessThanOrEqualTo(data.consumeInt())); + } catch (AssertionError e) { + // documented ignore + } + + try { + assertThat(data.consumeString(10), equalToIgnoringCase(data.consumeString(10))); + } catch (AssertionError e) { + // documented ignore + } + + try { + assertThat(data.consumeString(10), is(emptyString())); + } catch (AssertionError e) { + // documented ignore + } + + try { + assertThat(data.consumeString(10), is(blankString())); + } catch (AssertionError e) { + // documented ignore + } + + try { + assertThat(data.consumeInt(), lessThanOrEqualTo(data.consumeInt())); + } catch (AssertionError e) { + // documented ignore + } + + try { + assertThat(data.consumeString(10), equalToIgnoringCase(data.consumeString(10))); + } catch (AssertionError e) { + // documented ignore + } + + try { + assertThat(data.consumeString(10), is(emptyOrNullString())); + } catch (AssertionError e) { + // documented ignore + } + + try { + assertThat(data.consumeString(10), is(blankOrNullString())); + } catch (AssertionError e) { + // documented ignore + } + + try { + assertThat(data.consumeString(10), matchesPattern(data.consumeString(10))); + } catch (AssertionError | PatternSyntaxException e) { + // documented ignore + } + + try { + assertThat(data.consumeString(10),stringContainsInOrder(Arrays.asList(data.consumeString(10), data.consumeString(10)))); + } catch (AssertionError e) { + // documented ignore + } + + try { + assertThat(data.consumeString(10), hasLength(data.consumeInt())); + } catch (AssertionError e) { + // documented ignore + } + + try { + assertThat(data.consumeBoolean(), hasToString(data.consumeString(10))); + } catch (AssertionError e) { + // documented ignore + } + + try { + assertThat(Integer.class, typeCompatibleWith(Number.class)); + } catch (AssertionError e) { + // documented ignore + } + + try { + DocumentBuilderFactory documentFactory = DocumentBuilderFactory.newInstance(); + DocumentBuilder documentBuilder = documentFactory.newDocumentBuilder(); + Document document = documentBuilder.newDocument(); + + Element root = document.createElement(data.consumeString(10)); + document.appendChild(root); + + Element employee = document.createElement(data.consumeString(10)); + root.appendChild(employee); + + assertThat(root, hasXPath(data.consumeString(10), equalTo(data.consumeString(10)))); + } catch (AssertionError | DOMException | IllegalArgumentException | ParserConfigurationException e) { + // documented ignore + } + + } + + public static void fuzzerTestOneInput(FuzzedDataProvider data) { + HamcrestFuzzer testClosure = new HamcrestFuzzer(data); + testClosure.runTest(data); + } + +} diff --git a/projects/hamcrest/build.sh b/projects/hamcrest/build.sh new file mode 100644 index 000000000..d5164c431 --- /dev/null +++ b/projects/hamcrest/build.sh @@ -0,0 +1,52 @@ +#!/bin/bash -eu +# Copyright 2022 Google LLC +# +# 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. +# +################################################################################ + + +ALL_JARS="" + +pushd "${SRC}/hamcrest" + ./gradlew jar + install -v ./hamcrest/build/libs/hamcrest-*-SNAPSHOT.jar "$OUT/hamcrest.jar" + ALL_JARS="${ALL_JARS} hamcrest.jar" +popd + +# The classpath at build-time includes the project jars in $OUT as well as the +# Jazzer API. +BUILD_CLASSPATH=$(echo $ALL_JARS | xargs printf -- "$OUT/%s:"):$JAZZER_API_PATH + +# All .jar and .class files lie in the same directory as the fuzzer at runtime. +RUNTIME_CLASSPATH=$(echo $ALL_JARS | xargs printf -- "\$this_dir/%s:"):\$this_dir + +# compile all java files and copy them to $OUT +javac -cp $SRC:$BUILD_CLASSPATH -g $SRC/*.java +cp $SRC/*.class $OUT/ + +for fuzzer in $(find $SRC -name '*Fuzzer.java'); do + fuzzer_basename=$(basename -s .java $fuzzer) + + # Create an execution wrapper that executes Jazzer with the correct arguments. + echo "#!/bin/sh +# LLVMFuzzerTestOneInput for fuzzer detection. +this_dir=\$(dirname \"\$0\") +LD_LIBRARY_PATH=\"$JVM_LD_LIBRARY_PATH\":\$this_dir \ +\$this_dir/jazzer_driver --agent_path=\$this_dir/jazzer_agent_deploy.jar \ +--cp=$RUNTIME_CLASSPATH \ +--target_class=$fuzzer_basename \ +--jvm_args=\"-Xmx2048m\" \ +\$@" > $OUT/$fuzzer_basename + chmod u+x $OUT/$fuzzer_basename +done diff --git a/projects/hamcrest/project.yaml b/projects/hamcrest/project.yaml new file mode 100644 index 000000000..c132020c7 --- /dev/null +++ b/projects/hamcrest/project.yaml @@ -0,0 +1,14 @@ +homepage: http://hamcrest.org/ +language: jvm +main_repo: https://github.com/hamcrest/JavaHamcrest.git +fuzzing_engines: + - libfuzzer +sanitizers: + - address +vendor_ccs: + - "wagner@code-intelligence.com" + - "yakdan@code-intelligence.com" + - "glendowne@code-intelligence.com" + - "patrice.salathe@code-intelligence.com" + - "hlin@code-intelligence.com" + - "schaich@code-intelligence.com"