summaryrefslogtreecommitdiff
path: root/internal.h
AgeCommit message (Collapse)Author
2019-12-03Fixed stack overflow [Bug #16382]Nobuyoshi Nakada
Get rid of infinite recursion in expanding a load path to the real path while loading a transcoder. Notes: Merged: https://github.com/ruby/ruby/pull/2714
2019-11-28Make prepending a refined module after inclusion not break refinementsJeremy Evans
After the previous commit, this was still broken. The reason it was broken is that a refined module that hasn't been prepended to yet keeps the refined methods in the module's method table. When prepending, the module's method table is moved to the origin iclass, and then the refined methods are moved from the method table to a new method table in the module itself. Unfortunately, that means that if a class has included the module, prepending breaks the refinements, because when the methods are moved from the origin iclass method table to the module method table, they are removed from the method table from the iclass created when the module was included earlier. Fix this by always creating an origin class when including a module that has any refinements, even if the refinements are not currently used. I wasn't sure the best way to do that. The approach I choose was to use an object flag. The flag is set on the module when Module#refine is called, and if the flag is present when the module is included in another module or class, an origin iclass is created for the module. Fixes [Bug #13446] Notes: Merged: https://github.com/ruby/ruby/pull/2550
2019-11-27prefer class_serial over m_tbl卜部昌平
Decades ago, among all the data that a class has, its method table was no doubt the most frequently accessed data. Previous data structures were based on that assumption. Today that is no longer true. The most frequently accessed field moved to class_serial. That field is not always as wide as VALUE but if it is, let us swap m_tbl and class_serial. Calculating ------------------------------------- ours trunk Optcarrot Lan_Master.nes 47.363 46.630 fps Comparison: Optcarrot Lan_Master.nes ours: 47.4 fps trunk: 46.6 fps - 1.02x slower Notes: Merged: https://github.com/ruby/ruby/pull/2701
2019-11-22internal.h: Add a prototype declaration for rb_gvar_readonly_setterYusuke Endoh
emscripten fails to compile ruby due to lack of this.
2019-11-20re-add function prototypes卜部昌平
This commit reverts a part of 0e8219f591f3f17cb7ee361e8a60dbef08145883 which broke icc. See also https://rubyci.org/logs/rubyci.s3.amazonaws.com/icc-x64/ruby-master/log/20191120T010006Z.fail.html.gz
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-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-14delete unused functions卜部昌平
Looking at the list of symbols inside of libruby-static.a, I found hundreds of functions that are defined, but used from nowhere. There can be reasons for each of them (e.g. some functions are specific to some platform, some are useful when debugging, etc). However it seems the functions deleted here exist for no reason. This changeset reduces the size of ruby binary from 26,671,456 bytes to 26,592,864 bytes on my machine. Notes: Merged: https://github.com/ruby/ruby/pull/2677
2019-11-08delete cc->aux.inc_sp卜部昌平
Nobody uses it.
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-11-05let the .bss section initialize static variables卜部昌平
ISO/IEC 9899:1999 section 6.7.8 specifies the values of static storage which are not explicitly initialized. According to that these initializers can be omitted. Doing so improvoes future compatibility against addition / deletion of the fields of this struct.
2019-11-05rb_method_basic_definition_p with CC卜部昌平
Noticed that rb_method_basic_definition_p is frequently called. Its callers include vm_caller_setup_args_block(), rb_hash_default_value(), rb_num_neative_int_p(), and a lot more. It seems worth caching the method resolution part. Majority of rb_method_basic_definion_p() usages take fixed class and fixed method id combinations. Calculating ------------------------------------- ours trunk so_matrix 2.379 2.115 i/s - 1.000 times in 0.420409s 0.472879s Comparison: so_matrix ours: 2.4 i/s trunk: 2.1 i/s - 1.12x slower Notes: Merged: https://github.com/ruby/ruby/pull/2629
2019-11-01mark functions that do not return NULL as such.卜部昌平
Apply __attribute__((__returns_nonnull__)) when available.
2019-11-01ruby_mimmalloc can return NULL卜部昌平
malloc can fail. Should treat such situations.
2019-10-29Check for nonnull attribute in configureNobuyoshi Nakada
2019-10-29Revert "Check for nonnull attribute in configure"卜部昌平
This reverts commit 54eb51d72bc43f90b595f0d7ffb5069ebf1a56d9. Windows build failure. See also https://github.com/ruby/ruby/runs/278718805
2019-10-29Check for nonnull attribute in configureNobuyoshi Nakada
2019-10-29Clang can also use C call cacheJohn Hawthorn
Previously this was restricted to only gcc because of the GCC_VERSION_SINCE check (which explicitly excludes clang). GCC 3.3.0 is quite old so I feel relatively safe assuming that all reasonable versions of clang support this. Notes: Merged: https://github.com/ruby/ruby/pull/2628
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-10make rb_raise a GVL-only function again卜部昌平
Requested by ko1 that ability of calling rb_raise from anywhere outside of GVL is "too much". Give up that part, move the GVL aquisition routine into gc.c, and make our new gc_raise().
2019-10-09avoid overflow in integer multiplication卜部昌平
This changeset basically replaces `ruby_xmalloc(x * y)` into `ruby_xmalloc2(x, y)`. Some convenient functions are also provided for instance `rb_xmalloc_mul_add(x, y, z)` which allocates x * y + z byes. Notes: Merged: https://github.com/ruby/ruby/pull/2540
2019-10-09annotate malloc-ish functions卜部昌平
Make them gcc friendly. Note that realloc canot be __malloc__ attributed, according to the GCC manual. Notes: Merged: https://github.com/ruby/ruby/pull/2540
2019-10-09fix arity mismatch卜部昌平
Seems nobody has actually used this macro. Such an obvious typo. Notes: Merged: https://github.com/ruby/ruby/pull/2540
2019-10-05Fix potential memory leaks by `rb_imemo_tmpbuf_auto_free_pointer`Nobuyoshi Nakada
This function has been used wrongly always at first, "allocate a buffer then wrap it with tmpbuf". This order can cause a memory leak, as tmpbuf creation also can raise a NoMemoryError exception. The right order is "create a tmpbuf then allocate&wrap a buffer". So the argument of this function is rather harmful than just useless. TODO: * Rename this function to more proper name, as it is not used "temporary" (function local) purpose. * Allocate and wrap at once safely, like `ALLOCV`.
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-10-03Revert https://github.com/ruby/ruby/pull/2486卜部昌平
This reverts commits: 10d6a3aca7 8ba48c1b85 fba8627dc1 dd883de5ba 6c6a25feca 167e6b48f1 7cb96d41a5 3207979278 595b3c4fdd 1521f7cf89 c11c5e69ac cf33608203 3632a812c0 f56506be0d 86427a3219 . The reason for the revert is that we observe ABA problem around inline method cache. When a cache misshits, we search for a method entry. And if the entry is identical to what was cached before, we reuse the cache. But the commits we are reverting here introduced situations where a method entry is freed, then the identical memory region is used for another method entry. An inline method cache cannot detect that ABA. Here is a code that reproduce such situation: ```ruby require 'prime' class << Integer alias org_sqrt sqrt def sqrt(n) raise end GC.stress = true Prime.each(7*37){} rescue nil # <- Here we populate CC class << Object.new; end # These adjacent remove-then-alias maneuver # frees a method entry, then immediately # reuses it for another. remove_method :sqrt alias sqrt org_sqrt end Prime.each(7*37).to_a # <- SEGV ```
2019-09-30delete unnecessary branch卜部昌平
At last, not only myself but also your compiler are fully confident that the method entries pointed from call caches are immutable. We don't have to worry about silent updates. Just delete the branch that is now always false. Calculating ------------------------------------- ours trunk vm2_poly_same_method 2.142M 2.070M i/s - 6.000M times in 2.801148s 2.898994s Comparison: vm2_poly_same_method ours: 2141979.2 i/s trunk: 2069683.8 i/s - 1.03x slower Notes: Merged: https://github.com/ruby/ruby/pull/2486
2019-09-26Add rb_adjust_argv_kw_splat to internal.hJeremy Evans
We are calling this in a few other files, it is better to have it in a header than adding prototypes to the other files. Notes: Merged: https://github.com/ruby/ruby/pull/2491
2019-09-25Add Module#ruby2_keywords for passing keywords through regular argument splatsJeremy Evans
This approach uses a flag bit on the final hash object in the regular splat, as opposed to a previous approach that used a VM frame flag. The hash flag approach is less invasive, and handles some cases that the VM frame flag approach does not, such as saving the argument splat array and splatting it later: ruby2_keywords def foo(*args) @args = args bar end def bar baz(*@args) end def baz(*args, **kw) [args, kw] end foo(a:1) #=> [[], {a: 1}] foo({a: 1}, **{}) #=> [[{a: 1}], {}] foo({a: 1}) #=> 2.7: [[], {a: 1}] # and warning foo({a: 1}) #=> 3.0: [[{a: 1}], {}] It doesn't handle some cases that the VM frame flag handles, such as when the final hash object is replaced using Hash#merge, but those cases are probably less common and are unlikely to properly support keyword argument separation. Use ruby2_keywords to handle argument delegation in the delegate library. Notes: Merged: https://github.com/ruby/ruby/pull/2477
2019-09-25Make rb_scan_args handle keywords more similar to Ruby methods (#2460)Jeremy Evans
Cfuncs that use rb_scan_args with the : entry suffer similar keyword argument separation issues that Ruby methods suffer if the cfuncs accept optional or variable arguments. This makes the following changes to : handling. * Treats as **kw, prompting keyword argument separation warnings if called with a positional hash. * Do not look for an option hash if empty keywords are provided. For backwards compatibility, treat an empty keyword splat as a empty mandatory positional hash argument, but emit a a warning, as this behavior will be removed in Ruby 3. The argument number check needs to be moved lower so it can correctly handle an empty positional argument being added. * If the last argument is nil and it is necessary to treat it as an option hash in order to make sure all arguments are processed, continue to treat the last argument as the option hash. Emit a warning in this case, as this behavior will be removed in Ruby 3. * If splitting the keyword hash into two hashes, issue a warning, as we will not be splitting hashes in Ruby 3. * If the keyword argument is required to fill a mandatory positional argument, continue to do so, but emit a warning as this behavior will be going away in Ruby 3. * If keyword arguments are provided and the last argument is not a hash, that indicates something wrong. This can happen if a cfunc is calling rb_scan_args multiple times, and providing arguments that were not passed to it from Ruby. Callers need to switch to the new rb_scan_args_kw function, which allows passing of whether keywords were provided. This commit fixes all warnings caused by the changes above. It switches some function calls to *_kw versions with appropriate kw_splat flags. If delegating arguments, RB_PASS_CALLED_KEYWORDS is used. If creating new arguments, RB_PASS_KEYWORDS is used if the last argument is a hash to be treated as keywords. In open_key_args in io.c, use rb_scan_args_kw. In this case, the arguments provided come from another C function, not Ruby. The last argument may or may not be a hash, so we can't set keyword argument mode. However, if it is a hash, we don't want to warn when treating it as keywords. In Ruby files, make sure to appropriately use keyword splats or literal keywords when calling Cfuncs that now issue keyword argument separation warnings through rb_scan_args. Also, make sure not to pass nil in place of an option hash. Work around Kernel#warn warnings due to problems in the Rubygems override of the method. There is an open pull request to fix these issues in Rubygems, but part of the Rubygems tests for their override fail on ruby-head due to rb_scan_args not recognizing empty keyword splats, which this commit fixes. Implementation wise, adding rb_scan_args_kw is kind of a pain, because rb_scan_args takes a variable number of arguments. In order to not duplicate all the code, the function internals need to be split into two functions taking a va_list, and to avoid passing in a ton of arguments, a single struct argument is used to handle the variables previously local to the function. Notes: Merged-By: jeremyevans <code@jeremyevans.net>
2019-09-22variable.c: Rename rb_st_copy to rb_iv_tbl_copyYusuke Endoh
This function was created as a variant of st_copy with firing write barrier. It should have more explicit name, such as st_copy_with_write_barrier. But because it is used only for copying iv_tbl, so I rename it to rb_iv_tbl_copy now. If we face other use case than iv_tbl, we may want to rename it to more general name.
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-19reuse cc->call卜部昌平
I noticed that in case of cache misshit, re-calculated cc->me can be the same method entry than the pevious one. That is an okay situation but can't we partially reuse the cache, because cc->call should still be valid then? One thing that has to be special-cased is when the method entry gets amended by some refinements. That happens behind-the-scene of call cache mechanism. We have to check if cc->me->def points to the previously saved one. Calculating ------------------------------------- trunk ours vm2_poly_same_method 1.534M 2.025M i/s - 6.000M times in 3.910203s 2.962752s Comparison: vm2_poly_same_method ours: 2025143.9 i/s trunk: 1534447.2 i/s - 1.32x slower Notes: Merged: https://github.com/ruby/ruby/pull/2468
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-10Fixed GCC version for diagnostic-pragmasNobuyoshi Nakada
"GCC diagnostic push/pop" seems appeared at gcc 4.6.
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-05Make Symbol#to_proc calls handle keyword argumentsJeremy Evans
Make rb_sym_proc_call take a flag for whether a keyword argument is used, and use the new rb_funcall_with_block_kw function to pass that information.
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-08-30Set symbol export for rb_hash_stlike_foreachJeremy Evans
This fixes MJIT after rb_hash_stlike_foreach used vm_args.c. Notes: Merged: https://github.com/ruby/ruby/pull/2395
2019-08-30Allow ** syntax to be used for calling methods that do not accept keywordsJeremy Evans
Treat the ** syntax as passing a copy of the hash as the last positional argument. If the hash being double splatted is empty, do not add a positional argument. Remove rb_no_keyword_hash, no longer needed. Notes: Merged: https://github.com/ruby/ruby/pull/2395
2019-08-30Separate keyword arguments from positional argumentsYusuke Endoh
And, allow non-symbol keys as a keyword arugment Notes: Merged: https://github.com/ruby/ruby/pull/2395
2019-08-27rb_uninterruptible now free from ANYARGS卜部昌平
After 5e86b005c0f2ef30df2f9906c7e2f3abefe286a2, I now think ANYARGS is dangerous and should be extinct. This function has only one call site so adding appropriate prototype is trivial.
2019-08-27struct MEMO now free from ANYARGS卜部昌平
After 5e86b005c0f2ef30df2f9906c7e2f3abefe286a2, I now think ANYARGS is dangerous and should be extinct. There is only one usage of MEMO::u3::func in load.c (where void Init_Foobar(vodi) is registered) so why not just be explicit.
2019-08-27rb_hash_foreach now free from ANYARGS卜部昌平
After 5e86b005c0f2ef30df2f9906c7e2f3abefe286a2, I now think ANYARGS is dangerous and should be extinct. This commit adds function prototypes for rb_hash_foreach / st_foreach_safe. Also fixes some prototype mismatches.
2019-08-27rb_ensure now free from ANYARGS卜部昌平
After 5e86b005c0f2ef30df2f9906c7e2f3abefe286a2, I now think ANYARGS is dangerous and should be extinct. This commit deletes ANYARGS from rb_ensure, which also revealed many arity / type mismatches.
2019-08-27decouple compile.c usage of imemo_ifunc卜部昌平
After 5e86b005c0f2ef30df2f9906c7e2f3abefe286a2, I now think ANYARGS is dangerous and should be extinct. This commit deletes ANYARGS from struct vm_ifunc, but in doing so we also have to decouple the usage of this struct in compile.c, which (I think) is an abuse of ANYARGS.
2019-08-27delete rb_method_defined_by declaration.卜部昌平
Ko1 missed this in d5893b91faa7dc77ca6c9728d1054dabd757aead.
2019-08-21`rp(obj)` shows func, file and line. (#2394)Koichi Sasada
rp() macro for debug also shows file location and function name such as: [OBJ_INFO:rb_call_inits@inits.c:73] 0x000056147741b248 ... Notes: Merged-By: ko1
2019-08-13Renamed ruby_finalize_{0,1}Nobuyoshi Nakada
And pass rb_execution_context_t as an argument.