From 73cfa4c8f8b271233ec85e1c671e2a22c5a96a6a Mon Sep 17 00:00:00 2001 From: David Korczynski Date: Mon, 11 Nov 2024 16:15:03 -0800 Subject: [PATCH 1/3] spirv-cross: initial integration Signed-off-by: David Korczynski --- projects/spirv-cross/Dockerfile | 27 ++++++++++++++++ projects/spirv-cross/build.sh | 41 ++++++++++++++++++++++++ projects/spirv-cross/parser_fuzzer.cpp | 44 ++++++++++++++++++++++++++ projects/spirv-cross/project.yaml | 6 ++++ 4 files changed, 118 insertions(+) create mode 100644 projects/spirv-cross/Dockerfile create mode 100644 projects/spirv-cross/build.sh create mode 100644 projects/spirv-cross/parser_fuzzer.cpp create mode 100644 projects/spirv-cross/project.yaml diff --git a/projects/spirv-cross/Dockerfile b/projects/spirv-cross/Dockerfile new file mode 100644 index 000000000000..17a571749e46 --- /dev/null +++ b/projects/spirv-cross/Dockerfile @@ -0,0 +1,27 @@ +# Copyright 2024 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 +RUN apt-get update && + apt-get install -y build-essential autoconf automake libtool pkg-config make \ + cmake + + +RUN git clone https://github.com/KhronosGroup/SPIRV-Cross spirv-cross +WORKDIR $SRC/spirv-cross + +COPY *_fuzzer.cpp build.sh $SRC/ + diff --git a/projects/spirv-cross/build.sh b/projects/spirv-cross/build.sh new file mode 100644 index 000000000000..0657fe112333 --- /dev/null +++ b/projects/spirv-cross/build.sh @@ -0,0 +1,41 @@ +#!/bin/bash -eu +# Copyright 2024 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. +# +################################################################################ + +# Update submodule +./checkout_glslang_spirv_tools.sh +NPROC="--parallel $(nproc)" ./build_glslang_spirv_tools.sh + +# Build spirv-cross binaries with debug options +cmake -S . -B build/ -D CMAKE_BUILD_TYPE=Debug \ + -D CMAKE_CXX_FLAGS="$CXXFLAGS -pthread -stdlib=libc++" +cmake --build build --config Debug --parallel $(nproc) + +# Copy built binaries of the spirv-cross project +for fuzzers in $(find $SRC -maxdepth 1 -name '*_fuzzer.cpp'); do + fuzz_basename=$(basename -s .cpp $fuzzers) + $CXX $CXXFLAGS -std=c++17 -I$SRC/spirv-cross \ + -I$SRC/spirv-cross/external/spirv-tools \ + -I$SRC/spirv-cross/external/spirv-tools/include \ + -c $fuzzers -o $fuzz_basename.o + + $CXX $CXXFLAGS -std=c++17 $LIB_FUZZING_ENGINE \ + $fuzz_basename.o -o $OUT/$fuzz_basename \ + -Wl,--start-group \ + $SRC/spirv-cross/build/*.a \ + $SRC/spirv-cross/external/glslang-build/output/lib/*.a \ + -Wl,--end-group +done diff --git a/projects/spirv-cross/parser_fuzzer.cpp b/projects/spirv-cross/parser_fuzzer.cpp new file mode 100644 index 000000000000..f726aecc63b6 --- /dev/null +++ b/projects/spirv-cross/parser_fuzzer.cpp @@ -0,0 +1,44 @@ +/* Copyright 2024 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. +*/ + +#include "spirv_common.hpp" +#include "spirv_parser.hpp" +#include +#include + +using namespace spirv_cross; + +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { + // Skip this iteration if data is not enough + if (size < (sizeof(uint32_t) * 5) || (size % 4 != 0)) { + return 0; + } + + // Initialise objects and random data + std::vector spirv_data((uint32_t *)data, (uint32_t *)(data + size)); + + // Set magic number, since this is needed to get past initial checks. + spirv_data[0] = 0x07230203; + spirv_data[1] = 0x10600; + + Parser parser(spirv_data); + ParsedIR &ir = parser.get_parsed_ir(); + SPIRFunction *current_function = nullptr; + SPIRBlock *current_block = nullptr; + + try { + parser.parse(); + } catch (...) { + } + + return 0; +} diff --git a/projects/spirv-cross/project.yaml b/projects/spirv-cross/project.yaml new file mode 100644 index 000000000000..01c9fad8ec8f --- /dev/null +++ b/projects/spirv-cross/project.yaml @@ -0,0 +1,6 @@ +homepage: "https://github.com/KhronosGroup/SPIRV-Cross" +main_repo: "https://github.com/KhronosGroup/SPIRV-Cross.git" +language: c++ +vendor_ccs: +- "david@adalogics.com" +- "arthur.chan@adalogics.com" From 7de0df4aadfd65fd6de6f167392d63622c51db31 Mon Sep 17 00:00:00 2001 From: David Korczynski Date: Mon, 11 Nov 2024 16:22:19 -0800 Subject: [PATCH 2/3] nit Signed-off-by: David Korczynski --- projects/spirv-cross/Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/projects/spirv-cross/Dockerfile b/projects/spirv-cross/Dockerfile index 17a571749e46..565148b074b8 100644 --- a/projects/spirv-cross/Dockerfile +++ b/projects/spirv-cross/Dockerfile @@ -15,7 +15,7 @@ ################################################################################ FROM gcr.io/oss-fuzz-base/base-builder -RUN apt-get update && +RUN apt-get update && \ apt-get install -y build-essential autoconf automake libtool pkg-config make \ cmake From 9f745df9f6d84c51b56c59785b26f77070bb5ace Mon Sep 17 00:00:00 2001 From: David Korczynski Date: Mon, 11 Nov 2024 16:32:18 -0800 Subject: [PATCH 3/3] nit Signed-off-by: David Korczynski --- projects/spirv-cross/project.yaml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/projects/spirv-cross/project.yaml b/projects/spirv-cross/project.yaml index 01c9fad8ec8f..98d1e51de6b8 100644 --- a/projects/spirv-cross/project.yaml +++ b/projects/spirv-cross/project.yaml @@ -1,6 +1,8 @@ homepage: "https://github.com/KhronosGroup/SPIRV-Cross" main_repo: "https://github.com/KhronosGroup/SPIRV-Cross.git" language: c++ +sanitizers: +- address vendor_ccs: - "david@adalogics.com" - "arthur.chan@adalogics.com"