Skip to content

Commit

Permalink
Merge pull request #19 from chibash/transpiler
Browse files Browse the repository at this point in the history
optimize ROOT_SET macro for shadow stack
  • Loading branch information
maejima-fumika authored Nov 2, 2024
2 parents d83c3c6 + ccfba0a commit a81efec
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 12 deletions.
12 changes: 8 additions & 4 deletions microcontroller/core/include/c-runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,18 @@ struct gc_root_set {
value_t values[1];
};

#define ROOT_SET(name,n) struct { struct gc_root_set* next; uint32_t length; value_t values[n]; } name;\
gc_init_rootset((struct gc_root_set*)&name, n);

#define ROOT_SET_DECL(name,n) struct { struct gc_root_set* next; uint32_t length; value_t values[n]; } name;
#define ROOT_SET_DECL(name,n) struct { struct gc_root_set* next; uint32_t length; value_t values[n]; } name
#define ROOT_SET_INIT(name,n) gc_init_rootset((struct gc_root_set*)&name, n);

#define DELETE_ROOT_SET(name) { gc_root_set_head = name.next; }

#define ROOT_SET(name,n) ROOT_SET_DECL(name,n); ROOT_SET_INIT(name,n)

#define VALUE_UNDEF_2 VALUE_UNDEF, VALUE_UNDEF
#define VALUE_UNDEF_3 VALUE_UNDEF, VALUE_UNDEF, VALUE_UNDEF
#define ROOT_SET_N(name,n,initv) ROOT_SET_DECL(name,n) \
= { .next = gc_root_set_head, .length = n, .values = { initv }}; gc_root_set_head = (struct gc_root_set*)&name;

extern int32_t CR_SECTION try_and_catch(void (*main_function)());

extern int32_t CR_SECTION safe_value_to_int(value_t v);
Expand Down
6 changes: 2 additions & 4 deletions microcontroller/core/src/c-runtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

/*
To run on a 64bit machine (for testing/debugging purpose only),
compile with -DTEST64. To include test code, compile with -DTEST.
So,
cc -DTEST -DTEST64 gc.c -lm
will produce ./a.out that runs test code on a 64bit machine.
compile with -DTEST64.
Typical usecase:
Expand Down Expand Up @@ -1479,6 +1476,7 @@ uint32_t gc_test_run() {
}
#endif

// when you modify this function, also modify ROOT_SET_N macro.
void gc_init_rootset(struct gc_root_set* set, uint32_t length) {
set->next = gc_root_set_head;
if (length > 0) {
Expand Down
14 changes: 10 additions & 4 deletions server/src/transpiler/code-generator/c-runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,14 +260,20 @@ export const minusAnyValue = 'minus_any_value'
export const globalRootSetName = 'global_rootset'

export function makeRootSet(n: number) {
if (n > 0)
return `ROOT_SET(func_rootset, ${n})`
else
if (n < 1)
return ''
else if (n == 1)
return 'ROOT_SET_N(func_rootset,1,VALUE_UNDEF)'
else if (n == 2)
return 'ROOT_SET_N(func_rootset,2,VALUE_UNDEF_2)'
else if (n == 3)
return 'ROOT_SET_N(func_rootset,3,VALUE_UNDEF_3)'
else
return `ROOT_SET(func_rootset,${n})`
}

export function declareRootSet(name: string, n: number) {
return `ROOT_SET_DECL(${name}, ${n})`
return `ROOT_SET_DECL(${name}, ${n});`
}

export function initRootSet(name: string, n: number) {
Expand Down

0 comments on commit a81efec

Please sign in to comment.