Reputation: 155
I'm writing network application in c++ and I want to enable making plugins, but I don't know what to do, to protect my application from errors like segfault. For example: I have interface:
class IPlugin{
public:
IPlugin();
virtual ~IPlugin();
virtual void callPlugin() = 0;
}
And someone will write dynamic library:
class Plugin : public IPlugin{
public:
Plugin();
virtual ~Plugin();
virtual void callPlugin();
}
void Plugin::callPlugin(){
int* a = NULL;
*a = 5;
}
Calling this function in my app will end with terminating everything. I know that I can use fork(), but functions in plugins will be short and will be used many times, so I think that fork() is too slow. Any ideas?
PS. And sorry for my English.
Upvotes: 3
Views: 251
Reputation: 4705
It's a losing battle trying to protect you from bad code loaded into your address space. Those plugins could do real damage that you can never recover from.
Either accept the fact that a buggy plugin will bring your application down, or you have to isolate the plugin in a separate process as you suggest. But you only need to call fork once, when you load the plugin, don't worry about that being slow. The real work will be to communicate with the plugin from your app using some form of IPC - pipes might be suitable.
Upvotes: 5