Commit Graph

5973 Commits

Author SHA1 Message Date
Hoshiumi Arata 52e1ebe191 mruby-bigint: handle rhs bigint in int_mod and int_divmod functions 2024-12-11 20:58:03 +09:00
dearblue 95e2f8e844 Fixed wrong range condition in OP_JMPUW
fixed #6441
2024-12-03 22:14:50 +09:00
Yukihiro "Matz" Matsumoto 4a99f28ec3 Revert "vm.c (cipush): small refactoring"
This reverts commit d6e23f3cdc.
Caused some memory crashes.
2024-12-03 16:48:48 +09:00
Yukihiro "Matz" Matsumoto 77e08c9193 ops.h: rename OP_LOADI to OP_LOADI8
OP_LOADI stores an 8 bit integer to a register, so we renamed the
instruction name to describe the behavior more precisely, like
OP_LOADI16 and OP_LOADI32.
2024-12-01 19:16:54 +09:00
Yukihiro "Matz" Matsumoto d6e23f3cdc vm.c (cipush): small refactoring
- simplified if-statement
- rename local variable size to diff (means diff between cibase & ci).
2024-11-30 11:13:38 +09:00
Yukihiro "Matz" Matsumoto 5b69185171 dump.c (mrb_dump_irep): skip lv section if MRB_DUMP_NO_LVAR is set 2024-11-29 16:29:18 +09:00
Yukihiro "Matz" Matsumoto 673d71ffca dump.c (mrb_dump_irep): reorganize if-structure 2024-11-28 10:14:06 +09:00
Yukihiro "Matz" Matsumoto 8a25c424a4 dump.c: remove unnecessary braces around return 2024-11-28 04:54:20 +09:00
Yukihiro "Matz" Matsumoto b1091e7c64 dump.c: use post-decrements 2024-11-28 04:54:19 +09:00
dearblue fd97241cd6 Moving code in macro arguments out of macros
In `src/hash.c`, there are code blocks that are passed as macro arguments.
These code blocks are interpreted as part of the macro function, so breakpoints cannot be set in the debugger.
Also, the gcov command will aggregate them to the caller, and the code in the block will not be counted.

This patch will prevent them from being interpreted as part of a macro, and thus the aforementioned problems will no longer occur.
2024-11-21 22:52:29 +09:00
Yukihiro "Matz" Matsumoto 4a54b551e8 hash.c (ht_set): inline ht_set_without_ib_adjustment() 2024-11-21 07:59:11 +09:00
Yukihiro "Matz" Matsumoto 33357d4f35 hash.c (ht_set_without_ib_adjustment): need to skip tombstone; #6421
If a tombstone (a deleted entry slot) is found in searching the entry,
it should be skipped, but we had added the new entry even if the entry
to be replaced might be found in the further search. #6414 and #6421
tried to rehash the table to remove tombstone. But rehashing consumes
memory. So for the time being, we just skip tombstones in the search.
Maybe we will add some heuristics to rehash when the table has too many
tombstones. close #6414
2024-11-20 10:52:39 +09:00
dearblue 0337e0e0f1 Need to update ci variable after re-entry to VM
Some functions called by `mrb_vm_exec()` involve re-entry into the mruby VM.
If the `ci` variable is not updated after re-entry, use-after-free is caused.

This patch makes the following after-call fixes.

| called                  | might call methods
| ----------------------- | ----------------
| `mrb_ary_splat()`       | `#to_a`
| `hash_new_from_regs()`  | `#eql?` `#hash`
| `mrb_hash_delete_key()` | `#eql?` `#hash`
| `mrb_hash_get()`        | `#eql?` `#hash` `#default`
| `mrb_hash_key_p()`      | `#eql?` `#hash`
| `mrb_hash_merge()`      | `#eql?` `#hash`
| `mrb_hash_set()`        | `#eql?` `#hash`
| `mrb_range_new()`       | `#<=>`
2024-11-19 22:47:04 +09:00
dearblue 9501c3f58c Add annotations for function names defined in the preprocessor 2024-11-16 20:50:49 +09:00
Yukihiro "Matz" Matsumoto a247f48cf6 Merge pull request #6416 from hoshiumiarata/optimize_class_new_iseq 2024-11-15 15:42:57 +09:00
Yukihiro "Matz" Matsumoto ab3bc12fcb class.c: remove 'class' prefix from error messages 2024-11-15 15:33:14 +09:00
Yukihiro "Matz" Matsumoto a109599d53 vm.c (prepare_missing): add receiver's class in superclass calls 2024-11-15 15:15:08 +09:00
Yukihiro "Matz" Matsumoto 65e0e5bbd3 class.c (mrb_method_missing): add receiver's class to clarify
We needed to modify a lot of test code that expected the old style.
2024-11-15 15:13:24 +09:00
Hoshiumi Arata 0b1d55a088 class.c: replace OP_SEND with OP_SSEND in "new" method's iseq 2024-11-13 18:34:16 +09:00
Yukihiro "Matz" Matsumoto 5387b74be7 hash.c (mrb_hash_to_s): put spaces around =>
CRuby 3.4 puts spaces around `=>` since for example `{:a!=>2}` can be
confusing where to separate tokens.  mruby should follow the behavior.

Many tests in `test/t` directory assumed no spaces around `=>`, so we
needed to fix them too.
2024-11-11 11:07:43 +09:00
dearblue c654123561 Distinguish the call frame of the generator with OP_RETURN_BLK
When multiple identical proc objects are placed on the call stack, it is not possible to distinguish where to `return`.
Therefore, use env object comparisons to do this.

fixed #6411
2024-11-10 18:39:00 +09:00
dearblue a92b97ff45 Improve compliance with C++ standards
- Avoid "Designated initializer"
    C++20 is required for C++.
  - Avoid "Compound literals"
    Not available for C++.
2024-11-05 22:07:53 +09:00
Yukihiro "Matz" Matsumoto 49525fa207 mempool.c: renamed from pool.c
To avoid confusion with pools in irep, we renamed region-based memory
manager from pool to mempool.

- rename pool.c to mempool.c
- separate mempool.h
- rename all mrb_pool to mrb_mempool

So if someone is using pool.c functions (I suppose no one does though),
they need to rename all `mrb_pool` to `mrb_mempool` and include
`mruby/mempool.h` header at the top.
2024-10-31 14:06:10 +09:00
Yukihiro "Matz" Matsumoto 62ef5db13e irep.h: rename mrb_pool_value to mrb_irep_pool
mrb_pool_value is a structure that represents a value in the irep
literal pool and is unrelated to mrb_pool, which performs region-based
memory management. It has been renamed mrb_irep_pool to avoid confusion.
2024-10-29 20:34:53 +09:00
dearblue 94290b9bb8 Follow-up to #6391
The #6392 has been merged, so the call to `mrb_yield_with_class()` is no longer needed.
2024-10-25 21:11:07 +09:00
Yukihiro "Matz" Matsumoto ba69f36d6d Merge pull request #6392 from dearblue/mrb_exec_irep 2024-10-25 07:12:05 +09:00
dearblue 8d64788cbf Fix argument forwarding in mrb_exec_irep()
C to Ruby calls using `mrb_exec_irep()` were not forwarding arguments.

There was also a problem in setting the target class and method ID, which is also fixed.

This issue was discovered during the work to fix #6389.
2024-10-24 22:33:18 +09:00
dearblue f9151e0bd9 Fixed argument forwarding in instance_exec
However, on C, there is no easy way to pass keyword arguments.
Therefore, when called `Kernel#instance_exec` on C, keyword arguments are converted to positional arguments.
This is a limitation of current mruby.

fixed #6389
2024-10-24 21:20:46 +09:00
dearblue be9431a9ef Fix use-after-free by mrb_gc_unregistor()
Calling `mrb_gc_unregistor()` from `mrb_data_type::dfree` caused a use-after-free deep inside `mrb_close()`.
The impetus to investigate was <https://github.com/mruby/mruby/pull/6342#pullrequestreview-2292747530>.

Currently, when `mrb_close()` is called, all objects are destroyed first.
The process is done heap page by heap page, and when all objects belonging to a heap page are destroyed, the heap page is released.
If the next heap page contains `RData` objects, the `mrb_gc_unregistor()` function may be called from the `mrb_data_type::dfree` function.
At this time, the `mrb_gc_unregistor()` function gets an array object from a Ruby global variable.
If the array object belongs to a freed heap page, use-after-free is established by referencing this array object.

About the fixes.

First of all, there is the fact that the `mrb_gv_get()` function returns `nil` if `mrb->globals` is `NULL`.
Therefore, before destroying all objects, free `mrb->globals` and set `mrb->globals` to `NULL` at the same time.
Now the `mrb_gv_get()` function will return `nil` to the calling `mrb_gc_unregistor()` function and `mrb_gc_unregistor()` will do nothing more.

ref. https://github.com/mruby/mruby/issues/4618
2024-10-22 22:57:49 +09:00
Yukihiro "Matz" Matsumoto f2908031c4 Merge pull request #6387 from dearblue/gc-unregister.1 2024-10-21 10:56:49 +09:00
Yukihiro "Matz" Matsumoto f6fbfdfcf0 Merge pull request #6386 from dearblue/gc-unregister 2024-10-21 10:55:38 +09:00
Yukihiro "Matz" Matsumoto df0fc6f3a8 Merge pull request #6385 from dearblue/gc-register.1 2024-10-21 10:49:37 +09:00
Yukihiro "Matz" Matsumoto c317659b8f Merge pull request #6382 from dearblue/gc-register 2024-10-21 10:48:46 +09:00
Yukihiro "Matz" Matsumoto 1f5ba7e01d vm.c (OP_STRCAT): mrb_str_concat may call VM recursively
And recursive calls may reallocate call-info stack.
2024-10-20 19:46:23 +09:00
dearblue 2ec2437d23 Small improvements for mrb_gc_unregister()
`ARY_PTR()` and `ARY_LEN()` avoid using them in a loop if the array is not changed, since they involve branching.
2024-10-19 11:10:10 +09:00
dearblue 1dcc2cac03 Avoid calling mrb_gv_set() from mrb_gc_unregister()
Should simply be ignored for the possibility of being called from `RData::dfree`.
2024-10-19 11:08:32 +09:00
dearblue b28d53bda9 Small improvements for mrb_gc_register()
The `mrb_nil_p()` used in conditional expressions is semantically included in `!mrb_array_p()`.
2024-10-19 10:59:21 +09:00
dearblue 13512aba91 Make array objects invisible in mrb_gc_register()
Once the class is set, objects can be referenced and manipulated from the Ruby side by using `ObjectSpace.each_object`.
Also, currently `mrb_gc_unregister()` assumes that the element is a non-immediate object.
However, `mrb_gc_unregister()` does not read or write to the address, so there was no problem.
2024-10-18 22:29:23 +09:00
Yukihiro "Matz" Matsumoto 84d17f2c44 array.c (mrb_ary_clear): remove unnecessary function 2024-10-16 16:33:42 +09:00
Yukihiro "Matz" Matsumoto 8cfc1a4581 variable.c: fix int and mrb_bool confusion 2024-10-15 21:10:10 +09:00
Yukihiro "Matz" Matsumoto 540bd54ed3 variable.c (iv_rehash): name magic number 4 as IV_INITIAL_SIZE 2024-10-15 21:10:10 +09:00
Yukihiro "Matz" Matsumoto fa68e634a7 variable.c (mrb_exc_const_get): E_XXX_ERROR should not call const_missing
As #6359 pointed out, calling const_missing hook from E_XXX_ERROR (that
calls mrb_exc_get_id()) can be an attack vector.  Since E_XXX_ERROR is
supposed to be a defined error class, we think that the situation where
it is undefined and the const_missing hook is called should be detected
as an error; fix #6359
2024-10-15 21:09:48 +09:00
Yukihiro "Matz" Matsumoto 32a527ad6d readfloat.c: fix float to int assignment 2024-10-11 08:45:27 +09:00
Yukihiro "Matz" Matsumoto 1348daa683 vm.c: save offset in a local variable in OP_BLKPUSH 2024-09-27 16:31:13 +09:00
Yukihiro "Matz" Matsumoto b94be7de5a gc.c (mark_context): no need to mark ci->blk; ref #5791
Since the passed block is referenced from the stack after the last
commit.
2024-09-26 02:57:57 +09:00
Yukihiro "Matz" Matsumoto 204928120b vm.c (cipop): fix improper code to check if env is reclaimed; fix #6369
The callinfo refers blk since #5786 but not marked at the time. Later we
added reclamation check by #5791 but its repeated heap scans decrease
the performance drastically in some cases. So the original @dearblue's
solution should be taken

Probably we need to always keep the original block at the bottom of
arguments. And the explicit block argument should be a normal local
variable. We will investigate it later.
2024-09-25 14:12:50 +09:00
John Bampton e12da35645 Fix spelling 2024-09-24 00:14:24 +10:00
Yukihiro "Matz" Matsumoto 17acf23914 readfloat.c: we don't need long double for mruby; fix #6365
`mrb_read_float()` function does not raise ERANGE (unlike `strtod()`)
so we don't need high precision double for calculation.
2024-09-23 08:11:23 +09:00
Yukihiro "Matz" Matsumoto bd668e4c9d readfloat.c: new implementation of mrb_read_float(); ref #6187
The new version gives more accurate values for decimal number
representation that are not divisible in binary representations, for
example `0.3`.

The function uses `long double` for precision. Please report if `long
double` causes problems on any platform (especially microcontrollers).
Ref #6182
2024-09-21 02:47:42 +09:00
dearblue cf23861166 Making splat argument objects invisible from Ruby side
The `mrb_get_argv()` function and the `*` specifier of `mrb_get_args()` get the address of the argument.
At this time, if it is passed in the form of a splat argument, it will be an address to an element of an array object.

After getting the pointer to the array object, the caller may call `mrb_vm_exec()` directly or indirectly.
At this time, a splat argument with the class set can be retrieved as an array object by searching with `ObjectSpace.each_object`.
If changes are made as array objects, addresses on the heap as arrays may become invalid, or objects in the array may be recycled by the GC.
When the caller references the changed address in a subsequent operation, use-after-free is established.

This patch assigns `NULL` as the class of the array object so that it cannot be detected by `ObjectSpace.each_object` from the Ruby side.
2024-09-19 23:13:18 +09:00