Skip to content
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

Fix use-after-free in externref example #8410

Merged
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
121 changes: 64 additions & 57 deletions examples/externref.c
Original file line number Diff line number Diff line change
Expand Up @@ -106,71 +106,78 @@ int main() {
wasmtime_extern_t item;

// Lookup the `table` export.
ok = wasmtime_instance_export_get(context, &instance, "table",
strlen("table"), &item);
assert(ok);
assert(item.kind == WASMTIME_EXTERN_TABLE);

// Set `table[3]` to our `externref`.
wasmtime_val_t externref_val;
externref_val.kind = WASMTIME_EXTERNREF;
externref_val.of.externref = externref;
error = wasmtime_table_set(context, &item.of.table, 3, &externref_val);
if (error != NULL)
exit_with_error("failed to set table", error, NULL);

// `table[3]` should now be our `externref`.
wasmtime_val_t elem;
ok = wasmtime_table_get(context, &item.of.table, 3, &elem);
assert(ok);
assert(elem.kind == WASMTIME_EXTERNREF);
assert(strcmp((char *)wasmtime_externref_data(context, elem.of.externref),
"Hello, World!") == 0);
wasmtime_val_delete(context, &elem);
{
ok = wasmtime_instance_export_get(context, &instance, "table",
strlen("table"), &item);
assert(ok);
assert(item.kind == WASMTIME_EXTERN_TABLE);

// Set `table[3]` to our `externref`.
wasmtime_val_t externref_val;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is now declared inside this block, but then reused in the other block and has wasmtime_val_delete called outside of the block. Can we move it up, out of this block then?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait what how does this compile given this mistake...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah the answer is that it doesn't...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lol yeah I was wondering myself

externref_val.kind = WASMTIME_EXTERNREF;
externref_val.of.externref = externref;
error = wasmtime_table_set(context, &item.of.table, 3, &externref_val);
if (error != NULL)
exit_with_error("failed to set table", error, NULL);

// `table[3]` should now be our `externref`.
wasmtime_val_t elem;
ok = wasmtime_table_get(context, &item.of.table, 3, &elem);
assert(ok);
assert(elem.kind == WASMTIME_EXTERNREF);
assert(strcmp((char *)wasmtime_externref_data(context, elem.of.externref),
"Hello, World!") == 0);
wasmtime_val_delete(context, &elem);
}

printf("Touching `externref` global...\n");

// Lookup the `global` export.
ok = wasmtime_instance_export_get(context, &instance, "global",
strlen("global"), &item);
assert(ok);
assert(item.kind == WASMTIME_EXTERN_GLOBAL);

// Set the global to our `externref`.
error = wasmtime_global_set(context, &item.of.global, &externref_val);
if (error != NULL)
exit_with_error("failed to set global", error, NULL);

// Get the global, and it should return our `externref` again.
wasmtime_val_t global_val;
wasmtime_global_get(context, &item.of.global, &global_val);
assert(global_val.kind == WASMTIME_EXTERNREF);
assert(strcmp((char *)wasmtime_externref_data(context, elem.of.externref),
"Hello, World!") == 0);
wasmtime_val_delete(context, &global_val);
{
ok = wasmtime_instance_export_get(context, &instance, "global",
strlen("global"), &item);
assert(ok);
assert(item.kind == WASMTIME_EXTERN_GLOBAL);

// Set the global to our `externref`.
error = wasmtime_global_set(context, &item.of.global, &externref_val);
if (error != NULL)
exit_with_error("failed to set global", error, NULL);

// Get the global, and it should return our `externref` again.
wasmtime_val_t global_val;
wasmtime_global_get(context, &item.of.global, &global_val);
assert(global_val.kind == WASMTIME_EXTERNREF);
assert(strcmp((char *)wasmtime_externref_data(context,
global_val.of.externref),
"Hello, World!") == 0);
wasmtime_val_delete(context, &global_val);
}

printf("Calling `externref` func...\n");

// Lookup the `func` export.
ok = wasmtime_instance_export_get(context, &instance, "func", strlen("func"),
&item);
assert(ok);
assert(item.kind == WASMTIME_EXTERN_FUNC);

// And call it!
wasmtime_val_t results[1];
error = wasmtime_func_call(context, &item.of.func, &externref_val, 1, results,
1, &trap);
if (error != NULL || trap != NULL)
exit_with_error("failed to call function", error, trap);

// `func` returns the same reference we gave it, so `results[0]` should be our
// `externref`.
assert(results[0].kind == WASMTIME_EXTERNREF);
assert(
strcmp((char *)wasmtime_externref_data(context, results[0].of.externref),
"Hello, World!") == 0);
wasmtime_val_delete(context, &results[0]);
{
ok = wasmtime_instance_export_get(context, &instance, "func",
strlen("func"), &item);
assert(ok);
assert(item.kind == WASMTIME_EXTERN_FUNC);

// And call it!
wasmtime_val_t results[1];
error = wasmtime_func_call(context, &item.of.func, &externref_val, 1,
results, 1, &trap);
if (error != NULL || trap != NULL)
exit_with_error("failed to call function", error, trap);

// `func` returns the same reference we gave it, so `results[0]` should be
// our `externref`.
assert(results[0].kind == WASMTIME_EXTERNREF);
assert(strcmp((char *)wasmtime_externref_data(context,
results[0].of.externref),
"Hello, World!") == 0);
wasmtime_val_delete(context, &results[0]);
}
wasmtime_val_delete(context, &externref_val);

// We can GC any now-unused references to our externref that the store is
Expand Down