Commit graph

149 commits

Author SHA1 Message Date
NARUSE, Yui
ab4264a21f suppress a warning 2023-12-25 22:15:25 +09:00
Sutou Kouhei
2a6d6d3d65 [ruby/fiddle] Use Ruby's true/false for C bool
GitHub: fix https://github.com/ruby/fiddle/pull/130

Reported by Benoit Daloze. Thanks!!!

2640e0148e
2023-11-08 09:25:44 +09:00
Sutou Kouhei
15e8cf7ad9 [ruby/fiddle] Add support for bool
GitHub: fix https://github.com/ruby/fiddle/pull/130

Reported by Benoit Daloze. Thanks!!!

bc6c66bbb9
2023-08-02 18:17:18 +09:00
Sutou Kouhei
74e049294e [ruby/fiddle] Add support for more "short" variants
4598e6cd50
2023-06-28 16:36:51 +09:00
Sutou Kouhei
ebf14d01ba [ruby/fiddle] Add support for "long" variants
GitHub: fix https://github.com/ruby/fiddle/pull/100

Reported by David M. Lary. Thanks!!!

516333dd78
2023-06-28 16:36:50 +09:00
Aaron Patterson
85937f3a0a [ruby/fiddle] Add a helper method for reading/writing memory
(https://github.com/ruby/fiddle/pull/123)

This commit adds two new methods, `Fiddle::Pointer.read` and
`Fiddle::Pointer.write`. Both methods take an address, and will read or
write bytes at that address respectively.

For example we can read from an address without making a Pointer object:

```ruby
Fiddle::Pointer.read(address, 5) # read 5 bytes
```

We can also write to an address without allocating a Pointer object:

```ruby
Fiddle::Pointer.write(address, "bytes") # write 5 bytes
```

This allows us to read / write memory at arbitrary addresses without
instantiating a new `Fiddle::Pointer` object.

Examples where this API would be useful
[1](f03481d28b/lib/tenderjit/fiddle_hacks.rb (L26-L28))
[2](77c8daa2d4/lib/ruby_vm/rjit/c_pointer.rb (L193))
[3](77c8daa2d4/lib/ruby_vm/rjit/c_pointer.rb (L284))

I also added a writer method for the same reasons as the reader.

---------

04238cefed

Co-authored-by: Sutou Kouhei <kou@clear-code.com>
2023-06-28 16:36:49 +09:00
Hiroshi SHIBATA
286812bcf3 Merge fiddle-1.1.1 2022-12-09 16:36:22 +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
John Paul Adrian Glaubitz
93da67d463 [ruby/fiddle] Fix filenames for glibc SO files on alpha and ia64
(https://github.com/ruby/fiddle/pull/105)

Fixes [Bug #18645]

9a5a1dab1d
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
92f0c53934 [ruby/fiddle] test: don't use assert_true/assert_false
GitHub: GH-102

They aren't available in ruby/ruby.

ced671e43b
2022-10-07 15:18:54 +09:00
Sutou Kouhei
9f62768e51 [ruby/fiddle] test: ensure freeing closure
GitHub: GH-102

b2fef1770d
2022-10-07 15:18:53 +09:00
Sutou Kouhei
824c474c95 [ruby/fiddle] test: ensure freeing closure
GitHub: GH-102

This also improves freed closures assertions.

0495624caf
2022-10-07 15:18:53 +09:00
Sutou Kouhei
dfca6a8799 [ruby/fiddle] test: don't use power-assert
It seems that we can't use it in ruby/ruby.

e1221297fb
2022-10-07 15:18:52 +09:00
Sutou Kouhei
7c33141293 [ruby/fiddle] test: ensure freeing closure
GitHub: GH-102

This also improves freed closures assertions.

f6431f3cf8
2022-10-07 15:18:52 +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
Sutou Kouhei
191b91f47a [ruby/fiddle] test: suppress a warning
test/fiddle/test_import.rb:138: warning:
    ambiguous first argument; put parentheses or a space even after `-' operator

060eef76ad
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
Sutou Kouhei
6d01b66764 [ruby/fiddle] test: ensure GC-ing closures
GitHub: fix GH-102

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!!!

1343ac7a95
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
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
Nobuyoshi Nakada
bcc5a2b67e [ruby/fiddle] Simplify libc and libm path logics (https://github.com/ruby/fiddle/pull/91)
* Simplify libc_so and libm_so

If nil, no need to set to nil.

* Get rid of repeating inversions

4323e689d8
2021-08-24 16:18:24 +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
Nobuyoshi Nakada
f347b586fb
[ruby/fiddle] Handle#file_name results in very platform dependent 2021-07-15 09:49:56 +09:00
Nobuyoshi Nakada
c67c83fb68
[ruby/fiddle] Module file name may be the realpath
Even when the path which was used to dlopen may be a symlink.
2021-07-15 09:20:10 +09:00
Nobuyoshi Nakada
2fa3209a35
[ruby/fiddle] fixed the test on case-insensitive filesystem 2021-07-14 22:00:56 +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
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
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
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
8c905349bb
[ruby/fiddle] test: fix SetLastError's input type
ca5e6a0404
2021-07-13 19:37:45 +09:00
Sutou Kouhei
37d16bb9dc
[ruby/fiddle] test: use double quote for string literal
fab7eab95b
2021-07-13 19:37:45 +09:00
Sutou Kouhei
5516d74ad2
[ruby/fiddle] test: add a test for win32_last_socket_error
c86cec03cd
2021-07-13 19:37:45 +09:00
Sutou Kouhei
303ab5da8b
[ruby/fiddle] test: add missing receiver
1da3b4af16
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
Nobuyoshi Nakada
d172f8ac09
Skip fiddle tests if fiddle is not avaiable 2021-07-10 19:14:40 +09:00
Jeremy Evans
2579593a56 [ruby/fiddle] Do not use a libdir for glibc, it breaks Linux PPC64 (#70)
Fixes [Bug #12666]

a267a40be7
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
881b2dc898 [ruby/fiddle] closure: add support for const char *
GitHub: fix GH-62

Reported by Cody Krieger. Thanks!!!

284b820f2d
2021-05-18 12:48:40 +09:00
Sutou Kouhei
b2de5999d8 [ruby/fiddle] closure: accept symbol as type
dc2da6633e
2021-05-18 12:48:40 +09:00
Aaron Patterson
d45466dc5b
Oops! Add another test and fix to_proc implementation 2021-02-26 10:06:56 -08:00
Aaron Patterson
0590e9b677
Fiddle::Function responds to to_proc
This lets us cast a Fiddle::Function to a block, allowing is to write
things like:

```ruby
f = Fiddle::Function.new(@libc['strcpy'], [TYPE_VOIDP, TYPE_VOIDP], TYPE_VOIDP)
define_method :strcpy, &f
```
2021-02-26 09:57:13 -08:00
Kenta Murata
74652e640a
[memory_view][fiddle] Rename len to byte_size in rb_memory_view_t 2020-12-23 09:24:53 +09:00
Zoltán Mizsei
8f6cb5b70b
TEST: multiarch support for Haiku 2020-12-15 23:13:24 +09:00
Nobuyoshi Nakada
eea756ac86
Strip trailing spaces [ci skip] 2020-12-11 23:14:36 +09:00
Kenta Murata
9b0c36b390
Import fiddle-1.0.4 (#3860)
I don't use tool/sync_default_gem.rb because the last sync was incomplete.

Co-authored-by: Hiroshi SHIBATA <hsbt@ruby-lang.org>
Co-authored-by: Alan Wu <XrXr@users.noreply.github.com>
Co-authored-by: sinisterchipmunk <sinisterchipmunk@gmail.com>
Co-authored-by: Sutou Kouhei <kou@clear-code.com>
2020-12-11 09:41:12 +09:00
Kenta Murata
2427393b4d
Revert "test/fiddle/helper.rb: remove duplication (#3863)" (#3865)
This reverts commit bd47a8d660.

`libc_so` and `libm_so` are `nil` at line 124 because Big Sur doesn't have `/usr/lib/libSystem.B.dylib`.
The reassignment at line 127 is necessary in this case.
2020-12-08 21:58:08 +09:00