Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

spirv-cross: initial integration #12708

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions projects/spirv-cross/Dockerfile
Original file line number Diff line number Diff line change
@@ -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/

41 changes: 41 additions & 0 deletions projects/spirv-cross/build.sh
Original file line number Diff line number Diff line change
@@ -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
44 changes: 44 additions & 0 deletions projects/spirv-cross/parser_fuzzer.cpp
Original file line number Diff line number Diff line change
@@ -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 <cstdint>
#include <vector>

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<uint32_t> 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;
}
8 changes: 8 additions & 0 deletions projects/spirv-cross/project.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
homepage: "https://github.com/KhronosGroup/SPIRV-Cross"
main_repo: "https://github.com/KhronosGroup/SPIRV-Cross.git"
language: c++
sanitizers:
- address
vendor_ccs:
- "[email protected]"
- "[email protected]"