I want to reflective call function with same arguments without manually coping params (for future this will be macro and this will be automatically).
UFUNCTION()
void PleaseRespawnMe(FName className)
{
uint64 _frame; // Here is stack pointer?
UFunction* func = GetController()->ServerCheatManager->FindFunction("PleaseRespawnMe"); // function with same name
void* startStack = (void*)(&_frame - func->ParmsSize); // Start stack is pointer to first variable minus params size?
GetController()->ServerCheatManager->ProcessEvent(func, startStack); // calling using pointer of first argument
}
But &_frame pointer is too far from &className. Is there solutions to get pointer of args using address of local variable?
P.S. This is Unreal Engine 4 framework (calling function by name is unreal reflection system).
I checked the codes by searching FindFunction( and found this: for (TFieldIterator<UProperty>, I think this is what you want.
I tested it with my UFUNCTION and the iterators run through the input parameters then the return value.
I suggest that to search for (TFieldIterator<UProperty> or for (TFieldIterator<UProperty> It(Function); to see the use cases, and note the property flags, which could help with filtering specific properties you are looking for.
Related
In learning the LLVM framework, I am trying to implement an 'optimization' pass that prints the name of each method at runtime when the method is called.
I read that global variables should only be created in a Module Pass, and I create the strings there (one per function), with
Constant* data = ConstantDataArray::getString(M.getContext(), F.getName());
GlobalVariable* gvar =
new GlobalVariable(M,
data->getType(),
true,
GlobalValue::ExternalLinkage,
data,
"fname_" + F.getName().str());
This works fine, insofar as the strings are laid out correctly in memory in the assembly file generated by the 'optimized' bitcode.
However, I have not found a way to insert calls to print these strings in the Function Pass.
I want to use
Value* string = F.getValueSymbolTable().lookup("fname_" + F.getName().str());
CallInst* call = builder.CreateCall(emitPutS(string, builder, &TLI));
but string comes back as NULL. Is there a better way to look up global variables from a function?
Figured it out:
Basic blocks have a getModule() method, and modules have a getGlobalVariable(StringRef Name) method.
Alternatively, IRBuilder:CreateGlobalStringPtr(...) can be called from the function pass, and the Value* returned can be passed to emitPutS(...) directly. The module pass was not necessary.
Note, CallInst* call = builder.CreateCall(emitPutS(string, builder, &TLI)); is incorrect. emitPutS(...) will create the call in the basic block already. The CreateCall is erroneous.
I'm working on creating a user-level thread library and what I want to do is run a function inside a stub and so I would like to pass the function pointer to the stub function.
Here is my stub function:
void _ut_function_stub(void (*f)(void), int id)
{
(*f)();
DeleteThread(id);
}
This is what the user calls. What I want to do is get pointer of _ut_function_stub to assign to pc and I've tried various different options including casting but the compiler keeps saying "invalid use of void expression".
int CreateThread (void (*f) (void), int weight)
{
... more code
pc = (address_t)(_ut_function_stub(f, tcb->id));
... more code
}
Any help is appreciated. Thanks!
If you're interested in implementing your own user-level-threads library, I'd suggest looking into the (now deprecated) ucontext implementation. Specifically, looking at the definitions for the structs used in ucontext.h will help you see all the stuff you actually need to capture to get a valid snapshot of the thread state.
What you're really trying to capture with the erroneous (address_t) cast in your example is the current continuation. Unfortunately, C doesn't support first-class continuations, so you're going to be stuck doing something much more low-level, like swapping stacks and dumping registers (hence why I pointed you to ucontext as a reference—it's going to be kind of complicated if you really want to get this right).
I have the following code in Lua:
ABC:
test (X)
The test function is implemented in C + +. My problem is this: I need to know what the variable name passed as parameter (in this case X). In C + + only have access to the value of this variable, but I must know her name.
Help please
Functions are not passed variables; they are passed values. Variables are just locations that store values.
When you say X somewhere in your Lua code, that means to get the value from the variable X (note: it's actually more complicated than that, but I won't get into that here).
So when you say test(X), you're saying, "Get the value from the variable X and pass that value as the first parameter to the function test."
What it seems like you want to do is change the contents of X, right? You want to have the test function modify X in some way. Well, you can't really do that directly in Lua. Nor should you.
See, in Lua, you can return values from functions. And you can return multiple values. Even from C++ code, you can return multiple values. So whatever it is you wanted to store in X can just be returned:
X = test(X)
This way, the caller of the function decides what to do with the value, not the function itself. If the caller wants to modify the variable, that's fine. If the caller wants to stick it somewhere else, that's also fine. Your function should not care one way or the other.
Also, this allows the user to do things like test(5). Here, there is no variable; you just pass a value directly. That's one reason why functions cannot modify the "variable" that is passed; because it doesn't have to be a variable. Only values are passed, so the user could simply pass a literal value rather than one stored in a variable.
In short: you can't do it, and you shouldn't want to.
The correct answer is that Lua doesn't really support this, but there is the debug interface. See this question for the solution you're looking for. If you can't get a call to debug to work directly from C++, then wrap your function call with a Lua function that first extracts the debug results and then calls your C++ function.
If what you're after is a string representation of the argument, then you're kind of stuck in lua.
I'm thinking something like in C:
assert( x==y );
Which generates a nice message on failure. In C this is done through macros.
Something like this (untested and probably broken).
#define assert(X) if(!(X)) { printf("ASSERION FAILED: %s\n", #X ); abort(); }
Here #X means the string form of the arguments. In the example above that is "x==y". Note that this is subtly different from a variable name - its just the string used in the parser when expanding the macro.
Unfortunately there's no such corresponding functionality in lua. For my lua testing libraries I end up passing the stringified version as part of the expression, so in lua my code looks something like this:
assert( x==y, "x==y")
There may be ways to make this work as assert("x==y") using some kind of string evaluation and closure mechanism, but it seemed to tricky to be worth doing to me.
EDIT:
While this doesn't appear to be possible in pure lua, there's a patched version that does seem to support macros: http://lua-users.org/wiki/LuaMacros . They even have an example of a nicer assert.
i am working on making my applications international. After two days digging on msdn i came up with a test, which loads language-specific library containing resources. This is also my first attempt at loading library as a resource, loading strings from it and so on.
Next, according to msdn example at http://msdn.microsoft.com/en-us/library/windows/desktop/dd319071%28v=VS.85%29.aspx, i'm trying the LoadString.
Since string loading for entire application equals a lot of text copying, i thought i would use the - what i think is - memory efficient feature of LoadString, which is setting nBufferMax parameter to zero. According LoadString documentation, it should return a pointer to string resource. I thought i'd make a struct or a class of string pointers and do something along these lines (i extracted only the important bits):
wchar_t textBuf[SOMEVALUE]; // <-- this is how id DOES work
wchar_t *myString; // <-- this is how i would like it
HMODULE resContainer=LoadLibraryEx(L"MUILibENU.dll",NULL, LOAD_LIBRARY_AS_DATAFILE);
if(0!=resContainer){
// this works OK
int copied=LoadStringW(resContainer,IDS_APP_TITLE,textBuf,SOMEVALUE);
// this fails, also gives a warning at compile time about uninitialized variable used.
int copied=LoadStringW(resContainer,IDS_APP_TITLE,myString,0);
}
As you can see i am trying to get myString to become a pointer to loaded resource library's string without actually copying anything.
My question is: am i misunderstanding msdn documentation? Can i or can i not get a pointer to the string directly within loaded library, and simply use it later, e.g. to show a messagebox, without actually copying anything? Until i unload said library?
MSDN says:
[...] If this parameter is 0, then lpBuffer receives a read-only pointer to the resource itself.
It means that a) the pointer must be of type const wchar_t*:
const wchar_t *myString;
and b) you must pass a pointer to the pointer and use an ugly cast:
int copied=LoadStringW(resContainer,IDS_APP_TITLE,(LPWSTR)&myString,0);
I'm learning how to bind C++ objects to Lua with type checking from the book Programming Gems 6 (Chapter 4.2). For the type checking the userdata/string pairs are being stored in an environment table, with the code given how to do that:
void Binder::pushusertype(void* udata, const char* tname) {
lua_pushlightuserdata(L, udata); // push address
lua_pushvalue(L, -1); // duplicate address
lua_pushstring(L, tname); // push type name
lua_rawset(L, LUA_ENVIRONMENTINDEX); // envtable[address] = tname
}
Where the Binder class has a Lua State as an attribute, named "L"
As you can see the address is pushed twice. As this small piece of code is only given as an example it doesn't seem like a duplicate address pushed onto the stack would serve any purpose outside of this function, which leads me to believe there's a specific reason for it. So my question is, why would one do this?
You don't.
The lua_rawset will pop tname & the duplicate off the stack, but will leave the original userdata on the stack.
I'm not sure if its a typo (looks unlikely), I guess it might be needed later.
Don't know if there is some more in the book, that mentions this, but thats what the code will do.
This function does two things:
it pushes a lightuserdata object to the stack and it will be at lua stack position -1 when the function returns. It also updates the current function environment with the name tname stored at table key address (equal to udata). If the current function environment is the normal global environment, the equivalent Lua code would be:
local x = <udata as lightuserdata>
_G[x] = <tname>
One duplicate of x is used to do the _G[x] = ... thing, the other is left on the stack when the function returns (consistent with the name of the function that starts with push).