aboutsummaryrefslogtreecommitdiffstats
path: root/erts/emulator/beam/beam_load.c
AgeCommit message (Collapse)Author
2019-07-01Merge branch 'maint'John Högberg
* maint: erts: Fix integer overflow in loader erts: Fix integer overflow in list subtraction
2019-07-01Merge branch 'john/erts/lists_subtract_fixes/OTP-15938/OTP-15939' into maintJohn Högberg
* john/erts/lists_subtract_fixes/OTP-15938/OTP-15939: erts: Fix integer overflow in loader erts: Fix integer overflow in list subtraction
2019-07-01erts: Fix integer overflow in loaderJohn Högberg
qsort expects the comparison function to return an int; returning an `Sint` may yield nonsensical results.
2019-06-14Support sharing of fun entries in the runtime systemBjörn Gustavsson
Before OTP 23, there had to be a one-to-one correspodence between `make_fun2` instructions and fun entries. In the OTP 23 runtime system, start keying the fun entries with the explicit ("new") index included in the fun entry. That will allow multiple `make_fun2` instructions to refer to the same entry in the fun table. This change is safe for code compiled by OTP R15B or later, because the old and new indices are identical. The loader will refuse to load code where the old and new indices are not equal unless the code is known to be compiled with an OTP 23 compiler.
2019-05-22Make the swap instruction known to the compilerBjörn Gustavsson
BEAM has had a `swap` instruction for several releases, but it was not known to the compiler. The loader would translate a sequence of three `move` instructions to the `swap` instructions, but only when it was possible to determine that it would be safe. By making `swap` known to the compiler, it can be applied in more situations since it is easier for the compiler than for the loader to ensure that the usage is safe, and the loader shenanigans can be eliminated.
2019-03-28Merge branch 'john/erts/remove-destructive-bs_get_binary2/ERL-901'John Högberg
* john/erts/remove-destructive-bs_get_binary2/ERL-901: erts: Remove unsafe bs_get_binary2 optimization from loader
2019-03-27erts: Remove unsafe bs_get_binary2 optimization from loaderJohn Högberg
A load-time optimization assumed that match contexts had no further uses when a bs_get_binary2 overwrote the match context's register, and figured it would be safe to reuse the match context's memory for the resulting binary. This is no longer safe as of OTP 22, as a match context may be reused after being passed to another function.
2019-03-27Fix unsafe optimization made by the loaderBjörn Gustavsson
Fix the unsafe load-time optimization introduced in 07bdbb3a1edc. https://bugs.erlang.org/browse/ERL-899
2019-03-19Optimize funs converted to literalsBjörn Gustavsson
2019-03-19Optimize some common uses of '+' and '-'Björn Gustavsson
2019-03-19Replace swap_temp with swap more aggressivelyBjörn Gustavsson
Also support swap of Y registers.
2019-03-19beam_load.c: Introduce the GENOP_NAME_ARITY() macroBjörn Gustavsson
Introduce the GENOP_NAME_ARITY() macro to avoid setting the arity wrong for for an instruction.
2019-03-09Merge pull request #2176 from josevalim/jv-beam-load-messageBjörn Gustavsson
Clarify beam_load error message on file/module mismatch
2019-03-08Clarify beam_load error message on file/module mismatchJosé Valim
This is particularly important in case insensitive filesystems, where attempting to invoke a module with the wrong case leads to confusing error messages: 1> erlpress_core:foo(). beam/beam_load.c(1428): Error loading module 'erlpress_core': module name in object code is erlPress_core Loading of erlPress_core.beam failed: :badfile This commit replaces object code by BEAM file and improves the readability of the message.
2019-03-07Slightly optimize binary constructionBjörn Gustavsson
Use S operands instead of s operands for a slight speed increase and reduction in code size of process_main(). Use micro instructions for frequently executed instructions. While at it, use safe multiplication in gen_get_integer() in beam_load.c.
2019-03-06Eliminate unused i_bs_skip_bits_all2 instructionBjörn Gustavsson
Starting in OTP 19 (in commit 9504c0dd71d0), the compiler emits a test_unit instruction instead of a skip instruction at the end of binary. We can do the same replacement in the loader to get rid of the i_bs_skip_bits_all2 instruction.
2019-03-06Reduce code size for binary matching instructionsBjörn Gustavsson
The new compiler required adding support for Y register for all binary matching instructions. That was (intentionally) done in a naive way that simplicated duplicated the entire body of each instruction. Now it's time to be less naive. Rewrite the binary matching instructions using micro instructions. Because some of the binary instructions are huge, that will significantly decrease the size of process_main(). When compiling with clang, a huge process_main() would mess up profile-guide optimization resulting in a significant performance degradation. On my Mac, profile-guide optimzation would decrease the estone benchmark by 100K estones (about 20 percent). This commit gives me back the lost estones.
2019-03-01Combine more init instructionsBjörn Gustavsson
2019-02-28Tune move instructionsBjörn Gustavsson
Of the `move_dup` instructions, only `move_dup x x x` was frequently used. Remove the other register combinations. With those instruction `move_dup` instructions removed, it is necessary to add new predicates to avoid unsafe translation to `move_shift` and `move2_par`. Also add additional transformations to transform more `move` instructions into `move2_par`. The existing transformation would require the `move` instructions to be in the "right" order in order to be transformed. Remove `move3 x y x y x y` because it turns out to be rarely executed.
2019-02-22erts: Add ERL_NODE_BOOKKEEP to node tables refcLukas Larsson
2019-01-21Optimize the is_function/2 guard testBjörn Gustavsson
The is_function2 instruction is executed surprisingly frequently when running dialyzer or the compiler. It cannot hurt to optimize it a little.
2018-12-13Simplify GC BIFsBjörn Gustavsson
Summary: This commit simplifies the implementation of the "GC BIFs" so that they no longer need to do a garbage collection, removing duplicate code for all GC BIFs in the runtime system, as well as potentially reducing the size of the loaded BEAM code by using shorter instructions calling those BIFs. A GC BIF is a guard BIF that will do a garbage collection if it needs to build anything on the heap. For example, `abs/1` is a GC BIF because it might need to allocate space on the heap (if the result is a floating point number or the resulting integer is a bignum). Before R12, a guard BIF (such as `abs/1`) that need to allocate heap space would allocate outside of process's main heap, in a heap fragment. GC BIFs were introduced in R12B to support literals. During garbage collection it become necessary to quickly test whether a term was a literal. To make the check simple, guards BIFs were no longer allowed to create heap fragments. Instead GC BIFs were introduced. In OTP 19, the implementation of literals was changed to support storing messages in heap fragments outside of the main heap for a process. That change again made it allowed for guard BIFs to create heap fragments when they need to build terms on the heap. It would even be possible for the guard BIFs to build directly on the main heap if there is room there, because the compiler assumes that a new `test_heap/2` instruction must be emitted when building anything after calling a GC BIF. (We don't do that in this commit; see below.) This commit simplifies the implementation of the GC BIFs in the runtime system. Each GC BIF had a dual implementation: one that was used when the GC BIF was called directly and one used when it was called via `apply/3`. For example, `abs/1` was implemented in `abs_1()` and `erts_gc_abs_1()`. This commit removes the GC version of each BIF. The other version that allocates heap space using `HAlloc()` is updated to use the new `HeapFragOnlyAlloc()` macro that will allocate heap space in a heap fragment outside of the main heap. Because the BIFs will allocate outside of the main heap, the same `bif` instructions used by nonbuilding BIFs can be used for the (former) GC BIFs. Those instructions don't use the macros that save and restore the heap and stack pointers (SWAPOUT/SWAPIN). If the former GC BIFs would build on the main heap, either new instructions would be needed, or SWAPOUT/SWAPIN instructions would need to be added to the `bif` instructions. Instructions that call the former GC BIFs don't need the operand that specifies the number of live X registers. Therefore, the instructions that call the BIFs are usually one word shorter.
2018-09-24Support using match contexts from Y registersJohn Högberg
The upcoming beam_ssa_bsm pass allows match contexts to be used across function calls that take said context as an argument, which means it's fairly common for them to end up in Y registers.
2018-07-17Optimise creation of anonymous functionsMichał Muskała
This introduces a similar optimisation for normal funs to what was introduced for external funs in #1725. It is possible to allocate the fun as a literal, if it does not capture the environment (i.e. it does not close over any variables). Unfortunately it's not possible to do this in the compiler due to problems with representation of such functions in the `.beam` files. Fortunately, we can do this in the loader. Simple evaluation shows that functions that don't capture the enviornment consistute over 60% of all funs in the source code of Erlang/OTP itself. The only downside is that we lose a meningful value in the `pid` field of the fun. The goal of this field, beyond debugging, was to be able to identify the original node of a function. To be able to still do this, the functions that are created in the loader are assigned the init pid as the creator. To solve issues with staryp, initially set the `erts_init_process_id` to `ERTS_INVALID_PID` and skip the described optimisation if the value is still uninitialised.
2018-04-04Merge pull request #1725 from michalmuskala/fun-literalsBjörn Gustavsson
Compile external fun expressions to literals OTP-15003
2018-03-26Compile external fun expressions to literalsMichał Muskała
The expressions fun M:F/A, when all elements are literals are also treated as a literal. Since they have consistent representation and don't depend on the code currently loaded in the VM, this is safe. This can provide significant performance improvements in code using such functions extensively - a full function call to erlang:make_fun/3 is replaced by a single move instruction and no register shuffling or saving registers to stack is necessary. Additionally, compound data types that contain such external functions as elements can be treated as literals too. The commit also changes the representation of external funs to be a valid Erlang syntax and adds support for literal external funs to core Erlang.
2018-03-21Add an option to ?MODULE:module_info/1 for listing NIFsJohn Högberg
2018-03-14erts: Fix faulty sys_memcpy of 0 bytesSverker Eriksson
2018-03-09Always use sys_memcpy/cmp/etc instead of plain memcpy/cmp/etcJohn Högberg
2018-01-24Merge branch 'maint'Sverker Eriksson
2018-01-24Merge branch 'sverker/hipe-load-fixing/OTP-14891' into maintSverker Eriksson
2018-01-17Fix slow hipe executionSverker Eriksson
particularly slow erlc when compiler is hipe compiled. hipe_unified_loader:load did not patch external call sites and instead caused a double hipe mode switch per call. hipe_unified_loader:load is only used for early modules first loaded as beam and by code:atomic_load and friends.
2018-01-11Merge branch 'maint'Rickard Green
* maint: Fix encoding of filenames in stacktraces
2018-01-11Fix encoding of filenames in stacktracesRickard Green
2017-11-27erts: Fix bad merge of PR #1644Björn Gustavsson
2017-11-27Merge branch 'maint'John Högberg
2017-11-24Fix purging of modules with "fake literals"Björn Gustavsson
When compiling Erlang source code, the literal area for the module can only contain data types that have a literal syntax. However, it is possible to sneak in other data types (such as references) in the literal pool by compiling from abstract or assembly code. Those "fake literals" would work fine, but would crash the runtime system when the module containing the literals was purged. Although fake literals are not officially supported, the runtime should not crash when attempting to use them. Therefore, fix the garbage collection of literals and releasing of literal areas. https://bugs.erlang.org/browse/ERL-508
2017-11-13Refuse to load "literals" that can be confused with registersBjörn Gustavsson
The 's' operand overloads the tags for pids and ports to represent X and Y registers, respectively. At load time, refuse to load the module if the "literal" term is not a pid or port, as it would be interpreted as a register. This does not happen with normally compiled code, but it can happen if the compiler (or beam_asm) is abused like in the following example: make_bad() -> Pid = self(), Forms = [{attribute, 0, module, bad_s_operand}, {attribute, 0, export, [{test, 0}]}, {function, 0, test, 0, [{clause, 0, [], [], [{call,0,{atom,0,tuple_size},[{integer, 0, Pid}]}]}]}], {ok, Module, Bin} = compile:forms(Forms, [no_copt,no_postopt,report_errors]), code:load_binary(Module, "bad_s_operand.erl", Bin). With this commit applied, the following message will be printed when make_bad() is run: =ERROR REPORT==== 10-Nov-2017::14:47:59 === Loading of bad_s_operand.erl failed: badfile =ERROR REPORT==== 10-Nov-2017::14:47:59 === beam/beam_load.c(2396): Error loading function bad_s_operand:test/0: op bif1_body: bsd: the term '<0.60.0>' would be confused with a register
2017-10-30Merge branch 'lukas/erts/misc_fixes'Lukas Larsson
* lukas/erts/misc_fixes: erts: Fix a bunch of compiler warnings kernel: Fix gen_tcp_misc indentation erts: Fail port_SUITE:huge_env if error code > 127 erts: Add lcnt prototype for dist locks update
2017-10-30erts: Fix a bunch of compiler warningsLukas Larsson
2017-10-17Merge branch 'sverker/on_load-nonblocking/OTP-14680'Sverker Eriksson
* sverker/on_load-nonblocking/OTP-14680: erts: Remove scheduler blocking during finish_after_on_load_2
2017-10-12erts: Remove scheduler blocking during finish_after_on_load_2Sverker Eriksson
for normal case. We still block for default trace and hipe.
2017-10-05Pack operands into the instruction wordBjörn Gustavsson
On 64-bit machines where the C code is always at address below 4Gb, pack one or more operands into the instruction word.
2017-10-01beam_load.c: Generalize the 'P' operator in the packing engineBjörn Gustavsson
In the 'P' operator, don't assume that a packed target label ('f' or 'j') is always the leftmost argument. Instead, transfer the patch position from the accumulator to the stack.
2017-10-01Refactor macros for accessing Beam instructionsBjörn Gustavsson
The BeamOp() macro in erl_vm.h is clumsy to use. All users cast the return value to BeamInstr. Define new macros that are easier to use. In the future, we might want to pack an operand into the same word as the pointer to the instruction, so we will define two macros. BeamIsOpCode() is used to rewrite code like this: if (Instr == (BeamInstr) BeamOp(op_i_func_info_IaaI) { ... } to: if (BeamIsOpCode(Instr, op_i_func_info_IaaI)) { ... } BeamOpCodeAddr(op_apply_bif) is used when we need the address for an instruction. Also elimiminate the global variables em_* in beam_emu.c. They are not really needed. Use the BeamOpCodeAddr() macro instead.
2017-09-14Pack failure labels in i_select_val2 and i_select_tuple_arity2Björn Gustavsson
2017-09-14Rewrite select_val_bins so that its labels can be packedBjörn Gustavsson
2017-09-14Pack sequences of trailing 'f' operandsBjörn Gustavsson
Pack sequences of trailing 'f' operands for instructions such at jump_on_val or i_select_val_lins.
2017-09-14Implement packing of 'f' and 'j'Björn Gustavsson
2017-09-14Use relative failure labelsBjörn Gustavsson
Relative failure in itself is not an optimization, but we plan to pack failure labels in the future to save memory.