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 address of function in decltype context #202

Merged
merged 1 commit into from
Feb 2, 2024
Merged

Conversation

DutChen18
Copy link
Member

There is a check that tries to prevent you from taking the address of a wasm function from genericjs, or vice versa. The check uses CurScope->getFnParent() to get the function in which the address of operator takes place. This is not a reliable way to get the current function declaration, and its usage here allows some invalid code to sneak past the check.

#include <cheerp/clientlib.h>

struct functor {
	[[cheerp::wasm]]
	void wasm_func() {}
};

template<class T>
[[cheerp::genericjs]]
auto js_func() -> void(functor::*)() {
	return &T::wasm_func; // address of wasm function here
}

[[cheerp::genericjs]]
int main() {
	client::console.log(js_func<functor>());
}

getCurFunctionDecl() is a more reliable way to get the current function declaration, and using it, the above code no longer compiles (yay!).

There is one place in cheerp/client.h where the address of a wasm function is taken from inside a decltype expression in the return type of a genericjs function. Because the operand of a decltype expression is never evaluated, we can always allow taking the address of a function like this as long as it happens in a decltype expression. And, this way, there is no issue in cheerp/client.h.

There is a check that tries to prevent you from taking the address of a
wasm function from genericjs, or vice versa. The check uses
`CurScope->getFnParent()` to get the function in which the address of
operator takes place. This is not a reliable way to get the current
function declaration, and its usage here allows some invalid code to
sneak past the check.

	#include <cheerp/clientlib.h>

	struct functor {
		[[cheerp::wasm]]
		void wasm_func() {}
	};

	template<class T>
	[[cheerp::genericjs]]
	auto js_func() -> void(functor::*)() {
		return &T::wasm_func; // address of wasm function here
	}

	[[cheerp::genericjs]]
	int main() {
		client::console.log(js_func<functor>());
	}

`getCurFunctionDecl()` is a more reliable way to get the current
function declaration, and using it, the above code no longer compiles
(yay!).

There is one place in `cheerp/client.h` where the address of a wasm
function is taken from inside a decltype expression in the return type
of a genericjs function. Because the operand of a decltype expression is
never evaluated, we can always allow taking the address of a function
like this as long as it happens in a decltype expression. And, this way,
there is no issue in `cheerp/client.h`.
@DutChen18 DutChen18 requested a review from yuri91 February 2, 2024 14:22
Copy link
Member

@yuri91 yuri91 left a comment

Choose a reason for hiding this comment

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

nice double fix!

@yuri91 yuri91 merged commit 5bc97cc into master Feb 2, 2024
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants