connection-test.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910
  1. /*
  2. * Copyright © 2012 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining
  5. * a copy of this software and associated documentation files (the
  6. * "Software"), to deal in the Software without restriction, including
  7. * without limitation the rights to use, copy, modify, merge, publish,
  8. * distribute, sublicense, and/or sell copies of the Software, and to
  9. * permit persons to whom the Software is furnished to do so, subject to
  10. * the following conditions:
  11. *
  12. * The above copyright notice and this permission notice (including the
  13. * next paragraph) shall be included in all copies or substantial
  14. * portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  17. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  18. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  19. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  20. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  21. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  22. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23. * SOFTWARE.
  24. */
  25. #include <math.h>
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <stdarg.h>
  29. #include <stdint.h>
  30. #include <string.h>
  31. #include <assert.h>
  32. #include <sys/socket.h>
  33. #include <unistd.h>
  34. #include <errno.h>
  35. #include <sys/types.h>
  36. #include <sys/stat.h>
  37. #include <poll.h>
  38. #include "wayland-os.h"
  39. #include "wayland-private.h"
  40. #include "test-runner.h"
  41. #include "test-compositor.h"
  42. static const char message[] = "Hello, world";
  43. static struct wl_connection *
  44. setup(int *s)
  45. {
  46. struct wl_connection *connection;
  47. assert(wl_os_socketpair_cloexec(AF_UNIX, SOCK_STREAM, 0, s) == 0);
  48. connection = wl_connection_create(s[0], WL_BUFFER_DEFAULT_MAX_SIZE);
  49. assert(connection);
  50. return connection;
  51. }
  52. TEST(connection_create)
  53. {
  54. struct wl_connection *connection;
  55. int s[2];
  56. connection = setup(s);
  57. wl_connection_destroy(connection);
  58. close(s[0]);
  59. close(s[1]);
  60. }
  61. TEST(connection_write)
  62. {
  63. struct wl_connection *connection;
  64. int s[2];
  65. char buffer[64];
  66. connection = setup(s);
  67. assert(wl_connection_write(connection, message, sizeof message) == 0);
  68. assert(wl_connection_flush(connection) == sizeof message);
  69. assert(read(s[1], buffer, sizeof buffer) == sizeof message);
  70. assert(memcmp(message, buffer, sizeof message) == 0);
  71. wl_connection_destroy(connection);
  72. close(s[0]);
  73. close(s[1]);
  74. }
  75. TEST(connection_data)
  76. {
  77. struct wl_connection *connection;
  78. int s[2];
  79. char buffer[64];
  80. connection = setup(s);
  81. assert(write(s[1], message, sizeof message) == sizeof message);
  82. assert(wl_connection_read(connection) == sizeof message);
  83. wl_connection_copy(connection, buffer, sizeof message);
  84. assert(memcmp(message, buffer, sizeof message) == 0);
  85. wl_connection_consume(connection, sizeof message);
  86. wl_connection_destroy(connection);
  87. close(s[0]);
  88. close(s[1]);
  89. }
  90. TEST(connection_queue)
  91. {
  92. struct wl_connection *connection;
  93. int s[2];
  94. char buffer[64];
  95. connection = setup(s);
  96. /* Test that wl_connection_queue() puts data in the output
  97. * buffer without flush it. Verify that the data did get in
  98. * the buffer by writing another message and making sure that
  99. * we receive the two messages on the other fd. */
  100. assert(wl_connection_queue(connection, message, sizeof message) == 0);
  101. assert(wl_connection_flush(connection) == 0);
  102. assert(wl_connection_write(connection, message, sizeof message) == 0);
  103. assert(wl_connection_flush(connection) == 2 * sizeof message);
  104. assert(read(s[1], buffer, sizeof buffer) == 2 * sizeof message);
  105. assert(memcmp(message, buffer, sizeof message) == 0);
  106. assert(memcmp(message, buffer + sizeof message, sizeof message) == 0);
  107. wl_connection_destroy(connection);
  108. close(s[0]);
  109. close(s[1]);
  110. }
  111. static void
  112. va_list_wrapper(const char *signature, union wl_argument *args, int count, ...)
  113. {
  114. va_list ap;
  115. va_start(ap, count);
  116. wl_argument_from_va_list(signature, args, count, ap);
  117. va_end(ap);
  118. }
  119. TEST(argument_from_va_list)
  120. {
  121. union wl_argument args[WL_CLOSURE_MAX_ARGS];
  122. struct wl_object fake_object, fake_new_object;
  123. struct wl_array fake_array;
  124. va_list_wrapper("i", args, 1, 100);
  125. assert(args[0].i == 100);
  126. va_list_wrapper("is", args, 2, 101, "value");
  127. assert(args[0].i == 101);
  128. assert(strcmp(args[1].s, "value") == 0);
  129. va_list_wrapper("?iuf?sonah", args, 8,
  130. 102, 103, wl_fixed_from_int(104), "value",
  131. &fake_object, &fake_new_object, &fake_array, 106);
  132. assert(args[0].i == 102);
  133. assert(args[1].u == 103);
  134. assert(args[2].f == wl_fixed_from_int(104));
  135. assert(strcmp(args[3].s, "value") == 0);
  136. assert(args[4].o == &fake_object);
  137. assert(args[5].o == &fake_new_object);
  138. assert(args[6].a == &fake_array);
  139. assert(args[7].h == 106);
  140. }
  141. struct marshal_data {
  142. struct wl_connection *read_connection;
  143. struct wl_connection *write_connection;
  144. int s[2];
  145. uint32_t buffer[10];
  146. union {
  147. uint32_t u;
  148. int32_t i;
  149. const char *s;
  150. int h;
  151. } value;
  152. };
  153. static void
  154. setup_marshal_data(struct marshal_data *data)
  155. {
  156. assert(wl_os_socketpair_cloexec(AF_UNIX,
  157. SOCK_STREAM, 0, data->s) == 0);
  158. data->read_connection = wl_connection_create(data->s[0],
  159. WL_BUFFER_DEFAULT_MAX_SIZE);
  160. assert(data->read_connection);
  161. data->write_connection = wl_connection_create(data->s[1],
  162. WL_BUFFER_DEFAULT_MAX_SIZE);
  163. assert(data->write_connection);
  164. }
  165. static void
  166. release_marshal_data(struct marshal_data *data)
  167. {
  168. close(wl_connection_destroy(data->read_connection));
  169. close(wl_connection_destroy(data->write_connection));
  170. }
  171. static void
  172. marshal(struct marshal_data *data, const char *format, int size, ...)
  173. {
  174. struct wl_closure *closure;
  175. static const uint32_t opcode = 4444;
  176. static struct wl_object sender = { NULL, NULL, 1234 };
  177. struct wl_message message = { "test", format, NULL };
  178. va_list ap;
  179. va_start(ap, size);
  180. closure = wl_closure_vmarshal(&sender, opcode, ap, &message);
  181. va_end(ap);
  182. assert(closure);
  183. assert(wl_closure_send(closure, data->write_connection) == 0);
  184. wl_closure_destroy(closure);
  185. assert(wl_connection_flush(data->write_connection) == size);
  186. assert(read(data->s[0], data->buffer, sizeof data->buffer) == size);
  187. assert(data->buffer[0] == sender.id);
  188. assert(data->buffer[1] == (opcode | (size << 16)));
  189. }
  190. TEST(connection_marshal)
  191. {
  192. struct marshal_data data;
  193. struct wl_object object;
  194. struct wl_array array;
  195. static const char text[] = "curry";
  196. setup_marshal_data(&data);
  197. marshal(&data, "i", 12, 42);
  198. assert(data.buffer[2] == 42);
  199. marshal(&data, "u", 12, 55);
  200. assert(data.buffer[2] == 55);
  201. marshal(&data, "s", 20, "frappo");
  202. assert(data.buffer[2] == 7);
  203. assert(strcmp((char *) &data.buffer[3], "frappo") == 0);
  204. object.id = 557799;
  205. marshal(&data, "o", 12, &object);
  206. assert(data.buffer[2] == object.id);
  207. marshal(&data, "n", 12, &object);
  208. assert(data.buffer[2] == object.id);
  209. array.data = (void *) text;
  210. array.size = sizeof text;
  211. marshal(&data, "a", 20, &array);
  212. assert(data.buffer[2] == array.size);
  213. assert(memcmp(&data.buffer[3], text, array.size) == 0);
  214. release_marshal_data(&data);
  215. }
  216. static void
  217. expected_fail_marshal(int expected_error, const char *format, ...)
  218. {
  219. struct wl_closure *closure;
  220. static const uint32_t opcode = 4444;
  221. static const struct wl_interface test_interface = {
  222. .name = "test_object"
  223. };
  224. static struct wl_object sender = { 0 };
  225. struct wl_message message = { "test", format, NULL };
  226. sender.interface = &test_interface;
  227. sender.id = 1234;
  228. va_list ap;
  229. va_start(ap, format);
  230. closure = wl_closure_vmarshal(&sender, opcode, ap, &message);
  231. va_end(ap);
  232. assert(closure == NULL);
  233. assert(errno == expected_error);
  234. }
  235. static void
  236. marshal_send(struct marshal_data *data, const char *format, ...)
  237. {
  238. struct wl_closure *closure;
  239. static const uint32_t opcode = 4444;
  240. static struct wl_object sender = { NULL, NULL, 1234 };
  241. struct wl_message message = { "test", format, NULL };
  242. va_list ap;
  243. va_start(ap, format);
  244. closure = wl_closure_vmarshal(&sender, opcode, ap, &message);
  245. va_end(ap);
  246. assert(closure);
  247. assert(wl_closure_send(closure, data->write_connection) == 0);
  248. wl_closure_destroy(closure);
  249. }
  250. static void
  251. expected_fail_marshal_send(struct marshal_data *data, int expected_error,
  252. const char *format, ...)
  253. {
  254. struct wl_closure *closure;
  255. static const uint32_t opcode = 4444;
  256. static struct wl_object sender = { NULL, NULL, 1234 };
  257. struct wl_message message = { "test", format, NULL };
  258. va_list ap;
  259. va_start(ap, format);
  260. closure = wl_closure_vmarshal(&sender, opcode, ap, &message);
  261. va_end(ap);
  262. assert(closure);
  263. assert(wl_closure_send(closure, data->write_connection) < 0);
  264. assert(errno == expected_error);
  265. wl_closure_destroy(closure);
  266. }
  267. TEST(connection_marshal_nullables)
  268. {
  269. struct marshal_data data;
  270. struct wl_object object;
  271. const char text[] = "curry";
  272. setup_marshal_data(&data);
  273. expected_fail_marshal(EINVAL, "o", NULL);
  274. expected_fail_marshal(EINVAL, "s", NULL);
  275. expected_fail_marshal(EINVAL, "a", NULL);
  276. marshal(&data, "?o", 12, NULL);
  277. assert(data.buffer[2] == 0);
  278. marshal(&data, "?s", 12, NULL);
  279. assert(data.buffer[2] == 0);
  280. object.id = 55293;
  281. marshal(&data, "?o", 12, &object);
  282. assert(data.buffer[2] == object.id);
  283. marshal(&data, "?s", 20, text);
  284. assert(data.buffer[2] == sizeof text);
  285. assert(strcmp((char *) &data.buffer[3], text) == 0);
  286. release_marshal_data(&data);
  287. }
  288. static void
  289. validate_demarshal_u(struct marshal_data *data,
  290. struct wl_object *object, uint32_t u)
  291. {
  292. assert(data->value.u == u);
  293. }
  294. static void
  295. validate_demarshal_i(struct marshal_data *data,
  296. struct wl_object *object, int32_t i)
  297. {
  298. assert(data->value.i == i);
  299. }
  300. static void
  301. validate_demarshal_s(struct marshal_data *data,
  302. struct wl_object *object, const char *s)
  303. {
  304. if (data->value.s != NULL)
  305. assert(strcmp(data->value.s, s) == 0);
  306. else
  307. assert(s == NULL);
  308. }
  309. static void
  310. validate_demarshal_h(struct marshal_data *data,
  311. struct wl_object *object, int fd)
  312. {
  313. struct stat buf1, buf2;
  314. assert(fd != data->value.h);
  315. fstat(fd, &buf1);
  316. fstat(data->value.h, &buf2);
  317. assert(buf1.st_dev == buf2.st_dev);
  318. assert(buf1.st_ino == buf2.st_ino);
  319. close(fd);
  320. close(data->value.h);
  321. }
  322. static void
  323. validate_demarshal_f(struct marshal_data *data,
  324. struct wl_object *object, wl_fixed_t f)
  325. {
  326. assert(data->value.i == f);
  327. }
  328. static void
  329. demarshal(struct marshal_data *data, const char *format,
  330. uint32_t *msg, void (*func)(void))
  331. {
  332. struct wl_message message = { "test", format, NULL };
  333. struct wl_closure *closure;
  334. struct wl_map objects;
  335. struct wl_object object = { NULL, &func, 0 };
  336. int size = msg[1] >> 16;
  337. assert(write(data->s[1], msg, size) == size);
  338. assert(wl_connection_read(data->read_connection) == size);
  339. wl_map_init(&objects, WL_MAP_SERVER_SIDE);
  340. object.id = msg[0];
  341. closure = wl_connection_demarshal(data->read_connection,
  342. size, &objects, &message);
  343. assert(closure);
  344. wl_closure_invoke(closure, WL_CLOSURE_INVOKE_SERVER, &object, 0, data);
  345. wl_closure_destroy(closure);
  346. }
  347. TEST(connection_demarshal)
  348. {
  349. struct marshal_data data;
  350. uint32_t msg[10];
  351. setup_marshal_data(&data);
  352. data.value.u = 8000;
  353. msg[0] = 400200; /* object id */
  354. msg[1] = 12 << 16; /* size = 12, opcode = 0 */
  355. msg[2] = data.value.u;
  356. demarshal(&data, "u", msg, (void *) validate_demarshal_u);
  357. data.value.i = -557799;
  358. msg[0] = 400200;
  359. msg[1] = 12 << 16;
  360. msg[2] = data.value.i;
  361. demarshal(&data, "i", msg, (void *) validate_demarshal_i);
  362. data.value.s = "superdude";
  363. msg[0] = 400200;
  364. msg[1] = 24 << 16;
  365. msg[2] = 10;
  366. msg[3 + msg[2]/4] = 0;
  367. memcpy(&msg[3], data.value.s, msg[2]);
  368. demarshal(&data, "s", msg, (void *) validate_demarshal_s);
  369. data.value.s = "superdude";
  370. msg[0] = 400200;
  371. msg[1] = 24 << 16;
  372. msg[2] = 10;
  373. msg[3 + msg[2]/4] = 0;
  374. memcpy(&msg[3], data.value.s, msg[2]);
  375. demarshal(&data, "?s", msg, (void *) validate_demarshal_s);
  376. data.value.i = wl_fixed_from_double(-90000.2390);
  377. msg[0] = 400200;
  378. msg[1] = 12 << 16;
  379. msg[2] = data.value.i;
  380. demarshal(&data, "f", msg, (void *) validate_demarshal_f);
  381. data.value.s = NULL;
  382. msg[0] = 400200;
  383. msg[1] = 12 << 16;
  384. msg[2] = 0;
  385. demarshal(&data, "?s", msg, (void *) validate_demarshal_s);
  386. release_marshal_data(&data);
  387. }
  388. static void
  389. marshal_demarshal(struct marshal_data *data,
  390. void (*func)(void), int size, const char *format, ...)
  391. {
  392. struct wl_closure *closure;
  393. static const int opcode = 4444;
  394. static struct wl_object sender = { NULL, NULL, 1234 };
  395. struct wl_message message = { "test", format, NULL };
  396. struct wl_map objects;
  397. struct wl_object object = { NULL, &func, 0 };
  398. va_list ap;
  399. uint32_t msg[1] = { 1234 };
  400. va_start(ap, format);
  401. closure = wl_closure_vmarshal(&sender, opcode, ap, &message);
  402. va_end(ap);
  403. assert(closure);
  404. assert(wl_closure_send(closure, data->write_connection) == 0);
  405. wl_closure_destroy(closure);
  406. assert(wl_connection_flush(data->write_connection) == size);
  407. assert(wl_connection_read(data->read_connection) == size);
  408. wl_map_init(&objects, WL_MAP_SERVER_SIDE);
  409. object.id = msg[0];
  410. closure = wl_connection_demarshal(data->read_connection,
  411. size, &objects, &message);
  412. assert(closure);
  413. wl_closure_invoke(closure, WL_CLOSURE_INVOKE_SERVER, &object, 0, data);
  414. wl_closure_destroy(closure);
  415. }
  416. TEST(connection_marshal_demarshal)
  417. {
  418. struct marshal_data data;
  419. char f[] = "/tmp/wayland-tests-XXXXXX";
  420. setup_marshal_data(&data);
  421. data.value.u = 889911;
  422. marshal_demarshal(&data, (void *) validate_demarshal_u,
  423. 12, "u", data.value.u);
  424. data.value.i = -13;
  425. marshal_demarshal(&data, (void *) validate_demarshal_i,
  426. 12, "i", data.value.i);
  427. data.value.s = "cookie robots";
  428. marshal_demarshal(&data, (void *) validate_demarshal_s,
  429. 28, "s", data.value.s);
  430. data.value.s = "cookie robots";
  431. marshal_demarshal(&data, (void *) validate_demarshal_s,
  432. 28, "?s", data.value.s);
  433. data.value.h = mkstemp(f);
  434. assert(data.value.h >= 0);
  435. unlink(f);
  436. marshal_demarshal(&data, (void *) validate_demarshal_h,
  437. 8, "h", data.value.h);
  438. data.value.i = wl_fixed_from_double(1234.5678);
  439. marshal_demarshal(&data, (void *) validate_demarshal_f,
  440. 12, "f", data.value.i);
  441. data.value.i = wl_fixed_from_double(-90000.2390);
  442. marshal_demarshal(&data, (void *) validate_demarshal_f,
  443. 12, "f", data.value.i);
  444. data.value.i = wl_fixed_from_double((1 << 23) - 1 + 0.0941);
  445. marshal_demarshal(&data, (void *) validate_demarshal_f,
  446. 12, "f", data.value.i);
  447. release_marshal_data(&data);
  448. }
  449. static void
  450. expected_fail_demarshal(struct marshal_data *data, const char *format,
  451. const uint32_t *msg, int expected_error)
  452. {
  453. struct wl_message message = { "test", format, NULL };
  454. struct wl_closure *closure;
  455. struct wl_map objects;
  456. int size = (msg[1] >> 16);
  457. assert(write(data->s[1], msg, size) == size);
  458. assert(wl_connection_read(data->read_connection) == size);
  459. wl_map_init(&objects, WL_MAP_SERVER_SIDE);
  460. closure = wl_connection_demarshal(data->read_connection,
  461. size, &objects, &message);
  462. assert(closure == NULL);
  463. assert(errno == expected_error);
  464. }
  465. TEST(connection_demarshal_null_strings)
  466. {
  467. struct marshal_data data;
  468. uint32_t msg[3];
  469. setup_marshal_data(&data);
  470. data.value.s = NULL;
  471. msg[0] = 400200; /* object id */
  472. msg[1] = 12 << 16; /* size = 12, opcode = 0 */
  473. msg[2] = 0; /* string length = 0 */
  474. demarshal(&data, "?s", msg, (void *) validate_demarshal_s);
  475. expected_fail_demarshal(&data, "s", msg, EINVAL);
  476. release_marshal_data(&data);
  477. }
  478. /* These tests are verifying that the demarshaling code will gracefully handle
  479. * clients lying about string and array lengths and giving values near
  480. * UINT32_MAX. Before fixes f7fdface and f5b9e3b9 this test would crash on
  481. * 32bit systems.
  482. */
  483. TEST(connection_demarshal_failures)
  484. {
  485. struct marshal_data data;
  486. unsigned int i;
  487. uint32_t msg[3];
  488. const uint32_t overflowing_values[] = {
  489. /* Values very close to UINT32_MAX. Before f5b9e3b9 these
  490. * would cause integer overflow in DIV_ROUNDUP. */
  491. 0xffffffff, 0xfffffffe, 0xfffffffd, 0xfffffffc,
  492. /* Values at various offsets from UINT32_MAX. Before f7fdface
  493. * these would overflow the "p" pointer on 32bit systems,
  494. * effectively subtracting the offset from it. It had good
  495. * chance to cause crash depending on what was stored at that
  496. * offset before "p". */
  497. 0xfffff000, 0xffffd000, 0xffffc000, 0xffffb000
  498. };
  499. setup_marshal_data(&data);
  500. /* sender_id, does not matter */
  501. msg[0] = 0;
  502. /* (size << 16 | opcode), opcode is 0, does not matter */
  503. msg[1] = sizeof(msg) << 16;
  504. for (i = 0; i < ARRAY_LENGTH(overflowing_values); i++) {
  505. /* length of the string or array */
  506. msg[2] = overflowing_values[i];
  507. expected_fail_demarshal(&data, "s", msg, EINVAL);
  508. expected_fail_demarshal(&data, "a", msg, EINVAL);
  509. }
  510. release_marshal_data(&data);
  511. }
  512. TEST(connection_marshal_alot)
  513. {
  514. struct marshal_data data;
  515. char f[64];
  516. int i;
  517. setup_marshal_data(&data);
  518. /* We iterate enough to make sure we wrap the circular buffers
  519. * for both regular data an fds. */
  520. for (i = 0; i < 2000; i++) {
  521. strcpy(f, "/tmp/wayland-tests-XXXXXX");
  522. data.value.h = mkstemp(f);
  523. assert(data.value.h >= 0);
  524. unlink(f);
  525. marshal_demarshal(&data, (void *) validate_demarshal_h,
  526. 8, "h", data.value.h);
  527. }
  528. release_marshal_data(&data);
  529. }
  530. TEST(connection_marshal_too_big)
  531. {
  532. struct marshal_data data;
  533. char *big_string = malloc(5000);
  534. assert(big_string);
  535. memset(big_string, ' ', 4999);
  536. big_string[4999] = '\0';
  537. setup_marshal_data(&data);
  538. expected_fail_marshal_send(&data, E2BIG, "s", big_string);
  539. release_marshal_data(&data);
  540. free(big_string);
  541. }
  542. TEST(connection_marshal_big_enough)
  543. {
  544. struct marshal_data data;
  545. char *big_string = malloc(5000);
  546. assert(big_string);
  547. memset(big_string, ' ', 4999);
  548. big_string[4999] = '\0';
  549. setup_marshal_data(&data);
  550. wl_connection_set_max_buffer_size(data.write_connection, 5120);
  551. marshal_send(&data, "s", big_string);
  552. release_marshal_data(&data);
  553. free(big_string);
  554. }
  555. TEST(connection_marshal_unbounded_boundary_size)
  556. {
  557. /* A string of length 8178 requires a buffer size of exactly 2^13. */
  558. struct marshal_data data;
  559. char *big_string = malloc(8178);
  560. assert(big_string);
  561. memset(big_string, ' ', 8177);
  562. big_string[8177] = '\0';
  563. setup_marshal_data(&data);
  564. /* Set the max size to 0 (unbounded). */
  565. wl_connection_set_max_buffer_size(data.write_connection, 0);
  566. marshal_send(&data, "s", big_string);
  567. release_marshal_data(&data);
  568. free(big_string);
  569. }
  570. static void
  571. marshal_helper(const char *format, void *handler, ...)
  572. {
  573. struct wl_closure *closure;
  574. static struct wl_object sender = { NULL, NULL, 1234 };
  575. struct wl_object object = { NULL, &handler, 0 };
  576. static const int opcode = 4444;
  577. struct wl_message message = { "test", format, NULL };
  578. va_list ap;
  579. int done;
  580. va_start(ap, handler);
  581. closure = wl_closure_vmarshal(&sender, opcode, ap, &message);
  582. va_end(ap);
  583. assert(closure);
  584. done = 0;
  585. wl_closure_invoke(closure, WL_CLOSURE_INVOKE_SERVER, &object, 0, &done);
  586. wl_closure_destroy(closure);
  587. assert(done);
  588. }
  589. static void
  590. suu_handler(void *data, struct wl_object *object,
  591. const char *s, uint32_t u1, uint32_t u2)
  592. {
  593. int *done = data;
  594. assert(strcmp(s, "foo") == 0);
  595. assert(u1 == 500);
  596. assert(u2 == 404040);
  597. *done = 1;
  598. }
  599. TEST(invoke_closure)
  600. {
  601. marshal_helper("suu", suu_handler, "foo", 500, 404040);
  602. }
  603. static void
  604. leak_closure(void)
  605. {
  606. struct wl_callback *cb;
  607. struct pollfd pfd;
  608. struct client *c = client_connect();
  609. cb = wl_display_sync(c->wl_display);
  610. assert(cb);
  611. assert(wl_display_flush(c->wl_display) > 0);
  612. /* we don't need it, it is referenced */
  613. wl_callback_destroy(cb);
  614. pfd.fd = wl_display_get_fd(c->wl_display);
  615. pfd.events = POLLIN;
  616. test_set_timeout(2);
  617. assert(poll(&pfd, 1, -1) == 1);
  618. /* read events, but do not dispatch them */
  619. assert(wl_display_prepare_read(c->wl_display) == 0);
  620. assert(wl_display_read_events(c->wl_display) == 0);
  621. /*
  622. * now we have wl_callback.done and wl_display.delete_id queued;
  623. * if we now release the queue (in wl_display_disconnect())
  624. * we should not leak memory
  625. */
  626. client_disconnect(c);
  627. }
  628. TEST(closure_leaks)
  629. {
  630. struct display *d = display_create();
  631. client_create_noarg(d, leak_closure);
  632. display_run(d);
  633. display_destroy(d);
  634. }
  635. static void
  636. leak_after_error(void)
  637. {
  638. struct client *c = client_connect();
  639. /* this should return -1, because we'll send error
  640. * from server. */
  641. assert(stop_display(c, 1) == -1);
  642. assert(wl_display_dispatch_pending(c->wl_display) == -1);
  643. assert(wl_display_get_error(c->wl_display) == ENOMEM);
  644. /* after we got error, we have display_resume event
  645. * in the queue. It should be freed in wl_display_disconnect().
  646. * Let's see! */
  647. wl_proxy_destroy((struct wl_proxy *) c->tc);
  648. wl_display_disconnect(c->wl_display);
  649. free(c);
  650. }
  651. TEST(closure_leaks_after_error)
  652. {
  653. struct display *d = display_create();
  654. struct client_info *cl;
  655. cl = client_create_noarg(d, leak_after_error);
  656. display_run(d);
  657. wl_client_post_no_memory(cl->wl_client);
  658. display_resume(d);
  659. display_destroy(d);
  660. }
  661. /** Raw read from socket expecting wl_display.error
  662. *
  663. * \param sockfd The socket to read from.
  664. * \param expected_error The expected wl_display error code.
  665. *
  666. * Reads the socket and manually parses one message, expecting it to be a
  667. * wl_display.error with the wl_display as the originating object.
  668. * Asserts that the received error code is expected_error.
  669. */
  670. static void
  671. expect_error_recv(int sockfd, uint32_t expected_error)
  672. {
  673. uint32_t buf[1024];
  674. ssize_t slen;
  675. uint32_t opcode;
  676. int str_len;
  677. slen = recv(sockfd, buf, sizeof buf, 0);
  678. assert(slen >= 2 * (ssize_t)sizeof (uint32_t));
  679. opcode = buf[1] & 0xffff;
  680. fprintf(stderr, "Received %zd bytes, object %u, opcode %u\n",
  681. slen, buf[0], opcode);
  682. /* check error event */
  683. assert(buf[0] == 1);
  684. assert(opcode == WL_DISPLAY_ERROR);
  685. str_len = buf[4];
  686. assert(str_len > 0);
  687. assert(str_len <= slen - 5 * (ssize_t)sizeof (uint32_t));
  688. fprintf(stderr, "Error event on object %u, code %u, message \"%*s\"\n",
  689. buf[2], buf[3], str_len, (const char *)&buf[5]);
  690. assert(buf[3] == expected_error);
  691. }
  692. /* A test for https://gitlab.freedesktop.org/wayland/wayland/issues/52
  693. * trying to provoke a read from uninitialized memory in
  694. * wl_connection_demarshal() for sender_id and opcode.
  695. *
  696. * This test might not fail as is even with #52 unfixed, since there is no way
  697. * to detect what happens and the crash with zero size depends on stack content.
  698. * However, running under Valgrind would point out invalid reads and use of
  699. * uninitialized values.
  700. */
  701. TEST(request_bogus_size)
  702. {
  703. struct wl_display *display;
  704. struct wl_client *client;
  705. int s[2];
  706. uint32_t msg[3];
  707. int bogus_size;
  708. test_set_timeout(1);
  709. /*
  710. * The manufactured message has real size 12. Test all bogus sizes
  711. * smaller than that, and zero as the last one since wl_closure_init
  712. * handles zero specially and having garbage in the stack makes it more
  713. * likely to crash in wl_connection_demarshal.
  714. */
  715. for (bogus_size = 11; bogus_size >= 0; bogus_size--) {
  716. fprintf(stderr, "* bogus size %d\n", bogus_size);
  717. assert(wl_os_socketpair_cloexec(AF_UNIX, SOCK_STREAM, 0, s) == 0);
  718. display = wl_display_create();
  719. assert(display);
  720. client = wl_client_create(display, s[0]);
  721. assert(client);
  722. /* manufacture a request that lies about its size */
  723. msg[0] = 1; /* sender id: wl_display */
  724. msg[1] = (bogus_size << 16) | WL_DISPLAY_SYNC; /* size and opcode */
  725. msg[2] = 2; /* sync argument: new_id for wl_callback */
  726. assert(send(s[1], msg, sizeof msg, 0) == sizeof msg);
  727. wl_event_loop_dispatch(wl_display_get_event_loop(display), 0);
  728. expect_error_recv(s[1], WL_DISPLAY_ERROR_INVALID_METHOD);
  729. /* Do not wl_client_destroy, the error already caused it. */
  730. close(s[1]);
  731. wl_display_destroy(display);
  732. }
  733. }