8000 [Backport 3.3] [Bug #20688] Fix use-after-free for WeakMap and WeakKeyMap by peterzhu2118 · Pull Request #11439 · ruby/ruby · GitHub
[go: up one dir, main page]

Skip to content

[Backport 3.3] [Bug #20688] Fix use-after-free for WeakMap and WeakKeyMap #11439

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Aug 22, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
8000 Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Refactor wmap_foreach to pass weakmap_entry
  • Loading branch information
peterzhu2118 committed Aug 22, 2024
commit 191163a237da695db26b468e0a9f2112b717b475
46 changes: 25 additions & 21 deletions weakmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,7 @@ wmap_allocate(VALUE klass)
}

struct wmap_foreach_data {
struct weakmap *w;
void (*func)(VALUE, VALUE, st_data_t);
void (*func)(struct weakmap_entry *, st_data_t);
st_data_t arg;
};

Expand All @@ -202,11 +201,17 @@ wmap_foreach_i(st_data_t key, st_data_t val, st_data_t arg)
{
struct wmap_foreach_data *data = (struct wmap_foreach_data *)arg;

VALUE key_obj = *(VALUE *)key;
VALUE val_obj = *(VALUE *)val;
struct weakmap_entry *entry = (struct weakmap_entry *)key;
RUBY_ASSERT(&entry->val == (VALUE *)val);

if (wmap_live_p(key_obj) && wmap_live_p(val_obj)) {
data->func(key_obj, val_obj, data->arg);
if (wmap_live_p(entry->key) && wmap_live_p(entry->val)) {
VALUE k = entry->key;
VALUE v = entry->val;

data->func(entry, data->arg);

RB_GC_GUARD(k);
RB_GC_GUARD(v);
}
else {
wmap_free_entry((VALUE *)key, (VALUE *)val);
Expand All @@ -218,10 +223,9 @@ wmap_foreach_i(st_data_t key, st_data_t val, st_data_t arg)
}

static void
wmap_foreach(struct weakmap *w, void (*func)(VALUE, VALUE, st_data_t), st_data_t arg)
wmap_foreach(struct weakmap *w, void (*func)(struct weakmap_entry *, st_data_t), st_data_t arg)
{
struct wmap_foreach_data foreach_data = {
.w = w,
.func = func,
.arg = arg,
};
Expand All @@ -241,7 +245,7 @@ wmap_inspect_append(VALUE str, VALUE obj)
}

static void
wmap_inspect_i(VALUE key, VALUE val, st_data_t data)
wmap_inspect_i(struct weakmap_entry *entry, st_data_t data)
{
VALUE str = (VALUE)data;

Expand All @@ -253,9 +257,9 @@ wmap_inspect_i(VALUE key, VALUE val, st_data_t data)
RSTRING_PTR(str)[0] = '#';
}

wmap_inspect_append(str, key);
wmap_inspect_append(str, entry->key);
rb_str_cat2(str, " => ");
wmap_inspect_append(str, val);
wmap_inspect_append(str, entry->val);
}

static VALUE
Expand All @@ -276,9 +280,9 @@ wmap_inspect(VALUE self)
}

static void
wmap_each_i(VALUE key, VALUE val, st_data_t _)
wmap_each_i(struct weakmap_entry *entry, st_data_t _)
{
rb_yield_values(2, key, val);
rb_yield_values(2, entry->key, entry->val);
}

/*
Expand All @@ -301,9 +305,9 @@ wmap_each(VALUE self)
}

static void
wmap_each_key_i(VALUE key, VALUE _val, st_data_t _data)
wmap_each_key_i(struct weakmap_entry *entry, st_data_t _data)
{
rb_yield(key);
rb_yield(entry->key);
}

/*
Expand All @@ -326,9 +330,9 @@ wmap_each_key(VALUE self)
}

static void
wmap_each_value_i(VALUE _key, VALUE val, st_data_t _data)
wmap_each_value_i(struct weakmap_entry *entry, st_data_t _data)
{
rb_yield(val);
rb_yield(entry->val);
}

/*
Expand All @@ -351,11 +355,11 @@ wmap_each_value(VALUE self)
}

static void
wmap_keys_i(st_data_t key, st_data_t _, st_data_t arg)
wmap_keys_i(struct weakmap_entry *entry, st_data_t arg)
{
VALUE ary = (VALUE)arg;

rb_ary_push(ary, key);
rb_ary_push(ary, entry->key);
}

/*
Expand All @@ -378,11 +382,11 @@ wmap_keys(VALUE self)
}

static void
wmap_values_i(st_data_t key, st_data_t val, st_data_t arg)
wmap_values_i(struct weakmap_entry *entry, st_data_t arg)
{
VALUE ary = (VALUE)arg;

rb_ary_push(ary, (VALUE)val);
rb_ary_push(ary, entry->val);
}

/*
Expand Down
0