Commit Graph

4698 Commits

Author SHA1 Message Date
dearblue ced89c25ff Unified pc and err of mrb_callinfo
This enhances self-containment.

- Changed the `mrb_callinfo::pc` field to point to itself.
  Previously it indicated the return destination of the previous call level.
  `mrb_callinfo::pc` will now hold the address to its own `proc->body.irep->iseq`.
- Removed `mrb_callinfo::err` field.
  This is because `mrb_callinfo::pc - 1` is semantically the same as the previous `err`.
- The `pc0` and `pc_save` variables in `mrb_vm_exec()` are no longer needed and have been deleted.
- It removes the argument because `cipush()` doesn't need to save the previous `pc`.
2021-01-10 13:23:43 +09:00
dearblue 16baea0677 Changes stackent to stack of mrb_callinfo
This enhances self-containment.

Previously `mrb_context::stack` had the current call level stack, but now it owns it.
The `mrb_context::stack` field, which is no longer needed, will be removed.
2021-01-10 13:23:35 +09:00
dearblue 58e9442737 Unified target_class and env of mrb_callinfo
If there is `env`, `env->c` means `target_class`.
2021-01-10 13:23:28 +09:00
Yukihiro "Matz" Matsumoto f81591ceb6 Detect invalid first byte of UTF-8 char; fix #5269
The first byte of UTF-8 character should not be `80..c1`.
2021-01-09 19:09:57 +09:00
KOBAYASHI Shuji 892bf15cd7 Remove unnecessary #include in generated files 2021-01-09 14:50:23 +09:00
KOBAYASHI Shuji f7ff4810f0 Reapply 116e128b because it is back at 456878ba 2021-01-08 15:23:50 +09:00
KOBAYASHI Shuji b8486d73e2 Consider the case a local variable name does not become a named presym
Non-ASCII characters are allowed to local variable names, so they are not
always named presym.
2021-01-06 23:29:45 +09:00
Yukihiro "Matz" Matsumoto 74c9502bd4 Merge branch 'improve-source-scanning-for-presym' of https://github.com/shuujii/mruby into shuujii-improve-source-scanning-for-presym 2021-01-06 17:06:07 +09:00
Yukihiro "Matz" Matsumoto ca3a6156bf Allow context switch from C using mrb_fiber_resume().
But you still cannot cross C function boundary.
2021-01-06 14:16:56 +09:00
Yukihiro "Matz" Matsumoto 7c9d9b1c89 Merge pull request #5255 from shuujii/avoid-64-bit-operations-in-src-hash.c
Avoid 64-bit operations in `src/hash.c`; close #5201
2021-01-03 20:28:00 +09:00
Katsuyoshi Ito f23918f61a replace ; to : of OPT_SETGV in codedump.c
Maybe it's a typo.
2021-01-03 19:47:30 +09:00
KOBAYASHI Shuji eb9d9e3425 Avoid 64-bit operations in src/hash.c; close #5201
The idea of using `size_t` in `ea_next_capa_for` is by @dearblue.
2021-01-03 18:42:22 +09:00
Yukihiro "Matz" Matsumoto c52efe7be8 Avoid double inclusion on <mruby.h>. 2021-01-02 23:50:18 +09:00
Yukihiro "Matz" Matsumoto 392aff7099 Use Jenkins One At A Time Hash for mrb_str_hash(). 2021-01-02 20:28:29 +09:00
Yukihiro "Matz" Matsumoto b151a697e0 Fixed wrong casting in OP_LOADI32.
Negative integer `>-65535` had wrong value, e,g, `p(-40550)` printed
`4294926746` since Nov. 2020, sigh.
2021-01-02 20:28:28 +09:00
Yukihiro "Matz" Matsumoto c1f05a7fb3 Avoid uint64_t in string-to-integer conversion; ref #5201 2021-01-02 20:28:27 +09:00
Yukihiro "Matz" Matsumoto 4ac73307fd Reduce strength of the hash function; ref #5201
Also avoid using `uint64_t`.
2021-01-02 20:28:27 +09:00
KOBAYASHI Shuji 1a9ac02d8d Refine "wrong number of arguments" message in mrb_get_args
#### Before this patch:

```ruby
__send__         #=> wrong number of arguments
{}.default(1,2)  #=> wrong number of arguments
```

#### After this patch:

```ruby
__send__         #=> wrong number of arguments (given 0, expected 1+)
{}.default(1,2)  #=> wrong number of arguments (given 2, expected 0..1)
```
2021-01-02 19:15:34 +09:00
dearblue ae72feb468 Removed unusable GC.test
The substance of the method was removed in commit 15ceb35e05.
2020-12-31 22:22:45 +09:00
KOBAYASHI Shuji 1fae7c0a69 Refine error message from mrb_get_arg1
#### Before this patch:

```console
$ bin/mruby -e '{}.key?'
trace (most recent call last):
-e:1: wrong number of arguments (ArgumentError)
```

#### After this patch:

```console
$ bin/mruby -e '{}.key?'
trace (most recent call last):
-e:1: wrong number of arguments (given 0, expected 1) (ArgumentError)
```
2020-12-29 19:04:19 +09:00
Yukihiro "Matz" Matsumoto 2850b58dd9 Check integer overflow in float bit operations. 2020-12-24 23:17:18 +09:00
Yukihiro "Matz" Matsumoto 0bb4afe942 Fix the integer overflow in mrb_str_len_to_inum(). 2020-12-23 10:35:15 +09:00
Yukihiro "Matz" Matsumoto c1c8c25e70 Merge pull request #5223 from shuujii/ensure-initialization-of-RVALUE_zero-in-mrb_obj_alloc
Ensure initialization of `RVALUE_zero` in `mrb_obj_alloc`
2020-12-15 22:32:32 +09:00
John Bampton 4fa3359d44 refactor: remove trailing whitespace from C, Header, Ruby and YAML files
Lint
2020-12-15 19:44:02 +10:00
KOBAYASHI Shuji d5cb54d4df Ensure initialization of RVALUE_zero in mrb_obj_alloc
Union initialization initializes the first member. The first member of
`RVALUE` is `struct free_obj`, but because it is only 4-words, it seems that
initialization after the 5th word is not ensured.

Therefore, I created 6-words `struct RVALUE_initializer` for initialization
and made it the first member.
2020-12-15 17:48:06 +09:00
John Bampton 940dec5e7d Fix spelling 2020-12-13 18:38:22 +10:00
KOBAYASHI Shuji 456878ba06 Improve source scanning for presym
The accuracy is greatly improved by using the C preprocessor to scan C
sources for presym. C preprocessor can perfectly interpret all comments and
preprocessor directives, so it can detect all symbols defined, for example
`mrbgems/mruby-socket/src/const.cstub`.

Also, as described later, this change will greatly improve the accuracy of
presym detection from Ruby sources.

## Result

The number of lines in the `presym` file for all gems is as follows:

  ```console
  Previous:   999 (false positive = 89, undetected = 297)
  New:       1207
  ```

## Build process

The new build process (with presym) is as follows:

1. Build `mrbc` without presym (more on building without presym later).
2. Compile Ruby sources to C struct format with the `mrbc` created in
   step 1, and create` mrblib.c` and `gem_init.c`. Note that the symbols
   in the created files are output as `MRB_SYM` family macros or
   `mrb_intern_lit` instead of IDs (details will be described later).
3. C preprocessor processes C sources including the created files of
   step 2 and outputs them as `.i` files. In these files, for example,
   `MRB_IVSYM(foo)` is converted to `<@! "@" "foo" !@>` and
   `mrb_define_module(mrb, "Foo")` is converted to `<@! "Foo" !@>`.
4. Scan the files created in step 3 and create `presym` and` presym.inc`
   files.

The files created in step 2 should output all static symbols defined in Ruby
sources, including local variables, so we can detect all presyms by just
scanning C sources without scanning Ruby sources directly.

Further, by this process, the files to be scanned becomes the same as the
files to be compiled, so that there is no excess or deficiency.

## Related changes

The following changes have been made in relation to realizing this feature.

### Allow build without presym

It enables build without presym to achieve the "Build process: 1". This
incorporates #5202, see its issue for details.

Note that when presym is enabled, even adding a local variable to a Ruby
source may change contents of presym and require recompilation of almost
all C sources. This is inconvenient, especially during trial and error in
development, but this feature is also useful because it does not cause
this problem if presym is disabled.

### Automatically create build target for `mrbc` without presym

The `mrbc` used in the "Build process: 1" will be built by automatically
creating a build target for it. The build name is `SOURCE_BUILD_NAME/mrbc`.

### Constantize output of C struct format by `mrbc`

To realizing the "Build process: 2", as mentioned above, symbol IDs are not
output directly in C struct format output by `mrbc`. As a result, the output
becomes constant regardless of the state of presym at the time of `mrbc`
build, and it is possible to detect symbols of Ruby sources in the same way
as other C sources.

Note that `mrb_intern_lit` is used for symbols that do not become presym,
but in this state, the corresponding element in the symbol array cannot be
statically initialized, so it is initialized at run time (therefore, in this
case, the `const` qualifier is not added to the symbol array).

### Specify arbitrary `mrbc` file

To realizing the "Build process: 2", enabled to specify `mrbc` created by
another build target or pre-built` mrbc`. Use `MRuby::Build#mrbcfile =` to
specify it explicitly. You can omit the "Build process: 1" by specifying
pre-built `mrbc`, and you can always use an optimized build to compile Ruby
sources faster. I think changes that affect the output of `mrbc` are rare,
so in many cases it helps to improve efficiency.

With presym, the build will be a little slower due to more build steps, but
this feature will improve it a bit.

### Create presym files for each build target

This feature was proposed at #5194 and merged once, but was reverted in
5c205e6e due to problems especially with cross-compilation. It has been
introduced again because this change solves the problem.

The presym files will be created below.

* `build/NAME/presym`
* `build/NAME/include/mruby/presym.inc`

### Other changes

* Because presym detection accuracy is greatly improved as mentioned above,
  `MRuby::Gem::Specification#cdump?` is set to true by default, and
  `disable_cdump` is added instead of `enable_cdump`. Also, support for gem
  specific presym files has been discontinued (https://github.com/mruby/mruby/issues/5151#issuecomment-730967232).
* Previously, `mrbc` was automatically created for the `host` build, but it
  will not be created if the build target for `mrbc` mentioned above is
  automatically created. At this time, `mrbc` file of the `mrbc` build is
  copied to` bin/`.
* Two types of `.d` files will be created, `.o.d` and `.i.d`. oThis is
  because if `.i` depends on `presym.inc`, the dependency will circulate, so
  the `.d` file cannot be shared.
* Changed file created with `enable_cxx_exception` to `X-cxx.cxx` from
  `X.cxx` to use the mruby standard Rake rule.

### Note

Almost all C sources will need to be recompiled if there are any changes to
`persym.inc` (if not recompiled properly, it will often result in run-time
error). If `gcc` toolchain is used, dependencies are resolved by the `.d`
file, so it become automatically recompile target, but if not (e.g. MSVC),
it is necessary to manually make it recompile target.

Also, even if `gcc` toolchain is used, it may not become recompile target if
external gems does not use the mruby standard Rake rule. In particular, if
the standard rule is overwritten, such as
https://github.com/mruby/mruby/pull/5112/files, `.d` file will not be read,
so be careful.
2020-12-13 15:27:53 +09:00
Yukihiro "Matz" Matsumoto 116e128b1a Remove positive check for unsigned integer; close #5218 2020-12-12 18:25:47 +09:00
Yukihiro "Matz" Matsumoto ed29d74bfd Make type of pc arguments in debug.c consistent; close #5218
They used to be `size_t`, `uint32_t` and `ptrdiff_t`. Now all of them
made to be `uint32_t`.
2020-12-07 13:59:00 +09:00
Yukihiro "Matz" Matsumoto 00d1fd0e6f Fixed a bug with modules prepended many times.
Adjust insertion point in `fix_prepend_module()`.
2020-12-05 08:01:05 +09:00
Yukihiro "Matz" Matsumoto 3972df57fe Make Module#include and Module#prepend behave like Ruby3.0.
Module#include and Module#prepend now affect classes and modules
that have already included or prepended the receiver, mirroring the
behavior if the arguments were included in the receiver before
the other modules and classes included or prepended the receiver.

```ruby
class C; end
module M1; end
module M2; end
C.include M1
M1.include M2
p C.ancestors #=> [C, M1, M2, Object, Kernel, BasicObject]
```
2020-12-04 22:02:37 +09:00
Yukihiro "Matz" Matsumoto dd0715fd58 Print implicit operands for some instructions. 2020-12-02 09:44:34 +09:00
Yukihiro "Matz" Matsumoto 25985876b8 Remove a newline. 2020-12-02 09:44:07 +09:00
KOBAYASHI Shuji 5c205e6ea1 Revert "Create presym files for each build target" (58ba883e)
Due to the above changes, it may not work with the existing build
configurations in cross-compilation (even if we can build without presym),
therefore revert it once (ref https://github.com/mruby/mruby/pull/5202#issuecomment-735412643). Sorry for the lack of consideration.
2020-12-01 12:16:55 +09:00
Yukihiro "Matz" Matsumoto 98d5aa8d74 Fix OP_JMPUW address bug. 2020-12-01 00:25:09 +09:00
Yukihiro "Matz" Matsumoto 1c361f8a22 Merge pull request #5206 from dearblue/dumpirep
Improves dump irep
2020-11-30 23:21:19 +09:00
dearblue 3bc9453c9a Integrate the output of the catch handler table into iseq
Alleviates confusingness.

ref #5203
2020-11-30 22:56:59 +09:00
dearblue a56b601f56 Fixed irep size measurement by dump; ref #5203 2020-11-30 22:56:41 +09:00
dearblue 0ecb0fd97b Fixed print catch handler address in codedump; ref #5200
It became 32 bits in #5200, but only the upper 16 bits were printed.
2020-11-30 22:36:26 +09:00
KOBAYASHI Shuji 82f3e4d31b Use MRB_SYM in src/gc.c 2020-11-29 19:01:48 +09:00
Yukihiro "Matz" Matsumoto d4439479cf Merge pull request #5203 from komainu8/fix-heap-buffer-overflow
Fix heap buffer overflow when dump irep
2020-11-29 16:24:30 +09:00
Horimoto Yasuhiro df13d418c3 Fix heap buffer overflow when dump irep
Currently, the size of writing in heap by write_irep_record() is
bigger than The size that is calculated by get_irep_record_size.

Therefore, irep is dumped over the size of allocating memory when we
execute dump_irep().
2020-11-29 15:57:44 +09:00
dearblue b0cea30f32 Change the catch handler address to 32 bits
Follow commit 7150c67539 .
2020-11-29 13:48:45 +09:00
KOBAYASHI Shuji 58ba883ee9 Create presym files for each build target
Previously, presym files were always created in `build/{presym,presym.inc}`.
However, this constraint is inconvenient because it is common to use
multiple build configurations and build targets in a single mruby tree.
Therefore, change to create presym file for each build target.
2020-11-26 19:05:07 +09:00
Yukihiro "Matz" Matsumoto 63d4e8eca2 Fix a bug in find_symbol(); fix #5192 2020-11-26 13:18:45 +09:00
Yukihiro "Matz" Matsumoto 7150c67539 Make OP_JMP* operand address to be relative.
Jump target address is `operand (16bit)` + `address of next instruction`.

In addition, `ilen` was made `uint32_t` so that `iseq` length limitation
of 65536 is removed. Only jump target address should be within signed
16bit (-32768 .. 32767).
2020-11-26 10:34:31 +09:00
Yukihiro "Matz" Matsumoto a33f9d7b65 Small refactoring regarding symbols for clarity. 2020-11-26 09:42:08 +09:00
Yukihiro "Matz" Matsumoto 50b57f6ff1 Include mruby.h instead of mrbconf.h directly. 2020-11-26 09:14:12 +09:00
Yukihiro "Matz" Matsumoto 2daf1987f9 Avoid memory leak when mrb_read_irep() fails. 2020-11-26 09:04:30 +09:00
Yukihiro "Matz" Matsumoto 71f9add1c5 Move inline iseq in array.c to array.rb.
There's no efficiency difference since `cdump` is implemented.
2020-11-25 23:14:38 +09:00