Getting Open Namespaces and Modules in an FSI Session - f#

Is there any way to retrieve a list of open namespaces and modules in an FSI Session? I'm playing around with printing an F# quoted expression and I'd like to be able to distinguish between those values of a module which should be printed fully qualified versus those which should not.

I don't think there is a way to do this.
The fsi object doesn't have any way of doing that, so the simple approach like this won't work. I believe that internally, the information is stored in the FsiDynamicCompilerState record that F# Interactive uses to keep current state of the interaction (you can find it in "fsi.fs" in the sources).
Unfortunatelly, I didn't find any static property that you could use to access this state - it is kept as a field in FsiDynamicCompiler which is created in the main function and not stored anywhere else. In principle, you may want to be able to access the instance via stack trace (but I couldn't find a way to get local variables of a stack frame).
If you wanted to modify fsi.exe to make this possible, that would be another question, but I suppose that's not what you want.

Related

Get class name based on address of its instance in another process

I'm looking for anything that can help me deviate string GetRTTIClassName(IntPtr ProcessHandle, IntPtr StructAddress). The function would use another (third-party) app's process handle to get names of structures located at specific addresses in its memory (should there be found any).
All of RTTI questions/documentation I can find relate to it being used in the same application, and have nothing to do with process interop. The only thing close to what I'm looking for is this module in Cheat Engine's source code (which is also how I found out that it's possible in the first place), but it has over a dozen of nested language-specific dependencies, let alone the fact that Lazarus won't let me build it outside of the project context anyway.
If you know of code examples, libraries, documentation on what I've described, or just info on accessing another app's low-level metadata (pardon my French), please share them. If it makes a difference, I'm targeting C#.
Edit: from what I've gathered, the way runtime information is stored depends on the compiler, so I'll mention that the third-party app I'm "exploring" is a MSVC project.
As I understand, I need to:
Get address of the structure based on address of its instance;
Starting from structure address, navigate through pointers to find its name (possibly "decorated").
I've also found a more readable C# implementation and a bunch of articles on reversing (works for step 2), but I can't seem to find step 1.
I'll update/comment as I find more info, but right now I'm getting a headache just digging into this low-level stuff.
It's a pretty long pointer ladder. I've transcribed the solution ReClass.NET uses to clean C# without dependencies.
Resulting library can be found here.

Lua - Can you run code at runtime independently from the rest of the program?

I'm new to stackoverflow and I'd like to ask a question regarding the use of dofile()/dostring() in lua. Basically, what I'd like to do is run a piece of code using either dofile() or dostring(), which is created at runtime. However, it is possible for variables or classes to overlap with the "normal" source code/rest of the program. Thus it could happen that the code generated at runtime alters and manipulates the values of variables it shouldn't touch. Since the generated code depends on the user, I have no control over this and can't necessarily prevent it.
Is there a way to run code at runtime in lua, but independently from the rest of the program?

writing a parser for GUI debugger

I want to write a GUI frontend to gdb, using MI. Currently I can communicate with gdb via pipe, but a GUI debugger should be able to display source code and allow users to check/modify data using thier mouse.
The question is, in order to know what variable the user is pointing at, I think I need to write a parser. However, I don't want to implement the whole lexer and parser things. How can I get the locations of those identifiers in the source code?
[EDIT]
In short, I want the user to be able to check the value of a variable by hover over the variable using mouse, so I have to parse the code to know where does each variable appear. I want to achieve functions like this:
How can I get the locations of those identifiers in the source code?
... without writing a parser.
You can't. You would need to either write your own (for all programming languages your GUI will support), or hook one of the existing ones.
Clang makes it relatively easy to incorporate the C/C++ parser into a GUI, but ...
not everything can be parsed with Clang
this one aspect of writing a GUI is likely to be 100x more complicated than all the others, so perhaps not worth the effort.

how to make lua config file to be safe

I am new to Lua and want to ask whether it is possible to restrict lua syntax in config file? I know that config loading have to be performed in jail, but how we can cope with while 1 do end in config file we want to load? Is there a way to allow only strings, assignments and tables in config and if not, then what is the best way to check that lua file doesn't contain undesirable constructs? Is manual pre-parsing the only solution?
You seem to already know about "sandboxing" in Lua. So what's left is as you say malicious constructs like infinite loops. And to solve that you need to solve the Halting Problem. Which is not practical.
Instead of "manually" parsing and hoping you find all the malicious content (you won't), how about just running your Lua interpreter with a timer set so that the script will be interrupted if it takes longer than N seconds?
If you want to explicitly forbid certain constructs in Lua, you have to actually scan the file yourself. Note that there are valid uses for those constructs, even in config files, so you are restricting what the user can do.
It wouldn't be too hard to write a simple Lua lexer that ignores the contents of strings and comments, but errors on any of the Lua keywords other than return. Given proper sandboxing (ie: no functions are available to be called), that should be sufficient to weed out anything malicious.
Also, note that Lua 5.1 doesn't make it easy to keep the parser from parsing non-text data (ie: compiled Lua bytecode). 5.2 offers specific API support for forcing the loader to only recognize text and therefore reject bytecode.

How to discover what is available in lua environment?

Many games these days make available some lua scripting, but this is universally undocumented.
So let's say I can get a game to run my lua script (it's lua 5.1) - and the script can write what it finds to text files on disk. How much can I discover about environment the script is executing in?
For example it seems I can list keys in tables, and find out what's a function and what's some other type of object, but there's no obvious way to guess how many arguments function takes (and a mistake usually results in crash to desktop).
Most languages provide some reflection functionality that could be used here - how much is possible in embedded lua environment?
"debug" standard library has some functions, which you may find useful:
debug.getfenv - Returns the environment of object.
debug.getinfo - Returns a table with information about a function.
... and more
Lua Reference Manual also states:
several of these functions violate some assumptions about Lua code (e.g., that variables local to a function cannot be accessed from outside or that userdata metatables cannot be changed by Lua code) and therefore can compromise otherwise secure code.
So with debug library, you may access more.
Unfortunately, there is not much you can learn about functions in Lua - they by design accept any number of parameters. Without the ability to look at the sources, your only resort is the documentation and/or other samples.
The most you can do in this case is traverse the entire _G table recursively and dump every table/function, printing the results to a file.
"A mistake usually results in crash to desktop" is a sign of a really bad design - good API should tell you, that it expects A, and you passed B. For example in Lqt, a Qt binding to Lua, we check every parameter against the original Qt API, so that the programmer is notified of mistakes:
> QApplication.setFont(1, 2)
QApplication::setFont(number, number): incorrect or extra arguments, expecting: QFont*,string,.

Resources