mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
c39bdb798d
* vm_core.h (rb_vm_t): move `rb_execution_context_t::safe_level` to `rb_vm_t::safe_level_` because `$SAFE` is a process (VM) global state. * vm_core.h (rb_proc_t): remove `rb_proc_t::safe_level` because `Proc` objects don't need to keep `$SAFE` at the creation. Also make `is_from_method` and `is_lambda` as 1 bit fields. * cont.c (cont_restore_thread): no need to keep `$SAFE` for Continuation. * eval.c (ruby_cleanup): use `rb_set_safe_level_force()` instead of access `vm->safe_level_` directly. * eval_jump.c: End procs `END{}` doesn't keep `$SAFE`. * proc.c (proc_dup): removed and introduce `rb_proc_dup` in vm.c. * safe.c (rb_set_safe_level): don't check `$SAFE` 1 -> 0 changes. * safe.c (safe_setter): use `rb_set_safe_level()`. * thread.c (rb_thread_safe_level): `Thread#safe_level` returns `$SAFE`. It should be obsolete. * transcode.c (load_transcoder_entry): `rb_safe_level()` only returns 0 or 1 so that this check is not needed. * vm.c (vm_proc_create_from_captured): don't need to keep `$SAFE` for Proc. * vm.c (rb_proc_create): renamed to `proc_create`. * vm.c (rb_proc_dup): moved from proc.c. * vm.c (vm_invoke_proc): do not need to set and restore `$SAFE` for `Proc#call`. * vm_eval.c (rb_eval_cmd): rename a local variable to represent clearer meaning. * lib/drb/drb.rb: restore `$SAFE`. * lib/erb.rb: restore `$SAFE`, too. * test/lib/leakchecker.rb: check `$SAFE == 0` at the end of tests. * test/rubygems/test_gem.rb: do not set `$SAFE = 1`. * bootstraptest/test_proc.rb: catch up this change. * spec/ruby/optional/capi/string_spec.rb: ditto. * test/bigdecimal/test_bigdecimal.rb: ditto. * test/fiddle/test_func.rb: ditto. * test/fiddle/test_handle.rb: ditto. * test/net/imap/test_imap_response_parser.rb: ditto. * test/pathname/test_pathname.rb: ditto. * test/readline/test_readline.rb: ditto. * test/ruby/test_file.rb: ditto. * test/ruby/test_optimization.rb: ditto. * test/ruby/test_proc.rb: ditto. * test/ruby/test_require.rb: ditto. * test/ruby/test_thread.rb: ditto. * test/rubygems/test_gem_specification.rb: ditto. * test/test_tempfile.rb: ditto. * test/test_tmpdir.rb: ditto. * test/win32ole/test_win32ole.rb: ditto. * test/win32ole/test_win32ole_event.rb: ditto. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@61510 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
198 lines
5.7 KiB
Ruby
198 lines
5.7 KiB
Ruby
# frozen_string_literal: true
|
|
begin
|
|
require_relative 'helper'
|
|
rescue LoadError
|
|
end
|
|
|
|
module Fiddle
|
|
class TestHandle < TestCase
|
|
include Fiddle
|
|
|
|
def test_safe_handle_open
|
|
Thread.new do
|
|
$SAFE = 1
|
|
assert_raise(SecurityError) {
|
|
Fiddle::Handle.new(LIBC_SO.dup.taint)
|
|
}
|
|
end.join
|
|
ensure
|
|
$SAFE = 0
|
|
end
|
|
|
|
def test_safe_function_lookup
|
|
Thread.new do
|
|
h = Fiddle::Handle.new(LIBC_SO)
|
|
$SAFE = 1
|
|
assert_raise(SecurityError) {
|
|
h["qsort".dup.taint]
|
|
}
|
|
end.join
|
|
ensure
|
|
$SAFE = 0
|
|
end
|
|
|
|
def test_to_i
|
|
handle = Fiddle::Handle.new(LIBC_SO)
|
|
assert_kind_of Integer, handle.to_i
|
|
end
|
|
|
|
def test_static_sym_unknown
|
|
assert_raise(DLError) { Fiddle::Handle.sym('fooo') }
|
|
assert_raise(DLError) { Fiddle::Handle['fooo'] }
|
|
end
|
|
|
|
def test_static_sym
|
|
begin
|
|
# Linux / Darwin / FreeBSD
|
|
refute_nil Fiddle::Handle.sym('dlopen')
|
|
assert_equal Fiddle::Handle.sym('dlopen'), Fiddle::Handle['dlopen']
|
|
return
|
|
rescue
|
|
end
|
|
|
|
begin
|
|
# NetBSD
|
|
require '-test-/dln/empty'
|
|
refute_nil Fiddle::Handle.sym('Init_empty')
|
|
assert_equal Fiddle::Handle.sym('Init_empty'), Fiddle::Handle['Init_empty']
|
|
return
|
|
rescue
|
|
end
|
|
end unless /mswin|mingw/ =~ RUBY_PLATFORM
|
|
|
|
def test_sym_closed_handle
|
|
handle = Fiddle::Handle.new(LIBC_SO)
|
|
handle.close
|
|
assert_raise(DLError) { handle.sym("calloc") }
|
|
assert_raise(DLError) { handle["calloc"] }
|
|
end
|
|
|
|
def test_sym_unknown
|
|
handle = Fiddle::Handle.new(LIBC_SO)
|
|
assert_raise(DLError) { handle.sym('fooo') }
|
|
assert_raise(DLError) { handle['fooo'] }
|
|
end
|
|
|
|
def test_sym_with_bad_args
|
|
handle = Handle.new(LIBC_SO)
|
|
assert_raise(TypeError) { handle.sym(nil) }
|
|
assert_raise(TypeError) { handle[nil] }
|
|
end
|
|
|
|
def test_sym
|
|
handle = Handle.new(LIBC_SO)
|
|
refute_nil handle.sym('calloc')
|
|
refute_nil handle['calloc']
|
|
end
|
|
|
|
def test_handle_close
|
|
handle = Handle.new(LIBC_SO)
|
|
assert_equal 0, handle.close
|
|
end
|
|
|
|
def test_handle_close_twice
|
|
handle = Handle.new(LIBC_SO)
|
|
handle.close
|
|
assert_raise(DLError) do
|
|
handle.close
|
|
end
|
|
end
|
|
|
|
def test_dlopen_returns_handle
|
|
assert_instance_of Handle, dlopen(LIBC_SO)
|
|
end
|
|
|
|
def test_initialize_noargs
|
|
handle = Handle.new
|
|
refute_nil handle['rb_str_new']
|
|
end
|
|
|
|
def test_initialize_flags
|
|
handle = Handle.new(LIBC_SO, RTLD_LAZY | RTLD_GLOBAL)
|
|
refute_nil handle['calloc']
|
|
end
|
|
|
|
def test_enable_close
|
|
handle = Handle.new(LIBC_SO)
|
|
assert !handle.close_enabled?, 'close is enabled'
|
|
|
|
handle.enable_close
|
|
assert handle.close_enabled?, 'close is not enabled'
|
|
end
|
|
|
|
def test_disable_close
|
|
handle = Handle.new(LIBC_SO)
|
|
|
|
handle.enable_close
|
|
assert handle.close_enabled?, 'close is enabled'
|
|
handle.disable_close
|
|
assert !handle.close_enabled?, 'close is enabled'
|
|
end
|
|
|
|
def test_NEXT
|
|
begin
|
|
# Linux / Darwin
|
|
#
|
|
# There are two special pseudo-handles, RTLD_DEFAULT and RTLD_NEXT. The former will find
|
|
# the first occurrence of the desired symbol using the default library search order. The
|
|
# latter will find the next occurrence of a function in the search order after the current
|
|
# library. This allows one to provide a wrapper around a function in another shared
|
|
# library.
|
|
# --- Ubuntu Linux 8.04 dlsym(3)
|
|
handle = Handle::NEXT
|
|
refute_nil handle['malloc']
|
|
return
|
|
rescue
|
|
end
|
|
|
|
begin
|
|
# BSD
|
|
#
|
|
# If dlsym() is called with the special handle RTLD_NEXT, then the search
|
|
# for the symbol is limited to the shared objects which were loaded after
|
|
# the one issuing the call to dlsym(). Thus, if the function is called
|
|
# from the main program, all the shared libraries are searched. If it is
|
|
# called from a shared library, all subsequent shared libraries are
|
|
# searched. RTLD_NEXT is useful for implementing wrappers around library
|
|
# functions. For example, a wrapper function getpid() could access the
|
|
# "real" getpid() with dlsym(RTLD_NEXT, "getpid"). (Actually, the dlfunc()
|
|
# interface, below, should be used, since getpid() is a function and not a
|
|
# data object.)
|
|
# --- FreeBSD 8.0 dlsym(3)
|
|
require '-test-/dln/empty'
|
|
handle = Handle::NEXT
|
|
refute_nil handle['Init_empty']
|
|
return
|
|
rescue
|
|
end
|
|
end unless /mswin|mingw/ =~ RUBY_PLATFORM
|
|
|
|
def test_DEFAULT
|
|
handle = Handle::DEFAULT
|
|
refute_nil handle['malloc']
|
|
end unless /mswin|mingw/ =~ RUBY_PLATFORM
|
|
|
|
def test_dlerror
|
|
# FreeBSD (at least 7.2 to 7.2) calls nsdispatch(3) when it calls
|
|
# getaddrinfo(3). And nsdispatch(3) doesn't call dlerror(3) even if
|
|
# it calls _nss_cache_cycle_prevention_function with dlsym(3).
|
|
# So our Fiddle::Handle#sym must call dlerror(3) before call dlsym.
|
|
# In general uses of dlerror(3) should call it before use it.
|
|
require 'socket'
|
|
Socket.gethostbyname("localhost")
|
|
Fiddle.dlopen("/lib/libc.so.7").sym('strcpy')
|
|
end if /freebsd/=~ RUBY_PLATFORM
|
|
|
|
def test_no_memory_leak
|
|
assert_no_memory_leak(%w[-W0 -rfiddle.so], '', '100_000.times {Fiddle::Handle.allocate}; GC.start', rss: true)
|
|
end
|
|
|
|
if /cygwin|mingw|mswin/ =~ RUBY_PLATFORM
|
|
def test_fallback_to_ansi
|
|
k = Fiddle::Handle.new("kernel32.dll")
|
|
ansi = k["GetFileAttributesA"]
|
|
assert_equal(ansi, k["GetFileAttributes"], "should fallback to ANSI version")
|
|
end
|
|
end
|
|
end
|
|
end if defined?(Fiddle)
|