[Q] problem with class static value

Search This thread

Falseclock

Senior Member
Jun 30, 2012
552
367
Almaty
have some problems with static values used as flags.

for example I need to hook 2 methods in different FRAMEWORK classes during initZygote. (no problem with system apps during handleLoadPackage)
I hook method in first class, set static value and waiting when another method of another class will be triggered.
once another method is invoked, flag always has it's initial state.

it looks like each hook method processed in different class instances.

i tried volatile type, tried to synchronize - nothing help.

unreliable remedy:
1. register broadcast receiver inside hook method.
2. send action to own service
3. send broadcast from own service.
4. catch broadcast with receiver.

BUT it is not battery cost efficient solution in my case. And mostly I receive broadcast message when hooking method already processed and returned value. Otherwise I need to wait when service will process request and send broadcast. But it is not usable.

is there any solution?

Code:
public class XMain implements IXposedHookInitPackageResources, IXposedHookZygoteInit, IXposedHookLoadPackage
{
	private static boolean isNeedToRun = false;

	public void initZygote(StartupParam startupParam) throws Throwable
	{
		findAndHookMethod("com.class1", null, "method1", int.class, new XC_MethodHook()
		{
			@Override
			protected void beforeHookedMethod(final MethodHookParam param) throws Throwable
			{			    
				isNeedToRun = true;
			}
		});

		findAndHookMethod("com.class2", null, "method2", int.class, new XC_MethodHook()
		{
			@Override
			protected void beforeHookedMethod(final MethodHookParam param) throws Throwable
			{			    
				if (isNeedToRun) param.setResult(null); // always FALSE even if previous hook set as TRUE

			}
		});
	}
}
 
Last edited:

GermainZ

Inactive Recognized Developer / Retired Forum Mod
Aug 3, 2012
6,170
8,805
Why do you think that using a broadcast receiver is not battery efficient?
 

Falseclock

Senior Member
Jun 30, 2012
552
367
Almaty
Why do you think that using a broadcast receiver is not battery efficient?

I do not want to invoke service and send broadcast very often (several thousands times during normal battery one cycle charge)

I just want use easiest way by storing flag inside class and worried that users will find the module in the list of gluttonous applications.
 

Tungstwenty

Senior Member
Nov 1, 2011
1,830
4,512
Code:
	if (isNeedToRun) param.setResult(null); // always FALSE even if previous hook set as TRUE
You're probably not considering the fact that different processes will each have its own "isNeededToRun" variable.
Whenever new processes (system_process, apps, etc.) are forked from zygote, each of them will have its own independent state which includes this static variable. From then on they will be completely separate things even if it's a variable with the same name, don't get confused by that.
Try adding a Log.i(...) call to write something in both methods, and then check the logcat for the pid in which the messages are logged. I bet you'll see that different pids are writing and reading, and you can't expect them to be doing it in the same global variable.
 
  • Like
Reactions: Falseclock

Falseclock

Senior Member
Jun 30, 2012
552
367
Almaty
You're probably not considering the fact that different processes will each have its own "isNeededToRun" variable.
Whenever new processes (system_process, apps, etc.) are forked from zygote, each of them will have its own independent state which includes this static variable. From then on they will be completely separate things even if it's a variable with the same name, don't get confused by that.
Try adding a Log.i(...) call to write something in both methods, and then check the logcat for the pid in which the messages are logged. I bet you'll see that different pids are writing and reading, and you can't expect them to be doing it in the same global variable.

Yes, I realized this. But under handleLoadPackage everything work perfect.
Any suggestion to solve situation?
 

rovo89

Senior Recognized Developer
Jan 4, 2012
2,585
81,434
Yes, I realized this. But under handleLoadPackage everything work perfect.
Any suggestion to solve situation?

handleLoadPackage is executed after the fork, in the new process. Not sure what exactly you hooked when you tried that way, but probably both callbacks were executed in the same app/process.

You have the typical problem that requires some kind of inter-process communication (IPC). Most common examples for IPC in Android are broadcasts and services. Files might also be an option (especially with observers), but probably not when you need to change the value very frequently.
 
  • Like
Reactions: Falseclock

Top Liked Posts

  • There are no posts matching your filters.
  • 1
    Code:
    	if (isNeedToRun) param.setResult(null); // always FALSE even if previous hook set as TRUE
    You're probably not considering the fact that different processes will each have its own "isNeededToRun" variable.
    Whenever new processes (system_process, apps, etc.) are forked from zygote, each of them will have its own independent state which includes this static variable. From then on they will be completely separate things even if it's a variable with the same name, don't get confused by that.
    Try adding a Log.i(...) call to write something in both methods, and then check the logcat for the pid in which the messages are logged. I bet you'll see that different pids are writing and reading, and you can't expect them to be doing it in the same global variable.
    1
    Yes, I realized this. But under handleLoadPackage everything work perfect.
    Any suggestion to solve situation?

    handleLoadPackage is executed after the fork, in the new process. Not sure what exactly you hooked when you tried that way, but probably both callbacks were executed in the same app/process.

    You have the typical problem that requires some kind of inter-process communication (IPC). Most common examples for IPC in Android are broadcasts and services. Files might also be an option (especially with observers), but probably not when you need to change the value very frequently.