Coverage Report

Created: 2026-11-22 06:21

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/lite_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 the FlatBuffers-based "lite" tokenizer
16
// (lite/sentencepiece_lite.cc, lite/sentencepiece_model_converters.cc).
17
// SentencePieceLiteProcessor loads its model from a raw byte buffer that it
18
// parses in place as a FlatBuffers blob (a double-array trie and vocabulary),
19
// with pointer arithmetic over the bytes (DoubleArray::transition, LoadLe64,
20
// StringScanner).
21
//
22
// Two complementary modes:
23
//   * Valid-model mode converts the embedded ModelProto into a lite flatbuffer
24
//     (exercising ToFlatbuffer in sentencepiece_model_converters.cc) and drives
25
//     Encode / Normalize / sampling with fuzzed text.
26
//   * Raw-buffer mode hands arbitrary fuzzer bytes to the constructor to
27
//     exercise the flatbuffer load/verification and error paths directly.
28
29
#include <cstddef>
30
#include <cstdint>
31
#include <memory>
32
#include <string>
33
#include <vector>
34
35
#include <fuzzer/FuzzedDataProvider.h>
36
37
#include "sentencepiece_lite.h"
38
#include "sentencepiece_model_converters.h"
39
#include "sentencepiece_model.pb.h"
40
41
#include "embedded_model.h"
42
43
using sentencepiece::lite::SentencePieceLiteProcessor;
44
using sentencepiece::lite::StatusCode;
45
46
// A valid lite flatbuffer, built once from the trusted embedded model.
47
static std::shared_ptr<std::string> g_valid_buffer;
48
49
2
extern "C" int LLVMFuzzerInitialize(int * /*argc*/, char *** /*argv*/) {
50
2
  sentencepiece::ModelProto proto;
51
2
  if (proto.ParseFromArray(reinterpret_cast<const char *>(kEmbeddedModelData),
52
2
                           static_cast<int>(kEmbeddedModelSize))) {
53
2
    auto buffer = sentencepiece::lite::ToFlatbuffer(proto);
54
2
    if (buffer.ok()) {
55
2
      g_valid_buffer = std::make_shared<std::string>(std::move(buffer).value());
56
2
    }
57
2
  }
58
2
  return 0;
59
2
}
60
61
static void DriveProcessor(const SentencePieceLiteProcessor &proc,
62
5.08k
                           const std::string &text) {
63
5.08k
  if (proc.status() != StatusCode::kOk)
64
3.72k
    return;
65
66
1.36k
  std::vector<int> ids;
67
1.36k
  proc.Encode(text, &ids);
68
69
1.36k
  std::string normalized;
70
1.36k
  std::vector<size_t> offsets;
71
1.36k
  proc.Normalize(text, &normalized, &offsets);
72
73
1.36k
  proc.CanSkipNormalization(text);
74
75
1.36k
  if (!normalized.empty()) {
76
1.34k
    std::vector<int> nids;
77
1.34k
    std::vector<std::string_view> pieces;
78
1.34k
    proc.EncodeNormalized(normalized, &nids, &pieces);
79
1.34k
  }
80
1.36k
}
81
82
5.08k
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
83
5.08k
  if (size < 1)
84
0
    return 0;
85
86
5.08k
  FuzzedDataProvider fdp(data, size);
87
5.08k
  const bool use_valid_model = fdp.ConsumeBool();
88
89
5.08k
  if (use_valid_model && g_valid_buffer) {
90
    // Happy path: a valid model buffer with fuzzed text drives the tokenizer
91
    // (lite trie lookups, normalization, encoding).
92
1.36k
    const std::string text = fdp.ConsumeRemainingBytesAsString();
93
1.36k
    SentencePieceLiteProcessor proc(g_valid_buffer);
94
1.36k
    DriveProcessor(proc, text);
95
3.72k
  } else {
96
    // Feed arbitrary bytes as the serialized flatbuffer model. The shared_ptr
97
    // constructor keeps the buffer alive for the processor's lifetime (the
98
    // buffer is referenced, not copied).
99
3.72k
    auto buffer =
100
3.72k
        std::make_shared<std::string>(fdp.ConsumeRemainingBytesAsString());
101
3.72k
    SentencePieceLiteProcessor proc(buffer);
102
    // If a mutated buffer happens to load, exercise the tokenizer too.
103
3.72k
    DriveProcessor(proc, "hello world");
104
3.72k
  }
105
106
5.08k
  return 0;
107
5.08k
}