/src/model_load_fuzzer.cc
Line | Count | Source |
1 | | // Copyright 2026 Google LLC |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | | // you may not use this file except in compliance with the License. |
5 | | // You may obtain a copy of the License at |
6 | | // |
7 | | // http://www.apache.org/licenses/LICENSE-2.0 |
8 | | // |
9 | | // Unless required by applicable law or agreed to in writing, software |
10 | | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | | // See the License for the specific language governing permissions and |
13 | | // limitations under the License. |
14 | | |
15 | | // Fuzzer for SentencePiece model loading and post-load operations. |
16 | | // Feeds arbitrary binary data as a serialized ModelProto, then exercises |
17 | | // encoding/decoding if the model loads successfully. |
18 | | |
19 | | #include <cstddef> |
20 | | #include <cstdint> |
21 | | #include <cstdio> |
22 | | #include <string> |
23 | | #include <unistd.h> |
24 | | #include <vector> |
25 | | |
26 | | #include <fuzzer/FuzzedDataProvider.h> |
27 | | #include "sentencepiece_processor.h" |
28 | | |
29 | | // Writes `model_data` to a temporary file and loads it through the file-based |
30 | | // Load() entry point, which exercises src/filesystem.cc (PosixReadableFile / |
31 | | // ReadAll) and io::LoadModelProto. The in-memory LoadFromSerializedProto path |
32 | | // below does not go through the filesystem layer. |
33 | 11.4k | static void ExerciseFileLoad(const std::string &model_data) { |
34 | 11.4k | char path[] = "/tmp/spm_model_fuzz_XXXXXX"; |
35 | 11.4k | const int fd = mkstemp(path); |
36 | 11.4k | if (fd < 0) |
37 | 0 | return; |
38 | 11.4k | if (!model_data.empty()) { |
39 | 11.4k | const ssize_t written = write(fd, model_data.data(), model_data.size()); |
40 | 11.4k | (void)written; |
41 | 11.4k | } |
42 | 11.4k | close(fd); |
43 | | |
44 | 11.4k | sentencepiece::SentencePieceProcessor file_processor; |
45 | | // Return value intentionally ignored; a successful load is further exercised |
46 | | // by the in-memory processor below. |
47 | 11.4k | file_processor.Load(path); |
48 | | |
49 | 11.4k | unlink(path); |
50 | 11.4k | } |
51 | | |
52 | 11.4k | extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { |
53 | 11.4k | if (size < 4) |
54 | 2 | return 0; |
55 | | |
56 | 11.4k | FuzzedDataProvider fdp(data, size); |
57 | | |
58 | | // Split data: most goes to model, some to test text |
59 | 11.4k | std::string model_data = fdp.ConsumeRandomLengthString(size); |
60 | 11.4k | std::string test_text = fdp.ConsumeRemainingBytesAsString(); |
61 | | |
62 | | // Exercise the on-disk loading path (filesystem.cc + io::LoadModelProto). |
63 | 11.4k | ExerciseFileLoad(model_data); |
64 | | |
65 | 11.4k | sentencepiece::SentencePieceProcessor processor; |
66 | | |
67 | | // Try loading fuzz data as a serialized model proto |
68 | 11.4k | auto status = processor.LoadFromSerializedProto(model_data); |
69 | 11.4k | if (!status.ok()) |
70 | 4.91k | return 0; |
71 | | |
72 | | // Model loaded successfully - exercise all major operations |
73 | | |
74 | | // Basic encoding |
75 | 6.56k | std::vector<std::string> pieces; |
76 | 6.56k | processor.Encode(test_text, &pieces); |
77 | | |
78 | | // Encode to IDs |
79 | 6.56k | std::vector<int> ids; |
80 | 6.56k | processor.Encode(test_text, &ids); |
81 | | |
82 | | // Decode from pieces |
83 | 6.56k | if (!pieces.empty()) { |
84 | 5.05k | std::string decoded; |
85 | 5.05k | processor.Decode(pieces, &decoded); |
86 | 5.05k | } |
87 | | |
88 | | // Decode from IDs |
89 | 6.56k | if (!ids.empty()) { |
90 | 5.05k | std::string decoded; |
91 | 5.05k | processor.Decode(ids, &decoded); |
92 | 5.05k | } |
93 | | |
94 | | // Normalization |
95 | 6.56k | std::string normalized; |
96 | 6.56k | processor.Normalize(test_text, &normalized); |
97 | | |
98 | | // Vocabulary operations |
99 | 6.56k | int vocab_size = processor.GetPieceSize(); |
100 | 6.56k | if (vocab_size > 0) { |
101 | | // PieceToId / IdToPiece round-trip |
102 | 33.5k | for (int i = 0; i < vocab_size && i < 10; i++) { |
103 | 27.0k | std::string piece = processor.IdToPiece(i); |
104 | 27.0k | processor.PieceToId(piece); |
105 | 27.0k | processor.GetScore(i); |
106 | 27.0k | processor.IsUnknown(i); |
107 | 27.0k | processor.IsControl(i); |
108 | 27.0k | processor.IsUnused(i); |
109 | 27.0k | processor.IsByte(i); |
110 | 27.0k | } |
111 | | |
112 | | // Try lookup with test_text as a piece |
113 | 6.56k | processor.PieceToId(test_text); |
114 | 6.56k | } |
115 | | |
116 | | // Special token IDs |
117 | 6.56k | processor.unk_id(); |
118 | 6.56k | processor.bos_id(); |
119 | 6.56k | processor.eos_id(); |
120 | 6.56k | processor.pad_id(); |
121 | | |
122 | | // NBest encoding (with small nbest_size to avoid slowness) |
123 | 6.56k | std::vector<std::vector<std::string>> nbest_pieces; |
124 | 6.56k | processor.NBestEncode(test_text, 2, &nbest_pieces); |
125 | | |
126 | | // Sample encoding |
127 | 6.56k | std::vector<std::string> sampled; |
128 | 6.56k | processor.SampleEncode(test_text, 1, 0.5, &sampled); |
129 | | |
130 | | // Upstream dropped EncodeAsSerializedProto/SampleEncodeAsSerializedProto. |
131 | | // The piece-returning overloads reach the same encode paths; they are |
132 | | // [[nodiscard]], so keep the results alive. |
133 | 6.56k | const auto encoded_pieces = processor.EncodeAsPieces(test_text); |
134 | 6.56k | const auto sampled_pieces = processor.SampleEncodeAsPieces(test_text, 1, 0.5); |
135 | 6.56k | (void)encoded_pieces.size(); |
136 | 6.56k | (void)sampled_pieces.size(); |
137 | | |
138 | | // Get serialized model |
139 | 6.56k | processor.serialized_model_proto(); |
140 | | |
141 | 6.56k | return 0; |
142 | 11.4k | } |