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