@mikrojs/quickjs 0.18.0 → 0.18.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -30364,27 +30364,36 @@ static void js_free_module_def(JSContext *ctx, JSModuleDef *m)
30364
30364
 
30365
30365
  JS_FreeAtom(ctx, m->module_name);
30366
30366
 
30367
- for(i = 0; i < m->req_module_entries_count; i++) {
30368
- JSReqModuleEntry *rme = &m->req_module_entries[i];
30369
- JS_FreeAtom(ctx, rme->module_name);
30370
- JS_FreeValue(ctx, rme->attributes);
30367
+ /* mikrojs patch: a partially-deserialized module (JS_ReadModule OOM
30368
+ mid-read) can carry a non-zero entry count with a NULL array — the
30369
+ count is decoded before the array allocation. Guard each loop. */
30370
+ if (m->req_module_entries) {
30371
+ for(i = 0; i < m->req_module_entries_count; i++) {
30372
+ JSReqModuleEntry *rme = &m->req_module_entries[i];
30373
+ JS_FreeAtom(ctx, rme->module_name);
30374
+ JS_FreeValue(ctx, rme->attributes);
30375
+ }
30371
30376
  }
30372
30377
  js_free(ctx, m->req_module_entries);
30373
30378
 
30374
- for(i = 0; i < m->export_entries_count; i++) {
30375
- JSExportEntry *me = &m->export_entries[i];
30376
- if (me->export_type == JS_EXPORT_TYPE_LOCAL)
30377
- free_var_ref(ctx->rt, me->u.local.var_ref);
30378
- JS_FreeAtom(ctx, me->export_name);
30379
- JS_FreeAtom(ctx, me->local_name);
30379
+ if (m->export_entries) {
30380
+ for(i = 0; i < m->export_entries_count; i++) {
30381
+ JSExportEntry *me = &m->export_entries[i];
30382
+ if (me->export_type == JS_EXPORT_TYPE_LOCAL)
30383
+ free_var_ref(ctx->rt, me->u.local.var_ref);
30384
+ JS_FreeAtom(ctx, me->export_name);
30385
+ JS_FreeAtom(ctx, me->local_name);
30386
+ }
30380
30387
  }
30381
30388
  js_free(ctx, m->export_entries);
30382
30389
 
30383
30390
  js_free(ctx, m->star_export_entries);
30384
30391
 
30385
- for(i = 0; i < m->import_entries_count; i++) {
30386
- JSImportEntry *mi = &m->import_entries[i];
30387
- JS_FreeAtom(ctx, mi->import_name);
30392
+ if (m->import_entries) {
30393
+ for(i = 0; i < m->import_entries_count; i++) {
30394
+ JSImportEntry *mi = &m->import_entries[i];
30395
+ JS_FreeAtom(ctx, mi->import_name);
30396
+ }
30388
30397
  }
30389
30398
  js_free(ctx, m->import_entries);
30390
30399
  js_free(ctx, m->async_parent_modules);
@@ -34181,6 +34190,14 @@ static int resolve_scope_var(JSContext *ctx, JSFunctionDef *s,
34181
34190
  idx = get_closure_var(ctx, s, fd, JS_CLOSURE_LOCAL,
34182
34191
  fd->var_object_idx, vd->var_name,
34183
34192
  false, false, JS_VAR_NORMAL);
34193
+ /* mikrojs patch: get_closure_var fails under OOM; the -1 index
34194
+ would be emitted as var_ref 65535 (or dereference
34195
+ closure_var[-1] at has_idx). Poison the dbuf so
34196
+ resolve_variables reports the error. */
34197
+ if (idx < 0) {
34198
+ dbuf_set_error(bc);
34199
+ goto done;
34200
+ }
34184
34201
  dbuf_putc(bc, OP_get_var_ref);
34185
34202
  dbuf_put_u16(bc, idx);
34186
34203
  var_object_test(ctx, s, var_name, op, bc, &label_done, 0);
@@ -34193,6 +34210,11 @@ static int resolve_scope_var(JSContext *ctx, JSFunctionDef *s,
34193
34210
  idx = get_closure_var(ctx, s, fd, JS_CLOSURE_LOCAL,
34194
34211
  fd->arg_var_object_idx, vd->var_name,
34195
34212
  false, false, JS_VAR_NORMAL);
34213
+ /* mikrojs patch: see the eval object case above */
34214
+ if (idx < 0) {
34215
+ dbuf_set_error(bc);
34216
+ goto done;
34217
+ }
34196
34218
  dbuf_putc(bc, OP_get_var_ref);
34197
34219
  dbuf_put_u16(bc, idx);
34198
34220
  var_object_test(ctx, s, var_name, op, bc, &label_done, 0);
@@ -34220,6 +34242,11 @@ static int resolve_scope_var(JSContext *ctx, JSFunctionDef *s,
34220
34242
  } else {
34221
34243
  idx = idx1;
34222
34244
  }
34245
+ /* mikrojs patch: see the eval object case above */
34246
+ if (idx < 0) {
34247
+ dbuf_set_error(bc);
34248
+ goto done;
34249
+ }
34223
34250
  goto has_idx;
34224
34251
  } else if ((cv->var_name == JS_ATOM__var_ ||
34225
34252
  cv->var_name == JS_ATOM__arg_var_ ||
@@ -34234,6 +34261,11 @@ static int resolve_scope_var(JSContext *ctx, JSFunctionDef *s,
34234
34261
  } else {
34235
34262
  idx = idx1;
34236
34263
  }
34264
+ /* mikrojs patch: see the eval object case above */
34265
+ if (idx < 0) {
34266
+ dbuf_set_error(bc);
34267
+ goto done;
34268
+ }
34237
34269
  dbuf_putc(bc, OP_get_var_ref);
34238
34270
  dbuf_put_u16(bc, idx);
34239
34271
  var_object_test(ctx, s, var_name, op, bc, &label_done, is_with);
@@ -35939,18 +35971,24 @@ static __exception int resolve_labels(JSContext *ctx, JSFunctionDef *s)
35939
35971
  for(re = ls->first_reloc; re != NULL; re = re_next) {
35940
35972
  int diff = ls->addr - re->addr;
35941
35973
  re_next = re->next;
35942
- switch (re->size) {
35943
- case 4:
35944
- put_u32(bc_out.buf + re->addr, diff);
35945
- break;
35946
- case 2:
35947
- assert(diff == (int16_t)diff);
35948
- put_u16(bc_out.buf + re->addr, diff);
35949
- break;
35950
- case 1:
35951
- assert(diff == (int8_t)diff);
35952
- put_u8(bc_out.buf + re->addr, diff);
35953
- break;
35974
+ /* mikrojs patch: after a dbuf OOM the recorded addresses
35975
+ no longer match the buffer contents (and the buffer may
35976
+ be NULL); skip the writes and let the dbuf_error check
35977
+ at the end of resolve_labels report the failure. */
35978
+ if (!dbuf_error(&bc_out)) {
35979
+ switch (re->size) {
35980
+ case 4:
35981
+ put_u32(bc_out.buf + re->addr, diff);
35982
+ break;
35983
+ case 2:
35984
+ assert(diff == (int16_t)diff);
35985
+ put_u16(bc_out.buf + re->addr, diff);
35986
+ break;
35987
+ case 1:
35988
+ assert(diff == (int8_t)diff);
35989
+ put_u8(bc_out.buf + re->addr, diff);
35990
+ break;
35991
+ }
35954
35992
  }
35955
35993
  js_free(ctx, re);
35956
35994
  }
@@ -36635,6 +36673,11 @@ static __exception int resolve_labels(JSContext *ctx, JSFunctionDef *s)
36635
36673
 
36636
36674
  /* more jump optimizations */
36637
36675
  patch_offsets = 0;
36676
+ /* mikrojs patch: on dbuf OOM the recorded jump/label offsets are stale
36677
+ relative to the buffer contents; skip the peephole passes and let the
36678
+ dbuf_error check below report the failure. */
36679
+ if (dbuf_error(&bc_out))
36680
+ goto skip_peephole;
36638
36681
  for (i = 0, jp = s->jump_slots; i < s->jump_count; i++, jp++) {
36639
36682
  LabelSlot *ls;
36640
36683
  JumpSlot *jp1;
@@ -36708,6 +36751,7 @@ static __exception int resolve_labels(JSContext *ctx, JSFunctionDef *s)
36708
36751
  }
36709
36752
  }
36710
36753
 
36754
+ skip_peephole: /* mikrojs patch */
36711
36755
  js_free(ctx, s->jump_slots);
36712
36756
  s->jump_slots = NULL;
36713
36757
  js_free(ctx, s->label_slots);
@@ -37096,6 +37140,14 @@ static JSValue js_create_function(JSContext *ctx, JSFunctionDef *fd)
37096
37140
  }
37097
37141
  #endif
37098
37142
 
37143
+ /* mikrojs patch: a dbuf error during emission leaves byte_code truncated
37144
+ relative to its opcode structure; the resolve passes would then read
37145
+ past the end of the buffer. Fail before walking it. */
37146
+ if (dbuf_error(&fd->byte_code)) {
37147
+ JS_ThrowOutOfMemory(ctx);
37148
+ goto fail;
37149
+ }
37150
+
37099
37151
  if (resolve_variables(ctx, fd))
37100
37152
  goto fail;
37101
37153
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mikrojs/quickjs",
3
- "version": "0.18.0",
3
+ "version": "0.18.1",
4
4
  "description": "QuickJS-NG engine source, shared CMake module, and qjsc bytecode compiler",
5
5
  "keywords": [
6
6
  "bytecode",