Skip to content

Commit

Permalink
Add stack protection and memory usage
Browse files Browse the repository at this point in the history
Add stack protection by explicit stack setting
Dump memory usage(heap/stack) via macro
  • Loading branch information
XuJiandong committed Oct 31, 2023
1 parent 21c216b commit fb66fb6
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 4 deletions.
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ CFLAGS += -Wno-incompatible-library-redeclaration -Wno-implicit-const-int-float-

CFLAGS += -DCKB_DECLARATION_ONLY
CFLAGS += -D__BYTE_ORDER=1234 -D__LITTLE_ENDIAN=1234 -D__ISO_C_VISIBLE=1999 -D__GNU_VISIBLE
CFLAGS += -DCKB_MALLOC_DECLARATION_ONLY -DCKB_PRINTF_DECLARATION_ONLY -DCONFIG_BIGNUM
CFLAGS += -DCKB_MALLOC_DECLARATION_ONLY -DCKB_PRINTF_DECLARATION_ONLY -DCONFIG_BIGNUM -DCONFIG_STACK_CHECK
# uncomment to dump memory usage
# CFLAGS += -DMEMORY_USAGE

LDFLAGS := -static --gc-sections
LDFLAGS += -Ldeps/compiler-rt-builtins-riscv/build -lcompiler-rt
Expand Down
10 changes: 10 additions & 0 deletions include/c-stdlib/my_malloc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#ifndef _STDLIB_MALLOC_H_
#define _STDLIB_MALLOC_H_

#include <stdint.h>

size_t malloc_usable_size(void *ptr);
void malloc_config(uintptr_t min, uintptr_t max);
size_t malloc_usage();

#endif // _STDLIB_MALLOC_H_
11 changes: 11 additions & 0 deletions include/c-stdlib/src/malloc_impl.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ void malloc_config(uintptr_t min, uintptr_t max) {
s_program_break = 0;
}

size_t malloc_usage() {
size_t high = (size_t)s_program_break;
size_t low = (size_t)s_brk_min;
return high - low;
}

void *_sbrk(uintptr_t incr) {
if (!s_program_break) {
s_program_break = s_brk_min;
Expand Down Expand Up @@ -385,3 +391,8 @@ void free(void *p) {
struct chunk *self = CKB_MEM_TO_CHUNK(p);
__bin_chunk(self);
}

size_t malloc_usable_size(void *ptr) {
struct chunk *c = CKB_MEM_TO_CHUNK(ptr);
return CKB_CHUNK_PSIZE(c);
}
11 changes: 10 additions & 1 deletion quickjs/qjs.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include <string.h>
#include <stddef.h>
#include <stdbool.h>
#include "my_malloc.h"
#include "cutils.h"
#include "std_module.h"
#include "ckb_module.h"
Expand Down Expand Up @@ -290,7 +291,7 @@ int main(int argc, const char **argv) {
JSRuntime *rt = NULL;
JSContext *ctx = NULL;
size_t memory_limit = 0;
size_t stack_size = 0;
size_t stack_size = 1024 * 1020;
size_t optind = 1;
RunJSType type = parse_args(argc, argv);
if (type == RunJsError) {
Expand Down Expand Up @@ -355,6 +356,14 @@ int main(int argc, const char **argv) {
return -1;
}
CHECK(err);

#ifdef MEMORY_USAGE
size_t heap_usage = malloc_usage();
printf("Total bytes used by allocator(malloc/realloc) is %d K", heap_usage / 1024);
size_t stack_usage = JS_GetStackPeak();
printf("Total bytes used by stack(peak value) is %d K", (4 * 1024 * 1024 - stack_usage) / 1024);
#endif

exit:
// No cleanup is needed.
// js_std_free_handlers(rt);
Expand Down
12 changes: 11 additions & 1 deletion quickjs/quickjs.c
Original file line number Diff line number Diff line change
Expand Up @@ -1589,6 +1589,7 @@ static inline BOOL js_check_stack_overflow(JSRuntime *rt, size_t alloca_size)
return FALSE;
}
#else
uintptr_t s_stack_peak = 4*1024*1024;
/* Note: OS and CPU dependent */
static inline uintptr_t js_get_stack_pointer(void)
{
Expand All @@ -1598,9 +1599,18 @@ static inline uintptr_t js_get_stack_pointer(void)
static inline BOOL js_check_stack_overflow(JSRuntime *rt, size_t alloca_size)
{
uintptr_t sp;
sp = js_get_stack_pointer() - alloca_size;
uintptr_t stack = js_get_stack_pointer();
if (stack < s_stack_peak) {
s_stack_peak = stack;
}
sp = stack - alloca_size;
return unlikely(sp < rt->stack_limit);
}

uintptr_t JS_GetStackPeak() {
return s_stack_peak;
}

#endif

JSRuntime *JS_NewRuntime2(const JSMallocFunctions *mf, void *opaque)
Expand Down
1 change: 1 addition & 0 deletions quickjs/quickjs.h
Original file line number Diff line number Diff line change
Expand Up @@ -1051,6 +1051,7 @@ int JS_SetModuleExport(JSContext *ctx, JSModuleDef *m, const char *export_name,
JSValue val);
int JS_SetModuleExportList(JSContext *ctx, JSModuleDef *m,
const JSCFunctionListEntry *tab, int len);
uintptr_t JS_GetStackPeak();

#undef js_unlikely
#undef js_force_inline
Expand Down
5 changes: 4 additions & 1 deletion tests/ckb_js_tests/test_data/simple_udt.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,7 @@ function main() {
return 0;
}

ckb.exit(main());
let exit_code = main();
if (exit_code != 0) {
ckb.exit(exit_code);
}

0 comments on commit fb66fb6

Please sign in to comment.