c_interface_test.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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[] = " 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 beginning 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(
  106. "Expected operand for OpName instruction, but found the end of the "
  107. "stream.",
  108. message);
  109. });
  110. spv_binary binary = nullptr;
  111. EXPECT_EQ(SPV_ERROR_INVALID_TEXT,
  112. spvTextToBinary(context, input_text, sizeof(input_text), &binary,
  113. nullptr));
  114. #ifndef SPIRV_TOOLS_SHAREDLIB
  115. EXPECT_EQ(1, invocation);
  116. #endif
  117. spvBinaryDestroy(binary);
  118. spvContextDestroy(context);
  119. }
  120. TEST(CInterface, SpecifyConsumerNullDiagnosticForDisassembling) {
  121. const char input_text[] = "OpNop";
  122. auto context = spvContextCreate(SPV_ENV_UNIVERSAL_1_1);
  123. int invocation = 0;
  124. SetContextMessageConsumer(
  125. context,
  126. [&invocation](spv_message_level_t level, const char* source,
  127. const spv_position_t& position, const char* message) {
  128. ++invocation;
  129. EXPECT_EQ(SPV_MSG_ERROR, level);
  130. EXPECT_STREQ("input", source);
  131. EXPECT_EQ(0u, position.line);
  132. EXPECT_EQ(0u, position.column);
  133. EXPECT_EQ(1u, position.index);
  134. EXPECT_STREQ("Invalid opcode: 65535", message);
  135. });
  136. spv_binary binary = nullptr;
  137. ASSERT_EQ(SPV_SUCCESS, spvTextToBinary(context, input_text,
  138. sizeof(input_text), &binary, nullptr));
  139. // Change OpNop to an invalid (wordcount|opcode) word.
  140. binary->code[binary->wordCount - 1] = 0xffffffff;
  141. spv_text text = nullptr;
  142. EXPECT_EQ(SPV_ERROR_INVALID_BINARY,
  143. spvBinaryToText(context, binary->code, binary->wordCount, 0, &text,
  144. nullptr));
  145. #ifndef SPIRV_TOOLS_SHAREDLIB
  146. EXPECT_EQ(1, invocation);
  147. #endif
  148. spvTextDestroy(text);
  149. spvBinaryDestroy(binary);
  150. spvContextDestroy(context);
  151. }
  152. TEST(CInterface, SpecifyConsumerNullDiagnosticForValidating) {
  153. const char input_text[] = "OpNop";
  154. auto context = spvContextCreate(SPV_ENV_UNIVERSAL_1_1);
  155. int invocation = 0;
  156. SetContextMessageConsumer(
  157. context,
  158. [&invocation](spv_message_level_t level, const char* source,
  159. const spv_position_t& position, const char* message) {
  160. ++invocation;
  161. EXPECT_EQ(SPV_MSG_ERROR, level);
  162. EXPECT_STREQ("input", source);
  163. EXPECT_EQ(0u, position.line);
  164. EXPECT_EQ(0u, position.column);
  165. // TODO(antiagainst): what validation reports is not a word offset here.
  166. // It is inconsistent with diassembler. Should be fixed.
  167. EXPECT_EQ(1u, position.index);
  168. EXPECT_STREQ(
  169. "Nop cannot appear before the memory model instruction\n"
  170. " OpNop\n",
  171. message);
  172. });
  173. spv_binary binary = nullptr;
  174. ASSERT_EQ(SPV_SUCCESS, spvTextToBinary(context, input_text,
  175. sizeof(input_text), &binary, nullptr));
  176. spv_const_binary_t b{binary->code, binary->wordCount};
  177. EXPECT_EQ(SPV_ERROR_INVALID_LAYOUT, spvValidate(context, &b, nullptr));
  178. #ifndef SPIRV_TOOLS_SHAREDLIB
  179. EXPECT_EQ(1, invocation);
  180. #endif
  181. spvBinaryDestroy(binary);
  182. spvContextDestroy(context);
  183. }
  184. // When having both a consumer and an diagnostic object, the diagnostic object
  185. // should take priority.
  186. TEST(CInterface, SpecifyConsumerSpecifyDiagnosticForAssembling) {
  187. const char input_text[] = " OpName";
  188. auto context = spvContextCreate(SPV_ENV_UNIVERSAL_1_1);
  189. int invocation = 0;
  190. SetContextMessageConsumer(
  191. context,
  192. [&invocation](spv_message_level_t, const char*, const spv_position_t&,
  193. const char*) { ++invocation; });
  194. spv_binary binary = nullptr;
  195. spv_diagnostic diagnostic = nullptr;
  196. EXPECT_EQ(SPV_ERROR_INVALID_TEXT,
  197. spvTextToBinary(context, input_text, sizeof(input_text), &binary,
  198. &diagnostic));
  199. EXPECT_EQ(0, invocation); // Consumer should not be invoked at all.
  200. EXPECT_STREQ(
  201. "Expected operand for OpName instruction, but found the end of the "
  202. "stream.",
  203. diagnostic->error);
  204. spvDiagnosticDestroy(diagnostic);
  205. spvBinaryDestroy(binary);
  206. spvContextDestroy(context);
  207. }
  208. TEST(CInterface, SpecifyConsumerSpecifyDiagnosticForDisassembling) {
  209. const char input_text[] = "OpNop";
  210. auto context = spvContextCreate(SPV_ENV_UNIVERSAL_1_1);
  211. int invocation = 0;
  212. SetContextMessageConsumer(
  213. context,
  214. [&invocation](spv_message_level_t, const char*, const spv_position_t&,
  215. const char*) { ++invocation; });
  216. spv_binary binary = nullptr;
  217. ASSERT_EQ(SPV_SUCCESS, spvTextToBinary(context, input_text,
  218. sizeof(input_text), &binary, nullptr));
  219. // Change OpNop to an invalid (wordcount|opcode) word.
  220. binary->code[binary->wordCount - 1] = 0xffffffff;
  221. spv_diagnostic diagnostic = nullptr;
  222. spv_text text = nullptr;
  223. EXPECT_EQ(SPV_ERROR_INVALID_BINARY,
  224. spvBinaryToText(context, binary->code, binary->wordCount, 0, &text,
  225. &diagnostic));
  226. EXPECT_EQ(0, invocation); // Consumer should not be invoked at all.
  227. EXPECT_STREQ("Invalid opcode: 65535", diagnostic->error);
  228. spvTextDestroy(text);
  229. spvDiagnosticDestroy(diagnostic);
  230. spvBinaryDestroy(binary);
  231. spvContextDestroy(context);
  232. }
  233. TEST(CInterface, SpecifyConsumerSpecifyDiagnosticForValidating) {
  234. const char input_text[] = "OpNop";
  235. auto context = spvContextCreate(SPV_ENV_UNIVERSAL_1_1);
  236. int invocation = 0;
  237. SetContextMessageConsumer(
  238. context,
  239. [&invocation](spv_message_level_t, const char*, const spv_position_t&,
  240. const char*) { ++invocation; });
  241. spv_binary binary = nullptr;
  242. ASSERT_EQ(SPV_SUCCESS, spvTextToBinary(context, input_text,
  243. sizeof(input_text), &binary, nullptr));
  244. spv_diagnostic diagnostic = nullptr;
  245. spv_const_binary_t b{binary->code, binary->wordCount};
  246. EXPECT_EQ(SPV_ERROR_INVALID_LAYOUT, spvValidate(context, &b, &diagnostic));
  247. EXPECT_EQ(0, invocation); // Consumer should not be invoked at all.
  248. EXPECT_STREQ(
  249. "Nop cannot appear before the memory model instruction\n"
  250. " OpNop\n",
  251. diagnostic->error);
  252. spvDiagnosticDestroy(diagnostic);
  253. spvBinaryDestroy(binary);
  254. spvContextDestroy(context);
  255. }
  256. } // namespace
  257. } // namespace spvtools