aboutsummaryrefslogtreecommitdiff
path: root/test/val/val_limits_test.cpp
blob: 8fb80a46afefd5a893481690aaf03db481ac7ff7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
// Copyright (c) 2016 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Validation tests for Universal Limits. (Section 2.17 of the SPIR-V Spec)

#include <sstream>
#include <string>
#include <utility>

#include "gmock/gmock.h"
#include "test/unit_spirv.h"
#include "test/val/val_fixtures.h"

namespace spvtools {
namespace val {
namespace {

using ::testing::HasSubstr;
using ::testing::MatchesRegex;

using ValidateLimits = spvtest::ValidateBase<bool>;

std::string header = R"(
     OpCapability Shader
     OpCapability Linkage
     OpMemoryModel Logical GLSL450
)";

TEST_F(ValidateLimits, IdLargerThanBoundBad) {
  std::string str = header + R"(
;  %i32 has ID 1
%i32    = OpTypeInt 32 1
%c      = OpConstant %i32 100

; Fake an instruction with 64 as the result id.
; !64 = OpConstantNull %i32
!0x3002e !1 !64
)";

  CompileSuccessfully(str);
  ASSERT_EQ(SPV_ERROR_INVALID_BINARY, ValidateInstructions());
  EXPECT_THAT(
      getDiagnosticString(),
      HasSubstr("Result <id> '64' must be less than the ID bound '3'."));
}

TEST_F(ValidateLimits, IdEqualToBoundBad) {
  std::string str = header + R"(
;  %i32 has ID 1
%i32    = OpTypeInt 32 1
%c      = OpConstant %i32 100

; Fake an instruction with 64 as the result id.
; !64 = OpConstantNull %i32
!0x3002e !1 !64
)";

  CompileSuccessfully(str);

  // The largest ID used in this program is 64. Let's overwrite the ID bound in
  // the header to be 64. This should result in an error because all IDs must
  // satisfy: 0 < id < bound.
  OverwriteAssembledBinary(3, 64);

  ASSERT_EQ(SPV_ERROR_INVALID_BINARY, ValidateInstructions());
  EXPECT_THAT(
      getDiagnosticString(),
      HasSubstr("Result <id> '64' must be less than the ID bound '64'."));
}

TEST_F(ValidateLimits, IdBoundTooBigDeaultLimit) {
  std::string str = header;

  CompileSuccessfully(str);

  // The largest ID used in this program is 64. Let's overwrite the ID bound in
  // the header to be 64. This should result in an error because all IDs must
  // satisfy: 0 < id < bound.
  OverwriteAssembledBinary(3, 0x4FFFFF);

  ASSERT_EQ(SPV_ERROR_INVALID_BINARY, ValidateInstructions());
  EXPECT_THAT(getDiagnosticString(),
              HasSubstr("Invalid SPIR-V.  The id bound is larger than the max "
                        "id bound 4194303."));
}

TEST_F(ValidateLimits, IdBoundAtSetLimit) {
  std::string str = header;

  CompileSuccessfully(str);

  // The largest ID used in this program is 64. Let's overwrite the ID bound in
  // the header to be 64. This should result in an error because all IDs must
  // satisfy: 0 < id < bound.
  uint32_t id_bound = 0x4FFFFF;

  OverwriteAssembledBinary(3, id_bound);
  getValidatorOptions()->universal_limits_.max_id_bound = id_bound;

  ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
}

TEST_F(ValidateLimits, IdBoundJustAboveSetLimit) {
  std::string str = header;

  CompileSuccessfully(str);

  // The largest ID used in this program is 64. Let's overwrite the ID bound in
  // the header to be 64. This should result in an error because all IDs must
  // satisfy: 0 < id < bound.
  uint32_t id_bound = 5242878;

  OverwriteAssembledBinary(3, id_bound);
  getValidatorOptions()->universal_limits_.max_id_bound = id_bound - 1;

  ASSERT_EQ(SPV_ERROR_INVALID_BINARY, ValidateInstructions());
  EXPECT_THAT(getDiagnosticString(),
              HasSubstr("Invalid SPIR-V.  The id bound is larger than the max "
                        "id bound 5242877."));
}

TEST_F(ValidateLimits, IdBoundAtInMaxLimit) {
  std::string str = header;

  CompileSuccessfully(str);

  uint32_t id_bound = std::numeric_limits<uint32_t>::max();

  OverwriteAssembledBinary(3, id_bound);
  getValidatorOptions()->universal_limits_.max_id_bound = id_bound;

  ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
}

TEST_F(ValidateLimits, StructNumMembersGood) {
  std::ostringstream spirv;
  spirv << header << R"(
%1 = OpTypeInt 32 0
%2 = OpTypeStruct)";
  for (int i = 0; i < 16383; ++i) {
    spirv << " %1";
  }
  CompileSuccessfully(spirv.str());
  ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
}

TEST_F(ValidateLimits, StructNumMembersExceededBad) {
  std::ostringstream spirv;
  spirv << header << R"(
%1 = OpTypeInt 32 0
%2 = OpTypeStruct)";
  for (int i = 0; i < 16384; ++i) {
    spirv << " %1";
  }
  CompileSuccessfully(spirv.str());
  ASSERT_EQ(SPV_ERROR_INVALID_BINARY, ValidateInstructions());
  EXPECT_THAT(getDiagnosticString(),
              HasSubstr("Number of OpTypeStruct members (16384) has exceeded "
                        "the limit (16383)."));
}

TEST_F(ValidateLimits, CustomizedStructNumMembersGood) {
  std::ostringstream spirv;
  spirv << header << R"(
%1 = OpTypeInt 32 0
%2 = OpTypeStruct)";
  for (int i = 0; i < 32000; ++i) {
    spirv << " %1";
  }
  spvValidatorOptionsSetUniversalLimit(
      options_, spv_validator_limit_max_struct_members, 32000u);
  CompileSuccessfully(spirv.str());
  ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
}

TEST_F(ValidateLimits, CustomizedStructNumMembersBad) {
  std::ostringstream spirv;
  spirv << header << R"(
%1 = OpTypeInt 32 0
%2 = OpTypeStruct)";
  for (int i = 0; i < 32001; ++i) {
    spirv << " %1";
  }
  spvValidatorOptionsSetUniversalLimit(
      options_, spv_validator_limit_max_struct_members, 32000u);
  CompileSuccessfully(spirv.str());
  ASSERT_EQ(SPV_ERROR_INVALID_BINARY, ValidateInstructions());
  EXPECT_THAT(getDiagnosticString(),
              HasSubstr("Number of OpTypeStruct members (32001) has exceeded "
                        "the limit (32000)."));
}

// Valid: Switch statement has 16,383 branches.
TEST_F(ValidateLimits, SwitchNumBranchesGood) {
  std::ostringstream spirv;
  spirv << header << R"(
%1 = OpTypeVoid
%2 = OpTypeFunction %1
%3 = OpTypeInt 32 0
%4 = OpConstant %3 1234
%5 = OpFunction %1 None %2
%7 = OpLabel
%8 = OpIAdd %3 %4 %4
     OpSelectionMerge %10 None
     OpSwitch %4 %10)";

  // Now add the (literal, label) pairs
  for (int i = 0; i < 16383; ++i) {
    spirv << " 1 %10";
  }

  spirv << R"(
%10 = OpLabel
OpReturn
OpFunctionEnd
  )";

  CompileSuccessfully(spirv.str());
  ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
}

// Invalid: Switch statement has 16,384 branches.
TEST_F(ValidateLimits, SwitchNumBranchesBad) {
  std::ostringstream spirv;
  spirv << header << R"(
%1 = OpTypeVoid
%2 = OpTypeFunction %1
%3 = OpTypeInt 32 0
%4 = OpConstant %3 1234
%5 = OpFunction %1 None %2
%7 = OpLabel
%8 = OpIAdd %3 %4 %4
     OpSelectionMerge %10 None
     OpSwitch %4 %10)";

  // Now add the (literal, label) pairs
  for (int i = 0; i < 16384; ++i) {
    spirv << " 1 %10";
  }

  spirv << R"(
%10 = OpLabel
OpReturn
OpFunctionEnd
  )";

  CompileSuccessfully(spirv.str());
  ASSERT_EQ(SPV_ERROR_INVALID_BINARY, ValidateInstructions());
  EXPECT_THAT(getDiagnosticString(),
              HasSubstr("Number of (literal, label) pairs in OpSwitch (16384) "
                        "exceeds the limit (16383)."));
}

// Valid: Switch statement has 10 branches (limit is 10)
TEST_F(ValidateLimits, CustomizedSwitchNumBranchesGood) {
  std::ostringstream spirv;
  spirv << header << R"(
%1 = OpTypeVoid
%2 = OpTypeFunction %1
%3 = OpTypeInt 32 0
%4 = OpConstant %3 1234
%5 = OpFunction %1 None %2
%7 = OpLabel
%8 = OpIAdd %3 %4 %4
     OpSelectionMerge %10 None
     OpSwitch %4 %10)";

  // Now add the (literal, label) pairs
  for (int i = 0; i < 10; ++i) {
    spirv << " 1 %10";
  }

  spirv << R"(
%10 = OpLabel
OpReturn
OpFunctionEnd
  )";

  spvValidatorOptionsSetUniversalLimit(
      options_, spv_validator_limit_max_switch_branches, 10u);
  CompileSuccessfully(spirv.str());
  ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
}

// Invalid: Switch statement has 11 branches (limit is 10)
TEST_F(ValidateLimits, CustomizedSwitchNumBranchesBad) {
  std::ostringstream spirv;
  spirv << header << R"(
%1 = OpTypeVoid
%2 = OpTypeFunction %1
%3 = OpTypeInt 32 0
%4 = OpConstant %3 1234
%5 = OpFunction %1 None %2
%7 = OpLabel
%8 = OpIAdd %3 %4 %4
     OpSelectionMerge %10 None
     OpSwitch %4 %10)";

  // Now add the (literal, label) pairs
  for (int i = 0; i < 11; ++i) {
    spirv << " 1 %10";
  }

  spirv << R"(
%10 = OpLabel
OpReturn
OpFunctionEnd
  )";

  spvValidatorOptionsSetUniversalLimit(
      options_, spv_validator_limit_max_switch_branches, 10u);
  CompileSuccessfully(spirv.str());
  ASSERT_EQ(SPV_ERROR_INVALID_BINARY, ValidateInstructions());
  EXPECT_THAT(getDiagnosticString(),
              HasSubstr("Number of (literal, label) pairs in OpSwitch (11) "
                        "exceeds the limit (10)."));
}

// Valid: OpTypeFunction with 255 arguments.
TEST_F(ValidateLimits, OpTypeFunctionGood) {
  int num_args = 255;
  std::ostringstream spirv;
  spirv << header << R"(
%1 = OpTypeInt 32 0
%2 = OpTypeFunction %1)";
  // add parameters
  for (int i = 0; i < num_args; ++i) {
    spirv << " %1";
  }
  CompileSuccessfully(spirv.str());
  EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
}

// Invalid: OpTypeFunction with 256 arguments. (limit is 255 according to the
// spec Universal Limits (2.17).
TEST_F(ValidateLimits, OpTypeFunctionBad) {
  int num_args = 256;
  std::ostringstream spirv;
  spirv << header << R"(
%1 = OpTypeInt 32 0
%2 = OpTypeFunction %1)";
  for (int i = 0; i < num_args; ++i) {
    spirv << " %1";
  }
  CompileSuccessfully(spirv.str());
  EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
  EXPECT_THAT(getDiagnosticString(),
              HasSubstr("OpTypeFunction may not take more than 255 arguments. "
                        "OpTypeFunction <id> '2[%2]' has 256 arguments."));
}

// Valid: OpTypeFunction with 100 arguments (Custom limit: 100)
TEST_F(ValidateLimits, CustomizedOpTypeFunctionGood) {
  int num_args = 100;
  std::ostringstream spirv;
  spirv << header << R"(
%1 = OpTypeInt 32 0
%2 = OpTypeFunction %1)";
  // add parameters
  for (int i = 0; i < num_args; ++i) {
    spirv << " %1";
  }
  spvValidatorOptionsSetUniversalLimit(
      options_, spv_validator_limit_max_function_args, 100u);
  CompileSuccessfully(spirv.str());
  EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
}

// Invalid: OpTypeFunction with 101 arguments. (Custom limit: 100)
TEST_F(ValidateLimits, CustomizedOpTypeFunctionBad) {
  int num_args = 101;
  std::ostringstream spirv;
  spirv << header << R"(
%1 = OpTypeInt 32 0
%2 = OpTypeFunction %1)";
  for (int i = 0; i < num_args; ++i) {
    spirv << " %1";
  }
  spvValidatorOptionsSetUniversalLimit(
      options_, spv_validator_limit_max_function_args, 100u);
  CompileSuccessfully(spirv.str());
  EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
  EXPECT_THAT(getDiagnosticString(),
              HasSubstr("OpTypeFunction may not take more than 100 arguments. "
                        "OpTypeFunction <id> '2[%2]' has 101 arguments."));
}

// Valid: module has 65,535 global variables.
TEST_F(ValidateLimits, NumGlobalVarsGood) {
  int num_globals = 65535;
  std::ostringstream spirv;
  spirv << header << R"(
     %int = OpTypeInt 32 0
%_ptr_int = OpTypePointer Input %int
  )";

  for (int i = 0; i < num_globals; ++i) {
    spirv << "%var_" << i << " = OpVariable %_ptr_int Input\n";
  }

  CompileSuccessfully(spirv.str());
  EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
}

// Invalid: module has 65,536 global variables (limit is 65,535).
TEST_F(ValidateLimits, NumGlobalVarsBad) {
  int num_globals = 65536;
  std::ostringstream spirv;
  spirv << header << R"(
     %int = OpTypeInt 32 0
%_ptr_int = OpTypePointer Input %int
  )";

  for (int i = 0; i < num_globals; ++i) {
    spirv << "%var_" << i << " = OpVariable %_ptr_int Input\n";
  }

  CompileSuccessfully(spirv.str());
  EXPECT_EQ(SPV_ERROR_INVALID_BINARY, ValidateInstructions());
  EXPECT_THAT(getDiagnosticString(),
              HasSubstr("Number of Global Variables (Storage Class other than "
                        "'Function') exceeded the valid limit (65535)."));
}

// Valid: module has 50 global variables (limit is 50)
TEST_F(ValidateLimits, CustomizedNumGlobalVarsGood) {
  int num_globals = 50;
  std::ostringstream spirv;
  spirv << header << R"(
     %int = OpTypeInt 32 0
%_ptr_int = OpTypePointer Input %int
  )";

  for (int i = 0; i < num_globals; ++i) {
    spirv << "%var_" << i << " = OpVariable %_ptr_int Input\n";
  }

  spvValidatorOptionsSetUniversalLimit(
      options_, spv_validator_limit_max_global_variables, 50u);
  CompileSuccessfully(spirv.str());
  EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
}

// Invalid: module has 51 global variables (limit is 50).
TEST_F(ValidateLimits, CustomizedNumGlobalVarsBad) {
  int num_globals = 51;
  std::ostringstream spirv;
  spirv << header << R"(
     %int = OpTypeInt 32 0
%_ptr_int = OpTypePointer Input %int
  )";

  for (int i = 0; i < num_globals; ++i) {
    spirv << "%var_" << i << " = OpVariable %_ptr_int Input\n";
  }

  spvValidatorOptionsSetUniversalLimit(
      options_, spv_validator_limit_max_global_variables, 50u);
  CompileSuccessfully(spirv.str());
  EXPECT_EQ(SPV_ERROR_INVALID_BINARY, ValidateInstructions());
  EXPECT_THAT(getDiagnosticString(),
              HasSubstr("Number of Global Variables (Storage Class other than "
                        "'Function') exceeded the valid limit (50)."));
}

// Valid: module has 524,287 local variables.
// Note: AppVeyor limits process time to 300s.  For a VisualStudio Debug
// build, going up to 524287 local variables gets too close to that
// limit.  So test with an artificially lowered limit.
TEST_F(ValidateLimits, NumLocalVarsGoodArtificiallyLowLimit5K) {
  int num_locals = 5000;
  std::ostringstream spirv;
  spirv << header << R"(
 %int      = OpTypeInt 32 0
 %_ptr_int = OpTypePointer Function %int
 %voidt    = OpTypeVoid
 %funct    = OpTypeFunction %voidt
 %main     = OpFunction %voidt None %funct
 %entry    = OpLabel
  )";

  for (int i = 0; i < num_locals; ++i) {
    spirv << "%var_" << i << " = OpVariable %_ptr_int Function\n";
  }

  spirv << R"(
    OpReturn
    OpFunctionEnd
  )";

  CompileSuccessfully(spirv.str());
  // Artificially limit it.
  spvValidatorOptionsSetUniversalLimit(
      options_, spv_validator_limit_max_local_variables, num_locals);
  EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
}

// Invalid: module has 524,288 local variables (limit is 524,287).
// Artificially limit the check to 5001.
TEST_F(ValidateLimits, NumLocalVarsBadArtificiallyLowLimit5K) {
  int num_locals = 5001;
  std::ostringstream spirv;
  spirv << header << R"(
 %int      = OpTypeInt 32 0
 %_ptr_int = OpTypePointer Function %int
 %voidt    = OpTypeVoid
 %funct    = OpTypeFunction %voidt
 %main     = OpFunction %voidt None %funct
 %entry    = OpLabel
  )";

  for (int i = 0; i < num_locals; ++i) {
    spirv << "%var_" << i << " = OpVariable %_ptr_int Function\n";
  }

  spirv << R"(
    OpReturn
    OpFunctionEnd
  )";

  CompileSuccessfully(spirv.str());
  spvValidatorOptionsSetUniversalLimit(
      options_, spv_validator_limit_max_local_variables, 5000u);
  EXPECT_EQ(SPV_ERROR_INVALID_BINARY, ValidateInstructions());
  EXPECT_THAT(getDiagnosticString(),
              HasSubstr("Number of local variables ('Function' Storage Class) "
                        "exceeded the valid limit (5000)."));
}

// Valid: module has 100 local variables (limit is 100).
TEST_F(ValidateLimits, CustomizedNumLocalVarsGood) {
  int num_locals = 100;
  std::ostringstream spirv;
  spirv << header << R"(
 %int      = OpTypeInt 32 0
 %_ptr_int = OpTypePointer Function %int
 %voidt    = OpTypeVoid
 %funct    = OpTypeFunction %voidt
 %main     = OpFunction %voidt None %funct
 %entry    = OpLabel
  )";

  for (int i = 0; i < num_locals; ++i) {
    spirv << "%var_" << i << " = OpVariable %_ptr_int Function\n";
  }

  spirv << R"(
    OpReturn
    OpFunctionEnd
  )";

  spvValidatorOptionsSetUniversalLimit(
      options_, spv_validator_limit_max_local_variables, 100u);
  CompileSuccessfully(spirv.str());
  EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
}

// Invalid: module has 101 local variables (limit is 100).
TEST_F(ValidateLimits, CustomizedNumLocalVarsBad) {
  int num_locals = 101;
  std::ostringstream spirv;
  spirv << header << R"(
 %int      = OpTypeInt 32 0
 %_ptr_int = OpTypePointer Function %int
 %voidt    = OpTypeVoid
 %funct    = OpTypeFunction %voidt
 %main     = OpFunction %voidt None %funct
 %entry    = OpLabel
  )";

  for (int i = 0; i < num_locals; ++i) {
    spirv << "%var_" << i << " = OpVariable %_ptr_int Function\n";
  }

  spirv << R"(
    OpReturn
    OpFunctionEnd
  )";

  spvValidatorOptionsSetUniversalLimit(
      options_, spv_validator_limit_max_local_variables, 100u);
  CompileSuccessfully(spirv.str());
  EXPECT_EQ(SPV_ERROR_INVALID_BINARY, ValidateInstructions());
  EXPECT_THAT(getDiagnosticString(),
              HasSubstr("Number of local variables ('Function' Storage Class) "
                        "exceeded the valid limit (100)."));
}

// Valid: Structure nesting depth of 255.
TEST_F(ValidateLimits, StructNestingDepthGood) {
  std::ostringstream spirv;
  spirv << header << R"(
    %int = OpTypeInt 32 0
    %s_depth_1  = OpTypeStruct %int
  )";
  for (auto i = 2; i <= 255; ++i) {
    spirv << "%s_depth_" << i << " = OpTypeStruct %int %s_depth_" << i - 1;
    spirv << "\n";
  }
  CompileSuccessfully(spirv.str());
  EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
}

// Invalid: Structure nesting depth of 256.
TEST_F(ValidateLimits, StructNestingDepthBad) {
  std::ostringstream spirv;
  spirv << header << R"(
    %int = OpTypeInt 32 0
    %s_depth_1  = OpTypeStruct %int
  )";
  for (auto i = 2; i <= 256; ++i) {
    spirv << "%s_depth_" << i << " = OpTypeStruct %int %s_depth_" << i - 1;
    spirv << "\n";
  }
  CompileSuccessfully(spirv.str());
  EXPECT_EQ(SPV_ERROR_INVALID_BINARY, ValidateInstructions());
  EXPECT_THAT(
      getDiagnosticString(),
      HasSubstr(
          "Structure Nesting Depth may not be larger than 255. Found 256."));
}

// Valid: Structure nesting depth of 100 (limit is 100).
TEST_F(ValidateLimits, CustomizedStructNestingDepthGood) {
  std::ostringstream spirv;
  spirv << header << R"(
    %int = OpTypeInt 32 0
    %s_depth_1  = OpTypeStruct %int
  )";
  for (auto i = 2; i <= 100; ++i) {
    spirv << "%s_depth_" << i << " = OpTypeStruct %int %s_depth_" << i - 1;
    spirv << "\n";
  }
  spvValidatorOptionsSetUniversalLimit(
      options_, spv_validator_limit_max_struct_depth, 100u);
  CompileSuccessfully(spirv.str());
  EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
}

// Invalid: Structure nesting depth of 101 (limit is 100).
TEST_F(ValidateLimits, CustomizedStructNestingDepthBad) {
  std::ostringstream spirv;
  spirv << header << R"(
    %int = OpTypeInt 32 0
    %s_depth_1  = OpTypeStruct %int
  )";
  for (auto i = 2; i <= 101; ++i) {
    spirv << "%s_depth_" << i << " = OpTypeStruct %int %s_depth_" << i - 1;
    spirv << "\n";
  }
  spvValidatorOptionsSetUniversalLimit(
      options_, spv_validator_limit_max_struct_depth, 100u);
  CompileSuccessfully(spirv.str());
  EXPECT_EQ(SPV_ERROR_INVALID_BINARY, ValidateInstructions());
  EXPECT_THAT(
      getDiagnosticString(),
      HasSubstr(
          "Structure Nesting Depth may not be larger than 100. Found 101."));
}

// clang-format off
// Generates an SPIRV program with the given control flow nesting depth
void GenerateSpirvProgramWithCfgNestingDepth(std::string& str, int depth) {
  std::ostringstream spirv;
  spirv << header << R"(
       %void = OpTypeVoid
          %3 = OpTypeFunction %void
       %bool = OpTypeBool
         %12 = OpConstantTrue %bool
       %main = OpFunction %void None %3
          %5 = OpLabel
               OpBranch %6
          %6 = OpLabel
               OpLoopMerge %8 %9 None
               OpBranch %10
         %10 = OpLabel
               OpBranchConditional %12 %7 %8
          %7 = OpLabel
  )";
  int first_id = 13;
  int last_id = 14;
  // We already have 1 level of nesting due to the Loop.
  int num_if_conditions = depth-1;
  int largest_index = first_id + 2*num_if_conditions - 2;
  for (int i = first_id; i <= largest_index; i = i + 2) {
    spirv << "OpSelectionMerge %" << i+1 << " None" << "\n";
    spirv << "OpBranchConditional %12 " << "%" << i << " %" << i+1 << "\n";
    spirv << "%" << i << " = OpLabel" << "\n";
  }
  spirv << "OpBranch %9" << "\n";

  for (int i = largest_index+1; i > last_id; i = i - 2) {
    spirv << "%" << i << " = OpLabel" << "\n";
    spirv << "OpBranch %" << i-2 << "\n";
  }
  spirv << "%" << last_id << " = OpLabel" << "\n";
  spirv << "OpBranch %9" << "\n";
  spirv << R"(
    %9 = OpLabel
    OpBranch %6
    %8 = OpLabel
    OpReturn
    OpFunctionEnd
  )";
  str = spirv.str();
}
// clang-format on

// Invalid: Control Flow Nesting depth is 1024. (limit is 1023).
TEST_F(ValidateLimits, ControlFlowDepthBad) {
  std::string spirv;
  GenerateSpirvProgramWithCfgNestingDepth(spirv, 1024);
  CompileSuccessfully(spirv);
  EXPECT_EQ(SPV_ERROR_INVALID_CFG, ValidateInstructions());
  EXPECT_THAT(getDiagnosticString(),
              HasSubstr("Maximum Control Flow nesting depth exceeded."));
}

// Valid: Control Flow Nesting depth is 10 (custom limit: 10).
TEST_F(ValidateLimits, CustomizedControlFlowDepthGood) {
  std::string spirv;
  GenerateSpirvProgramWithCfgNestingDepth(spirv, 10);
  spvValidatorOptionsSetUniversalLimit(
      options_, spv_validator_limit_max_control_flow_nesting_depth, 10u);
  CompileSuccessfully(spirv);
  EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
}

// Invalid: Control Flow Nesting depth is 11. (custom limit: 10).
TEST_F(ValidateLimits, CustomizedControlFlowDepthBad) {
  std::string spirv;
  GenerateSpirvProgramWithCfgNestingDepth(spirv, 11);
  spvValidatorOptionsSetUniversalLimit(
      options_, spv_validator_limit_max_control_flow_nesting_depth, 10u);
  CompileSuccessfully(spirv);
  EXPECT_EQ(SPV_ERROR_INVALID_CFG, ValidateInstructions());
  EXPECT_THAT(getDiagnosticString(),
              HasSubstr("Maximum Control Flow nesting depth exceeded."));
}

// Valid. The purpose here is to test the CFG depth calculation code when a loop
// continue target is the loop iteself. It also exercises the case where a loop
// is unreachable.
TEST_F(ValidateLimits, ControlFlowNoEntryToLoopGood) {
  std::string str = header + R"(
           OpName %entry "entry"
           OpName %loop "loop"
           OpName %exit "exit"
%voidt   = OpTypeVoid
%boolt   = OpTypeBool
%undef   = OpUndef %boolt
%funct   = OpTypeFunction %voidt
%main    = OpFunction %voidt None %funct
%entry   = OpLabel
           OpBranch %exit
%loop    = OpLabel
           OpLoopMerge %dead %loop None
           OpBranchConditional %undef %loop %loop
%dead    = OpLabel
           OpUnreachable
%exit    = OpLabel
           OpReturn
           OpFunctionEnd
  )";
  CompileSuccessfully(str);
  EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
}

}  // namespace
}  // namespace val
}  // namespace spvtools