Taming and Dissecting Recursions through Interprocedural Weak Topological Ordering
Note: This work has been merged into SVF.
Run this script to clone SVF to your local machine:
git clone https://github.com/SVF-tools/SVF.git
cd SVFRun the build script to set up the LLVM and Z3 environments and build SVF:
source ./build.sh- By default, the build is in release mode.
- For debug mode, run:
source ./build.sh debugNote: Ensure the script is executed with source to properly configure the environment.
Clone the RecTopo repository to your local machine:
git clone https://github.com/JoelYYoung/RecTopo.git
cd RecTopoThere are three recursion handling modes:
widen-narrow: Applies both widening and narrowing at the cycle head of recursive functions.widen-only: Applies only widening at the cycle head of recursive functions.top: Assigns the top value to return values and any stored object pointed by q at *q = p in recursive functions.
Analyze programs involving recursive functions:
ae -handle-recur=<recursion handling mode> <bc path>#include "stdbool.h"
extern void svf_print(int, char*);
int demo(int a) {
if (a >= 10000)
return a;
demo(a+1);
}
int main() {
int result = demo(0);
svf_print(result, "result");
}Analyze recur.bc in widen-narrow mode:
ae -handle-recur=widen-narrow ./recur.bcIn this mode, the analysis yields:
Text: result, Value: ValVar ID: 55
%3 = load i32, ptr %1, align 4 , PrintVal: [10000, 10000], Loc:CallICFGNode:
The output indicates that in this mode, the interval for result is [10000, 10000]. The interval of parameter a is first widened to [10000, +∞], and then narrowed to [10000, 10000] based on the path condition of the second if branch. For comparison:
- In
widen-onlymode, the interval forresultis[10000, +∞]. - In
topmode, the interval forresultis[-∞, +∞].
#include "stdbool.h"
extern void svf_print(int, char*);
int mc91(int p){
if(p > 100){
return p - 10;
}else{
int p1 = p + 11;
int p2 = mc91(p1);
int result = mc91(p2);
return result;
}
}
int main(){
int result = mc91(40);
svf_print(result, "result");
}Analyze mc91.bc in widen-narrow mode:
ae -handle-recur=widen-narrow ./mc91.bcIn this mode, the analysis yields:
Text: result, Value: ValVar ID: 74
%3 = load i32, ptr %1, align 4 , PrintVal: [91, +∞], Loc:CallICFGNode:
The output indicates that the interval for result is [91, +∞].
#include "stdbool.h"
#include "stdio.h"
extern void svf_print(int, char*);
unsigned int id(unsigned int x) {
if (x==0) return 0;
unsigned int ret = id(x-1) + 1;
if (ret > 2) return 2;
return ret;
}
int main(){
int x;
scanf("%ud", &x);
int result = id(x);
svf_print(result, "result");
}Analyze id.bc in widen-narrow mode:
ae -handle-recur=widen-narrow ./id.bcIn this mode, the analysis yields:
Text: result, Value: ValVar ID: 77
%6 = load i32, ptr %2, align 4 , PrintVal: [0, 2], Loc:CallICFGNode:
The output indicates that the interval for result is [0, 2].