GenerateStubsWhiteList.cpp revision 4324eec445c6882dce89a24765482293ed31914e
1/* 2 * Copyright (C) 2015 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17#include <algorithm> 18#include <iostream> 19#include <iterator> 20#include <sstream> 21 22#include "Generator.h" 23#include "Specification.h" 24#include "Utilities.h" 25 26using namespace std; 27 28const int kMinimumApiLevelForTests = 11; 29const int kApiLevelWithFirst64Bit = 21; 30 31// Used to map the built-in types to their mangled representations 32struct BuiltInMangling { 33 const char* token[3]; // The last two entries can be nullptr 34 const char* equivalence; // The mangled equivalent 35}; 36 37BuiltInMangling builtInMangling[] = { 38 {{"long", "long"}, "x"}, 39 {{"unsigned", "long", "long"}, "y"}, 40 {{"long"}, "l"}, 41 {{"unsigned", "long"}, "m"}, 42 {{"int"}, "i"}, 43 {{"unsigned", "int"}, "j"}, 44 {{"short"}, "s"}, 45 {{"unsigned", "short"}, "t"}, 46 {{"char"}, "c"}, 47 {{"unsigned", "char"}, "h"}, 48 {{"signed", "char"}, "a"}, 49 {{"void"}, "v"}, 50 {{"wchar_t"}, "w"}, 51 {{"bool"}, "b"}, 52 {{"__fp16"}, "Dh"}, 53 {{"float"}, "f"}, 54 {{"double"}, "d"}, 55}; 56 57/* For the given API level and bitness (e.g. 32 or 64 bit), try to find a 58 * substitution for the provided type name, as would be done (mostly) by a 59 * preprocessor. Returns empty string if there's no substitution. 60 */ 61static string findSubstitute(const string& typeName, int apiLevel, int intSize) { 62 const auto& types = systemSpecification.getTypes(); 63 const auto type = types.find(typeName); 64 if (type != types.end()) { 65 for (TypeSpecification* spec : type->second->getSpecifications()) { 66 // Verify this specification applies 67 const VersionInfo info = spec->getVersionInfo(); 68 if (!info.includesVersion(apiLevel) || (info.intSize != 0 && info.intSize != intSize)) { 69 continue; 70 } 71 switch (spec->getKind()) { 72 case SIMPLE: { 73 /* For simple typedefs, replace it unless it's the special case of _RS_HANDLE 74 * which is a macro of a struct. 75 */ 76 const string s = spec->getSimpleType(); 77 if (s != "_RS_HANDLE") { 78 return s; 79 } 80 break; 81 } 82 case STRUCT: { 83 const string s = spec->getStructName(); 84 if (!s.empty()) { 85 return s; 86 } 87 break; 88 } 89 case ENUM: 90 // Do nothing 91 break; 92 } 93 } 94 } 95 return ""; 96} 97 98/* Expand the typedefs found in 'type' into their equivalents and tokenize 99 * the resulting list. 'apiLevel' and 'intSize' specifies the API level and bitness 100 * we are currently processing. 101 */ 102list<string> expandTypedefs(const string type, int apiLevel, int intSize) { 103 // Split the string in tokens. 104 istringstream stream(type); 105 list<string> tokens{istream_iterator<string>{stream}, istream_iterator<string>{}}; 106 // Try to substitue each token. 107 for (auto i = tokens.begin(); i != tokens.end();) { 108 const string substitute = findSubstitute(*i, apiLevel, intSize); 109 if (substitute.empty()) { 110 // No substitution possible, just go to the next token. 111 i++; 112 } else { 113 // Split the replacement string in tokens. 114 istringstream stream(substitute); 115 list<string> newTokens{istream_iterator<string>{stream}, istream_iterator<string>{}}; 116 // Replace the token with the substitution. Don't advance, as the new substitution 117 // might itself be replaced. 118 auto prev = i; 119 --prev; 120 tokens.insert(i, newTokens.begin(), newTokens.end()); 121 tokens.erase(i); 122 advance(i, -newTokens.size()); 123 } 124 } 125 return tokens; 126} 127 128// Remove the first element of the list if it equals 'prefix'. Return true in that case. 129static bool eatFront(list<string>* tokens, const char* prefix) { 130 if (tokens->front() == prefix) { 131 tokens->pop_front(); 132 return true; 133 } 134 return false; 135} 136 137/* Search the table of translations for the built-ins for the mangling that 138 * corresponds to this list of tokens. If a match is found, consume these tokens 139 * and return a pointer to the string. If not, return nullptr. 140 */ 141static const char* findManglingOfBuiltInType(list<string>* tokens) { 142 for (const BuiltInMangling& a : builtInMangling) { 143 auto t = tokens->begin(); 144 auto end = tokens->end(); 145 bool match = true; 146 // We match up to three tokens. 147 for (int i = 0; i < 3; i++) { 148 if (!a.token[i]) { 149 // No more tokens 150 break; 151 } 152 if (t == end || *t++ != a.token[i]) { 153 match = false; 154 } 155 } 156 if (match) { 157 tokens->erase(tokens->begin(), t); 158 return a.equivalence; 159 } 160 } 161 return nullptr; 162} 163 164// Mangle a long name by prefixing it with its length, e.g. "13rs_allocation". 165static inline string mangleLongName(const string& name) { 166 return to_string(name.size()) + name; 167} 168 169/* Mangle the type name that's represented by the vector size and list of tokens. 170 * The mangling will be returned in full form in 'mangling'. 'compressedMangling' 171 * will have the compressed equivalent. This is built using the 'previousManglings' 172 * list. false is returned if an error is encountered. 173 * 174 * This function is recursive because compression is possible at each level of the definition. 175 * See http://mentorembedded.github.io/cxx-abi/abi.html#mangle.type for a description 176 * of the Itanium mangling used by llvm. 177 * 178 * This function mangles correctly the types currently used by RenderScript. It does 179 * not currently mangle more complicated types like function pointers, namespaces, 180 * or other C++ types. In particular, we don't deal correctly with parenthesis. 181 */ 182static bool mangleType(string vectorSize, list<string>* tokens, vector<string>* previousManglings, 183 string* mangling, string* compressedMangling) { 184 string delta; // The part of the mangling we're generating for this recursion. 185 bool isTerminal = false; // True if this iteration parses a terminal node in the production. 186 bool canBeCompressed = true; // Will be false for manglings of builtins. 187 188 if (tokens->back() == "*") { 189 delta = "P"; 190 tokens->pop_back(); 191 } else if (eatFront(tokens, "const")) { 192 delta = "K"; 193 } else if (eatFront(tokens, "volatile")) { 194 delta = "V"; 195 } else if (vectorSize != "1" && vectorSize != "") { 196 // For vector, prefix with the abbreviation for a vector, including the size. 197 delta = "Dv" + vectorSize + "_"; 198 vectorSize.clear(); // Reset to mark the size as consumed. 199 } else if (eatFront(tokens, "struct")) { 200 // For a structure, we just use the structure name 201 if (tokens->size() == 0) { 202 cerr << "Expected a name after struct\n"; 203 return false; 204 } 205 delta = mangleLongName(tokens->front()); 206 isTerminal = true; 207 tokens->pop_front(); 208 } else { 209 const char* c = findManglingOfBuiltInType(tokens); 210 if (c) { 211 // It's a basic type. We don't use those directly for compression. 212 delta = c; 213 isTerminal = true; 214 canBeCompressed = false; 215 } else if (tokens->size() > 0) { 216 // It's a complex type name. 217 delta = mangleLongName(tokens->front()); 218 isTerminal = true; 219 tokens->pop_front(); 220 } 221 } 222 223 if (isTerminal) { 224 // If we're the terminal node, there should be nothing left to mangle. 225 if (tokens->size() > 0) { 226 cerr << "Expected nothing else but found"; 227 for (const auto& t : *tokens) { 228 cerr << " " << t; 229 } 230 cerr << "\n"; 231 return false; 232 } 233 *mangling = delta; 234 *compressedMangling = delta; 235 } else { 236 // We're not terminal. Recurse and prefix what we've translated this pass. 237 if (tokens->size() == 0) { 238 cerr << "Expected a more complete type\n"; 239 return false; 240 } 241 string rest, compressedRest; 242 if (!mangleType(vectorSize, tokens, previousManglings, &rest, &compressedRest)) { 243 return false; 244 } 245 *mangling = delta + rest; 246 *compressedMangling = delta + compressedRest; 247 } 248 249 /* If it's a built-in type, we don't look at previously emitted ones and we 250 * don't keep track of it. 251 */ 252 if (!canBeCompressed) { 253 return true; 254 } 255 256 // See if we've encountered this mangling before. 257 for (size_t i = 0; i < previousManglings->size(); ++i) { 258 if ((*previousManglings)[i] == *mangling) { 259 // We have a match, construct an index reference to that previously emitted mangling. 260 ostringstream stream2; 261 stream2 << 'S'; 262 if (i > 0) { 263 stream2 << (char)('0' + i - 1); 264 } 265 stream2 << '_'; 266 *compressedMangling = stream2.str(); 267 return true; 268 } 269 } 270 271 // We have not encountered this before. Add it to the list. 272 previousManglings->push_back(*mangling); 273 return true; 274} 275 276// Write to the stream the mangled representation of each parameter. 277static bool writeParameters(ostringstream* stream, const std::vector<ParameterDefinition*>& params, 278 int apiLevel, int intSize) { 279 if (params.empty()) { 280 *stream << "v"; 281 return true; 282 } 283 /* We keep track of the previously generated parameter types, as type mangling 284 * is compressed by reusing previous manglings. 285 */ 286 vector<string> previousManglings; 287 for (ParameterDefinition* p : params) { 288 // Expand the typedefs and create a tokenized list. 289 list<string> tokens = expandTypedefs(p->rsType, apiLevel, intSize); 290 if (p->isOutParameter) { 291 tokens.push_back("*"); 292 } 293 string mangling, compressedMangling; 294 if (!mangleType(p->mVectorSize, &tokens, &previousManglings, &mangling, 295 &compressedMangling)) { 296 return false; 297 } 298 *stream << compressedMangling; 299 } 300 return true; 301} 302 303/* Add the mangling for this permutation of the function. apiLevel and intSize is used 304 * to select the correct type when expanding complex type. 305 */ 306static bool addFunctionManglingToSet(const Function& function, 307 const FunctionPermutation& permutation, bool overloadable, 308 int apiLevel, int intSize, set<string>* allManglings) { 309 const string& functionName = permutation.getName(); 310 string mangling; 311 if (overloadable) { 312 ostringstream stream; 313 stream << "_Z" << mangleLongName(functionName); 314 if (!writeParameters(&stream, permutation.getParams(), apiLevel, intSize)) { 315 cerr << "Error mangling " << functionName << ". See above message.\n"; 316 return false; 317 } 318 mangling = stream.str(); 319 } else { 320 mangling = functionName; 321 } 322 allManglings->insert(mangling); 323 return true; 324} 325 326/* Add to the set the mangling of each function prototype that can be generated from this 327 * specification, i.e. for all the versions covered and for 32/64 bits. We call this 328 * for each API level because the implementation of a type may have changed in the range 329 * of API levels covered. 330 */ 331static bool addManglingsForSpecification(const Function& function, 332 const FunctionSpecification& spec, int lastApiLevel, 333 set<string>* allManglings) { 334 // If the function is inlined, we won't generate an unresolved external for that. 335 if (spec.hasInline()) { 336 return true; 337 } 338 const VersionInfo info = spec.getVersionInfo(); 339 const int minApiLevel = info.minVersion ? info.minVersion : kMinimumApiLevelForTests; 340 const int maxApiLevel = info.maxVersion ? info.maxVersion : lastApiLevel; 341 const bool overloadable = spec.isOverloadable(); 342 343 /* We track success rather than aborting early in case of failure so that we 344 * generate all the error messages. 345 */ 346 bool success = true; 347 for (int apiLevel = minApiLevel; apiLevel <= maxApiLevel; ++apiLevel) { 348 for (auto permutation : spec.getPermutations()) { 349 if (info.intSize == 0 || info.intSize == 32) { 350 if (!addFunctionManglingToSet(function, *permutation, overloadable, apiLevel, 32, 351 allManglings)) { 352 success = false; 353 } 354 } 355 if (apiLevel >= kApiLevelWithFirst64Bit && (info.intSize == 0 || info.intSize == 64)) { 356 if (!addFunctionManglingToSet(function, *permutation, overloadable, apiLevel, 64, 357 allManglings)) { 358 success = false; 359 } 360 } 361 } 362 } 363 return success; 364} 365 366/* Generate the white list file of the mangled function prototypes. This generated list is used 367 * to validate unresolved external references. 'lastApiLevel' is the largest api level found in 368 * all spec files. 369 */ 370static bool generateWhiteListFile(int lastApiLevel) { 371 bool success = true; 372 // We generate all the manglings in a set to remove duplicates and to order them. 373 set<string> allManglings; 374 for (auto f : systemSpecification.getFunctions()) { 375 const Function* function = f.second; 376 for (auto spec : function->getSpecifications()) { 377 if (!addManglingsForSpecification(*function, *spec, lastApiLevel, &allManglings)) { 378 success = false; // We continue so we can generate all errors. 379 } 380 } 381 } 382 383 if (success) { 384 GeneratedFile file; 385 if (!file.start(".", "RSStubsWhiteList.cpp")) { 386 return false; 387 } 388 389 file.writeNotices(); 390 file << "#include \"RSStubsWhiteList.h\"\n\n"; 391 file << "std::vector<std::string> stubList = {\n"; 392 for (const auto& e : allManglings) { 393 file << "\"" << e << "\",\n"; 394 } 395 file << "};\n"; 396 } 397 return success; 398} 399 400// Add a uniquely named variable definition to the file and return its name. 401static const string addVariable(GeneratedFile* file, unsigned int* variableNumber) { 402 const string name = "buf" + to_string((*variableNumber)++); 403 /* Some data structures like rs_tm can't be exported. We'll just use a dumb buffer 404 * and cast its address later on. 405 */ 406 *file << "char " << name << "[200];\n"; 407 return name; 408} 409 410/* Write to the file the globals needed to make the call for this permutation. The actual 411 * call is stored in 'calls', as we'll need to generate all the global variable declarations 412 * before the function definition. 413 */ 414static void generateTestCall(GeneratedFile* file, ostringstream* calls, 415 unsigned int* variableNumber, const Function& function, 416 const FunctionPermutation& permutation) { 417 *calls << " "; 418 419 // Handle the return type. 420 const auto ret = permutation.getReturn(); 421 if (ret && ret->rsType != "void" && ret->rsType != "const void") { 422 *calls << "*(" << ret->rsType << "*)" << addVariable(file, variableNumber) << " = "; 423 } 424 425 *calls << permutation.getName() << "("; 426 427 // Generate the arguments. 428 const char* separator = ""; 429 for (auto p : permutation.getParams()) { 430 *calls << separator; 431 // Special case for the kernel context, as it has a special existence. 432 if (p->rsType == "rs_kernel_context") { 433 *calls << "context"; 434 } else if (p->isOutParameter) { 435 *calls << "(" << p->rsType << "*) " << addVariable(file, variableNumber); 436 } else { 437 *calls << "*(" << p->rsType << "*)" << addVariable(file, variableNumber); 438 } 439 separator = ", "; 440 } 441 *calls << ");\n"; 442} 443 444/* Generate a test file that will be used in the frameworks/compile/slang/tests unit tests. 445 * This file tests that all RenderScript APIs can be called for the specified API level. 446 * To avoid the compiler agressively pruning out our calls, we use globals as inputs and outputs. 447 * 448 * Since some structures can't be defined at the global level, we use casts of simple byte 449 * buffers to get around that restriction. 450 * 451 * This file can be used to verify the white list that's also generated in this file. To do so, 452 * run "llvm-nm -undefined-only -just-symbol-name" on the resulting bit code. 453 */ 454static bool generateApiTesterFile(const string& slangTestDirectory, int apiLevel) { 455 GeneratedFile file; 456 if (!file.start(slangTestDirectory, "all" + to_string(apiLevel) + ".rs")) { 457 return false; 458 } 459 460 /* This unusual comment is used by slang/tests/test.py to know which parameter to pass 461 * to llvm-rs-cc when compiling the test. 462 */ 463 file << "// -target-api " << apiLevel << " -Wno-deprecated-declarations\n"; 464 465 file.writeNotices(); 466 file << "#pragma version(1)\n"; 467 file << "#pragma rs java_package_name(com.example.renderscript.testallapi)\n\n"; 468 if (apiLevel < 23) { // All rs_graphics APIs were deprecated in api level 23. 469 file << "#include \"rs_graphics.rsh\"\n\n"; 470 } 471 472 /* The code below emits globals and calls to functions in parallel. We store 473 * the calls in a stream so that we can emit them in the file in the proper order. 474 */ 475 ostringstream calls; 476 unsigned int variableNumber = 0; // Used to generate unique names. 477 for (auto f : systemSpecification.getFunctions()) { 478 const Function* function = f.second; 479 for (auto spec : function->getSpecifications()) { 480 VersionInfo info = spec->getVersionInfo(); 481 if (!info.includesVersion(apiLevel)) { 482 continue; 483 } 484 if (info.intSize == 32) { 485 calls << "#ifndef __LP64__\n"; 486 } else if (info.intSize == 64) { 487 calls << "#ifdef __LP64__\n"; 488 } 489 for (auto permutation : spec->getPermutations()) { 490 generateTestCall(&file, &calls, &variableNumber, *function, *permutation); 491 } 492 if (info.intSize != 0) { 493 calls << "#endif\n"; 494 } 495 } 496 } 497 file << "\n"; 498 499 // Modify the style of kernel as required by the API level. 500 if (apiLevel >= 23) { 501 file << "void RS_KERNEL test(int in, rs_kernel_context context) {\n"; 502 } else if (apiLevel >= 17) { 503 file << "void RS_KERNEL test(int in) {\n"; 504 } else { 505 file << "void root(const int* in) {\n"; 506 } 507 file << calls.str(); 508 file << "}\n"; 509 510 return true; 511} 512 513bool generateStubsWhiteList(const string& slangTestDirectory, int maxApiLevel) { 514 int lastApiLevel = min(systemSpecification.getMaximumApiLevel(), maxApiLevel); 515 if (!generateWhiteListFile(lastApiLevel)) { 516 return false; 517 } 518 // Generate a test file for each apiLevel. 519 for (int i = kMinimumApiLevelForTests; i <= lastApiLevel; ++i) { 520 if (!generateApiTesterFile(slangTestDirectory, i)) { 521 return false; 522 } 523 } 524 return true; 525} 526