Commit Graph

208 Commits

Author SHA1 Message Date
Yukihiro "Matz" Matsumoto a1027812bd error.c: small refactoring regarding #2485 2021-10-12 22:38:59 +09:00
Yukihiro "Matz" Matsumoto 291234c7aa Merge branch 'exc_mesg' of https://github.com/take-cheeze/mruby into take-cheeze-exc_mesg 2021-10-12 22:14:11 +09:00
Yukihiro "Matz" Matsumoto fd7896e579 error.c: stop calling initialize from VM exceptions.
The invocation of `initialize` hook can cause infinite recursion too
easily. We stop invoking the method for safety, at the cost of less
flexibility.

The `initialize` methods (e.g. ones defined in `mrblib/10error.rb`) are
called only from `NoMethodError.new(args..)` forms.
2021-09-22 11:41:38 +09:00
Yukihiro "Matz" Matsumoto 2c41739b66 mruby.h: obsolete mrb_to_str().
Replace them by `mrb_ensure_string_type()`.
2021-09-01 07:00:55 +09:00
Yukihiro "Matz" Matsumoto b9d2b47f16 Replace mrb_fixnum_value() with mrb_int_value().
Use `mrb_fixnum_value()` only when you are absolutely sure that the
value is within `Fixnum` range, i.e. 31 bits signed integer at least.
2021-08-03 13:00:11 +09:00
Yukihiro "Matz" Matsumoto 5c804cf68f Remove redundant include headers.
- stdlib.h
- stddef.h
- stdint.h
- stdarg.h
- limits.h
- float.h
2021-07-25 13:07:10 +09:00
Yukihiro "Matz" Matsumoto 040d1799ec error.c: mrb_obj_as_string and mrb_inspect may return non-object (undef). 2021-07-02 14:24:20 +09:00
dearblue c0cd004b21 Fix symbol leak in exc_to_s() 2021-04-25 10:49:33 +09:00
Yukihiro "Matz" Matsumoto 810d13dacd Move MRB_TRY part of mrb_close to src/error.c; ref 6cc52b3
We don't want to increase number of files that should be compiled by C++
compiler when `enable_cxx_exception` is turned on.
2021-02-03 09:43:41 +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 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
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
KOBAYASHI Shuji 3d056d084a Rename MRB_{ENABLE,DISABLE}_ to MRB_{USE,NO}_; close #5163
|        Previous Name         |        New Name         |
|------------------------------|-------------------------|
| MRB_ENABLE_ALL_SYMBOLS       | MRB_USE_ALL_SYMBOLS     |
| MRB_ENABLE_SYMBOLL_ALL       | MRB_USE_ALL_SYMBOLS     |
| MRB_ENABLE_CXX_ABI           | MRB_USE_CXX_ABI         |
| MRB_ENABLE_CXX_EXCEPTION     | MRB_USE_CXX_EXCEPTION   |
| MRB_ENABLE_DEBUG_HOOK        | MRB_USE_DEBUG_HOOK      |
| MRB_DISABLE_DIRECT_THREADING | MRB_NO_DIRECT_THREADING |
| MRB_DISABLE_STDIO            | MRB_NO_STDIO            |
| ENABLE_LINENOISE             | MRB_USE_LINENOISE       |
| ENABLE_READLINE              | MRB_USE_READLINE        |
| DISABLE_MIRB_UNDERSCORE      | MRB_NO_MIRB_UNDERSCORE  |
| DISABLE_GEMS                 | MRB_NO_GEMS             |

* `MRB_ENABLE_SYMBOLL_ALL` seems to be a typo, so it is fixed.
* `MRB_` prefix is added to those without.
* The previous names can also be used for compatibility.
2020-11-21 21:14:40 +09:00
Yukihiro "Matz" Matsumoto 82046ff26e Revert "Add a new function mrb_exc_protect()."
This reverts commit 8746a6fe4e7bda8a0fbc0eaece9314ec51a0c255.

We already have `mrb_protect()`, `mrb_ensure()` and `mrb_rescue()`
functions. If you need to handle exceptions from C functions, use those
functions above.
2020-10-12 18:20:30 +09:00
Yukihiro "Matz" Matsumoto 3d6adccfbe Add a new function mrb_exc_protect().
`mrb_exc_protect()` takes two C functions, `body` to be executed first,
and `resc` to be executed when an error happens during `body` execution.
Since `mrb_exc_protect()` should be compiled with the proper compiler,
we will not see the problem like #5088 that was caused by `setjmp()` and
`throw` mixture.
2020-10-12 18:20:29 +09:00
Yukihiro "Matz" Matsumoto 7e3d22f095 Raname mrb_exc_new_str_lit() to mrb_exc_new_lit().
It uses `mrb_str_new_lit()` internally, but it doesn't need to express
it in the name of the function (macro).
2020-10-12 18:20:06 +09:00
Yukihiro "Matz" Matsumoto 8a87549315 Rename float configuration option names.
- `MRB_WITHOUT_FLOAT` => `MRB_NO_FLOAT`
- `MRB_USE_FLOAT` => `MRB_USE_FLOAT32`

The former is to use `USE_XXX` naming convention. The latter is to make
sure `float` is 32bit float and not floating point number in general.
2020-10-12 16:21:40 +09:00
Yukihiro "Matz" Matsumoto 00f5ddc9ae Use mrb_funcall_id() extensively.
Except for support files e.g. `mruby-test/driver.c`, which are not
target of symbol collection via `rake gensym`.
2020-10-12 16:20:58 +09:00
Yukihiro "Matz" Matsumoto eddd324979 Add MRB_SYM() for inline symbols. 2020-10-12 16:20:41 +09:00
Yukihiro "Matz" Matsumoto 49ae2a69f2 Add mrb_get_arg1() that retrieves single (and only) argument.
`mrb_get_arg1()` raises `ArgumentError` if the method does not receive one
argument.

And replaces all `mrb_get_args(mrb, "o", &arg)` by the new function.
2020-06-20 12:49:46 +09:00
Yukihiro "Matz" Matsumoto f1523d2404 Squashed commit of the following:
commit 2d7d545c4c4bfce7fdcbcbe9baaeb437915742f0
Merge: 625a1249 b178914b
Author: Yukihiro "Matz" Matsumoto <matz@ruby.or.jp>
Date:   Fri Jun 5 14:35:13 2020 +0900

    Merge branch 'fix-mrb_open-with-nomem' of https://github.com/dearblue/mruby into dearblue-fix-mrb_open-with-nomem

commit b178914b11
Author: dearblue <dearblue@users.noreply.github.com>
Date:   Sat Jan 19 22:22:44 2019 +0900

    Fix invalid pointer free inside other heap's block

     1. `e = mrb_obj_alloc(...)`
     2. `e->stack = mrb->c->stack` (`mrb->c->stack` is anywhere in the range `stbase...stend`)
     3. And raised exception by `mrb_malloc()`!
     4. `mrb_free(e->stack)` by GC part (wrong free)

commit 52e3d5d858
Author: dearblue <dearblue@users.noreply.github.com>
Date:   Sat Jan 19 21:55:36 2019 +0900

    Fix memory leak for temporary symbols when out of memory

commit 4c5499b88e
Author: dearblue <dearblue@users.noreply.github.com>
Date:   Sun Jan 20 11:42:07 2019 +0900

    Fix uninitialized pointer dereference for debug section

commit 8e993167de
Author: dearblue <dearblue@users.noreply.github.com>
Date:   Sun Jan 20 11:41:09 2019 +0900

    Fix memory leak for temporary filenames when out of memory

commit 8b422577e6
Author: dearblue <dearblue@users.noreply.github.com>
Date:   Sun Jan 20 10:57:51 2019 +0900

    Fix memory leak for irep when out of memory

commit 6b35ebf49a
Author: dearblue <dearblue@users.noreply.github.com>
Date:   Sun Jan 20 10:55:50 2019 +0900

    Fix uninitialized pointer dereference when do not finished initializing irep

commit 2531f2631e
Author: dearblue <dearblue@users.noreply.github.com>
Date:   Sun Jan 20 10:48:15 2019 +0900

    Fix NULL pointer dereference when do not finished initializing irep

commit e2d6896eba
Author: dearblue <dearblue@users.noreply.github.com>
Date:   Sat Jan 19 12:54:19 2019 +0900

    Fix memory leak for irep when out of memory by `mrb_proc_new()`

commit b6214ff8a0
Author: dearblue <dearblue@users.noreply.github.com>
Date:   Sat Jan 19 12:53:07 2019 +0900

    Fix memory leak for `khash_t` in `kh_init_size()` when out of memory by `kh_alloc()`

commit 19162dd6c1
Author: dearblue <dearblue@users.noreply.github.com>
Date:   Sun Jan 20 02:15:07 2019 +0900

    Fix memory leak for symbol string when out of memory in `kh_put()`

commit 15e67297ff
Author: dearblue <dearblue@users.noreply.github.com>
Date:   Sun Jan 20 02:12:24 2019 +0900

    Fix keep wrong symbol index when out of memory

commit 3f8e2b3752
Author: dearblue <dearblue@users.noreply.github.com>
Date:   Sun Jan 20 02:08:13 2019 +0900

    Fix keep wrong symbol capacity when out of memory

commit a3cfe755ab
Author: dearblue <dearblue@users.noreply.github.com>
Date:   Sat Jan 19 10:11:37 2019 +0900

    Fix NULL pointer dereference `mrb->c` by `mark_context()`

commit d9c7b6be6e
Author: dearblue <dearblue@users.noreply.github.com>
Date:   Sun Jan 20 15:25:09 2019 +0900

    Fix protect exception for print error message

commit 100642750e
Author: dearblue <dearblue@users.noreply.github.com>
Date:   Sun Jan 20 11:59:02 2019 +0900

    Protect exception for mruby core initialization

commit 7a0418304e
Author: dearblue <dearblue@users.noreply.github.com>
Date:   Fri Jan 18 20:38:27 2019 +0900

    Fix memory leak for string object when out of memory

    The `mrb_str_pool()` function has a path to call `malloc()` twice.

    If occurs `NoMemoryError` exception in second `malloc()`,
    first `malloc()` pointer is not freed.

commit fef1c152ce
Author: dearblue <dearblue@users.noreply.github.com>
Date:   Sat Jan 19 13:05:09 2019 +0900

    Fix stack overflow when out of memory

    As a result of this change, no backtrace information is set
    for NoMemoryError (`mrb->nomem_err`).

    Detailes:

    When generating a backtrace, called `mrb_intern_lit()`,
    `mrb_str_new_cstr()` and `mrb_obj_iv_set()` function with
    `exc_debug_info()` function in `src/error.c`.

    If a `NoMemoryError` exception occurs at this time,
    the `exc_debug_info()` function will be called again,
    and in the same way `NoMemoryError` exception raised will result
    in an infinite loop to occurs stack overflow (and SIGSEGV).

commit da7d7f881b
Author: dearblue <dearblue@users.noreply.github.com>
Date:   Sun Jan 20 12:00:38 2019 +0900

    Fix NULL pointer dereference `mrb->nomem_err` when not initialized

    Add internal functions (not `static`):

      * `mrb_raise_nomemory()`
      * `mrb_core_init_abort()`
2020-06-05 14:40:07 +09:00
Yukihiro "Matz" Matsumoto 9174b18f34 Rename mrb_num_args_error to mrb_argnum_error; ref #4863 2020-01-01 22:37:14 +09:00
Yukihiro "Matz" Matsumoto b9c78c3fcc Merge pull request #4863 from shuujii/add-mrb_num_args_error-for-wrong-number-of-arguments-error
Add `mrb_num_args_error()` for "wrong number of arguments" error
2020-01-01 22:34:22 +09:00
KOBAYASHI Shuji e8299a89da Fix potentially crash in %n of mrb_vformat() with 64-bit int
If `mrb_sym` is smaller than `int`, it is promoted to `int`.
2019-12-20 23:04:46 +09:00
KOBAYASHI Shuji 6df6bd4ac7 Simplify print_backtrace() 2019-12-18 19:35:24 +09:00
Yukihiro "Matz" Matsumoto 815e0c400a Merge pull request #4875 from shuujii/remove-location-info-from-Exception-inspect
Remove location info from `Exception#inspect`
2019-12-18 01:55:23 +09:00
KOBAYASHI Shuji 29ecc3840a Refine output of mrb_print_error()
The following improvements are made according to Ruby's behavior:

- Match location number to index.
- Remove duplicate most recent call output.
- Fix that first call is not output when array (unpacked) backtrace.

### Example

  ```ruby
  def a; raise "error!" end
  def b; a end
  begin
    b
  rescue => e
    e.backtrace if ARGV[0] == "unpack"  # unpack backtrace
    raise e
  end
  ```

#### Before this patch:

  ```
  $ bin/mruby example.rb unpack
  trace (most recent call last):
    [0] example.rb:2:in b
    [1] example.rb:1:in a
  example.rb:1: error! (RuntimeError)
  ```

#### After this patch:

  ```
  $ bin/mruby example.rb unpack
  trace (most recent call last):
    [2] example.rb:4
    [1] example.rb:2:in b
  example.rb:1:in a: error! (RuntimeError)
  ```
2019-12-17 23:16:20 +09:00
KOBAYASHI Shuji d2f2f9db51 Remove location info from Exception#inspect
Because location info (file name and line number) is kept in the backtrace,
it should not be kept in the result of `inspect` (and the exception object
itself), I think.

### Example

  ```ruby
  # example.rb
  begin
    raise "err"
  rescue => e
    p e
  end
  ```

#### Before this patch:

  ```
  $ bin/mruby example.rb
  example.rb:2: err (RuntimeError)
  ```

#### After this patch:

  ```
  $ bin/mruby example.rb
  err (RuntimeError)
  ```
2019-12-14 22:29:37 +09:00
KOBAYASHI Shuji 81de1f159c Add mrb_num_args_error() for "wrong number of arguments" error
To unify the style of messages.
2019-12-12 11:45:58 +09:00
KOBAYASHI Shuji 85e34f1ca4 Fix argument specs to Exception 2019-10-24 22:24:40 +09:00
dearblue d8f1099419 Allow rethrowing MRB_TT_BREAK 2019-09-29 15:11:36 +09:00
KOBAYASHI Shuji 07856a7e8f Exception#initialize should not allow two or more arguments 2019-09-24 21:43:46 +09:00
Yukihiro "Matz" Matsumoto 57d7fe94a9 Add a macro mrb_frozen_p that points to MRB_FROZEN_P. 2019-09-14 23:21:44 +09:00
dearblue 311d867deb Fix mrb_vformat() crashes with MRB_INT16
If `MRB_INT16` is specified, the variable length argument `mrb_int` is
converted to `int`.
2019-09-08 20:31:35 +09:00
dearblue 279c21b816 Prohibit changes to iseq in principle 2019-08-18 15:00:32 +09:00
KOBAYASHI Shuji 334afb167c Use new specifiers/modifiers of mrb_vfromat()
The binary sizes (gems are only `mruby-bin-mruby`) are reduced slightly in
my environment than before the introduction of new specifiers/modifiers
(5116789a) with this change.

  ------------+-------------------+-------------------+--------
   BINARY     | BEFORE (5116789a) |   AFTER (This PR) |  RATIO
  ------------+-------------------+-------------------+--------
   mruby      |      593416 bytes |      593208 bytes | -0.04%
   libmruby.a |      769048 bytes |      767264 bytes | -0.23%
  ------------+-------------------+-------------------+--------

BTW, I accidentally changed `tasks/toolchains/visualcpp.rake` at #4613,
so I put it back.
2019-08-05 13:18:50 +09:00
KOBAYASHI Shuji 1e9cb74cc6 Change second argument to %l of mrb_vformat() to size_t from mrb_int
- `size_t` is more commonly used.
- `len` argument of `mrb_str_new()` is `size_t`.

NOTE:

The test for `%l` is temporarily disabled because adding a new type to
`mrbgems/mruby-test/vformat.c` causes an error (memory error?) on Visual
Studio 2017 in AppVeyor.
2019-08-03 19:42:22 +09:00
KOBAYASHI Shuji a4243360a2 Fix mrb_vformat("%f") with MRB_USE_FLOAT
It potentially not work when `mrb_float` is `float` because `float` variable
in variable length arguments is promoted to `double`.

Also I fixed build with `MRB_WITHOUT_FLOAT`.
2019-08-03 11:54:45 +09:00
KOBAYASHI Shuji d1817e7791 Change the mrb_vformat specifier %d for int
It potentially breaks, for example, in the case of `mrb_int` is 64-bit
and more smaller type is passed by `%d`. In fact, the problem could
become apparent when I used `%d` to `backtrace_location::lineno` in
`src/backtrace.c:mrb_unpack_backtrace()` on AppVeyor.

Therefore, change `%d` for `int` (not `mrb_int`) so that it can be
used mostly without casting.
2019-08-02 19:22:08 +09:00
KOBAYASHI Shuji eea42e06af Add new specifiers/modifiers to format string of mrb_vfromat()
Format sequence syntax:

  %[modifier]specifier

Modifiers:

  ----------+------------------------------------------------------------
  Modifier  | Meaning
  ----------+------------------------------------------------------------
      !     | Convert to string by corresponding `inspect` instead of
            | corresponding `to_s`.
  ----------+------------------------------------------------------------

Specifiers:

  ----------+----------------+--------------------------------------------
  Specifier | Argument Type  | Note
  ----------+----------------+--------------------------------------------
      c     | char           |
     d,i    | mrb_int        |
      f     | mrb_float      |
      l     | char*, mrb_int | Arguments are string and length.
      n     | mrb_sym        |
      s     | char*          | Argument is NUL terminated string.
      t     | mrb_value      | Convert to type (class) of object.
     v,S    | mrb_value      |
      C     | struct RClass* |
      T     | mrb_value      | Convert to real type (class) of object.
      Y     | mrb_value      | Same as `!v` if argument is `true`, `false`
            |                | or `nil`, otherwise same as `T`.
      %     | -              | Convert to percent sign itself (no argument
            |                | taken).
  ----------+----------------+--------------------------------------------

This change will increase the binary size, but replacing all format strings
with new specifiers/modifiers will decrease the size because it reduces
inline expansion of `mrb_obj_value()`, etc. at the caller.
2019-08-01 13:24:52 +09:00
KOBAYASHI Shuji e3beef065c Extract frozen checking to function 2019-04-09 18:23:11 +09:00
Yukihiro "Matz" Matsumoto 2871d0cdc5 Avoid keeping pointers from mrb_sym2name_len(); fix #4342
The addresses for packed inline symbols reference `mrb->symbuf` that
could be overridden by the later call of `mrb_sym2name_len`. Since
file names in call stack information are kept as symbols, keeping the
address in the C structures could cause problems like #4342.

This changes small incompatible changes in function prototypes:
* `mrb_parser_get_filename`: return value changed to `mrb_sym`.
* `mrb_debug_get_filename`: add `mrb_state*` as a first argument.
* `mrb_debug_get_line`: ditto.

I believe above functions are almost internal, and no third-party
mrbgem use them.
2019-04-01 14:13:06 +09:00
KOBAYASHI Shuji 62bb9c6436 Add newline to warning by mrb_warn() 2019-02-27 20:58:20 +09:00
Yukihiro "Matz" Matsumoto 9516731329 Use type checking mrb_to_str instead of converting mrb_str_to_str. 2018-11-19 12:08:28 +09:00
Yukihiro "Matz" Matsumoto 14c21793a0 Check if the exception is frozen; fix #4025
`exc_debug_info()` and `mrb_keep_backtrace()` raise `FrozenError`
if the exception is frozen and lead to infinite loop.
2018-05-23 08:30:50 +09:00
Yukihiro "Matz" Matsumoto d895861a30 Check arena_idx before accessing; fix #3934 2018-01-25 12:30:10 +09:00
Yukihiro "Matz" Matsumoto 6f4d4bbcc7 Remove temporary objects from GC arena in `mrb_vformat()'; #3863 2017-12-04 08:56:11 +09:00
Yukihiro "Matz" Matsumoto cd9f53d42e Pop exception objects from the bottom of GC arena; fix #3863 2017-12-04 08:56:11 +09:00
Tomoyuki Sahara f6896751b4 don't overwrite backtrace info. 2017-11-02 15:44:05 +09:00
Tomasz Dąbrowski 971a4f9122 fix: src\kernel.c(874): warning C4244: 'function': conversion from 'mrb_int' to 'int', possible loss of data 2017-09-27 22:22:05 +02:00