diff --git a/projects/llamacpp/build.sh b/projects/llamacpp/build.sh index 6a85ca938a30..3667ebcdb782 100755 --- a/projects/llamacpp/build.sh +++ b/projects/llamacpp/build.sh @@ -47,6 +47,7 @@ FLAGS="-std=c++11 -Iggml/include -Iggml/src -Iinclude -Isrc -Icommon -I./ -DNDEB cp fuzzers/*.dict $OUT/ $CXX $LIB_FUZZING_ENGINE $CXXFLAGS ${FLAGS} ${OBJ_FILES} fuzzers/fuzz_json_to_grammar.cpp -o $OUT/fuzz_json_to_grammar +$CXX $LIB_FUZZING_ENGINE $CXXFLAGS ${FLAGS} ${OBJ_FILES} fuzzers/fuzz_apply_template.cpp -o $OUT/fuzz_apply_template $CXX $LIB_FUZZING_ENGINE $CXXFLAGS ${FLAGS} ${OBJ_FILES} fuzzers/fuzz_grammar.cpp -o $OUT/fuzz_grammar # Create a corpus for load_model_fuzzer diff --git a/projects/llamacpp/fuzzers/fuzz_apply_template.cpp b/projects/llamacpp/fuzzers/fuzz_apply_template.cpp new file mode 100644 index 000000000000..9e27dabe55ee --- /dev/null +++ b/projects/llamacpp/fuzzers/fuzz_apply_template.cpp @@ -0,0 +1,39 @@ +/* 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 "llama.h" +#include + +char buf[4096]; + +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { + FuzzedDataProvider fuzzed_data(data, size); + + std::string p1 = fuzzed_data.ConsumeRandomLengthString(); + std::string p2 = fuzzed_data.ConsumeRandomLengthString(); + std::string p3 = fuzzed_data.ConsumeRandomLengthString(); + std::string p4 = fuzzed_data.ConsumeRandomLengthString(); + std::string p5 = fuzzed_data.ConsumeRandomLengthString(); + std::string p6 = fuzzed_data.ConsumeRandomLengthString(); + std::string p7 = fuzzed_data.ConsumeRandomLengthString(); + + llama_chat_message conversation[] = { + {"system", p2.c_str()}, {"user", p3.c_str()}, + {"assistant", p4.c_str()}, {"user", p5.c_str()}, + {"assistant", p6.c_str()}, {"user", p7.c_str()}, + }; + size_t message_count = 6; + + llama_chat_apply_template(nullptr, p1.c_str(), conversation, message_count, + true, buf, 4096); + return 0; +}