Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to isolate bad behaving 3rd-party c-lib which only allows to be created as singleton?

Tags:

c++

c

node.js

currently i'm developing a native-C nodejs Addon to wrap a 3rdparty closed source c-lib and expose it to nodejs.

So far so good. My solution works and native code can be called and worked with.

One problem arises, when calls to those functions are being made subsequently. The 3rdParty library seems to alocate always the same struct(?; speak: same place in memory) for it's runtime object.

Can I "sandbox" this 3rdparty-lib somehow? Is there a pattern to solve this? Maybe specific to nodejs-addon development, 'cause nodejs is single threaded, single process-application.

The general suggestion in such cases seems to run the 3rdParty lib in a seperate Process like stated here for example: Isolating and multiply instantiating a C library in-process

I'm not quite sure how to adapt this pattern to nodejs except creating own nodejs processes as "workers" and comunicate to it via rpc somehow. But this seems a little awkward to me and i don't want to reinvent the wheel.

IMHO spawning different node-processes just for this seems to be the 'ugliest' solution. But correct me if i'm wrong.

Long story, short question. Thanks for adivce.

like image 823
crushervx Avatar asked Nov 10 '22 12:11

crushervx


1 Answers

This is a very very difficult problem, the only solution I have found is to create many copies of the shared object in a temporary directory (with different names) where your program loads each of them once. Most linkers will not realize they are the same and allow you to load them into different address spaces. However this does rely on the code being position independent AFAIK so may not work. It is also nearly as ugly as using IPC so it is up to you what to do.

like image 190
Vality Avatar answered Nov 14 '22 23:11

Vality