Commit graph

289 commits

Author SHA1 Message Date
Nobuyoshi Nakada
899ea35035
Extract include/ruby/internal/attr/packed_struct.h
Split `PACKED_STRUCT` and `PACKED_STRUCT_UNALIGNED` macros into the
macros bellow:
* `RBIMPL_ATTR_PACKED_STRUCT_BEGIN`
* `RBIMPL_ATTR_PACKED_STRUCT_END`
* `RBIMPL_ATTR_PACKED_STRUCT_UNALIGNED_BEGIN`
* `RBIMPL_ATTR_PACKED_STRUCT_UNALIGNED_END`
2023-02-08 12:34:13 +09:00
Khem Raj
f49bb1505b
[ruby/fiddle] fiddle: Use C11 _Alignof to define ALIGN_OF when
possible
(https://github.com/ruby/fiddle/pull/120)

WG14 N2350 made very clear that it is an UB having type definitions
within "offsetof" [1]. This patch enhances the implementation of macro
ALIGN_OF to use builtin "_Alignof" to avoid undefined behavior when
using std=c11 or newer

clang 16+ has started to flag this [2]

Fixes build when using -std >= gnu11 and using clang16+

Older compilers gcc < 4.9 or clang < 8 has buggy _Alignof even though it
may support C11, exclude those compiler versions

[1] https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2350.htm
[2] https://reviews.llvm.org/D133574

Signed-off-by: Khem Raj <raj.khem@gmail.com>

ad6c9aa826
2023-01-27 11:30:13 +09:00
Aaron Patterson
f4609b6bc4
[ruby/fiddle] Fix comment formatting
36b2432575
2023-01-27 11:30:13 +09:00
Aaron Patterson
1df7f359d1
[ruby/fiddle] Update documentation
(https://github.com/ruby/fiddle/pull/119)

The documentation for `Fiddle.dlwrap` and `Fiddle.dlunwrap` were not
very accurate and pretty confusing. This commit updates the
documentation so it's easier to understand what the methods do.
2023-01-27 11:30:13 +09:00
Mau Magnaguagno
8df574a492 [ruby/fiddle] Prefer String#unpack1 in Fiddle::ValueUtil
(https://github.com/ruby/fiddle/pull/118)

String#unpack1 is available since Ruby 2.4, and support for older than
Ruby 2.5 was dropped by #85.
Also simplified a common return statement.
2022-12-26 15:09:21 +09:00
Sutou Kouhei
1dc8c18b72 [ruby/fiddle] Bump version
3033266902
2022-12-26 15:09:21 +09:00
Hiroshi SHIBATA
286812bcf3 Merge fiddle-1.1.1 2022-12-09 16:36:22 +09:00
Aaron Patterson
dffca50bb6 [ruby/fiddle] Free closures immediately
(https://github.com/ruby/fiddle/pull/109)

These structs don't need to be freed as part of finalization, so lets
free them immediately.

8a10ec1152
2022-10-18 17:21:45 +09:00
Sutou Kouhei
e84ea4af69 [ruby/fiddle] Add support for linker script on Linux
GitHub: fix https://github.com/ruby/fiddle/pull/107

Reported by nicholas a. evans. Thanks!!!

49ea1490df
2022-10-18 17:21:45 +09:00
Sutou Kouhei
08ec656282 [ruby/fiddle] Bump version 2022-10-18 17:21:45 +09:00
Nobuyoshi Nakada
091e3522d7 fiddle: use the old rb_ary_tmp_new() alias
Fiddle is a gem and has the external upstream which supports older
versions of Ruby.
2022-10-18 17:21:45 +09:00
Hiroshi SHIBATA
4f78560cf1
Add --with-libffi-source-dir feature and removed --enable-bundled-libffi option. (#113)
https://bugs.ruby-lang.org/issues/18571

Co-authored-by: Nobuyoshi Nakada <nobu@ruby-lang.org>
Co-authored-by: Sutou Kouhei <kou@clear-code.com>
2022-10-07 15:20:11 +09:00
Sutou Kouhei
ada9f8a9f7 [ruby/fiddle] closure: follow variable name change
GitHub: GH-102

2530496602
2022-10-07 15:18:54 +09:00
Sutou Kouhei
a4ad6bd9aa [ruby/fiddle] closure: free resources when an exception is raised in Closure.new
GitHub: GH-102

81a8a56239
2022-10-07 15:18:53 +09:00
Sutou Kouhei
255e617bc3 [ruby/fiddle] Add Fiddle::Closure.create and Fiddle::Closure.free
GitHub: fix GH-102

It's for freeing a closure explicitly.

We can't use Fiddle::Closure before we fork the process. If we do it,
the process may be crashed with SELinux.

See https://github.com/ruby/fiddle/issues/102#issuecomment-1241763091
for details.

Reported by Vít Ondruch. Thanks!!!

a0ccc6bb1b
2022-10-07 15:18:51 +09:00
Aaron Patterson
0097c7f388 [ruby/fiddle] Add sym_defined? methods to test if a symbol is defined (https://github.com/ruby/fiddle/pull/108)
I would like to check if a symbol is defined before trying to access it.
Some symbols aren't available on all platforms, so instead of raising an
exception, I want to check if it's defined first.

Today we have to do:

```ruby
begin
  addr = Fiddle::Handle.sym("something")
  # do something
rescue Fiddle::DLError
end
```

I want to write this:

```ruby
if Fiddle::Handle.sym_defined?("something")
  addr = Fiddle::Handle.sym("something")
  # do something
end
```

9d3371de13

Co-authored-by: Sutou Kouhei <kou@clear-code.com>
2022-10-07 15:18:51 +09:00
Aaron Patterson
755d99e878 [ruby/fiddle] Move "type" constants to Fiddle::Types (https://github.com/ruby/fiddle/pull/112)
This helps to reduce repetition in code. Instead of doing "TYPE_*"
everywhere, you can do `include Fiddle::Types`, and write the type name
directly.

This PR is to help reduce repetition when writing Fiddle code. Right now
we have to type `TYPE_` everywhere, and you also have to include all of
`Fiddle` to access `TYPE_*` constants. With this change, you can just
include `Fiddle::Types` and it will shorten your code and also you only
have to include those constants.

Here is an example before:

```ruby
require "fiddle"

module MMAP
  # All Fiddle constants included
  include Fiddle

  def self.make_function name, args, ret
    ptr = Handle::DEFAULT[name]
    func = Function.new ptr, args, ret, name: name
    define_singleton_method name, &func.to_proc
  end

  make_function "munmap", [TYPE_VOIDP, # addr
                           TYPE_SIZE_T], # len
                           TYPE_INT

  make_function "mmap", [TYPE_VOIDP,
                         TYPE_SIZE_T,
                         TYPE_INT,
                         TYPE_INT,
                         TYPE_INT,
                         TYPE_INT], TYPE_VOIDP

  make_function "mprotect", [TYPE_VOIDP, TYPE_SIZE_T, TYPE_INT], TYPE_INT
end
```

After:

```ruby
require "fiddle"

module MMAP
  # Only type names included
  include Fiddle::Types

  def self.make_function name, args, ret
    ptr = Fiddle::Handle::DEFAULT[name]
    func = Fiddle::Function.new ptr, args, ret, name: name
    define_singleton_method name, &func.to_proc
  end

  make_function "munmap", [VOIDP, # addr
                           SIZE_T], # len
                           INT

  make_function "mmap", [VOIDP, SIZE_T, INT, INT, INT, INT], VOIDP

  make_function "mprotect", [VOIDP, SIZE_T, INT], INT
end
```

We only need to import the type names, and you don't have to type
`TYPE_` over and over. I think this makes Fiddle code easier to read.

49fa7233e5

Co-authored-by: Sutou Kouhei <kou@clear-code.com>
2022-10-07 15:18:50 +09:00
Aaron Patterson
48a6498406 [ruby/fiddle] Add constants for unsigned values (https://github.com/ruby/fiddle/pull/111)
This commit adds constants for unsigned values. Currently we can use `-`
to mean "unsigned", but I think having a specific name makes Fiddle more
user friendly. This commit continues to support `-`, but introduces
negative constants with "unsigned" names

I think this will help to eliminate [this
code](3a56bf0bcc/lib/mjit/c_type.rb (L31-L38))

2bef0f1082

Co-authored-by: Sutou Kouhei <kou@clear-code.com>
2022-10-07 15:18:49 +09:00
Takashi Kokubun
106744107b
[ruby/fiddle] Fix PACK_MAP for unsigned types (https://github.com/ruby/fiddle/pull/110)
4a71246645
2022-09-11 15:30:49 +09:00
Nobuyoshi Nakada
e4f5296f06
No longer bundle external library sources 2022-09-09 01:33:53 +09:00
Peter Zhu
efb91ff19b Rename rb_ary_tmp_new to rb_ary_hidden_new
rb_ary_tmp_new suggests that the array is temporary in some way, but
that's not true, it just creates an array that's hidden and not on the
transient heap. This commit renames it to rb_ary_hidden_new.
2022-07-26 09:12:09 -04:00
Peter Zhu
2d5ecd60a5 [Feature #18249] Update dependencies 2022-02-22 09:55:21 -05:00
Koichi Sasada
cc8064ba2e Do not need to print to stderr 2022-01-26 10:05:10 +09:00
Nobuyoshi Nakada
ac152b3cac
Update dependencies 2021-11-21 16:21:18 +09:00
卜部昌平
5c167a9778 ruby tool/update-deps --fix 2021-10-05 14:18:23 +09:00
Hiroshi SHIBATA
8f752c95d2
[ruby/fiddle] Use test-unit gem (https://github.com/ruby/fiddle/pull/69)
e08c4c635e

Co-authored-by: Sutou Kouhei <kou@clear-code.com>
2021-09-05 17:43:48 +09:00
Aaron Patterson
0f1e8f38c9 [ruby/fiddle] Improve "offsetof" calculations (https://github.com/ruby/fiddle/pull/90)
I need to get the offset of members inside sub structures.  This patch
adds sub-structure offset support for structs.

cf78eddbb6
2021-08-24 16:18:22 +09:00
Kenta Murata
67897762cf
[ruby/fiddle] Add Fiddle::Handle#file_name (https://github.com/ruby/fiddle/pull/88)
4ee1c6fc4b
2021-07-14 18:56:00 +09:00
Nobuyoshi Nakada
169529a0c0
[ruby/fiddle] Check HAVE_RUBY_MEMORY_VIEW_H rather than API version (https://github.com/ruby/fiddle/pull/86)
c5abcc3a7e
2021-07-14 18:55:59 +09:00
Kenta Murata
818c74b7f4 [ruby/fiddle] Return the module handle value in Fiddle::Handle#to_i and add FIddle::Handle#to_ptr (https://github.com/ruby/fiddle/pull/87)
170111a0cb
2021-07-14 18:43:32 +09:00
Nobuyoshi Nakada
cb955dc9ac
[ruby/fiddle] update dependencies 2021-07-13 21:34:28 +09:00
Nobuyoshi Nakada
40d45ab093
[ruby/fiddle] Check HAVE_RUBY_MEMORY_VIEW_H rather than API version
93f9564446
2021-07-13 20:36:45 +09:00
Nobuyoshi Nakada
472d8c5555
[ruby/fiddle] Update required_ruby_version (https://github.com/ruby/fiddle/pull/85)
Drop supports for old versions, keeping 2.5 as CI supports it for
now.

90634e7c55
2021-07-13 19:37:46 +09:00
Sutou Kouhei
bb868f4814
[ruby/fiddle] Use have_header and have_type to detect memory view availability
Fix https://github.com/ruby/fiddle/pull/84

It may detect ruby/memory_view.h for system Ruby that is installed in
/usr.

We can use RUBY_API_VERSION_MAJOR to detect memory view availability
because memory view is available since Ruby 3.0.

Reported by Jun Aruga. Thanks!!!

3292929830
2021-07-13 19:37:46 +09:00
Aaron Patterson
5c0d8c6369
[ruby/fiddle] Add "offsetof" to Struct classes (https://github.com/ruby/fiddle/pull/83)
* Add "offsetof" to Struct classes

I need to get the offset of a member inside a struct without allocating
the struct.  This patch adds an "offsetof" class method to structs that
are generated.

The usage is like this:

```ruby
MyStruct = struct [
  "int64_t i",
  "char c",
]

MyStruct.offsetof("i") # => 0
MyStruct.offsetof("c") # => 8
```

* Update test/fiddle/test_c_struct_builder.rb

Co-authored-by: Sutou Kouhei <kou@cozmixng.org>

4e3b60c5b6

Co-authored-by: Sutou Kouhei <kou@cozmixng.org>
2021-07-13 19:37:46 +09:00
Sutou Kouhei
a2c9e1b58a
[ruby/fiddle] Bump version
049138b4b8
2021-07-13 19:37:46 +09:00
Sutou Kouhei
70b0318646
[ruby/fiddle] MemoryView: ensure reset rb_memory_view_t::obj on error
0ed39345fe
2021-07-13 19:37:46 +09:00
Sutou Kouhei
9f86e50e1e
[ruby/fiddle] StringValuePtr may change the val
bddca7c895
2021-07-13 19:37:45 +09:00
Sutou Kouhei
10e26cfa76
[ruby/fiddle] Add MemoryView.export and MemoryView#release (https://github.com/ruby/fiddle/pull/80)
fix https://github.com/ruby/fiddle/pull/79

Users can release memory views explicitly before process exit.

Reported by xtkoba. Thanks!!!

1de64b7e76
2021-07-13 19:37:45 +09:00
Sutou Kouhei
9988f6ac4e
[ruby/fiddle] Add Fiddle::MemoryView#to_s (https://github.com/ruby/fiddle/pull/78)
Fix https://github.com/ruby/fiddle/pull/74

Reported by dsisnero. Thanks!!!
2021-07-13 19:37:45 +09:00
Sutou Kouhei
d1eeb9fec9
[ruby/fiddle] windows: use GetLastError() for win32_last_error
Ruby: [Bug #11579]

Patch by cremno phobia. Thanks!!!

760a8f9b14
2021-07-13 19:37:45 +09:00
Sutou Kouhei
c0f9191ab6
[ruby/fiddle] Bump version
3784cfeec4
2021-07-13 19:37:44 +09:00
Sutou Kouhei
79717f81f8 [ruby/fiddle] windows: link to ws2_32 for WSAGetLastError()
e9955d74ae
2021-05-18 12:48:40 +09:00
Sutou Kouhei
71d4a493b8 [ruby/fiddle] windows: add Fiddle.win32_last_socket_error{,=}
GitHub: fix GH-72

Users can't use WSAGetLastError() with Ruby 3.0 or later because
rb_funcall() resets the last socket error internally.

Users can get the last socket error by Fiddle.win32_last_socket_error.

Reported by Kentaro Hayashi. Thanks!!!

76158db00a
2021-05-18 12:48:40 +09:00
Sutou Kouhei
ab5212b3c9 [ruby/fiddle] Add support for "const" in type
GitHub: fix #68

Reported by kojix2. Thanks!!!

d7322c234a
2021-05-18 12:48:40 +09:00
Sutou Kouhei
4d1bb460f6 [ruby/fiddle] Add --enable-debug-build option to extconf.rb
e0498e60ea
2021-05-18 12:48:40 +09:00
Sutou Kouhei
791e8eec66 [ruby/fiddle] win32types: sort
35dec6c5a5
2021-05-18 12:48:40 +09:00
Sutou Kouhei
8758b07b1e [ruby/fiddle] Fix more Win32Types definitions
https://docs.microsoft.com/en-us/windows/win32/winprog/windows-data-types

805c1a595a
2021-05-18 12:48:40 +09:00
Orgad Shaneh
25e56fe374 [ruby/fiddle] Fix Win32Types for Windows 64-bit (#63)
https://docs.microsoft.com/en-us/windows/win32/winprog/windows-data-types

28ee5b1608
2021-05-18 12:48:40 +09:00
Sutou Kouhei
8d63b1dc75 [ruby/fiddle] Bump version
0cbd370fd6
2021-05-18 12:48:40 +09:00