Skip to content

Commit 94bc54f

Browse files
committed
ports/cheriot-rtos: Bugfixes and scrapped C++ closures.
Signed-off-by: Duncan Lowther <[email protected]>
1 parent 127e480 commit 94bc54f

File tree

6 files changed

+37
-29
lines changed

6 files changed

+37
-29
lines changed

lib/berkeley-db-1.xx

Submodule berkeley-db-1.xx updated 212 files

lib/mbedtls

lib/micropython-lib

Submodule micropython-lib updated 195 files

ports/cheriot-rtos/main.cpp

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
#include "mp_entry.h"
77
#include "mphalport.h"
88

9-
#include <stdio.h>
10-
119
void __cheri_compartment("main") entry(void) {
1210
*MMIO_CAPABILITY(uint32_t, gpio) = 0xaa;
1311
printf("Test\n");
@@ -41,6 +39,26 @@ void __cheri_compartment("main") entry(void) {
4139
printf("File-mode string execution exited with a failure\n");
4240
return;
4341
}
42+
case 'E':
43+
if(!ctx.exec_str_file("def foo(a,b):\n print(a)\n return [a , b]\n\ndef bar(a,b):\n for x in a:\n print(x,b)\n\nprint('created functions `foo` and `bar`')\n")) {
44+
std::optional<SObj> ret = ctx.exec_func<SObj>("foo", "baz", 7);
45+
if(ret) {
46+
printf("Call to python `foo('baz', 7)` returned SOobj <%x>\n", (ptraddr_t)*ret);
47+
if(ctx.exec_func<void>("bar", *ret, "quux")) {
48+
printf("Call to python `bar(foo('baz',7), 'quux')` succeeded.\n");
49+
} else {
50+
printf("Call to python `bar(foo('baz',7), 'quux')` failed.\n");
51+
}
52+
ctx.free_obj_handle(*ret);
53+
continue;
54+
} else {
55+
printf("Call to python `foo('bar', 7)` failed.\n");
56+
return;
57+
}
58+
} else {
59+
printf("File-mode string execution exited with a failure\n");
60+
return;
61+
}
4462
case 'q':
4563
printf("Exiting\n");
4664
return;

ports/cheriot-rtos/mp_entry.c

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ int __cheri_compartment("mp_vm") mp_exec_str_file(SObj ctx, const char *src) {
6767
}
6868

6969
int __cheri_compartment("mp_vm") mp_free_obj_handle(SObj ctx, SObj obj) {
70+
MP_STATE_THREAD_HACK_INIT(token_obj_unseal(mp_ctx_key, ctx))
7071
obj_export_handle_t * handle = token_obj_unseal(MP_STATE_VM(obj_key), obj);
7172
if(!handle) return -1;
7273
if(handle->next) handle->next->prevnext = handle->prevnext;
@@ -101,7 +102,7 @@ static int mp_obj_to_cobj(void * ret, char type, mp_obj_t in) {
101102
obj->obj = in;
102103
obj->next = MP_STATE_VM(obj_export_head);
103104
obj->prevnext = &MP_STATE_VM(obj_export_head);
104-
MP_STATE_VM(obj_export_head)->prevnext = &obj->next;
105+
if(MP_STATE_VM(obj_export_head)) MP_STATE_VM(obj_export_head)->prevnext = &obj->next;
105106
MP_STATE_VM(obj_export_head) = obj;
106107
return 0;
107108
}
@@ -160,13 +161,13 @@ int __cheri_compartment("mp_vm") mp_exec_func_v(SObj ctx, const char * func, voi
160161
break;
161162
}
162163
case 'C': { /* Cross-compartment callback */
163-
const mp_callback_t cba = va_arg(ap, mp_callback_t);
164-
mp_obj_ext_callback_t * cb = m_new_obj_var(mp_obj_ext_callback_t, sig, char, cba.n_args + 1);
164+
const mp_callback_t* cba = va_arg(ap, mp_callback_t*);
165+
mp_obj_ext_callback_t * cb = m_new_obj_var(mp_obj_ext_callback_t, sig, char, cba->n_args + 1);
165166
cb->base.type = &mp_type_ext_callback;
166-
cb->func = cba.func;
167-
cb->data = cba.data;
168-
cb->n_args = cba.n_args;
169-
memcpy(&cb->sig, cba.sig, cba.n_args + 1);
167+
cb->func = cba->func;
168+
cb->data = cba->data;
169+
cb->n_args = cba->n_args;
170+
memcpy(&cb->sig, cba->sig, cba->n_args + 1);
170171
args[i] = MP_OBJ_FROM_PTR(cb);
171172
break;
172173
}
@@ -246,7 +247,9 @@ SObj __cheri_compartment("mp_vm") mp_vminit(size_t heapsize) {
246247
token_obj_destroy(MALLOC_CAPABILITY, mp_ctx_key, vm_handle);
247248
return INVALID_SOBJ;
248249
}
250+
SKey okey = token_key_new();
249251
MP_STATE_THREAD_HACK_INIT(ctx)
252+
MP_STATE_VM(obj_key) = okey;
250253
#if MICROPY_ENABLE_GC
251254
gc_init(heap, heap + heapsize);
252255
printf("GC initialised\n");

ports/cheriot-rtos/mp_entry.h

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,11 @@ typedef struct {
4343
mp_cb_arg_t __cheri_callback (*func)(void * data, mp_cb_arg_t * args);
4444
void * data;
4545
int n_args;
46-
char * sig;
46+
const char * sig;
4747
} mp_callback_t;
4848

4949
#ifdef __cplusplus
50+
5051
class MicropythonContext {
5152
private:
5253
SObj ctx;
@@ -64,8 +65,9 @@ class MicropythonContext {
6465
static constexpr char sig_chr(const char * s) { return 's'; }
6566
static constexpr char sig_chr(SObj o) { return 'O'; }
6667
static constexpr char sig_chr(void * p) { return 'P'; }
67-
static constexpr char sig_chr(mp_callback_t p){ return 'C'; }
6868
template<typename T> static constexpr char sig_chr_ty = sig_chr(static_cast<T>(0));
69+
template<> static constexpr char sig_chr_ty<mp_callback_t*> = 'C';
70+
template<> static constexpr char sig_chr_ty<const mp_callback_t*> = 'C';
6971
template<typename T> static constexpr char sig_chr_exact = 0;
7072
template<> static constexpr char sig_chr_exact<int> = 'i';
7173
template<> static constexpr char sig_chr_exact<unsigned> = 'I';
@@ -75,17 +77,6 @@ class MicropythonContext {
7577
template<> static constexpr char sig_chr_exact<SObj> = 'O';
7678
template<> static constexpr char sig_chr_exact<mp_callback_t> = 'C';
7779
template<typename T> static constexpr char sig_chr_exact<T*> = 'P';
78-
template<typename R, typename... Ts>
79-
static inline mp_cb_arg_t cb_wrapper_aux(std::function<R(Ts...)> * func, mp_cb_arg_t * empty, Ts... cargs) {
80-
return (*func)(cargs...);
81-
}
82-
template<typename R, typename... Ts, typename T, typename... Us>
83-
static inline mp_cb_arg_t cb_wrapper_aux(std::function<R(Ts..., T, Us...)> * func, mp_cb_arg_t * pyargs, Ts... cargs) {
84-
return cb_wrapper_aux(func, pyargs + 1, cargs..., argconv<T>(pyargs[0]));
85-
}
86-
template<typename R, typename... Ts> static mp_cb_arg_t __cheri_callback cb_wrapper(std::function<R(Ts...)> * func, mp_cb_arg_t * args) {
87-
return cb_wrapper_aux<R, Ts...>(func, args);
88-
}
8980
public:
9081
[[nodiscard]] MicropythonContext(MicropythonContext&& src) : ctx(src.ctx) { src.ctx = INVALID_SOBJ; }
9182
[[nodiscard]] static std::optional<MicropythonContext> create(size_t heapsize) {
@@ -116,10 +107,6 @@ class MicropythonContext {
116107
return !err;
117108
}
118109
int free_obj_handle(SObj obj) { return mp_free_obj_handle(ctx, obj); }
119-
template<typename R, typename... Ts> [[nodiscard]] static mp_callback_t leaf_callback(std::function<R(Ts...)> &func) {
120-
constexpr const char sig[sizeof...(Ts) + 1] = { sig_chr_exact<R>, sig_chr_exact<Ts>... };
121-
return { .func = &cb_wrapper<R, Ts...> , .data = &func , .n_args = sizeof...(Ts), .sig = sig };
122-
}
123110
};
124111

125112
#endif

0 commit comments

Comments
 (0)