Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Make ruby workable with CALC_EXACT_MALLOC_SIZE set to 1 #78

Closed
wants to merge 10 commits into from

2 participants

Sokolov Yura NARUSE, Yui
Sokolov Yura

Fix inconsistent usage of xmalloc/xfree and malloc/free in ruby-trunk.
Errors founded with CALC_EXACT_MALLOC_SIZE set to 1.
make check now works on Ubuntu 11.04 without crashes and has exactly same failures as ruby-trunk.

Didn't test Windows related libraries.

Patch reverts Oniguruma to use ruby_xmalloc and ruby_xfree. I did not test how it affects performance.

NARUSE, Yui nurse commented on the diff
ext/syslog/syslog.c
@@ -49,7 +49,7 @@ static VALUE mSyslog_close(VALUE self)
closelog();
- free((void *)syslog_ident);
+ xfree((void *)syslog_ident);
NARUSE, Yui Collaborator

syslog_ident is allocated by malloc in strdup, so this should be wrong

strdup is overwritten by #include "ruby/util.h" on top of syslog.c

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
ext/dl/cfunc.c
@@ -163,7 +164,7 @@
sname = NIL_P(name) ? NULL : StringValuePtr(name);
TypedData_Get_Struct(self, struct cfunc_data, &dlcfunc_data_type, data);
- if( data->name ) xfree(data->name);
+ if( data->name ) free(data->name);
NARUSE, Yui Collaborator

I think data->name is not allocated anything, isn't it?

Well, yes. This line is a my mistake, which is not exposed cause data->name is not allocated at this point.
This is mistake, cause I already override strdup by including , so that I should revert to use xfree.

By the way: we could call #initialize on a live object, so that data->name could be initialized.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
NARUSE, Yui
Collaborator

I merged parts of this in r34238.

I don't merge regint.h because I hesitated.

I don't merge ruby_mimmalloc because I can't understand why it is needed, and temporally fix with free(3).

Sokolov Yura

I watched to r3428
cfunc.c and syslog.c should be also fixed, cause they produce SIGSEGV while make check.

Sokolov Yura

I don't merge ruby_mimmalloc because I can't understand why it is needed, and temporally fix with free(3).

Your "temporary fix" is not workable: make test fails on test_class.rb (SIGSEGV).
ruby_mimmalloc is unavoidable because only main thread is allocated with malloc, all other are allocated with ruby_xmalloc.

Sokolov Yura

Updated pull request #79

Sandor Szücs szuecs referenced this pull request from a commit in szuecs/ruby
NARUSE, Yui nurse * gc.c (rb_objspace_free): global_List is allocated with xmalloc.
  patched by Sokolov Yura.  ruby#78

* dln_find.c: remove useless replacement of free.

* ext/readline/readline.c (readline_attempted_completion_function):
  strings for readline must allocated with malloc.

* process.c (run_exec_dup2): use free; see also r20950.

* re.c (onig_new_with_source): use malloc for oniguruma.

* vm.c (ruby_vm_destruct): use free for VMs.

* vm.c (thread_free): use free for threads.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@34238 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
88b16ce
Aaron Patterson tenderlove referenced this pull request from a commit in tenderlove/ruby
NARUSE, Yui nurse * gc.c (rb_objspace_free): global_List is allocated with xmalloc.
  patched by Sokolov Yura.  ruby#78

* dln_find.c: remove useless replacement of free.

* ext/readline/readline.c (readline_attempted_completion_function):
  strings for readline must allocated with malloc.

* process.c (run_exec_dup2): use free; see also r20950.

* re.c (onig_new_with_source): use malloc for oniguruma.

* vm.c (ruby_vm_destruct): use free for VMs.

* vm.c (thread_free): use free for threads.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@34238 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
5e58aef
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 06, 2012
  1. Sokolov Yura

    fix rb_objspace_free

    funny-falcon authored
  2. Sokolov Yura

    fix Init_BareVM

    funny-falcon authored
  3. Sokolov Yura

    fix dln_find

    funny-falcon authored
  4. Sokolov Yura

    fix run_exec_dup2

    funny-falcon authored
  5. Sokolov Yura
  6. Sokolov Yura

    fix cfunc.c

    funny-falcon authored
  7. Sokolov Yura

    fix syslog.c

    funny-falcon authored
Commits on Jan 08, 2012
  1. Sokolov Yura

    better way to fix cfunc.c

    funny-falcon authored
  2. Sokolov Yura

    fix readline

    funny-falcon authored
Commits on Jan 09, 2012
  1. Sokolov Yura

    fix error in cfunc.c

    funny-falcon authored
This page is out of date. Refresh to see the latest.
8 dln_find.c
View
@@ -45,14 +45,6 @@ char *dln_argv0;
# include <strings.h>
#endif
-#ifndef xmalloc
-void *xmalloc();
-void *xcalloc();
-void *xrealloc();
-#endif
-
-#define free(x) xfree(x)
-
#include <stdio.h>
#if defined(_WIN32)
#include "missing/file.h"
1  ext/dl/cfunc.c
View
@@ -4,6 +4,7 @@
#include <ruby.h>
#include <errno.h>
+#include <ruby/util.h>
#include "dl.h"
VALUE rb_cDLCFunc;
6 ext/readline/readline.c
View
@@ -672,10 +672,10 @@ readline_attempted_completion_function(const char *text, int start, int end)
matches = RARRAY_LEN(ary);
if (matches == 0)
return NULL;
- result = ALLOC_N(char *, matches + 2);
+ result = (char**)malloc((matches + 2)*sizeof(char*));
for (i = 0; i < matches; i++) {
temp = rb_obj_as_string(RARRAY_PTR(ary)[i]);
- result[i + 1] = ALLOC_N(char, RSTRING_LEN(temp) + 1);
+ result[i + 1] = (char*)malloc(RSTRING_LEN(temp) + 1);
strcpy(result[i + 1], RSTRING_PTR(temp));
}
result[matches + 1] = NULL;
@@ -707,7 +707,7 @@ readline_attempted_completion_function(const char *text, int start, int end)
if (low > si) low = si;
i++;
}
- result[0] = ALLOC_N(char, low + 1);
+ result[0] = (char*)malloc(low + 1);
strncpy(result[0], result[1], low);
result[0][low] = '\0';
}
2  ext/syslog/syslog.c
View
@@ -49,7 +49,7 @@ static VALUE mSyslog_close(VALUE self)
closelog();
- free((void *)syslog_ident);
+ xfree((void *)syslog_ident);
NARUSE, Yui Collaborator

syslog_ident is allocated by malloc in strdup, so this should be wrong

strdup is overwritten by #include "ruby/util.h" on top of syslog.c

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
syslog_ident = NULL;
syslog_options = syslog_facility = syslog_mask = -1;
syslog_opened = 0;
19 gc.c
View
@@ -492,7 +492,7 @@ rb_objspace_free(rb_objspace_t *objspace)
struct gc_list *list, *next;
for (list = global_List; list; list = next) {
next = list->next;
- free(list);
+ ruby_xfree(list);
}
}
if (objspace->heap.sorted) {
@@ -908,6 +908,23 @@ ruby_xfree(void *x)
vm_xfree(&rb_objspace, x);
}
+/* Mimic ruby_xmalloc, but need not rb_objspace.
+ * should return pointer suitable for ruby_xfree
+ */
+void *
+ruby_mimmalloc(size_t size)
+{
+ void *mem;
+#if CALC_EXACT_MALLOC_SIZE
+ size += sizeof(size_t);
+#endif
+ mem = malloc(size);
+#if CALC_EXACT_MALLOC_SIZE
+ ((size_t *)mem)[0] = size;
+ mem = (size_t *)mem + 1;
+#endif
+ return mem;
+}
/*
* call-seq:
1  internal.h
View
@@ -93,6 +93,7 @@ void Init_File(void);
/* gc.c */
void Init_heap(void);
+void *ruby_mimmalloc(size_t size);
/* inits.c */
void rb_call_inits(void);
2  process.c
View
@@ -2031,7 +2031,7 @@ run_exec_dup2(VALUE ary, VALUE save, char *errmsg, size_t errmsg_buflen)
} *pairs = 0;
n = RARRAY_LEN(ary);
- pairs = (struct fd_pair *)malloc(sizeof(struct fd_pair) * n);
+ pairs = (struct fd_pair *)xmalloc(sizeof(struct fd_pair) * n);
if (pairs == NULL) {
ERRMSG("malloc");
return -1;
4 regint.h
View
@@ -84,10 +84,6 @@
/* escape other system UChar definition */
#ifndef RUBY_DEFINES_H
#include "ruby/ruby.h"
-#undef xmalloc
-#undef xrealloc
-#undef xcalloc
-#undef xfree
#endif
#ifdef ONIG_ESCAPE_UCHAR_COLLISION
#undef ONIG_ESCAPE_UCHAR_COLLISION
4 vm.c
View
@@ -2198,8 +2198,8 @@ void
Init_BareVM(void)
{
/* VM bootstrap: phase 1 */
- rb_vm_t * vm = malloc(sizeof(*vm));
- rb_thread_t * th = malloc(sizeof(*th));
+ rb_vm_t * vm = ruby_mimmalloc(sizeof(*vm));
+ rb_thread_t * th = ruby_mimmalloc(sizeof(*th));
if (!vm || !th) {
fprintf(stderr, "[FATAL] failed to allocate memory\n");
exit(EXIT_FAILURE);
Something went wrong with that request. Please try again.