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
|
/*
* Copyright (c) 2017, Intel Corporation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
//!
//! \file cm_visa.cpp
//! \brief Contains ISAfile definitions
//!
#include "cm_visa.h"
#include "cm_def_os.h"
#include <fstream>
using namespace vISA;
ISAfile::ISAfile(const uint8_t *data, unsigned size) : version(0), data(data), end(data + size),
size(size), error(0), header(0), kernel_data_loaded(false), function_data_loaded(false) { }
ISAfile::ISAfile(const ISAfile& other) {
version = other.version;
data = other.data;
end = other.end;
size = other.size;
delete[] error;
char *perror = new char[std::strlen(other.error)];
MOS_SecureMemcpy(perror, sizeof(perror), other.error, sizeof(other.error));
error = perror;
kernel_data_loaded = other.kernel_data_loaded;
function_data_loaded = other.function_data_loaded;
errorIndex = other.errorIndex;
header = new Header(other.version);
*header = *other.header;
for (KernelBody *kb : other.kernel_data) {
KernelBody *kb2 = new KernelBody(other.version);
*kb2 = *kb;
kernel_data.push_back(kb2);
}
for (FunctionBody *fb : other.function_data) {
FunctionBody *fb2 = new FunctionBody(other.version);
*fb2 = *fb;
function_data.push_back(fb2);
}
}
ISAfile& ISAfile::operator= (const ISAfile& other) {
if (this != &other) {
version = other.version;
data = other.data;
end = other.end;
size = other.size;
delete[] error;
char *perror = new char[std::strlen(other.error)];
MOS_SecureMemcpy(perror, sizeof perror, other.error, sizeof other.error);
error = perror;
kernel_data_loaded = other.kernel_data_loaded;
function_data_loaded = other.function_data_loaded;
errorIndex = other.errorIndex;
*header = *other.header;
for (KernelBody *kb : kernel_data)
delete kb;
for (FunctionBody *fb : function_data)
delete fb;
for (KernelBody *kb : other.kernel_data)
kernel_data.push_back(&*kb);
for (FunctionBody *fb : other.function_data)
function_data.push_back(&*fb);
}
return *this;
}
ISAfile::~ISAfile() {
delete header;
for (KernelBody *kb : kernel_data)
delete kb;
for (FunctionBody *f : function_data)
delete f;
}
bool ISAfile::readFile() {
bool status = true;
status &= loadHeader();
if (version < 302)
return false;
status &= loadKernelData();
status &= loadFunctionData();
return status;
}
bool ISAfile::loadHeader() {
header = new Header(version);
const uint8_t *p = header->parse(data, end, this);
if (!p) {
delete header;
return false; //error loading header
}
return true;
}
bool ISAfile::loadKernelData() {
const uint8_t *p = 0;
for (Kernel *k : header->getKernelInfo()) {
KernelBody *kb = new KernelBody(version);
p = kb->parse(data + k->getOffset(), end, this);
if (!p) {
delete kb;
return false; //error loading kernel_data
}
kernel_data.push_back(kb);
}
kernel_data_loaded = true;
return true;
}
bool ISAfile::loadFunctionData() {
const uint8_t *p = 0;
for (Function *f : header->getFunctionInfo()) {
FunctionBody *fb = new FunctionBody(version);
p = fb->parse(data + f->getOffset(), end, this);
if (!p) {
delete fb;
return false; //error loading kernel_data
}
function_data.push_back(fb);
}
function_data_loaded = true;
return true;
}
std::vector<KernelBody*> &ISAfile::getKernelsData() {
if (!kernel_data_loaded) loadKernelData();
return kernel_data;
}
std::vector<FunctionBody*> &ISAfile::getFunctionsData() {
if (!function_data_loaded) loadFunctionData();
return function_data;
}
const uint8_t* ISAfile::readField(const uint8_t *p, const uint8_t *buffEnd,
Field &field, unsigned dataSize) {
switch (field.type) {
case Datatype::ONE: field.number8 = *((int8_t *)p); p++; break;
case Datatype::TWO: field.number16 = *((int16_t *)p); p += 2; break;
case Datatype::FOUR: field.number32 = *((int32_t *)p); p += 4; break;
case Datatype::EIGHT: field.number64 = *((int64_t *)p); p += 8; break;
case Datatype::VARCHAR:
{
if (p + dataSize > buffEnd) {
// error: truncated
p = 0;
return 0;
}
char *string = new char[dataSize + 1];
MOS_SecureMemcpy(string, dataSize + 1, p, dataSize);
string[dataSize] = '\0';
field.size = dataSize;
field.varchar = string;
p += dataSize;
break;
}
case Datatype::VARCHAR_POOL:
{
const uint8_t *strEnd = (const uint8_t *)std::memchr(p, 0, end - p);
auto len = strEnd - p;
char *string = new char[len + 1];
MOS_SecureMemcpy(string, len + 1, p, len);
string[len] = '\0';
field.size = (uint32_t)len + 1;
field.varchar = string;
p = strEnd + 1;
break;
}
case Datatype::GDATA:
{
// copy only if no out of bound.
if (p + dataSize < end) {
uint8_t *gdata = new uint8_t[dataSize];
MOS_SecureMemcpy(gdata, dataSize , p, dataSize);
field.gdata = gdata;
field.size = dataSize;
p += dataSize;
} else {
field.gdata = nullptr;
field.size = 0;
}
break;
}
default: break;
}
return p;
}
const uint8_t *ISAfile::setError(const char * e, unsigned index) {
error = e;
errorIndex = index;
return 0;
}
bool ISAfile::writeToFile(const char *filename, std::vector<uint8_t> &originalBuffer) {
if (!header) {
setError("Header not loaded", 0);
return false;
}
if (!kernel_data_loaded)
loadKernelData();
if (!function_data_loaded)
loadFunctionData();
std::ofstream newFile(filename, std::ios::out | std::ios::binary);
if (!newFile) {
setError("Error creating new file ", 0);
return false;
}
// buffer
std::vector<char> buffer;
// header
header->addToBuffer(buffer, this);
// kernel body
for (KernelBody* k : kernel_data) {
k->addToBuffer(buffer, this);
}
// function body
for (FunctionBody* f : function_data) {
f->addToBuffer(buffer, this);
}
// gen binaries for this kernel
for (Kernel *k : header->getKernelInfo()) {
for (GenBinary *g : k->getGenBinaryInfo()) {
uint32_t offset = g->getBinaryOffset();
if (offset > originalBuffer.size()) {
setError("Error writing GEN binary into ISA file, bad offset from original file", 0);
return false;
}
for (uint32_t b = 0; b < g->getBinarySize(); b++) {
buffer.push_back(static_cast<char>(originalBuffer[offset + b]));
}
}
}
newFile.write(buffer.data(), buffer.size());
newFile.close();
return true;
}
void ISAfile::addToBuffer(Field &field, std::vector<char> &buffer) {
switch (field.type) {
case Datatype::ONE: buffer.push_back(field.ui8[0]); break;
case Datatype::TWO: buffer.push_back(field.ui8[0]); buffer.push_back(field.ui8[1]); break;
case Datatype::FOUR: buffer.push_back(field.ui8[0]); buffer.push_back(field.ui8[1]);
buffer.push_back(field.ui8[2]); buffer.push_back(field.ui8[3]); break;
case Datatype::EIGHT: buffer.push_back(field.ui8[0]); buffer.push_back(field.ui8[1]);
buffer.push_back(field.ui8[2]); buffer.push_back(field.ui8[3]);
buffer.push_back(field.ui8[4]); buffer.push_back(field.ui8[5]);
buffer.push_back(field.ui8[6]); buffer.push_back(field.ui8[7]); break;
case Datatype::VARCHAR:
{
for (unsigned i = 0; i < field.size; i++) {
buffer.push_back(static_cast<char>(field.varchar[i]));
}
break;
}
case Datatype::VARCHAR_POOL:
{
for (unsigned i = 0; i < field.size; i++) {
buffer.push_back(static_cast<char>(field.varchar[i]));
}
break;
}
case Datatype::GDATA:
{
for (unsigned i = 0; i < field.size; i++) {
buffer.push_back(static_cast<char>(field.gdata[i]));
}
break;
}
default: break;
}
}
|