Commit Graph

1484 Commits

Author SHA1 Message Date
Yukihiro "Matz" Matsumoto ff366b65eb Merge pull request #5415 from dearblue/unwind-mrb_protect
Introducing the `mrb_protect_raw()` API function
2021-04-19 22:34:20 +09:00
dearblue 891e852286 Introducing the mrb_protect_raw() API function
The purpose is two-fold:

 1. to be able to specify a pointer directly when user data is used

    When using `mrb_protect()`, it is necessary to allocate objects by `mrb_obj_cptr()` function when using user data.

    Adding `mrb_protect_raw()` will make it simpler to reimplement `mrbgems/mruby-error`.

 2. to correctly unwind callinfo when an exception is raised from a C function defined as a method (the main topic)

    If a method call is made directly under `mrb_protect()` and a C function is called, control is returned from `mrb_protect()` if an exception occurs there.
    In this case, callinfo is not restored, so it is out of sync.
    Moreover, returning to mruby VM (`mrb_vm_exec()` function) in this state will indicate `ci->pc` of C function which is equal to `NULL`, and subsequent `JUMP` will cause `SIGSEGV`.

    Following is an example that actually causes `SIGSEGV`:

    - `crash.c`

      ```c
      #include <mruby.h>
      #include <mruby/compile.h>
      #include <mruby/error.h>

      static mrb_value
      level1_body(mrb_state *mrb, mrb_value self)
      {
        return mrb_funcall(mrb, self, "level2", 0);
      }

      static mrb_value
      level1(mrb_state *mrb, mrb_value self)
      {
        return mrb_protect(mrb, level1_body, self, NULL);
      }

      static mrb_value
      level2(mrb_state *mrb, mrb_value self)
      {
        mrb_raise(mrb, E_RUNTIME_ERROR, "error!");
        return mrb_nil_value();
      }

      int
      main(int argc, char *argv[])
      {
        mrb_state *mrb = mrb_open();
        mrb_define_method(mrb, mrb->object_class, "level1", level1, MRB_ARGS_NONE());
        mrb_define_method(mrb, mrb->object_class, "level2", level2, MRB_ARGS_NONE());
        mrb_p(mrb, mrb_load_string(mrb, "p level1"));
        mrb_close(mrb);
        return 0;
      }
      ```

    - compile & run

      ```console
      % `bin/mruby-config --cc --cflags --ldflags` crash.c `bin/mruby-config --libs`

      % ./a.out
      zsh: segmentation fault (core dumped)  ./a.out
      ```

    After applying this patch, it will print exception object and exit normally.

The `mrb_protect()`, `mrb_ensure()` and `mrb_rescue_exceptions()` in `mrbgems/mruby-error` have been rewritten using `mrb_protect_raw()`.
2021-04-19 22:11:57 +09:00
Yukihiro "Matz" Matsumoto 9d362f2c27 backtrace.c: remove MRB_API from internal functions.
- `mrb_exc_backtrace` to implement `Exception#backtrace`
- `mrb_get_backtrace` to implement `#caller`
2021-04-19 15:49:51 +09:00
John Bampton 9d32d440eb feat(CI): add the GitHub Super Linter
The GitHub Super Linter is a more robust and better supported
tool than the current GitHub Actions we are using.

Running these checks:

ERROR_ON_MISSING_EXEC_BIT: true
VALIDATE_BASH: true
VALIDATE_BASH_EXEC: true
VALIDATE_EDITORCONFIG: true
VALIDATE_MARKDOWN: true
VALIDATE_SHELL_SHFMT: true
VALIDATE_YAML: true

https://github.com/marketplace/actions/super-linter
https://github.com/github/super-linter

Added the GitHub Super Linter badge to the README.

Also updated the pre-commit framework and added
more documentation on pre-commit.

Added one more pre-commit check: check-executables-have-shebangs

Added one extra check for merge conflicts to our
GitHub Actions.

EditorConfig and Markdown linting.

Minor grammar and spelling fixes.

Update linter.yml
2021-04-16 16:37:52 +09:00
Yukihiro "Matz" Matsumoto ff2ffe3b07 proc.h: add type cast to silence warning; ref #5402 2021-04-12 15:21:54 +09:00
John Bampton d8622fc68b chore: fix spelling 2021-04-03 19:51:01 +10:00
Yukihiro "Matz" Matsumoto 08f9d5bab5 rational.c: overhaul rational operators.
- define `MRB_TT_RATIONAL`
- change object structure (`struct RRational`)
- add memory management for `MRB_TT_RATIONAL`
- avoid operator overloading as much as possible
- implement division overloading in C
- as a result, performance improved a lot
2021-03-19 10:37:10 +09:00
Yukihiro "Matz" Matsumoto 4ae5ae3535 codegen.c: no integer overflow error in codegen; close #5376
Add new pool value type `IREP_TT_BIGINT` and generate integer overflow
error in the VM. In the future, `mruby` will support `Bignum` for
integers bigger than `mrb_int` (probably using `mpz`).
2021-03-12 16:21:32 +09:00
Yukihiro "Matz" Matsumoto cb3a6dd168 ISO C99 doesn't support unnamed unions; fix #5354 2021-03-08 11:50:52 +09:00
Yukihiro "Matz" Matsumoto 111e4e1ba8 Merge pull request #5373 from mruby/stable
Merge mruby 3.0.0
2021-03-05 17:36:12 +09:00
mimaki 0f45836b59 Update version and release date. (mruby 3.0.0 (2021-03-05)) 2021-03-05 17:07:35 +09:00
Yukihiro "Matz" Matsumoto 2f335e1a58 Fix mrb_pool_value to keep int64 on 32 bit platforms; fix #5366 2021-03-04 17:17:13 +09:00
Yukihiro "Matz" Matsumoto 16fe040479 mruby/value.h: MRB_TT_CPTR is immediate with MRB_NO_BOXING.
The issue is reported by @shuujii.
2021-02-19 23:23:18 +09:00
Yukihiro "Matz" Matsumoto e5559184e0 Fix mrb_immediate_p() definition for MRB_NO_BOXING; ref #5352 2021-02-19 21:02:31 +09:00
Yukihiro "Matz" Matsumoto 787487b16d Check MRB_TT_* before object allocation; ref #5352 2021-02-19 15:36:41 +09:00
KOBAYASHI Shuji a6a76d8fd2 Add -s option to mrbc for make variable static 2021-02-14 11:55:53 +09:00
John Bampton be40e9c783 chore: fix spelling
Normally a single spell checker can't find all the mistakes or check all types of code.

These mistakes were found by another spell checker inside my editor with a more manual sift / find.
2021-02-13 17:21:17 +10:00
Yukihiro "Matz" Matsumoto 2af8e16cc6 include/mruby/array.h: ARY_LEN() should return mrb_int.
It used to return `mrb_ssize` but its size may differ from `mrb_int`,
e.g. `MRB_INT64` on `MRB_32BIT` architecture.
2021-02-12 16:57:33 +09:00
Yukihiro "Matz" Matsumoto db1172b48c Allow the case MRB_32BIT and MRB_NO_BOXING and MRB_USE_FLOAT32; #4382 2021-02-08 17:46:38 +09:00
mimaki ea3874ed3c Update version to 3.0.0RC. 2021-02-03 16:45:11 +09:00
Yukihiro "Matz" Matsumoto d759a73525 Allow more than 256 child irep; fix #5310
We have introduced following new instructions.

 * `OP_LAMBDA16`
 * `OP_BLOCK16`
 * `OP_METHOD16`
 * `OP_EXEC16`

Each instruction uses 16 bits operand for `reps` index. Since new
instructions are added, `mruby/c` VM should be updated.

Due to new instructions, dump format compatibility is lost, we have
increased `RITE_BINARY_MAJOR_VER`.

In addition, we have decreased the size of `refcnt` in `mrb_irep` from
`uint32_t` to `uint16_t`, which is reasonably big enough.
2021-02-01 16:20:58 +09:00
dearblue a9949f5da4 Introduced MRB_PRESYM_INIT_SYMBOLS()
The `init_SYMBOLS()` function implicitly defined in `MRB_PRESYM_DEFINE_VAR_AND_INITER()` requires some familiarity when trying to find it from the caller.
By introducing `MRB_PRESYM_INIT_SYMBOLS()`, it is possible to find directly from the identifier.
2021-01-31 18:13:28 +09:00
KOBAYASHI Shuji 3104aed8c6 Split presym_table for reduced program size
Because a structure that is an element of `presym_table` has padding, split
it into individual arrays for name and length.

#### Result (64-bit CPU with full-core gembox)

|        |   mruby    | libmruby.a |
|--------|------------|------------|
| Before | 1,087,444B | 1,476,872B |
| After  | 1,079,340B | 1,469,784B |
2021-01-27 20:47:10 +09:00
KOBAYASHI Shuji 826e0b108f Fix typos in mruby/presym/{disable,enable}.h [ci skip] 2021-01-27 12:18:41 +09:00
KOBAYASHI Shuji dd07067067 Fix "Use MRB_SYM() for error class retrieval"; ref 2ddfd50df
The new macro (`MRB_E_SYM`) was not being used, so it is being used. Also
`MRB_E_SYM` is confusing with `MRB_SYM_E`, so change it to `MRB_ERROR_SYM`.
2021-01-27 11:23:08 +09:00
Yukihiro "Matz" Matsumoto 2ddfd50df3 Use MRB_SYM() for error class retrieval; ref #5277
Note that `MRB_SYM()` is only available when `mruby/presym.h` is
included. Use `mrb_intern_lit()` otherwise.
2021-01-26 11:13:06 +09:00
Yukihiro "Matz" Matsumoto 17ecf14511 Revert "Minimize the changes in #5277"
This reverts commit dc51d89ac2.
2021-01-26 10:57:07 +09:00
Yukihiro "Matz" Matsumoto 48b214f0b5 Remove test debug lines in fallback presym.inc
ref dc51d89ac2
2021-01-23 18:12:04 +09:00
Yukihiro "Matz" Matsumoto dc51d89ac2 Minimize the changes in #5277
Instead of including `mruby/presym.h` everywhere, we provided the
fallback `mruby/presym.inc` under `include/mruby` directory, and specify
`-I<build-dir>/include` before `-I<top-dir>/include` in `presym.rake`.
So even when someone drops `-I<build-dir>/include` in compiler options,
it just compiles without failure.
2021-01-22 18:38:53 +09:00
Yukihiro "Matz" Matsumoto c5c156a6dd Reduce size of mrb_callinfo by rearranging members.
From 48 bytes to 40 bytes on 64 bit platforms (unchanged on 32 bit).
2021-01-21 21:11:32 +09:00
Yukihiro "Matz" Matsumoto 6c5ccd5abe Merge branch 'avoid-including-presym.inc-in-existing-header-files' of https://github.com/shuujii/mruby into shuujii-avoid-including-presym.inc-in-existing-header-files 2021-01-21 14:53:18 +09:00
Yukihiro "Matz" Matsumoto 47a68e89ff Remove CRC16 from dumped mruby binary.
`calc_crc_16_ccitt()` consumes a lot of clock cycles in programs like
`mrbtest` which loads a lot of dumped binary. Error detection for flaky
channels should be done in the higher level.

Note: `mruby/c` should be updated to support this change.
2021-01-20 23:12:43 +09:00
Yukihiro "Matz" Matsumoto bec4d30dc6 Merge branch 'reorganize-ci' of https://github.com/dearblue/mruby into dearblue-reorganize-ci 2021-01-12 18:38:42 +09:00
Yukihiro "Matz" Matsumoto b64ec814d6 Fix a signed shift bug on 32 bit platforms; ref e1c9e7e 2021-01-12 16:26:05 +09:00
Yukihiro "Matz" Matsumoto a8b8d316ab Save NOARG information in struct mt_elem; fix #5257 2021-01-12 14:51:34 +09:00
Yukihiro "Matz" Matsumoto e1c9e7efab Changed packing format of inline symbols.
To make inline symbols packed in 30 bits.
2021-01-12 14:51:33 +09:00
KOBAYASHI Shuji 48feda0692 Remove the definition of mrb_uint in numeric.h that is no longer needed 2021-01-11 16:20:27 +09:00
KOBAYASHI Shuji 90b53f4c29 Avoid including presym.inc in existing header files
Addressed an issue where existing programs linking `libmruby.a` could only
be built by adding `<build-dir>/include` to compiler's include path.
2021-01-11 09:21:07 +09:00
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
dearblue b210cfa34e Use uint16_t for argc and acc of mrb_callinfo
This is because it is enough to express the range up to (-1..255) or (-3..255).
2021-01-10 13:22:28 +09:00
Yukihiro "Matz" Matsumoto 92cc6b4996 Introduce mrb_uint which is unsigned integer with size of mrb_int. 2021-01-09 19:09:56 +09:00
Yukihiro "Matz" Matsumoto 12cc959998 Remove khash.h leftovers. 2021-01-09 19:09:56 +09:00
KOBAYASHI Shuji d35cd42dc8 Add scan target functions for presym
Add the following functions:

- mrb_intern_cstr
- mrb_define_singleton_method
- mrb_define_class_under
- mrb_define_module_under
2021-01-07 16:38:46 +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
Seeker 1099050377 Treat tabs as 8 spaces in squiggly heredocs 2020-12-31 19:09:38 -08:00
Yukihiro "Matz" Matsumoto 1fbabe207d Merge pull request #5245 from jbampton/for-mruby-for-2021
Update copyright year for 2021
2021-01-01 00:24:53 +09:00
Seeker 49b01f23a4 Fix mixed indentation and escaped tabs in squiggly heredocs 2020-12-28 18:22:13 -08:00
Seeker 54bfcaf1ff Add support for squiggly heredocs 2020-12-26 23:48:20 -08:00