Frama-C中的数组大小

时间:2018-01-05 12:06:39

标签: c frama-c

我是frama-c插件开发的新手。

我想问一下是否有办法从Frama-C获取数组大小值和数组元素类型,无论是静态还是动态创建的数组。

例如

float *A = (float*)malloc(ni * nk * sizeof(float)); 
int tab[100];

我想执行一些指针别名分析,以便知道何时何地可以插入restrict关键字。

我不知道是否可以修改函数的形式参数。以此为例:

void test(int a, int b, int *tmp) {//}

如果在此函数体中的别名分析结束时,我确定指针tmp没有任何别名情况,那么我可以通过

在tmp声明中插入restrict关键字
void test(int a, int b, int *__restrict__ tmp) {//}

所以我需要知道:指针基地址,如果指针指向一个数组(常量或动态),那么得到数组大小和数组元素的数量。就目前而言,这就是我如何形成关于获取数组大小,基址和元素数量的部分:

start 
launch value analysis
launch pdg analysis
for kf do 
Get all locals variables (eg: let locals = Kernel_function.get_locals kf;)
    for each local var do 
        if Cil.isPointerType vi.vtype then
        begin 
            find local var declaration (eg: with vi.vdecl);
            print vi.vdecl in output file;
            Know if local var is initialized during the declaration;
            if local var is not initialized during the declaration then
                locate local var initialization or first utilisation;
                print this localisation in output file;
                if this initialization is "an variable assignement" then 
                    match initialization with
                        | ptr = a (where ptr (the local var concerned) and a are pointer type) -> get_base_addr a; get_array_size a; get_num_of_elm a; 
                                                                                                                                                                            print previous get_* results in output file; add ptr to some list
                        | ptr = &val -> match val with
                                                        | Const -> 
                                                        | Lval ->
                                                        | ArrayType -> get_base_addr val; get_array_size val; get_num_of_elm val; 
                                                                                     print previous get_* results in output file; add ptr to some list
                                                        | _ ->
                        | _ ->
                else if this initialization is a dynamic allocation (eg: with malloc, ...) then 
                    get_base_addr ptr; get_array_size ptr; get_num_of_elm ptr; 
                    print previous get_* results in output file; add ptr to some list
            else if local var is initialized during the declaration then
            (*----*)
        end 
        else if Cil.isArrayType vi.vtype then
            get_base_addr ptr; get_array_size ptr; get_num_of_elm ptr; 
            print previous get_* results in output file; add ptr to some list
        else 
            (*local var is not a pointer and not an array*)
    end of for 
    Get list of all called functions;
    Know if previous pointers or arrays analysed are passed as parameters to some functions in this list and print the name of these functions;
    (*-----------*) 
end of for ; end 

我查看了文件Cil_types.mli和db.mli(为了使用一些Db.Value。*函数)并找到了一些类型(如TPtr和TArray)和许多有用的函数(mk *)但是暂时但是现在我还没有真正理解如何使用它们。

提前致谢。

0 个答案:

没有答案