ruby-changes:13807
From: tenderlove <ko1@a...>
Date: Sun, 1 Nov 2009 10:46:37 +0900 (JST)
Subject: [ruby-changes:13807] Ruby:r25605 (trunk): * test/dl/test_dl2.rb (**) testing malloc and realloc
tenderlove 2009-11-01 10:46:27 +0900 (Sun, 01 Nov 2009) New Revision: 25605 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=25605 Log: * test/dl/test_dl2.rb (**) testing malloc and realloc * ext/dl/dl.c (**) adding documentation Modified files: trunk/ext/dl/dl.c trunk/test/dl/test_dl2.rb Index: ext/dl/dl.c =================================================================== --- ext/dl/dl.c (revision 25604) +++ ext/dl/dl.c (revision 25605) @@ -16,6 +16,12 @@ 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 @@ 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) { Index: test/dl/test_dl2.rb =================================================================== --- test/dl/test_dl2.rb (revision 25604) +++ test/dl/test_dl2.rb (revision 25605) @@ -3,6 +3,53 @@ 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!*")) -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/