Skip to content

Commit e5ae6cf

Browse files
committed
Fix handling of memory limit
Default to 0, which is "disabled", just like the stack limit.
1 parent 2050bc7 commit e5ae6cf

File tree

3 files changed

+18
-13
lines changed

3 files changed

+18
-13
lines changed

qjs.c

+12-10
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,8 @@ static void *js_trace_malloc(JSMallocState *s, size_t size)
199199
/* Do not allocate zero bytes: behavior is platform dependent */
200200
assert(size != 0);
201201

202-
if (unlikely(s->malloc_size + size > s->malloc_limit))
202+
/* When malloc_limit is 0 (unlimited), malloc_limit - 1 will be SIZE_MAX. */
203+
if (unlikely(s->malloc_size + size > s->malloc_limit - 1))
203204
return NULL;
204205
ptr = malloc(size);
205206
js_trace_malloc_printf(s, "A %zd -> %p\n", size, ptr);
@@ -238,7 +239,8 @@ static void *js_trace_realloc(JSMallocState *s, void *ptr, size_t size)
238239
free(ptr);
239240
return NULL;
240241
}
241-
if (s->malloc_size + size - old_size > s->malloc_limit)
242+
/* When malloc_limit is 0 (unlimited), malloc_limit - 1 will be SIZE_MAX. */
243+
if (s->malloc_size + size - old_size > s->malloc_limit - 1)
242244
return NULL;
243245

244246
js_trace_malloc_printf(s, "R %zd %p", size, ptr);
@@ -295,10 +297,10 @@ int main(int argc, char **argv)
295297
int module = -1;
296298
int load_std = 0;
297299
int dump_unhandled_promise_rejection = 0;
298-
size_t memory_limit = 0;
299300
char *include_list[32];
300301
int i, include_count = 0;
301-
size_t stack_size = 0;
302+
int64_t memory_limit = -1;
303+
int64_t stack_size = -1;
302304

303305
argv0 = (JSCFunctionListEntry)JS_PROP_STRING_DEF("argv0", argv[0],
304306
JS_PROP_C_W_E);
@@ -403,7 +405,7 @@ int main(int argc, char **argv)
403405
opt_arg = argv[optind++];
404406
}
405407
// TODO(chqrlie): accept kmg suffixes
406-
memory_limit = (size_t)strtod(opt_arg, NULL);
408+
memory_limit = strtoull(opt_arg, NULL, 0);
407409
break;
408410
}
409411
if (!strcmp(longopt, "stack-size")) {
@@ -415,7 +417,7 @@ int main(int argc, char **argv)
415417
opt_arg = argv[optind++];
416418
}
417419
// TODO(chqrlie): accept kmg suffixes
418-
stack_size = (size_t)strtod(opt_arg, NULL);
420+
stack_size = strtoull(opt_arg, NULL, 0);
419421
break;
420422
}
421423
if (opt) {
@@ -437,10 +439,10 @@ int main(int argc, char **argv)
437439
fprintf(stderr, "qjs: cannot allocate JS runtime\n");
438440
exit(2);
439441
}
440-
if (memory_limit != 0)
441-
JS_SetMemoryLimit(rt, memory_limit);
442-
if (stack_size != 0)
443-
JS_SetMaxStackSize(rt, stack_size);
442+
if (memory_limit >= 0)
443+
JS_SetMemoryLimit(rt, (size_t)memory_limit);
444+
if (stack_size >= 0)
445+
JS_SetMaxStackSize(rt, (size_t)stack_size);
444446
if (dump_flags != 0)
445447
JS_SetDumpFlags(rt, dump_flags);
446448
js_std_set_worker_new_context_func(JS_NewCustomContext);

quickjs.c

+5-3
Original file line numberDiff line numberDiff line change
@@ -1608,7 +1608,7 @@ JSRuntime *JS_NewRuntime2(const JSMallocFunctions *mf, void *opaque)
16081608

16091609
memset(&ms, 0, sizeof(ms));
16101610
ms.opaque = opaque;
1611-
ms.malloc_limit = -1;
1611+
ms.malloc_limit = 0;
16121612

16131613
rt = mf->js_malloc(&ms, sizeof(JSRuntime));
16141614
if (!rt)
@@ -1685,7 +1685,8 @@ static void *js_def_malloc(JSMallocState *s, size_t size)
16851685
/* Do not allocate zero bytes: behavior is platform dependent */
16861686
assert(size != 0);
16871687

1688-
if (unlikely(s->malloc_size + size > s->malloc_limit))
1688+
/* When malloc_limit is 0 (unlimited), malloc_limit - 1 will be SIZE_MAX. */
1689+
if (unlikely(s->malloc_size + size > s->malloc_limit - 1))
16891690
return NULL;
16901691

16911692
ptr = malloc(size);
@@ -1723,7 +1724,8 @@ static void *js_def_realloc(JSMallocState *s, void *ptr, size_t size)
17231724
free(ptr);
17241725
return NULL;
17251726
}
1726-
if (s->malloc_size + size - old_size > s->malloc_limit)
1727+
/* When malloc_limit is 0 (unlimited), malloc_limit - 1 will be SIZE_MAX. */
1728+
if (s->malloc_size + size - old_size > s->malloc_limit - 1)
17271729
return NULL;
17281730

17291731
ptr = realloc(ptr, size);

quickjs.h

+1
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,7 @@ typedef struct JSGCObjectHeader JSGCObjectHeader;
291291
JS_EXTERN JSRuntime *JS_NewRuntime(void);
292292
/* info lifetime must exceed that of rt */
293293
JS_EXTERN void JS_SetRuntimeInfo(JSRuntime *rt, const char *info);
294+
/* use 0 to disable memory limit */
294295
JS_EXTERN void JS_SetMemoryLimit(JSRuntime *rt, size_t limit);
295296
JS_EXTERN void JS_SetDumpFlags(JSRuntime *rt, uint64_t flags);
296297
JS_EXTERN void JS_SetGCThreshold(JSRuntime *rt, size_t gc_threshold);

0 commit comments

Comments
 (0)