| 1 |
// Copyright (c) 2023 Google LLC. |
| 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 "extract_source.h" |
| 16 |
|
| 17 |
#include <cassert> |
| 18 |
#include <string> |
| 19 |
#include <unordered_map> |
| 20 |
#include <vector> |
| 21 |
|
| 22 |
#include "source/latest_version_spirv_header.h" |
| 23 |
#include "source/opt/log.h" |
| 24 |
#include "spirv-tools/libspirv.hpp" |
| 25 |
#include "tools/util/cli_consumer.h" |
| 26 |
|
| 27 |
namespace { |
| 28 |
|
| 29 |
constexpr auto kDefaultEnvironment = SPV_ENV_UNIVERSAL_1_6; |
| 30 |
|
| 31 |
// Extract a string literal from a given range. |
| 32 |
// Copies all the characters from `begin` to the first '\0' it encounters, while |
| 33 |
// removing escape patterns. |
| 34 |
// Not finding a '\0' before reaching `end` fails the extraction. |
| 35 |
// |
| 36 |
// Returns `true` if the extraction succeeded. |
| 37 |
// `output` value is undefined if false is returned. |
| 38 |
spv_result_t ExtractStringLiteral(const spv_position_t& loc, const char* begin, |
| 39 |
const char* end, std::string* output) { |
| 40 |
size_t sourceLength = std::distance(begin, end); |
| 41 |
std::string escapedString; |
| 42 |
escapedString.resize(sourceLength); |
| 43 |
|
| 44 |
size_t writeIndex = 0; |
| 45 |
size_t readIndex = 0; |
| 46 |
for (; readIndex < sourceLength; writeIndex++, readIndex++) { |
| 47 |
const char read = begin[readIndex]; |
| 48 |
if (read == '\0') { |
| 49 |
escapedString.resize(writeIndex); |
| 50 |
output->append(escapedString); |
| 51 |
return SPV_SUCCESS; |
| 52 |
} |
| 53 |
|
| 54 |
if (read == '\\') { |
| 55 |
++readIndex; |
| 56 |
} |
| 57 |
escapedString[writeIndex] = begin[readIndex]; |
| 58 |
} |
| 59 |
|
| 60 |
spvtools::Error(spvtools::utils::CLIMessageConsumer, "", loc, |
| 61 |
"Missing NULL terminator for literal string."); |
| 62 |
return SPV_ERROR_INVALID_BINARY; |
| 63 |
} |
| 64 |
|
| 65 |
spv_result_t extractOpString(const spv_position_t& loc, |
| 66 |
const spv_parsed_instruction_t& instruction, |
| 67 |
std::string* output) { |
| 68 |
assert(output != nullptr); |
| 69 |
assert(instruction.opcode == static_cast<unsigned>(spv::Op::OpString)); |
| 70 |
if (instruction.num_operands != 2) { |
| 71 |
spvtools::Error(spvtools::utils::CLIMessageConsumer, "", loc, |
| 72 |
"Missing operands for OpString."); |
| 73 |
return SPV_ERROR_INVALID_BINARY; |
| 74 |
} |
| 75 |
|
| 76 |
const auto& operand = instruction.operands[1]; |
| 77 |
const char* stringBegin = |
| 78 |
reinterpret_cast<const char*>(instruction.words + operand.offset); |
| 79 |
const char* stringEnd = reinterpret_cast<const char*>( |
| 80 |
instruction.words + operand.offset + operand.num_words); |
| 81 |
return ExtractStringLiteral(loc, stringBegin, stringEnd, output); |
| 82 |
} |
| 83 |
|
| 84 |
spv_result_t extractOpSourceContinued( |
| 85 |
const spv_position_t& loc, const spv_parsed_instruction_t& instruction, |
| 86 |
std::string* output) { |
| 87 |
assert(output != nullptr); |
| 88 |
assert(instruction.opcode == |
| 89 |
static_cast<unsigned>(spv::Op::OpSourceContinued)); |
| 90 |
if (instruction.num_operands != 1) { |
| 91 |
spvtools::Error(spvtools::utils::CLIMessageConsumer, "", loc, |
| 92 |
"Missing operands for OpSourceContinued."); |
| 93 |
return SPV_ERROR_INVALID_BINARY; |
| 94 |
} |
| 95 |
|
| 96 |
const auto& operand = instruction.operands[0]; |
| 97 |
const char* stringBegin = |
| 98 |
reinterpret_cast<const char*>(instruction.words + operand.offset); |
| 99 |
const char* stringEnd = reinterpret_cast<const char*>( |
| 100 |
instruction.words + operand.offset + operand.num_words); |
| 101 |
return ExtractStringLiteral(loc, stringBegin, stringEnd, output); |
| 102 |
} |
| 103 |
|
| 104 |
spv_result_t extractOpSource(const spv_position_t& loc, |
| 105 |
const spv_parsed_instruction_t& instruction, |
| 106 |
spv::Id* filename, std::string* code) { |
| 107 |
assert(filename != nullptr && code != nullptr); |
| 108 |
assert(instruction.opcode == static_cast<unsigned>(spv::Op::OpSource)); |
| 109 |
// OpCode [ Source Language | Version | File (optional) | Source (optional) ] |
| 110 |
if (instruction.num_words < 3) { |
| 111 |
spvtools::Error(spvtools::utils::CLIMessageConsumer, "", loc, |
| 112 |
"Missing operands for OpSource."); |
| 113 |
return SPV_ERROR_INVALID_BINARY; |
| 114 |
} |
| 115 |
|
| 116 |
*filename = 0; |
| 117 |
*code = ""; |
| 118 |
if (instruction.num_words < 4) { |
| 119 |
return SPV_SUCCESS; |
| 120 |
} |
| 121 |
*filename = instruction.words[3]; |
| 122 |
|
| 123 |
if (instruction.num_words < 5) { |
| 124 |
return SPV_SUCCESS; |
| 125 |
} |
| 126 |
|
| 127 |
const char* stringBegin = |
| 128 |
reinterpret_cast<const char*>(instruction.words + 4); |
| 129 |
const char* stringEnd = |
| 130 |
reinterpret_cast<const char*>(instruction.words + instruction.num_words); |
| 131 |
return ExtractStringLiteral(loc, stringBegin, stringEnd, code); |
| 132 |
} |
| 133 |
|
| 134 |
} // namespace |
| 135 |
|
| 136 |
bool ExtractSourceFromModule( |
| 137 |
const std::vector<uint32_t>& binary, |
| 138 |
std::unordered_map<std::string, std::string>* output) { |
| 139 |
auto context = spvtools::SpirvTools(kDefaultEnvironment); |
| 140 |
context.SetMessageConsumer(spvtools::utils::CLIMessageConsumer); |
| 141 |
|
| 142 |
// There is nothing valuable in the header. |
| 143 |
spvtools::HeaderParser headerParser = [](const spv_endianness_t, |
| 144 |
const spv_parsed_header_t&) { |
| 145 |
return SPV_SUCCESS; |
| 146 |
}; |
| 147 |
|
| 148 |
std::unordered_map<uint32_t, std::string> stringMap; |
| 149 |
std::vector<std::pair<spv::Id, std::string>> sources; |
| 150 |
spv::Op lastOpcode = spv::Op::Max; |
| 151 |
size_t instructionIndex = 0; |
| 152 |
|
| 153 |
spvtools::InstructionParser instructionParser = |
| 154 |
[&stringMap, &sources, &lastOpcode, |
| 155 |
&instructionIndex](const spv_parsed_instruction_t& instruction) { |
| 156 |
const spv_position_t loc = {0, 0, instructionIndex + 1}; |
| 157 |
spv_result_t result = SPV_SUCCESS; |
| 158 |
|
| 159 |
if (instruction.opcode == static_cast<unsigned>(spv::Op::OpString)) { |
| 160 |
std::string content; |
| 161 |
result = extractOpString(loc, instruction, &content); |
| 162 |
if (result == SPV_SUCCESS) { |
| 163 |
stringMap.emplace(instruction.result_id, std::move(content)); |
| 164 |
} |
| 165 |
} else if (instruction.opcode == |
| 166 |
static_cast<unsigned>(spv::Op::OpSource)) { |
| 167 |
spv::Id filenameId; |
| 168 |
std::string code; |
| 169 |
result = extractOpSource(loc, instruction, &filenameId, &code); |
| 170 |
if (result == SPV_SUCCESS) { |
| 171 |
sources.emplace_back(std::make_pair(filenameId, std::move(code))); |
| 172 |
} |
| 173 |
} else if (instruction.opcode == |
| 174 |
static_cast<unsigned>(spv::Op::OpSourceContinued)) { |
| 175 |
if (lastOpcode != spv::Op::OpSource) { |
| 176 |
spvtools::Error(spvtools::utils::CLIMessageConsumer, "", loc, |
| 177 |
"OpSourceContinued MUST follow an OpSource."); |
| 178 |
return SPV_ERROR_INVALID_BINARY; |
| 179 |
} |
| 180 |
|
| 181 |
assert(sources.size() > 0); |
| 182 |
result = extractOpSourceContinued(loc, instruction, |
| 183 |
&sources.back().second); |
| 184 |
} |
| 185 |
|
| 186 |
++instructionIndex; |
| 187 |
lastOpcode = static_cast<spv::Op>(instruction.opcode); |
| 188 |
return result; |
| 189 |
}; |
| 190 |
|
| 191 |
if (!context.Parse(binary, headerParser, instructionParser)) { |
| 192 |
return false; |
| 193 |
} |
| 194 |
|
| 195 |
std::string defaultName = "unnamed-"; |
| 196 |
size_t unnamedCount = 0; |
| 197 |
for (auto & [ id, code ] : sources) { |
| 198 |
std::string filename; |
| 199 |
const auto it = stringMap.find(id); |
| 200 |
if (it == stringMap.cend() || it->second.empty()) { |
| 201 |
filename = "unnamed-" + std::to_string(unnamedCount) + ".hlsl"; |
| 202 |
++unnamedCount; |
| 203 |
} else { |
| 204 |
filename = it->second; |
| 205 |
} |
| 206 |
|
| 207 |
if (output->count(filename) != 0) { |
| 208 |
spvtools::Error(spvtools::utils::CLIMessageConsumer, "", {}, |
| 209 |
"Source file name conflict."); |
| 210 |
return false; |
| 211 |
} |
| 212 |
output->insert({filename, code}); |
| 213 |
} |
| 214 |
|
| 215 |
return true; |
| 216 |
} |