Coverage Report

Created: 2025-09-15 18:08

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