Skip to content

Commit

Permalink
Rename functions in the binaries to be unique
Browse files Browse the repository at this point in the history
This might fix some flaky tests because Valgrind may be combining
functions in different binaries witht he same name.
  • Loading branch information
peterzhu2118 committed Jul 5, 2023
1 parent 3c189a1 commit 1749394
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 39 deletions.
28 changes: 14 additions & 14 deletions test/ruby_memcheck/ext/ruby_memcheck_c_test_one.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,41 @@

static VALUE cRubyMemcheckCTestOne;

static VALUE no_memory_leak(VALUE _)
static VALUE c_test_one_no_memory_leak(VALUE _)
{
return Qnil;
}

/* This function must not be inlined to ensure that it has a stack frame. */
static void __attribute__((noinline)) allocate_memory_leak(void)
static void __attribute__((noinline)) c_test_one_allocate_memory_leak(void)
{
volatile char *ptr = malloc(100);
ptr[0] = 'a';
}
}

static VALUE memory_leak(VALUE _)
static VALUE c_test_one_memory_leak(VALUE _)
{
allocate_memory_leak();
c_test_one_allocate_memory_leak();
return Qnil;
}

static VALUE use_after_free(VALUE _)
static VALUE c_test_one_use_after_free(VALUE _)
{
volatile char *ptr = malloc(100);
free((void *)ptr);
ptr[0] = 'a';
return Qnil;
}

static VALUE uninitialized_value(VALUE _)
static VALUE c_test_one_uninitialized_value(VALUE _)
{
volatile int foo;
#pragma GCC diagnostic ignored "-Wuninitialized"
return foo == 0 ? rb_str_new_cstr("zero") : rb_str_new_cstr("not zero");
#pragma GCC diagnostic pop
}

static VALUE call_into_ruby_mem_leak(VALUE obj)
static VALUE c_test_one_call_into_ruby_mem_leak(VALUE obj)
{
char str[20];
for (int i = 0; i < 10000; i++) {
Expand All @@ -49,15 +49,15 @@ static VALUE call_into_ruby_mem_leak(VALUE obj)
void Init_ruby_memcheck_c_test_one(void)
{
/* Memory leaks in the Init functions should be ignored. */
allocate_memory_leak();
c_test_one_allocate_memory_leak();

VALUE mRubyMemcheck = rb_define_module("RubyMemcheck");
cRubyMemcheckCTestOne = rb_define_class_under(mRubyMemcheck, "CTestOne", rb_cObject);
rb_global_variable(&cRubyMemcheckCTestOne);

rb_define_method(cRubyMemcheckCTestOne, "no_memory_leak", no_memory_leak, 0);
rb_define_method(cRubyMemcheckCTestOne, "memory_leak", memory_leak, 0);
rb_define_method(cRubyMemcheckCTestOne, "use_after_free", use_after_free, 0);
rb_define_method(cRubyMemcheckCTestOne, "uninitialized_value", uninitialized_value, 0);
rb_define_method(cRubyMemcheckCTestOne, "call_into_ruby_mem_leak", call_into_ruby_mem_leak, 0);
rb_define_method(cRubyMemcheckCTestOne, "no_memory_leak", c_test_one_no_memory_leak, 0);
rb_define_method(cRubyMemcheckCTestOne, "memory_leak", c_test_one_memory_leak, 0);
rb_define_method(cRubyMemcheckCTestOne, "use_after_free", c_test_one_use_after_free, 0);
rb_define_method(cRubyMemcheckCTestOne, "uninitialized_value", c_test_one_uninitialized_value, 0);
rb_define_method(cRubyMemcheckCTestOne, "call_into_ruby_mem_leak", c_test_one_call_into_ruby_mem_leak, 0);
}
28 changes: 14 additions & 14 deletions test/ruby_memcheck/ext/ruby_memcheck_c_test_two.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,41 @@

static VALUE cRubyMemcheckCTestTwo;

static VALUE no_memory_leak(VALUE _)
static VALUE c_test_two_no_memory_leak(VALUE _)
{
return Qnil;
}

/* This function must not be inlined to ensure that it has a stack frame. */
static void __attribute__((noinline)) allocate_memory_leak(void)
static void __attribute__((noinline)) c_test_two_allocate_memory_leak(void)
{
volatile char *ptr = malloc(100);
ptr[0] = 'a';
}
}

static VALUE memory_leak(VALUE _)
static VALUE c_test_two_memory_leak(VALUE _)
{
allocate_memory_leak();
c_test_two_allocate_memory_leak();
return Qnil;
}

static VALUE use_after_free(VALUE _)
static VALUE c_test_two_use_after_free(VALUE _)
{
volatile char *ptr = malloc(100);
free((void *)ptr);
ptr[0] = 'a';
return Qnil;
}

static VALUE uninitialized_value(VALUE _)
static VALUE c_test_two_uninitialized_value(VALUE _)
{
volatile int foo;
#pragma GCC diagnostic ignored "-Wuninitialized"
return foo == 0 ? rb_str_new_cstr("zero") : rb_str_new_cstr("not zero");
#pragma GCC diagnostic pop
}

static VALUE call_into_ruby_mem_leak(VALUE obj)
static VALUE c_test_two_call_into_ruby_mem_leak(VALUE obj)
{
char str[20];
for (int i = 0; i < 10000; i++) {
Expand All @@ -49,15 +49,15 @@ static VALUE call_into_ruby_mem_leak(VALUE obj)
void Init_ruby_memcheck_c_test_two(void)
{
/* Memory leaks in the Init functions should be ignored. */
allocate_memory_leak();
c_test_two_allocate_memory_leak();

VALUE mRubyMemcheck = rb_define_module("RubyMemcheck");
cRubyMemcheckCTestTwo = rb_define_class_under(mRubyMemcheck, "CTestTwo", rb_cObject);
rb_global_variable(&cRubyMemcheckCTestTwo);

rb_define_method(cRubyMemcheckCTestTwo, "no_memory_leak", no_memory_leak, 0);
rb_define_method(cRubyMemcheckCTestTwo, "memory_leak", memory_leak, 0);
rb_define_method(cRubyMemcheckCTestTwo, "use_after_free", use_after_free, 0);
rb_define_method(cRubyMemcheckCTestTwo, "uninitialized_value", uninitialized_value, 0);
rb_define_method(cRubyMemcheckCTestTwo, "call_into_ruby_mem_leak", call_into_ruby_mem_leak, 0);
rb_define_method(cRubyMemcheckCTestTwo, "no_memory_leak", c_test_two_no_memory_leak, 0);
rb_define_method(cRubyMemcheckCTestTwo, "memory_leak", c_test_two_memory_leak, 0);
rb_define_method(cRubyMemcheckCTestTwo, "use_after_free", c_test_two_use_after_free, 0);
rb_define_method(cRubyMemcheckCTestTwo, "uninitialized_value", c_test_two_uninitialized_value, 0);
rb_define_method(cRubyMemcheckCTestTwo, "call_into_ruby_mem_leak", c_test_two_call_into_ruby_mem_leak, 0);
}
20 changes: 10 additions & 10 deletions test/ruby_memcheck/shared_test_task_reporter_tests.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def test_reports_memory_leak
output = @output_io.string
refute_empty(output)
assert_match(/^100 bytes in 1 blocks are definitely lost in loss record/, output)
assert_match(/^ \*memory_leak \(ruby_memcheck_c_test_one\.c:\d+\)$/, output)
assert_match(/^ \*c_test_one_memory_leak \(ruby_memcheck_c_test_one\.c:\d+\)$/, output)
end

def test_reports_use_after_free
Expand All @@ -43,7 +43,7 @@ def test_reports_use_after_free
output = @output_io.string
refute_empty(output)
assert_match(/^Invalid write of size 1$/, output)
assert_match(/^ \*use_after_free \(ruby_memcheck_c_test_one\.c:\d+\)$/, output)
assert_match(/^ \*c_test_one_use_after_free \(ruby_memcheck_c_test_one\.c:\d+\)$/, output)
end

# Potential improvement: support uninitialized values
Expand Down Expand Up @@ -107,10 +107,10 @@ def test_generation_of_suppressions
output = @output_io.string
refute_empty(output)
assert_match(/^100 bytes in 1 blocks are definitely lost in loss record/, output)
assert_match(/^ \*memory_leak \(ruby_memcheck_c_test_one\.c:\d+\)$/, output)
assert_match(/^ \*c_test_one_memory_leak \(ruby_memcheck_c_test_one\.c:\d+\)$/, output)
assert_match(/^ insert_a_suppression_name_here/, output)
assert_match(/^ Memcheck:Leak/, output)
assert_match(/^ fun:allocate_memory_leak/, output)
assert_match(/^ fun:c_test_one_allocate_memory_leak/, output)
end

def test_follows_forked_children
Expand All @@ -130,7 +130,7 @@ def test_follows_forked_children
output = @output_io.string
refute_empty(output)
assert_match(/^100 bytes in 1 blocks are definitely lost in loss record/, output)
assert_match(/^ \*memory_leak \(ruby_memcheck_c_test_one\.c:\d+\)$/, output)
assert_match(/^ \*c_test_one_memory_leak \(ruby_memcheck_c_test_one\.c:\d+\)$/, output)
end

def test_reports_multiple_errors
Expand All @@ -148,9 +148,9 @@ def test_reports_multiple_errors

refute_empty(output)
assert_match(/^100 bytes in 1 blocks are definitely lost in loss record/, output)
assert_match(/^ \*memory_leak \(ruby_memcheck_c_test_one\.c:\d+\)$/, output)
assert_match(/^ \*c_test_one_memory_leak \(ruby_memcheck_c_test_one\.c:\d+\)$/, output)
assert_match(/^Invalid write of size 1$/, output)
assert_match(/^ \*use_after_free \(ruby_memcheck_c_test_one\.c:\d+\)$/, output)
assert_match(/^ \*c_test_one_use_after_free \(ruby_memcheck_c_test_one\.c:\d+\)$/, output)
end

def test_reports_errors_in_all_binaries
Expand All @@ -168,8 +168,8 @@ def test_reports_errors_in_all_binaries

refute_empty(output)
assert_match(/^100 bytes in 1 blocks are definitely lost in loss record/, output)
assert_match(/^ \*memory_leak \(ruby_memcheck_c_test_one\.c:\d+\)$/, output)
assert_match(/^ \*memory_leak \(ruby_memcheck_c_test_two\.c:\d+\)$/, output)
assert_match(/^ \*c_test_one_memory_leak \(ruby_memcheck_c_test_one\.c:\d+\)$/, output)
assert_match(/^ \*c_test_two_memory_leak \(ruby_memcheck_c_test_two\.c:\d+\)$/, output)
end

def test_can_run_multiple_times
Expand Down Expand Up @@ -208,7 +208,7 @@ def test_ruby_failure_with_errors
output = @output_io.string
refute_empty(output)
assert_match(/^100 bytes in 1 blocks are definitely lost in loss record/, output)
assert_match(/^ \*memory_leak \(ruby_memcheck_c_test_one\.c:\d+\)$/, output)
assert_match(/^ \*c_test_one_memory_leak \(ruby_memcheck_c_test_one\.c:\d+\)$/, output)
end

def test_test_helper_is_loaded
Expand Down
2 changes: 1 addition & 1 deletion test/ruby_memcheck/suppressions/ruby.supp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
suppress memory_leak
Memcheck:Leak
...
fun:memory_leak
fun:c_test_one_memory_leak
...
}

0 comments on commit 1749394

Please sign in to comment.