aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--ext/dl/dl.c13
-rw-r--r--test/dl/test_dl2.rb47
2 files changed, 60 insertions, 0 deletions
diff --git a/ext/dl/dl.c b/ext/dl/dl.c
index 6bef7bb326..beaed84393 100644
--- a/ext/dl/dl.c
+++ b/ext/dl/dl.c
@@ -16,6 +16,12 @@ rb_dl_dlopen(int argc, VALUE argv[], VALUE self)
return rb_class_new_instance(argc, argv, rb_cDLHandle);
}
+/*
+ * call-seq: DL.malloc
+ *
+ * Allocate +size+ bytes of memory and return the integer memory address
+ * for the allocated memory.
+ */
VALUE
rb_dl_malloc(VALUE self, VALUE size)
{
@@ -26,6 +32,13 @@ rb_dl_malloc(VALUE self, VALUE size)
return PTR2NUM(ptr);
}
+/*
+ * call-seq: DL.realloc(addr, size)
+ *
+ * Change the size of the memory allocated at the memory location +addr+ to
+ * +size+ bytes. Returns the memory address of the reallocated memory, which
+ * may be different than the address passed in.
+ */
VALUE
rb_dl_realloc(VALUE self, VALUE addr, VALUE size)
{
diff --git a/test/dl/test_dl2.rb b/test/dl/test_dl2.rb
index 5a9daba42a..d391e976e5 100644
--- a/test/dl/test_dl2.rb
+++ b/test/dl/test_dl2.rb
@@ -3,6 +3,53 @@ require 'dl/callback'
module DL
class TestDL < TestBase
+ # TODO: refactor test repetition
+
+ def test_realloc
+ str = "abc"
+ ptr_id = DL.realloc(0, 4)
+ ptr = CPtr.new(ptr_id, 4)
+
+ assert_equal ptr_id, ptr.to_i
+
+ cfunc = CFunc.new(@libc['strcpy'], TYPE_VOIDP, 'strcpy')
+ x = cfunc.call([ptr_id,str].pack("l!p").unpack("l!*"))
+ assert_equal("abc\0", ptr[0,4])
+ DL.free ptr_id
+ end
+
+ def test_realloc_secure
+ assert_raises(SecurityError) do
+ Thread.new do
+ $SAFE = 4
+ DL.realloc(0, 4)
+ end.join
+ end
+ end
+
+ def test_malloc
+ str = "abc"
+
+ ptr_id = DL.malloc(4)
+ ptr = CPtr.new(ptr_id, 4)
+
+ assert_equal ptr_id, ptr.to_i
+
+ cfunc = CFunc.new(@libc['strcpy'], TYPE_VOIDP, 'strcpy')
+ x = cfunc.call([ptr_id,str].pack("l!p").unpack("l!*"))
+ assert_equal("abc\0", ptr[0,4])
+ DL.free ptr_id
+ end
+
+ def test_malloc_security
+ assert_raises(SecurityError) do
+ Thread.new do
+ $SAFE = 4
+ DL.malloc(4)
+ end.join
+ end
+ end
+
def test_call_int()
cfunc = CFunc.new(@libc['atoi'], TYPE_INT, 'atoi')
x = cfunc.call(["100"].pack("p").unpack("l!*"))