coverage.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include "py/obj.h"
  4. #include "py/objstr.h"
  5. #include "py/runtime.h"
  6. #include "py/gc.h"
  7. #include "py/repl.h"
  8. #include "py/mpz.h"
  9. #include "py/builtin.h"
  10. #include "py/emit.h"
  11. #include "py/formatfloat.h"
  12. #include "py/stream.h"
  13. #include "py/binary.h"
  14. #include "py/bc.h"
  15. #if defined(MICROPY_UNIX_COVERAGE)
  16. // stream testing object
  17. typedef struct _mp_obj_streamtest_t {
  18. mp_obj_base_t base;
  19. uint8_t *buf;
  20. size_t len;
  21. size_t pos;
  22. int error_code;
  23. } mp_obj_streamtest_t;
  24. STATIC mp_obj_t stest_set_buf(mp_obj_t o_in, mp_obj_t buf_in) {
  25. mp_obj_streamtest_t *o = MP_OBJ_TO_PTR(o_in);
  26. mp_buffer_info_t bufinfo;
  27. mp_get_buffer_raise(buf_in, &bufinfo, MP_BUFFER_READ);
  28. o->buf = m_new(uint8_t, bufinfo.len);
  29. memcpy(o->buf, bufinfo.buf, bufinfo.len);
  30. o->len = bufinfo.len;
  31. o->pos = 0;
  32. return mp_const_none;
  33. }
  34. STATIC MP_DEFINE_CONST_FUN_OBJ_2(stest_set_buf_obj, stest_set_buf);
  35. STATIC mp_obj_t stest_set_error(mp_obj_t o_in, mp_obj_t err_in) {
  36. mp_obj_streamtest_t *o = MP_OBJ_TO_PTR(o_in);
  37. o->error_code = mp_obj_get_int(err_in);
  38. return mp_const_none;
  39. }
  40. STATIC MP_DEFINE_CONST_FUN_OBJ_2(stest_set_error_obj, stest_set_error);
  41. STATIC mp_uint_t stest_read(mp_obj_t o_in, void *buf, mp_uint_t size, int *errcode) {
  42. mp_obj_streamtest_t *o = MP_OBJ_TO_PTR(o_in);
  43. if (o->pos < o->len) {
  44. if (size > o->len - o->pos) {
  45. size = o->len - o->pos;
  46. }
  47. memcpy(buf, o->buf + o->pos, size);
  48. o->pos += size;
  49. return size;
  50. } else if (o->error_code == 0) {
  51. return 0;
  52. } else {
  53. *errcode = o->error_code;
  54. return MP_STREAM_ERROR;
  55. }
  56. }
  57. STATIC mp_uint_t stest_write(mp_obj_t o_in, const void *buf, mp_uint_t size, int *errcode) {
  58. mp_obj_streamtest_t *o = MP_OBJ_TO_PTR(o_in);
  59. (void)buf;
  60. (void)size;
  61. *errcode = o->error_code;
  62. return MP_STREAM_ERROR;
  63. }
  64. STATIC mp_uint_t stest_ioctl(mp_obj_t o_in, mp_uint_t request, uintptr_t arg, int *errcode) {
  65. mp_obj_streamtest_t *o = MP_OBJ_TO_PTR(o_in);
  66. (void)arg;
  67. (void)request;
  68. (void)errcode;
  69. if (o->error_code != 0) {
  70. *errcode = o->error_code;
  71. return MP_STREAM_ERROR;
  72. }
  73. return 0;
  74. }
  75. STATIC const mp_rom_map_elem_t rawfile_locals_dict_table[] = {
  76. { MP_ROM_QSTR(MP_QSTR_set_buf), MP_ROM_PTR(&stest_set_buf_obj) },
  77. { MP_ROM_QSTR(MP_QSTR_set_error), MP_ROM_PTR(&stest_set_error_obj) },
  78. { MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&mp_stream_read_obj) },
  79. { MP_ROM_QSTR(MP_QSTR_read1), MP_ROM_PTR(&mp_stream_read1_obj) },
  80. { MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&mp_stream_write_obj) },
  81. { MP_ROM_QSTR(MP_QSTR_write1), MP_ROM_PTR(&mp_stream_write1_obj) },
  82. { MP_ROM_QSTR(MP_QSTR_readinto), MP_ROM_PTR(&mp_stream_readinto_obj) },
  83. { MP_ROM_QSTR(MP_QSTR_readline), MP_ROM_PTR(&mp_stream_unbuffered_readline_obj) },
  84. { MP_ROM_QSTR(MP_QSTR_ioctl), MP_ROM_PTR(&mp_stream_ioctl_obj) },
  85. };
  86. STATIC MP_DEFINE_CONST_DICT(rawfile_locals_dict, rawfile_locals_dict_table);
  87. STATIC const mp_stream_p_t fileio_stream_p = {
  88. .read = stest_read,
  89. .write = stest_write,
  90. .ioctl = stest_ioctl,
  91. };
  92. STATIC const mp_obj_type_t mp_type_stest_fileio = {
  93. { &mp_type_type },
  94. .protocol = &fileio_stream_p,
  95. .locals_dict = (mp_obj_dict_t*)&rawfile_locals_dict,
  96. };
  97. // stream read returns non-blocking error
  98. STATIC mp_uint_t stest_read2(mp_obj_t o_in, void *buf, mp_uint_t size, int *errcode) {
  99. (void)o_in;
  100. (void)buf;
  101. (void)size;
  102. *errcode = MP_EAGAIN;
  103. return MP_STREAM_ERROR;
  104. }
  105. STATIC const mp_rom_map_elem_t rawfile_locals_dict_table2[] = {
  106. { MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&mp_stream_read_obj) },
  107. };
  108. STATIC MP_DEFINE_CONST_DICT(rawfile_locals_dict2, rawfile_locals_dict_table2);
  109. STATIC const mp_stream_p_t textio_stream_p2 = {
  110. .read = stest_read2,
  111. .write = NULL,
  112. .is_text = true,
  113. };
  114. STATIC const mp_obj_type_t mp_type_stest_textio2 = {
  115. { &mp_type_type },
  116. .protocol = &textio_stream_p2,
  117. .locals_dict = (mp_obj_dict_t*)&rawfile_locals_dict2,
  118. };
  119. // str/bytes objects without a valid hash
  120. STATIC const mp_obj_str_t str_no_hash_obj = {{&mp_type_str}, 0, 10, (const byte*)"0123456789"};
  121. STATIC const mp_obj_str_t bytes_no_hash_obj = {{&mp_type_bytes}, 0, 10, (const byte*)"0123456789"};
  122. // function to run extra tests for things that can't be checked by scripts
  123. STATIC mp_obj_t extra_coverage(void) {
  124. // mp_printf (used by ports that don't have a native printf)
  125. {
  126. mp_printf(&mp_plat_print, "# mp_printf\n");
  127. mp_printf(&mp_plat_print, "%d %+d % d\n", -123, 123, 123); // sign
  128. mp_printf(&mp_plat_print, "%05d\n", -123); // negative number with zero padding
  129. mp_printf(&mp_plat_print, "%ld\n", 123); // long
  130. mp_printf(&mp_plat_print, "%lx\n", 0x123); // long hex
  131. mp_printf(&mp_plat_print, "%X\n", 0x1abcdef); // capital hex
  132. mp_printf(&mp_plat_print, "%.2s %.3s\n", "abc", "abc"); // fixed string precision
  133. mp_printf(&mp_plat_print, "%.*s\n", -1, "abc"); // negative string precision
  134. mp_printf(&mp_plat_print, "%b %b\n", 0, 1); // bools
  135. #ifndef NDEBUG
  136. mp_printf(&mp_plat_print, "%s\n", NULL); // null string
  137. #else
  138. mp_printf(&mp_plat_print, "(null)\n"); // without debugging mp_printf won't check for null
  139. #endif
  140. mp_printf(&mp_plat_print, "%d\n", 0x80000000); // should print signed
  141. mp_printf(&mp_plat_print, "%u\n", 0x80000000); // should print unsigned
  142. mp_printf(&mp_plat_print, "%x\n", 0x80000000); // should print unsigned
  143. mp_printf(&mp_plat_print, "%X\n", 0x80000000); // should print unsigned
  144. mp_printf(&mp_plat_print, "abc\n%"); // string ends in middle of format specifier
  145. }
  146. // GC
  147. {
  148. mp_printf(&mp_plat_print, "# GC\n");
  149. // calling gc_free while GC is locked
  150. gc_lock();
  151. gc_free(NULL);
  152. gc_unlock();
  153. // using gc_realloc to resize to 0, which means free the memory
  154. void *p = gc_alloc(4, false);
  155. mp_printf(&mp_plat_print, "%p\n", gc_realloc(p, 0, false));
  156. // calling gc_nbytes with a non-heap pointer
  157. mp_printf(&mp_plat_print, "%p\n", gc_nbytes(NULL));
  158. }
  159. // vstr
  160. {
  161. mp_printf(&mp_plat_print, "# vstr\n");
  162. vstr_t *vstr = vstr_new(16);
  163. vstr_hint_size(vstr, 32);
  164. vstr_add_str(vstr, "ts");
  165. vstr_ins_byte(vstr, 1, 'e');
  166. vstr_ins_char(vstr, 3, 't');
  167. vstr_ins_char(vstr, 10, 's');
  168. mp_printf(&mp_plat_print, "%.*s\n", (int)vstr->len, vstr->buf);
  169. vstr_cut_head_bytes(vstr, 2);
  170. mp_printf(&mp_plat_print, "%.*s\n", (int)vstr->len, vstr->buf);
  171. vstr_cut_tail_bytes(vstr, 10);
  172. mp_printf(&mp_plat_print, "%.*s\n", (int)vstr->len, vstr->buf);
  173. vstr_printf(vstr, "t%cst", 'e');
  174. mp_printf(&mp_plat_print, "%.*s\n", (int)vstr->len, vstr->buf);
  175. vstr_cut_out_bytes(vstr, 3, 10);
  176. mp_printf(&mp_plat_print, "%.*s\n", (int)vstr->len, vstr->buf);
  177. VSTR_FIXED(fix, 4);
  178. nlr_buf_t nlr;
  179. if (nlr_push(&nlr) == 0) {
  180. vstr_add_str(&fix, "large");
  181. nlr_pop();
  182. } else {
  183. mp_obj_print_exception(&mp_plat_print, MP_OBJ_FROM_PTR(nlr.ret_val));
  184. }
  185. fix.len = fix.alloc;
  186. if (nlr_push(&nlr) == 0) {
  187. vstr_null_terminated_str(&fix);
  188. nlr_pop();
  189. } else {
  190. mp_obj_print_exception(&mp_plat_print, MP_OBJ_FROM_PTR(nlr.ret_val));
  191. }
  192. }
  193. // repl autocomplete
  194. {
  195. mp_printf(&mp_plat_print, "# repl\n");
  196. const char *str;
  197. size_t len = mp_repl_autocomplete("__n", 3, &mp_plat_print, &str);
  198. mp_printf(&mp_plat_print, "%.*s\n", (int)len, str);
  199. mp_store_global(MP_QSTR_sys, mp_import_name(MP_QSTR_sys, mp_const_none, MP_OBJ_NEW_SMALL_INT(0)));
  200. mp_repl_autocomplete("sys.", 4, &mp_plat_print, &str);
  201. len = mp_repl_autocomplete("sys.impl", 8, &mp_plat_print, &str);
  202. mp_printf(&mp_plat_print, "%.*s\n", (int)len, str);
  203. }
  204. // attrtuple
  205. {
  206. mp_printf(&mp_plat_print, "# attrtuple\n");
  207. static const qstr fields[] = {MP_QSTR_start, MP_QSTR_stop, MP_QSTR_step};
  208. static const mp_obj_t items[] = {MP_OBJ_NEW_SMALL_INT(1), MP_OBJ_NEW_SMALL_INT(2), MP_OBJ_NEW_SMALL_INT(3)};
  209. mp_obj_print_helper(&mp_plat_print, mp_obj_new_attrtuple(fields, 3, items), PRINT_REPR);
  210. mp_printf(&mp_plat_print, "\n");
  211. }
  212. // str
  213. {
  214. mp_printf(&mp_plat_print, "# str\n");
  215. // intern string
  216. mp_printf(&mp_plat_print, "%d\n", MP_OBJ_IS_QSTR(mp_obj_str_intern(mp_obj_new_str("intern me", 9))));
  217. }
  218. // bytearray
  219. {
  220. mp_printf(&mp_plat_print, "# bytearray\n");
  221. // create a bytearray via mp_obj_new_bytearray
  222. mp_buffer_info_t bufinfo;
  223. mp_get_buffer_raise(mp_obj_new_bytearray(4, "data"), &bufinfo, MP_BUFFER_RW);
  224. mp_printf(&mp_plat_print, "%.*s\n", bufinfo.len, bufinfo.buf);
  225. }
  226. // mpz
  227. {
  228. mp_printf(&mp_plat_print, "# mpz\n");
  229. mp_uint_t value;
  230. mpz_t mpz;
  231. mpz_init_zero(&mpz);
  232. // mpz_as_uint_checked, with success
  233. mpz_set_from_int(&mpz, 12345678);
  234. mp_printf(&mp_plat_print, "%d\n", mpz_as_uint_checked(&mpz, &value));
  235. mp_printf(&mp_plat_print, "%d\n", (int)value);
  236. // mpz_as_uint_checked, with negative arg
  237. mpz_set_from_int(&mpz, -1);
  238. mp_printf(&mp_plat_print, "%d\n", mpz_as_uint_checked(&mpz, &value));
  239. // mpz_as_uint_checked, with overflowing arg
  240. mpz_set_from_int(&mpz, 1);
  241. mpz_shl_inpl(&mpz, &mpz, 70);
  242. mp_printf(&mp_plat_print, "%d\n", mpz_as_uint_checked(&mpz, &value));
  243. // mpz_set_from_float with inf as argument
  244. mpz_set_from_float(&mpz, 1.0 / 0.0);
  245. mpz_as_uint_checked(&mpz, &value);
  246. mp_printf(&mp_plat_print, "%d\n", (int)value);
  247. // mpz_set_from_float with 0 as argument
  248. mpz_set_from_float(&mpz, 0);
  249. mpz_as_uint_checked(&mpz, &value);
  250. mp_printf(&mp_plat_print, "%d\n", (int)value);
  251. // mpz_set_from_float with 0<x<1 as argument
  252. mpz_set_from_float(&mpz, 1e-10);
  253. mpz_as_uint_checked(&mpz, &value);
  254. mp_printf(&mp_plat_print, "%d\n", (int)value);
  255. // mpz_set_from_float with 1<=x<2 as argument
  256. mpz_set_from_float(&mpz, 1.5);
  257. mpz_as_uint_checked(&mpz, &value);
  258. mp_printf(&mp_plat_print, "%d\n", (int)value);
  259. // mpz_set_from_float with 2<x as argument
  260. mpz_set_from_float(&mpz, 12345);
  261. mpz_as_uint_checked(&mpz, &value);
  262. mp_printf(&mp_plat_print, "%d\n", (int)value);
  263. // mpz_mul_inpl with dest==rhs, lhs!=rhs
  264. mpz_t mpz2;
  265. mpz_set_from_int(&mpz, 2);
  266. mpz_init_from_int(&mpz2, 3);
  267. mpz_mul_inpl(&mpz, &mpz2, &mpz);
  268. mpz_as_uint_checked(&mpz, &value);
  269. mp_printf(&mp_plat_print, "%d\n", (int)value);
  270. }
  271. // runtime utils
  272. {
  273. mp_printf(&mp_plat_print, "# runtime utils\n");
  274. // call mp_call_function_1_protected
  275. mp_call_function_1_protected(MP_OBJ_FROM_PTR(&mp_builtin_abs_obj), MP_OBJ_NEW_SMALL_INT(1));
  276. // call mp_call_function_1_protected with invalid args
  277. mp_call_function_1_protected(MP_OBJ_FROM_PTR(&mp_builtin_abs_obj), mp_obj_new_str("abc", 3));
  278. // call mp_call_function_2_protected
  279. mp_call_function_2_protected(MP_OBJ_FROM_PTR(&mp_builtin_divmod_obj), MP_OBJ_NEW_SMALL_INT(1), MP_OBJ_NEW_SMALL_INT(1));
  280. // call mp_call_function_2_protected with invalid args
  281. mp_call_function_2_protected(MP_OBJ_FROM_PTR(&mp_builtin_divmod_obj), mp_obj_new_str("abc", 3), mp_obj_new_str("abc", 3));
  282. }
  283. // warning
  284. {
  285. mp_emitter_warning(MP_PASS_CODE_SIZE, "test");
  286. }
  287. // format float
  288. {
  289. mp_printf(&mp_plat_print, "# format float\n");
  290. // format with inadequate buffer size
  291. char buf[5];
  292. mp_format_float(1, buf, sizeof(buf), 'g', 0, '+');
  293. mp_printf(&mp_plat_print, "%s\n", buf);
  294. // format with just enough buffer so that precision must be
  295. // set from 0 to 1 twice
  296. char buf2[8];
  297. mp_format_float(1, buf2, sizeof(buf2), 'g', 0, '+');
  298. mp_printf(&mp_plat_print, "%s\n", buf2);
  299. // format where precision is trimmed to avoid buffer overflow
  300. mp_format_float(1, buf2, sizeof(buf2), 'e', 0, '+');
  301. mp_printf(&mp_plat_print, "%s\n", buf2);
  302. }
  303. // binary
  304. {
  305. mp_printf(&mp_plat_print, "# binary\n");
  306. // call function with float and double typecodes
  307. float far[1];
  308. double dar[1];
  309. mp_binary_set_val_array_from_int('f', far, 0, 123);
  310. mp_printf(&mp_plat_print, "%.0f\n", (double)far[0]);
  311. mp_binary_set_val_array_from_int('d', dar, 0, 456);
  312. mp_printf(&mp_plat_print, "%.0lf\n", dar[0]);
  313. }
  314. // VM
  315. {
  316. mp_printf(&mp_plat_print, "# VM\n");
  317. // call mp_execute_bytecode with invalide bytecode (should raise NotImplementedError)
  318. mp_obj_fun_bc_t fun_bc;
  319. fun_bc.bytecode = (const byte*)"\x01"; // just needed for n_state
  320. mp_code_state_t *code_state = m_new_obj_var(mp_code_state_t, mp_obj_t, 1);
  321. code_state->fun_bc = &fun_bc;
  322. code_state->ip = (const byte*)"\x00"; // just needed for an invalid opcode
  323. code_state->sp = &code_state->state[0];
  324. code_state->exc_sp = NULL;
  325. code_state->old_globals = NULL;
  326. mp_vm_return_kind_t ret = mp_execute_bytecode(code_state, MP_OBJ_NULL);
  327. mp_printf(&mp_plat_print, "%d %d\n", ret, mp_obj_get_type(code_state->state[0]) == &mp_type_NotImplementedError);
  328. }
  329. // scheduler
  330. {
  331. mp_printf(&mp_plat_print, "# scheduler\n");
  332. // lock scheduler
  333. mp_sched_lock();
  334. // schedule multiple callbacks; last one should fail
  335. for (int i = 0; i < 5; ++i) {
  336. mp_printf(&mp_plat_print, "sched(%d)=%d\n", i, mp_sched_schedule(MP_OBJ_FROM_PTR(&mp_builtin_print_obj), MP_OBJ_NEW_SMALL_INT(i)));
  337. }
  338. // test nested locking/unlocking
  339. mp_sched_lock();
  340. mp_sched_unlock();
  341. // shouldn't do anything while scheduler is locked
  342. mp_handle_pending();
  343. // unlock scheduler
  344. mp_sched_unlock();
  345. mp_printf(&mp_plat_print, "unlocked\n");
  346. // drain pending callbacks
  347. while (mp_sched_num_pending()) {
  348. mp_handle_pending();
  349. }
  350. }
  351. mp_obj_streamtest_t *s = m_new_obj(mp_obj_streamtest_t);
  352. s->base.type = &mp_type_stest_fileio;
  353. s->buf = NULL;
  354. s->len = 0;
  355. s->pos = 0;
  356. s->error_code = 0;
  357. mp_obj_streamtest_t *s2 = m_new_obj(mp_obj_streamtest_t);
  358. s2->base.type = &mp_type_stest_textio2;
  359. // return a tuple of data for testing on the Python side
  360. mp_obj_t items[] = {(mp_obj_t)&str_no_hash_obj, (mp_obj_t)&bytes_no_hash_obj, MP_OBJ_FROM_PTR(s), MP_OBJ_FROM_PTR(s2)};
  361. return mp_obj_new_tuple(MP_ARRAY_SIZE(items), items);
  362. }
  363. MP_DEFINE_CONST_FUN_OBJ_0(extra_coverage_obj, extra_coverage);
  364. #endif