ndless-nspire / Ndless

The TI-Nspire calculator extension for native applications
http://ndless.me
852 stars 103 forks source link

freeing resident programms #192

Open nspiredev500 opened 4 years ago

nspiredev500 commented 4 years ago

On Hackspire I read that there is no way to free a resident programm, but in the sourcecode I found this in ploaderhook.c: // If resident_ptr isn't NULL, the program's memory block isn't freed and is stored in resident_ptr. It may be freed later with ld_free(). ... // To free the program's memory block when run with ld_exec(non null resident_ptr) void ld_free(void *resident_ptr) { free(resident_ptr); } This function isn't exposed as a system call, but could you free a programm if you properly clean up before, like uninstalling hooks and making sure no pointer to data in the programm is used? Could ld_free be added as a feature for Ndless?

Vogtinator commented 4 years ago

Technically yes, but where would you call that function from? If you do it from the program code, ld_free would return into now freed memory.

nspiredev500 commented 4 years ago

I want to make a seperate uninstaller for my programm so I can test it without a memory leak and without rebooting, because that actually takes some time for real hardware. I already encountered things that are different in firebird and therefore also want to test it on real hardware. So would there be a way to get my programms resident_ptr and call ld_free()? Or would I have to include a zehn loader for that so I can first load my programm, save the resident_ptr, clear up and unload it?

Vogtinator commented 4 years ago

So would there be a way to get my programms resident_ptr and call ld_free()?

The only way that would possibly work is to have a syscall which sets a timer, returns, and then later when the timer fires, calls ld_free. That's incredibly ugly.

Or would I have to include a zehn loader for that so I can first load my programm, save the resident_ptr, clear up and unload it?

You'd face the same issue, how would you work around that?

nspiredev500 commented 4 years ago

But can't I make a non-resident programm that frees my resident programm and then in turn gets freed when it returns?

Vogtinator commented 4 years ago

No. Your non-resident program would free your resident program directly after it installed its hooks.

nspiredev500 commented 4 years ago

I mean, the non-resident programm would uninstall the hooks and then free the resident one, and you would have to run it manually from the file explorer, so the execution isn't in one of the hooks that get uninstalled or the code that get freed.

Vogtinator commented 4 years ago

That would work, yes.

nspiredev500 commented 4 years ago

And because there currently is no syscall for ld_free, I would have to compile my own Ndless version with ld_free added as a syscall and figure out how to get the resident_ptr of a programm?

Vogtinator commented 4 years ago

If you would load the executable yourself, you could also free it yourself. Otherwise you could "cheat" and assume that the pointer points to the beginning of the binary, which is usually true and use some linker tricks.