Skip to content

Commit ba8b80f

Browse files
authored
Remove broken JS_READ_OBJ_ROM_DATA flag (#216)
This JS_ReadObject() flag no longer works for bytecode. The IC opcodes are patched during execution. Fixes: #206 Refs: #120
1 parent e581286 commit ba8b80f

File tree

2 files changed

+15
-36
lines changed

2 files changed

+15
-36
lines changed

quickjs.c

Lines changed: 14 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -626,8 +626,7 @@ typedef struct JSFunctionBytecode {
626626
uint8_t super_allowed : 1;
627627
uint8_t arguments_allowed : 1;
628628
uint8_t backtrace_barrier : 1; /* stop backtrace on this function */
629-
uint8_t read_only_bytecode : 1;
630-
/* XXX: 4 bits available */
629+
/* XXX: 5 bits available */
631630
uint8_t *byte_code_buf; /* (self pointer) */
632631
int byte_code_len;
633632
JSAtom func_name;
@@ -5770,7 +5769,7 @@ static void compute_bytecode_size(JSFunctionBytecode *b, JSMemoryUsage_helper *h
57705769
if (b->closure_var) {
57715770
js_func_size += b->closure_var_count * sizeof(*b->closure_var);
57725771
}
5773-
if (!b->read_only_bytecode && b->byte_code_buf) {
5772+
if (b->byte_code_buf) {
57745773
hp->js_func_code_size += b->byte_code_len;
57755774
}
57765775
memory_used_count++;
@@ -32117,7 +32116,7 @@ typedef enum BCTagEnum {
3211732116
BC_TAG_OBJECT_REFERENCE,
3211832117
} BCTagEnum;
3211932118

32120-
#define BC_VERSION 6
32119+
#define BC_VERSION 7
3212132120

3212232121
typedef struct BCWriterState {
3212332122
JSContext *ctx;
@@ -32981,7 +32980,6 @@ typedef struct BCReaderState {
3298132980
int error_state;
3298232981
BOOL allow_sab : 8;
3298332982
BOOL allow_bytecode : 8;
32984-
BOOL is_rom_data : 8;
3298532983
BOOL allow_reference : 8;
3298632984
/* object references */
3298732985
JSObject **objects;
@@ -33213,17 +33211,9 @@ static int JS_ReadFunctionBytecode(BCReaderState *s, JSFunctionBytecode *b,
3321333211
JSAtom atom;
3321433212
uint32_t idx;
3321533213

33216-
if (s->is_rom_data) {
33217-
/* directly use the input buffer */
33218-
if (unlikely(s->buf_end - s->ptr < bc_len))
33219-
return bc_read_error_end(s);
33220-
bc_buf = (uint8_t *)s->ptr;
33221-
s->ptr += bc_len;
33222-
} else {
33223-
bc_buf = (void *)((uint8_t*)b + byte_code_offset);
33224-
if (bc_get_buf(s, bc_buf, bc_len))
33225-
return -1;
33226-
}
33214+
bc_buf = (uint8_t*)b + byte_code_offset;
33215+
if (bc_get_buf(s, bc_buf, bc_len))
33216+
return -1;
3322733217
b->byte_code_buf = bc_buf;
3322833218

3322933219
pos = 0;
@@ -33237,20 +33227,15 @@ static int JS_ReadFunctionBytecode(BCReaderState *s, JSFunctionBytecode *b,
3323733227
case OP_FMT_atom_label_u8:
3323833228
case OP_FMT_atom_label_u16:
3323933229
idx = get_u32(bc_buf + pos + 1);
33240-
if (s->is_rom_data) {
33241-
/* just increment the reference count of the atom */
33242-
JS_DupAtom(s->ctx, (JSAtom)idx);
33243-
} else {
33244-
if (bc_idx_to_atom(s, &atom, idx)) {
33245-
/* Note: the atoms will be freed up to this position */
33246-
b->byte_code_len = pos;
33247-
return -1;
33248-
}
33249-
put_u32(bc_buf + pos + 1, atom);
33230+
if (bc_idx_to_atom(s, &atom, idx)) {
33231+
/* Note: the atoms will be freed up to this position */
33232+
b->byte_code_len = pos;
33233+
return -1;
33234+
}
33235+
put_u32(bc_buf + pos + 1, atom);
3325033236
#ifdef DUMP_READ_OBJECT
33251-
bc_read_trace(s, "at %d, fixup atom: ", pos + 1); print_atom(s->ctx, atom); printf("\n");
33237+
bc_read_trace(s, "at %d, fixup atom: ", pos + 1); print_atom(s->ctx, atom); printf("\n");
3325233238
#endif
33253-
}
3325433239
break;
3325533240
default:
3325633241
break;
@@ -33394,7 +33379,6 @@ static JSValue JS_ReadFunctionTag(BCReaderState *s)
3339433379
bc.super_allowed = bc_get_flags(v16, &idx, 1);
3339533380
bc.arguments_allowed = bc_get_flags(v16, &idx, 1);
3339633381
bc.backtrace_barrier = bc_get_flags(v16, &idx, 1);
33397-
bc.read_only_bytecode = s->is_rom_data;
3339833382
if (bc_get_u8(s, &v8))
3339933383
goto fail;
3340033384
bc.js_mode = v8;
@@ -33425,9 +33409,7 @@ static JSValue JS_ReadFunctionTag(BCReaderState *s)
3342533409
closure_var_offset = function_size;
3342633410
function_size += bc.closure_var_count * sizeof(*bc.closure_var);
3342733411
byte_code_offset = function_size;
33428-
if (!bc.read_only_bytecode) {
33429-
function_size += bc.byte_code_len;
33430-
}
33412+
function_size += bc.byte_code_len;
3343133413

3343233414
b = js_mallocz(ctx, function_size);
3343333415
if (!b)
@@ -34071,8 +34053,6 @@ static int JS_ReadObjectAtoms(BCReaderState *s)
3407134053
if (atom == JS_ATOM_NULL)
3407234054
return s->error_state = -1;
3407334055
s->idx_to_atom[i] = atom;
34074-
if (s->is_rom_data && (atom != (i + s->first_atom)))
34075-
s->is_rom_data = FALSE; /* atoms must be relocated */
3407634056
}
3407734057
bc_read_trace(s, "}\n");
3407834058
return 0;
@@ -34105,7 +34085,6 @@ JSValue JS_ReadObject(JSContext *ctx, const uint8_t *buf, size_t buf_len,
3410534085
s->buf_end = buf + buf_len;
3410634086
s->ptr = buf;
3410734087
s->allow_bytecode = ((flags & JS_READ_OBJ_BYTECODE) != 0);
34108-
s->is_rom_data = ((flags & JS_READ_OBJ_ROM_DATA) != 0);
3410934088
s->allow_sab = ((flags & JS_READ_OBJ_SAB) != 0);
3411034089
s->allow_reference = ((flags & JS_READ_OBJ_REFERENCE) != 0);
3411134090
if (s->allow_bytecode)

quickjs.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -812,7 +812,7 @@ JS_EXTERN uint8_t *JS_WriteObject2(JSContext *ctx, size_t *psize, JSValue obj,
812812
int flags, uint8_t ***psab_tab, size_t *psab_tab_len);
813813

814814
#define JS_READ_OBJ_BYTECODE (1 << 0) /* allow function/module */
815-
#define JS_READ_OBJ_ROM_DATA (1 << 1) /* avoid duplicating 'buf' data */
815+
#define JS_READ_OBJ_ROM_DATA (0) /* avoid duplicating 'buf' data (obsolete, broken by ICs) */
816816
#define JS_READ_OBJ_SAB (1 << 2) /* allow SharedArrayBuffer */
817817
#define JS_READ_OBJ_REFERENCE (1 << 3) /* allow object references */
818818
JS_EXTERN JSValue JS_ReadObject(JSContext *ctx, const uint8_t *buf, size_t buf_len, int flags);

0 commit comments

Comments
 (0)