Coverage Report

Created: 2026-08-14 06:34

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/spirv-tools/source/val/validate.cpp
Line
Count
Source
1
// Copyright (c) 2015-2016 The Khronos Group Inc.
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
#include "source/val/validate.h"
16
17
#include <functional>
18
#include <iterator>
19
#include <memory>
20
#include <string>
21
#include <unordered_set>
22
#include <vector>
23
24
#include "source/binary.h"
25
#include "source/diagnostic.h"
26
#include "source/extensions.h"
27
#include "source/opcode.h"
28
#include "source/spirv_constant.h"
29
#include "source/spirv_endian.h"
30
#include "source/spirv_target_env.h"
31
#include "source/table2.h"
32
#include "source/val/construct.h"
33
#include "source/val/instruction.h"
34
#include "source/val/validation_state.h"
35
#include "spirv-tools/libspirv.h"
36
37
namespace {
38
// TODO(issue 1950): The validator only returns a single message anyway, so no
39
// point in generating more than 1 warning.
40
static uint32_t kDefaultMaxNumOfWarnings = 1;
41
}  // namespace
42
43
namespace spvtools {
44
namespace val {
45
namespace {
46
47
// Parses OpExtension instruction and registers extension.
48
void RegisterExtension(ValidationState_t& _,
49
19.6k
                       const spv_parsed_instruction_t* inst) {
50
19.6k
  const std::string extension_str = spvtools::GetExtensionString(inst);
51
19.6k
  Extension extension;
52
19.6k
  if (!GetExtensionFromString(extension_str.c_str(), &extension)) {
53
    // The error will be logged in the ProcessInstruction pass.
54
17.4k
    return;
55
17.4k
  }
56
57
2.19k
  _.RegisterExtension(extension);
58
2.19k
}
59
60
// Parses the beginning of the module searching for OpExtension instructions.
61
// Registers extensions if recognized. Returns SPV_REQUESTED_TERMINATION
62
// once an instruction which is not spv::Op::OpCapability and
63
// spv::Op::OpExtension is encountered. According to the SPIR-V spec extensions
64
// are declared after capabilities and before everything else.
65
spv_result_t ProcessExtensions(void* user_data,
66
135k
                               const spv_parsed_instruction_t* inst) {
67
135k
  const spv::Op opcode = static_cast<spv::Op>(inst->opcode);
68
135k
  if (opcode == spv::Op::OpCapability ||
69
72.1k
      opcode == spv::Op::OpConditionalCapabilityINTEL)
70
63.3k
    return SPV_SUCCESS;
71
72
72.0k
  if (opcode == spv::Op::OpExtension ||
73
52.7k
      opcode == spv::Op::OpConditionalExtensionINTEL) {
74
19.6k
    ValidationState_t& _ = *(reinterpret_cast<ValidationState_t*>(user_data));
75
19.6k
    RegisterExtension(_, inst);
76
19.6k
    return SPV_SUCCESS;
77
19.6k
  }
78
79
  // OpExtension block is finished, requesting termination.
80
52.3k
  return SPV_REQUESTED_TERMINATION;
81
72.0k
}
82
83
spv_result_t ProcessInstruction(void* user_data,
84
15.9M
                                const spv_parsed_instruction_t* inst) {
85
15.9M
  ValidationState_t& _ = *(reinterpret_cast<ValidationState_t*>(user_data));
86
87
15.9M
  auto* instruction = _.AddOrderedInstruction(inst);
88
15.9M
  _.RegisterDebugInstruction(instruction);
89
90
15.9M
  return SPV_SUCCESS;
91
15.9M
}
92
93
40.8k
spv_result_t ValidateForwardDecls(ValidationState_t& _) {
94
40.8k
  if (_.unresolved_forward_id_count() == 0) return SPV_SUCCESS;
95
96
2.87k
  std::stringstream ss;
97
2.87k
  std::vector<uint32_t> ids = _.UnresolvedForwardIds();
98
99
2.87k
  std::transform(
100
2.87k
      std::begin(ids), std::end(ids),
101
2.87k
      std::ostream_iterator<std::string>(ss, " "),
102
2.87k
      bind(&ValidationState_t::getIdName, std::ref(_), std::placeholders::_1));
103
104
2.87k
  auto id_str = ss.str();
105
2.87k
  return _.diag(SPV_ERROR_INVALID_ID, nullptr)
106
2.87k
         << "The following forward referenced IDs have not been defined:\n"
107
2.87k
         << id_str.substr(0, id_str.size() - 1);
108
40.8k
}
109
110
// Entry point validation. Based on 2.16.1 (Universal Validation Rules) of the
111
// SPIRV spec:
112
// * There is at least one OpEntryPoint instruction, unless the Linkage
113
//   capability is being used.
114
// * No function can be targeted by both an OpEntryPoint instruction and an
115
//   OpFunctionCall instruction.
116
//
117
// Additionally enforces that entry points for Vulkan should not have recursion.
118
30.5k
spv_result_t ValidateEntryPoints(ValidationState_t& _) {
119
30.5k
  _.ComputeFunctionToEntryPointMapping();
120
30.5k
  _.ComputeRecursiveEntryPoints();
121
122
30.5k
  if (_.entry_points().empty() && !_.HasCapability(spv::Capability::Linkage) &&
123
3.46k
      !_.HasCapability(spv::Capability::GraphARM)) {
124
3.46k
    return _.diag(SPV_ERROR_INVALID_BINARY, nullptr)
125
3.46k
           << "No OpEntryPoint instruction was found. This is only allowed if "
126
3.46k
              "the Linkage or GraphARM capability is being used.";
127
3.46k
  }
128
129
27.0k
  for (const auto& entry_point : _.entry_points()) {
130
20.8k
    if (_.IsFunctionCallTarget(entry_point)) {
131
59
      return _.diag(SPV_ERROR_INVALID_BINARY, _.FindDef(entry_point))
132
59
             << "A function (" << entry_point
133
59
             << ") may not be targeted by both an OpEntryPoint instruction and "
134
59
                "an OpFunctionCall instruction.";
135
59
    }
136
137
    // For Vulkan, the static function-call graph for an entry point
138
    // must not contain cycles.
139
20.7k
    if (spvIsVulkanEnv(_.context()->target_env)) {
140
0
      if (_.recursive_entry_points().find(entry_point) !=
141
0
          _.recursive_entry_points().end()) {
142
0
        return _.diag(SPV_ERROR_INVALID_BINARY, _.FindDef(entry_point))
143
0
               << _.VkErrorID(4634)
144
0
               << "Entry points may not have a call graph with cycles.";
145
0
      }
146
0
    }
147
20.7k
  }
148
149
27.0k
  if (auto error = ValidateFloatControls2(_)) {
150
0
    return error;
151
0
  }
152
27.0k
  if (auto error = ValidateDuplicateExecutionModes(_)) {
153
338
    return error;
154
338
  }
155
156
26.6k
  return SPV_SUCCESS;
157
27.0k
}
158
159
26.6k
spv_result_t ValidateGraphEntryPoints(ValidationState_t& _) {
160
26.6k
  if (_.graph_entry_points().empty() &&
161
26.6k
      _.HasCapability(spv::Capability::GraphARM)) {
162
0
    return _.diag(SPV_ERROR_INVALID_BINARY, nullptr)
163
0
           << "No OpGraphEntryPointARM instruction was found but the GraphARM "
164
0
              "capability is declared.";
165
0
  }
166
26.6k
  return SPV_SUCCESS;
167
26.6k
}
168
169
spv_result_t ValidateBinaryUsingContextAndValidationState(
170
    const spv_context_t& context, const uint32_t* words, const size_t num_words,
171
57.0k
    spv_diagnostic* pDiagnostic, ValidationState_t* vstate) {
172
57.0k
  auto binary = std::unique_ptr<spv_const_binary_t>(
173
57.0k
      new spv_const_binary_t{words, num_words});
174
175
57.0k
  spv_endianness_t endian;
176
57.0k
  spv_position_t position = {};
177
57.0k
  if (spvBinaryEndianness(binary.get(), &endian)) {
178
97
    return DiagnosticStream(position, context.consumer, "",
179
97
                            SPV_ERROR_INVALID_BINARY)
180
97
           << "Invalid SPIR-V magic number.";
181
97
  }
182
183
56.9k
  spv_header_t header;
184
56.9k
  if (spvBinaryHeaderGet(binary.get(), endian, &header)) {
185
64
    return DiagnosticStream(position, context.consumer, "",
186
64
                            SPV_ERROR_INVALID_BINARY)
187
64
           << "Invalid SPIR-V header.";
188
64
  }
189
190
56.8k
  if (header.version > spvVersionForTargetEnv(context.target_env)) {
191
1.06k
    return DiagnosticStream(position, context.consumer, "",
192
1.06k
                            SPV_ERROR_WRONG_VERSION)
193
1.06k
           << "Invalid SPIR-V binary version "
194
1.06k
           << SPV_SPIRV_VERSION_MAJOR_PART(header.version) << "."
195
1.06k
           << SPV_SPIRV_VERSION_MINOR_PART(header.version)
196
1.06k
           << " for target environment "
197
1.06k
           << spvTargetEnvDescription(context.target_env) << ".";
198
1.06k
  }
199
200
55.8k
  if (header.bound > vstate->options()->universal_limits_.max_id_bound) {
201
1.30k
    return DiagnosticStream(position, context.consumer, "",
202
1.30k
                            SPV_ERROR_INVALID_BINARY)
203
1.30k
           << "Invalid SPIR-V.  The id bound is larger than the max id bound "
204
1.30k
           << vstate->options()->universal_limits_.max_id_bound << ".";
205
1.30k
  }
206
207
  // Look for OpExtension instructions and register extensions.
208
  // This parse should not produce any error messages. Hijack the context and
209
  // replace the message consumer so that we do not pollute any state in input
210
  // consumer.
211
54.4k
  spv_context_t hijacked_context = context;
212
54.4k
  hijacked_context.consumer = [](spv_message_level_t, const char*,
213
54.4k
                                 const spv_position_t&, const char*) {};
214
54.4k
  spvBinaryParse(&hijacked_context, vstate, words, num_words,
215
54.4k
                 /* parsed_header = */ nullptr, ProcessExtensions,
216
54.4k
                 /* diagnostic = */ nullptr);
217
218
  // Parse the module and perform inline validation checks. These checks do
219
  // not require the knowledge of the whole module.
220
54.4k
  if (auto error = spvBinaryParse(&context, vstate, words, num_words,
221
54.4k
                                  /*parsed_header =*/nullptr,
222
54.4k
                                  ProcessInstruction, pDiagnostic)) {
223
1.49k
    return error;
224
1.49k
  }
225
226
53.0k
  bool has_mask_task_nv = false;
227
53.0k
  bool has_mask_task_ext = false;
228
53.0k
  std::vector<Instruction*> visited_entry_points;
229
53.0k
  std::unordered_set<std::string> graph_entry_point_names;
230
15.4M
  for (auto& instruction : vstate->ordered_instructions()) {
231
15.4M
    {
232
      // In order to do this work outside of Process Instruction we need to be
233
      // able to, briefly, de-const the instruction.
234
15.4M
      Instruction* inst = const_cast<Instruction*>(&instruction);
235
236
15.4M
      if ((inst->opcode() == spv::Op::OpEntryPoint) ||
237
15.4M
          (inst->opcode() == spv::Op::OpConditionalEntryPointINTEL)) {
238
30.1k
        const int i_model = inst->opcode() == spv::Op::OpEntryPoint ? 0 : 1;
239
30.1k
        const int i_point = inst->opcode() == spv::Op::OpEntryPoint ? 1 : 2;
240
30.1k
        const int i_name = inst->opcode() == spv::Op::OpEntryPoint ? 2 : 3;
241
30.1k
        const int min_num_operands =
242
30.1k
            inst->opcode() == spv::Op::OpEntryPoint ? 3 : 4;
243
244
30.1k
        const auto entry_point = inst->GetOperandAs<uint32_t>(i_point);
245
30.1k
        const auto execution_model =
246
30.1k
            inst->GetOperandAs<spv::ExecutionModel>(i_model);
247
30.1k
        const std::string desc_name = inst->GetOperandAs<std::string>(i_name);
248
249
30.1k
        ValidationState_t::EntryPointDescription desc;
250
30.1k
        desc.name = desc_name;
251
252
30.1k
        std::vector<uint32_t> interfaces;
253
1.54M
        for (size_t j = min_num_operands; j < inst->operands().size(); ++j)
254
1.51M
          desc.interfaces.push_back(inst->word(inst->operand(j).offset));
255
256
30.1k
        vstate->RegisterEntryPoint(entry_point, execution_model,
257
30.1k
                                   std::move(desc));
258
259
30.1k
        if (inst->opcode() == spv::Op::OpEntryPoint) {
260
          // conditional entry points are allowed to share the same name and
261
          // exec mode
262
30.1k
          if (visited_entry_points.size() > 0) {
263
22.3k
            for (const Instruction* check_inst : visited_entry_points) {
264
22.3k
              const auto check_execution_model =
265
22.3k
                  check_inst->GetOperandAs<spv::ExecutionModel>(i_model);
266
22.3k
              const std::string check_name =
267
22.3k
                  check_inst->GetOperandAs<std::string>(i_name);
268
269
22.3k
              if (desc_name == check_name &&
270
476
                  execution_model == check_execution_model) {
271
92
                return vstate->diag(SPV_ERROR_INVALID_DATA, inst)
272
92
                       << "2 Entry points cannot share the same name and "
273
92
                          "ExecutionMode.";
274
92
              }
275
22.3k
            }
276
4.91k
          }
277
30.0k
          visited_entry_points.push_back(inst);
278
30.0k
        }
279
280
30.0k
        has_mask_task_nv |= (execution_model == spv::ExecutionModel::TaskNV ||
281
30.0k
                             execution_model == spv::ExecutionModel::MeshNV);
282
30.0k
        has_mask_task_ext |= (execution_model == spv::ExecutionModel::TaskEXT ||
283
30.0k
                              execution_model == spv::ExecutionModel::MeshEXT);
284
30.0k
      }
285
15.4M
      if (inst->opcode() == spv::Op::OpGraphEntryPointARM) {
286
42
        const std::string name = inst->GetOperandAs<std::string>(1);
287
42
        if (!graph_entry_point_names.insert(name).second) {
288
0
          return vstate->diag(SPV_ERROR_INVALID_DATA, inst)
289
0
                 << "Graph entry points cannot share the same name.";
290
0
        }
291
42
        const auto graph = inst->GetOperandAs<uint32_t>(0);
292
42
        vstate->RegisterGraphEntryPoint(graph);
293
42
      }
294
15.4M
      if (inst->opcode() == spv::Op::OpFunctionCall) {
295
36.6k
        if (!vstate->in_function_body()) {
296
1
          return vstate->diag(SPV_ERROR_INVALID_LAYOUT, &instruction)
297
1
                 << "A FunctionCall must happen within a function body.";
298
1
        }
299
300
36.6k
        const auto called_id = inst->GetOperandAs<uint32_t>(2);
301
36.6k
        vstate->AddFunctionCallTarget(called_id);
302
36.6k
      }
303
304
15.4M
      if (vstate->in_function_body()) {
305
3.03M
        inst->set_function(&(vstate->current_function()));
306
3.03M
        inst->set_block(vstate->current_function().current_block());
307
308
3.03M
        if (vstate->in_block() && spvOpcodeIsBlockTerminator(inst->opcode())) {
309
489k
          vstate->current_function().current_block()->set_terminator(inst);
310
489k
        }
311
3.03M
      }
312
313
15.4M
      if (auto error = IdPass(*vstate, inst)) return error;
314
15.4M
    }
315
316
15.4M
    if (auto error = CapabilityPass(*vstate, &instruction)) return error;
317
15.4M
    if (auto error = ModuleLayoutPass(*vstate, &instruction)) return error;
318
15.4M
    if (auto error = CfgPass(*vstate, &instruction)) return error;
319
15.4M
    if (auto error = InstructionPass(*vstate, &instruction)) return error;
320
321
    // Now that all of the checks are done, update the state.
322
15.4M
    {
323
15.4M
      Instruction* inst = const_cast<Instruction*>(&instruction);
324
15.4M
      vstate->RegisterInstruction(inst);
325
15.4M
      if (inst->opcode() == spv::Op::OpTypeForwardPointer) {
326
930
        vstate->RegisterForwardPointer(inst->GetOperandAs<uint32_t>(0));
327
930
      }
328
15.4M
    }
329
15.4M
  }
330
331
43.3k
  if (!vstate->has_memory_model_specified())
332
726
    return vstate->diag(SPV_ERROR_INVALID_LAYOUT, nullptr)
333
726
           << "Missing required OpMemoryModel instruction.";
334
335
42.6k
  if (vstate->in_function_body())
336
1.78k
    return vstate->diag(SPV_ERROR_INVALID_LAYOUT, nullptr)
337
1.78k
           << "Missing OpFunctionEnd at end of module.";
338
339
40.8k
  if (vstate->graph_definition_region() != kGraphDefinitionOutside)
340
0
    return vstate->diag(SPV_ERROR_INVALID_LAYOUT, nullptr)
341
0
           << "Missing OpGraphEndARM at end of module.";
342
343
40.8k
  if (vstate->HasCapability(spv::Capability::BindlessTextureNV) &&
344
0
      !vstate->has_samplerimage_variable_address_mode_specified())
345
0
    return vstate->diag(SPV_ERROR_INVALID_LAYOUT, nullptr)
346
0
           << "Missing required OpSamplerImageAddressingModeNV instruction.";
347
348
40.8k
  if (has_mask_task_ext && has_mask_task_nv)
349
0
    return vstate->diag(SPV_ERROR_INVALID_LAYOUT, nullptr)
350
0
           << vstate->VkErrorID(7102)
351
0
           << "Module can't mix MeshEXT/TaskEXT with MeshNV/TaskNV Execution "
352
0
              "Model.";
353
354
  // Catch undefined forward references before performing further checks.
355
40.8k
  if (auto error = ValidateForwardDecls(*vstate)) return error;
356
357
  // Calculate reachability after all the blocks are parsed, but early that it
358
  // can be relied on in subsequent passes.
359
37.9k
  ReachabilityPass(*vstate);
360
361
  // ID usage needs be handled in its own iteration of the instructions,
362
  // between the two others. It depends on the first loop to have been
363
  // finished, so that all instructions have been registered. And the following
364
  // loop depends on all of the usage data being populated. Thus it cannot live
365
  // in either of those iterations.
366
  // It should also live after the forward declaration check, since it will
367
  // have problems with missing forward declarations, but give less useful error
368
  // messages.
369
15.1M
  for (size_t i = 0; i < vstate->ordered_instructions().size(); ++i) {
370
15.1M
    auto& instruction = vstate->ordered_instructions()[i];
371
15.1M
    if (auto error = UpdateIdUse(*vstate, &instruction)) return error;
372
15.1M
  }
373
374
  // Validate individual opcodes.
375
14.9M
  for (size_t i = 0; i < vstate->ordered_instructions().size(); ++i) {
376
14.9M
    auto& instruction = vstate->ordered_instructions()[i];
377
378
    // Keep these passes in the order they appear in the SPIR-V specification
379
    // sections to maintain test consistency.
380
14.9M
    if (auto error = MiscPass(*vstate, &instruction)) return error;
381
14.9M
    if (auto error = DebugPass(*vstate, &instruction)) return error;
382
14.9M
    if (auto error = AnnotationPass(*vstate, &instruction)) return error;
383
14.9M
    if (auto error = ExtensionPass(*vstate, &instruction)) return error;
384
14.9M
    if (auto error = ModeSettingPass(*vstate, &instruction)) return error;
385
14.9M
    if (auto error = TypePass(*vstate, &instruction)) return error;
386
14.9M
    if (auto error = ConstantPass(*vstate, &instruction)) return error;
387
14.9M
    if (auto error = MemoryPass(*vstate, &instruction)) return error;
388
14.9M
    if (auto error = FunctionPass(*vstate, &instruction)) return error;
389
14.9M
    if (auto error = ImagePass(*vstate, &instruction)) return error;
390
14.9M
    if (auto error = ConversionPass(*vstate, &instruction)) return error;
391
14.9M
    if (auto error = CompositesPass(*vstate, &instruction)) return error;
392
14.9M
    if (auto error = ArithmeticsPass(*vstate, &instruction)) return error;
393
14.9M
    if (auto error = BitwisePass(*vstate, &instruction)) return error;
394
14.9M
    if (auto error = LogicalsPass(*vstate, &instruction)) return error;
395
14.9M
    if (auto error = ControlFlowPass(*vstate, &instruction)) return error;
396
14.9M
    if (auto error = DerivativesPass(*vstate, &instruction)) return error;
397
14.9M
    if (auto error = AtomicsPass(*vstate, &instruction)) return error;
398
14.9M
    if (auto error = PrimitivesPass(*vstate, &instruction)) return error;
399
14.9M
    if (auto error = BarriersPass(*vstate, &instruction)) return error;
400
14.9M
    if (auto error = DotProductPass(*vstate, &instruction)) return error;
401
14.9M
    if (auto error = GroupPass(*vstate, &instruction)) return error;
402
    // Device-Side Enqueue
403
14.9M
    if (auto error = PipePass(*vstate, &instruction)) return error;
404
14.9M
    if (auto error = NonUniformPass(*vstate, &instruction)) return error;
405
406
14.9M
    if (auto error = LiteralsPass(*vstate, &instruction)) return error;
407
14.9M
    if (auto error = RayQueryPass(*vstate, &instruction)) return error;
408
14.9M
    if (auto error = RayTracingPass(*vstate, &instruction)) return error;
409
14.9M
    if (auto error = RayReorderNVPass(*vstate, &instruction)) return error;
410
14.9M
    if (auto error = RayReorderEXTPass(*vstate, &instruction)) return error;
411
14.9M
    if (auto error = MeshShadingPass(*vstate, &instruction)) return error;
412
14.9M
    if (auto error = TensorLayoutPass(*vstate, &instruction)) return error;
413
14.9M
    if (auto error = TensorPass(*vstate, &instruction)) return error;
414
14.9M
    if (auto error = GraphPass(*vstate, &instruction)) return error;
415
14.9M
    if (auto error = InvalidTypePass(*vstate, &instruction)) return error;
416
14.9M
  }
417
418
  // Validate the preconditions involving adjacent instructions. e.g.
419
  // spv::Op::OpPhi must only be preceded by spv::Op::OpLabel, spv::Op::OpPhi,
420
  // or spv::Op::OpLine.
421
30.6k
  if (auto error = ValidateAdjacency(*vstate)) return error;
422
423
30.5k
  if (auto error = ValidateEntryPoints(*vstate)) return error;
424
26.6k
  if (auto error = ValidateGraphEntryPoints(*vstate)) return error;
425
  // CFG checks are performed after the binary has been parsed
426
  // and the CFGPass has collected information about the control flow
427
26.6k
  if (auto error = PerformCfgChecks(*vstate)) return error;
428
25.1k
  if (auto error = CheckIdDefinitionDominateUse(*vstate)) return error;
429
25.0k
  if (auto error = ValidateDecorations(*vstate)) return error;
430
23.4k
  if (auto error = ValidateExplicitLayout(*vstate)) return error;
431
23.4k
  if (auto error = ValidateInterfaces(*vstate)) return error;
432
  // TODO(dsinclair): Restructure ValidateBuiltins so we can move into the
433
  // for() above as it loops over all ordered_instructions internally.
434
23.3k
  if (auto error = ValidateBuiltIns(*vstate)) return error;
435
  // These checks must be performed after individual opcode checks because
436
  // those checks register the limitation checked here.
437
13.8M
  for (const auto& inst : vstate->ordered_instructions()) {
438
13.8M
    if (auto error = ValidateExecutionLimitations(*vstate, &inst)) return error;
439
13.8M
    if (auto error = ValidateSmallTypeUses(*vstate, &inst)) return error;
440
13.8M
    if (auto error = ValidateQCOMImageProcessingTextureUsages(*vstate, &inst))
441
0
      return error;
442
13.8M
  }
443
23.1k
  if (auto error = ValidateLogicalPointers(*vstate)) return error;
444
445
23.0k
  return SPV_SUCCESS;
446
23.1k
}
447
448
}  // namespace
449
450
spv_result_t ValidateBinaryAndKeepValidationState(
451
    const spv_const_context context, spv_const_validator_options options,
452
    const uint32_t* words, const size_t num_words, spv_diagnostic* pDiagnostic,
453
0
    std::unique_ptr<ValidationState_t>* vstate) {
454
0
  spv_context_t hijack_context = *context;
455
0
  if (pDiagnostic) {
456
0
    *pDiagnostic = nullptr;
457
0
    UseDiagnosticAsMessageConsumer(&hijack_context, pDiagnostic);
458
0
  }
459
460
0
  vstate->reset(new ValidationState_t(&hijack_context, options, words,
461
0
                                      num_words, kDefaultMaxNumOfWarnings));
462
463
0
  return ValidateBinaryUsingContextAndValidationState(
464
0
      hijack_context, words, num_words, pDiagnostic, vstate->get());
465
0
}
466
467
}  // namespace val
468
}  // namespace spvtools
469
470
spv_result_t spvValidate(const spv_const_context context,
471
                         const spv_const_binary binary,
472
0
                         spv_diagnostic* pDiagnostic) {
473
0
  return spvValidateBinary(context, binary->code, binary->wordCount,
474
0
                           pDiagnostic);
475
0
}
476
477
spv_result_t spvValidateBinary(const spv_const_context context,
478
                               const uint32_t* words, const size_t num_words,
479
20.4k
                               spv_diagnostic* pDiagnostic) {
480
20.4k
  spv_context_t hijack_context = *context;
481
20.4k
  if (pDiagnostic) {
482
0
    *pDiagnostic = nullptr;
483
0
    spvtools::UseDiagnosticAsMessageConsumer(&hijack_context, pDiagnostic);
484
0
  }
485
486
  // This interface is used for default command line options.
487
20.4k
  spv_validator_options default_options = spvValidatorOptionsCreate();
488
489
  // Create the ValidationState using the context and default options.
490
20.4k
  spvtools::val::ValidationState_t vstate(&hijack_context, default_options,
491
20.4k
                                          words, num_words,
492
20.4k
                                          kDefaultMaxNumOfWarnings);
493
494
20.4k
  spv_result_t result =
495
20.4k
      spvtools::val::ValidateBinaryUsingContextAndValidationState(
496
20.4k
          hijack_context, words, num_words, pDiagnostic, &vstate);
497
498
20.4k
  spvValidatorOptionsDestroy(default_options);
499
20.4k
  return result;
500
20.4k
}
501
502
spv_result_t spvValidateWithOptions(const spv_const_context context,
503
                                    spv_const_validator_options options,
504
                                    const spv_const_binary binary,
505
36.5k
                                    spv_diagnostic* pDiagnostic) {
506
36.5k
  spv_context_t hijack_context = *context;
507
36.5k
  if (pDiagnostic) {
508
36.5k
    *pDiagnostic = nullptr;
509
36.5k
    spvtools::UseDiagnosticAsMessageConsumer(&hijack_context, pDiagnostic);
510
36.5k
  }
511
512
  // Create the ValidationState using the context.
513
36.5k
  spvtools::val::ValidationState_t vstate(&hijack_context, options,
514
36.5k
                                          binary->code, binary->wordCount,
515
36.5k
                                          kDefaultMaxNumOfWarnings);
516
517
36.5k
  return spvtools::val::ValidateBinaryUsingContextAndValidationState(
518
36.5k
      hijack_context, binary->code, binary->wordCount, pDiagnostic, &vstate);
519
36.5k
}