-
-
Notifications
You must be signed in to change notification settings - Fork 928
add integer pointer memory strategy #8423
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
@headius I've added a naive integer pointer parameter strategy, but it's causing a segfault. I thought simply wrapping the integer using NativeMemoryIO would work, but apparently not. Any ideas? On a promising note, it seems to allow some of mittsu's examples to run, although most of them do segfault. |
Hmm I would have expected that to work ok. Can you point me toward a segfault report or show me how to generate the same error? |
f8a360e
to
9f6e0c5
Compare
@headius You can trigger a segfault by just running the ffi specs. I don't even know which spec is actually failing since it crashes the whole VM. Also I haven't figured out how to debug JNI yet, any tips? In the meantime, I've managed a workaround in Fiddle's FFI backend: ruby/fiddle#162 |
@danini-the-panini Looking into this today. If you can stop by our Matrix channel I'll be in the office. |
Deleted previous repro and stack because it was reflecting known failures on macOS with function callbacks (see #6995). I dug out the first failure in the FFI suite, and I think this is a failure due to integers now looking like they could be used as a valid pointer. New repro and stack: require 'ffi'
require 'delegate'
module PointerTestLib
extend FFI::Library
ffi_lib "spec/ffi/fixtures/libtest.so"
attach_function :ptr_ret_int32_t, [ :pointer, :int ], :int
end
PointerTestLib.ptr_ret_int32_t(0, 0) This is from the "Fixnum cannot be used as a Pointer argument" spec in spec/ffi/pointer_spec.rb. It properly raised the error before because Integers (Fixnum is the old name for 64-bit range Integers) could not be converted to a pointer. Now that you added such a strategy, it goes ahead and tries to pointerify the zero value in the first argument to ptr_ret_int32, and that naturally blows up when accessed. The JVM trace is below:
@danini-the-panini Everything seems like it's actually working correctly! The problem is that now it accepts integer values for pointer arguments, but most places you pass a pointer it should apparently NOT accept an integer (according to FFI specs). |
@danini-the-panini Can you show me what this change fixed in your codebase? I'm wondering if it's perhaps a feature that Fiddle supports but FFI is supposed to error, so this spec is right for FFI but prevents some functionality Fiddle needs. |
@headius ok that makes sense, I had a sneaky suspicion this wasn't going to be the correct fix. I had tried something similar by just adding Fiddle does seem to allow integers as pointers in CRuby, and that is used extensively in mittsu-opengl since OpenGL has a few methods that have VOIDP arguments that are actually just used as byte offsets e.g. glVertexAttribPointer Here is an example use in Mittsu: https://github.com/danini-the-panini/mittsu-opengl/blob/a69434802b183577a6f0f770ca17de9d9a48d37f/lib/mittsu/opengl/geometry_like.rb#L43 In light of FFI's spec, I'll close this and finish up the fix in Fiddle's ffi_backend |
Interestingly, I managed yesterday to run all of Mittsu's examples using this patch instead of the ffi_backend fix, and I didn't get any segfaults. This is on Apple Silicon (M2 Pro) with callbacks 🤔 |
@danini-the-panini I don't think anything is wrong with the patch other than suddenly allowing integers to be passed for pointer arguments. That may be a valid feature to add, or it may lead to confusion if someone passes an integer they don't expect to become a pointer. Perhaps open an issue with ffi/ffi and we can discuss there with other stakeholders? |
@headius Fair enough. It appears this was a feature that was removed for "JRuby compat" ffi/ffi@59b48a9 |
cc383e4
to
628a83e
Compare
628a83e
to
078e4e2
Compare
Closing this since ffi/ffi#1130 was closed |
This allows for passing integers as pointer arguments to functions when using the FFI backend. This is a workaround until we can get JRuby's FFI implementation to allow for it directly (see also jruby/jruby#8423) --------- Co-authored-by: Benoit Daloze <[email protected]>
(ruby/fiddle#162) This allows for passing integers as pointer arguments to functions when using the FFI backend. This is a workaround until we can get JRuby's FFI implementation to allow for it directly (see also jruby/jruby#8423) --------- ruby/fiddle@e2f0952e9b Co-authored-by: Benoit Daloze <[email protected]>
Allows implicitly passing integers into FFI functions that take pointers, using the passed in integer as the address.
This mimics the behavior of CRuby.