validate_literals.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // Copyright (c) 2017 Google 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. // Validates literal numbers.
  15. #include "source/val/validate.h"
  16. #include <cassert>
  17. #include "source/diagnostic.h"
  18. #include "source/opcode.h"
  19. #include "source/val/instruction.h"
  20. #include "source/val/validation_state.h"
  21. namespace spvtools {
  22. namespace val {
  23. namespace {
  24. // Returns true if the operand holds a literal number
  25. bool IsLiteralNumber(const spv_parsed_operand_t& operand) {
  26. switch (operand.number_kind) {
  27. case SPV_NUMBER_SIGNED_INT:
  28. case SPV_NUMBER_UNSIGNED_INT:
  29. case SPV_NUMBER_FLOATING:
  30. return true;
  31. default:
  32. return false;
  33. }
  34. }
  35. // Verifies that the upper bits of the given upper |word| with given
  36. // lower |width| are zero- or sign-extended when |signed_int| is true
  37. bool VerifyUpperBits(uint32_t word, uint32_t width, bool signed_int) {
  38. assert(width < 32);
  39. assert(0 < width);
  40. const uint32_t upper_mask = 0xFFFFFFFFu << width;
  41. const uint32_t upper_bits = word & upper_mask;
  42. bool result = false;
  43. if (signed_int) {
  44. const uint32_t sign_bit = word & (1u << (width - 1));
  45. if (sign_bit) {
  46. result = upper_bits == upper_mask;
  47. } else {
  48. result = upper_bits == 0;
  49. }
  50. } else {
  51. result = upper_bits == 0;
  52. }
  53. return result;
  54. }
  55. } // namespace
  56. // Validates that literal numbers are represented according to the spec
  57. spv_result_t LiteralsPass(ValidationState_t& _, const Instruction* inst) {
  58. // For every operand that is a literal number
  59. for (size_t i = 0; i < inst->operands().size(); i++) {
  60. const spv_parsed_operand_t& operand = inst->operand(i);
  61. if (!IsLiteralNumber(operand)) continue;
  62. // The upper bits are always in the last word (little-endian)
  63. int last_index = operand.offset + operand.num_words - 1;
  64. const uint32_t upper_word = inst->word(last_index);
  65. // TODO(jcaraban): is the |word size| defined in some header?
  66. const uint32_t word_size = 32;
  67. uint32_t bit_width = operand.number_bit_width;
  68. // Bit widths that are a multiple of the word size have no upper bits
  69. const auto remaining_value_bits = bit_width % word_size;
  70. if (remaining_value_bits == 0) continue;
  71. const bool signedness = operand.number_kind == SPV_NUMBER_SIGNED_INT;
  72. if (!VerifyUpperBits(upper_word, remaining_value_bits, signedness)) {
  73. return _.diag(SPV_ERROR_INVALID_VALUE, inst)
  74. << "The high-order bits of a literal number in instruction <id> "
  75. << inst->id() << " must be 0 for a floating-point type, "
  76. << "or 0 for an integer type with Signedness of 0, "
  77. << "or sign extended when Signedness is 1";
  78. }
  79. }
  80. return SPV_SUCCESS;
  81. }
  82. } // namespace val
  83. } // namespace spvtools