c_interface_test.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. // Copyright (c) 2016 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. #include "gtest/gtest.h"
  15. #include "source/table.h"
  16. #include "spirv-tools/libspirv.h"
  17. namespace spvtools {
  18. namespace {
  19. // TODO(antiagainst): Use public C API for setting the consumer once exists.
  20. #ifndef SPIRV_TOOLS_SHAREDLIB
  21. void SetContextMessageConsumer(spv_context context, MessageConsumer consumer) {
  22. spvtools::SetContextMessageConsumer(context, consumer);
  23. }
  24. #else
  25. void SetContextMessageConsumer(spv_context, MessageConsumer) {}
  26. #endif
  27. // The default consumer is a null std::function.
  28. TEST(CInterface, DefaultConsumerNullDiagnosticForValidInput) {
  29. auto context = spvContextCreate(SPV_ENV_UNIVERSAL_1_1);
  30. const char input_text[] =
  31. "OpCapability Shader\n"
  32. "OpCapability Linkage\n"
  33. "OpMemoryModel Logical GLSL450";
  34. spv_binary binary = nullptr;
  35. EXPECT_EQ(SPV_SUCCESS, spvTextToBinary(context, input_text,
  36. sizeof(input_text), &binary, nullptr));
  37. {
  38. // Sadly the compiler don't allow me to feed binary directly to
  39. // spvValidate().
  40. spv_const_binary_t b{binary->code, binary->wordCount};
  41. EXPECT_EQ(SPV_SUCCESS, spvValidate(context, &b, nullptr));
  42. }
  43. spv_text text = nullptr;
  44. EXPECT_EQ(SPV_SUCCESS, spvBinaryToText(context, binary->code,
  45. binary->wordCount, 0, &text, nullptr));
  46. spvTextDestroy(text);
  47. spvBinaryDestroy(binary);
  48. spvContextDestroy(context);
  49. }
  50. // The default consumer is a null std::function.
  51. TEST(CInterface, DefaultConsumerNullDiagnosticForInvalidAssembling) {
  52. auto context = spvContextCreate(SPV_ENV_UNIVERSAL_1_1);
  53. const char input_text[] = "%1 = OpName";
  54. spv_binary binary = nullptr;
  55. EXPECT_EQ(SPV_ERROR_INVALID_TEXT,
  56. spvTextToBinary(context, input_text, sizeof(input_text), &binary,
  57. nullptr));
  58. spvBinaryDestroy(binary);
  59. spvContextDestroy(context);
  60. }
  61. // The default consumer is a null std::function.
  62. TEST(CInterface, DefaultConsumerNullDiagnosticForInvalidDiassembling) {
  63. auto context = spvContextCreate(SPV_ENV_UNIVERSAL_1_1);
  64. const char input_text[] = "OpNop";
  65. spv_binary binary = nullptr;
  66. ASSERT_EQ(SPV_SUCCESS, spvTextToBinary(context, input_text,
  67. sizeof(input_text), &binary, nullptr));
  68. // Change OpNop to an invalid (wordcount|opcode) word.
  69. binary->code[binary->wordCount - 1] = 0xffffffff;
  70. spv_text text = nullptr;
  71. EXPECT_EQ(SPV_ERROR_INVALID_BINARY,
  72. spvBinaryToText(context, binary->code, binary->wordCount, 0, &text,
  73. nullptr));
  74. spvTextDestroy(text);
  75. spvBinaryDestroy(binary);
  76. spvContextDestroy(context);
  77. }
  78. // The default consumer is a null std::function.
  79. TEST(CInterface, DefaultConsumerNullDiagnosticForInvalidValidating) {
  80. auto context = spvContextCreate(SPV_ENV_UNIVERSAL_1_1);
  81. const char input_text[] = "OpNop";
  82. spv_binary binary = nullptr;
  83. ASSERT_EQ(SPV_SUCCESS, spvTextToBinary(context, input_text,
  84. sizeof(input_text), &binary, nullptr));
  85. spv_const_binary_t b{binary->code, binary->wordCount};
  86. EXPECT_EQ(SPV_ERROR_INVALID_LAYOUT, spvValidate(context, &b, nullptr));
  87. spvBinaryDestroy(binary);
  88. spvContextDestroy(context);
  89. }
  90. TEST(CInterface, SpecifyConsumerNullDiagnosticForAssembling) {
  91. const char input_text[] = "%1 = OpName\n";
  92. auto context = spvContextCreate(SPV_ENV_UNIVERSAL_1_1);
  93. int invocation = 0;
  94. SetContextMessageConsumer(
  95. context,
  96. [&invocation](spv_message_level_t level, const char* source,
  97. const spv_position_t& position, const char* message) {
  98. ++invocation;
  99. EXPECT_EQ(SPV_MSG_ERROR, level);
  100. // The error happens at scanning the begining of second line.
  101. EXPECT_STREQ("input", source);
  102. EXPECT_EQ(1u, position.line);
  103. EXPECT_EQ(0u, position.column);
  104. EXPECT_EQ(12u, position.index);
  105. EXPECT_STREQ("Expected operand, found end of stream.", message);
  106. });
  107. spv_binary binary = nullptr;
  108. EXPECT_EQ(SPV_ERROR_INVALID_TEXT,
  109. spvTextToBinary(context, input_text, sizeof(input_text), &binary,
  110. nullptr));
  111. #ifndef SPIRV_TOOLS_SHAREDLIB
  112. EXPECT_EQ(1, invocation);
  113. #endif
  114. spvBinaryDestroy(binary);
  115. spvContextDestroy(context);
  116. }
  117. TEST(CInterface, SpecifyConsumerNullDiagnosticForDisassembling) {
  118. const char input_text[] = "OpNop";
  119. auto context = spvContextCreate(SPV_ENV_UNIVERSAL_1_1);
  120. int invocation = 0;
  121. SetContextMessageConsumer(
  122. context,
  123. [&invocation](spv_message_level_t level, const char* source,
  124. const spv_position_t& position, const char* message) {
  125. ++invocation;
  126. EXPECT_EQ(SPV_MSG_ERROR, level);
  127. EXPECT_STREQ("input", source);
  128. EXPECT_EQ(0u, position.line);
  129. EXPECT_EQ(0u, position.column);
  130. EXPECT_EQ(1u, position.index);
  131. EXPECT_STREQ("Invalid opcode: 65535", message);
  132. });
  133. spv_binary binary = nullptr;
  134. ASSERT_EQ(SPV_SUCCESS, spvTextToBinary(context, input_text,
  135. sizeof(input_text), &binary, nullptr));
  136. // Change OpNop to an invalid (wordcount|opcode) word.
  137. binary->code[binary->wordCount - 1] = 0xffffffff;
  138. spv_text text = nullptr;
  139. EXPECT_EQ(SPV_ERROR_INVALID_BINARY,
  140. spvBinaryToText(context, binary->code, binary->wordCount, 0, &text,
  141. nullptr));
  142. #ifndef SPIRV_TOOLS_SHAREDLIB
  143. EXPECT_EQ(1, invocation);
  144. #endif
  145. spvTextDestroy(text);
  146. spvBinaryDestroy(binary);
  147. spvContextDestroy(context);
  148. }
  149. TEST(CInterface, SpecifyConsumerNullDiagnosticForValidating) {
  150. const char input_text[] = "OpNop";
  151. auto context = spvContextCreate(SPV_ENV_UNIVERSAL_1_1);
  152. int invocation = 0;
  153. SetContextMessageConsumer(
  154. context,
  155. [&invocation](spv_message_level_t level, const char* source,
  156. const spv_position_t& position, const char* message) {
  157. ++invocation;
  158. EXPECT_EQ(SPV_MSG_ERROR, level);
  159. EXPECT_STREQ("input", source);
  160. EXPECT_EQ(0u, position.line);
  161. EXPECT_EQ(0u, position.column);
  162. // TODO(antiagainst): what validation reports is not a word offset here.
  163. // It is inconsistent with diassembler. Should be fixed.
  164. EXPECT_EQ(1u, position.index);
  165. EXPECT_STREQ(
  166. "Nop cannot appear before the memory model instruction\n"
  167. " OpNop\n",
  168. message);
  169. });
  170. spv_binary binary = nullptr;
  171. ASSERT_EQ(SPV_SUCCESS, spvTextToBinary(context, input_text,
  172. sizeof(input_text), &binary, nullptr));
  173. spv_const_binary_t b{binary->code, binary->wordCount};
  174. EXPECT_EQ(SPV_ERROR_INVALID_LAYOUT, spvValidate(context, &b, nullptr));
  175. #ifndef SPIRV_TOOLS_SHAREDLIB
  176. EXPECT_EQ(1, invocation);
  177. #endif
  178. spvBinaryDestroy(binary);
  179. spvContextDestroy(context);
  180. }
  181. // When having both a consumer and an diagnostic object, the diagnostic object
  182. // should take priority.
  183. TEST(CInterface, SpecifyConsumerSpecifyDiagnosticForAssembling) {
  184. const char input_text[] = "%1 = OpName";
  185. auto context = spvContextCreate(SPV_ENV_UNIVERSAL_1_1);
  186. int invocation = 0;
  187. SetContextMessageConsumer(
  188. context,
  189. [&invocation](spv_message_level_t, const char*, const spv_position_t&,
  190. const char*) { ++invocation; });
  191. spv_binary binary = nullptr;
  192. spv_diagnostic diagnostic = nullptr;
  193. EXPECT_EQ(SPV_ERROR_INVALID_TEXT,
  194. spvTextToBinary(context, input_text, sizeof(input_text), &binary,
  195. &diagnostic));
  196. EXPECT_EQ(0, invocation); // Consumer should not be invoked at all.
  197. EXPECT_STREQ("Expected operand, found end of stream.", diagnostic->error);
  198. spvDiagnosticDestroy(diagnostic);
  199. spvBinaryDestroy(binary);
  200. spvContextDestroy(context);
  201. }
  202. TEST(CInterface, SpecifyConsumerSpecifyDiagnosticForDisassembling) {
  203. const char input_text[] = "OpNop";
  204. auto context = spvContextCreate(SPV_ENV_UNIVERSAL_1_1);
  205. int invocation = 0;
  206. SetContextMessageConsumer(
  207. context,
  208. [&invocation](spv_message_level_t, const char*, const spv_position_t&,
  209. const char*) { ++invocation; });
  210. spv_binary binary = nullptr;
  211. ASSERT_EQ(SPV_SUCCESS, spvTextToBinary(context, input_text,
  212. sizeof(input_text), &binary, nullptr));
  213. // Change OpNop to an invalid (wordcount|opcode) word.
  214. binary->code[binary->wordCount - 1] = 0xffffffff;
  215. spv_diagnostic diagnostic = nullptr;
  216. spv_text text = nullptr;
  217. EXPECT_EQ(SPV_ERROR_INVALID_BINARY,
  218. spvBinaryToText(context, binary->code, binary->wordCount, 0, &text,
  219. &diagnostic));
  220. EXPECT_EQ(0, invocation); // Consumer should not be invoked at all.
  221. EXPECT_STREQ("Invalid opcode: 65535", diagnostic->error);
  222. spvTextDestroy(text);
  223. spvDiagnosticDestroy(diagnostic);
  224. spvBinaryDestroy(binary);
  225. spvContextDestroy(context);
  226. }
  227. TEST(CInterface, SpecifyConsumerSpecifyDiagnosticForValidating) {
  228. const char input_text[] = "OpNop";
  229. auto context = spvContextCreate(SPV_ENV_UNIVERSAL_1_1);
  230. int invocation = 0;
  231. SetContextMessageConsumer(
  232. context,
  233. [&invocation](spv_message_level_t, const char*, const spv_position_t&,
  234. const char*) { ++invocation; });
  235. spv_binary binary = nullptr;
  236. ASSERT_EQ(SPV_SUCCESS, spvTextToBinary(context, input_text,
  237. sizeof(input_text), &binary, nullptr));
  238. spv_diagnostic diagnostic = nullptr;
  239. spv_const_binary_t b{binary->code, binary->wordCount};
  240. EXPECT_EQ(SPV_ERROR_INVALID_LAYOUT, spvValidate(context, &b, &diagnostic));
  241. EXPECT_EQ(0, invocation); // Consumer should not be invoked at all.
  242. EXPECT_STREQ(
  243. "Nop cannot appear before the memory model instruction\n"
  244. " OpNop\n",
  245. diagnostic->error);
  246. spvDiagnosticDestroy(diagnostic);
  247. spvBinaryDestroy(binary);
  248. spvContextDestroy(context);
  249. }
  250. } // namespace
  251. } // namespace spvtools