Coverage Report

Created: 2026-10-28 06:54

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/shaderc/third_party/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
198
                       const spv_parsed_instruction_t* inst) {
50
198
  const std::string extension_str = spvtools::GetExtensionString(inst);
51
198
  Extension extension;
52
198
  if (!GetExtensionFromString(extension_str.c_str(), &extension)) {
53
    // The error will be logged in the ProcessInstruction pass.
54
0
    return;
55
0
  }
56
57
198
  _.RegisterExtension(extension);
58
198
}
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
1.45k
                               const spv_parsed_instruction_t* inst) {
67
1.45k
  const spv::Op opcode = static_cast<spv::Op>(inst->opcode);
68
1.45k
  if (opcode == spv::Op::OpCapability ||
69
536
      opcode == spv::Op::OpConditionalCapabilityINTEL)
70
918
    return SPV_SUCCESS;
71
72
536
  if (opcode == spv::Op::OpExtension ||
73
338
      opcode == spv::Op::OpConditionalExtensionINTEL) {
74
198
    ValidationState_t& _ = *(reinterpret_cast<ValidationState_t*>(user_data));
75
198
    RegisterExtension(_, inst);
76
198
    return SPV_SUCCESS;
77
198
  }
78
79
  // OpExtension block is finished, requesting termination.
80
338
  return SPV_REQUESTED_TERMINATION;
81
536
}
82
83
spv_result_t ProcessInstruction(void* user_data,
84
133k
                                const spv_parsed_instruction_t* inst) {
85
133k
  ValidationState_t& _ = *(reinterpret_cast<ValidationState_t*>(user_data));
86
87
133k
  auto* instruction = _.AddOrderedInstruction(inst);
88
133k
  _.RegisterDebugInstruction(instruction);
89
90
133k
  return SPV_SUCCESS;
91
133k
}
92
93
320
spv_result_t ValidateForwardDecls(ValidationState_t& _) {
94
320
  if (_.unresolved_forward_id_count() == 0) return SPV_SUCCESS;
95
96
0
  std::stringstream ss;
97
0
  std::vector<uint32_t> ids = _.UnresolvedForwardIds();
98
99
0
  std::transform(
100
0
      std::begin(ids), std::end(ids),
101
0
      std::ostream_iterator<std::string>(ss, " "),
102
0
      bind(&ValidationState_t::getIdName, std::ref(_), std::placeholders::_1));
103
104
0
  auto id_str = ss.str();
105
0
  return _.diag(SPV_ERROR_INVALID_ID, nullptr)
106
0
         << "The following forward referenced IDs have not been defined:\n"
107
0
         << id_str.substr(0, id_str.size() - 1);
108
320
}
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
284
spv_result_t ValidateEntryPoints(ValidationState_t& _) {
119
284
  _.ComputeFunctionToEntryPointMapping();
120
284
  _.ComputeRecursiveEntryPoints();
121
122
284
  if (_.entry_points().empty() && !_.HasCapability(spv::Capability::Linkage) &&
123
0
      !_.HasCapability(spv::Capability::GraphARM)) {
124
0
    return _.diag(SPV_ERROR_INVALID_BINARY, nullptr)
125
0
           << "No OpEntryPoint instruction was found. This is only allowed if "
126
0
              "the Linkage or GraphARM capability is being used.";
127
0
  }
128
129
284
  for (const auto& entry_point : _.entry_points()) {
130
284
    if (_.IsFunctionCallTarget(entry_point)) {
131
0
      return _.diag(SPV_ERROR_INVALID_BINARY, _.FindDef(entry_point))
132
0
             << "A function (" << entry_point
133
0
             << ") may not be targeted by both an OpEntryPoint instruction and "
134
0
                "an OpFunctionCall instruction.";
135
0
    }
136
137
    // For Vulkan, the static function-call graph for an entry point
138
    // must not contain cycles.
139
284
    if (spvIsVulkanEnv(_.context()->target_env)) {
140
284
      if (_.recursive_entry_points().find(entry_point) !=
141
284
          _.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
284
    }
147
284
  }
148
149
284
  if (auto error = ValidateFloatControls2(_)) {
150
0
    return error;
151
0
  }
152
284
  if (auto error = ValidateDuplicateExecutionModes(_)) {
153
0
    return error;
154
0
  }
155
156
284
  return SPV_SUCCESS;
157
284
}
158
159
284
spv_result_t ValidateGraphEntryPoints(ValidationState_t& _) {
160
284
  if (_.graph_entry_points().empty() &&
161
284
      _.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
284
  return SPV_SUCCESS;
167
284
}
168
169
spv_result_t ValidateBinaryUsingContextAndValidationState(
170
    const spv_context_t& context, const uint32_t* words, const size_t num_words,
171
338
    spv_diagnostic* pDiagnostic, ValidationState_t* vstate) {
172
338
  auto binary = std::unique_ptr<spv_const_binary_t>(
173
338
      new spv_const_binary_t{words, num_words});
174
175
338
  spv_endianness_t endian;
176
338
  spv_position_t position = {};
177
338
  if (spvBinaryEndianness(binary.get(), &endian)) {
178
0
    return DiagnosticStream(position, context.consumer, "",
179
0
                            SPV_ERROR_INVALID_BINARY)
180
0
           << "Invalid SPIR-V magic number.";
181
0
  }
182
183
338
  spv_header_t header;
184
338
  if (spvBinaryHeaderGet(binary.get(), endian, &header)) {
185
0
    return DiagnosticStream(position, context.consumer, "",
186
0
                            SPV_ERROR_INVALID_BINARY)
187
0
           << "Invalid SPIR-V header.";
188
0
  }
189
190
338
  if (header.version > spvVersionForTargetEnv(context.target_env)) {
191
0
    return DiagnosticStream(position, context.consumer, "",
192
0
                            SPV_ERROR_WRONG_VERSION)
193
0
           << "Invalid SPIR-V binary version "
194
0
           << SPV_SPIRV_VERSION_MAJOR_PART(header.version) << "."
195
0
           << SPV_SPIRV_VERSION_MINOR_PART(header.version)
196
0
           << " for target environment "
197
0
           << spvTargetEnvDescription(context.target_env) << ".";
198
0
  }
199
200
338
  if (header.bound > vstate->options()->universal_limits_.max_id_bound) {
201
0
    return DiagnosticStream(position, context.consumer, "",
202
0
                            SPV_ERROR_INVALID_BINARY)
203
0
           << "Invalid SPIR-V.  The id bound is larger than the max id bound "
204
0
           << vstate->options()->universal_limits_.max_id_bound << ".";
205
0
  }
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
338
  spv_context_t hijacked_context = context;
212
338
  hijacked_context.consumer = [](spv_message_level_t, const char*,
213
338
                                 const spv_position_t&, const char*) {};
214
338
  spvBinaryParse(&hijacked_context, vstate, words, num_words,
215
338
                 /* parsed_header = */ nullptr, ProcessExtensions,
216
338
                 /* 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
338
  if (auto error = spvBinaryParse(&context, vstate, words, num_words,
221
338
                                  /*parsed_header =*/nullptr,
222
338
                                  ProcessInstruction, pDiagnostic)) {
223
4
    return error;
224
4
  }
225
226
334
  bool has_mask_task_nv = false;
227
334
  bool has_mask_task_ext = false;
228
334
  std::vector<Instruction*> visited_entry_points;
229
334
  std::unordered_set<std::string> graph_entry_point_names;
230
128k
  for (auto& instruction : vstate->ordered_instructions()) {
231
128k
    {
232
      // In order to do this work outside of Process Instruction we need to be
233
      // able to, briefly, de-const the instruction.
234
128k
      Instruction* inst = const_cast<Instruction*>(&instruction);
235
236
128k
      if ((inst->opcode() == spv::Op::OpEntryPoint) ||
237
128k
          (inst->opcode() == spv::Op::OpConditionalEntryPointINTEL)) {
238
328
        const int i_model = inst->opcode() == spv::Op::OpEntryPoint ? 0 : 1;
239
328
        const int i_point = inst->opcode() == spv::Op::OpEntryPoint ? 1 : 2;
240
328
        const int i_name = inst->opcode() == spv::Op::OpEntryPoint ? 2 : 3;
241
328
        const int min_num_operands =
242
328
            inst->opcode() == spv::Op::OpEntryPoint ? 3 : 4;
243
244
328
        const auto entry_point = inst->GetOperandAs<uint32_t>(i_point);
245
328
        const auto execution_model =
246
328
            inst->GetOperandAs<spv::ExecutionModel>(i_model);
247
328
        const std::string desc_name = inst->GetOperandAs<std::string>(i_name);
248
249
328
        ValidationState_t::EntryPointDescription desc;
250
328
        desc.name = desc_name;
251
252
328
        std::vector<uint32_t> interfaces;
253
732
        for (size_t j = min_num_operands; j < inst->operands().size(); ++j)
254
404
          desc.interfaces.push_back(inst->word(inst->operand(j).offset));
255
256
328
        vstate->RegisterEntryPoint(entry_point, execution_model,
257
328
                                   std::move(desc));
258
259
328
        if (inst->opcode() == spv::Op::OpEntryPoint) {
260
          // conditional entry points are allowed to share the same name and
261
          // exec mode
262
328
          if (visited_entry_points.size() > 0) {
263
0
            for (const Instruction* check_inst : visited_entry_points) {
264
0
              const auto check_execution_model =
265
0
                  check_inst->GetOperandAs<spv::ExecutionModel>(i_model);
266
0
              const std::string check_name =
267
0
                  check_inst->GetOperandAs<std::string>(i_name);
268
269
0
              if (desc_name == check_name &&
270
0
                  execution_model == check_execution_model) {
271
0
                return vstate->diag(SPV_ERROR_INVALID_DATA, inst)
272
0
                       << "2 Entry points cannot share the same name and "
273
0
                          "ExecutionMode.";
274
0
              }
275
0
            }
276
0
          }
277
328
          visited_entry_points.push_back(inst);
278
328
        }
279
280
328
        has_mask_task_nv |= (execution_model == spv::ExecutionModel::TaskNV ||
281
328
                             execution_model == spv::ExecutionModel::MeshNV);
282
328
        has_mask_task_ext |= (execution_model == spv::ExecutionModel::TaskEXT ||
283
328
                              execution_model == spv::ExecutionModel::MeshEXT);
284
328
      }
285
128k
      if (inst->opcode() == spv::Op::OpGraphEntryPointARM) {
286
0
        const std::string name = inst->GetOperandAs<std::string>(1);
287
0
        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
0
        const auto graph = inst->GetOperandAs<uint32_t>(0);
292
0
        vstate->RegisterGraphEntryPoint(graph);
293
0
      }
294
128k
      if (inst->opcode() == spv::Op::OpFunctionCall) {
295
1.45k
        if (!vstate->in_function_body()) {
296
0
          return vstate->diag(SPV_ERROR_INVALID_LAYOUT, &instruction)
297
0
                 << "A FunctionCall must happen within a function body.";
298
0
        }
299
300
1.45k
        const auto called_id = inst->GetOperandAs<uint32_t>(2);
301
1.45k
        vstate->AddFunctionCallTarget(called_id);
302
1.45k
      }
303
304
128k
      if (vstate->in_function_body()) {
305
87.7k
        inst->set_function(&(vstate->current_function()));
306
87.7k
        inst->set_block(vstate->current_function().current_block());
307
308
87.7k
        if (vstate->in_block() && spvOpcodeIsBlockTerminator(inst->opcode())) {
309
7.73k
          vstate->current_function().current_block()->set_terminator(inst);
310
7.73k
        }
311
87.7k
      }
312
313
128k
      if (auto error = IdPass(*vstate, inst)) return error;
314
128k
    }
315
316
128k
    if (auto error = CapabilityPass(*vstate, &instruction)) return error;
317
128k
    if (auto error = ModuleLayoutPass(*vstate, &instruction)) return error;
318
128k
    if (auto error = CfgPass(*vstate, &instruction)) return error;
319
128k
    if (auto error = InstructionPass(*vstate, &instruction)) return error;
320
321
    // Now that all of the checks are done, update the state.
322
128k
    {
323
128k
      Instruction* inst = const_cast<Instruction*>(&instruction);
324
128k
      vstate->RegisterInstruction(inst);
325
128k
      if (inst->opcode() == spv::Op::OpTypeForwardPointer) {
326
28
        vstate->RegisterForwardPointer(inst->GetOperandAs<uint32_t>(0));
327
28
      }
328
128k
    }
329
128k
  }
330
331
320
  if (!vstate->has_memory_model_specified())
332
0
    return vstate->diag(SPV_ERROR_INVALID_LAYOUT, nullptr)
333
0
           << "Missing required OpMemoryModel instruction.";
334
335
320
  if (vstate->in_function_body())
336
0
    return vstate->diag(SPV_ERROR_INVALID_LAYOUT, nullptr)
337
0
           << "Missing OpFunctionEnd at end of module.";
338
339
320
  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
320
  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
320
  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
320
  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
320
  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
128k
  for (size_t i = 0; i < vstate->ordered_instructions().size(); ++i) {
370
128k
    auto& instruction = vstate->ordered_instructions()[i];
371
128k
    if (auto error = UpdateIdUse(*vstate, &instruction)) return error;
372
128k
  }
373
374
  // Validate individual opcodes.
375
124k
  for (size_t i = 0; i < vstate->ordered_instructions().size(); ++i) {
376
124k
    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
124k
    if (auto error = MiscPass(*vstate, &instruction)) return error;
381
124k
    if (auto error = DebugPass(*vstate, &instruction)) return error;
382
124k
    if (auto error = AnnotationPass(*vstate, &instruction)) return error;
383
124k
    if (auto error = ExtensionPass(*vstate, &instruction)) return error;
384
124k
    if (auto error = ModeSettingPass(*vstate, &instruction)) return error;
385
124k
    if (auto error = TypePass(*vstate, &instruction)) return error;
386
124k
    if (auto error = ConstantPass(*vstate, &instruction)) return error;
387
124k
    if (auto error = MemoryPass(*vstate, &instruction)) return error;
388
124k
    if (auto error = FunctionPass(*vstate, &instruction)) return error;
389
124k
    if (auto error = ImagePass(*vstate, &instruction)) return error;
390
124k
    if (auto error = ConversionPass(*vstate, &instruction)) return error;
391
124k
    if (auto error = CompositesPass(*vstate, &instruction)) return error;
392
124k
    if (auto error = ArithmeticsPass(*vstate, &instruction)) return error;
393
124k
    if (auto error = BitwisePass(*vstate, &instruction)) return error;
394
124k
    if (auto error = LogicalsPass(*vstate, &instruction)) return error;
395
124k
    if (auto error = ControlFlowPass(*vstate, &instruction)) return error;
396
124k
    if (auto error = DerivativesPass(*vstate, &instruction)) return error;
397
124k
    if (auto error = AtomicsPass(*vstate, &instruction)) return error;
398
124k
    if (auto error = PrimitivesPass(*vstate, &instruction)) return error;
399
124k
    if (auto error = BarriersPass(*vstate, &instruction)) return error;
400
124k
    if (auto error = DotProductPass(*vstate, &instruction)) return error;
401
124k
    if (auto error = GroupPass(*vstate, &instruction)) return error;
402
    // Device-Side Enqueue
403
124k
    if (auto error = PipePass(*vstate, &instruction)) return error;
404
124k
    if (auto error = NonUniformPass(*vstate, &instruction)) return error;
405
406
124k
    if (auto error = LiteralsPass(*vstate, &instruction)) return error;
407
124k
    if (auto error = RayQueryPass(*vstate, &instruction)) return error;
408
124k
    if (auto error = RayTracingPass(*vstate, &instruction)) return error;
409
124k
    if (auto error = RayReorderNVPass(*vstate, &instruction)) return error;
410
124k
    if (auto error = RayReorderEXTPass(*vstate, &instruction)) return error;
411
124k
    if (auto error = MeshShadingPass(*vstate, &instruction)) return error;
412
124k
    if (auto error = TensorLayoutPass(*vstate, &instruction)) return error;
413
124k
    if (auto error = TensorPass(*vstate, &instruction)) return error;
414
124k
    if (auto error = GraphPass(*vstate, &instruction)) return error;
415
124k
    if (auto error = InvalidTypePass(*vstate, &instruction)) return error;
416
124k
  }
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
284
  if (auto error = ValidateAdjacency(*vstate)) return error;
422
423
284
  if (auto error = ValidateEntryPoints(*vstate)) return error;
424
284
  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
284
  if (auto error = PerformCfgChecks(*vstate)) return error;
428
284
  if (auto error = CheckIdDefinitionDominateUse(*vstate)) return error;
429
284
  if (auto error = ValidateDecorations(*vstate)) return error;
430
284
  if (auto error = ValidateExplicitLayout(*vstate)) return error;
431
284
  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
284
  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
123k
  for (const auto& inst : vstate->ordered_instructions()) {
438
123k
    if (auto error = ValidateExecutionLimitations(*vstate, &inst)) return error;
439
123k
    if (auto error = ValidateSmallTypeUses(*vstate, &inst)) return error;
440
123k
    if (auto error = ValidateQCOMImageProcessingTextureUsages(*vstate, &inst))
441
0
      return error;
442
123k
  }
443
284
  if (auto error = ValidateLogicalPointers(*vstate)) return error;
444
445
284
  return SPV_SUCCESS;
446
284
}
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
0
                               spv_diagnostic* pDiagnostic) {
480
0
  spv_context_t hijack_context = *context;
481
0
  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
0
  spv_validator_options default_options = spvValidatorOptionsCreate();
488
489
  // Create the ValidationState using the context and default options.
490
0
  spvtools::val::ValidationState_t vstate(&hijack_context, default_options,
491
0
                                          words, num_words,
492
0
                                          kDefaultMaxNumOfWarnings);
493
494
0
  spv_result_t result =
495
0
      spvtools::val::ValidateBinaryUsingContextAndValidationState(
496
0
          hijack_context, words, num_words, pDiagnostic, &vstate);
497
498
0
  spvValidatorOptionsDestroy(default_options);
499
0
  return result;
500
0
}
501
502
spv_result_t spvValidateWithOptions(const spv_const_context context,
503
                                    spv_const_validator_options options,
504
                                    const spv_const_binary binary,
505
338
                                    spv_diagnostic* pDiagnostic) {
506
338
  spv_context_t hijack_context = *context;
507
338
  if (pDiagnostic) {
508
338
    *pDiagnostic = nullptr;
509
338
    spvtools::UseDiagnosticAsMessageConsumer(&hijack_context, pDiagnostic);
510
338
  }
511
512
  // Create the ValidationState using the context.
513
338
  spvtools::val::ValidationState_t vstate(&hijack_context, options,
514
338
                                          binary->code, binary->wordCount,
515
338
                                          kDefaultMaxNumOfWarnings);
516
517
338
  return spvtools::val::ValidateBinaryUsingContextAndValidationState(
518
338
      hijack_context, binary->code, binary->wordCount, pDiagnostic, &vstate);
519
338
}