0

I'd like to create C-linkage functions to intercept my program's calls via LD_PRELOAD but I'm implementing the interception functions in Rust.

I have no problem creating the pub extern fn to intercept the calls but I'd like to read/write from the program's global state somewhere since I can't alter the signature of the functions I'm intercepting to accept the metadata. I don't need heap allocation necessarily, I could live with some large statically initialized pool of HashSet or Vec entries. But I do need to be able to add and remove elements of the collection during the program's run.

I recognize that this pattern is contrary to many Rust design goals and yet it seems like a legitimate use case to me.

I'm brand new to Rust, so I'm hoping that there's an unsafe option out there to do this.

If it's useful to see code examples:

static mut foo Vec<c_int> = Vec::new();

...fails because you can't have static storage that requires a destructor, or does heap allocations. And lazy_static! doesn't allow for mutable structures.

Ideally I'd prefer a HashSet<c_int>. If I can't get that to work, I suppose I could try a Vec::from_elem(100, 0). That's not ideal since I'll need to nominate some kind of sentry value, but if it works I think I'd be satisfied.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Brian Cain
  • 14,403
  • 3
  • 50
  • 88
  • 1
    I suspect [this and similar answers](http://stackoverflow.com/a/27826181/489590) will be satisfactory. – Brian Cain Sep 18 '15 at 21:41
  • 3
    *lazy_static! doesn't allow for mutable structures* — just put a `Mutex` or equivalent in there. I updated the linked question with an example; you can mark this question as a duplicate of that one if it's good enough for you. – Shepmaster Sep 18 '15 at 22:18

0 Answers0