Android的自修改code - NDK [英] Android Self-modifying code - NDK

查看:191
本文介绍了Android的自修改code - NDK的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做一个自我修改code库,我已经scowered遍,我有后续code:

I am trying to make a self-modifying code library and I have scowered all over and I have the follow code:

typedef int (*FUNC) (void);
int test();

JNIEXPORT int Java_com_example_untitled_MyActivity_decrypt( JNIEnv* env, jobject thiz)
{
    void *code = mmap(NULL, 4, PROT_WRITE | PROT_EXEC, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);

    if (code != MAP_FAILED) {
        memcpy(code, test, 4);

        return ( (FUNC)code)();
    }

    return 0;
}

int test()
{
    return 100;
}

请帮忙...我用原住民自我修改$ C $在Androidç的作为我的出发点和他们说了一些有关与-marm和拇指位...

Please help...I used Native self-modifying code on Android as my starting point and they said something about compiling with "-marm" and thumb bit...

我的问题我有是,它只是崩溃。我已经使用cacheflush功能,似乎没有帮助试过。我很茫然。

My issue I'm having is, it's just crashing. I have tried using the cacheflush function, didn't seem to help. I am at a loss.

推荐答案

在ARM,则需要刷新CPU高速缓存,以确保在执行前,他们刚刚复制的指令是可见的CPU。一个简单的方法来做到这一点是:

On ARM, you need to flush the CPU caches in order to ensure that the instructions you just copied are visible to the CPU before they are executed. A simple way to do this is:

#include <unistd.h>  // for cacheflush()

...

// Copy the instructions to the destination address.
memcpy(dest, original_intructions, size_of_instructions);

// Clear the CPU cache
cacheflush((uintptr_t)dest, (uintptr_t)dest + size_of_instructions, 0);

// Run them.
return ((FUNC)dest)();

这篇关于Android的自修改code - NDK的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆