|
24 | 24 | #include <libhexagonrpc/fastrpc.h> |
25 | 25 | #include <libhexagonrpc/interfaces/remotectl.def> |
26 | 26 | #include <stddef.h> |
| 27 | +#include <stdint.h> |
27 | 28 | #include <stdio.h> |
28 | 29 |
|
29 | 30 | #include "interfaces/adsp_listener.def" |
30 | 31 | #include "iobuffer.h" |
31 | 32 | #include "listener.h" |
32 | 33 |
|
33 | | -static struct fastrpc_io_buffer *allocate_outbufs(const struct fastrpc_function_def_interp2 *def, |
34 | | - uint32_t *first_inbuf) |
| 34 | +static int check_buf_sizes(const struct hrpc_method_def_interp3 *def, |
| 35 | + uint8_t n_inbufs, uint8_t n_outbufs, |
| 36 | + const struct fastrpc_io_buffer *inbufs) |
35 | 37 | { |
36 | | - struct fastrpc_io_buffer *out; |
37 | | - size_t out_count; |
38 | | - size_t i, j; |
39 | | - off_t off; |
40 | | - uint32_t *sizes; |
41 | | - |
42 | | - out_count = def->out_bufs + (def->out_nums && 1); |
43 | | - /* |
44 | | - * POSIX allows malloc to return a non-NULL pointer to a zero-size area |
45 | | - * in memory. Since the code below assumes non-zero size if the pointer |
46 | | - * is non-NULL, exit early if we do not need to allocate anything. |
47 | | - */ |
48 | | - if (out_count == 0) |
49 | | - return NULL; |
| 38 | + const uint32_t *prim_in = inbufs[0].p; |
| 39 | + uint8_t n_def_outbufs; |
| 40 | + size_t i, j = 1; |
| 41 | + size_t off; |
| 42 | + size_t n_prim_in; |
| 43 | + |
| 44 | + if (def->msg_id < 31) |
| 45 | + off = 0; |
| 46 | + else |
| 47 | + off = 1; |
| 48 | + |
| 49 | + for (i = 0; i < def->n_args; i++) { |
| 50 | + if (def->args[i] == HEXAGONRPC_DELIMITER) |
| 51 | + break; |
50 | 52 |
|
51 | | - out = malloc(sizeof(struct fastrpc_io_buffer) * out_count); |
52 | | - if (out == NULL) |
53 | | - return NULL; |
| 53 | + if (!def->args[i]) |
| 54 | + continue; |
| 55 | + |
| 56 | + /* |
| 57 | + * We can't check the size because the primary input buffer is |
| 58 | + * too small, so skip checking and print an error later. |
| 59 | + */ |
| 60 | + if ((off + i) * sizeof(uint32_t) >= inbufs[0].s) |
| 61 | + continue; |
54 | 62 |
|
55 | | - out[0].s = def->out_nums * 4; |
56 | | - if (out[0].s) { |
57 | | - out[0].p = malloc(def->out_nums * 4); |
58 | | - if (out[0].p == NULL) |
59 | | - goto err_free_out; |
| 63 | + if (j >= n_inbufs) { |
| 64 | + fprintf(stderr, "listener: not enough input buffers\n"); |
| 65 | + return 1; |
| 66 | + } |
| 67 | + |
| 68 | + if ((size_t) prim_in[off + i] * def->args[i] > inbufs[j].s) { |
| 69 | + fprintf(stderr, "listener: input buffer too small\n"); |
| 70 | + return 1; |
| 71 | + } |
| 72 | + |
| 73 | + j++; |
60 | 74 | } |
61 | 75 |
|
62 | | - off = def->out_nums && 1; |
63 | | - sizes = &first_inbuf[def->in_nums + def->in_bufs]; |
| 76 | + n_prim_in = i; |
| 77 | + if (def->args[i] == HEXAGONRPC_DELIMITER) |
| 78 | + i++; |
| 79 | + |
| 80 | + if (def->has_prim_out) |
| 81 | + n_def_outbufs = 1; |
| 82 | + else |
| 83 | + n_def_outbufs = 0; |
64 | 84 |
|
65 | | - for (i = 0; i < def->out_bufs; i++) { |
66 | | - out[off + i].s = sizes[i]; |
67 | | - out[off + i].p = malloc(sizes[i]); |
68 | | - if (out[off + i].p == NULL) |
69 | | - goto err_free_prev; |
| 85 | + for (; i < def->n_args; i++) { |
| 86 | + if (def->args[i]) { |
| 87 | + n_def_outbufs++; |
| 88 | + n_prim_in++; |
| 89 | + } |
70 | 90 | } |
71 | 91 |
|
72 | | - return out; |
| 92 | + if (n_def_outbufs != n_outbufs) { |
| 93 | + fprintf(stderr, "listener: wrong amount of output buffers\n"); |
| 94 | + return 1; |
| 95 | + } |
73 | 96 |
|
74 | | -err_free_prev: |
75 | | - for (j = 0; j < i; j++) |
76 | | - free(out[off + j].p); |
| 97 | + if ((off + n_prim_in) * sizeof(uint32_t) > inbufs[0].s) { |
| 98 | + fprintf(stderr, "listener: primary input buffer too small\n"); |
| 99 | + return 1; |
| 100 | + } |
77 | 101 |
|
78 | | -err_free_out: |
79 | | - free(out); |
80 | | - return NULL; |
| 102 | + return 0; |
81 | 103 | } |
82 | 104 |
|
83 | | -static int check_inbuf_sizes(const struct fastrpc_function_def_interp2 *def, |
84 | | - const struct fastrpc_io_buffer *inbufs) |
| 105 | +static struct fastrpc_io_buffer *allocate_outbufs(const struct hrpc_method_def_interp3 *def, |
| 106 | + const uint32_t *prim_in) |
85 | 107 | { |
86 | | - uint8_t i; |
87 | | - const uint32_t *sizes = &((const uint32_t *) inbufs[0].p)[def->in_nums]; |
88 | | - |
89 | | - if (inbufs[0].s != 4 * (def->in_nums |
90 | | - + def->in_bufs |
91 | | - + def->out_bufs)) { |
92 | | - fprintf(stderr, "Invalid number of input numbers: %" PRIu32 " (expected %u)\n", |
93 | | - inbufs[0].s, |
94 | | - 4 * (def->in_nums |
95 | | - + def->in_bufs |
96 | | - + def->out_bufs)); |
97 | | - return -1; |
| 108 | + struct fastrpc_io_buffer *out; |
| 109 | + // This is only used when there is a delimiter. |
| 110 | + const uint32_t *size = NULL; |
| 111 | + size_t n_outbufs, n_prim_out = 0; |
| 112 | + size_t i; |
| 113 | + |
| 114 | + out = malloc(sizeof(struct fastrpc_io_buffer) * def->n_args); |
| 115 | + if (out == NULL) |
| 116 | + return NULL; |
| 117 | + |
| 118 | + if (def->has_prim_out) |
| 119 | + n_outbufs = 1; |
| 120 | + else |
| 121 | + n_outbufs = 0; |
| 122 | + |
| 123 | + for (i = 0; i < def->n_args; i++) { |
| 124 | + if (def->args[i] == HEXAGONRPC_DELIMITER) { |
| 125 | + size = &prim_in[i]; |
| 126 | + i++; |
| 127 | + break; |
| 128 | + } |
98 | 129 | } |
99 | 130 |
|
100 | | - for (i = 0; i < def->in_bufs; i++) { |
101 | | - if (inbufs[i + 1].s != sizes[i]) { |
102 | | - fprintf(stderr, "Invalid buffer size\n"); |
103 | | - return -1; |
| 131 | + for (; i < def->n_args; i++) { |
| 132 | + if (def->args[i]) { |
| 133 | + out[n_outbufs].s = (size_t) def->args[i] * *size; |
| 134 | + out[n_outbufs].p = malloc(out[n_outbufs].s); |
| 135 | + if (out[n_outbufs].p == NULL && out[n_outbufs].s) |
| 136 | + goto err_free_outbufs; |
| 137 | + |
| 138 | + n_outbufs++; |
| 139 | + size = &size[1]; |
| 140 | + } else { |
| 141 | + n_prim_out++; |
104 | 142 | } |
105 | 143 | } |
106 | 144 |
|
107 | | - return 0; |
| 145 | + if (def->has_prim_out) { |
| 146 | + out[0].s = sizeof(uint32_t) * n_prim_out; |
| 147 | + out[0].p = malloc(out[0].s); |
| 148 | + if (out[0].p == NULL && out[0].s) |
| 149 | + goto err_free_outbufs; |
| 150 | + } |
| 151 | + |
| 152 | + return out; |
| 153 | + |
| 154 | +err_free_outbufs: |
| 155 | + if (!def->has_prim_out) |
| 156 | + free(out[0].p); |
| 157 | + |
| 158 | + for (i = 1; i < n_outbufs; i++) |
| 159 | + free(out[i].p); |
| 160 | + |
| 161 | + free(out); |
| 162 | + return NULL; |
108 | 163 | } |
109 | 164 |
|
110 | 165 | static int return_for_next_invoke(int fd, |
@@ -189,11 +244,16 @@ static int invoke_requested_procedure(size_t n_ifaces, |
189 | 244 | struct fastrpc_io_buffer **returned) |
190 | 245 | { |
191 | 246 | const struct fastrpc_function_impl *impl; |
192 | | - uint8_t in_count; |
193 | | - uint8_t out_count; |
194 | 247 | uint32_t method = REMOTE_SCALARS_METHOD(sc); |
195 | 248 | int ret; |
196 | 249 |
|
| 250 | + if (method < 31) |
| 251 | + method = REMOTE_SCALARS_METHOD(sc); |
| 252 | + else if (decoded[0].s >= sizeof(uint32_t)) |
| 253 | + method = *(const uint32_t *) decoded[0].p; |
| 254 | + else |
| 255 | + method = UINT32_MAX; |
| 256 | + |
197 | 257 | if (sc & 0xff) { |
198 | 258 | fprintf(stderr, "Handles are not supported, but got %u in, %u out\n", |
199 | 259 | (sc & 0xf0) >> 4, sc & 0xf); |
@@ -221,26 +281,15 @@ static int invoke_requested_procedure(size_t n_ifaces, |
221 | 281 | return 1; |
222 | 282 | } |
223 | 283 |
|
224 | | - in_count = impl->def->in_bufs + ((impl->def->in_nums |
225 | | - || impl->def->in_bufs |
226 | | - || impl->def->out_bufs) && 1); |
227 | | - out_count = impl->def->out_bufs + (impl->def->out_nums && 1); |
228 | | - |
229 | | - if (REMOTE_SCALARS_INBUFS(sc) != in_count |
230 | | - || REMOTE_SCALARS_OUTBUFS(sc) != out_count) { |
231 | | - fprintf(stderr, "Unexpected buffer count: %08x\n", sc); |
232 | | - *result = AEE_EBADPARM; |
233 | | - return 1; |
234 | | - } |
235 | | - |
236 | | - ret = check_inbuf_sizes(impl->def, decoded); |
| 284 | + ret = check_buf_sizes(impl->def, REMOTE_SCALARS_INBUFS(sc), |
| 285 | + REMOTE_SCALARS_OUTBUFS(sc), decoded); |
237 | 286 | if (ret) { |
238 | 287 | *result = AEE_EBADPARM; |
239 | 288 | return 1; |
240 | 289 | } |
241 | 290 |
|
242 | 291 | *returned = allocate_outbufs(impl->def, decoded[0].p); |
243 | | - if (*returned == NULL && out_count > 0) { |
| 292 | + if (*returned == NULL && impl->def->n_args > 0) { |
244 | 293 | perror("Could not allocate output buffers"); |
245 | 294 | *result = AEE_ENOMEMORY; |
246 | 295 | return 1; |
|
0 commit comments