torture.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /* Torture fixture for the C kernel walker (R7a) — exercises every c-path
  2. * branch of the checklist: fn-ptr tables, typedef enum/struct, file-scope
  3. * consts incl. multi-declarator, the macro-prototype misparse skip,
  4. * value-refs (+ shadow prune), the leading-attr-macro preParse blank, and
  5. * the C call shapes. Must parse ERROR-FREE post-preParse or the kernel arm
  6. * defers. */
  7. #include <stdio.h>
  8. #include <sys/socket.h>
  9. #include "local_ops.h"
  10. /** Retry budget for the poller. */
  11. static const int MAX_RETRIES = 3;
  12. static const int LOW_WATER = 2, HIGH_WATER = 8;
  13. static int counter = 0;
  14. static const char *BANNER = "torture";
  15. /* Bare identifier declarators are the macro-prototype misparse shape and are
  16. * skipped by design (uninit scalars are the accepted loss). */
  17. int bare_global;
  18. MYLIB_API config_handle;
  19. /* Leading attribute macro — blanked by preParseCSource (#1211), so the real
  20. * name survives on both arms. */
  21. SEC_ATTR UINT32 masked_entry(VOID) { return 0; }
  22. typedef enum { STATE_IDLE, STATE_RUNNING, STATE_DONE } run_state_t;
  23. typedef struct {
  24. int fd;
  25. void (*on_recv)(int);
  26. } conn_t;
  27. typedef struct conn_pool conn_pool_t;
  28. typedef int (*cb_t)(int);
  29. enum wire_flags { WIRE_A = 1, WIRE_B = 2 };
  30. struct packet {
  31. int len;
  32. unsigned char body[64];
  33. struct packet *next;
  34. cb_t *async_cb;
  35. };
  36. /** Sum helper (docstring). */
  37. static int add(int a, int b) { return a + b; }
  38. static int cb_a(int x) { return add(x, 1); }
  39. static int cb_b(int x) { return add(x, 2); }
  40. /* fn-ptr table at file scope — the ungated 'list' capture positions. */
  41. static cb_t DISPATCH_TABLE[] = { cb_a, cb_b };
  42. static void handle_recv(int fd);
  43. /* struct initializer — the ungated 'value' capture positions. */
  44. static const struct handler_ops OPS = { .recv = handle_recv, .flags = WIRE_A };
  45. static void handle_recv(int fd) {
  46. struct packet pkt;
  47. pkt.len = fd;
  48. printf("fd=%d retries=%d\n", fd, MAX_RETRIES);
  49. }
  50. /* Local shadow of a file-scope const — the shadow prune drops HIGH_WATER as a
  51. * value-ref target while LOW_WATER stays live. */
  52. static int shadowed_reader(void) {
  53. int HIGH_WATER = 99;
  54. return HIGH_WATER + LOW_WATER;
  55. }
  56. static int use_table(int idx, int v) {
  57. cb_t fn = DISPATCH_TABLE[idx];
  58. int r = (*fn)(v);
  59. conn_t c = { 1, 0 };
  60. c.on_recv(r);
  61. return counter + r;
  62. }
  63. static void spawn_workers(void) {
  64. register_handler(cb_a);
  65. signal_connect(&cb_b);
  66. }