libc_builtin_test.cc 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "func_types.h"
  6. #include "test_helper.h"
  7. #include "wasm_export.h"
  8. #include "gtest/gtest.h"
  9. #include <limits.h>
  10. #include "../interpreter/wasm.h"
  11. void *func_ptr;
  12. #define CALL_FUNC(name, ...) \
  13. ((name##_func_type)get_func(#name))(dummy_exec_env.get(), ##__VA_ARGS__)
  14. extern "C" {
  15. extern uint32_t
  16. get_libc_builtin_export_apis(NativeSymbol **p_libc_builtin_apis);
  17. extern bool
  18. wasm_native_lookup_libc_builtin_global(const char *module_name,
  19. const char *global_name,
  20. WASMGlobalImport *global);
  21. }
  22. class LibcBuiltinTest : public testing::Test
  23. {
  24. protected:
  25. // You should make the members protected s.t. they can be
  26. // accessed from sub-classes.
  27. // virtual void SetUp() will be called before each test is run. You
  28. // should define it if you need to initialize the varaibles.
  29. // Otherwise, this can be skipped.
  30. virtual void SetUp()
  31. {
  32. n_native_symbols = get_libc_builtin_export_apis(&native_symbols);
  33. }
  34. // virtual void TearDown() will be called after each test is run.
  35. // You should define it if there is cleanup work to do. Otherwise,
  36. // you don't have to provide it.
  37. //
  38. virtual void TearDown() {}
  39. public:
  40. WAMRRuntimeRAII<512 * 1024> runtime;
  41. DummyExecEnv dummy_exec_env;
  42. static NativeSymbol *native_symbols;
  43. static uint32_t n_native_symbols;
  44. static void *get_func(const char *name)
  45. {
  46. int32_t i;
  47. for (i = 0; i < n_native_symbols; i++) {
  48. if (strcmp(native_symbols[i].symbol, name) == 0) {
  49. return native_symbols[i].func_ptr;
  50. }
  51. }
  52. return NULL;
  53. }
  54. };
  55. NativeSymbol *LibcBuiltinTest::native_symbols;
  56. uint32_t LibcBuiltinTest::n_native_symbols;
  57. static const char very_long_string[] =
  58. R"(2mwa9vxDhuuvO47XePZvc4DAMdR8dzgKrmRNAM3qVoedFhG7GYyhlC4JiuSdrw8G
  59. 7vrPoCLGlVlGwMw7ATDL3bA5Filds8krTxS7h8ioq6CY4UmKl1zjHlmnOYRO3Wmp
  60. ylp21RrG8LzfHFerFyKFxA1GB93OuTFcasO2n9uQljCx8h5KRolbvjdHVnado4B6
  61. 3zNV990V7T7LIJHwZKb0RGg0fFo4GQd6Mfdl6aD3UlpKBIxjbonyeaQBY7hPZB8R
  62. J1JV5iw2PWB2BJEGoGhTvlc0a9FxmeqWIjpnU3yNEg2lD3NjZU627pTFcoAy5GCz
  63. wDyF5QzcvtAgWBR95kRpDtV21CRyQ6HteorX1aHemoMYWOLIvX52stUTAnOImMD8
  64. tIw6xwkOZx5fs3x9m540pPnRDiihLn2XuQ1PLPwA6orWOGm3dBKthqsycTqaIl0L
  65. 0gpycKbVYFHmakfgEyP9fyMziLT11B6EPzomHQAYgTVUdDl9u63P6sQCeaPwAYsY
  66. gus28uK9YYjpXgOOziG8ocBddvids1iLJLdbiAqKyHaVY4IBLVWU3F74tKGF7TeI
  67. DGAfvpzHls19VM9bKReBfCmDgbib7mCpYEFAQCmu5my0C8QrJlUoOgiljIO0x3sH
  68. ByNf4k9OfhzYi1V4cvDnMELVrk0fyZWmIxDvig7nfzI57OltT28pughPBlLxTn8X
  69. xyMNVYn1dD6Wpp7sqOBjxWGWmdrjleyin0iQ05UbfioHazvLKHtDfm5P2WwVejm6)";
  70. TEST_F(LibcBuiltinTest, puts)
  71. {
  72. char ll_string[2048];
  73. /* Capture the stdout */
  74. testing::internal::CaptureStdout();
  75. EXPECT_EQ(CALL_FUNC(puts, "Hello Wrold"), strlen("Hello Wrold\n"));
  76. EXPECT_EQ(testing::internal::GetCapturedStdout(), "Hello Wrold\n");
  77. testing::internal::CaptureStdout();
  78. EXPECT_EQ(CALL_FUNC(puts, "c"), strlen("c\n"));
  79. EXPECT_EQ(testing::internal::GetCapturedStdout(), "c\n");
  80. testing::internal::CaptureStdout();
  81. EXPECT_EQ(CALL_FUNC(puts, very_long_string), strlen(very_long_string) + 1);
  82. EXPECT_EQ(testing::internal::GetCapturedStdout(),
  83. std::string(very_long_string) + "\n");
  84. memset(ll_string, 0xAA, sizeof(ll_string));
  85. testing::internal::CaptureStdout();
  86. EXPECT_EQ(CALL_FUNC(puts, ll_string), strlen(ll_string) + 1);
  87. EXPECT_EQ(testing::internal::GetCapturedStdout(),
  88. std::string(ll_string) + "\n");
  89. }
  90. TEST_F(LibcBuiltinTest, printf)
  91. {
  92. WAMRVaList empty_va_list(dummy_exec_env.get());
  93. /* Capture the stdout */
  94. testing::internal::CaptureStdout();
  95. EXPECT_EQ(CALL_FUNC(printf, "Hello Wrold", empty_va_list.get()),
  96. strlen("Hello Wrold"));
  97. EXPECT_EQ(testing::internal::GetCapturedStdout(), "Hello Wrold");
  98. testing::internal::CaptureStdout();
  99. EXPECT_EQ(CALL_FUNC(printf, "c", empty_va_list.get()), strlen("c"));
  100. EXPECT_EQ(testing::internal::GetCapturedStdout(), "c");
  101. testing::internal::CaptureStdout();
  102. EXPECT_EQ(CALL_FUNC(printf, very_long_string, empty_va_list.get()),
  103. strlen(very_long_string));
  104. EXPECT_EQ(testing::internal::GetCapturedStdout(), very_long_string);
  105. /* type */
  106. {
  107. /* Construct a va_list to call printf */
  108. WAMRVaList va_list(dummy_exec_env.get());
  109. va_list.add(20); //%d 20
  110. va_list.add(65); //%i 65
  111. va_list.add(10); //%O 12
  112. va_list.add(10); //%u 10
  113. va_list.add(255); //%x ff
  114. va_list.add(255); //%X FF
  115. va_list.add(3.14); //%f 3.14
  116. va_list.add(3.14); //%F 3.14
  117. va_list.add(0.000001); //%e 1.000000e-06
  118. va_list.add(0.000001); //%E 1.000000E-06
  119. va_list.add(0.000001); //%g 1e-06
  120. va_list.add(0.000001); //%G 1E-06
  121. va_list.add("Hello World"); //%s Hello World
  122. testing::internal::CaptureStdout();
  123. /* clang-format off */
  124. EXPECT_EQ(CALL_FUNC(printf, "%d, %i, %o, %u, %x, %X, %f, %F, %e, %E, %g, %G, %s", va_list.get()), 97);
  125. EXPECT_EQ(testing::internal::GetCapturedStdout(),
  126. "20, 65, 12, 10, ff, FF, 3.140000, 3.140000, 1.000000e-06, 1.000000E-06, 1e-06, 1E-06, Hello World");
  127. /* clang-format on */
  128. }
  129. /* %c */
  130. {
  131. /* Construct a va_list to call printf */
  132. WAMRVaList va_list(dummy_exec_env.get());
  133. va_list.add('C'); //%c C
  134. // va_list.add("Hello"); //%p
  135. testing::internal::CaptureStdout();
  136. EXPECT_EQ(CALL_FUNC(printf, "%c", va_list.get()), 1);
  137. EXPECT_EQ(testing::internal::GetCapturedStdout(), "C");
  138. }
  139. /* %p */
  140. {
  141. /* Construct a va_list to call printf */
  142. WAMRVaList va_list(dummy_exec_env.get());
  143. va_list.add("Hello");
  144. testing::internal::CaptureStdout();
  145. EXPECT_EQ(CALL_FUNC(printf, "%p", va_list.get()), 7);
  146. EXPECT_EQ(testing::internal::GetCapturedStdout(), "0x200a8");
  147. }
  148. {
  149. /* clang-format off */
  150. /* Construct a va_list to call printf */
  151. WAMRVaList va_list(dummy_exec_env.get());
  152. va_list.add(20); //%td
  153. va_list.add(20); //%zd
  154. va_list.add(20); //%ld
  155. va_list.add(20L); //%jd
  156. testing::internal::CaptureStdout();
  157. EXPECT_EQ(CALL_FUNC(printf, "%td, %zd, %ld, %jd", va_list.get()), 14);
  158. EXPECT_EQ(testing::internal::GetCapturedStdout(),
  159. "20, 20, 20, 20");
  160. /* clang-format on */
  161. }
  162. /* %% */
  163. {
  164. /* Construct a va_list to call printf */
  165. WAMRVaList va_list(dummy_exec_env.get());
  166. EXPECT_TRUE(CALL_FUNC(printf, "%%", va_list.get()));
  167. }
  168. /* %n */
  169. {
  170. /* Construct a va_list to call printf */
  171. WAMRVaList empty_va_list(dummy_exec_env.get());
  172. /* Capture the stdout */
  173. testing::internal::CaptureStdout();
  174. CALL_FUNC(printf, "0123%n", empty_va_list.get());
  175. EXPECT_EQ(testing::internal::GetCapturedStdout(), "0123");
  176. }
  177. /* flag */
  178. {
  179. /* Construct a va_list to call printf */
  180. WAMRVaList va_list(dummy_exec_env.get());
  181. /*%-*/
  182. va_list.add(20); //%-d 20
  183. va_list.add(20); //%d 20
  184. /*%+*/
  185. va_list.add(20); //%+d +20
  186. va_list.add(-20); //%+d -20
  187. /*% */
  188. va_list.add(20); //% d 20
  189. va_list.add(-20); //% d -20
  190. /*%#*/
  191. va_list.add(20); //%#o 024
  192. va_list.add(255); //%#x 0xff
  193. va_list.add(255); //%#X 0xFF
  194. va_list.add(3.14); //%#.f 3.
  195. va_list.add(3.14); //%#.lf 3.
  196. va_list.add(3.14); //%#.e 3.e+00
  197. va_list.add(3.14); //%#.E 3.E+00
  198. va_list.add(3.14); //%#.g 3.
  199. va_list.add(3.14); //%#.G 3.
  200. va_list.add(20); //%#.a %a
  201. va_list.add(20); //%#.A %A
  202. /*%0*/
  203. va_list.add(20); //%03d 020
  204. testing::internal::CaptureStdout();
  205. /* clang-format off */
  206. EXPECT_EQ(CALL_FUNC(printf, "%-d, %d, %+d, %+d, % d, % d, %#o, %#x, %#X, %#.f, %#.lf, %#.e, %#.E, %#.g, %#.G, %#.a, %#.A, %03d", va_list.get()), 88);
  207. EXPECT_EQ(testing::internal::GetCapturedStdout(),
  208. "20, 20, +20, -20, 20, -20, 024, 0xff, 0XFF, 3., 3., 3.e+00, 3.E+00, 3., 3., %a, %A, 020");
  209. /* clang-format on */
  210. }
  211. /* precision */
  212. {
  213. /* Construct a va_list to call printf */
  214. WAMRVaList va_list(dummy_exec_env.get());
  215. va_list.add(20); //%d 20
  216. va_list.add(20); //%.1d 20
  217. va_list.add(20); //%.2d 20
  218. va_list.add(20); //%.3d 020
  219. va_list.add(20); //%.4d 0020
  220. va_list.add(20); //%.5d 00020
  221. va_list.add(20); //%.6d 000020
  222. va_list.add(20); //%.7d 0000020
  223. va_list.add(20); //%.8d 00000020
  224. va_list.add(20); //%.9d 000000020
  225. testing::internal::CaptureStdout();
  226. /* clang-format off */
  227. EXPECT_EQ(CALL_FUNC(printf, "%d, %.1d, %.2d, %.3d, %.4d, %.5d, %.6d, %.7d, %.8d, %.9d", va_list.get()), 66);
  228. EXPECT_EQ(testing::internal::GetCapturedStdout(),
  229. "20, 20, 20, 020, 0020, 00020, 000020, 0000020, 00000020, 000000020");
  230. /* clang-format on */
  231. }
  232. /*length*/
  233. {
  234. /* clang-format off */
  235. /* Construct a va_list to call printf */
  236. WAMRVaList va_list(dummy_exec_env.get());
  237. va_list.add(0x7F); //%hhd 127 -char
  238. va_list.add(0xFF); //%hhu.1d 255 -unsiged char
  239. va_list.add(0x7FFF); //%hd 32767 -sing short int
  240. va_list.add(0xFFFF); //%hu 65535 -unsiged short
  241. va_list.add(0x7FFFFFFF); //%ld 2147483647 - sing long
  242. va_list.add(0xFFFFFFFF); //%lu 4294967295 -unsigned long
  243. va_list.add(0x7FFFFFFFFFFFFFFF); //%lld 9223372036854775807 sing long long
  244. va_list.add(0xFFFFFFFFFFFFFFFF);//%llu 18446744073709551615 unsiged long long
  245. testing::internal::CaptureStdout();
  246. EXPECT_EQ(CALL_FUNC(printf, "%hhd, %hhu, %hd, %hu, %ld, %lu, %lld, %llu", va_list.get()), 89);
  247. EXPECT_EQ(testing::internal::GetCapturedStdout(),
  248. "127, 255, 32767, 65535, 2147483647, 4294967295, 9223372036854775807, 18446744073709551615");
  249. /* clang-format on */
  250. }
  251. EXPECT_EQ(CALL_FUNC(printf, "Hello Wrold", 0), 0);
  252. EXPECT_STREQ(dummy_exec_env.get_exception(),
  253. "Exception: out of bounds memory access");
  254. dummy_exec_env.clear_exception();
  255. EXPECT_EQ(CALL_FUNC(printf, "Hello Wrold", NULL), 0);
  256. EXPECT_STREQ(dummy_exec_env.get_exception(),
  257. "Exception: out of bounds memory access");
  258. dummy_exec_env.clear_exception();
  259. EXPECT_EQ(CALL_FUNC(printf, "Hello Wrold", (char *)-1), 0);
  260. EXPECT_STREQ(dummy_exec_env.get_exception(),
  261. "Exception: out of bounds memory access");
  262. dummy_exec_env.clear_exception();
  263. EXPECT_EQ(CALL_FUNC(printf, (char *)-1, (char *)-1), 0);
  264. EXPECT_STREQ(dummy_exec_env.get_exception(),
  265. "Exception: out of bounds memory access");
  266. dummy_exec_env.clear_exception();
  267. }
  268. TEST_F(LibcBuiltinTest, sprintf)
  269. {
  270. const char *buf;
  271. const char *str = "Hello Wrold";
  272. const char *str_sig = "c";
  273. const char *str_f = "20, 3.140000, Hello World";
  274. const char *str_long = "eqwewerwerqwer34were"; // test ok
  275. // const char *str_long = "TDSFGAWE%#$TERFQ@$%$@!%$@!RS!$#@$%"
  276. // "WAWAAEWAFSDNGFUTKNZDAERQWYNZREWGHAH";
  277. // //fail
  278. WAMRVaList empty_va_list(dummy_exec_env.get());
  279. AppData buf_app{ dummy_exec_env.get(), buf };
  280. AppData str_app{ dummy_exec_env.get(), str };
  281. AppData str_sig_app{ dummy_exec_env.get(), str_sig };
  282. AppData str_long_app{ dummy_exec_env.get(), str_long };
  283. EXPECT_EQ(CALL_FUNC(sprintf, (char *)buf_app.get_native_addr(),
  284. (char *)str_app.get_native_addr(), 0),
  285. 0);
  286. EXPECT_EQ(CALL_FUNC(sprintf, (char *)buf_app.get_native_addr(),
  287. (char *)str_app.get_native_addr(), NULL),
  288. 0);
  289. EXPECT_EQ(CALL_FUNC(sprintf, (char *)buf_app.get_native_addr(),
  290. (char *)str_app.get_native_addr(), (char *)-1),
  291. 0);
  292. EXPECT_FALSE(CALL_FUNC(sprintf, (char *)-1,
  293. (char *)str_app.get_native_addr(),
  294. empty_va_list.get()));
  295. EXPECT_EQ(CALL_FUNC(sprintf, (char *)buf_app.get_native_addr(),
  296. (char *)str_app.get_native_addr(), empty_va_list.get()),
  297. strlen(str));
  298. EXPECT_EQ(CALL_FUNC(memcmp, buf_app.get_native_addr(),
  299. str_app.get_native_addr(), strlen(str)),
  300. 0);
  301. EXPECT_EQ(CALL_FUNC(sprintf, (char *)buf_app.get_native_addr(),
  302. (char *)str_sig_app.get_native_addr(),
  303. empty_va_list.get()),
  304. strlen(str_sig));
  305. EXPECT_EQ(CALL_FUNC(memcmp, buf_app.get_native_addr(),
  306. str_sig_app.get_native_addr(), strlen(str_sig)),
  307. 0);
  308. EXPECT_EQ(CALL_FUNC(sprintf, (char *)buf_app.get_native_addr(),
  309. (char *)str_long_app.get_native_addr(),
  310. empty_va_list.get()),
  311. strlen(str_long));
  312. EXPECT_EQ(CALL_FUNC(memcmp, buf_app.get_native_addr(),
  313. str_long_app.get_native_addr(), strlen(str_long)),
  314. 0);
  315. {
  316. /* Construct a va_list to call printf */
  317. WAMRVaList va_list(dummy_exec_env.get());
  318. va_list.add(20);
  319. va_list.add(3.14);
  320. va_list.add("Hello World");
  321. /* This is like printf("%d, %f, %s", 20, 3.14, "Hello World") */
  322. EXPECT_EQ(CALL_FUNC(sprintf, (char *)buf_app.get_native_addr(),
  323. "%d, %f, %s", va_list.get()),
  324. 25);
  325. EXPECT_EQ(CALL_FUNC(memcmp, buf_app.get_native_addr(), str_f, 25), 0);
  326. }
  327. }
  328. TEST_F(LibcBuiltinTest, snprintf)
  329. {
  330. char buf[1024];
  331. char buf1[10];
  332. WAMRVaList empty_va_list(dummy_exec_env.get());
  333. EXPECT_EQ(CALL_FUNC(snprintf, buf, strlen("Hello Wrold"), "Hello Wrold", 0),
  334. 0);
  335. EXPECT_EQ(
  336. CALL_FUNC(snprintf, buf, strlen("Hello Wrold"), "Hello Wrold", NULL),
  337. 0);
  338. EXPECT_EQ(CALL_FUNC(snprintf, buf, strlen("Hello Wrold"), "Hello Wrold",
  339. (char *)-1),
  340. 0);
  341. EXPECT_EQ(CALL_FUNC(snprintf, buf, strlen("Hello Wrold"), "Hello Wrold",
  342. empty_va_list.get()),
  343. strlen("Hello Wrold"));
  344. EXPECT_EQ(CALL_FUNC(memcmp, buf, "Hello Wrold", strlen("Hello Wrold")), 0);
  345. EXPECT_EQ(CALL_FUNC(snprintf, buf, strlen(very_long_string),
  346. very_long_string, empty_va_list.get()),
  347. strlen(very_long_string));
  348. {
  349. /* Construct a va_list to call printf */
  350. WAMRVaList va_list(dummy_exec_env.get());
  351. va_list.add(20);
  352. va_list.add(3.14);
  353. va_list.add("Hello World");
  354. EXPECT_EQ(CALL_FUNC(snprintf, buf, 25, "%d, %f, %s", va_list.get()),
  355. 25);
  356. }
  357. }
  358. TEST_F(LibcBuiltinTest, putchar)
  359. {
  360. char ch;
  361. for (ch = 'a'; ch <= 'z'; ch++)
  362. EXPECT_EQ(CALL_FUNC(putchar, ch), 1);
  363. for (ch = '0'; ch <= '9'; ch++)
  364. EXPECT_EQ(CALL_FUNC(putchar, ch), 1);
  365. for (ch = 0; ch < 127; ch++)
  366. EXPECT_EQ(CALL_FUNC(putchar, ch), 1);
  367. }
  368. TEST_F(LibcBuiltinTest, strdup)
  369. {
  370. const char *src = "Hello World!";
  371. AppData src_app{ dummy_exec_env.get(), src };
  372. /* exception */
  373. EXPECT_EQ(CALL_FUNC(strdup, NULL), 0);
  374. EXPECT_GE(CALL_FUNC(strdup, (char *)src_app.get_native_addr()), 0);
  375. }
  376. TEST_F(LibcBuiltinTest, _strdup)
  377. {
  378. const char *src = "Hello World!";
  379. AppData src_app{ dummy_exec_env.get(), src };
  380. /* exception */
  381. EXPECT_EQ(CALL_FUNC(_strdup, NULL), 0);
  382. EXPECT_GE(CALL_FUNC(_strdup, (char *)src_app.get_native_addr()), 0);
  383. }
  384. TEST_F(LibcBuiltinTest, memcmp)
  385. {
  386. const char *a = "aBcDeF";
  387. const char *b = "AbCdEf";
  388. const char *c = "aacdef";
  389. const char *d = "aBcDeF";
  390. AppData a_app{ dummy_exec_env.get(), a };
  391. AppData b_app{ dummy_exec_env.get(), b };
  392. AppData c_app{ dummy_exec_env.get(), c };
  393. AppData d_app{ dummy_exec_env.get(), d };
  394. /* exception */
  395. EXPECT_EQ(CALL_FUNC(memcmp, (void *)-1, d_app.get_native_addr(), 0), 0);
  396. EXPECT_STREQ(dummy_exec_env.get_exception(),
  397. "Exception: out of bounds memory access");
  398. dummy_exec_env.clear_exception();
  399. /* size = 0 */
  400. EXPECT_EQ(
  401. CALL_FUNC(memcmp, a_app.get_native_addr(), d_app.get_native_addr(), 0),
  402. 0);
  403. // /* s1>s2 */
  404. EXPECT_GT(CALL_FUNC(memcmp, a_app.get_native_addr(),
  405. b_app.get_native_addr(), strlen(a)),
  406. 0);
  407. // /* s1<s2 */
  408. EXPECT_LT(CALL_FUNC(memcmp, a_app.get_native_addr(),
  409. c_app.get_native_addr(), strlen(a)),
  410. 0);
  411. // /* s1=s2 */
  412. EXPECT_EQ(CALL_FUNC(memcmp, a_app.get_native_addr(),
  413. d_app.get_native_addr(), strlen(a)),
  414. 0);
  415. }
  416. TEST_F(LibcBuiltinTest, memcpy)
  417. {
  418. const char *src = "Hell World";
  419. char *dest;
  420. AppData src_app{ dummy_exec_env.get(), src };
  421. AppData dest_app{ dummy_exec_env.get(), dest };
  422. /* exception */
  423. EXPECT_EQ(CALL_FUNC(memcpy, (void *)-1, src_app.get_native_addr(), 0), 0);
  424. EXPECT_EQ(CALL_FUNC(memcpy, dest_app.get_native_addr(),
  425. src_app.get_native_addr(), 0),
  426. dest_app.get_app_addr());
  427. EXPECT_EQ(CALL_FUNC(memcpy, (void *)-1, src_app.get_native_addr(), 10), 0);
  428. EXPECT_STREQ(dummy_exec_env.get_exception(),
  429. "Exception: out of bounds memory access");
  430. dummy_exec_env.clear_exception();
  431. CALL_FUNC(memcpy, dest_app.get_native_addr(), src_app.get_native_addr(),
  432. strlen(src));
  433. EXPECT_EQ(CALL_FUNC(memcmp, dest_app.get_native_addr(),
  434. src_app.get_native_addr(), strlen(src)),
  435. 0);
  436. }
  437. TEST_F(LibcBuiltinTest, memmove)
  438. {
  439. const char *src = "Hell World";
  440. char *dest;
  441. AppData src_app{ dummy_exec_env.get(), src };
  442. AppData dest_app{ dummy_exec_env.get(), dest };
  443. /* exception */
  444. EXPECT_EQ(CALL_FUNC(memmove, (void *)-1, dest_app.get_native_addr(), 0), 0);
  445. EXPECT_EQ(CALL_FUNC(memmove, dest_app.get_native_addr(),
  446. src_app.get_native_addr(), 0),
  447. dest_app.get_app_addr());
  448. EXPECT_EQ(CALL_FUNC(memmove, (void *)-1, dest_app.get_native_addr(), 10),
  449. 0);
  450. EXPECT_STREQ(dummy_exec_env.get_exception(),
  451. "Exception: out of bounds memory access");
  452. dummy_exec_env.clear_exception();
  453. CALL_FUNC(memmove, dest_app.get_native_addr(), src_app.get_native_addr(),
  454. strlen(src));
  455. EXPECT_EQ(CALL_FUNC(memcmp, dest_app.get_native_addr(),
  456. src_app.get_native_addr(), strlen(src)),
  457. 0);
  458. }
  459. TEST_F(LibcBuiltinTest, memset)
  460. {
  461. const char *src = "Hello World!";
  462. AppData src_app{ dummy_exec_env.get(), src };
  463. /* exception */
  464. EXPECT_EQ(CALL_FUNC(memset, (void *)-1, 1, strlen(src)), 0);
  465. EXPECT_STREQ(dummy_exec_env.get_exception(),
  466. "Exception: out of bounds memory access");
  467. dummy_exec_env.clear_exception();
  468. EXPECT_EQ(CALL_FUNC(memcmp, src_app.get_native_addr(), "Hello World!",
  469. strlen(src)),
  470. 0);
  471. EXPECT_GE(CALL_FUNC(memset, src_app.get_native_addr(), '\0', strlen(src)),
  472. 0);
  473. EXPECT_EQ(CALL_FUNC(memcmp, src_app.get_native_addr(),
  474. "\0\0\0\0\0\0\0\0\0\0\0\0", strlen(src)),
  475. 0);
  476. }
  477. TEST_F(LibcBuiltinTest, strchr)
  478. {
  479. const char *src = "Hell World";
  480. unsigned int ch_existent = 'o';
  481. unsigned int ch_non_existent = '$';
  482. AppData src_app{ dummy_exec_env.get(), src };
  483. EXPECT_EQ(
  484. CALL_FUNC(strchr, (char *)src_app.get_native_addr(), ch_non_existent),
  485. 0);
  486. EXPECT_GE(CALL_FUNC(strchr, (char *)src_app.get_native_addr(), ch_existent),
  487. 0);
  488. }
  489. TEST_F(LibcBuiltinTest, strcmp)
  490. {
  491. const char *a = "Hello World!";
  492. const char *b = "hello World!";
  493. const char *c = "Hello World!";
  494. AppData a_app{ dummy_exec_env.get(), a };
  495. AppData b_app{ dummy_exec_env.get(), b };
  496. AppData c_app{ dummy_exec_env.get(), c };
  497. /*s1>s2*/
  498. EXPECT_GT(CALL_FUNC(strcmp, (char *)b_app.get_native_addr(),
  499. (char *)a_app.get_native_addr()),
  500. 0);
  501. /*s1<s2*/
  502. EXPECT_LT(CALL_FUNC(strcmp, (char *)a_app.get_native_addr(),
  503. (char *)b_app.get_native_addr()),
  504. 0);
  505. /*s1=s2*/
  506. EXPECT_EQ(CALL_FUNC(strcmp, (char *)a_app.get_native_addr(),
  507. (char *)c_app.get_native_addr()),
  508. 0);
  509. }
  510. TEST_F(LibcBuiltinTest, strncmp)
  511. {
  512. const char *a = "Hello World!";
  513. const char *b = "hello World!";
  514. const char *c = "Hello World!";
  515. AppData a_app{ dummy_exec_env.get(), a };
  516. AppData b_app{ dummy_exec_env.get(), b };
  517. AppData c_app{ dummy_exec_env.get(), c };
  518. /* exception */
  519. EXPECT_EQ(CALL_FUNC(strncmp, (char *)-1, (char *)a_app.get_native_addr(),
  520. strlen(a)),
  521. 0);
  522. EXPECT_STREQ(dummy_exec_env.get_exception(),
  523. "Exception: out of bounds memory access");
  524. dummy_exec_env.clear_exception();
  525. /* size = 0 */
  526. EXPECT_EQ(CALL_FUNC(strncmp, (char *)a_app.get_native_addr(),
  527. (char *)c_app.get_native_addr(), 0),
  528. 0);
  529. /*s1>s2*/
  530. EXPECT_GT(CALL_FUNC(strncmp, (char *)b_app.get_native_addr(),
  531. (char *)a_app.get_native_addr(), strlen(a)),
  532. 0);
  533. /*s1<s2*/
  534. EXPECT_LT(CALL_FUNC(strncmp, (char *)a_app.get_native_addr(),
  535. (char *)b_app.get_native_addr(), strlen(a)),
  536. 0);
  537. /*s1=s2*/
  538. EXPECT_EQ(CALL_FUNC(strncmp, (char *)a_app.get_native_addr(),
  539. (char *)c_app.get_native_addr(), strlen(a)),
  540. 0);
  541. /*s1>s2*/
  542. EXPECT_GT(CALL_FUNC(strncmp, (char *)b_app.get_native_addr(),
  543. (char *)a_app.get_native_addr(), 3),
  544. 0);
  545. /*s1<s2*/
  546. EXPECT_LT(CALL_FUNC(strncmp, (char *)a_app.get_native_addr(),
  547. (char *)b_app.get_native_addr(), 3),
  548. 0);
  549. /*s1=s2*/
  550. EXPECT_EQ(CALL_FUNC(strncmp, (char *)a_app.get_native_addr(),
  551. (char *)c_app.get_native_addr(), 3),
  552. 0);
  553. }
  554. TEST_F(LibcBuiltinTest, strcpy)
  555. {
  556. char *src = (char *)"Hello World!";
  557. char *dest;
  558. AppData src_app{ dummy_exec_env.get(), src };
  559. AppData dest_app{ dummy_exec_env.get(), dest };
  560. /* exception */
  561. EXPECT_EQ(CALL_FUNC(strcpy, (char *)-1, (char *)src_app.get_native_addr()),
  562. 0);
  563. EXPECT_STREQ(dummy_exec_env.get_exception(),
  564. "Exception: out of bounds memory access");
  565. dummy_exec_env.clear_exception();
  566. EXPECT_GE(CALL_FUNC(strcpy, (char *)dest_app.get_native_addr(),
  567. (char *)src_app.get_native_addr()),
  568. 0);
  569. EXPECT_EQ(CALL_FUNC(memcmp, dest_app.get_native_addr(),
  570. (char *)src_app.get_native_addr(), strlen(src)),
  571. 0);
  572. }
  573. TEST_F(LibcBuiltinTest, strncpy)
  574. {
  575. char *src = (char *)"Hello World!";
  576. char *dest;
  577. AppData src_app{ dummy_exec_env.get(), src };
  578. AppData dest_app{ dummy_exec_env.get(), dest };
  579. /* exception */
  580. EXPECT_EQ(CALL_FUNC(strncpy, (char *)-1, (char *)src_app.get_native_addr(),
  581. strlen(src)),
  582. 0);
  583. EXPECT_STREQ(dummy_exec_env.get_exception(),
  584. "Exception: out of bounds memory access");
  585. dummy_exec_env.clear_exception();
  586. EXPECT_GE(CALL_FUNC(strncpy, (char *)dest_app.get_native_addr(),
  587. (char *)src_app.get_native_addr(), strlen(src)),
  588. 0);
  589. EXPECT_EQ(CALL_FUNC(memcmp, (char *)dest_app.get_native_addr(),
  590. (char *)src_app.get_native_addr(), strlen(src)),
  591. 0);
  592. EXPECT_GE(CALL_FUNC(strncpy, (char *)dest_app.get_native_addr(),
  593. (char *)src_app.get_native_addr(), 3),
  594. 0);
  595. EXPECT_EQ(CALL_FUNC(memcmp, (char *)dest_app.get_native_addr(),
  596. (char *)src_app.get_native_addr(), 3),
  597. 0);
  598. }
  599. TEST_F(LibcBuiltinTest, strlen)
  600. {
  601. const char *src = "Hello World!";
  602. AppData src_app{ dummy_exec_env.get(), src };
  603. EXPECT_EQ(CALL_FUNC(strlen, (char *)src_app.get_native_addr()), 12);
  604. }
  605. TEST_F(LibcBuiltinTest, malloc)
  606. {
  607. EXPECT_GT(CALL_FUNC(malloc, 0), 0);
  608. EXPECT_GT(CALL_FUNC(malloc, 100), 0);
  609. EXPECT_EQ(CALL_FUNC(malloc, 0xFFFFFFFF), 0);
  610. }
  611. TEST_F(LibcBuiltinTest, calloc)
  612. {
  613. /* nmemb = 1 size=0xffffffff total_size >= UINT32_MAX */
  614. EXPECT_EQ(CALL_FUNC(calloc, 1, 0xffffffff), 0);
  615. /* nmemb = 1 size=0xffffffff-1 total_size >= UINT32_MAX-1 */
  616. EXPECT_EQ(CALL_FUNC(calloc, 1, (0xffffffff - 1)), 0);
  617. /* nmemb = 1 size = 0 total_size = 0 */
  618. /* According to Linux man page:
  619. If nmemb or size is 0, then calloc() returns either NULL, or a unique
  620. pointer value that can later be successfully passed to free() */
  621. EXPECT_GE(CALL_FUNC(calloc, 1, 0), 0);
  622. /* nmemb = 10 size = 1024 total_size < UINT32_MAX */
  623. EXPECT_GT(CALL_FUNC(calloc, 10, 1024), 0);
  624. }
  625. TEST_F(LibcBuiltinTest, realloc)
  626. {
  627. unsigned int ptr = 0;
  628. // ptr = 0;
  629. // EXPECT_EQ(CALL_FUNC(realloc, ptr, 1024), 0);
  630. // ptr = 1;
  631. // EXPECT_GT(CALL_FUNC(realloc, ptr, 1024), 0);
  632. // ptr = 3;
  633. // EXPECT_GT(CALL_FUNC(realloc, ptr, 1024), 0);
  634. /* If ptr is NULL, then the call is equivalent to malloc(size), for all
  635. * values of size */
  636. ptr = CALL_FUNC(realloc, ptr, 1024);
  637. EXPECT_EQ(ptr, ptr);
  638. EXPECT_EQ(CALL_FUNC(realloc, ptr, 10), ptr);
  639. EXPECT_EQ(CALL_FUNC(realloc, ptr, 15), ptr);
  640. ptr = CALL_FUNC(realloc, ptr, 2048);
  641. EXPECT_EQ(ptr, ptr);
  642. /* If size is equal to zero, and ptr is not NULL, then
  643. the call is equivalent to free(ptr) */
  644. CALL_FUNC(realloc, ptr, 0);
  645. }
  646. TEST_F(LibcBuiltinTest, free)
  647. {
  648. const char *src;
  649. const char *s = "Hello World!";
  650. AppMemory src_mem{ dummy_exec_env.get(), 15 };
  651. AppData s_app{ dummy_exec_env.get(), s };
  652. CALL_FUNC(free, (char *)0xFFFFFFFF);
  653. EXPECT_STREQ(dummy_exec_env.get_exception(),
  654. "Exception: out of bounds memory access");
  655. dummy_exec_env.clear_exception();
  656. CALL_FUNC(free, (char *)-1);
  657. EXPECT_STREQ(dummy_exec_env.get_exception(),
  658. "Exception: out of bounds memory access");
  659. dummy_exec_env.clear_exception();
  660. CALL_FUNC(free, NULL);
  661. EXPECT_STREQ(dummy_exec_env.get_exception(),
  662. "Exception: out of bounds memory access");
  663. dummy_exec_env.clear_exception();
  664. memset((char *)src_mem.get_native_addr(), '\0', 15);
  665. strcpy((char *)src_mem.get_native_addr(), (char *)s_app.get_native_addr());
  666. EXPECT_EQ(CALL_FUNC(memcmp, (char *)src_mem.get_native_addr(),
  667. s_app.get_native_addr(), 15),
  668. 0);
  669. /* free */
  670. CALL_FUNC(free, (char *)src_mem.get_native_addr());
  671. EXPECT_NE(CALL_FUNC(memcmp, (char *)src_mem.get_native_addr(),
  672. s_app.get_native_addr(), 15),
  673. 0);
  674. }
  675. TEST_F(LibcBuiltinTest, atoi)
  676. {
  677. char *src = (char *)"123";
  678. char *src1 = (char *)"-123";
  679. AppData src_app{ dummy_exec_env.get(), src };
  680. AppData src1_app{ dummy_exec_env.get(), src1 };
  681. EXPECT_EQ(CALL_FUNC(atoi, (char *)src_app.get_native_addr()), 123);
  682. EXPECT_EQ(CALL_FUNC(atoi, (char *)src1_app.get_native_addr()), -123);
  683. }
  684. TEST_F(LibcBuiltinTest, exit)
  685. {
  686. CALL_FUNC(exit, 3);
  687. EXPECT_STREQ(dummy_exec_env.get_exception(), "Exception: env.exit(3)");
  688. dummy_exec_env.clear_exception();
  689. }
  690. TEST_F(LibcBuiltinTest, strtol)
  691. {
  692. char str[20] = "20";
  693. char str1[20] = "-20";
  694. char buffer[20] = "0x31";
  695. char buffer1[20] = "10379cend$3";
  696. char *ptr;
  697. AppData src_app{ dummy_exec_env.get(), str };
  698. AppData src1_app{ dummy_exec_env.get(), str1 };
  699. AppData buffer_app{ dummy_exec_env.get(), buffer };
  700. AppData buffer1_app{ dummy_exec_env.get(), buffer1 };
  701. AppMemory ptr_app{ dummy_exec_env.get(), 20 };
  702. CALL_FUNC(strtol, (char *)src_app.get_native_addr(), NULL, 10);
  703. EXPECT_STREQ(dummy_exec_env.get_exception(),
  704. "Exception: out of bounds memory access");
  705. dummy_exec_env.clear_exception();
  706. CALL_FUNC(strtol, (char *)src_app.get_native_addr(), &ptr, 10);
  707. EXPECT_STREQ(dummy_exec_env.get_exception(),
  708. "Exception: out of bounds memory access");
  709. dummy_exec_env.clear_exception();
  710. EXPECT_EQ(CALL_FUNC(strtol, (char *)src_app.get_native_addr(), NULL, 2), 0);
  711. EXPECT_EQ(CALL_FUNC(strtol, (char *)src_app.get_native_addr(), NULL, 8), 0);
  712. EXPECT_EQ(CALL_FUNC(strtol, (char *)src_app.get_native_addr(), NULL, 10),
  713. 0);
  714. EXPECT_EQ(CALL_FUNC(strtol, (char *)src_app.get_native_addr(), NULL, 16),
  715. 0);
  716. EXPECT_EQ(CALL_FUNC(strtol, (char *)src_app.get_native_addr(), NULL, 32),
  717. 0);
  718. EXPECT_EQ(CALL_FUNC(strtol, (char *)src_app.get_native_addr(),
  719. (char **)ptr_app.get_native_addr(), 2),
  720. 0);
  721. EXPECT_EQ(CALL_FUNC(strtol, (char *)src_app.get_native_addr(),
  722. (char **)ptr_app.get_native_addr(), 8),
  723. 16);
  724. EXPECT_EQ(CALL_FUNC(strtol, (char *)src_app.get_native_addr(),
  725. (char **)ptr_app.get_native_addr(), 10),
  726. 20);
  727. EXPECT_EQ(CALL_FUNC(strtol, (char *)src_app.get_native_addr(),
  728. (char **)ptr_app.get_native_addr(), 16),
  729. 32);
  730. EXPECT_EQ(CALL_FUNC(strtol, (char *)src_app.get_native_addr(),
  731. (char **)ptr_app.get_native_addr(), 32),
  732. 64);
  733. EXPECT_EQ(CALL_FUNC(strtol, (char *)src1_app.get_native_addr(),
  734. (char **)ptr_app.get_native_addr(), 2),
  735. 0);
  736. EXPECT_EQ(CALL_FUNC(strtol, (char *)src1_app.get_native_addr(),
  737. (char **)ptr_app.get_native_addr(), 8),
  738. -16);
  739. EXPECT_EQ(CALL_FUNC(strtol, (char *)src1_app.get_native_addr(),
  740. (char **)ptr_app.get_native_addr(), 10),
  741. -20);
  742. EXPECT_EQ(CALL_FUNC(strtol, (char *)src1_app.get_native_addr(),
  743. (char **)ptr_app.get_native_addr(), 16),
  744. -32);
  745. EXPECT_EQ(CALL_FUNC(strtol, (char *)src1_app.get_native_addr(),
  746. (char **)ptr_app.get_native_addr(), 32),
  747. -64);
  748. EXPECT_EQ(CALL_FUNC(strtol, (char *)buffer_app.get_native_addr(),
  749. (char **)ptr_app.get_native_addr(), 0),
  750. 49);
  751. EXPECT_EQ(CALL_FUNC(strtol, (char *)buffer_app.get_native_addr(),
  752. (char **)ptr_app.get_native_addr(), 16),
  753. 49);
  754. EXPECT_EQ(CALL_FUNC(strtol, (char *)buffer_app.get_native_addr(),
  755. (char **)ptr_app.get_native_addr(), 10),
  756. 0);
  757. EXPECT_EQ(CALL_FUNC(strtol, (char *)buffer1_app.get_native_addr(),
  758. (char **)ptr_app.get_native_addr(), 10),
  759. 10379);
  760. // EXPECT_STREQ((char *)ptr_app.get_native_addr(), "cend$3");
  761. uint32_t str_app_addr = *(uint32_t *)ptr_app.get_native_addr();
  762. EXPECT_GT(str_app_addr, 0);
  763. char *str_native_addr = (char *)dummy_exec_env.app_to_native(str_app_addr);
  764. EXPECT_NE(str_native_addr, nullptr);
  765. EXPECT_STREQ(str_native_addr, "cend$3");
  766. }
  767. TEST_F(LibcBuiltinTest, strtoul)
  768. {
  769. char str[20] = "20";
  770. char buffer[20] = "0x31";
  771. char buffer1[20] = "10379cend$3";
  772. char *ptr;
  773. AppData src_app{ dummy_exec_env.get(), str };
  774. AppData buffer_app{ dummy_exec_env.get(), buffer };
  775. AppData buffer1_app{ dummy_exec_env.get(), buffer1 };
  776. AppMemory ptr_app{ dummy_exec_env.get(), 20 };
  777. CALL_FUNC(strtoul, (char *)src_app.get_native_addr(), NULL, 10);
  778. EXPECT_STREQ(dummy_exec_env.get_exception(),
  779. "Exception: out of bounds memory access");
  780. dummy_exec_env.clear_exception();
  781. CALL_FUNC(strtoul, (char *)src_app.get_native_addr(), &ptr, 10);
  782. EXPECT_STREQ(dummy_exec_env.get_exception(),
  783. "Exception: out of bounds memory access");
  784. dummy_exec_env.clear_exception();
  785. EXPECT_EQ(CALL_FUNC(strtoul, (char *)src_app.get_native_addr(), NULL, 2),
  786. 0);
  787. EXPECT_EQ(CALL_FUNC(strtoul, (char *)src_app.get_native_addr(), NULL, 8),
  788. 0);
  789. EXPECT_EQ(CALL_FUNC(strtoul, (char *)src_app.get_native_addr(), NULL, 10),
  790. 0);
  791. EXPECT_EQ(CALL_FUNC(strtoul, (char *)src_app.get_native_addr(), NULL, 16),
  792. 0);
  793. EXPECT_EQ(CALL_FUNC(strtoul, (char *)src_app.get_native_addr(), NULL, 32),
  794. 0);
  795. EXPECT_EQ(CALL_FUNC(strtoul, (char *)src_app.get_native_addr(),
  796. (char **)ptr_app.get_native_addr(), 2),
  797. 0);
  798. EXPECT_EQ(CALL_FUNC(strtoul, (char *)src_app.get_native_addr(),
  799. (char **)ptr_app.get_native_addr(), 8),
  800. 16);
  801. EXPECT_EQ(CALL_FUNC(strtoul, (char *)src_app.get_native_addr(),
  802. (char **)ptr_app.get_native_addr(), 10),
  803. 20);
  804. EXPECT_EQ(CALL_FUNC(strtoul, (char *)src_app.get_native_addr(),
  805. (char **)ptr_app.get_native_addr(), 16),
  806. 32);
  807. EXPECT_EQ(CALL_FUNC(strtoul, (char *)src_app.get_native_addr(),
  808. (char **)ptr_app.get_native_addr(), 32),
  809. 64);
  810. EXPECT_EQ(CALL_FUNC(strtoul, (char *)buffer_app.get_native_addr(),
  811. (char **)ptr_app.get_native_addr(), 0),
  812. 49);
  813. EXPECT_EQ(CALL_FUNC(strtoul, (char *)buffer_app.get_native_addr(),
  814. (char **)ptr_app.get_native_addr(), 16),
  815. 49);
  816. EXPECT_EQ(CALL_FUNC(strtoul, (char *)buffer_app.get_native_addr(),
  817. (char **)ptr_app.get_native_addr(), 10),
  818. 0);
  819. EXPECT_EQ(CALL_FUNC(strtoul, (char *)buffer1_app.get_native_addr(),
  820. (char **)ptr_app.get_native_addr(), 10),
  821. 10379);
  822. uint32_t str_app_addr = *(uint32_t *)ptr_app.get_native_addr();
  823. EXPECT_GT(str_app_addr, 0);
  824. char *str_native_addr = (char *)dummy_exec_env.app_to_native(str_app_addr);
  825. EXPECT_NE(str_native_addr, nullptr);
  826. EXPECT_STREQ(str_native_addr, "cend$3");
  827. }
  828. TEST_F(LibcBuiltinTest, memchr)
  829. {
  830. const char src[] = "Hello World.";
  831. char ch = 'o';
  832. AppData src_app{ dummy_exec_env.get(), src };
  833. /* exception */
  834. EXPECT_EQ(CALL_FUNC(memchr, (char *)-1, ch, strlen(src)), 0);
  835. EXPECT_STREQ(dummy_exec_env.get_exception(),
  836. "Exception: out of bounds memory access");
  837. dummy_exec_env.clear_exception();
  838. EXPECT_GE(CALL_FUNC(memchr, src_app.get_native_addr(), ch, strlen(src)), 0);
  839. }
  840. TEST_F(LibcBuiltinTest, strncasecmp)
  841. {
  842. const char *src1 = "Hello World.";
  843. const char *src2 = "hel";
  844. const char *src3 = "HELLO WORLD.";
  845. AppData src1_app{ dummy_exec_env.get(), src1 };
  846. AppData src2_app{ dummy_exec_env.get(), src2 };
  847. AppData src3_app{ dummy_exec_env.get(), src3 };
  848. EXPECT_GT(CALL_FUNC(strncasecmp, (char *)src1_app.get_native_addr(),
  849. (char *)src2_app.get_native_addr(), 4),
  850. 0);
  851. EXPECT_LT(CALL_FUNC(strncasecmp, (char *)src2_app.get_native_addr(),
  852. (char *)src1_app.get_native_addr(), 4),
  853. 0);
  854. EXPECT_EQ(CALL_FUNC(strncasecmp, (char *)src1_app.get_native_addr(),
  855. (char *)src3_app.get_native_addr(), strlen(src1)),
  856. 0);
  857. }
  858. TEST_F(LibcBuiltinTest, strspn)
  859. {
  860. const char *src1 = "Hello world!";
  861. const char *src2 = "abcd";
  862. const char *src3 = "l";
  863. const char *src4 = "Hell";
  864. const char *src5 = "Helo";
  865. AppData src1_app{ dummy_exec_env.get(), src1 };
  866. AppData src2_app{ dummy_exec_env.get(), src2 };
  867. AppData src3_app{ dummy_exec_env.get(), src3 };
  868. AppData src4_app{ dummy_exec_env.get(), src4 };
  869. AppData src5_app{ dummy_exec_env.get(), src5 };
  870. EXPECT_EQ(CALL_FUNC(strspn, (char *)src1_app.get_native_addr(),
  871. (char *)src2_app.get_native_addr()),
  872. 0);
  873. EXPECT_EQ(CALL_FUNC(strspn, (char *)src1_app.get_native_addr(),
  874. (char *)src3_app.get_native_addr()),
  875. 0);
  876. EXPECT_EQ(CALL_FUNC(strspn, (char *)src1_app.get_native_addr(),
  877. (char *)src4_app.get_native_addr()),
  878. 4);
  879. EXPECT_EQ(CALL_FUNC(strspn, (char *)src1_app.get_native_addr(),
  880. (char *)src5_app.get_native_addr()),
  881. 5);
  882. }
  883. TEST_F(LibcBuiltinTest, strcspn)
  884. {
  885. const char *src1 = "Hello world!";
  886. const char *src2 = ".?";
  887. const char *src3 = "llo";
  888. const char *src4 = "http://www.baidu.com/";
  889. const char *src5 = "?.,:\"\'-!";
  890. AppData src1_app{ dummy_exec_env.get(), src1 };
  891. AppData src2_app{ dummy_exec_env.get(), src2 };
  892. AppData src3_app{ dummy_exec_env.get(), src3 };
  893. AppData src4_app{ dummy_exec_env.get(), src4 };
  894. AppData src5_app{ dummy_exec_env.get(), src5 };
  895. EXPECT_EQ(CALL_FUNC(strcspn, (char *)src1_app.get_native_addr(),
  896. (char *)src2_app.get_native_addr()),
  897. 12);
  898. EXPECT_EQ(CALL_FUNC(strcspn, (char *)src1_app.get_native_addr(),
  899. (char *)src3_app.get_native_addr()),
  900. 2);
  901. EXPECT_EQ(CALL_FUNC(strcspn, (char *)src4_app.get_native_addr(),
  902. (char *)src5_app.get_native_addr()),
  903. 4);
  904. }
  905. TEST_F(LibcBuiltinTest, strstr)
  906. {
  907. const char *src1 = "Hello world!";
  908. const char *src2 = "abcd";
  909. const char *src3 = "Hello";
  910. const char *src4 = "H";
  911. const char *src5 = "llo";
  912. AppData src1_app{ dummy_exec_env.get(), src1 };
  913. AppData src2_app{ dummy_exec_env.get(), src2 };
  914. AppData src3_app{ dummy_exec_env.get(), src3 };
  915. AppData src4_app{ dummy_exec_env.get(), src4 };
  916. AppData src5_app{ dummy_exec_env.get(), src5 };
  917. EXPECT_EQ(CALL_FUNC(strstr, (char *)src1_app.get_native_addr(),
  918. (char *)src2_app.get_native_addr()),
  919. 0);
  920. EXPECT_EQ(CALL_FUNC(strstr, (char *)src3_app.get_native_addr(),
  921. (char *)src1_app.get_native_addr()),
  922. 0);
  923. EXPECT_GT(CALL_FUNC(strstr, (char *)src1_app.get_native_addr(),
  924. (char *)src4_app.get_native_addr()),
  925. 0);
  926. EXPECT_GT(CALL_FUNC(strstr, (char *)src1_app.get_native_addr(),
  927. (char *)src5_app.get_native_addr()),
  928. 0);
  929. }
  930. TEST_F(LibcBuiltinTest, isupper)
  931. {
  932. EXPECT_FALSE(CALL_FUNC(isupper, 'a'));
  933. EXPECT_FALSE(CALL_FUNC(isupper, 97));
  934. EXPECT_FALSE(CALL_FUNC(isupper, '0'));
  935. EXPECT_FALSE(CALL_FUNC(isupper, '.'));
  936. EXPECT_TRUE(CALL_FUNC(isupper, 'A'));
  937. EXPECT_TRUE(CALL_FUNC(isupper, 65));
  938. }
  939. TEST_F(LibcBuiltinTest, isalpha)
  940. {
  941. EXPECT_FALSE(CALL_FUNC(isalpha, '0'));
  942. EXPECT_FALSE(CALL_FUNC(isalpha, 0));
  943. EXPECT_FALSE(CALL_FUNC(isalpha, '?'));
  944. EXPECT_TRUE(CALL_FUNC(isalpha, 'A'));
  945. EXPECT_TRUE(CALL_FUNC(isalpha, 'a'));
  946. }
  947. TEST_F(LibcBuiltinTest, isspace)
  948. {
  949. EXPECT_FALSE(CALL_FUNC(isspace, '0'));
  950. EXPECT_FALSE(CALL_FUNC(isspace, 0));
  951. EXPECT_FALSE(CALL_FUNC(isspace, '?'));
  952. EXPECT_TRUE(CALL_FUNC(isspace, ' '));
  953. EXPECT_TRUE(CALL_FUNC(isspace, '\t'));
  954. EXPECT_TRUE(CALL_FUNC(isspace, '\n'));
  955. EXPECT_TRUE(CALL_FUNC(isspace, '\v'));
  956. EXPECT_TRUE(CALL_FUNC(isspace, '\f'));
  957. EXPECT_TRUE(CALL_FUNC(isspace, '\r'));
  958. }
  959. TEST_F(LibcBuiltinTest, isgraph)
  960. {
  961. /* ASCII 0x00-0x20 */
  962. EXPECT_FALSE(CALL_FUNC(isgraph, 0x00));
  963. EXPECT_FALSE(CALL_FUNC(isgraph, 0x20));
  964. /* ASCII 0x7F */
  965. EXPECT_FALSE(CALL_FUNC(isgraph, 0x7F));
  966. EXPECT_FALSE(CALL_FUNC(isgraph, 0x80));
  967. /* ASCII 0x21-0x7E */
  968. EXPECT_TRUE(CALL_FUNC(isgraph, 0x21));
  969. EXPECT_TRUE(CALL_FUNC(isgraph, 0x7E));
  970. }
  971. TEST_F(LibcBuiltinTest, isprint)
  972. {
  973. /* ASCII 0x00-0x1F */
  974. EXPECT_FALSE(CALL_FUNC(isprint, 0x00));
  975. EXPECT_FALSE(CALL_FUNC(isprint, 0x1F));
  976. /* ASCII 0x7F */
  977. EXPECT_FALSE(CALL_FUNC(isprint, 0x7F));
  978. EXPECT_FALSE(CALL_FUNC(isprint, 0x80));
  979. /* ASCII 0x20-0x7E */
  980. EXPECT_TRUE(CALL_FUNC(isprint, 0x20));
  981. EXPECT_TRUE(CALL_FUNC(isprint, 0x7E));
  982. }
  983. TEST_F(LibcBuiltinTest, isdigit)
  984. {
  985. /* ASCII 0x00-0x2F */
  986. EXPECT_FALSE(CALL_FUNC(isdigit, 0x00));
  987. EXPECT_FALSE(CALL_FUNC(isdigit, 0x2F));
  988. /* ASCII 0x3A-0x7F */
  989. EXPECT_FALSE(CALL_FUNC(isdigit, 0x3A));
  990. EXPECT_FALSE(CALL_FUNC(isdigit, 0x7F));
  991. /* ASCII 0x30-0x39 */
  992. EXPECT_TRUE(CALL_FUNC(isdigit, 0x30));
  993. EXPECT_TRUE(CALL_FUNC(isdigit, 0x39));
  994. }
  995. TEST_F(LibcBuiltinTest, isxdigit)
  996. {
  997. char str[] = "-FFEE";
  998. char str1[] = "FFEE";
  999. EXPECT_FALSE(CALL_FUNC(isxdigit, str[0]));
  1000. EXPECT_TRUE(CALL_FUNC(isxdigit, str1[0]));
  1001. /* ASCII 0x00-0x2F */
  1002. EXPECT_FALSE(CALL_FUNC(isxdigit, 0x00));
  1003. EXPECT_FALSE(CALL_FUNC(isxdigit, 0x2F));
  1004. /* ASCII 0x3A-0x40 */
  1005. EXPECT_FALSE(CALL_FUNC(isxdigit, 0x3A));
  1006. EXPECT_FALSE(CALL_FUNC(isxdigit, 0x40));
  1007. /* ASCII 0x49-0x60 */
  1008. EXPECT_FALSE(CALL_FUNC(isxdigit, 0x49));
  1009. EXPECT_FALSE(CALL_FUNC(isxdigit, 0x60));
  1010. /* ASCII 0x67-0x7F */
  1011. EXPECT_FALSE(CALL_FUNC(isxdigit, 0x67));
  1012. EXPECT_FALSE(CALL_FUNC(isxdigit, 0x7F));
  1013. /* ASCII 0x30-0x39 */
  1014. EXPECT_TRUE(CALL_FUNC(isxdigit, 0x30));
  1015. EXPECT_TRUE(CALL_FUNC(isxdigit, 0x39));
  1016. /* ASCII 0x41-0x46 */
  1017. EXPECT_TRUE(CALL_FUNC(isxdigit, 0x41));
  1018. EXPECT_TRUE(CALL_FUNC(isxdigit, 0x46));
  1019. /* ASCII 0x61-0x66 */
  1020. EXPECT_TRUE(CALL_FUNC(isxdigit, 0x61));
  1021. EXPECT_TRUE(CALL_FUNC(isxdigit, 0x66));
  1022. }
  1023. TEST_F(LibcBuiltinTest, tolower)
  1024. {
  1025. char src[] = "aBcDeFgH12345;!#$";
  1026. char dest[sizeof(src)];
  1027. int i;
  1028. for (i = 0; i < sizeof(src); i++) {
  1029. dest[i] = CALL_FUNC(tolower, (src[i]));
  1030. }
  1031. EXPECT_STREQ(dest, "abcdefgh12345;!#$");
  1032. }
  1033. TEST_F(LibcBuiltinTest, toupper)
  1034. {
  1035. char src[] = "aBcDeFgH12345;!#$";
  1036. char dest[sizeof(src)];
  1037. int i;
  1038. for (i = 0; i < sizeof(src); i++) {
  1039. dest[i] = CALL_FUNC(toupper, (src[i]));
  1040. }
  1041. EXPECT_STREQ(dest, "ABCDEFGH12345;!#$");
  1042. }
  1043. TEST_F(LibcBuiltinTest, isalnum)
  1044. {
  1045. char src[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ012345678"
  1046. "9!\"#$%&'()*+,-./:;<=>?@[^_'{|}~";
  1047. int i;
  1048. int isalnum_cnt = 0;
  1049. for (i = 0; i < sizeof(src); i++) {
  1050. if (CALL_FUNC(isalnum, (src[i])))
  1051. isalnum_cnt++;
  1052. }
  1053. EXPECT_EQ(isalnum_cnt, 62);
  1054. }
  1055. TEST_F(LibcBuiltinTest, emscripten_memcpy_big)
  1056. {
  1057. const char *src = "Hell World";
  1058. char *dest;
  1059. AppData src_app{ dummy_exec_env.get(), src };
  1060. AppData dest_app{ dummy_exec_env.get(), dest };
  1061. /* exception */
  1062. EXPECT_EQ(CALL_FUNC(emscripten_memcpy_big, (void *)-1,
  1063. src_app.get_native_addr(), 0),
  1064. 0);
  1065. CALL_FUNC(emscripten_memcpy_big, dest_app.get_native_addr(),
  1066. src_app.get_native_addr(), strlen(src));
  1067. EXPECT_EQ(CALL_FUNC(memcmp, dest_app.get_native_addr(),
  1068. src_app.get_native_addr(), strlen(src)),
  1069. 0);
  1070. }
  1071. TEST_F(LibcBuiltinTest, abort)
  1072. {
  1073. CALL_FUNC(abort, 33);
  1074. EXPECT_STREQ(dummy_exec_env.get_exception(), "Exception: env.abort(33)");
  1075. dummy_exec_env.clear_exception();
  1076. }
  1077. TEST_F(LibcBuiltinTest, abortStackOverflow)
  1078. {
  1079. CALL_FUNC(abortStackOverflow, 33);
  1080. EXPECT_STREQ(dummy_exec_env.get_exception(),
  1081. "Exception: env.abortStackOverflow(33)");
  1082. dummy_exec_env.clear_exception();
  1083. }
  1084. TEST_F(LibcBuiltinTest, nullFunc_X)
  1085. {
  1086. CALL_FUNC(nullFunc_X, 33);
  1087. EXPECT_STREQ(dummy_exec_env.get_exception(),
  1088. "Exception: env.nullFunc_X(33)");
  1089. dummy_exec_env.clear_exception();
  1090. }
  1091. TEST_F(LibcBuiltinTest, __cxa_allocate_exception)
  1092. {
  1093. EXPECT_NE(CALL_FUNC(__cxa_allocate_exception, 0x0), 0x0);
  1094. EXPECT_EQ(CALL_FUNC(__cxa_allocate_exception, 0xFFFF), 0x0);
  1095. }
  1096. TEST_F(LibcBuiltinTest, __cxa_begin_catch)
  1097. {
  1098. /* 无函数原型 */
  1099. }
  1100. TEST_F(LibcBuiltinTest, __cxa_throw)
  1101. {
  1102. void *excepton;
  1103. void *tinfo;
  1104. CALL_FUNC(__cxa_throw, excepton, tinfo, 1);
  1105. EXPECT_STREQ(dummy_exec_env.get_exception(),
  1106. "Exception: exception thrown by stdc++");
  1107. dummy_exec_env.clear_exception();
  1108. }
  1109. struct timespec_app app;
  1110. TEST_F(LibcBuiltinTest, clock_gettime)
  1111. {
  1112. struct timespec_app *tsapp;
  1113. tsapp = &app;
  1114. AppMemory tsapp_app{ dummy_exec_env.get(), sizeof(struct timespec_app) };
  1115. /* exception */
  1116. EXPECT_EQ(CALL_FUNC(clock_gettime, 0, (struct timespec_app *)-1), -1);
  1117. EXPECT_EQ(CALL_FUNC(clock_gettime, 100, NULL), -1);
  1118. EXPECT_EQ(CALL_FUNC(clock_gettime, 100, 0), -1);
  1119. EXPECT_EQ(CALL_FUNC(clock_gettime, 10,
  1120. (struct timespec_app *)tsapp_app.get_native_addr()),
  1121. 0);
  1122. }
  1123. TEST_F(LibcBuiltinTest, clock)
  1124. {
  1125. EXPECT_GE(CALL_FUNC(clock), 0);
  1126. }
  1127. WASMGlobalImport glb;
  1128. TEST_F(LibcBuiltinTest, wasm_native_lookup_libc_builtin_global)
  1129. {
  1130. const char *module_name = "module name";
  1131. const char *global_name = "global name";
  1132. const char *module_name1 = "global";
  1133. const char *global_name1 = "NaN";
  1134. const char *global_name2 = "Infinity";
  1135. WASMGlobalImport *global = &glb;
  1136. EXPECT_FALSE(
  1137. wasm_native_lookup_libc_builtin_global(NULL, global_name, global));
  1138. EXPECT_FALSE(
  1139. wasm_native_lookup_libc_builtin_global(module_name, NULL, global));
  1140. EXPECT_FALSE(
  1141. wasm_native_lookup_libc_builtin_global(module_name, global_name, NULL));
  1142. EXPECT_FALSE(wasm_native_lookup_libc_builtin_global(module_name,
  1143. global_name, global));
  1144. EXPECT_FALSE(wasm_native_lookup_libc_builtin_global(module_name,
  1145. global_name1, global));
  1146. EXPECT_TRUE(wasm_native_lookup_libc_builtin_global(module_name1,
  1147. global_name1, global));
  1148. EXPECT_TRUE(wasm_native_lookup_libc_builtin_global(module_name1,
  1149. global_name2, global));
  1150. }