Skip to content

Commit a186963

Browse files
committed
[GR-10320,GR-14696] Update return types and fix unsigned return values in Truffle::POSIX
PullRequest: truffleruby/135
2 parents 8949ef1 + 92ce89d commit a186963

File tree

5 files changed

+63
-3
lines changed

5 files changed

+63
-3
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ src/main/c/truffleposix/truffleposix.o
4444
src/main/c/truffleposix/libtruffleposix.so
4545
src/main/c/truffleposix/libtruffleposix.dylib
4646

47+
spec/truffle/fixtures/libtestnfi.so
48+
spec/truffle/fixtures/libtestnfi.dylib
49+
4750
test/truffle/cexts/**/Makefile
4851
test/truffle/cexts/**/*.bc
4952
test/truffle/cexts/**/*.su

spec/truffle.mspec

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,9 @@ if i = ARGV.index('slow') and ARGV[i-1] == '--excl-tag' and MSpecScript.child_pr
188188
]
189189

190190
module Kernel
191+
alias_method :mspec_old_system, :system
192+
private :mspec_old_system
193+
191194
alias_method :"mspec_old_`", :`
192195
private :"mspec_old_`"
193196
end

spec/truffle/fixtures/libtestnfi.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#include <limits.h>
2+
3+
unsigned short max_ushort(void) {
4+
return USHRT_MAX;
5+
}
6+
7+
unsigned int max_uint(void) {
8+
return UINT_MAX;
9+
}

spec/truffle/posix_spec.rb

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Copyright (c) 2019 Oracle and/or its affiliates. All rights reserved. This
2+
# code is released under a tri EPL/GPL/LGPL license. You can use it,
3+
# redistribute it and/or modify it under the terms of the:
4+
#
5+
# Eclipse Public License version 1.0, or
6+
# GNU General Public License version 2, or
7+
# GNU Lesser General Public License version 2.1.
8+
9+
require_relative '../ruby/spec_helper'
10+
11+
describe "Trffle::POSIX returns the correct value for an identity function returning" do
12+
before :all do
13+
src = fixture __FILE__, "libtestnfi.c"
14+
lib = src[0...-1] + RbConfig::CONFIG['NATIVE_DLEXT']
15+
unless system "cc", "-shared", "-o", lib, src
16+
abort "Could not compile libtestnfi"
17+
end
18+
19+
lazy_library = Truffle::POSIX::LazyLibrary.new do
20+
Polyglot.eval('nfi', "load '#{lib}'")
21+
end
22+
23+
@libtestnfi = Module.new do
24+
Truffle::POSIX.attach_function :max_ushort, [], :ushort, lazy_library, false, :max_ushort, self
25+
Truffle::POSIX.attach_function :max_uint, [], :uint, lazy_library, false, :max_uint, self
26+
end
27+
end
28+
29+
it "the maximum unsigned short" do
30+
@libtestnfi.max_ushort.should == 65535
31+
end
32+
33+
it "the maximum unsigned int" do
34+
@libtestnfi.max_uint.should == 4294967295
35+
end
36+
end

src/main/ruby/core/posix.rb

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,9 @@ def self.attach_function_eagerly(native_name, argument_types, return_type,
117117
string_args.freeze
118118

119119
nfi_return_type = to_nfi_type(return_type)
120+
if nfi_return_type.to_s.start_with?('uint')
121+
unsigned_return_type = 1 << nfi_return_type[-2..-1].to_i
122+
end
120123

121124
bound_func = func.bind("(#{nfi_args_types.join(',')}):#{nfi_return_type}")
122125

@@ -151,6 +154,12 @@ def self.attach_function_eagerly(native_name, argument_types, return_type,
151154
result = Truffle::FFI::Pointer.new(Truffle::Interop.as_pointer(result))
152155
elsif return_type == :ssize_t
153156
result = Truffle.invoke_primitive(:integer_lower, result)
157+
elsif unsigned_return_type
158+
if result >= 0
159+
result
160+
else
161+
result += unsigned_return_type
162+
end
154163
end
155164

156165
result
@@ -181,7 +190,7 @@ def self.attach_function_eagerly(native_name, argument_types, return_type,
181190
attach_function :fcntl, [:int, :int, :int], :int
182191
attach_function :truffleposix_flock, [:int, :int], :int, LIBTRUFFLEPOSIX, true
183192
attach_function :truffleposix_fstat, [:int, :pointer], :int, LIBTRUFFLEPOSIX
184-
attach_function :truffleposix_fstat_mode, [:int], :long, LIBTRUFFLEPOSIX
193+
attach_function :truffleposix_fstat_mode, [:int], :mode_t, LIBTRUFFLEPOSIX
185194
attach_function :truffleposix_fstat_size, [:int], :long, LIBTRUFFLEPOSIX
186195
attach_function :fsync, [:int], :int
187196
attach_function :ftruncate, [:int, :off_t], :int
@@ -193,7 +202,7 @@ def self.attach_function_eagerly(native_name, argument_types, return_type,
193202
attach_function :link, [:string, :string], :int
194203
attach_function :lseek, [:int, :off_t, :int], :off_t
195204
attach_function :truffleposix_lstat, [:string, :pointer], :int, LIBTRUFFLEPOSIX
196-
attach_function :truffleposix_lstat_mode, [:string], :long, LIBTRUFFLEPOSIX
205+
attach_function :truffleposix_lstat_mode, [:string], :mode_t, LIBTRUFFLEPOSIX
197206
attach_function :truffleposix_major, [:dev_t], :uint, LIBTRUFFLEPOSIX
198207
attach_function :truffleposix_minor, [:dev_t], :uint, LIBTRUFFLEPOSIX
199208
attach_function :mkdir, [:string, :mode_t], :int
@@ -210,7 +219,7 @@ def self.attach_function_eagerly(native_name, argument_types, return_type,
210219
attach_function :seekdir, [:pointer, :long], :void
211220
attach_function :truffleposix_select, [:int, :pointer, :int, :pointer, :int, :pointer, :long], :int, LIBTRUFFLEPOSIX
212221
attach_function :truffleposix_stat, [:string, :pointer], :int, LIBTRUFFLEPOSIX
213-
attach_function :truffleposix_stat_mode, [:string], :long, LIBTRUFFLEPOSIX
222+
attach_function :truffleposix_stat_mode, [:string], :mode_t, LIBTRUFFLEPOSIX
214223
attach_function :truffleposix_stat_size, [:string], :long, LIBTRUFFLEPOSIX
215224
attach_function :symlink, [:string, :string], :int
216225
attach_function :telldir, [:pointer], :long

0 commit comments

Comments
 (0)