Skip to content
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

If you call a member of a structure many times, the value will change. #65

Closed
kojix2 opened this issue Jan 12, 2021 · 3 comments
Closed

Comments

@kojix2
Copy link

kojix2 commented Jan 12, 2021

Hi @kou and fiddle developers!

When I reference a member of a structure multiple times with Fiddle, I find that the value of the member of the structure changes.

The following sample has a high probability of changing values in my environment. (Not always)

require 'fiddle/import'

module A
  extend Fiddle::Importer
  S = struct [
    'int8_t* hoge',
    'int8_t* fuga']
end

s = A::S.malloc
s.hoge = [*1..10].pack('c*')
s.fuga = [*1..10].reverse.pack('c*')

a1 = s.fuga[0,10].unpack('c*')

1000.times do
  s.fuga[0,10].unpack('c*')
end

b1 = s.fuga[0,10].unpack('c*')

if a1 == b1
  puts "OK"
else
  p a1, b1
end

I have confirmed that this problem occurs in the following environment.

  • Ruby 2.7, 3.0 + Ubuntu + fiddle 1.0.7
  • Ruby 2.6 + Mac + fiddle 1.0.7

I also asked people in ruby-jp slack to try it. The problem is reproducible.

Thank you.

@chrisseaton
Copy link
Contributor

The value of the struct's member is not changing - it's a pointer. What is changing is the values in the memory that the pointer is pointing to.

I modified your code to print the actual value (the pointer) from the struct to show it does not change.

require 'fiddle/import'

module A
  extend Fiddle::Importer
  S = struct [
    'int8_t* hoge',
    'int8_t* fuga']
end

s = A::S.malloc
s.hoge = [*1..10].pack('c*')
s.fuga = [*1..10].reverse.pack('c*')

a1 = s.fuga#[0,10].unpack('c*')

1000.times do
  s.fuga#[0,10].unpack('c*')
end

b1 = s.fuga#[0,10].unpack('c*')

if a1 == b1
  puts "OK"
else
  p a1, b1
end

What your code does is to read the memory pointed to by the struct members. This memory changes, but that's nothing to do with the struct, all it knows is what memory to point at - it doesn't own or manage or control that memory, so it cannot stop it changing.

So if the struct doesn't own or manage the memory its members point two, who is managing it? When you write [*1..10].pack('c*'), you create a C string, and then you store the address of the C string's memory in that struct's member. The C string still owns the memory, not the struct.

From this point, nothing points at the C string. Ruby has a garage collector, so the C string and its memory are collected by the garbage collector and deleted. Now the struct's member points at the same memory, but it's not own by the string anymore.

Then someone else gets the same memory, writes in it, and see that it's change.

That's how your memory changes.

I don't really see a bug here - you're pointing to the internals of a string, but how do you expect your string to be kept alive?

@kojix2
Copy link
Author

kojix2 commented Jan 12, 2021

Thank you for your quick response.

Based on your answer, I assigned a value to the variable to protect it from GC.

s.hoge = memo1 =  [*1..10].pack('c*')
s.fuga = memo2 = [*1..10].reverse.pack('c*')

And, certainly, the value is no longer changed.

GC ... It's difficult for me. But this issue has been resolved. Thank you!

@kojix2 kojix2 closed this as completed Jan 12, 2021
@chrisseaton
Copy link
Contributor

Note that you should not modify memo1 after this point, because that may cause the string data to be reallocated and so moved.

If you don't modify it, then your solution is fine.

If you might modify it, then you should malloc the data that s.hoge points to yourself, and copy the string data out of the Ruby string and into the data you own.

kojix2 added a commit to kojix2/ruby-libssw that referenced this issue Jan 12, 2021
* Assign to an instance variable to prevent recovery by CG.
* ruby/fiddle#65
kojix2 added a commit to kojix2/ruby-libssw that referenced this issue Jan 12, 2021
* Assign to an instance variable to prevent recovery by CG.
* ruby/fiddle#65
kojix2 added a commit to kojix2/ruby-libssw that referenced this issue Jan 13, 2021
* Refer to strings as instance variables of pointers.
* This prevents the memory area of the string from being freed by the GC.
* ruby/fiddle#65
* Thanks @chrisseaton!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants