Commit Graph

206 Commits

Author SHA1 Message Date
Roberto Ierusalimschy
997f11f543 Bug: 'break' may not properly close variable in a 'for' loop
Function 'leaveblock' was generating "break" label before removing
variables from the closing block. If 'createlabel' created a 'close'
instruction (which it did when matching a goto/break that exited
the scope of an upvalue), that instruction would use the wrong level.
2022-08-24 17:36:47 -03:00
Roberto Ierusalimschy
02060b7a37 Simpler handling of Byte Order Mark (BOM) 2022-08-23 16:08:53 -03:00
Roberto Ierusalimschy
d61b0c6028 More checks and documentation for uses of EXTRA_STACK 2022-05-26 15:14:54 -03:00
Roberto Ierusalimschy
196bb94d66 Bug: 'lua_settop' may use an invalid pointer to stack 2022-05-25 17:41:39 -03:00
Roberto Ierusalimschy
4a00f61276 'lua_checkstack' doesn't need to check stack overflow
'luaD_growstack' already checks that. This commit also fixes an
internal bug in 'luaD_growstack': a large 'n' could cause an arithmetic
overflow when computing 'needed'.
2022-05-23 10:38:03 -03:00
Roberto Ierusalimschy
c764ca71a6 Bug: Wrong code generation in bitwise operations 2022-04-25 14:42:51 -03:00
Roberto Ierusalimschy
1f3c6f4534 Bug: Lua can generate wrong code when _ENV is <const> 2022-02-15 12:28:46 -03:00
Roberto Ierusalimschy
597a53bbc6 Bug: finalizer calling exit can corrupt finalization order
'os.exit' can call lua_close again, separating new finalizers
created after all previous finalizers were already separated.
2021-12-22 09:00:52 -03:00
Roberto Ierusalimschy
066e0f93c4 Fix debug information about finalizers
The flag CIST_FIN does not mark a finalizer, but the function that was
running when the finalizer was called. (So, the function did not call
the finalizer, but it looks that way in the stack.)
2021-12-14 12:50:05 -03:00
Roberto Ierusalimschy
0bfc572e51 Bug: GC is not reentrant
As the GC is not reentrant, finalizers should not be able to invoke it.
2021-12-13 10:41:17 -03:00
Roberto Ierusalimschy
48835c76c8 Wrong assert in 'collectvalidlines' 2021-11-25 10:11:05 -03:00
Roberto Ierusalimschy
e8deac5a41 Avoid OP_VARARGPREP for active lines
when building the table 'activelines' for a vararg function, this
first instruction does not make the first line active.
2021-11-10 15:07:14 -03:00
Roberto Ierusalimschy
bfbff3703e Bug: Wrong status in coroutine during reset
When closing variables during 'coroutine.close' or 'lua_resetthread',
the status of a coroutine must be set to LUA_OK; a coroutine should
not run with any other status. (See assertion in 'lua_callk'.)

After the reset, the status should be kept as normal, as any error
was already reported.
2021-11-08 11:55:25 -03:00
Roberto Ierusalimschy
74d99057a5 Bug: C stack overflow with coroutines
'coroutine.resume' did not increment counter of C calls when
continuing execution after a protected error (that is,
while running 'precover').
2021-11-03 15:04:18 -03:00
Roberto Ierusalimschy
deac067ed3 Avoid overflows when incrementing parameters in C
Any C function can receive maxinteger as an integer argument, and
therefore cannot increment it without some care (e.g., doing unsigned
arithmetic as the core does).
2021-09-22 13:10:39 -03:00
Roberto Ierusalimschy
9db4bfed6b Revamp of format validation in 'string.format'
When calling 'sprintf', not all conversion specifiers accept all
flags; some combinations are undefined behavior.
2021-09-03 13:14:56 -03:00
Roberto Ierusalimschy
a393ac2554 Detail in 'testes/math.lua'
Added a print with the random seeds used in the tests of 'random'.
2021-08-18 10:46:18 -03:00
Roberto Ierusalimschy
65434b4d1b Option '-l' can give a name for the global variable.
Sintax for this option now is '-l [globname=]modname'.
2021-08-16 13:57:19 -03:00
Roberto Ierusalimschy
59acd79c05 Added tests for string reuse by the scanner 2021-08-11 11:19:33 -03:00
Roberto Ierusalimschy
439e45a2f6 Bug: luaL_tolstring may get confused with negative index
When object has a '__name' metafield, 'luaL_tolstring' used the
received index after pushing a string on the stack.
2021-07-22 13:48:43 -03:00
Roberto Ierusalimschy
62fb934427 Bug: Negation in 'luaV_shiftr' may overflow
Negation of an unchecked lua_Integer overflows with mininteger.
2021-07-22 13:44:53 -03:00
Roberto Ierusalimschy
6a0dace25a Bug: 'local function' can assign to '<const>' variables 2021-06-20 15:36:36 -03:00
Roberto Ierusalimschy
04e19712a5 C functions can be tail called, too
A tail call to a C function can have the behavior of a "real" tail
call, reusing the stack frame of the caller.
2021-06-14 13:28:21 -03:00
Roberto Ierusalimschy
681297187e Bug: yielding in '__close' mess up number of returns
Yielding in a __close metamethod called when returning vararg results
changes the top and so messes up the number of returned values.
2021-04-16 15:41:44 -03:00
Roberto Ierusalimschy
47cffdc723 Bug: tbc variables in "for" loops don't avoid tail calls 2021-04-07 14:59:26 -03:00
Roberto Ierusalimschy
36de01d988 Changes in cache for function constants
In 'lcode.c', when adding constants to the list of constants of a
function, integers represent themselves in the cache and floats
with integral values get a small delta to avoid collision with
integers. (This change avoids creating artificial addresses; the old
implementation converted integers to pointers to index the cache.)
2021-03-30 14:49:18 -03:00
Roberto Ierusalimschy
bf10593a3a Allow yields inside '__pairs' 2021-03-29 12:57:32 -03:00
Roberto Ierusalimschy
05b13651f9 File 'tracegc.lua' added to 'packtests' 2021-03-10 10:35:57 -03:00
Roberto Ierusalimschy
511d53a826 lua_settop/lua_pop closes to-be-closed variables
The existence of 'lua_closeslot' is no reason for lua_pop not to close
to-be-closed variables too.  It is too error-prone for lua_pop not to
close tbc variables being popped from the stack.
2021-03-09 11:42:45 -03:00
Roberto Ierusalimschy
f5df7f91f7 Wrong assertion in 'getbaseline'
The assertion cannot compute 'f->abslineinfo[i]' when the initial
estimate 'i' is -1.
2021-03-05 12:10:34 -03:00
Roberto Ierusalimschy
5276973224 New test module 'tracegc'
New module easies the inclusion of GC tracing in individual test files.
2021-03-01 13:54:29 -03:00
Roberto Ierusalimschy
1537d6680b New control for reentrancy of emergency collections
Instead of assuming that shrinking a block may be an emergency
collection, use an explicit field ('gcstopem') to stop emergency
collections while GC is working.
2021-02-26 11:41:02 -03:00
Roberto Ierusalimschy
e0260eb2d4 Bug (kind of) in 'isinstack'
The function 'isinstack' tried to work around the undefined behavior
of subtracting two pointers that do not point to the same object,
but the compiler killed to trick. (It optimizes out the safety check,
because in a correct execution it will be always true.)
2021-02-25 13:39:36 -03:00
Roberto Ierusalimschy
c03c527fd2 Bug: 'string.concat' error message uses wrong format 2021-02-15 13:31:45 -03:00
Roberto Ierusalimschy
38cc7d40a4 Bug: cannot allow the call 'debug.getinfo(0, ">")'
A 'what' argument starting with '>' indicates that there is a function
in the C stack, which won't be there if the first argument is not a
function.
2021-02-15 10:38:09 -03:00
Roberto Ierusalimschy
bc970005ce '__close' methods can yield in the return of a C function
When, inside a coroutine, a C function with to-be-closed slots return,
the corresponding metamethods can yield. ('__close' metamethods called
through 'lua_closeslot' still cannot yield, as there is no continuation
to go when resuming.)
2021-02-12 13:36:30 -03:00
Roberto Ierusalimschy
4e47f81188 New implementation for to-be-closed variables
To-be-closed variables are linked in their own list, embedded into the
stack elements. (Due to alignment, this information does not change
the size of the stack elements in most architectures.)  This new list
does not produce garbage and avoids memory errors when creating tbc
variables.
2021-02-09 14:00:05 -03:00
Roberto Ierusalimschy
58aa09a0b9 Small improvements in hooks
- 'L->top' is set once in 'luaD_hook', instead of being set in
'luaD_hookcall' and 'rethook';

- resume discard arguments when returning after an yield inside a hook
(arguments may interfere with the coroutine stack);

- yield inside a hook asserts it has no arguments.
2021-01-26 16:53:51 -03:00
Roberto Ierusalimschy
0e9254dfa0 Correct order of return hooks vs. close metamethods
The return hook should be called only after closing variables (which
are still part of the function). C functions were calling the hook
before the metamethods.
2021-01-21 10:27:22 -03:00
Roberto Ierusalimschy
6ccd24eff5 Simpler handling of errors when creating tbc variables
New field 'lua_State.ptbc' keeps to-be-closed variable until its
upvalue is created, so that it can be closed in case of a
memory-allocation error.
2021-01-19 10:03:13 -03:00
Roberto Ierusalimschy
d0f34d9137 Allow yields in '__close' metamethods ater errors
Completes commit b07fc10e91. '__close' metamethods can yield even
when they are being called due to an error. '__close' metamethods from
C functions are still not allowed to yield.
2021-01-18 11:40:45 -03:00
Roberto Ierusalimschy
b07fc10e91 Allow yields inside '__close' metamethods
Initial implementation to allow yields inside '__close' metamethods.
This current version still does not allow a '__close' metamethod
to yield when called due to an error. '__close' metamethods from
C functions also are not allowed to yield.
2021-01-13 13:54:10 -03:00
Roberto Ierusalimschy
cc1692515e New API function 'lua_closeslot'
Closing a to-be-closed variable with 'lua_settop' is too restrictive,
as it erases all slots above the variable. Moreover, it adds side
effects to 'lua_settop', which should be a fairly basic function.
2021-01-11 15:03:01 -03:00
Roberto Ierusalimschy
ce101dcaf7 Handles '__close' errors in coroutines in "coroutine style"
Errors in '__close' metamethods in coroutines are handled by the same
logic that handles other errors, through 'recover'.
2020-12-30 11:20:22 -03:00
Roberto Ierusalimschy
4bd10b6fe8 Better error messages for calling non-callable objects
When available, use the calling code to find a suitable name for what
was being called; this is particularly useful for errors of non-callable
metamethods. This commit also improved the debug information for
order metamethods.
2020-12-29 13:15:54 -03:00
Roberto Ierusalimschy
59e565d955 No need to recheck close method before calling it
A to-be-closed variable is constant and it must have a close metamethod
when it is created. A program has to go out of its way (e.g., by
changing the variable's metamethod) to invalidate that check. So,
it is not worth to test that again. If the program tampers with the
metamethod, Lua will raise a regular error when attempting to call it.
2020-12-29 10:23:02 -03:00
Roberto Ierusalimschy
0ceada8da9 Report last error in closing methods
When there are multiple errors around closing methods, report the
last error instead of the original.
2020-12-22 10:54:25 -03:00
Roberto Ierusalimschy
f9d29b0c44 Upvalues removed from 'openupval' before being closed
Undo commit c220b0a5d0: '__close' is not called again in case of
errors. (Upvalue is removed from the list before the call.) The
common error that justified that change was C stack overflows, which
are much rarer with the stackless implementation.
2020-12-21 15:21:45 -03:00
Roberto Ierusalimschy
409256b784 'coroutine.close'/'lua_resetthread' report original errors
Besides errors in closing methods, 'coroutine.close' and
'lua_resetthread' also consider the original error that stopped the
thread, if any.
2020-12-18 11:22:42 -03:00
Roberto Ierusalimschy
e2ea3b31c9 Details (do not affect regular code)
* Avoids multiple definitions of 'lua_assert' in test file.
* Smaller C-stack limit in test mode.
* Note in the manual about the use of false
* Extra test for constant reuse.
2020-12-07 11:17:30 -03:00