Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 1 | // Copyright (c) 2015 The Khronos Group Inc. |
| 2 | // |
| 3 | // Permission is hereby granted, free of charge, to any person obtaining a |
| 4 | // copy of this software and/or associated documentation files (the |
| 5 | // "Materials"), to deal in the Materials without restriction, including |
| 6 | // without limitation the rights to use, copy, modify, merge, publish, |
| 7 | // distribute, sublicense, and/or sell copies of the Materials, and to |
| 8 | // permit persons to whom the Materials are furnished to do so, subject to |
| 9 | // the following conditions: |
| 10 | // |
| 11 | // The above copyright notice and this permission notice shall be included |
| 12 | // in all copies or substantial portions of the Materials. |
| 13 | // |
| 14 | // MODIFICATIONS TO THIS FILE MAY MEAN IT NO LONGER ACCURATELY REFLECTS |
| 15 | // KHRONOS STANDARDS. THE UNMODIFIED, NORMATIVE VERSIONS OF KHRONOS |
| 16 | // SPECIFICATIONS AND HEADER INFORMATION ARE LOCATED AT |
| 17 | // https://www.khronos.org/registry/ |
| 18 | // |
| 19 | // THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 20 | // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 21 | // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 22 | // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 23 | // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 24 | // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 25 | // MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS. |
| 26 | |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 27 | #include <assert.h> |
| 28 | #include <stdio.h> |
| 29 | #include <string.h> |
| 30 | |
| 31 | #include <vector> |
| 32 | |
Lei Zhang | 923f6c1 | 2015-11-11 12:45:23 -0500 | [diff] [blame] | 33 | #include "libspirv/libspirv.h" |
| 34 | |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 35 | void print_usage(char* argv0) { |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 36 | printf( |
| 37 | "Validate a SPIR-V binary file.\n\n" |
| 38 | "USAGE: %s [options] <filename>\n\n" |
| 39 | " -basic Perform basic validation (disabled)\n" |
| 40 | " -layout Perform layout validation " |
| 41 | "(disabled)\n" |
| 42 | " -id Perform id validation (default ON)\n" |
| 43 | " -capability <capability> Performs OpCode validation " |
| 44 | "(disabled)\n", |
| 45 | argv0); |
| 46 | } |
| 47 | |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 48 | int main(int argc, char** argv) { |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 49 | if (2 > argc) { |
| 50 | print_usage(argv[0]); |
| 51 | return 1; |
| 52 | } |
| 53 | |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 54 | const char* inFile = nullptr; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 55 | uint32_t options = 0; |
| 56 | |
| 57 | for (int argi = 1; argi < argc; ++argi) { |
| 58 | if ('-' == argv[argi][0]) { |
| 59 | if (!strcmp("basic", argv[argi] + 1)) { |
| 60 | options |= SPV_VALIDATE_BASIC_BIT; |
| 61 | } else if (!strcmp("layout", argv[argi] + 1)) { |
| 62 | options |= SPV_VALIDATE_LAYOUT_BIT; |
| 63 | } else if (!strcmp("id", argv[argi] + 1)) { |
| 64 | options |= SPV_VALIDATE_ID_BIT; |
| 65 | } else if (!strcmp("rules", argv[argi] + 1)) { |
| 66 | options |= SPV_VALIDATE_RULES_BIT; |
| 67 | } else { |
| 68 | print_usage(argv[0]); |
| 69 | return 1; |
| 70 | } |
| 71 | } else { |
| 72 | if (!inFile) { |
| 73 | inFile = argv[argi]; |
| 74 | } else { |
| 75 | print_usage(argv[0]); |
| 76 | return 1; |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 81 | if (!inFile) { |
| 82 | fprintf(stderr, "error: input file is empty.\n"); |
| 83 | return 1; |
| 84 | } |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 85 | |
| 86 | std::vector<uint32_t> contents; |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 87 | if (FILE* fp = fopen(inFile, "rb")) { |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 88 | uint32_t buf[1024]; |
| 89 | while (size_t len = fread(buf, sizeof(uint32_t), |
| 90 | sizeof(buf) / sizeof(uint32_t), fp)) { |
| 91 | contents.insert(contents.end(), buf, buf + len); |
| 92 | } |
| 93 | fclose(fp); |
| 94 | } else { |
| 95 | fprintf(stderr, "error: file does not exist '%s'\n", inFile); |
| 96 | return 1; |
| 97 | } |
| 98 | |
Andrew Woloszyn | 55ecc2e | 2015-11-11 11:05:07 -0500 | [diff] [blame] | 99 | spv_const_binary_t binary = {contents.data(), contents.size()}; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 100 | |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 101 | spv_diagnostic diagnostic = nullptr; |
Lei Zhang | 972788b | 2015-11-12 13:48:30 -0500 | [diff] [blame^] | 102 | spv_context context = spvContextCreate(); |
| 103 | spv_result_t error = spvValidate(context, &binary, options, &diagnostic); |
| 104 | spvContextDestroy(context); |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 105 | if (error) { |
| 106 | spvDiagnosticPrint(diagnostic); |
| 107 | spvDiagnosticDestroy(diagnostic); |
| 108 | return error; |
| 109 | } |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 110 | |
| 111 | return 0; |
| 112 | } |