# --------2024.09.18--------
> Website: https://blog.csdn.net/qq_42295733/article/details/87453719?spm=1001.2101.3001.6650.6&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogOpenSearchComplete%7ERate-6-87453719-blog-116804152.235%5Ev43%5Epc_blog_bottom_relevance_base4&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogOpenSearchComplete%7ERate-6-87453719-blog-116804152.235%5Ev43%5Epc_blog_bottom_relevance_base4&utm_relevant_index=10#commentBox
```bash=
#Clone defects4j
#Env variable.
export D4J_HOME=~/defects4j/framework/bin
export PATH=$D4J_HOME/bin:$PATH
```
:::danger
## Problem of cpan DBI
```BASH=
cpan DBI
sudo apt update
sudo apt install build-essential
sudo apt install libdbi-perl
cpanm --installdeps .
sudo cpanm DBI
```
:::
# --------2024.10--------
SimFix
依照原本APR的方法去執行,在驗證補丁的時候需要把test all test cases這個function變成只測試failed test cases,
如此一來第二輪時,就可以使用原先failed test cases 相似的passed test cases進行錯誤定位進行修復
```bash=
#!/bin/bash
pwd
home_dir=(pwd)
simfix_dir=$home_dir/SimFix/
chart_dir=$home_dir/chart/chart_1_buggy/
spectra_dir=$simfix_dir/spectra/Chart
cd $simfix_dir
pwd
cd $simfix_dir/sbfl/
unzip data.zip
chmod +x *.sh
./setup.sh
./sbfl.sh Chart 1 $chart_dir
cp -i $spectra_dir/1 $simfix/final
```
# Gzoltar clone github
## Requirement: maven
```bash=
sudo apt-get install maven ## With current java version
git clone https://github.com/GZoltar/gzoltar.git
git checkout “switch_version”
mvn package
cd “gzolatrcli file”/target
#/** Use com.gzoltar.cli-1.7.3-jar-with-dependencies.jar **/
### Method-1
Step1. -> listTest.sh ## To list the test cases from relevant_test
Step2. -> serFile.sh ## To generate the ser.file though the sbfl tech.
Step3. -> genReport.sh ## To generate the report
### Method-2
-> run_gzoltar.sh ## Notice: Make sure the path is correct.
cd “path_to_your_work_dir” ## Checked out project
cd sfl/txt ## The gzoltar result
```
# -------2024.11.-------- (整理)
## SimFix clone github
> git clone https://github.com/xgdsmileboy/SimFix.git
SimFix作者在github上面要求unzip the sbfl/data.zip,但是d4j-info裡面的資料是defects4j v1.0.0的資料,Follow the command line that I traced to treat what you want it.
:::info
### Information for SimFix
#### d4j-info: all_tests, failed_tests, failed_tests_ori, location, src_path, project.json
1. all_tests: All dev-test-suites
defects4j test (In work_dir will generate the all_tests.txt)
2. failed_tests: Failed test case name
defects4j test -w (work_dir) >> (testcase.txt)
python testFilter.py ## To filter out the test cases name
3. failed_tests_ori: Failed test case exception thrown
defects4j coverage -w (work_dir)/ -t (get the failed test case name from above)
4. location: Though the SBFL tech. output
python testRank.py ## Sort the gzoltar result -> txt
:::
# SimFix with gzoltar
:::warning
**Note.**
Execute the sbfl/setup.sh is fine, but should replace the defects4j-file in sbfl/defects4j and switch to the correct defects4j version
:::
```bash=
/.setup.sh
cd sbfl/gzoltar
/run_gzoltar.sh $PID $VID $PROJ_DIR $SPECTRA_RESULT $GZOLTAR_JAR"
$COMP_SPECTRA ## Output is a “stmt-sus.txt” for SimFix/d4j-info/location
```
# –––-2025.01–––-
# Mutate defects4j v1.2.0
:::warning
**Note.**
對defects4jv1.2.0 植入錯誤,使用的工具: Major mutation framework
> https://mutation-testing.org/docs.html
:::
1. 在defects4j的論文當中,作者就有提到會將Major導入,由於在論文以及github解釋的不多,因此將實驗的過程以及作法記錄在這個note裡面
2. 值得注意的是defects4j v1.xx的版本中,使用的java版本為java 1.7,這邊可能會跟Major使用的java 1.8有所衝突,不過Major也有提供使用java1.7的版本,在下載Major的時候要注意一下
:::danger
變異準則之後再補充,以mml的形式去呈現
:::
分成以下兩點討論:
## Major framework from official
:::info
下面這個 xxx.mml 是一個可自定義Mutate的(演算法?)或者是(遊戲規則?),我稱它為(變異準則),在這個變異準則裡面,官方總共有8種準則,準則都已經寫好了,接下來就是要針對你的目標對象,也就是CheckeOut project,最底下有個targetop<xxx.xxx.xxx>,xxx存放的是你的source資料夾的位置,根據資料夾位置裡面所有的java,Major會遍歷整個資料夾,所以不需要再使用*去處理
:::
```bash=
// A simple mml file to generate all mutants
list_aor={+,-,*,/,%};
list_lor={&,|,^};
list_sor={<<,>>,>>>};
list_oru={+,-,~};
BIN(+)->list_aor;
BIN(-)->list_aor;
BIN(*)->list_aor;
BIN(/)->list_aor;
BIN(%)->list_aor;
BIN(>>)->list_sor;
BIN(<<)->list_sor;
BIN(>>>)->list_sor;
BIN(&)->list_lor;
BIN(|)->list_lor;
BIN(^)->list_lor;
UNR(+)->list_oru;
UNR(-)->list_oru;
UNR(~)->list_oru;
// Use sufficient replacements for ROR
BIN(>)->{>=,!=,FALSE};
BIN(<)->{<=,!=,FALSE};
BIN(>=)->{>,==,TRUE};
BIN(<=)->{<,==,TRUE};
BIN(==)->{<=,>=,FALSE,LHS,RHS};
BIN(!=)->{<,>,TRUE,LHS,RHS};
// Use sufficient replacements for COR
BIN(&&)->{==,LHS,RHS,FALSE};
BIN(||)->{!=,LHS,RHS,TRUE};
// Delete all types of supported statements
DEL(CALL);
DEL(INC);
DEL(DEC);
DEL(ASSIGN);
DEL(CONT);
DEL(BREAK);
DEL(RETURN);
// Enable all operators
AOR;
LOR;
SOR;
COR;
ROR;
LVR;
ORU;
STD;
// Call the defined operator group for the target method
targetOp<"org.jfree.chart">;
targetOp<"org.jfree.data">;
```
mml本來是一個plain text file,要對其進行compile的話,需使用官方給的mmmlc去編譯
```bash=
/path_to_major/bin/mmlc xxx.mml xyz.mml.bin
```
生成.mml.bin之後,就可以做變異了,利用javac去call func.
-d = copmile變異的java存放位置
```bash=
//example chart-1
/path_to_major/bin/javac \
-Xmutator=/path_to_mml.bin_loc/xyz.mml.bin -d \
/path_to_output_file/ \
/{home_dir}/chart-1/source/
```
官方也有提供ant的做法,但是再ant的作法可能會出現幾個問題,接下來就是我不確定的也沒得驗證正確性的結果
:::info
Ant 需要對build.xml這個檔案去做事,你需要再build.xml裡面添加下面以下command line
```bash=
<property name="mutOp" value=":NONE"/>
<property name="mutator" value="-XMutator${mutOp}"/>
<property name="major" value="{HOME_DIR}/major/bin/javac"/> <!-- 替換 Major 路徑 -->
<!-- JAR 依賴 -->
<property name="servlet.jar" value="{HOME_DIR}/chart-1-fixed/lib/servlet.jar"/>
<property name="iText.jar" value="{HOME_DIR}/chart-1-fixed/lib/iText-xxx.jar"/>
<property name="junit.jar" value="{HOME_DIR}/chart-1-fixed/lib/junit-xxx.jar"/>
<!-- INFO: 每個taregt 底下的classpath 都要添加: -->
<pathelement path="${servlet.jar}"/>
<pathelement path="${iText.jar}"/>
<pathelement path="${junit.jar}"/>
```
:::
:::warning
不過當你去呼叫mutation.test的時候,就會跑出很多紅色的error,並且junit最後會跟你說你的Mutants沒有做任何事...
:::
## Major framework from defects4j
:::info
使用defects4j command其實就能將mutate的java export出來,這邊感謝 neelofarhassan 對defects4j作者的提問,
> https://github.com/rjust/defects4j/issues/328
以下彙整
:::
```bash=
### 需要先將OPT export出來,一方面把mutant export以及路徑都要export
export MAJOR_OPT="-J-Dmajor.export.mutants=true -J-Dmajor.export.directory=xyz"
defects4j mutation
cd {WORK_DIR}/xyz
# 有多少數字就是有多少個mutants,但是defects4j只對 faulty source class mutate
# 目前還不知道原因為何
```
## Using D4J command to mutate the checkedout porject with customize mml
這邊要說明的是如何使用defects4j mutation 這個指令去跑你的自定義mml,接下來講的路徑要搞清楚,在操作時建議多資料夾一起看比較不會混淆
P.S: 可能有更好的方法,但是我不知道如何處理比較好,現階段就先這樣處理
### Step1: Compile mml
:::info
Major framework file裡面一定都會有mmlc javac這兩個檔案
```bash=
### compile 出來的bin檔案可改名稱
/path_to_major/bin/mmlc custom.mml custom.mml.bin
```
:::
### Step2: Find your target classes
:::info
如果你有要針對的java source 想要找出來,但是目標太多可以使用Linux指令去處理
```bash=
# find /path_to_source_path/.../ -name ...
# sed 's|source/||' 刪除source這個路徑,因為defects4j路徑的問題
find source/org -name "*.java" | sed 's|source/||' | sed 's|/|.|g' | sed 's|\.java$||g' > all_classes.txt
```
:::
### Step3-1: Mutate the target source
:::info
這邊做的就是變異source code,這方法比較複雜,知道有這個就好
1.javac -cp(classpath): 可以放lib、target、test等等
2.xMutator: 變異因子(準則)
3.-d: output location
4.-J-Dmajor.export.mutants=true: export mutate的所有.java
5.@sources.txt: target classes txt
```bash=
/path_to_major/bin/javac \
-cp /path_to_checkout_project/lib/servlet.jar:/path/lib/iText-2.1.4.jar:/path/lib/junit.jar \
-XMutator=/path_to_mml_loc/custom.mml.bin \
-d /path_to_checkout_project/output \
-J-Dmajor.export.mutants=true \
-J-Dmajor.log.level=DEBUG \
-Xlint:deprecation -Xlint:unchecked \
@sources.txt
```
:::
### Step3-2: Mutate the target source
:::info
defects4j mutation -i <instrument_classes>: 變異你指定在txt裡面的classes,所以step2的用意就在這
```bash=
defects4j mutation -i xxx.txt
```
:::
:::success
假如,你今天mutate之後,想再做一次mutate的話,你必須要把再checkout project file裡面的build出來的檔案以及"hidden file>.classes.mutated"都刪掉,這樣再跑一次的時候才不會出錯
:::
如果你只使用上面的指令,是無法用自定義的mml去做mutate的,所以你得先到defects4j>major>mml>template.mml去改寫,建議複製一份原檔
:::danger
原因: defects4j>framework>core>Mutation.pm這個腳本裡面就是使用這個,再加上如果你在checkout_project裡面做defects4j mutation在你的checkout file裡面的hidden file裡面會有一個.mml的檔案,裡面有一個default mml,上面就指定對faulty classes做mutate而已
:::
:::info
這是我一開始測試的mml,可以work
```bash=
list_aor={+,-,*,/,%};
list_lor={&,|,^};
list_sor={<<,>>,>>>};
list_oru={+,-,~};
// Replacements for the AOR operator
BIN(+)->list_aor;
BIN(-)->list_aor;
BIN(*)->list_aor;
BIN(/)->list_aor;
BIN(%)->list_aor;
-LVR<"org.apache.commons.math3.util.FastMathLiteralArrays">;
-ORU<"org.apache.commons.math3.util.FastMathLiteralArrays">;
-LVR<"org.apache.commons.math.optimization.direct.BOBYQAOptimizer@bobyqb">;
-STD<"org.apache.commons.math.optimization.direct.BOBYQAOptimizer@bobyqb">;
```
:::