quickjs 0.22.0 → 0.22.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.
- checksums.yaml +4 -4
- data/README.md +10 -1
- data/ext/quickjsrb/extconf.rb +1 -1
- data/ext/quickjsrb/quickjsrb.c +562 -51
- data/ext/quickjsrb/quickjsrb.h +176 -12
- data/ext/quickjsrb/quickjsrb_libc.c +17 -0
- data/ext/quickjsrb/quickjsrb_libc.h +14 -0
- data/lib/quickjs/version.rb +1 -1
- data/polyfills/package-lock.json +2 -2
- data/polyfills/package.json +1 -1
- metadata +3 -1
data/ext/quickjsrb/quickjsrb.c
CHANGED
|
@@ -38,6 +38,9 @@ VALUE to_rb_value_substituting(JSContext *ctx, JSValue j_val);
|
|
|
38
38
|
typedef struct ConvState ConvState;
|
|
39
39
|
static VALUE to_rb_value_inner(JSContext *ctx, JSValue j_val, ConvState *conv);
|
|
40
40
|
static VALUE raise_js_exception(JSContext *ctx);
|
|
41
|
+
// Defined beside the renderer that is its main caller; needed here because the
|
|
42
|
+
// conversion meets an exhaustion it cannot raise for one level earlier.
|
|
43
|
+
static bool condemn_if_out_of_memory(VMData *data, JSContext *ctx, JSValueConst j_val);
|
|
41
44
|
static VALUE vm_m_memoryUsage(VALUE r_self);
|
|
42
45
|
static VALUE vm_m_runGC(VALUE r_self);
|
|
43
46
|
static VALUE vm_m_poisoned(VALUE r_self);
|
|
@@ -757,6 +760,22 @@ static VALUE js_plain_object_to_rb(JSContext *ctx, JSValue j_val, ConvState *con
|
|
|
757
760
|
if (JS_GetOwnPropertyNames(ctx, &ptab, &plen, j_val, JS_GPN_STRING_MASK | JS_GPN_ENUM_ONLY) < 0)
|
|
758
761
|
{
|
|
759
762
|
conv_frame_pop(conv);
|
|
763
|
+
// The empty hash is what this has always answered for an enumeration it
|
|
764
|
+
// could not make, and #119 owns the question of whether that should raise.
|
|
765
|
+
// What it must not do is swallow the heap running out: this is the one
|
|
766
|
+
// failure here that outlives the value, and the caller would go on
|
|
767
|
+
// evaluating on the heap that refused.
|
|
768
|
+
JSValue j_pending = JS_GetException(ctx);
|
|
769
|
+
if (condemn_if_out_of_memory(JS_GetContextOpaque(ctx), ctx, j_pending))
|
|
770
|
+
{
|
|
771
|
+
// Handed back rather than dropped: the string site below reports its
|
|
772
|
+
// refusal and so does this one now, or the caller is told {} for an
|
|
773
|
+
// object it had and finds out at the next call. A guest trap that threw
|
|
774
|
+
// still answers {}, which is #119.
|
|
775
|
+
JS_Throw(ctx, j_pending);
|
|
776
|
+
return raise_js_exception(ctx);
|
|
777
|
+
}
|
|
778
|
+
JS_FreeValue(ctx, j_pending);
|
|
760
779
|
return rb_hash_new();
|
|
761
780
|
}
|
|
762
781
|
CONV_FRAME(conv, depth)->ptab = ptab;
|
|
@@ -900,11 +919,172 @@ static bool eval_budget_lapsed(VMData *data)
|
|
|
900
919
|
return data->eval_timer_armed && eval_elapsed_ms(data->eval_time) >= data->eval_time->limit_ms;
|
|
901
920
|
}
|
|
902
921
|
|
|
922
|
+
// The same question, for the readers that hold a context rather than the data.
|
|
903
923
|
bool eval_budget_lapsed_now(JSContext *ctx)
|
|
904
924
|
{
|
|
905
925
|
return eval_budget_lapsed(JS_GetContextOpaque(ctx));
|
|
906
926
|
}
|
|
907
927
|
|
|
928
|
+
// Whether the allocator refused an allocation inside the operation now
|
|
929
|
+
// running. The refusal happens in quickjsrb_malloc, below the guest, before
|
|
930
|
+
// QuickJS has decided whether it can even afford to build an error, so no
|
|
931
|
+
// property is read to find it and there is no sentence for a guest to write.
|
|
932
|
+
// The scope is opened by enter_oom_scope at every public entry point.
|
|
933
|
+
//
|
|
934
|
+
// Necessary, and on its own not sufficient. QuickJS tolerates some refusals:
|
|
935
|
+
// JS_NewShape and JS_NewAtom ignore what resize_shape_hash and
|
|
936
|
+
// JS_ResizeAtomHash answer, and the threshold only advances on success, so
|
|
937
|
+
// near the ceiling every later shape retries the refused block while the
|
|
938
|
+
// runtime stays correct and nothing ever throws. A reader that condemned on
|
|
939
|
+
// the count alone would condemn a healthy VM for whatever the guest threw
|
|
940
|
+
// next, which is the sentence #111 opened with, re-entered through a key the
|
|
941
|
+
// guest cannot write but also cannot see.
|
|
942
|
+
static bool allocator_refused(VMData *data)
|
|
943
|
+
{
|
|
944
|
+
return data->alloc_refusals > data->alloc_refusals_at_scope;
|
|
945
|
+
}
|
|
946
|
+
|
|
947
|
+
// Whether a value is a Ruby exception the bridge parked, read the way the
|
|
948
|
+
// message is read: an own data property only, so nothing guest-written runs,
|
|
949
|
+
// and a lookup that leaves alive_objects alone (find_ruby_error is the one
|
|
950
|
+
// thing allowed to take an entry out of it). A bridged error is the host's
|
|
951
|
+
// news and never QuickJS's, whatever its message happens to say.
|
|
952
|
+
static bool js_carries_bridged_error(JSContext *ctx, JSValueConst j_val, VMData *data)
|
|
953
|
+
{
|
|
954
|
+
// The lookup below reads the Ruby heap, and the rejection tracker reaches
|
|
955
|
+
// this from inside a GVL-released evaluation: the latch sits above the
|
|
956
|
+
// listener check on purpose, and that check was what used to keep every
|
|
957
|
+
// Ruby call here on the GVL-held path. Nothing is lost by refusing to look,
|
|
958
|
+
// because can_eval_gvl_free releases the GVL only for a VM with no bridges
|
|
959
|
+
// of any kind, and alive_objects is filled by bridges alone, so there is
|
|
960
|
+
// nothing to find on that path by construction.
|
|
961
|
+
if (data->gvl_released_js)
|
|
962
|
+
return false;
|
|
963
|
+
|
|
964
|
+
if (!JS_IsObject(j_val))
|
|
965
|
+
return false;
|
|
966
|
+
|
|
967
|
+
JSAtom id_atom = JS_NewAtom(ctx, "rb_object_id");
|
|
968
|
+
JSPropertyDescriptor desc;
|
|
969
|
+
int found = JS_GetOwnProperty(ctx, &desc, j_val, id_atom);
|
|
970
|
+
JS_FreeAtom(ctx, id_atom);
|
|
971
|
+
if (found <= 0)
|
|
972
|
+
return false;
|
|
973
|
+
|
|
974
|
+
bool bridged = false;
|
|
975
|
+
// FLOAT64 as well as INT, and int64_t rather than int32_t: handles are drawn
|
|
976
|
+
// from a range wider than a tagged int, so QuickJS carries most of them as
|
|
977
|
+
// doubles and a 32-bit read would miss every one of them. The other three
|
|
978
|
+
// readers of this table widened when the draw did.
|
|
979
|
+
int32_t tag = JS_VALUE_GET_NORM_TAG(desc.value);
|
|
980
|
+
if ((desc.flags & JS_PROP_TMASK) == JS_PROP_NORMAL && (tag == JS_TAG_INT || tag == JS_TAG_FLOAT64))
|
|
981
|
+
{
|
|
982
|
+
int64_t object_id = 0;
|
|
983
|
+
JS_ToInt64(ctx, &object_id, desc.value);
|
|
984
|
+
if (object_id > 0)
|
|
985
|
+
bridged = !NIL_P(rb_hash_aref(data->alive_objects, LL2NUM(object_id)));
|
|
986
|
+
}
|
|
987
|
+
JS_FreeValue(ctx, desc.value);
|
|
988
|
+
JS_FreeValue(ctx, desc.getter);
|
|
989
|
+
JS_FreeValue(ctx, desc.setter);
|
|
990
|
+
return bridged;
|
|
991
|
+
}
|
|
992
|
+
|
|
993
|
+
// The other half: QuickJS's own report that it ran out, taken together with a
|
|
994
|
+
// refusal that really happened. Three shapes count, and the same three are
|
|
995
|
+
// asked of a pending exception and of a rejection reason alike, because an
|
|
996
|
+
// exhaustion that rejects is the same exhaustion as one that throws.
|
|
997
|
+
//
|
|
998
|
+
// Its out-of-memory error carries "out of memory" in message, read as an own
|
|
999
|
+
// data property only. QuickJS defines it directly on the error it builds; an
|
|
1000
|
+
// accessor is not QuickJS's and is not consulted, and a Proxy is not
|
|
1001
|
+
// JS_IsError, so nothing guest-written runs on a throw that is about to be
|
|
1002
|
+
// discarded. The sentence alone is forgeable and always was, which is why it
|
|
1003
|
+
// is only ever asked alongside the count, and why a bridged host error is
|
|
1004
|
+
// turned away inside that branch rather than believed.
|
|
1005
|
+
//
|
|
1006
|
+
// The exhaustion that built its Error and got no further leaves the
|
|
1007
|
+
// JS_EXCEPTION sentinel as that message: an Error whose message is not a
|
|
1008
|
+
// string, which only QuickJS can make.
|
|
1009
|
+
//
|
|
1010
|
+
// And the end with no error at all, which is two values rather than one.
|
|
1011
|
+
// JS_ThrowError2 gives up with JS_NULL when it cannot allocate the object it
|
|
1012
|
+
// was going to report with, and __JS_NewAtom growing rt->atom_array has no
|
|
1013
|
+
// context to throw from, so the interpreter throws with the exception still
|
|
1014
|
+
// uninitialised. #116 is the first of those and a twelve-thousand-key object
|
|
1015
|
+
// is the second.
|
|
1016
|
+
//
|
|
1017
|
+
// A guest can write `throw null`, and `Promise.reject(null)`, and nothing in
|
|
1018
|
+
// the value tells either apart from JS_ThrowError2 giving up. So that last
|
|
1019
|
+
// shape is the one a guest can enter: after any refusal in the call, tolerated
|
|
1020
|
+
// ones included, both read as the heap having run out. Measured against
|
|
1021
|
+
// dropping it, which costs #116 its diagnosis and takes the rejection end with
|
|
1022
|
+
// it, so it stays and the exposure is written down instead. An uninitialised
|
|
1023
|
+
// value has no such twin, since the guest cannot produce one.
|
|
1024
|
+
static bool js_reports_out_of_memory(JSContext *ctx, JSValueConst j_val, bool refused)
|
|
1025
|
+
{
|
|
1026
|
+
if (!refused)
|
|
1027
|
+
return false;
|
|
1028
|
+
|
|
1029
|
+
// Two values QuickJS leaves where an error should be, and a guest can write
|
|
1030
|
+
// neither as a throw: JS_ThrowError2 gives up with JS_NULL when it cannot
|
|
1031
|
+
// build the object, and __JS_NewAtom growing rt->atom_array has no context to
|
|
1032
|
+
// throw from at all, so the interpreter throws with the exception still
|
|
1033
|
+
// uninitialised. The second reads as "[unsupported type]" without this.
|
|
1034
|
+
if (JS_IsNull(j_val) || JS_IsUninitialized(j_val))
|
|
1035
|
+
return true;
|
|
1036
|
+
|
|
1037
|
+
if (!JS_IsError(ctx, j_val))
|
|
1038
|
+
return false;
|
|
1039
|
+
|
|
1040
|
+
JSAtom message_atom = JS_NewAtom(ctx, "message");
|
|
1041
|
+
JSPropertyDescriptor desc;
|
|
1042
|
+
int found = JS_GetOwnProperty(ctx, &desc, j_val, message_atom);
|
|
1043
|
+
JS_FreeAtom(ctx, message_atom);
|
|
1044
|
+
if (found <= 0)
|
|
1045
|
+
return false;
|
|
1046
|
+
|
|
1047
|
+
bool reported = false;
|
|
1048
|
+
// The exhaustion that got as far as an Error and no further. JS_ThrowError2
|
|
1049
|
+
// allocates the object, and JS_NewString("out of memory") is then refused
|
|
1050
|
+
// inside in_out_of_memory, so JS_DefinePropertyValue stores the JS_EXCEPTION
|
|
1051
|
+
// sentinel as the own message: an Error whose message is not a string, which
|
|
1052
|
+
// is a thing only QuickJS can make. Nothing a guest writes can put that
|
|
1053
|
+
// value in a property, so it needs no sentence to back it up.
|
|
1054
|
+
if ((desc.flags & JS_PROP_TMASK) == JS_PROP_NORMAL && JS_IsException(desc.value))
|
|
1055
|
+
{
|
|
1056
|
+
reported = true;
|
|
1057
|
+
}
|
|
1058
|
+
else if ((desc.flags & JS_PROP_TMASK) == JS_PROP_NORMAL && JS_IsString(desc.value))
|
|
1059
|
+
{
|
|
1060
|
+
const char *message = JS_ToCString(ctx, desc.value);
|
|
1061
|
+
if (message != NULL)
|
|
1062
|
+
{
|
|
1063
|
+
// Asked here rather than of the value on the way in: JS_IsError above has
|
|
1064
|
+
// already turned away a Proxy, so reading an own property runs no trap,
|
|
1065
|
+
// and this is the only shape a bridged host error can satisfy. A bridge
|
|
1066
|
+
// reporting a failure of its own is the host's news whatever its message
|
|
1067
|
+
// happens to say.
|
|
1068
|
+
reported = strstr(message, "out of memory") != NULL &&
|
|
1069
|
+
!js_carries_bridged_error(ctx, j_val, JS_GetContextOpaque(ctx));
|
|
1070
|
+
JS_FreeCString(ctx, message);
|
|
1071
|
+
}
|
|
1072
|
+
else
|
|
1073
|
+
{
|
|
1074
|
+
// The value is already a string, so the only way this conversion fails
|
|
1075
|
+
// is allocation, which on a heap that just refused is the same news one
|
|
1076
|
+
// level deeper. QuickJS's own message is flat ASCII and converts without
|
|
1077
|
+
// allocating, so this branch is only ever the inspection running out.
|
|
1078
|
+
JS_FreeValue(ctx, JS_GetException(ctx));
|
|
1079
|
+
reported = true;
|
|
1080
|
+
}
|
|
1081
|
+
}
|
|
1082
|
+
JS_FreeValue(ctx, desc.value);
|
|
1083
|
+
JS_FreeValue(ctx, desc.getter);
|
|
1084
|
+
JS_FreeValue(ctx, desc.setter);
|
|
1085
|
+
return reported;
|
|
1086
|
+
}
|
|
1087
|
+
|
|
908
1088
|
// JS_ToCString converts through the value's own toString, so it answers NULL
|
|
909
1089
|
// whenever that throws — a getter that raises, a Symbol, a Proxy that refuses —
|
|
910
1090
|
// and not only when it runs out of memory. This keeps the NULL, for the two
|
|
@@ -912,6 +1092,12 @@ bool eval_budget_lapsed_now(JSContext *ctx)
|
|
|
912
1092
|
// stand in for it.
|
|
913
1093
|
static const char *js_hold_cstring_or_null(JsHold *hold, JSValue j_val)
|
|
914
1094
|
{
|
|
1095
|
+
VMData *data = JS_GetContextOpaque(hold->ctx);
|
|
1096
|
+
// The window is this read, not the call it sits in. Asked of the whole
|
|
1097
|
+
// scope, a read that answers NULL for its own reasons — a Symbol, a toString
|
|
1098
|
+
// written to throw — would report a refusal the guest had already caught and
|
|
1099
|
+
// recovered from, and condemn a VM whose call went on to return normally.
|
|
1100
|
+
uint64_t refusals_before = data->alloc_refusals;
|
|
915
1101
|
const char *str = JS_ToCString(hold->ctx, j_val);
|
|
916
1102
|
if (str != NULL)
|
|
917
1103
|
return js_hold_own_cstring(hold, str);
|
|
@@ -928,11 +1114,27 @@ static const char *js_hold_cstring_or_null(JsHold *hold, JSValue j_val)
|
|
|
928
1114
|
// remembering is not the throw but the clock: a read the budget outlived says
|
|
929
1115
|
// nothing about the value and everything about the evaluation.
|
|
930
1116
|
JSValue j_pending = js_hold_value(hold, JS_GetException(hold->ctx));
|
|
1117
|
+
|
|
1118
|
+
// Before the bridge exit below, which raises: a read that ran out and then
|
|
1119
|
+
// found a host error waiting would unwind past the latch and leave the VM
|
|
1120
|
+
// running on the heap that refused. What is pending is the throw that ended
|
|
1121
|
+
// the read, so it is the thing to ask whether QuickJS is reporting a heap
|
|
1122
|
+
// that ran out or the value simply having no string form.
|
|
1123
|
+
// The window covers this read, and the fetch above it when that is where
|
|
1124
|
+
// the failure happened: a caller handing in JS_EXCEPTION has already had its
|
|
1125
|
+
// JS_GetPropertyStr run a getter and come back empty, and a getter that ran
|
|
1126
|
+
// out did so before this window opened. Reading the call scope for that case
|
|
1127
|
+
// is safe because the pending value still has to be QuickJS reporting.
|
|
1128
|
+
bool refused = data->alloc_refusals > refusals_before ||
|
|
1129
|
+
(JS_IsException(j_val) && allocator_refused(data));
|
|
1130
|
+
if (js_reports_out_of_memory(hold->ctx, j_pending, refused))
|
|
1131
|
+
data->oom_poisoned = true;
|
|
1132
|
+
|
|
931
1133
|
VALUE r_ruby_error = find_ruby_error(hold->ctx, j_pending);
|
|
932
1134
|
if (!NIL_P(r_ruby_error))
|
|
933
1135
|
rb_exc_raise(r_ruby_error);
|
|
934
1136
|
|
|
935
|
-
if (eval_budget_lapsed(
|
|
1137
|
+
if (eval_budget_lapsed(data))
|
|
936
1138
|
hold->interrupted = true;
|
|
937
1139
|
|
|
938
1140
|
return NULL;
|
|
@@ -1025,16 +1227,72 @@ static void raise_if_interrupted(JsHold *hold)
|
|
|
1025
1227
|
rb_exc_raise(r_interrupted_error());
|
|
1026
1228
|
}
|
|
1027
1229
|
|
|
1230
|
+
// The heap running out inside a read outranks both the read and the budget:
|
|
1231
|
+
// the VM is already condemned, and the caller is told what a top-level
|
|
1232
|
+
// out-of-memory tells it, so the advice to recreate the VM reads the same.
|
|
1233
|
+
static VALUE r_out_of_memory_error(void)
|
|
1234
|
+
{
|
|
1235
|
+
VALUE r_message = rb_str_new2("out of memory");
|
|
1236
|
+
return rb_funcall(QUICKJSRB_ERROR_FOR(QUICKJSRB_ROOT_RUNTIME_ERROR), rb_intern("new"), 2, r_message, Qnil);
|
|
1237
|
+
}
|
|
1238
|
+
|
|
1239
|
+
// Asked of the scope rather than of one hold. The flag a hold used to carry
|
|
1240
|
+
// was set only where allocator_refused was already true and read where it
|
|
1241
|
+
// still is, so it said nothing the counter does not, and a flag copied along
|
|
1242
|
+
// a path that can raise is a flag that can be lost. The scope also covers the
|
|
1243
|
+
// throw being rendered, including the end where QuickJS could not allocate
|
|
1244
|
+
// the error object either and what is pending is not an Error at all.
|
|
1245
|
+
//
|
|
1246
|
+
// The refusal alone decides nothing, and neither does the class the throw
|
|
1247
|
+
// carries: what is pending has to be QuickJS reporting. A guest that catches
|
|
1248
|
+
// an out-of-memory and then throws a TypeError is heard as that TypeError and
|
|
1249
|
+
// keeps its VM.
|
|
1250
|
+
//
|
|
1251
|
+
// Condemns the heap, and answers whether it did. Split from the raise below
|
|
1252
|
+
// because the latch has to be set before dispatch_log runs: an on_log listener
|
|
1253
|
+
// can re-enter the VM, and a nested entry consults oom_poisoned to refuse.
|
|
1254
|
+
// Ordered before it the listener is turned away; after it, it ran JS on the
|
|
1255
|
+
// heap that had just refused.
|
|
1256
|
+
static bool condemn_if_out_of_memory(VMData *data, JSContext *ctx, JSValueConst j_val)
|
|
1257
|
+
{
|
|
1258
|
+
if (!js_reports_out_of_memory(ctx, j_val, allocator_refused(data)))
|
|
1259
|
+
return false;
|
|
1260
|
+
|
|
1261
|
+
data->oom_poisoned = true;
|
|
1262
|
+
return true;
|
|
1263
|
+
}
|
|
1264
|
+
|
|
1265
|
+
// Raised once the heap is condemned, in place of whatever the throw would have
|
|
1266
|
+
// been rendered as: the message is fixed, so the advice to recreate the VM
|
|
1267
|
+
// reads the same wherever the exhaustion was met. The name and the stack are
|
|
1268
|
+
// kept when there is an Error to take them from. QuickJS's own out-of-memory
|
|
1269
|
+
// is an InternalError thrown from a JS frame, and where the guest was is the
|
|
1270
|
+
// first thing a caller sizing memory_limit wants to know.
|
|
1271
|
+
static void raise_if_out_of_memory(bool out_of_memory, VALUE r_js_name, const char *js_stack)
|
|
1272
|
+
{
|
|
1273
|
+
if (!out_of_memory)
|
|
1274
|
+
return;
|
|
1275
|
+
|
|
1276
|
+
VALUE r_exc = rb_funcall(QUICKJSRB_ERROR_FOR(QUICKJSRB_ROOT_RUNTIME_ERROR), rb_intern("new"), 2, rb_str_new2("out of memory"), r_js_name);
|
|
1277
|
+
VALUE r_backtrace = r_backtrace_from_js_stack(js_stack);
|
|
1278
|
+
if (!NIL_P(r_backtrace))
|
|
1279
|
+
rb_funcall(r_exc, rb_intern("set_backtrace"), 1, r_backtrace);
|
|
1280
|
+
rb_exc_raise(r_exc);
|
|
1281
|
+
}
|
|
1282
|
+
|
|
1028
1283
|
struct js_exception_render
|
|
1029
1284
|
{
|
|
1030
1285
|
JSContext *ctx;
|
|
1031
1286
|
// Whether the exception is the evaluation's own result. Only then is it news:
|
|
1032
|
-
// it gets the "Uncaught" row the console would have printed
|
|
1033
|
-
//
|
|
1034
|
-
//
|
|
1035
|
-
//
|
|
1036
|
-
//
|
|
1037
|
-
//
|
|
1287
|
+
// it gets the "Uncaught" row the console would have printed. An exception
|
|
1288
|
+
// raised part-way through converting a value that did return is on its way to
|
|
1289
|
+
// the caller as that call's error, and telling the log listener it went
|
|
1290
|
+
// uncaught would be the opposite of what happened.
|
|
1291
|
+
//
|
|
1292
|
+
// The out-of-memory latch is not keyed on this and reads the same either way.
|
|
1293
|
+
// It was, while the latch lived in the class cascade below and only the
|
|
1294
|
+
// uncaught path reached it; the heap is a fact about the call rather than
|
|
1295
|
+
// about which of its exceptions is being rendered.
|
|
1038
1296
|
bool uncaught;
|
|
1039
1297
|
JsHold hold;
|
|
1040
1298
|
};
|
|
@@ -1046,18 +1304,27 @@ static VALUE js_exception_render_run(VALUE r_render)
|
|
|
1046
1304
|
JsHold *hold = &render->hold;
|
|
1047
1305
|
VMData *data = JS_GetContextOpaque(ctx);
|
|
1048
1306
|
|
|
1307
|
+
// What this render condemns the VM for, told apart from what it inherited.
|
|
1308
|
+
bool was_condemned = data->oom_poisoned;
|
|
1309
|
+
|
|
1049
1310
|
JSValue j_exceptionVal = js_hold_value(hold, JS_GetException(ctx));
|
|
1050
1311
|
|
|
1051
1312
|
if (!JS_IsError(ctx, j_exceptionVal))
|
|
1052
1313
|
{
|
|
1053
1314
|
// A thrown string, number or bare object: there is no name or stack to ask
|
|
1054
1315
|
// for, only what the value itself says.
|
|
1316
|
+
bool out_of_memory = condemn_if_out_of_memory(data, ctx, j_exceptionVal);
|
|
1055
1317
|
const char *errorMessage = js_hold_cstring(hold, j_exceptionVal, QUICKJSRB_UNRENDERABLE);
|
|
1318
|
+
// The read itself can be the thing that runs out, and the hold condemns
|
|
1319
|
+
// the VM when it does. Asked after the read for that reason, and before
|
|
1320
|
+
// the row is dispatched, so a listener that re-enters is turned away.
|
|
1321
|
+
out_of_memory = out_of_memory || (!was_condemned && data->oom_poisoned);
|
|
1056
1322
|
if (render->uncaught)
|
|
1057
1323
|
{
|
|
1058
1324
|
VALUE r_headline = rb_str_new2(js_hold_format(hold, "Uncaught '%s'", errorMessage));
|
|
1059
1325
|
dispatch_log(data, "error", rb_ary_new3(1, r_log_body_new(r_headline, r_headline)));
|
|
1060
1326
|
}
|
|
1327
|
+
raise_if_out_of_memory(out_of_memory, Qnil, NULL);
|
|
1061
1328
|
raise_if_interrupted(hold);
|
|
1062
1329
|
|
|
1063
1330
|
rb_exc_raise(rb_funcall(QUICKJSRB_ERROR_FOR(QUICKJSRB_ROOT_RUNTIME_ERROR), rb_intern("new"), 2, rb_str_new2(errorMessage), Qnil));
|
|
@@ -1065,9 +1332,21 @@ static VALUE js_exception_render_run(VALUE r_render)
|
|
|
1065
1332
|
|
|
1066
1333
|
VALUE r_maybe_ruby_error = find_ruby_error(ctx, j_exceptionVal);
|
|
1067
1334
|
if (!NIL_P(r_maybe_ruby_error))
|
|
1335
|
+
{
|
|
1336
|
+
// The host error is what comes back out: it is the bridge reporting a
|
|
1337
|
+
// failure of its own, and find_ruby_error is the only thing that takes it
|
|
1338
|
+
// out of alive_objects.
|
|
1339
|
+
// No reader here. What is pending is the bridged error itself, carrying
|
|
1340
|
+
// the Ruby exception message rather than anything QuickJS wrote, so there
|
|
1341
|
+
// is nothing on this exit that could be the heap reporting: a refusal the
|
|
1342
|
+
// guest met and recovered from before calling the bridge is the guest's
|
|
1343
|
+
// business, and one QuickJS reported would not be wearing an rb_object_id.
|
|
1068
1344
|
rb_exc_raise(r_maybe_ruby_error);
|
|
1345
|
+
}
|
|
1069
1346
|
// will support other errors like just returning an instance of Error
|
|
1070
1347
|
|
|
1348
|
+
bool out_of_memory = condemn_if_out_of_memory(data, ctx, j_exceptionVal);
|
|
1349
|
+
|
|
1071
1350
|
JSValue j_errorClassName = js_hold_value(hold, JS_GetPropertyStr(ctx, j_exceptionVal, "name"));
|
|
1072
1351
|
const char *readClassName = js_hold_cstring_or_null(hold, j_errorClassName);
|
|
1073
1352
|
// "Error" is a workable default for picking a Ruby class, which has to resolve
|
|
@@ -1084,11 +1363,16 @@ static VALUE js_exception_render_run(VALUE r_render)
|
|
|
1084
1363
|
// apology reads as a frame there.
|
|
1085
1364
|
const char *stackTrace = js_hold_cstring(hold, j_stackTrace, "");
|
|
1086
1365
|
|
|
1366
|
+
// As above: any of the three reads can run out, and the hold condemns when
|
|
1367
|
+
// one does.
|
|
1368
|
+
out_of_memory = out_of_memory || (!was_condemned && data->oom_poisoned);
|
|
1369
|
+
|
|
1087
1370
|
if (render->uncaught)
|
|
1088
1371
|
{
|
|
1089
1372
|
VALUE r_headline = rb_str_new2(js_hold_format(hold, "Uncaught %s: %s\n%s", errorClassName, errorClassMessage, stackTrace));
|
|
1090
1373
|
dispatch_log(data, "error", rb_ary_new3(1, r_log_body_new(r_headline, r_headline)));
|
|
1091
1374
|
}
|
|
1375
|
+
raise_if_out_of_memory(out_of_memory, r_error_name, stackTrace);
|
|
1092
1376
|
raise_if_interrupted(hold);
|
|
1093
1377
|
|
|
1094
1378
|
VALUE r_error_class, r_error_message = rb_str_new2(errorClassMessage);
|
|
@@ -1106,23 +1390,6 @@ static VALUE js_exception_render_run(VALUE r_render)
|
|
|
1106
1390
|
{
|
|
1107
1391
|
r_error_class = QUICKJSRB_ERROR_FOR(QUICKJSRB_INTERRUPTED_ERROR);
|
|
1108
1392
|
}
|
|
1109
|
-
else if (strcmp(errorClassName, "InternalError") == 0 && strstr(errorClassMessage, "out of memory") != NULL)
|
|
1110
|
-
{
|
|
1111
|
-
// Once OOM has fired, the QuickJS heap is in a state where another
|
|
1112
|
-
// throw inside the parser-error path can corrupt the shape table and
|
|
1113
|
-
// segfault. Mark the VM so further eval/call calls refuse cleanly.
|
|
1114
|
-
//
|
|
1115
|
-
// This is the one thing the uncaught path does not keep to itself. Running
|
|
1116
|
-
// out of memory is a fact about the heap, not about who was asking: a
|
|
1117
|
-
// getter on an object the evaluation successfully returned allocates on the
|
|
1118
|
-
// same heap as the evaluation did, and `({get x() { return new
|
|
1119
|
-
// Array(2_000_000).fill(0) }})` reaches OOM here with the result already in
|
|
1120
|
-
// hand. Both strings are guest-writable, so a forged InternalError condemns
|
|
1121
|
-
// the VM too — but that is reachable from any getter and always has been,
|
|
1122
|
-
// and refusing to latch here would trade a real guard for no ground.
|
|
1123
|
-
data->oom_poisoned = true;
|
|
1124
|
-
r_error_class = QUICKJSRB_ERROR_FOR(QUICKJSRB_ROOT_RUNTIME_ERROR);
|
|
1125
|
-
}
|
|
1126
1393
|
else
|
|
1127
1394
|
{
|
|
1128
1395
|
r_error_class = QUICKJSRB_ERROR_FOR(QUICKJSRB_ROOT_RUNTIME_ERROR);
|
|
@@ -1257,8 +1524,12 @@ static VALUE to_rb_value_inner(JSContext *ctx, JSValue j_val, ConvState *conv)
|
|
|
1257
1524
|
// transparently, so both tags share the same conversion path.
|
|
1258
1525
|
size_t len;
|
|
1259
1526
|
const char *str = JS_ToCStringLen(ctx, &len, j_val);
|
|
1527
|
+
// The value is already a string, so nothing guest-written runs here and
|
|
1528
|
+
// only a failed allocation answers NULL, as for the atom above. Returning
|
|
1529
|
+
// nil handed the caller an empty result for a string it did have and left
|
|
1530
|
+
// the refusal unreported, so the next call ran on the heap that refused.
|
|
1260
1531
|
if (str == NULL)
|
|
1261
|
-
return
|
|
1532
|
+
return raise_js_exception(ctx);
|
|
1262
1533
|
VALUE r_str = rb_utf8_str_new(str, (long)len);
|
|
1263
1534
|
JS_FreeCString(ctx, str);
|
|
1264
1535
|
return r_str;
|
|
@@ -1704,10 +1975,21 @@ static VALUE r_exception_from_js_reason(JSContext *ctx, JSValueConst j_reason, i
|
|
|
1704
1975
|
conversion.j_reason = j_reason;
|
|
1705
1976
|
conversion.peek_handle = peek_handle;
|
|
1706
1977
|
js_hold_init(&conversion.hold, ctx);
|
|
1978
|
+
VMData *data = JS_GetContextOpaque(ctx);
|
|
1979
|
+
// The window is the conversion, for the reason the hold's own read narrows
|
|
1980
|
+
// to itself: a refusal the guest met and caught earlier in the call says
|
|
1981
|
+
// nothing about this reason, and reporting it here would hand the listener
|
|
1982
|
+
// "out of memory" in place of the rejection it is waiting to hear about.
|
|
1983
|
+
bool was_condemned = data->oom_poisoned;
|
|
1707
1984
|
VALUE r_exc = rb_ensure(js_reason_conversion_run, (VALUE)&conversion, js_hold_release, (VALUE)&conversion.hold);
|
|
1708
|
-
//
|
|
1709
|
-
//
|
|
1710
|
-
//
|
|
1985
|
+
// Read after the release: a heap that ran out or a budget that lapsed while
|
|
1986
|
+
// the reason was read is what the listener hears about, since the tracker
|
|
1987
|
+
// cannot raise out and there may be no next interrupt check. The lapse is a
|
|
1988
|
+
// flag the hold carries, because a re-armed clock forgets; the refusal is a
|
|
1989
|
+
// count on the VM, which forgets nothing. The same precedence as the
|
|
1990
|
+
// renderer.
|
|
1991
|
+
if (!was_condemned && data->oom_poisoned)
|
|
1992
|
+
return r_out_of_memory_error();
|
|
1711
1993
|
return conversion.hold.interrupted ? r_interrupted_error() : r_exc;
|
|
1712
1994
|
}
|
|
1713
1995
|
|
|
@@ -1861,6 +2143,36 @@ static void quickjsrb_promise_rejection_tracker(
|
|
|
1861
2143
|
JS_BOOL is_handled, void *opaque)
|
|
1862
2144
|
{
|
|
1863
2145
|
VMData *data = JS_GetContextOpaque(ctx);
|
|
2146
|
+
|
|
2147
|
+
// A rejection nobody handled is not a recovery, so the heap is condemned
|
|
2148
|
+
// here when QuickJS is the one reporting the reason, and before the listener
|
|
2149
|
+
// check: with no listener registered there is no reader below at all, and a
|
|
2150
|
+
// job that ran out inside drain_jobs! left the VM running on the heap that
|
|
2151
|
+
// refused.
|
|
2152
|
+
//
|
|
2153
|
+
// The reason is asked the same questions a pending exception is, absent
|
|
2154
|
+
// object included, because an exhaustion that arrives as a rejection is the
|
|
2155
|
+
// same exhaustion: `async function f() { let n = null; for (;;) n = {n} }`
|
|
2156
|
+
// rejects with the JS_NULL that JS_ThrowError2 could not replace with an
|
|
2157
|
+
// Error, and asking a narrower question here meant the diagnosis depended on
|
|
2158
|
+
// whether the chain was thrown or awaited. It costs the symmetric exposure:
|
|
2159
|
+
// Promise.reject(null) after any refusal reads as the heap, as throw null
|
|
2160
|
+
// does. Promise.reject() does not, since undefined is nothing QuickJS leaves.
|
|
2161
|
+
//
|
|
2162
|
+
// This fires at rejection time, and QuickJS calls the tracker again with
|
|
2163
|
+
// is_handled once a handler attaches, which nothing here undoes. So a
|
|
2164
|
+
// rejection carrying a real out-of-memory condemns even when the guest goes
|
|
2165
|
+
// on to handle it, where the same exhaustion caught synchronously does not.
|
|
2166
|
+
// Conservative and deliberate, but not symmetric with try/catch; deferring
|
|
2167
|
+
// the latch to the end of the call is what would make them agree.
|
|
2168
|
+
// Every uncaught throw reaches this tracker before the renderer does, since
|
|
2169
|
+
// eval is async-wrapped, so the bridged errors pass through here too. The
|
|
2170
|
+
// renderer answers them at its find_ruby_error exit, above every reader;
|
|
2171
|
+
// here that has to be said out loud, or a host exception whose own message
|
|
2172
|
+
// says the words is read as the heap.
|
|
2173
|
+
if (js_reports_out_of_memory(ctx, reason, allocator_refused(data)))
|
|
2174
|
+
data->oom_poisoned = true;
|
|
2175
|
+
|
|
1864
2176
|
if (NIL_P(data->on_unhandled_rejection))
|
|
1865
2177
|
return;
|
|
1866
2178
|
|
|
@@ -1900,11 +2212,76 @@ static void quickjsrb_promise_rejection_tracker(
|
|
|
1900
2212
|
entry->error_handle = error_handle;
|
|
1901
2213
|
}
|
|
1902
2214
|
|
|
2215
|
+
static JSValue throw_unsettleable_await(JSContext *ctx)
|
|
2216
|
+
{
|
|
2217
|
+
JSValue j_error = JS_NewError(ctx);
|
|
2218
|
+
if (JS_IsException(j_error))
|
|
2219
|
+
return j_error;
|
|
2220
|
+
int flags = JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE;
|
|
2221
|
+
JS_DefinePropertyValueStr(ctx, j_error, "name", JS_NewString(ctx, "Quickjs::InterruptedError"), flags);
|
|
2222
|
+
JS_DefinePropertyValueStr(ctx, j_error, "message",
|
|
2223
|
+
JS_NewString(ctx, "Awaited Promise can never settle: no job, timer or I/O is left to resolve it"), flags);
|
|
2224
|
+
// Raised with no JS on the stack; left unset, the log row would read "undefined".
|
|
2225
|
+
JS_DefinePropertyValueStr(ctx, j_error, "stack", JS_NewString(ctx, ""), flags);
|
|
2226
|
+
return JS_Throw(ctx, j_error);
|
|
2227
|
+
}
|
|
2228
|
+
|
|
2229
|
+
// js_std_await, with the idle branch in our hands: there it retries forever,
|
|
2230
|
+
// out of reach of timeout_msec because no JS runs to poll the interrupt handler.
|
|
2231
|
+
// A wait on a live timer may still outlast the budget, as it always has. Frees
|
|
2232
|
+
// promise; a non-promise, including JS_EXCEPTION, passes through.
|
|
2233
|
+
static JSValue await_promise(JSContext *ctx, JSValue promise)
|
|
2234
|
+
{
|
|
2235
|
+
JSRuntime *runtime = JS_GetRuntime(ctx);
|
|
2236
|
+
for (;;)
|
|
2237
|
+
{
|
|
2238
|
+
JSValue ret;
|
|
2239
|
+
switch (JS_PromiseState(ctx, promise))
|
|
2240
|
+
{
|
|
2241
|
+
case JS_PROMISE_FULFILLED:
|
|
2242
|
+
ret = JS_PromiseResult(ctx, promise);
|
|
2243
|
+
JS_FreeValue(ctx, promise);
|
|
2244
|
+
return ret;
|
|
2245
|
+
case JS_PROMISE_REJECTED:
|
|
2246
|
+
ret = JS_Throw(ctx, JS_PromiseResult(ctx, promise));
|
|
2247
|
+
JS_FreeValue(ctx, promise);
|
|
2248
|
+
return ret;
|
|
2249
|
+
case JS_PROMISE_PENDING:
|
|
2250
|
+
break;
|
|
2251
|
+
default:
|
|
2252
|
+
return promise;
|
|
2253
|
+
}
|
|
2254
|
+
|
|
2255
|
+
int err = JS_ExecutePendingJob(runtime, NULL);
|
|
2256
|
+
if (err < 0)
|
|
2257
|
+
js_std_dump_error(ctx);
|
|
2258
|
+
if (err == 0)
|
|
2259
|
+
{
|
|
2260
|
+
if (!quickjsrb_os_has_event_source(ctx))
|
|
2261
|
+
{
|
|
2262
|
+
JS_FreeValue(ctx, promise);
|
|
2263
|
+
return throw_unsettleable_await(ctx);
|
|
2264
|
+
}
|
|
2265
|
+
if (quickjsrb_os_poll(ctx) != 0)
|
|
2266
|
+
{
|
|
2267
|
+
// Only a signal handler is left, and poll returns at once for those.
|
|
2268
|
+
if (eval_budget_lapsed(JS_GetContextOpaque(ctx)))
|
|
2269
|
+
{
|
|
2270
|
+
JS_FreeValue(ctx, promise);
|
|
2271
|
+
return JS_ThrowInternalError(ctx, "interrupted");
|
|
2272
|
+
}
|
|
2273
|
+
struct timespec nap = {0, 1000000};
|
|
2274
|
+
nanosleep(&nap, NULL);
|
|
2275
|
+
}
|
|
2276
|
+
}
|
|
2277
|
+
}
|
|
2278
|
+
}
|
|
2279
|
+
|
|
1903
2280
|
// A promise the host awaits is handled by the host: its rejection is raised.
|
|
1904
2281
|
static JSValue quickjsrb_host_await(JSContext *ctx, JSValue promise)
|
|
1905
2282
|
{
|
|
1906
2283
|
JSValue held = JS_DupValue(ctx, promise);
|
|
1907
|
-
JSValue ret =
|
|
2284
|
+
JSValue ret = await_promise(ctx, promise); // frees promise
|
|
1908
2285
|
pending_rejections_remove(JS_GetContextOpaque(ctx), held);
|
|
1909
2286
|
JS_FreeValue(ctx, held);
|
|
1910
2287
|
return ret;
|
|
@@ -1963,8 +2340,17 @@ static void quickjsrb_notify_all_rejections(VMData *data, bool jobs_may_run)
|
|
|
1963
2340
|
// at most max_pending_rejections are carried over, the oldest reported first.
|
|
1964
2341
|
static void quickjsrb_end_microtask_checkpoint(VMData *data)
|
|
1965
2342
|
{
|
|
2343
|
+
// A condemned VM has had its last checkpoint: the jobs still queued will
|
|
2344
|
+
// never run, so nothing pending can be handled later, as at dispose!. The
|
|
2345
|
+
// reasons were read when they rejected, so reporting them runs no JS, and
|
|
2346
|
+
// one of them is how the caller of a call that returned normally hears the
|
|
2347
|
+
// heap ran out.
|
|
1966
2348
|
if (data->oom_poisoned)
|
|
2349
|
+
{
|
|
2350
|
+
quickjsrb_notify_all_rejections(data, false);
|
|
2351
|
+
rejection_list_free(data->context, &data->pending_rejections);
|
|
1967
2352
|
return;
|
|
2353
|
+
}
|
|
1968
2354
|
uint32_t live = data->pending_rejections.live;
|
|
1969
2355
|
if (!JS_IsJobPending(JS_GetRuntime(data->context)))
|
|
1970
2356
|
quickjsrb_notify_all_rejections(data, true);
|
|
@@ -2286,8 +2672,24 @@ struct log_row_build
|
|
|
2286
2672
|
{
|
|
2287
2673
|
struct quickjsrb_log_call *call;
|
|
2288
2674
|
JsHold hold;
|
|
2675
|
+
// The build is its own scope. Every reader reached from inside it — the row's
|
|
2676
|
+
// own reads, and the renderers those reach through to_rb_value — then measures
|
|
2677
|
+
// the build rather than the call, so a refusal the guest already caught and
|
|
2678
|
+
// recovered from earlier in the call cannot be reported as this row's.
|
|
2679
|
+
// Restored by the ensure, on the raising path too.
|
|
2680
|
+
uint64_t scope_before;
|
|
2289
2681
|
};
|
|
2290
2682
|
|
|
2683
|
+
// Owns the build's exit, raising or not: releases what the row held and
|
|
2684
|
+
// closes the scope the build opened.
|
|
2685
|
+
static VALUE r_log_row_ensure(VALUE r_build)
|
|
2686
|
+
{
|
|
2687
|
+
struct log_row_build *build = (struct log_row_build *)r_build;
|
|
2688
|
+
VMData *data = JS_GetContextOpaque(build->hold.ctx);
|
|
2689
|
+
data->alloc_refusals_at_scope = build->scope_before;
|
|
2690
|
+
return js_hold_release((VALUE)&build->hold);
|
|
2691
|
+
}
|
|
2692
|
+
|
|
2291
2693
|
static VALUE r_build_log_row(VALUE r_build)
|
|
2292
2694
|
{
|
|
2293
2695
|
struct log_row_build *build = (struct log_row_build *)r_build;
|
|
@@ -2347,7 +2749,9 @@ static VALUE r_build_and_dispatch_log(VALUE r_call)
|
|
|
2347
2749
|
struct log_row_build build;
|
|
2348
2750
|
build.call = call;
|
|
2349
2751
|
js_hold_init(&build.hold, call->ctx);
|
|
2350
|
-
|
|
2752
|
+
build.scope_before = data->alloc_refusals_at_scope;
|
|
2753
|
+
data->alloc_refusals_at_scope = data->alloc_refusals;
|
|
2754
|
+
VALUE r_row = rb_ensure(r_build_log_row, (VALUE)&build, r_log_row_ensure, (VALUE)&build);
|
|
2351
2755
|
|
|
2352
2756
|
// Carried out rather than raised here: a Ruby exception would be bridged
|
|
2353
2757
|
// into a catchable JS Error, and a guest wrapping console.log in try/catch
|
|
@@ -2365,9 +2769,37 @@ static VALUE r_build_and_dispatch_log(VALUE r_call)
|
|
|
2365
2769
|
// JS exception instead of a cross-boundary longjmp.
|
|
2366
2770
|
static JSValue js_quickjsrb_log_inner(JSContext *ctx, int argc, JSValueConst *argv, const char *severity)
|
|
2367
2771
|
{
|
|
2772
|
+
VMData *data = JS_GetContextOpaque(ctx);
|
|
2773
|
+
// Asked of the VM either side of the protect rather than carried out on the
|
|
2774
|
+
// call. It covers the whole of it, the listener included: a listener that
|
|
2775
|
+
// re-enters the VM and exhausts the heap in its own call condemns this one
|
|
2776
|
+
// too, and the guest went on running here with nothing said. A nested call
|
|
2777
|
+
// that merely meets a refusal and recovers is still its own business, since
|
|
2778
|
+
// what is read is the verdict rather than the count.
|
|
2368
2779
|
struct quickjsrb_log_call call = {ctx, argc, argv, severity, JS_UNDEFINED, false};
|
|
2369
2780
|
int error;
|
|
2370
2781
|
rb_protect(r_build_and_dispatch_log, (VALUE)&call, &error);
|
|
2782
|
+
// The flag rather than a transition in it: inside a call it can only have
|
|
2783
|
+
// been set by this call, since enter_oom_scope turns a condemned VM away at
|
|
2784
|
+
// the door, so a heap something else in this call already exhausted stops
|
|
2785
|
+
// the guest here too rather than letting it run to the end.
|
|
2786
|
+
if (data->oom_poisoned)
|
|
2787
|
+
{
|
|
2788
|
+
// Ahead of the lapse, as in the renderer: a heap that ran out is a fact
|
|
2789
|
+
// about the VM rather than about this evaluation. Without this the
|
|
2790
|
+
// evaluation ran on, because QuickJS was never told its heap had run out:
|
|
2791
|
+
// the throw that said so was substituted away for the log row, or bridged
|
|
2792
|
+
// back as an ordinary catchable Error for the guest to swallow.
|
|
2793
|
+
data->oom_poisoned = true;
|
|
2794
|
+
if (error)
|
|
2795
|
+
rb_set_errinfo(Qnil);
|
|
2796
|
+
// Uncatchable for the reason the lapse is: a catchable Error here is one
|
|
2797
|
+
// the guest can swallow, and it would pin a Ruby exception in
|
|
2798
|
+
// alive_objects per catch on a heap that has nothing left to give.
|
|
2799
|
+
JS_ThrowInternalError(ctx, "out of memory");
|
|
2800
|
+
JS_SetUncatchableException(ctx, TRUE);
|
|
2801
|
+
return JS_EXCEPTION;
|
|
2802
|
+
}
|
|
2371
2803
|
if (call.interrupted)
|
|
2372
2804
|
{
|
|
2373
2805
|
// The lapse outranks a raise from the listener, as it does in the uncaught
|
|
@@ -2420,6 +2852,19 @@ static void *quickjsrb_log_with_gvl(void *p)
|
|
|
2420
2852
|
static JSValue js_quickjsrb_log(JSContext *ctx, int argc, JSValueConst *argv, const char *severity)
|
|
2421
2853
|
{
|
|
2422
2854
|
VMData *data = JS_GetContextOpaque(ctx);
|
|
2855
|
+
// Ahead of the listener check, because stopping a guest on a heap this call
|
|
2856
|
+
// has already exhausted is not a thing to make conditional on anyone
|
|
2857
|
+
// listening. Inside a call the flag can only have been set by that call,
|
|
2858
|
+
// since enter_oom_scope turns a condemned VM away at the door. Both this and
|
|
2859
|
+
// the listener read below are plain aligned loads, safe without the GVL, and
|
|
2860
|
+
// the throw is pure QuickJS.
|
|
2861
|
+
if (data->oom_poisoned)
|
|
2862
|
+
{
|
|
2863
|
+
JS_ThrowInternalError(ctx, "out of memory");
|
|
2864
|
+
JS_SetUncatchableException(ctx, TRUE);
|
|
2865
|
+
return JS_EXCEPTION;
|
|
2866
|
+
}
|
|
2867
|
+
|
|
2423
2868
|
// With no listener registered the built row would be discarded, so skip
|
|
2424
2869
|
// the whole pipeline — most importantly the GVL re-acquire below, which
|
|
2425
2870
|
// would otherwise turn every console.log of a log-heavy pure-path script
|
|
@@ -2487,7 +2932,7 @@ static JSValue js_console_error(JSContext *ctx, JSValueConst this, int argc, JSV
|
|
|
2487
2932
|
// gvl_released_js re-acquire either way.
|
|
2488
2933
|
//
|
|
2489
2934
|
// 3. vm_m_evalBytecode — user bytecode via Runnable#run, same gate and
|
|
2490
|
-
// buffer copy as 2, but with the awaiting runner:
|
|
2935
|
+
// buffer copy as 2, but with the awaiting runner: await_promise's job
|
|
2491
2936
|
// drain is bridge-free under the gate, the same argument eval_code's
|
|
2492
2937
|
// released path relies on.
|
|
2493
2938
|
//
|
|
@@ -2530,11 +2975,11 @@ static void *bytecode_load_job_run(void *p)
|
|
|
2530
2975
|
return NULL;
|
|
2531
2976
|
}
|
|
2532
2977
|
|
|
2533
|
-
// Awaiting variant of the core, for vm_m_evalBytecode:
|
|
2978
|
+
// Awaiting variant of the core, for vm_m_evalBytecode: await_promise drains
|
|
2534
2979
|
// the job queue until the eval's promise settles. Same MUST-NOT-touch-Ruby
|
|
2535
2980
|
// constraint — on a pure VM (can_eval_gvl_free) every drained job is
|
|
2536
2981
|
// bridge-free JS, the same argument eval_code's released path relies on.
|
|
2537
|
-
//
|
|
2982
|
+
// await_promise passes a non-promise — including an exception preserved by
|
|
2538
2983
|
// the JS_ReadObject short-circuit — through untouched.
|
|
2539
2984
|
static void *bytecode_eval_await_job_run(void *p)
|
|
2540
2985
|
{
|
|
@@ -2718,7 +3163,7 @@ static VALUE vm_m_initialize(int argc, VALUE *argv, VALUE r_self)
|
|
|
2718
3163
|
{
|
|
2719
3164
|
// setTimeout itself only enqueues, but js_delay_and_eval_job (the
|
|
2720
3165
|
// enqueued callback) calls rb_funcall and rb_thread_wait_for
|
|
2721
|
-
// synchronously while
|
|
3166
|
+
// synchronously while await_promise drains the queue — so this counts
|
|
2722
3167
|
// as a Ruby bridge and eval must keep the GVL.
|
|
2723
3168
|
JS_SetPropertyStr(
|
|
2724
3169
|
data->context, j_global, "setTimeout",
|
|
@@ -2783,6 +3228,19 @@ static VALUE vm_m_initialize(int argc, VALUE *argv, VALUE r_self)
|
|
|
2783
3228
|
JS_SetPropertyStr(data->context, j_global, "console", j_console);
|
|
2784
3229
|
JS_FreeValue(data->context, j_global);
|
|
2785
3230
|
|
|
3231
|
+
// Construction opens no scope, since there is no earlier call to be told
|
|
3232
|
+
// apart from: every refusal counted so far happened building this VM. Some
|
|
3233
|
+
// of them raise on their way out, but the feature module loads free their
|
|
3234
|
+
// results unchecked, so a std or os module that ran out is discarded here
|
|
3235
|
+
// and the VM is handed back on a heap that has already refused. The first
|
|
3236
|
+
// call would find it only by refusing too, and the scope it opens masks
|
|
3237
|
+
// the count that would have said so.
|
|
3238
|
+
if (data->alloc_refusals > 0)
|
|
3239
|
+
{
|
|
3240
|
+
data->oom_poisoned = true;
|
|
3241
|
+
data->condemned_at_build = true;
|
|
3242
|
+
}
|
|
3243
|
+
|
|
2786
3244
|
return r_self;
|
|
2787
3245
|
}
|
|
2788
3246
|
|
|
@@ -2829,7 +3287,17 @@ static VALUE to_rb_return_value(JSContext *ctx, JSValue j_val)
|
|
|
2829
3287
|
return rb_ensure(to_rb_return_value_body, (VALUE)&owned, to_rb_return_value_release, (VALUE)&owned);
|
|
2830
3288
|
}
|
|
2831
3289
|
|
|
2832
|
-
|
|
3290
|
+
// Refuses a VM the heap has already condemned, and then opens the scope the
|
|
3291
|
+
// out-of-memory readers measure against: one public API call is one
|
|
3292
|
+
// operation, and "the allocator refused" means it refused inside this one.
|
|
3293
|
+
//
|
|
3294
|
+
// The snapshot lives here, rather than beside each caller, because every
|
|
3295
|
+
// public entry point already begins with this line — and because the scope
|
|
3296
|
+
// cannot be a JS entry. An evaluation is two counted entries, the run and
|
|
3297
|
+
// then the rendering of what it left pending, so a snapshot taken as an
|
|
3298
|
+
// entry opens would be re-taken after the refusal and before the renderer
|
|
3299
|
+
// that has to see it.
|
|
3300
|
+
static void enter_oom_scope(VMData *data)
|
|
2833
3301
|
{
|
|
2834
3302
|
if (!NIL_P(data->r_registrar_error))
|
|
2835
3303
|
{
|
|
@@ -2849,9 +3317,18 @@ static void check_vm_poisoned(VMData *data)
|
|
|
2849
3317
|
|
|
2850
3318
|
if (data->oom_poisoned)
|
|
2851
3319
|
{
|
|
2852
|
-
VALUE r_msg =
|
|
3320
|
+
VALUE r_msg = data->condemned_at_build
|
|
3321
|
+
? rb_str_new2("VM is poisoned: an allocation was refused while this VM was being built, before any evaluation ran. A new VM with the same options will most likely be refused the same way: memory_limit is probably too small for the features requested.")
|
|
3322
|
+
: rb_str_new2("VM is poisoned: a previous evaluation hit out-of-memory; further evaluation may segfault. Recreate the Quickjs::VM.");
|
|
2853
3323
|
rb_exc_raise(rb_funcall(QUICKJSRB_ERROR_FOR(QUICKJSRB_ROOT_RUNTIME_ERROR), rb_intern("new"), 2, r_msg, Qnil));
|
|
2854
3324
|
}
|
|
3325
|
+
|
|
3326
|
+
// Only the outermost call opens a scope. A nested one — a define_function
|
|
3327
|
+
// proc or an on_log listener re-entering the VM while JS is in flight —
|
|
3328
|
+
// inherits the scope it was made from, because taking its own would drop a
|
|
3329
|
+
// refusal the enclosing call has already met and is still going to report.
|
|
3330
|
+
if (data->evals_in_flight == 0)
|
|
3331
|
+
data->alloc_refusals_at_scope = data->alloc_refusals;
|
|
2855
3332
|
}
|
|
2856
3333
|
|
|
2857
3334
|
static void check_disposed(VMData *data)
|
|
@@ -2923,7 +3400,7 @@ struct eval_code_job
|
|
|
2923
3400
|
JSValue result;
|
|
2924
3401
|
};
|
|
2925
3402
|
|
|
2926
|
-
// Shared eval core: JS_Eval (+
|
|
3403
|
+
// Shared eval core: JS_Eval (+ await_promise and the {value, done} unwrap for
|
|
2927
3404
|
// async). Pure C over JSValues — MUST NOT touch the Ruby VM, because the
|
|
2928
3405
|
// pure path runs it with the GVL released (see js_quickjsrb_log's dispatcher
|
|
2929
3406
|
// for how console.log re-acquires). The bridged path calls it directly with
|
|
@@ -3361,7 +3838,7 @@ static VALUE eval_code_job_run_body(VALUE p)
|
|
|
3361
3838
|
return to_rb_return_value(job->ctx, job->result);
|
|
3362
3839
|
}
|
|
3363
3840
|
|
|
3364
|
-
// Unwraps
|
|
3841
|
+
// Unwraps await_promise's {value, done}, or raises the JS exception.
|
|
3365
3842
|
static VALUE bytecode_eval_result_to_rb(JSContext *ctx, JSValue j_result)
|
|
3366
3843
|
{
|
|
3367
3844
|
if (JS_IsException(j_result))
|
|
@@ -3448,7 +3925,7 @@ static VALUE vm_m_evalCode(int argc, VALUE *argv, VALUE r_self)
|
|
|
3448
3925
|
TypedData_Get_Struct(r_self, VMData, &vm_type, data);
|
|
3449
3926
|
|
|
3450
3927
|
check_disposed(data);
|
|
3451
|
-
|
|
3928
|
+
enter_oom_scope(data);
|
|
3452
3929
|
check_js_entry_owner(data);
|
|
3453
3930
|
|
|
3454
3931
|
VALUE r_code, r_opts;
|
|
@@ -3665,7 +4142,7 @@ static VALUE vm_m_compile(int argc, VALUE *argv, VALUE r_self)
|
|
|
3665
4142
|
TypedData_Get_Struct(r_self, VMData, &vm_type, data);
|
|
3666
4143
|
|
|
3667
4144
|
check_disposed(data);
|
|
3668
|
-
|
|
4145
|
+
enter_oom_scope(data);
|
|
3669
4146
|
check_js_entry_owner(data);
|
|
3670
4147
|
|
|
3671
4148
|
VALUE r_code, r_opts;
|
|
@@ -3779,7 +4256,7 @@ static VALUE vm_m_compileModule(VALUE r_self, VALUE r_code, VALUE r_name)
|
|
|
3779
4256
|
TypedData_Get_Struct(r_self, VMData, &vm_type, data);
|
|
3780
4257
|
|
|
3781
4258
|
check_disposed(data);
|
|
3782
|
-
|
|
4259
|
+
enter_oom_scope(data);
|
|
3783
4260
|
check_js_entry_owner(data);
|
|
3784
4261
|
Check_Type(r_code, T_STRING);
|
|
3785
4262
|
Check_Type(r_name, T_STRING);
|
|
@@ -3868,7 +4345,7 @@ static VALUE vm_m_preloadModuleBytecode(VALUE r_self, VALUE r_bytecode, VALUE r_
|
|
|
3868
4345
|
TypedData_Get_Struct(r_self, VMData, &vm_type, data);
|
|
3869
4346
|
|
|
3870
4347
|
check_disposed(data);
|
|
3871
|
-
|
|
4348
|
+
enter_oom_scope(data);
|
|
3872
4349
|
check_js_entry_owner(data);
|
|
3873
4350
|
|
|
3874
4351
|
// Strict String rather than StringValue's coercion, matching
|
|
@@ -3930,7 +4407,22 @@ static VALUE vm_m_preloadModuleBytecode(VALUE r_self, VALUE r_bytecode, VALUE r_
|
|
|
3930
4407
|
js_module_set_import_meta(data->context, j_mod, FALSE, FALSE);
|
|
3931
4408
|
|
|
3932
4409
|
JSAtom j_baked = JS_GetModuleName(data->context, JS_VALUE_GET_PTR(j_mod));
|
|
4410
|
+
// An atom is already a string, so only a refusal answers NULL here, and it
|
|
4411
|
+
// takes a non-ASCII name to get that far: JS_ToCStringLen2 hands back the
|
|
4412
|
+
// string's own buffer for a pure-ASCII one, which cannot fail. The empty name
|
|
4413
|
+
// it used to fall back to is compared against the name the caller filed the
|
|
4414
|
+
// blob under two checks below, so an exhaustion was reported as a
|
|
4415
|
+
// disagreement about names, on a VM the latch had already condemned.
|
|
4416
|
+
uint64_t refusals_before = data->alloc_refusals;
|
|
3933
4417
|
const char *baked = JS_AtomToCString(data->context, j_baked);
|
|
4418
|
+
if (baked == NULL && data->alloc_refusals > refusals_before)
|
|
4419
|
+
{
|
|
4420
|
+
data->oom_poisoned = true;
|
|
4421
|
+
JS_FreeAtom(data->context, j_baked);
|
|
4422
|
+
JS_FreeValue(data->context, j_mod);
|
|
4423
|
+
JS_FreeValue(data->context, JS_GetException(data->context));
|
|
4424
|
+
rb_exc_raise(r_out_of_memory_error());
|
|
4425
|
+
}
|
|
3934
4426
|
VALUE r_baked = rb_str_new_cstr(baked ? baked : "");
|
|
3935
4427
|
if (baked)
|
|
3936
4428
|
JS_FreeCString(data->context, baked);
|
|
@@ -3974,7 +4466,7 @@ static VALUE vm_m_evalBytecode(VALUE r_self, VALUE r_bytecode)
|
|
|
3974
4466
|
TypedData_Get_Struct(r_self, VMData, &vm_type, data);
|
|
3975
4467
|
|
|
3976
4468
|
check_disposed(data);
|
|
3977
|
-
|
|
4469
|
+
enter_oom_scope(data);
|
|
3978
4470
|
check_js_entry_owner(data);
|
|
3979
4471
|
|
|
3980
4472
|
if (!RB_TYPE_P(r_bytecode, T_STRING))
|
|
@@ -4058,7 +4550,7 @@ static VALUE vm_m_loadPolyfillBytecode(VALUE r_self, VALUE r_bytecode)
|
|
|
4058
4550
|
}
|
|
4059
4551
|
|
|
4060
4552
|
check_disposed(data);
|
|
4061
|
-
|
|
4553
|
+
enter_oom_scope(data);
|
|
4062
4554
|
check_js_entry_owner(data);
|
|
4063
4555
|
|
|
4064
4556
|
// "Unbudgeted" needs enforcing, not just skipping arm_eval_timer: the
|
|
@@ -4181,13 +4673,19 @@ static VALUE define_function_resolve_and_install(VALUE p)
|
|
|
4181
4673
|
// The first segment is not a lookup, it is an eval: `myLib` can be a
|
|
4182
4674
|
// lexical binding as well as a global property, and either can be an
|
|
4183
4675
|
// accessor.
|
|
4184
|
-
|
|
4676
|
+
// Through a local, not the array slot: StringValueCStr takes a writable
|
|
4677
|
+
// pointer so it can store a converted String back, and RARRAY_AREF hands
|
|
4678
|
+
// out the array's own const storage. The segments are Strings already, so
|
|
4679
|
+
// nothing is stored, but the slot is not ours to offer.
|
|
4680
|
+
VALUE r_first_seg = RARRAY_AREF(r_segs, 0);
|
|
4681
|
+
const char *first_seg = StringValueCStr(r_first_seg);
|
|
4185
4682
|
install->j_parent = JS_Eval(ctx, first_seg, strlen(first_seg), vmInternalFilename, JS_EVAL_TYPE_GLOBAL);
|
|
4186
4683
|
check_path_segment(ctx, install->j_parent, first_seg);
|
|
4187
4684
|
|
|
4188
4685
|
for (long i = 1; i < path_len - 1; i++)
|
|
4189
4686
|
{
|
|
4190
|
-
|
|
4687
|
+
VALUE r_seg = RARRAY_AREF(r_segs, i);
|
|
4688
|
+
const char *seg = StringValueCStr(r_seg);
|
|
4191
4689
|
JSValue j_next = JS_GetPropertyStr(ctx, install->j_parent, seg);
|
|
4192
4690
|
JS_FreeValue(ctx, install->j_parent);
|
|
4193
4691
|
install->j_parent = j_next;
|
|
@@ -4206,7 +4704,8 @@ static VALUE define_function_resolve_and_install(VALUE p)
|
|
|
4206
4704
|
install->j_parent = JS_GetGlobalObject(ctx);
|
|
4207
4705
|
}
|
|
4208
4706
|
|
|
4209
|
-
|
|
4707
|
+
VALUE r_func_name = RARRAY_AREF(r_segs, path_len - 1);
|
|
4708
|
+
const char *funcName = StringValueCStr(r_func_name);
|
|
4210
4709
|
|
|
4211
4710
|
JSValue j_func = JS_NewCFunctionData(ctx, js_quickjsrb_call_global, 1, 0, 2, install->ruby_data);
|
|
4212
4711
|
if (JS_IsException(j_func))
|
|
@@ -4318,6 +4817,12 @@ static VALUE vm_m_defineGlobalFunction(int argc, VALUE *argv, VALUE r_self)
|
|
|
4318
4817
|
TypedData_Get_Struct(r_self, VMData, &vm_type, data);
|
|
4319
4818
|
|
|
4320
4819
|
check_disposed(data);
|
|
4820
|
+
// The one entry point that opened no scope. Two things followed: it ran on a
|
|
4821
|
+
// condemned VM instead of refusing, and the readers it reaches through its
|
|
4822
|
+
// own JS_Eval measured against whatever call last opened a scope, so a
|
|
4823
|
+
// refusal an earlier call had already recovered from could be reported here
|
|
4824
|
+
// as this one running out.
|
|
4825
|
+
enter_oom_scope(data);
|
|
4321
4826
|
check_no_gvl_release_in_flight(data);
|
|
4322
4827
|
|
|
4323
4828
|
struct define_function_call call = {
|
|
@@ -4524,7 +5029,7 @@ static VALUE call_global_function_run(VALUE p)
|
|
|
4524
5029
|
JS_FreeValue(data->context, j_func);
|
|
4525
5030
|
JS_FreeValue(data->context, j_this);
|
|
4526
5031
|
|
|
4527
|
-
//
|
|
5032
|
+
// await_promise handles both async (promise) and sync results; frees j_result
|
|
4528
5033
|
return to_rb_return_value(data->context, quickjsrb_host_await(data->context, j_result));
|
|
4529
5034
|
}
|
|
4530
5035
|
|
|
@@ -4537,7 +5042,7 @@ static VALUE vm_m_callGlobalFunction(int argc, VALUE *argv, VALUE r_self)
|
|
|
4537
5042
|
TypedData_Get_Struct(r_self, VMData, &vm_type, data);
|
|
4538
5043
|
|
|
4539
5044
|
check_disposed(data);
|
|
4540
|
-
|
|
5045
|
+
enter_oom_scope(data);
|
|
4541
5046
|
check_js_entry_owner(data);
|
|
4542
5047
|
|
|
4543
5048
|
// evals_in_flight stays elevated for the whole call, not just the
|
|
@@ -4705,7 +5210,7 @@ static VALUE vm_m_import(int argc, VALUE *argv, VALUE r_self)
|
|
|
4705
5210
|
TypedData_Get_Struct(r_self, VMData, &vm_type, data);
|
|
4706
5211
|
|
|
4707
5212
|
check_disposed(data);
|
|
4708
|
-
|
|
5213
|
+
enter_oom_scope(data);
|
|
4709
5214
|
check_js_entry_owner(data);
|
|
4710
5215
|
|
|
4711
5216
|
// Module top-level code is user JS like any eval — budget it. Without
|
|
@@ -4781,6 +5286,12 @@ static VALUE vm_m_memoryUsage(VALUE r_self)
|
|
|
4781
5286
|
rb_hash_aset(h, ID2SYM(rb_intern("js_func_code_size")), LL2NUM(s.js_func_code_size));
|
|
4782
5287
|
rb_hash_aset(h, ID2SYM(rb_intern("c_func_count")), LL2NUM(s.c_func_count));
|
|
4783
5288
|
rb_hash_aset(h, ID2SYM(rb_intern("array_count")), LL2NUM(s.array_count));
|
|
5289
|
+
// Ours rather than QuickJS's: the number of allocations this VM asked for
|
|
5290
|
+
// and did not get. QuickJS carries on correctly through some of them, so a
|
|
5291
|
+
// nonzero count is not by itself a VM in trouble; it is the one number that
|
|
5292
|
+
// says the ceiling was reached at all, which memory_poisoned? cannot, and
|
|
5293
|
+
// malloc_size cannot either once the failed allocation is rolled back.
|
|
5294
|
+
rb_hash_aset(h, ID2SYM(rb_intern("alloc_refusals")), ULL2NUM(data->alloc_refusals));
|
|
4784
5295
|
return h;
|
|
4785
5296
|
}
|
|
4786
5297
|
|
|
@@ -4821,7 +5332,7 @@ static VALUE vm_m_drainJobs(VALUE r_self)
|
|
|
4821
5332
|
TypedData_Get_Struct(r_self, VMData, &vm_type, data);
|
|
4822
5333
|
|
|
4823
5334
|
check_disposed(data);
|
|
4824
|
-
|
|
5335
|
+
enter_oom_scope(data);
|
|
4825
5336
|
check_js_entry_owner(data);
|
|
4826
5337
|
|
|
4827
5338
|
// Rejections can be pending with no job queued.
|