ffi-extra 0.0.3.1 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/ffi/executable_memory.rb +78 -0
  2. metadata +28 -35
@@ -0,0 +1,78 @@
1
+ #--
2
+ # DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
3
+ # Version 2, December 2004
4
+ #
5
+ # Copyleft meh. [http://meh.paranoid.pk | meh@paranoici.org]
6
+ #
7
+ # DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
8
+ # TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
9
+ #
10
+ # 0. You just DO WHAT THE FUCK YOU WANT TO.
11
+ #++
12
+
13
+ require 'ffi/extra'
14
+
15
+ module FFI
16
+
17
+ class ExecutableMemoryPointer < Pointer
18
+ module C
19
+ extend FFI::Library
20
+
21
+ ffi_lib FFI::Library::LIBC
22
+
23
+ attach_function :mmap, [:pointer, :size_t, :int, :int, :int, :off_t], :pointer
24
+ attach_function :munmap, [:pointer, :size_t], :int
25
+ attach_function :getpagesize, [], :int
26
+ end
27
+
28
+ def self.round_up (base)
29
+ page_size = C.getpagesize
30
+ over = base % page_size
31
+
32
+ base + (over > 0 ? page_size - over : 0)
33
+ end
34
+
35
+ def self.alloc (size)
36
+ size = round_up(size)
37
+ base = C.mmap(nil, size, 0x01 | 0x02 | 0x04, 0x02 | 0x20, -1, 0)
38
+ # PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE | MAP_ANONYMOUS
39
+
40
+ return nil if base.address == -1
41
+
42
+ return base, size
43
+ end
44
+
45
+ def self.free (pointer, size)
46
+ C.munmap(address, size)
47
+ end
48
+
49
+ def self.from_string (string)
50
+ new(string.size).tap {|p|
51
+ p.write_string(string)
52
+ }
53
+ end
54
+
55
+ attr_reader :size
56
+
57
+ def initialize (size)
58
+ address, size = self.class.alloc(size)
59
+
60
+ ObjectSpace.define_finalizer self, self.class.finalizer(address, size)
61
+
62
+ super(address)
63
+
64
+ @size = size
65
+ end
66
+
67
+ def self.finalizer (address, size)
68
+ proc {
69
+ self.free(address, size)
70
+ }
71
+ end
72
+
73
+ def inspect
74
+ "#<FFI::ExecutableMemoryPointer address=0x#{'%x' % address} size=#{size}>"
75
+ end
76
+ end
77
+
78
+ end
metadata CHANGED
@@ -1,64 +1,57 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: ffi-extra
3
- version: !ruby/object:Gem::Version
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.4
4
5
  prerelease:
5
- version: 0.0.3.1
6
6
  platform: ruby
7
- authors:
7
+ authors:
8
8
  - meh.
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
-
13
- date: 2011-09-21 00:00:00 Z
14
- dependencies:
15
- - !ruby/object:Gem::Dependency
12
+ date: 2011-10-28 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
16
15
  name: ffi
17
- prerelease: false
18
- requirement: &id001 !ruby/object:Gem::Requirement
16
+ requirement: &15812500 !ruby/object:Gem::Requirement
19
17
  none: false
20
- requirements:
21
- - - ">="
22
- - !ruby/object:Gem::Version
23
- version: "0"
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
24
22
  type: :runtime
25
- version_requirements: *id001
23
+ prerelease: false
24
+ version_requirements: *15812500
26
25
  description:
27
26
  email: meh@paranoici.org
28
27
  executables: []
29
-
30
28
  extensions: []
31
-
32
29
  extra_rdoc_files: []
33
-
34
- files:
30
+ files:
35
31
  - lib/ffi/extra.rb
32
+ - lib/ffi/executable_memory.rb
36
33
  homepage: http://github.com/meh/ruby-ffi-extra
37
34
  licenses: []
38
-
39
35
  post_install_message:
40
36
  rdoc_options: []
41
-
42
- require_paths:
37
+ require_paths:
43
38
  - lib
44
- required_ruby_version: !ruby/object:Gem::Requirement
39
+ required_ruby_version: !ruby/object:Gem::Requirement
45
40
  none: false
46
- requirements:
47
- - - ">="
48
- - !ruby/object:Gem::Version
49
- version: "0"
50
- required_rubygems_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ! '>='
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
46
  none: false
52
- requirements:
53
- - - ">="
54
- - !ruby/object:Gem::Version
55
- version: "0"
47
+ requirements:
48
+ - - ! '>='
49
+ - !ruby/object:Gem::Version
50
+ version: '0'
56
51
  requirements: []
57
-
58
52
  rubyforge_project:
59
- rubygems_version: 1.8.5
53
+ rubygems_version: 1.8.10
60
54
  signing_key:
61
55
  specification_version: 3
62
56
  summary: Some extra methods for FFI
63
57
  test_files: []
64
-