Skip to content

Commit 1393c19

Browse files
committed
fastrpc: hexagonrpcd: listener: switch to interp3
For a proper transition to version 3 of the method definitions, switch the reverse tunnel to version 3.
1 parent 040440f commit 1393c19

File tree

4 files changed

+140
-85
lines changed

4 files changed

+140
-85
lines changed

hexagonrpcd/apps_std.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -408,23 +408,23 @@ static const struct fastrpc_function_impl apps_std_procs[] = {
408408
{ .def = NULL, .impl = NULL, },
409409
{ .def = NULL, .impl = NULL, },
410410
{
411-
.def = &apps_std_fflush_def,
411+
.def = &apps_std3_fflush_def,
412412
.impl = apps_std_fflush,
413413
},
414414
{
415-
.def = &apps_std_fclose_def,
415+
.def = &apps_std3_fclose_def,
416416
.impl = apps_std_fclose,
417417
},
418418
{
419-
.def = &apps_std_fread_def,
419+
.def = &apps_std3_fread_def,
420420
.impl = apps_std_fread,
421421
},
422422
{ .def = NULL, .impl = NULL, },
423423
{ .def = NULL, .impl = NULL, },
424424
{ .def = NULL, .impl = NULL, },
425425
{ .def = NULL, .impl = NULL, },
426426
{
427-
.def = &apps_std_fseek_def,
427+
.def = &apps_std3_fseek_def,
428428
.impl = apps_std_fseek,
429429
},
430430
{ .def = NULL, .impl = NULL, },
@@ -437,7 +437,7 @@ static const struct fastrpc_function_impl apps_std_procs[] = {
437437
{ .def = NULL, .impl = NULL, },
438438
{ .def = NULL, .impl = NULL, },
439439
{
440-
.def = &apps_std_fopen_with_env_def,
440+
.def = &apps_std3_fopen_with_env_def,
441441
.impl = apps_std_fopen_with_env,
442442
},
443443
{ .def = NULL, .impl = NULL, },
@@ -447,21 +447,21 @@ static const struct fastrpc_function_impl apps_std_procs[] = {
447447
{ .def = NULL, .impl = NULL, },
448448
{ .def = NULL, .impl = NULL, },
449449
{
450-
.def = &apps_std_opendir_def,
450+
.def = &apps_std3_opendir_def,
451451
.impl = apps_std_opendir,
452452
},
453453
{
454-
.def = &apps_std_closedir_def,
454+
.def = &apps_std3_closedir_def,
455455
.impl = apps_std_closedir,
456456
},
457457
{
458-
.def = &apps_std_readdir_def,
458+
.def = &apps_std3_readdir_def,
459459
.impl = apps_std_readdir,
460460
},
461461
{ .def = NULL, .impl = NULL, },
462462
{ .def = NULL, .impl = NULL, },
463463
{
464-
.def = &apps_std_stat_def,
464+
.def = &apps_std3_stat_def,
465465
.impl = apps_std_stat,
466466
},
467467
};

hexagonrpcd/listener.c

Lines changed: 122 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -24,87 +24,142 @@
2424
#include <libhexagonrpc/fastrpc.h>
2525
#include <libhexagonrpc/interfaces/remotectl.def>
2626
#include <stddef.h>
27+
#include <stdint.h>
2728
#include <stdio.h>
2829

2930
#include "interfaces/adsp_listener.def"
3031
#include "iobuffer.h"
3132
#include "listener.h"
3233

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)
3537
{
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;
5052

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;
5462

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++;
6074
}
6175

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;
6484

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+
}
7090
}
7191

72-
return out;
92+
if (n_def_outbufs != n_outbufs) {
93+
fprintf(stderr, "listener: wrong amount of output buffers\n");
94+
return 1;
95+
}
7396

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+
}
77101

78-
err_free_out:
79-
free(out);
80-
return NULL;
102+
return 0;
81103
}
82104

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)
85107
{
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+
}
98129
}
99130

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++;
104142
}
105143
}
106144

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;
108163
}
109164

110165
static int return_for_next_invoke(int fd,
@@ -189,11 +244,16 @@ static int invoke_requested_procedure(size_t n_ifaces,
189244
struct fastrpc_io_buffer **returned)
190245
{
191246
const struct fastrpc_function_impl *impl;
192-
uint8_t in_count;
193-
uint8_t out_count;
194247
uint32_t method = REMOTE_SCALARS_METHOD(sc);
195248
int ret;
196249

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+
197257
if (sc & 0xff) {
198258
fprintf(stderr, "Handles are not supported, but got %u in, %u out\n",
199259
(sc & 0xf0) >> 4, sc & 0xf);
@@ -221,26 +281,15 @@ static int invoke_requested_procedure(size_t n_ifaces,
221281
return 1;
222282
}
223283

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);
237286
if (ret) {
238287
*result = AEE_EBADPARM;
239288
return 1;
240289
}
241290

242291
*returned = allocate_outbufs(impl->def, decoded[0].p);
243-
if (*returned == NULL && out_count > 0) {
292+
if (*returned == NULL && impl->def->n_args > 0) {
244293
perror("Could not allocate output buffers");
245294
*result = AEE_ENOMEMORY;
246295
return 1;

hexagonrpcd/listener.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
#include "iobuffer.h"
3030

3131
struct fastrpc_function_impl {
32-
const struct fastrpc_function_def_interp2 *def;
32+
const struct hrpc_method_def_interp3 *def;
3333
uint32_t (*impl)(void *data,
3434
const struct fastrpc_io_buffer *inbufs,
3535
struct fastrpc_io_buffer *outbufs);

hexagonrpcd/localctl.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,14 @@ void fastrpc_localctl_deinit(struct fastrpc_interface *iface)
145145
}
146146

147147
static const struct fastrpc_function_impl localctl_procs[] = {
148-
{ .def = &remotectl_open_def, .impl = localctl_open, },
149-
{ .def = &remotectl_close_def, .impl = localctl_close, },
148+
{
149+
.def = &remotectl3_open_def,
150+
.impl = localctl_open,
151+
},
152+
{
153+
.def = &remotectl3_close_def,
154+
.impl = localctl_close,
155+
},
150156
};
151157

152158
const struct fastrpc_interface localctl_interface = {

0 commit comments

Comments
 (0)