Commit Graph

183 Commits

Author SHA1 Message Date
Tor Andersson
1780d0ea73 Bug 704756: Don't trust function.length property!
Calling js_call with n < 0 led to us popping a negative number of items
from the stack, which could make us miss the stack size check.

Sanitize all uses of function.length in Function.prototype.apply and
Function.prototype.bind.
2021-12-08 12:56:12 +01:00
Tor Andersson
78e56b7854 Bug 704748: Save original object in stack slot for returning after constructor.
The object in the 'this' slot may be overwritten if the constructor converts
it to a primitive value. Save the original object in an explicit stack slot
to keep it safe for returning afterwards.
2021-12-06 16:40:44 +01:00
Tor Andersson
fe8cac61e3 Add user data to C functions with extended constructor.
Accessible from C with js_currentfunctiondata(J).
2021-11-04 13:08:51 +01:00
Tor Andersson
b06a5e9b02 Bug 704238: Limit max string lengths.
Check string length when creating strings to not exceed a maximum,
so we avoid integer overflows when concatenating strings.

The string limit must be small enough that we'll not integer overflow
in one concatenation (A + B + 1 must not overflow while still
exceeding the string limit).

Set the limit to 64KB for now.

If we need 2GB strings then we will have to use double or int64 variables
when calculating string lengths.
2021-09-07 18:02:28 +02:00
Tor Andersson
9dd965b679 Add special error handling for safe 'protected' functions. 2021-07-23 16:23:08 +02:00
Tor Andersson
a9d88e54af Handle try stack errors like stack overflow errors.
Don't create a new object, because that may cause a cascade of
other errors since we're at the limit.
2021-07-23 11:42:35 +02:00
Tor Andersson
7ef066a3bb Fix use-after-free in regexp source property access.
The underlying string of the "source" property of a regular expression
object can be freed if the regexp is garbage collected.

This could lead to a use-after-free, because the accessor incorrectly
assumed that the regexp source was an interned (thus never freed) string.
Fix this by calling js_pushstring instead of the faster but unsafe
js_pushliteral.

Many thanks to Connor Nelson for spotting this!
2021-04-23 11:13:50 +02:00
Tor Andersson
3d29cd2f2f Issue #135: Expose type of value as an enum with js_type().
This matches the values used by the 'typeof' operator.
2021-03-26 15:47:35 +01:00
Tor Andersson
33ffe6efeb Inline doubles and interned string pointers in the byte code.
Avoid linearly searched per function string and number tables.
2021-03-26 14:35:14 +01:00
Tor Andersson
06a6f9fb11 Issue #120: Optimize array construction bytecode.
Use a specialized array initializer that pushes values to the end
of the array instead of using a lot of setprop. This avoids the need
to create a lot of number constants for the array indices.
2021-03-26 12:05:35 +01:00
Tor Andersson
364acef42e Bug 703670: Throw when redefining non-configurable/readonly properties.
Add an explicit 'throw' argument to jsR_defproperty to throw in
non-strict mode too.
2021-03-10 18:10:34 +01:00
Tor Andersson
a34fdf2af8 Bug 701886: Don't redefine/reset existing vars in script code.
If a var is already declared in the same scope, don't redeclare it.
Should fix issues with "var" used in eval() code.
2021-03-04 12:20:46 +01:00
Tor Andersson
9f34a074eb Revert "Bug 701886: Always create new scope for eval()."
This reverts commit d248b0ce18.
2021-03-04 12:08:42 +01:00
Tor Andersson
789f30b587 Bug 703376: Don't allow creating new properties on transient objects. 2021-01-25 14:38:15 +01:00
Avi Halachmi (:avih)
b5eccea611 gc: use proportional instead of fixed threshold
The problem with a fixed count value is that it result in varying
degrees of performance and memory impact proportions depending on
the usage pattern of each script.

E.g. if a script keeps using the same 1M objects, then a threshold of
10K objects will result in GC cycles which free only 1% of objects,
which is hugely wasteful in terms of performance. On the other hand,
if a script only uses 100 objects then a threshold of 10K means it
uses 100 times more memory than it actually needs before GC triggers.

Now the threshold is a target memory usage factor (of the minimum
needed memory) which GC tries to aim at. This makes the GC impact
have constant proportions.

The default aims at a memory usage factor of 5, i.e. 80% garbage
and 20% remaining on each GC cycle.

The factor is only a target/goal because the actual overhead is not
known until GC completes. However, most scripts exhibit consistent
enough behavior such that the real overhead is within 10% or less of
the goal even when the usage pattern changes over time.

Within the v8 bench test suite, the actual GC threshold count varies
between ~50K to ~500K, where only one test (raytrace.js) stabilizes on
less than 10K (the previous fixed default) - at about 9K and its score
decreases by ~5%.

The splay.js score increases about x12 fold (or x50 fold from the
previous commit which counts properties), other tests quite a bit
less or none at all, and the overall score increases by nearly 40%.

Also, change the count type from int to unsigned int to get twice the
range. Preferably we should make it even bigger, maybe uint64_t.

For instance the splay.js v8 bench test surpasses million non-garbage
allocations within few seconds (that's why its score increased so much
with a proportional overhead), and the default GC threshold is 5 times
that number.
2020-07-23 12:40:09 +02:00
Tor Andersson
0261579d78 Support embedded 0 in strings by using modified UTF-8. 2020-05-27 12:32:32 +02:00
Tor Andersson
8c5f2f24c7 Don't allow setting a property when the prototype's property is read-only. 2020-05-27 12:32:32 +02:00
Tor Andersson
e3f04e7f96 Check for empty string in js_isarrayindex. 2020-02-19 10:24:33 +01:00
Tor Andersson
e082e6e61c Check for leading zero in js_isarrayindex that caused false positives.
We're supposed to check whether a string turned into an integer and back
is itself, while also returning the value of the integer. We were
unintentionally allowing integers with leading zero through.
2020-01-20 12:39:58 +01:00
Tor Andersson
d248b0ce18 Bug 701886: Always create new scope for eval().
Distinguish eval code from script code.
2020-01-02 14:37:05 +01:00
Tor Andersson
6e62eb0923 Issue 113: Add a js_delglobal function. 2019-11-19 12:56:22 +01:00
Tor Andersson
0c03f9c057 Add missing break statement. 2019-03-29 19:40:44 +01:00
Tor Andersson
bb65f18fcc Set 'lightweight' and 'arguments' during compile pass.
Avoid doing a separate analysis pass by using the same bytecode for both
lightweight and non-lightweight functions.
2019-03-22 14:00:20 +01:00
Tor Andersson
f5de9d4d2e Remove line opcode in favor of storing the line for each instruction. 2019-03-22 14:00:20 +01:00
Tor Andersson
20d0fa04df Simplify opcodes: numbers and integer constants. 2019-03-22 14:00:20 +01:00
Tor Andersson
ffe0ca7d7f Issue 95: Improve error message when trying to call a non-callable. 2019-03-18 14:11:34 +01:00
Tor Andersson
5de1f97c52 Set appropriate internal class property of arguments object. 2019-03-18 14:11:34 +01:00
Tor Andersson
6a592abfc4 Use emitarg instead of emitraw to emit opcode arguments. 2019-03-07 16:08:06 +01:00
Tor Andersson
bd9920c571 Handle null/undefined in OP_NEXTITER rather than creating empty iterator.
Only create an iterator for coercible types in OP_ITERATOR, and then
detect the lack of a real iterator in OP_NEXTITER.
Thus we don't need to allocate and push an empty iterator object for
these cases.
2019-01-07 13:28:22 +01:00
Tor Andersson
7be32a0f5f Bug 700441: Handle null and undefined expressions in for-in statement. 2019-01-04 10:58:30 +01:00
Tor Andersson
b4297c0dec Fix 699557: Pause garbage collector during Ap_sort.
The temporary array we use for sorting cannot be seen by the GC, and that
violates the constraint that all js_Value values must always be reachable
from the stack or global environment.

Temporarily turning off the GC will let us use the temporary array for
fast sorting using qsort(), without tripping over this violation.
2018-07-25 12:00:33 +02:00
Tor Andersson
40b73014d9 Fix 699549: Integer overflow in Array.prototype.sort().
Check size calculation for overflow before allocating memory buffer.
2018-07-23 11:30:22 +02:00
Tor Andersson
49271d3cf2 Add js_iserror function. 2018-07-03 12:00:57 +02:00
Tor Andersson
be0685e89e Optimize js_isarrayindex.
Avoid floating point number conversions.
2017-05-23 13:15:11 +02:00
Tor Andersson
2e7550e718 Fix bounds checks for string object array accesses. 2017-04-21 11:04:43 +02:00
Tor Andersson
1062e70c86 Track strictness during runtime based on 'use strict' pragma. 2017-04-17 23:27:00 +02:00
Tor Andersson
75cab70afd Handle undefined this at the OP_THIS level.
Don't distinguish lax mode code by pushing the global object.
Always push undefined and let the calling code promote an undefined
this to the global object instead.
2017-04-17 14:57:38 +02:00
Tor Andersson
9e9f168cbb Add strictness check when setting a property that only has a getter. 2017-04-17 14:55:12 +02:00
Tor Andersson
4006739a28 Fix 697497: Ensure array length is positive.
As a side effect when changing to using regular integers (and avoid the
nightmare of mixing signed and unsigned) we accidentally allowed negative
array lengths.
2017-01-24 14:52:27 +01:00
Tor Andersson
77ab465f1c Fix 697401: Error when dropping extra arguments to lightweight functions. 2017-01-12 14:47:01 +01:00
Tor Andersson
5c337af4b3 Fix bug 697142: Stale string pointer stored in regexp object.
Make sure to make a copy of the source pattern string.
A case we missed when adding short and memory strings to the runtime.
The code assumed all strings passed to it were either literal or interned.
2016-09-21 16:04:14 +02:00
Tor Andersson
a4158ae6ff Fix call stack overflow triggering off-by-one too late.
Thanks to katlogic <kat@lua.cz> for spotting the error.
2016-07-04 15:19:08 +02:00
Tor Andersson
1b5cc3a082 Improve debugging stack trace print-out. 2016-04-28 13:33:58 +02:00
Tor Andersson
4b68d52351 Add js_iscoercible function. 2016-04-28 12:48:09 +02:00
Tor Andersson
3e3c382a0c Avoid using 'unsigned int'.
Mixing signed and unsigned ints is just a plain headache.
2016-04-27 16:26:00 +02:00
Tor Andersson
aba6644234 Add delete callback to userdata objects. 2016-03-01 21:52:00 +01:00
Tor Andersson
2db68ab80f Chain if-else statements in has/put/delproperty. 2016-03-01 21:51:38 +01:00
Tor Andersson
0fd82b60cf Make js_try, js_savetry and js_endtry public functions/macros. 2016-01-18 13:34:00 +01:00
Tor Andersson
4e6c74b551 Add userdata has/put callbacks for custom properties.
This goes beyond defining accessors, and allows capturing all property
accesses. With these callbacks, things like typed arrays can be
implemented.
2016-01-11 17:02:43 +01:00
Tor Andersson
d800b59f0f Clean up stack on errors in js_pcall and js_pconstruct.
Exactly one value will remain on the stack after js_pcall: either
the return value or the error object.
2016-01-06 13:43:59 +01:00