summaryrefslogtreecommitdiff
path: root/vm_eval.c
AgeCommit message (Collapse)Author
2020-02-22Introduce disposable call-cache.Koichi Sasada
This patch contains several ideas: (1) Disposable inline method cache (IMC) for race-free inline method cache * Making call-cache (CC) as a RVALUE (GC target object) and allocate new CC on cache miss. * This technique allows race-free access from parallel processing elements like RCU. (2) Introduce per-Class method cache (pCMC) * Instead of fixed-size global method cache (GMC), pCMC allows flexible cache size. * Caching CCs reduces CC allocation and allow sharing CC's fast-path between same call-info (CI) call-sites. (3) Invalidate an inline method cache by invalidating corresponding method entries (MEs) * Instead of using class serials, we set "invalidated" flag for method entry itself to represent cache invalidation. * Compare with using class serials, the impact of method modification (add/overwrite/delete) is small. * Updating class serials invalidate all method caches of the class and sub-classes. * Proposed approach only invalidate the method cache of only one ME. See [Feature #16614] for more details. Notes: Merged: https://github.com/ruby/ruby/pull/2888
2020-02-22VALUE size packed callinfo (ci).Koichi Sasada
Now, rb_call_info contains how to call the method with tuple of (mid, orig_argc, flags, kwarg). Most of cases, kwarg == NULL and mid+argc+flags only requires 64bits. So this patch packed rb_call_info to VALUE (1 word) on such cases. If we can not represent it in VALUE, then use imemo_callinfo which contains conventional callinfo (rb_callinfo, renamed from rb_call_info). iseq->body->ci_kw_size is removed because all of callinfo is VALUE size (packed ci or a pointer to imemo_callinfo). To access ci information, we need to use these functions: vm_ci_mid(ci), _flag(ci), _argc(ci), _kwarg(ci). struct rb_call_info_kw_arg is renamed to rb_callinfo_kwarg. rb_funcallv_with_cc() and rb_method_basic_definition_p_with_cc() is temporary removed because cd->ci should be marked. Notes: Merged: https://github.com/ruby/ruby/pull/2888
2020-02-11Remove rb_eval_cmdJeremy Evans
This was related to $SAFE, and was deprecated in 2.7. I missed it earlier when removing the other $SAFE-related code. Notes: Merged: https://github.com/ruby/ruby/pull/2899
2020-01-06script_compiled event on compile error.Koichi Sasada
script_compiled event for TracePoint should not be invoked on compile error (SyntaxError) because it is not "compiled". [Bug #16459]
2020-01-03Make eval(code, binding) use (eval) as __FILE__ and 1 as __LINE__Jeremy Evans
This removes the warning that was added in 3802fb92ff8c83eed3e867db20f72c53932f542d, and switches the behavior so that the eval does not use the binding's __FILE__ and __LINE__ implicitly. Fixes [Bug #4352] Notes: Merged: https://github.com/ruby/ruby/pull/2816
2020-01-02Fully separate positional arguments and keyword argumentsJeremy Evans
This removes the warnings added in 2.7, and changes the behavior so that a final positional hash is not treated as keywords or vice-versa. To handle the arg_setup_block splat case correctly with keyword arguments, we need to check if we are taking a keyword hash. That case didn't have a test, but it affects real-world code, so add a test for it. This removes rb_empty_keyword_given_p() and related code, as that is not needed in Ruby 3. The empty keyword case is the same as the no keyword case in Ruby 3. This changes rb_scan_args to implement keyword argument separation for C functions when the : character is used. For backwards compatibility, it returns a duped hash. This is a bad idea for performance, but not duping the hash breaks at least Enumerator::ArithmeticSequence#inspect. Instead of having RB_PASS_CALLED_KEYWORDS be a number, simplify the code by just making it be rb_keyword_given_p(). Notes: Merged: https://github.com/ruby/ruby/pull/2794
2019-12-23Reword keyword arguments warning messages to convey these are deprecation ↵Marc-Andre Lafortune
warnings
2019-12-18delete rb_vm_call()卜部昌平
Nobody uses it any longer.
2019-12-18per-method serial number卜部昌平
Methods and their definitions can be allocated/deallocated on-the-fly. One pathological situation is when a method is deallocated then another one is allocated immediately after that. Address of those old/new method entries/definitions can be the same then, depending on underlying malloc/free implementation. So pointer comparison is insufficient. We have to check the contents. To do so we introduce def->method_serial, which is an integer unique to that specific method definition. PS: Note that method_serial being uintptr_t rather than rb_serial_t is intentional. This is because rb_serial_t can be bigger than a pointer on a 32bit system (rb_serial_t is at least 64bit). In order to preserve old packing of struct rb_call_cache, rb_serial_t is inappropriate. Notes: Merged: https://github.com/ruby/ruby/pull/2759
2019-12-16ensure cc->def == cc->me->def卜部昌平
The equation shall hold for every call cache. However prior to this changeset cc->me could be updated without also updating cc->def. Let's make it sure by introducing new macro named CC_SET_ME which sets cc->me and cc->def at once.
2019-12-05Make rb_eval_string_wrap specify a cref so constant setting works correctlyJeremy Evans
Fixes [Bug #10466] Notes: Merged: https://github.com/ruby/ruby/pull/2722
2019-11-19make functions static卜部昌平
These functions are used from within a compilation unit so we can make them static, for better binary size. This changeset reduces the size of generated ruby binary from 26,590,128 bytes to 26,584,472 bytes on my macihne. Notes: Merged: https://github.com/ruby/ruby/pull/2682
2019-11-18Deprecate rb_eval_cmd, add rb_eval_cmd_kwJeremy Evans
rb_eval_cmd takes a safe level, and now that $SAFE is deprecated, it should be deprecated as well. Replace with rb_eval_cmd_kw, which takes a keyword flag. Switch the two callers to this function. Notes: Merged: https://github.com/ruby/ruby/pull/2476
2019-11-18Warn on access/modify of $SAFE, and remove effects of modifying $SAFEJeremy Evans
This removes the security features added by $SAFE = 1, and warns for access or modification of $SAFE from Ruby-level, as well as warning when calling all public C functions related to $SAFE. This modifies some internal functions that took a safe level argument to no longer take the argument. rb_require_safe now warns, rb_require_string has been added as a version that takes a VALUE and does not warn. One public C function that still takes a safe level argument and that this doesn't warn for is rb_eval_cmd. We may want to consider adding an alternative method that does not take a safe level argument, and warn for rb_eval_cmd. Notes: Merged: https://github.com/ruby/ruby/pull/2476
2019-11-07extend rb_call_cache卜部昌平
Prior to this changeset, majority of inline cache mishits resulted into the same method entry when rb_callable_method_entry() resolves a method search. Let's not call the function at the first place on such situations. In doing so we extend the struct rb_call_cache from 44 bytes (in case of 64 bit machine) to 64 bytes, and fill the gap with secondary class serial(s). Call cache's class serials now behavies as a LRU cache. Calculating ------------------------------------- ours 2.7 2.6 vm2_poly_same_method 2.339M 1.744M 1.369M i/s - 6.000M times in 2.565086s 3.441329s 4.381386s Comparison: vm2_poly_same_method ours: 2339103.0 i/s 2.7: 1743512.3 i/s - 1.34x slower 2.6: 1369429.8 i/s - 1.71x slower Notes: Merged: https://github.com/ruby/ruby/pull/2583
2019-10-26refactor assign variables卜部昌平
For readability. Requested by ko1. See: https://github.com/ruby/ruby/commit/356e203a3acd4d3d20ba12f956fd22e17b6363e9#r35661401
2019-10-25more on struct rb_call_data卜部昌平
Replacing adjacent struct rb_call_info and struct rb_call_cache into a struct rb_call_data.
2019-10-09vm_eval.c (rb_adjust_argv_kw_splat): avoid memcpy with zero lengthYusuke Endoh
A method call is often with `argc = 1` and `argv = &v` where v is a VALUE, and some functions shift the arguments by `argc-1` and `argv+1` (for example, rb_sym_proc_call). I'm unsure whether it is safe or not to pass a pointer `argv+1` to memcpy with zero length, but Coverity Scan complains it. So this attempts to suppress the warning by explicit check of the length.
2019-10-04Make parser_params have parent_iseq instead of base_blockYusuke Endoh
The parser needs to determine whether a local varaiable is defined or not in outer scope. For the sake, "base_block" field has kept the outer block. However, the whole block was actually unneeded; the parser used only base_block->iseq. So, this change lets parser_params have the iseq directly, instead of the whole block. Notes: Merged: https://github.com/ruby/ruby/pull/2519
2019-09-30Issue keyword flag warning even with no argumentsJeremy Evans
If the keyword flag is set, there should be at least one argument, if there isn't, that is a sign the keyword flag was passed when it should not have been. Notes: Merged: https://github.com/ruby/ruby/pull/2509
2019-09-29Add three more C-API functions for handling keywordsJeremy Evans
This adds rb_funcall_passing_block_kw, rb_funcallv_public_kw, and rb_yield_splat_kw. This functions are necessary to easily handle cases where rb_funcall_passing_block, rb_funcallv_public, and rb_yield_splat are currently used and a keyword argument separation warning is raised. Notes: Merged: https://github.com/ruby/ruby/pull/2507
2019-09-29Remove VM_NO_KEYWORDS, replace with RB_NO_KEYWORDSJeremy Evans
VM_NO_KEYWORDS was introduced first in vm_core.h, but it is best to only use a single definition for this.
2019-09-29Make direct calls to rb_{obj_instance,mod_module}_{eval,exec} not pass keywordsJeremy Evans
In general RB_PASS_CALLED_KEYWORDS should only be set if we are sure the arguments passed come directly from Ruby. For direct calls to these C functions, we should not assume that keywords are passed. Add static *_internal versions of these functions that Kernel#instance_{eval,exec} and Module#{class,module}_{eval,exec} call that set RB_PASS_CALLED_KEYWORDS. Also, change struct.c back to calling rb_mod_module_eval, now that the call is safe. Notes: Merged: https://github.com/ruby/ruby/pull/2505
2019-09-26Fix more keyword separation issuesJeremy Evans
This fixes instance_exec and similar methods. It also fixes Enumerator::Yielder#yield, rb_yield_block, and a couple of cases with Proc#{<<,>>}. This support requires the addition of rb_yield_values_kw, similar to rb_yield_values2, for passing the keyword flag. Unlike earlier attempts at this, this does not modify the rb_block_call_func type or add a separate function type. The functions of type rb_block_call_func are called by Ruby with a separate VM frame, and we can get the keyword flag information from the VM frame flags, so it doesn't need to be passed as a function argument. These changes require the following VM functions accept a keyword flag: * vm_yield_with_cref * vm_yield * vm_yield_with_block Notes: Merged: https://github.com/ruby/ruby/pull/2493
2019-09-23Make public_send and rb_f_send handle keyword argument separationJeremy Evans
Kernel#send takes a different optimized code path that was already handled. Notes: Merged: https://github.com/ruby/ruby/pull/2481
2019-09-20Call rb_vm_call_kw insted of rb_vm_call0 in a few casesJeremy Evans
rb_vm_call_kw handles the tmp buffer for you. Also, change method_missing so it also calls rb_vm_call_kw to handle the kw_splat flag, instead of requiring callers to handle kw_splat flag before calling method_missing. This may fix other cases where method_missing is currently called without the kw_splat being handled.
2019-09-20Handle keyword argument separation for Enumerator#sizeJeremy Evans
When Object#to_enum is passed a block, the block is called to get a size with the arguments given to to_enum. This calls the block with the same keyword flag as to_enum is called with. This requires adding rb_check_funcall_kw and rb_check_funcall_default_kw to handle keyword flags.
2019-09-20Make passing empty keywords to dig pass empty keywords to next dig methodJeremy Evans
If defined in Ruby, dig would be defined as def dig(arg, *rest) end, it would not use keywords. If the last dig argument was an empty hash, it could be treated as keyword arguments by the next dig method. Allow dig to pass along the empty keyword flag if called with an empty keyword, to suppress the previous behavior and force treating the hash as a positional argument and not keywords. Also handle the case where dig calls method_missing, passing the empty keyword flag to that as well. This requires adding rb_check_funcall_with_hook_kw functions, so that dig can specify how arguments are treated. It also adds kw_splat arguments to a couple static functions.
2019-09-19refactor reuse existing on-stack structs卜部昌平
rb_vm_call0 allocates its own struct call_info etc. But they are already there in case of rb_funcallv_with_cc. Let's just pass the existing ones, instead of re-creation.
2019-09-17Fix keyword argument separation issues with sym procs when using refinementsJeremy Evans
Make sure that vm_yield_with_cfunc can correctly set the empty keyword flag by passing 2 as the kw_splat value when calling it in vm_invoke_ifunc_block. Make sure calling.kw_splat is set to 1 and not 128 in vm_sendish, so we can safely check for different kw_splat values. vm_args.c needs to call add_empty_keyword, and to make JIT happy, the function needs to be exported. Rename the function to rb_adjust_argv_kw_splat to more accurately reflect what it does, and mark it as MJIT exported. Notes: Merged: https://github.com/ruby/ruby/pull/2462
2019-09-17Pass keyword argument flag when rb_call_super_kw calls method_missingJeremy Evans
This makes method_missing take a flag for whether keyword arguments were passed. Adds tests both for rb_call_super_kw usage as well as general usage of super calling method_missing in Ruby methods. Notes: Merged: https://github.com/ruby/ruby/pull/2462
2019-09-14Issue a warning if invalid kw_splat is passed to *_kw functionJeremy Evans
This should only happen if the API is misused. It's much better to warn here and fix the problem, versus to try to debug TypeErrors or segfaults later.
2019-09-14Keep the reference of imemo while argv may be usedTakashi Kokubun
To prevent the `v` reference from being eliminated before argv is used, calling `rb_free_tmp_buffer` against `v` explicitly.
2019-09-14Fixed one-off errorNobuyoshi Nakada
Needs another room to append an empty hash.
2019-09-14Fix memory leak when adding empty keyword hashesJeremy Evans
nagachika pointed out that ALLOC_N is actually just malloc, so this memory wasn't being freed. This shouldn't be a performance sensitive code path, and will be going away after 2.7, so just allocate a temp buffer that will be freed later by Ruby GC.
2019-09-14Only set RB_PASS_CALLED_KEYWORDS in C functions called directly from RubyJeremy Evans
It is not safe to set this in C functions that can be called from other C functions, as in the non argument-delegation case, you can end up calling a Ruby method with a flag indicating keywords are set without passing keywords. Introduce some new *_kw functions that take a kw_splat flag and use these functions to set RB_PASS_CALLED_KEYWORDS in places where we know we are delegating methods (e.g. Class#new, Method#call)
2019-09-14* remove trailing spaces. [ci skip]git
2019-09-13Consolidate empty keyword handlingJeremy Evans
Remove rb_add_empty_keyword, and instead of calling that every place you need to add empty keyword hashes, run that code in a single static function in vm_eval.c. Add 4 defines to include/ruby/ruby.h, these are to be used as int kw_splat values when calling the various rb_*_kw functions: RB_NO_KEYWORDS :: Do not pass keywords RB_PASS_KEYWORDS :: Pass final argument (which should be hash) as keywords RB_PASS_EMPTY_KEYWORDS :: Add an empty hash to arguments and pass as keywords RB_PASS_CALLED_KEYWORDS :: Passes same keyword type as current method was called with (for method delegation) rb_empty_keyword_given_p needs to stay. It is required if argument delegation is done but delayed to a later point, which Enumerator does. Use RB_PASS_CALLED_KEYWORDS in rb_call_super to correctly delegate keyword arguments to super method.
2019-09-13Correctly handle keywords for Method#call for cfuncs, send, and attr_*Jeremy Evans
This sets the correct VM frame flags when using Method#call to call funcs, and handles empty keyword hashes for cfuncs, attr_reader, and attr_writer. It also fixes calls to send through Method#call. It adds tests for all of those, as well as tests for using Method#call to call define_method, lambda, and sym_procs (which didn't require code changes).
2019-09-06Fix keyword argument separation warnings for enumeratorsJeremy Evans
This makes objects created via #to_enum and related methods pass keyword arguments as keywords. To implement this, add a kw_splat member of struct enumerator and struct iter_method_arg, and add rb_block_call_kw, which is the same as rb_block_call_kw with a flag for whether the last argument is keyword options. Notes: Merged: https://github.com/ruby/ruby/pull/2432
2019-09-06Convert keyword argument to required positional hash argument for Class#new, ↵Jeremy Evans
Method#call, UnboundMethod#bind_call Also add keyword argument separation warnings for Class#new and Method#call. To allow for keyword argument to required positional hash converstion in cfuncs, add a vm frame flag indicating the cfunc was called with an empty keyword hash (which was removed before calling the cfunc). The cfunc can check this frame flag and add back an empty hash if it is passing its arguments to another Ruby method. Add rb_empty_keyword_given_p function for checking if called with an empty keyword hash, and rb_add_empty_keyword for adding back an empty hash to argv. All of this empty keyword argument support is only for 2.7. It will be removed in 3.0 as Ruby 3 will not convert empty keyword arguments to required positional hash arguments. Comment all of the relevent code to make it obvious this is expected to be removed. Add rb_funcallv_kw as an public C-API function, just like rb_funcallv but with a keyword flag. This is used by rb_obj_call_init (internals of Class#new). This also required expected call_type enum with CALL_FCALL_KW, similar to the recent addition of CALL_PUBLIC_KW. Add rb_vm_call_kw as a internal function, used by call_method_data (internals of Method#call and UnboundMethod#bind_call). Add tests for UnboundMethod#bind_call keyword handling. Notes: Merged: https://github.com/ruby/ruby/pull/2432
2019-09-06doxygen update [ci skip]卜部昌平
2019-09-05Add rb_funcall_with_block_kwJeremy Evans
This is needed for C functions to call methods with keyword arguments. This is a copy of rb_funcall_with_block with an extra argument for the keyword flag. There isn't a clean way to implement this that doesn't involve changing a lot of function signatures, because rb_call doesn't support a way to mark that the call has keyword arguments. So hack this in using a CALL_PUBLIC_KW call_type, which we switch for CALL_PUBLIC later in the call stack. We do need to modify rm_vm_call0 to take an argument for whether keyword arguments are used, since the call_type is no longer available at that point. Use the passed in value to set the appropriate keyword flag in both calling and ci_entry.
2019-09-05Propagate kw_splat informationYusuke Endoh
The kw_splat flag is whether the original call passes keyword or not. Some types of methods (e.g., bmethod and sym_proc) drops the information. This change tries to propagate the flag to the final callee, as far as I can.
2019-09-05hide rb_funcallv_with_cc from public卜部昌平
Requested by ko1. Also, because now that this function is internal use only, why not just directly use struct rb_call_cache to purge the ZALLOC.
2019-09-05use existing vm_search_method()卜部昌平
Ko1 plans to implement Guild. That can interface the caching mechanism introduced here. To prevent future breakage we would better avoid rolling our own code here. Instead use the existing vm_search_method() which would be modified by him. This commit deletes some asserions, but they are in fact checked inside of vm_search_method().
2019-09-04add rb_funcallv_with_cc()Urabe, Shyouhei
Why not cache the method entry at each caller site. The void** is in fact a method entry, but this struct is hidden from ruby.h so intentionally left opaque. Notes: Merged: https://github.com/ruby/ruby/pull/1981
2019-09-03delete ruby_eval_string_from_file_protect卜部昌平
Not used from anywhere.
2019-08-29drop-in type check for rb_define_global_function卜部昌平
We can check the function pointer passed to rb_define_global_function like we do so in rb_define_method. It turns out that almost anybody is misunderstanding the API.
2019-08-27rb_catch now free from ANYARGS卜部昌平
After 5e86b005c0f2ef30df2f9906c7e2f3abefe286a2, I now think ANYARGS is dangerous and should be extinct. This commit deletes ANYARGS from rb_catch, and fixes some bugs revealed by that.