validate_literals.cpp 3.1 KB

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