Skip to content

Port recent OOM-handling and memory leak fixes from bellard/quickjs #1069

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
119 changes: 93 additions & 26 deletions quickjs.c
Original file line number Diff line number Diff line change
Expand Up @@ -5284,6 +5284,10 @@ JSValue JS_NewCFunction3(JSContext *ctx, JSCFunction *func,
if (!name)
name = "";
name_atom = JS_NewAtom(ctx, name);
if (name_atom == JS_ATOM_NULL) {
JS_FreeValue(ctx, func_obj);
return JS_EXCEPTION;
}
js_function_set_properties(ctx, func_obj, name_atom, length);
JS_FreeAtom(ctx, name_atom);
return func_obj;
Expand Down Expand Up @@ -8486,6 +8490,8 @@ JSValue JS_GetPropertyStr(JSContext *ctx, JSValueConst this_obj,
JSAtom atom;
JSValue ret;
atom = JS_NewAtom(ctx, prop);
if (atom == JS_ATOM_NULL)
return JS_EXCEPTION;
ret = JS_GetProperty(ctx, this_obj, atom);
JS_FreeAtom(ctx, atom);
return ret;
Expand Down Expand Up @@ -9273,6 +9279,10 @@ int JS_SetPropertyStr(JSContext *ctx, JSValueConst this_obj,
JSAtom atom;
int ret;
atom = JS_NewAtom(ctx, prop);
if (atom == JS_ATOM_NULL) {
JS_FreeValue(ctx, val);
return -1;
}
ret = JS_SetPropertyInternal(ctx, this_obj, atom, val, JS_PROP_THROW);
JS_FreeAtom(ctx, atom);
return ret;
Expand Down Expand Up @@ -9824,6 +9834,10 @@ int JS_DefinePropertyValueStr(JSContext *ctx, JSValueConst this_obj,
JSAtom atom;
int ret;
atom = JS_NewAtom(ctx, prop);
if (atom == JS_ATOM_NULL) {
JS_FreeValue(ctx, val);
return -1;
}
ret = JS_DefinePropertyValue(ctx, this_obj, atom, val, flags);
JS_FreeAtom(ctx, atom);
return ret;
Expand Down Expand Up @@ -20552,6 +20566,7 @@ static __exception int js_parse_template_part(JSParseState *s,
const uint8_t *p_next;
uint32_t c;
StringBuffer b_s, *b = &b_s;
JSValue str;

/* p points to the first byte of the template part */
if (string_buffer_init(s->ctx, b, 32))
Expand Down Expand Up @@ -20597,9 +20612,12 @@ static __exception int js_parse_template_part(JSParseState *s,
if (string_buffer_putc(b, c))
goto fail;
}
str = string_buffer_end(b);
if (JS_IsException(str))
return -1;
s->token.val = TOK_TEMPLATE;
s->token.u.str.sep = c;
s->token.u.str.str = string_buffer_end(b);
s->token.u.str.str = str;
s->buf_ptr = p;
return 0;

Expand All @@ -20618,6 +20636,7 @@ static __exception int js_parse_string(JSParseState *s, int sep,
int ret;
uint32_t c;
StringBuffer b_s, *b = &b_s;
JSValue str;

/* string */
if (string_buffer_init(s->ctx, b, 32))
Expand Down Expand Up @@ -20726,9 +20745,12 @@ static __exception int js_parse_string(JSParseState *s, int sep,
if (string_buffer_putc(b, c))
goto fail;
}
str = string_buffer_end(b);
if (JS_IsException(str))
return -1;
token->val = TOK_STRING;
token->u.str.sep = c;
token->u.str.str = string_buffer_end(b);
token->u.str.str = str;
*pp = p;
return 0;

Expand Down Expand Up @@ -20756,6 +20778,7 @@ static __exception int js_parse_regexp(JSParseState *s)
StringBuffer b_s, *b = &b_s;
StringBuffer b2_s, *b2 = &b2_s;
uint32_t c;
JSValue body_str, flags_str;

p = s->buf_ptr;
p++;
Expand Down Expand Up @@ -20828,9 +20851,17 @@ static __exception int js_parse_regexp(JSParseState *s)
p = p_next;
}

body_str = string_buffer_end(b);
flags_str = string_buffer_end(b2);
if (JS_IsException(body_str) ||
JS_IsException(flags_str)) {
JS_FreeValue(s->ctx, body_str);
JS_FreeValue(s->ctx, flags_str);
return -1;
}
s->token.val = TOK_REGEXP;
s->token.u.regexp.body = string_buffer_end(b);
s->token.u.regexp.flags = string_buffer_end(b2);
s->token.u.regexp.body = body_str;
s->token.u.regexp.flags = flags_str;
s->buf_ptr = p;
return 0;
fail:
Expand Down Expand Up @@ -21860,7 +21891,7 @@ static void skip_shebang(const uint8_t **pp, const uint8_t *buf_end)
}

static inline int get_prev_opcode(JSFunctionDef *fd) {
if (fd->last_opcode_pos < 0)
if (fd->last_opcode_pos < 0 || dbuf_error(&fd->byte_code))
return OP_invalid;
else
return fd->byte_code.buf[fd->last_opcode_pos];
Expand Down Expand Up @@ -21921,7 +21952,11 @@ static void emit_op(JSParseState *s, uint8_t val)

static void emit_atom(JSParseState *s, JSAtom name)
{
emit_u32(s, JS_DupAtom(s->ctx, name));
DynBuf *bc = &s->cur_func->byte_code;
if (dbuf_realloc(bc, bc->size + 4))
return; /* not enough memory : don't duplicate the atom */
put_u32(bc->buf + bc->size, JS_DupAtom(s->ctx, name));
bc->size += 4;
}

static int update_label(JSFunctionDef *s, int label, int delta)
Expand All @@ -21935,29 +21970,33 @@ static int update_label(JSFunctionDef *s, int label, int delta)
return ls->ref_count;
}

static int new_label_fd(JSFunctionDef *fd, int label)
static int new_label_fd(JSFunctionDef *fd)
{
int label;
LabelSlot *ls;

if (label < 0) {
if (js_resize_array(fd->ctx, (void *)&fd->label_slots,
if (js_resize_array(fd->ctx, (void *)&fd->label_slots,
sizeof(fd->label_slots[0]),
&fd->label_size, fd->label_count + 1))
return -1;
label = fd->label_count++;
ls = &fd->label_slots[label];
ls->ref_count = 0;
ls->pos = -1;
ls->pos2 = -1;
ls->addr = -1;
ls->first_reloc = NULL;
}
return -1;
label = fd->label_count++;
ls = &fd->label_slots[label];
ls->ref_count = 0;
ls->pos = -1;
ls->pos2 = -1;
ls->addr = -1;
ls->first_reloc = NULL;
return label;
}

static int new_label(JSParseState *s)
{
return new_label_fd(s->cur_func, -1);
int label;
label = new_label_fd(s->cur_func);
if (unlikely(label < 0)) {
dbuf_set_error(&s->cur_func->byte_code);
}
return label;
}

/* don't update the last opcode and don't emit line number info */
Expand Down Expand Up @@ -21985,8 +22024,11 @@ static int emit_label(JSParseState *s, int label)
static int emit_goto(JSParseState *s, int opcode, int label)
{
if (js_is_live_code(s)) {
if (label < 0)
if (label < 0) {
label = new_label(s);
if (label < 0)
return -1;
}
emit_op(s, opcode);
emit_u32(s, label);
s->cur_func->label_slots[label].ref_count++;
Expand Down Expand Up @@ -24121,6 +24163,8 @@ static __exception int get_lvalue(JSParseState *s, int *popcode, int *pscope,
switch(opcode) {
case OP_scope_get_var:
label = new_label(s);
if (label < 0)
return -1;
emit_op(s, OP_scope_make_ref);
emit_atom(s, name);
emit_u32(s, label);
Expand Down Expand Up @@ -24156,6 +24200,8 @@ static __exception int get_lvalue(JSParseState *s, int *popcode, int *pscope,
switch(opcode) {
case OP_scope_get_var:
label = new_label(s);
if (label < 0)
return -1;
emit_op(s, OP_scope_make_ref);
emit_atom(s, name);
emit_u32(s, label);
Expand Down Expand Up @@ -27861,6 +27907,8 @@ JSModuleDef *JS_NewCModule(JSContext *ctx, const char *name_str,
if (name == JS_ATOM_NULL)
return NULL;
m = js_new_module_def(ctx, name);
if (!m)
return NULL;
m->init_func = func;
return m;
}
Expand Down Expand Up @@ -29964,6 +30012,8 @@ static void free_bytecode_atoms(JSRuntime *rt,
case OP_FMT_atom_u16:
case OP_FMT_atom_label_u8:
case OP_FMT_atom_label_u16:
if ((pos + 1 + 4) > bc_len)
break; /* may happen if there is not enough memory when emiting bytecode */
atom = get_u32(bc_buf + pos + 1);
JS_FreeAtomRT(rt, atom);
break;
Expand Down Expand Up @@ -30781,7 +30831,13 @@ static void var_object_test(JSContext *ctx, JSFunctionDef *s,
{
dbuf_putc(bc, get_with_scope_opcode(op));
dbuf_put_u32(bc, JS_DupAtom(ctx, var_name));
*plabel_done = new_label_fd(s, *plabel_done);
if (*plabel_done < 0) {
*plabel_done = new_label_fd(s);
if (*plabel_done < 0) {
dbuf_set_error(bc);
return;
}
}
dbuf_put_u32(bc, *plabel_done);
dbuf_putc(bc, is_with);
update_label(s, *plabel_done, 1);
Expand Down Expand Up @@ -31825,7 +31881,11 @@ static void instantiate_hoisted_definitions(JSContext *ctx, JSFunctionDef *s, Dy
evaluating the module so that the exported functions are
visible if there are cyclic module references */
if (s->module) {
label_next = new_label_fd(s, -1);
label_next = new_label_fd(s);
if (label_next < 0) {
dbuf_set_error(bc);
return;
}

/* if 'this' is true, initialize the global variables and return */
dbuf_putc(bc, OP_push_this);
Expand Down Expand Up @@ -37545,17 +37605,22 @@ static int JS_InstantiateFunctionListItem(JSContext *ctx, JSValueConst obj,
return 0;
}

void JS_SetPropertyFunctionList(JSContext *ctx, JSValueConst obj,
int JS_SetPropertyFunctionList(JSContext *ctx, JSValueConst obj,
const JSCFunctionListEntry *tab, int len)
{
int i;
int i, ret;

for (i = 0; i < len; i++) {
const JSCFunctionListEntry *e = &tab[i];
JSAtom atom = find_atom(ctx, e->name);
JS_InstantiateFunctionListItem(ctx, obj, atom, e);
if (atom == JS_ATOM_NULL)
return -1;
ret = JS_InstantiateFunctionListItem(ctx, obj, atom, e);
JS_FreeAtom(ctx, atom);
if (ret)
return -1;
}
return 0;
}

int JS_AddModuleExportList(JSContext *ctx, JSModuleDef *m,
Expand Down Expand Up @@ -42956,7 +43021,9 @@ static JSValue js_string_constructor(JSContext *ctx, JSValueConst new_target,
JSString *p1 = JS_VALUE_GET_STRING(val);

obj = js_create_from_ctor(ctx, new_target, JS_CLASS_STRING);
if (!JS_IsException(obj)) {
if (JS_IsException(obj)) {
JS_FreeValue(ctx, val);
} else {
JS_SetObjectData(ctx, obj, val);
JS_DefinePropertyValue(ctx, obj, JS_ATOM_length, js_int32(p1->len), 0);
}
Expand Down
2 changes: 1 addition & 1 deletion quickjs.h
Original file line number Diff line number Diff line change
Expand Up @@ -1224,7 +1224,7 @@ typedef struct JSCFunctionListEntry {
#define JS_ALIAS_DEF(name, from) { name, JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE, JS_DEF_ALIAS, 0, { .alias = { from, -1 } } }
#define JS_ALIAS_BASE_DEF(name, from, base) { name, JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE, JS_DEF_ALIAS, 0, { .alias = { from, base } } }

JS_EXTERN void JS_SetPropertyFunctionList(JSContext *ctx, JSValueConst obj,
JS_EXTERN int JS_SetPropertyFunctionList(JSContext *ctx, JSValueConst obj,
const JSCFunctionListEntry *tab,
int len);

Expand Down
Loading