├── .gitattributes ├── Dockerfile ├── LICENSE ├── PathEval_Preprint.pdf ├── README.md ├── data ├── logic_bombs_c.jsonl ├── patheval_cpp.jsonl ├── patheval_java.jsonl ├── patheval_py.jsonl └── rw │ └── coreutils │ ├── build.sh │ ├── check.sh │ ├── info.json │ └── p.c ├── example.py ├── patheval.py ├── sample.jsonl ├── scripts ├── c_check.py ├── cpp_check.py ├── include │ ├── a_tester.h │ ├── aes.h │ ├── sha1.h │ └── utils.h ├── java_check.py ├── lib │ ├── aes.c │ ├── crypto_utils.c │ ├── sha1.c │ └── utils.c ├── py_check.py └── utils.py └── utils.py /.gitattributes: -------------------------------------------------------------------------------- 1 | .pdf filter=lfs diff=lfs merge=lfs -text 2 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM ubuntu:20.04 2 | 3 | RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime 4 | RUN echo 'Asia/Shanghai' >/etc/timezone 5 | 6 | RUN apt update 7 | RUN apt install -y -q build-essential gcc g++ python3 python3-pip openjdk-11-jdk-headless libssl-dev 8 | RUN python3 -m pip install tqdm 9 | COPY . /work 10 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2024 CGCL-codes 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /PathEval_Preprint.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CGCL-codes/PathEval/b4eb3263598af31aec32dcc17cc1df58df783928/PathEval_Preprint.pdf -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # PathEval: Evaluation Set for Directed Test Input Generation 2 | 3 | This is an evaluation set for the problem of **directed/targeted test input generation**, especially targeting Large Language Models (LLMs). 4 | The goal of directed test input (a.k.a. targeted test input) generation is to automatically generate test inputs to reach a certain code location or produce a particular result. 5 | 6 | For example: 7 | ```c 8 | // EXAMPLE 1: 9 | // generate an input of `s` that can reach the target line: 10 | int arr(char *s){ 11 | int symvar = s[0] - 48; 12 | int ary[] ={1,2,3,4,5}; 13 | if (ary[symvar%5] == 5){ 14 | // target line 15 | } 16 | } 17 | 18 | // EXAMPLE 2: 19 | // or produce the expected output 20 | int execute(char* s) { 21 | int ret = system(s); 22 | if(ret == 0){ 23 | return OK; // expected 24 | } 25 | return ERR; 26 | } 27 | ``` 28 | 29 | Targeted input generation plays a crucial role in various software engineering and security tasks, including fuzzing, bug reproduction (where the target is the bug location), and test suite augmentation (where the target is the specific code to be covered). It has also been extensively integrated with conventional testing tools to enhance coverage and overall performance. 30 | 31 | Constraint-based techniques, such as symbolic execution 32 | and concolic testing, have been well-explored in this problem while Large Language Models (LLMs) have demonstrated exceptionally good performance in code understanding and reasoning. 33 | We use PathEval to benchmark and evaluate the ability of LLMs to solve the problem of directed test input generation. 34 | 35 | 36 | LLMs and constraint-based tools have distinct advantages and disadvantages towards this problem. 37 | For instance, it is easy for constrint-based tools to generate the target input for the above EXAMPLE 1 while being challenging for LLMs. 38 | Interestingly, it is the opposite for EXAMPLE 2. 39 | 40 | ## Installation 41 | 42 | ### Linux (Debian) 43 | Install gcc, g++, python3 and openjdk by the following commands. 44 | ```shell 45 | apt update 46 | apt install -y -q build-essential gcc g++ python3 python3-pip openjdk-11-jdk-headless libssl-dev 47 | python3 -m pip install tqdm 48 | 49 | git clone https://github.com/CGCL-codes/PathEval.git 50 | cd PathEval 51 | ``` 52 | 53 | ### Docker 54 | ```shell 55 | git clone https://github.com/CGCL-codes/PathEval.git 56 | cd PathEval 57 | 58 | docker build -t patheval . 59 | docker run -it --rm patheval /bin/bash 60 | 61 | # cd /work in container 62 | ``` 63 | 64 | ## Usage 65 | **The evaluation will compile and execute untrusted model-generated data and code (see scripts/*_check.py). It is strongly encouraged to run this project in a sandbox (e.g., docker container).** 66 | 67 | Users can simply use this dataset through a couple of APIs. 68 | 69 | **Example** 70 | ```python 71 | from patheval import set_dataset, read_problems, evaluate_one 72 | # select the dataset that corresponds to the language 73 | set_dataset("patheval_cpp") # or "patheval_java" or "patheval_py" or "logic_bombs_c" 74 | # load the problems 75 | problems = read_problems() 76 | # give the completion from your LLMs and the evaluate result will return. 77 | 78 | # completion = query(...) 79 | evaluate_one(problems[0], completion)['pass'] # True / False 80 | ``` 81 | 82 | Or you can validate offline with the script `patheval.py`: 83 | ```shell 84 | python3 patheval.py --dataset patheval_java --input sample.jsonl 85 | ``` 86 | For the optional dataset see the documentation for `set_dataset` below. 87 | The format of `sample.jsonl` is as follows, the order doesn't matter. 88 | ```text 89 | {"index": 486, "completion": "\"Mary had a little lamb\", 4"} 90 | {"index": 532, "completion": "\"Hello,Hello,world !\" "} 91 | {"index": 572, "completion": "Arrays.asList(1.0, 2., 3.)"} 92 | ... 93 | ``` 94 | We expect that each question can have **one or more rows of answers (multiple rows of data with the same index)**. If a question does not have a corresponding answer, then it fails by default. 95 | 96 | **APIs** 97 | 98 | **`set_dataset(dataset_name)`** 99 | - Purpose: Selects the dataset for evaluation. 100 | - Parameters: 101 | - `dataset_name` (str): The name of the dataset to use. Valid values are: 102 | - `"patheval_cpp"`: C++ dataset 103 | - `"patheval_java"`: Java dataset 104 | - `"patheval_py"`: Python dataset 105 | - `"logic_bombs_c"`: logic bombs dataset (in C language) 106 | - Returns: None 107 | 108 | **`read_problems()`** 109 | - Purpose: Loads the problems from the selected dataset. 110 | - Parameters: None 111 | - Returns: `problems` (list): A list of problems. 112 | 113 | **`evaluate_one(problem, completion)`** 114 | - Purpose: Evaluate the given completion for a single problem. 115 | - Parameters: 116 | - `problem` (object): One problem from the `read_problems()` returned list. 117 | - `completion` (str): The completion generated by LLMs. 118 | - Returns: the input `problem` dictionary, with an additional key: 119 | - `"pass"` (bool): Indicates whether the completion passes the problem 120 | 121 | ### Sample 122 | For each sample, the following information is provided: 123 | | Key | Description | 124 | | ---- | -----------| 125 | | index | index in **this dataset** | 126 | | humaneval_task_id | task id in **HumanEval** | 127 | | focal_method_name | function name of focal method | 128 | | focal_method_para | parameters of focal method | 129 | | focal_method_return_type | return value type of focal method | 130 | | focal_method | code of focal method | 131 | | target | code of target | 132 | 133 | *In logic_bombs samples, humaneval_task_id is replaced by logic_bombs_task_id.* 134 | 135 | The samples are placed under the `data` folder in `.jsonl` files, the samples in the three different files are semantically equivalent, but are implemented in different programming languages. 136 | 137 | An example C++ sample is shown as follow. 138 | ```json 139 | { 140 | "index": 180, 141 | "humaneval_task_id": "CPP/53", 142 | "focal_method_name": "add", 143 | "focal_method_para": "(int x,int y)", 144 | "focal_method_return_type": "int", 145 | "focal_method": "#include\n#include\nusing namespace std;\n#include\n#include\nint add(int x,int y){\n return x+y;\n}", 146 | "target": "#undef NDEBUG\n#include\n\nint main(){\n\tauto result = add();\n\tassert(result==5);\n}" 147 | } 148 | ``` 149 | We use `` to mark the position for LLMs to fill in. 150 | 151 | ## DataSet Extension 152 | We are extending the dataset from real-world open-source projects, a sample is shown under `data/rw`. These samples will be added to this repo soon. 153 | 154 | ## Known Issues 155 | There exists subtle difference in the number of samples in the dataset for the three programming languages due to the fact that this dataset was converted from [HumanEval-X](https://huggingface.co/datasets/THUDM/humaneval-x) in a automated manner, where a very small number of samples failed in the automated process and were thus discarded. 156 | 157 | ## Citation 158 | This is originally created for our paper "Towards Understanding the Effectiveness of Large Language Models on Directed Test Input Generation" (ASE 2024). The preview version is [here](./PathEval_Preprint.pdf). 159 | 160 | ```text 161 | @inproceedings{DBLP:conf/kbse/Jiang0CS024, 162 | author = {Zongze Jiang and 163 | Ming Wen and 164 | Jialun Cao and 165 | Xuanhua Shi and 166 | Hai Jin}, 167 | editor = {Vladimir Filkov and 168 | Baishakhi Ray and 169 | Minghui Zhou}, 170 | title = {Towards Understanding the Effectiveness of Large Language Models on 171 | Directed Test Input Generation}, 172 | booktitle = {Proceedings of the 39th {IEEE/ACM} International Conference on Automated 173 | Software Engineering, {ASE} 2024, Sacramento, CA, USA, October 27 174 | - November 1, 2024}, 175 | pages = {1408--1420}, 176 | publisher = {{ACM}}, 177 | year = {2024}, 178 | url = {https://doi.org/10.1145/3691620.3695513}, 179 | doi = {10.1145/3691620.3695513}, 180 | timestamp = {Mon, 03 Mar 2025 21:16:48 +0100}, 181 | biburl = {https://dblp.org/rec/conf/kbse/Jiang0CS024.bib}, 182 | bibsource = {dblp computer science bibliography, https://dblp.org} 183 | } 184 | ``` 185 | -------------------------------------------------------------------------------- /data/logic_bombs_c.jsonl: -------------------------------------------------------------------------------- 1 | {"index": 0, "logic_bombs_task_id": "ln_ef_l2", "focal_method_name": "logic_bomb", "focal_method_para": "(char* s)", "focal_method_return_type": "int", "focal_method": "\n#include \n#include \n\n\nint logic_bomb(char* s) {\n int symvar = s[0] - 48;\n double d = log(symvar); \n if(1.94 < d && d < 1.95){\n return 1;\n }else{\n return 0;\n }\n}\n", "target": "#include \"assert.h\"\nint main(){\n\tassert(logic_bomb() == 1);\n}"} 2 | {"index": 1, "logic_bombs_task_id": "sin_ef_l2", "focal_method_name": "logic_bomb", "focal_method_para": "(char* s)", "focal_method_return_type": "int", "focal_method": "\n#include \n#include \n\n#define PI 3.14159265358979323846264338327\n\n\n\nint logic_bomb(char* s) {\n int symvar = s[0];\n float v = sin(symvar*PI/30);\n if(v > 0.5){\n return 1;\n }else{\n return 0;\n }\n}\n", "target": "#include \"assert.h\"\nint main(){\n\tassert(logic_bomb() == 1);\n}"} 3 | {"index": 2, "logic_bombs_task_id": "atoi_ef_l2", "focal_method_name": "logic_bomb", "focal_method_para": "(char* s)", "focal_method_return_type": "int", "focal_method": "\n#include \n#include \n\n\nint logic_bomb(char* symvar) {\n int i = atoi(symvar);\n if(i==7){\n return 1;\n }else{\n return 0;\n }\n}\n", "target": "#include \"assert.h\"\nint main(){\n\tassert(logic_bomb() == 1);\n}"} 4 | {"index": 3, "logic_bombs_task_id": "printint_int_l1", "focal_method_name": "logic_bomb", "focal_method_para": "(char* s)", "focal_method_return_type": "int", "focal_method": "\n#include \n\n\nint logic_bomb(char* s) {\n int symvar = s[0] - 48;\n int x = symvar + 190;\n printf(\"x = %d\\n\", x);\n if(x == 197){\n return 1;\n }else{\n return 0;\n }\n}\n", "target": "#include \"assert.h\"\nint main(){\n\tassert(logic_bomb() == 1);\n}"} 5 | {"index": 4, "logic_bombs_task_id": "atof_ef_l2", "focal_method_name": "logic_bomb", "focal_method_para": "(char* s)", "focal_method_return_type": "int", "focal_method": "\n#include \n#include \n\n\nint logic_bomb(char* symvar) {\n float i = atof(symvar);\n if(i - 7 == 0){\n return 1;\n }else{\n return 0;\n }\n}\n", "target": "#include \"assert.h\"\nint main(){\n\tassert(logic_bomb() == 1);\n}"} 6 | {"index": 5, "logic_bombs_task_id": "printfloat_ef_l1", "focal_method_name": "logic_bomb", "focal_method_para": "(char* s)", "focal_method_return_type": "int", "focal_method": "\n#include \n\n\nint logic_bomb(char* s) {\n int symvar = s[0] - 48;\n float x = symvar + 190;\n printf(\"x = %f\\n\", x);\n if(x - 197 == 0){\n return 1;\n }else{\n return 0;\n }\n}\n", "target": "#include \"assert.h\"\nint main(){\n\tassert(logic_bomb() == 1);\n}"} 7 | {"index": 6, "logic_bombs_task_id": "rand_ef_l2", "focal_method_name": "logic_bomb", "focal_method_para": "(char* s)", "focal_method_return_type": "int", "focal_method": "\n#include \n\n\nint logic_bomb(char* s) {\n int symvar = s[0];\n srand(symvar);\n int r = rand()%100;\n if(r == 77){\n return 1;\n }else{\n return 0;\n }\n}\n", "target": "#include \"assert.h\"\nint main(){\n\tassert(logic_bomb() == 1);\n}"} 8 | {"index": 7, "logic_bombs_task_id": "pow_ef_l2", "focal_method_name": "logic_bomb", "focal_method_para": "(char* s)", "focal_method_return_type": "int", "focal_method": "\n#include \n#include \n\n\nint logic_bomb(char* s) {\n int symvar = s[0] - 48;\n if(pow(symvar, 2) == 49){\n return 1;\n }else{\n return 0;\n }\n}\n", "target": "#include \"assert.h\"\nint main(){\n\tassert(logic_bomb() == 1);\n}"} 9 | {"index": 8, "logic_bombs_task_id": "jmp_sj_l1", "focal_method_name": "logic_bomb", "focal_method_para": "(char* s)", "focal_method_return_type": "int", "focal_method": "#include \n\n#define jmp(addr) asm(\"jmp *%0\"::\"r\"(addr):)\n\n\n\nint logic_bomb(char* s) {\n int symvar = s[0] - 48;\n if (symvar%6 != 1 || symvar < 10|| symvar > 40 || symvar == 19)\n\tsymvar = 13;\n long long addr = &&flag_0 + symvar;\n jmp(addr);\n flag_0:\n if (symvar > 0){\n symvar++;\n if(symvar == 0)\n return 1;\n }\n return 0;\n}\n", "target": "#include \"assert.h\"\nint main(){\n\tassert(logic_bomb() == 1);\n}"} 10 | {"index": 9, "logic_bombs_task_id": "arrayjmp_sj_l2", "focal_method_name": "logic_bomb", "focal_method_para": "(char* s)", "focal_method_return_type": "int", "focal_method": "#include \n\n#define jmp(addr) asm(\"jmp *%0\"::\"r\"(addr):)\n\n\n\nint logic_bomb(char* s) {\n int symvar = s[0] - 48;\n int array[] = {7,13,14,15,16,21,22,37,23,24};\n long long addr = &&flag_0 + array[symvar%10];\n jmp(addr);\n flag_0:\n if (symvar > 0){\n symvar++;\n if(symvar == 0)\n return 1;\n }\n return 0;\n}\n", "target": "#include \"assert.h\"\nint main(){\n\tassert(logic_bomb() == 1);\n}"} 11 | {"index": 10, "logic_bombs_task_id": "pointers_sj_l1", "focal_method_name": "logic_bomb", "focal_method_para": "(char* s)", "focal_method_return_type": "int", "focal_method": "#include \n\n\nint func0(){return 0;}\nint func1(){return 1;}\nint func2(){return 2;}\nint func3(){return 3;}\nint func4(){return 4;}\nint func5(){return 5;}\nint func6(){return 6;}\n\n\nint logic_bomb(char* s) {\n int (*f[7])() = {func0, func1, func2, func3, func4, func5, func6};\n int symvar = s[0] - 48;\n int ret = f[symvar%7](); \n printf (\"ret = %d\\n\", ret);\n if (ret == 5){\n return 1;\n }\n return 0;\n}\n", "target": "#include \"assert.h\"\nint main(){\n\tassert(logic_bomb() == 1);\n}"} 12 | {"index": 11, "logic_bombs_task_id": "stackoutofbound_sm_l2", "focal_method_name": "logic_bomb", "focal_method_para": "(char* s)", "focal_method_return_type": "int", "focal_method": "#include \n\n\nint logic_bomb(char* s) {\n int symvar = s[0] - 48;\n int a[] = {1, 2, 3, 4, 5, 6};\n if (a[symvar]<0 || a[symvar] > 6){\n return 1;\n }\n return 0;\n}\n", "target": "#include \"assert.h\"\nint main(){\n\tassert(logic_bomb() == 1);\n}"} 13 | {"index": 12, "logic_bombs_task_id": "stackarray_sm_l2", "focal_method_name": "logic_bomb", "focal_method_para": "(char* s)", "focal_method_return_type": "int", "focal_method": "\n#include\n\n\n\nint logic_bomb(char* s) {\n int symvar = s[0] - 48;\n int l1_ary[] ={1,2,3,4,5}; \n int l2_ary[] ={6,7,8,9,10}; \n\n int x = symvar%5;\n if(l2_ary[l1_ary[x]] == 9){\n return 1;\n }\n else\n return 0;\n}\n", "target": "#include \"assert.h\"\nint main(){\n\tassert(logic_bomb() == 1);\n}"} 14 | {"index": 13, "logic_bombs_task_id": "heapoutofbound_sm_l2", "focal_method_name": "logic_bomb", "focal_method_para": "(char* s)", "focal_method_return_type": "int", "focal_method": "#include\n#include\n\n\nint logic_bomb(char* s) {\n int symvar = s[0] - 48;\n int *array = (int *) malloc(sizeof(int) * 10);\n int k = 0;\n for (k=0; k<10; k++){\n\tarray[k] = k;\n }\n if(array[symvar]<0 || array[symvar]>10){\n return 1;\n }\n return 0;\n}\n", "target": "#include \"assert.h\"\nint main(){\n\tassert(logic_bomb() == 1);\n}"} 15 | {"index": 14, "logic_bombs_task_id": "stackarray_sm_ln", "focal_method_name": "logic_bomb", "focal_method_para": "(char* s)", "focal_method_return_type": "int", "focal_method": "\n#include\n\n\n\nint logic_bomb(char* s) {\n int symvar = s[0] - 48;\n int i = symvar;\n int j = abs(i%11);\n int a[] = {(i+5)%11,(i+6)%11,(i+7)%11,(i+8)%11,(i+9)%11,(i+10)%11,i%11,(i+1)%11,(i+2)%11,(i+3)%11,(i+4)%11};\n int* p = &a[a[a[j]]];\n int* q = &a[a[a[a[a[*p]]]]];\n\n if(p == q){\n return 1;\n }\n else\n return 0;\n}\n", "target": "#include \"assert.h\"\nint main(){\n\tassert(logic_bomb() == 1);\n}"} 16 | {"index": 15, "logic_bombs_task_id": "malloc_sm_l1", "focal_method_name": "logic_bomb", "focal_method_para": "(char* s)", "focal_method_return_type": "int", "focal_method": "#include\n#include\n\n\nint logic_bomb(char* s) {\n int symvar = s[0] - 48;\n int *array = (int *) malloc(sizeof(int) * 10);\n int k = 0;\n for (k=0; k<10; k++){\n\tarray[k] = k;\n }\n if(array[symvar % 10] == 7){\n return 1;\n }\n return 0;\n}\n", "target": "#include \"assert.h\"\nint main(){\n\tassert(logic_bomb() == 1);\n}"} 17 | {"index": 16, "logic_bombs_task_id": "realloc_sm_l1", "focal_method_name": "logic_bomb", "focal_method_para": "(char* s)", "focal_method_return_type": "int", "focal_method": "#include\n#include\n\n\nint logic_bomb(char* s) {\n int symvar = s[0] - 48;\n int *array = (int *) malloc(sizeof(int) * 5);\n int k = 0;\n for (k=0; k<5; k++){\n array[k] = k;\n }\n array = (int *) realloc (array, sizeof(int) * 10);\n for (k=5; k<10; k++){\n array[k] = k;\n }\n if(array[symvar%10] == 7){\n return 1;\n }\n return 0;\n}\n\n", "target": "#include \"assert.h\"\nint main(){\n\tassert(logic_bomb() == 1);\n}"} 18 | {"index": 17, "logic_bombs_task_id": "stackarray_sm_l1", "focal_method_name": "logic_bomb", "focal_method_para": "(char* s)", "focal_method_return_type": "int", "focal_method": "#include\n\n\nint logic_bomb(char* s) {\n int symvar = s[0] - 48;\n int ary[] ={1,2,3,4,5};\n if(ary[symvar%5] == 5){\n return 1;\n }\n else\n\treturn 0;\n}\n", "target": "#include \"assert.h\"\nint main(){\n\tassert(logic_bomb() == 1);\n}"} 19 | {"index": 18, "logic_bombs_task_id": "aes_cf", "focal_method_name": "logic_bomb", "focal_method_para": "(char* s)", "focal_method_return_type": "int", "focal_method": "\n#include \n#include \"aes.h\"\n\nvoid aes_print(uint8_t* str) {\n unsigned char i;\n for(i = 0; i < 16; ++i)\n printf(\"%.2x\", str[i]);\n printf(\"\\n\");\n}\n\n\n\nint logic_bomb(char* s) {\n if(strlen(s) != 32){\n \n\treturn 0;\n }\n\n uint8_t key[16];\n\n sscanf(s,\n \"%2\" SCNx8 \"%2\" SCNx8\n \"%2\" SCNx8 \"%2\" SCNx8\n \"%2\" SCNx8 \"%2\" SCNx8\n \"%2\" SCNx8 \"%2\" SCNx8\n \t\"%2\" SCNx8 \"%2\" SCNx8\n \t\"%2\" SCNx8 \"%2\" SCNx8\n \t\"%2\" SCNx8 \"%2\" SCNx8\n \t\"%2\" SCNx8 \"%2\" SCNx8,\n \t&key[0],&key[1],\n \t&key[2],&key[3],\n \t&key[4],&key[5],\n \t&key[6],&key[7],\n \t&key[8],&key[9],\n \t&key[10],&key[11],\n \t&key[12],&key[13],\n \t&key[14],&key[15]);\n\n \n\n uint8_t decodetext[16];\n uint8_t ciphertext[] = {0x3a, 0xd7, 0x7b, 0xb4, 0x0d, 0x7a, 0x36, 0x60, 0xa8, 0x9e, 0xca, 0xf3, 0x24, 0x66, 0xef, 0x97};\n uint8_t plaintext[] = {0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a};\n\n AES128_ECB_decrypt(ciphertext, key, decodetext);\n\n \n if(0 == memcmp((char*) plaintext, (char*) decodetext, 16)){\n return 1;\n }else{\n return 0;\n }\n}\n", "target": "#include \"assert.h\"\nint main(){\n\tassert(logic_bomb() == 1);\n}"} 20 | {"index": 19, "logic_bombs_task_id": "sha_cf", "focal_method_name": "logic_bomb", "focal_method_para": "(char* s)", "focal_method_return_type": "int", "focal_method": "\n#include \"sha1.h\"\n\n\n\nint logic_bomb(char* s) {\n int symvar = s[0] - 48;\n int plaintext = symvar;\n unsigned cipher[5];\n cipher[0] = 0X902ba3cd;\n cipher[1] = 0Xa1883801;\n cipher[2] = 0X594b6e1b;\n cipher[3] = 0X452790cc;\n cipher[4] = 0X53948fda;\n\n if(SHA1_COMP(plaintext,cipher)==0){\n return 1;\n }else{\n return 0;\n }\n}\n", "target": "#include \"assert.h\"\nint main(){\n\tassert(logic_bomb() == 1);\n}"} 21 | {"index": 20, "logic_bombs_task_id": "df2cf_cp_l1", "focal_method_name": "logic_bomb", "focal_method_para": "(char* s)", "focal_method_return_type": "int", "focal_method": "\n#include \n\n\nint df2cf(char a)\n{\n int b;\n switch(a){\n case 0:\n b = 0;\n\tbreak;\n case 1:\n b = 1;\n\tbreak;\n case 2:\n b = 2;\n\tbreak;\n case 3:\n b = 3;\n\tbreak;\n case 4:\n b = 4;\n\tbreak;\n case 5:\n b = 5;\n\tbreak;\n case 6:\n b = 6;\n\tbreak;\n case 7:\n b = 7;\n\tbreak;\n case 8:\n b = 8;\n\tbreak;\n case 9:\n b = 9;\n\tbreak;\n default:\n b = 0;\n break;\n }\n return b;\n}\n\n\nint logic_bomb(char* s) {\n int symvar = s[0] - 48;\n int a = df2cf(symvar%10);\n a++;\n int b = symvar + a;\n if(b == 15)\n return 1;\n else\n return 0;\n}\n", "target": "#include \"assert.h\"\nint main(){\n\tassert(logic_bomb() == 1);\n}"} 22 | {"index": 21, "logic_bombs_task_id": "stack_cp_l1", "focal_method_name": "logic_bomb", "focal_method_para": "(char* s)", "focal_method_return_type": "int", "focal_method": "\n\n\n\nint logic_bomb(char* s) {\n int symvar = s[0] - 48;\n int j;\n __asm__ __volatile__(\"push %0\" :: \"m\"(symvar));\n __asm__ __volatile__(\"pop %0\" :: \"m\"(j));\n if(j == 7){\n return 1;\n } else{\n return 0;\n }\n}\n", "target": "#include \"assert.h\"\nint main(){\n\tassert(logic_bomb() == 1);\n}"} 23 | {"index": 22, "logic_bombs_task_id": "echofile_cp_l1", "focal_method_name": "logic_bomb", "focal_method_para": "(char* s)", "focal_method_return_type": "int", "focal_method": "\n#include \n\n\n\nint logic_bomb(char* s) {\n int symvar = s[0] - 48;\n int j;\n char file[] = \"tmp.covpro\";\n char cmd[256];\n sprintf(cmd, \"echo %d > %s\\n\", symvar, file); \n system(cmd);\n\n FILE *fp = stdin;\n fp = fopen(file, \"r\");\n fscanf(fp,\"%d\",&j);\n fclose(fp);\n remove(file);\n\n if(j == 7){\n return 1;\n } else{\n return 0;\n }\n}\n", "target": "#include \"assert.h\"\nint main(){\n\tassert(logic_bomb() == 1);\n}"} 24 | {"index": 23, "logic_bombs_task_id": "echo_cp_l1", "focal_method_name": "logic_bomb", "focal_method_para": "(char* s)", "focal_method_return_type": "int", "focal_method": "#include \n\n\nchar* shell(const char* cmd)\n{\n char* rs = \"\";\n FILE *f;\n f = popen(cmd, \"r\");\n char buf[1024];\n memset(buf,'\\0',sizeof(buf));\n while(fgets(buf,1024-1,f)!=NULL)\n { \n rs = buf;\n }\n\n pclose(f);\n return rs;\n}\n\n\nint logic_bomb(char* s) {\n int symvar = s[0] - 48;\n char cmd[256];\n sprintf(cmd, \"echo %d\\n\", symvar); \n char* rs = shell(cmd);\n\n if(atoi(rs) == 7)\n return 1;\n else\n return 0;\n}\n", "target": "#include \"assert.h\"\nint main(){\n\tassert(logic_bomb() == 1);\n}"} 25 | {"index": 24, "logic_bombs_task_id": "file_cp_l1", "focal_method_name": "logic_bomb", "focal_method_para": "(char* s)", "focal_method_return_type": "int", "focal_method": "\n#include \n\n\nint logic_bomb(char* s) {\n int symvar = s[0] - 48;\n int j;\n char file[] = \"tmp.covpro\";\n FILE *fp = fopen(file, \"ab+\");\n if(fp == NULL)\n {\n \n exit(1); \n }\n fprintf(fp,\"%d\",symvar);\n fclose(fp);\n\n fp = fopen(\"tmp.covpro\", \"r\");\n fscanf(fp,\"%d\",&j);\n fclose(fp);\n remove(file);\n if(j == 7){\n return 1;\n } else{\n return 0;\n }\n}\n", "target": "#include \"assert.h\"\nint main(){\n\tassert(logic_bomb() == 1);\n}"} 26 | {"index": 25, "logic_bombs_task_id": "file_posix_cp_l1", "focal_method_name": "logic_bomb", "focal_method_para": "(char* s)", "focal_method_return_type": "int", "focal_method": "#include \n\n\n\nint logic_bomb(char* s) {\n int symvar = s[0] - 48;\n int j;\n char file[] = \"tmp.covpro\";\n int fd = open(file, O_CREAT | O_WRONLY | O_TRUNC, S_IRUSR | S_IWUSR);\n if(fd < 0)\n {\n exit(-1); \n }\n write(fd, &symvar, sizeof symvar);\n close(fd);\n fd = open(file, O_RDONLY);\n read(fd, &j, sizeof j);\n close(fd);\n if(j == 7){\n return 1;\n } else{\n return 0;\n }\n}\n", "target": "#include \"assert.h\"\nint main(){\n\tassert(logic_bomb() == 1);\n}"} 27 | {"index": 26, "logic_bombs_task_id": "socket_cp_l1", "focal_method_name": "logic_bomb", "focal_method_para": "(char* s)", "focal_method_return_type": "int", "focal_method": "\n#include \n\nint server(){\n int server_sockfd,client_sockfd;\n int server_len,client_len;\n struct sockaddr_in server_address;\n struct sockaddr_in client_address;\n int i,btye;\n char char_recv,char_send;\n\n server_address.sin_family = AF_INET;\n server_address.sin_addr.s_addr = inet_addr(\"127.0.0.1\");\n server_address.sin_port = 19991;\n server_len = sizeof(server_address);\n\n server_sockfd = socket(AF_INET,SOCK_STREAM,0);\n\n bind(server_sockfd,(struct sockaddr *)&server_address,server_len);\n\n listen(server_sockfd,5);\n \n\n client_len = sizeof(client_address);\n client_sockfd = accept(server_sockfd,(struct sockaddr *)&client_address,(socklen_t *)&client_len);\n\n if(btye = recv(client_sockfd,&char_recv,1,0) == -1) {\n perror(\"recv\");\n exit(EXIT_FAILURE);\n }\n \n\n char_send = char_recv;\n if(btye = send(client_sockfd,&char_send,1,0) == -1) {\n perror(\"send\");\n exit(EXIT_FAILURE);\n }\n\n close(client_sockfd);\n close(server_sockfd);\n}\n\nint client_send(char char_send){\n \n int sockfd;\n int len;\n struct sockaddr_in address;\n int result;\n int i,byte;\n char char_recv;\n if((sockfd = socket(AF_INET,SOCK_STREAM,0)) == -1) {\n perror(\"socket\");\n exit(-1);\n }\n address.sin_family = AF_INET;\n address.sin_addr.s_addr = inet_addr(\"127.0.0.1\");\n address.sin_port = 19991;\n len = sizeof(address);\n if((result = connect(sockfd,(struct sockaddr *)&address,len)) == -1) {\n perror(\"connect\");\n exit(-1);\n }\n\n if(byte = send(sockfd,&char_send,1,0) == -1) {\n perror(\"send\");\n exit(-1);\n }\n if(byte = recv(sockfd,&char_recv,1,0) == -1) {\n perror(\"recv\");\n exit(-1);\n }\n \n int ret = char_recv - 48;\n close(sockfd);\n return ret;\n}\n\n\nint logic_bomb(char* s) {\n int pid1,status,i=0;\n pid1=fork();\n if(pid1 < 0){\n return 0;\n }\n else if(pid1 == 0){\n server();\n waitpid(NULL);\n exit(0);\n }else{\n sleep(5);\n i=client_send(s[0]);\n if(i == 7){\n return 1;\n }else{\n return 0;\n }\n }\n}\n", "target": "#include \"assert.h\"\nint main(){\n\tassert(logic_bomb() == 1);\n}"} 28 | {"index": 27, "logic_bombs_task_id": "pid_csv", "focal_method_name": "logic_bomb", "focal_method_para": "(char* s)", "focal_method_return_type": "int", "focal_method": "#define _GNU_SOURCE\n#include \n#include \n#include \n\n\n\nint logic_bomb(char* s) {\n int symvar = s[0] - 48;\n int pid = (int) getpid();\n if(pid%78 == symvar)\n return 1;\n else\n return 0;\n}\n", "target": "#include \"assert.h\"\nint main(){\n\tassert(logic_bomb() == 1);\n}"} 29 | {"index": 28, "logic_bombs_task_id": "syscall_csv", "focal_method_name": "logic_bomb", "focal_method_para": "(char* s)", "focal_method_return_type": "int", "focal_method": "\n#include \n\n\n\nint logic_bomb(char* s) {\n if(s == NULL)\n\treturn 0;\n if(s[0]=='\\0')\n\treturn 0;\n int trigger = -1;\n trigger = system(s);\n if(trigger == 0) {\n return 1;\n } else {\n return 0;\n }\n}\n", "target": "#include \"assert.h\"\nint main(){\n\tassert(logic_bomb() == 1);\n}"} 30 | {"index": 29, "logic_bombs_task_id": "file_posix_csv", "focal_method_name": "logic_bomb", "focal_method_para": "(char* s)", "focal_method_return_type": "int", "focal_method": "#include \n\n\n\nint logic_bomb(char* s) {\n int trigger = 0;\n int fd = open(s, O_RDONLY);\n if(fd != -1) {\n \ttrigger = 1;\n close(fd);\n }\n\n if(trigger) {\n return 1;\n } else {\n return 0;\n }\n}\n", "target": "#include \"assert.h\"\nint main(){\n\tassert(logic_bomb() == 1);\n}"} 31 | {"index": 30, "logic_bombs_task_id": "file_csv", "focal_method_name": "logic_bomb", "focal_method_para": "(char* s)", "focal_method_return_type": "int", "focal_method": "#include \n\n\n\nint logic_bomb(char* s) {\n int trigger = 0;\n FILE *fp = fopen(s, \"r\");\n if(fp != NULL) {\n\ttrigger = 1;\n fclose(fp);\n }\n\n if(trigger) {\n return 1;\n } else {\n return 0;\n }\n}\n", "target": "#include \"assert.h\"\nint main(){\n\tassert(logic_bomb() == 1);\n}"} 32 | {"index": 31, "logic_bombs_task_id": "ping_csv", "focal_method_name": "logic_bomb", "focal_method_para": "(char* s)", "focal_method_return_type": "int", "focal_method": "#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\n\nint ping_it(struct in_addr *dst)\n{\n struct icmphdr icmp_hdr;\n struct sockaddr_in addr;\n int sequence = 0;\n int sock = socket(AF_INET,SOCK_DGRAM,IPPROTO_ICMP);\n if (sock < 0) {\n perror(\"socket\");\n return -1;\n }\n\n memset(&addr, 0, sizeof addr);\n addr.sin_family = AF_INET;\n addr.sin_addr = *dst;\n\n memset(&icmp_hdr, 0, sizeof icmp_hdr);\n icmp_hdr.type = ICMP_ECHO;\n icmp_hdr.un.echo.id = 1234;\n\n unsigned char data[2048];\n int rc;\n struct timeval timeout = {1, 0}; \n fd_set read_set;\n socklen_t slen;\n struct icmphdr rcv_hdr;\n\n icmp_hdr.un.echo.sequence = sequence++;\n memcpy(data, &icmp_hdr, sizeof icmp_hdr);\n memcpy(data + sizeof icmp_hdr, \"hello\", 5); \n rc = sendto(sock, data, sizeof icmp_hdr + 5,\n 0, (struct sockaddr*)&addr, sizeof addr);\n if (rc <= 0) {\n perror(\"Sendto\");\n }\n puts(\"Sent ICMP\\n\");\n\n memset(&read_set, 0, sizeof read_set);\n FD_SET(sock, &read_set);\n\n \n rc = select(sock + 1, &read_set, NULL, NULL, &timeout);\n if (rc == 0) {\n puts(\"Got no reply\\n\");\n \treturn 0;\n } else if (rc < 0) {\n perror(\"Select\");\n \treturn 0;\n }\n\n \n slen = 0;\n rc = recvfrom(sock, data, sizeof data, 0, NULL, &slen);\n if (rc <= 0) {\n perror(\"recvfrom\");\n } else if (rc < sizeof rcv_hdr) {\n \n }\n memcpy(&rcv_hdr, data, sizeof rcv_hdr);\n if (rcv_hdr.type == ICMP_ECHOREPLY) {\n \n \n } else {\n \n }\n return 1;\n}\n\n\n\nint logic_bomb(char* s) {\n struct in_addr dst;\n\n if (inet_aton(s, &dst) == 0) {\n perror(\"inet_aton\");\n \n return 0;\n }\n\n if (ping_it(&dst) == 1){\n return 1;\n }else{\n return 0;\n }\n}\n", "target": "#include \"assert.h\"\nint main(){\n\tassert(logic_bomb() == 1);\n}"} 33 | {"index": 32, "logic_bombs_task_id": "addint_to_l1", "focal_method_name": "logic_bomb", "focal_method_para": "(char* s)", "focal_method_return_type": "int", "focal_method": "\n\nint logic_bomb(char* s) {\n int symvar = s[0] - 48;\n if (symvar + 2147483640 < 0 && symvar > 0)\n return 1;\n else\n return 0;\n}\n", "target": "#include \"assert.h\"\nint main(){\n\tassert(logic_bomb() == 1);\n}"} 34 | {"index": 33, "logic_bombs_task_id": "multiplyint_to_l1", "focal_method_name": "logic_bomb", "focal_method_para": "(char* s)", "focal_method_return_type": "int", "focal_method": "\n\nint logic_bomb(char* s) {\n int symvar = s[0] - 48;\n if (254748364 * symvar < 0 && symvar > 0)\n return 1;\n else\n return 0;\n}\n", "target": "#include \"assert.h\"\nint main(){\n\tassert(logic_bomb() == 1);\n}"} 35 | {"index": 34, "logic_bombs_task_id": "7n+1_lo_l1", "focal_method_name": "logic_bomb", "focal_method_para": "(char* s)", "focal_method_return_type": "int", "focal_method": "#include \n\nlong f(long x){\n if (x%2 == 0)\n\treturn x/2;\n else if (x%3 == 0)\n\treturn x/3;\n else if (x%5 == 0)\n\treturn x/5;\n else\n return 7*x + 1;\n}\n\n\nint logic_bomb(char* s) {\n int symvar = s[0] - 48;\n symvar = symvar + 1104;\n long j = f(symvar);\n int loopcount = 1;\n while(j != 1){\n\tj = f(j);\n loopcount ++;\n }\n if(loopcount == 50)\n return 1;\n else\n return 0;\n}\n", "target": "#include \"assert.h\"\nint main(){\n\tassert(logic_bomb() == 1);\n}"} 36 | {"index": 35, "logic_bombs_task_id": "5n+1_lo_l1", "focal_method_name": "logic_bomb", "focal_method_para": "(char* s)", "focal_method_return_type": "int", "focal_method": "#include \n\nlong f(long x){\n if (x%2 == 0)\n\treturn x/2;\n else if (x%3 == 0)\n\treturn x/3;\n else\n return 3*x + 1;\n}\n\n\nint logic_bomb(char* s) {\n int symvar = s[0] - 48;\n symvar = symvar + 94;\n long j = f(symvar);\n int loopcount = 1;\n while(j != 1){\n\tj = f(j);\n loopcount ++;\n }\n if(loopcount == 25)\n return 1;\n else\n return 0;\n}\n", "target": "#include \"assert.h\"\nint main(){\n\tassert(logic_bomb() == 1);\n}"} 37 | {"index": 36, "logic_bombs_task_id": "collaz_lo_l2", "focal_method_name": "logic_bomb", "focal_method_para": "(char* s)", "focal_method_return_type": "int", "focal_method": "#include \n\nlong f(long x){\n if (x%2 == 0)\n\treturn x/2;\n return 3*x + 1;\n}\n\n\nint logic_bomb(char* s) {\n int symvar = s[0] - 48;\n symvar = symvar + 670617272;\n if(symvar>999999999)\n\treturn 0;\n long j = f(symvar);\n int loopcount = 1;\n while(j != 1){\n\tj = f(j);\n loopcount ++;\n }\n if(loopcount == 986)\n return 1;\n else\n return 0;\n}\n", "target": "#include \"assert.h\"\nint main(){\n\tassert(logic_bomb() == 1);\n}"} 38 | {"index": 37, "logic_bombs_task_id": "paraloop_lo_l2", "focal_method_name": "logic_bomb", "focal_method_para": "(char* s)", "focal_method_return_type": "int", "focal_method": "#include \n#include \n#include \n\nvoid* trigger(void* i){\n sleep(5);\n ++ *((int*) i);\n}\n\n\nint logic_bomb(char* s) {\n int symvar = s[0] - 48;\n int i = symvar + 1;\n pthread_t tid;\n int rc = pthread_create(&tid, NULL, trigger, (void *) &symvar); \n while (symvar != i){\n\tsleep(1);\n symvar ++;\n i ++;\n }\n rc = pthread_join(tid, NULL); \n if(symvar == 13)\n return 1;\n return 0;\n}\n", "target": "#include \"assert.h\"\nint main(){\n\tassert(logic_bomb() == 1);\n}"} 39 | {"index": 38, "logic_bombs_task_id": "collaz_lo_l1", "focal_method_name": "logic_bomb", "focal_method_para": "(char* s)", "focal_method_return_type": "int", "focal_method": "#include \n\nint f(int x){\n if (x%2 == 0)\n\treturn x/2;\n return 3*x + 1;\n}\n\n\nint logic_bomb(char* s) {\n int symvar = s[0] - 48;\n symvar = symvar + 94;\n int j = f(symvar);\n int loopcount = 1;\n while(j != 1){\n\tj = f(j);\n loopcount ++;\n }\n if(loopcount == 25)\n return 1;\n else\n return 0;\n}\n", "target": "#include \"assert.h\"\nint main(){\n\tassert(logic_bomb() == 1);\n}"} 40 | {"index": 39, "logic_bombs_task_id": "stack_bo_l1", "focal_method_name": "logic_bomb", "focal_method_para": "(char* s)", "focal_method_return_type": "int", "focal_method": "#include \n\n\n\nint logic_bomb(char* symvar) {\n int flag = 0;\n char buf[8];\n strcpy(buf, symvar);\n if(flag == 1){\n return 1;\n }\n return 0;\n}\n", "target": "#include \"assert.h\"\nint main(){\n\tassert(logic_bomb() == 1);\n}"} 41 | {"index": 40, "logic_bombs_task_id": "heap_bo_l1", "focal_method_name": "logic_bomb", "focal_method_para": "(char* s)", "focal_method_return_type": "int", "focal_method": "#include \n\nint n = 5;\n\n\nint logic_bomb(char* symvar) {\n char *p, *q;\n p = (char*)malloc(16);\n q = (char*)malloc(16);\n strcpy(p, symvar);\n free(q);\n if (n != 5){\n free(p);\n return 1;\n }else {\n free(p);\n return 0;\n }\n}\n", "target": "#include \"assert.h\"\nint main(){\n\tassert(logic_bomb() == 1);\n}"} 42 | {"index": 41, "logic_bombs_task_id": "stack_bo_l2", "focal_method_name": "logic_bomb", "focal_method_para": "(char* s)", "focal_method_return_type": "int", "focal_method": "#include \n\nint trigger(){\n return 1;\n}\n\n\nint logic_bomb(char* symvar) {\n char buf[8];\n strcpy(buf, symvar);\n if(buf < 0)\n return trigger();\n return 0;\n}\n", "target": "#include \"assert.h\"\nint main(){\n\tassert(logic_bomb() == 1);\n}"} 43 | {"index": 42, "logic_bombs_task_id": "stacknocrash_bo_l1", "focal_method_name": "logic_bomb", "focal_method_para": "(char* s)", "focal_method_return_type": "int", "focal_method": "#include \n\n\n\nint logic_bomb(char* symvar) {\n int flag = 0;\n char buf[8];\n if(strlen(symvar) > 9)\n return 0;\n strcpy(buf, symvar);\n if(flag == 1){\n return 1;\n }\n return 0;\n}\n", "target": "#include \"assert.h\"\nint main(){\n\tassert(logic_bomb() == 1);\n}"} 44 | {"index": 43, "logic_bombs_task_id": "2thread_pp_l2", "focal_method_name": "logic_bomb", "focal_method_para": "(char* s)", "focal_method_return_type": "int", "focal_method": "#include \n#include \n#include \n\nvoid* Inc(void* i){\n int count = 0;\n while (*((int *) i) > -1000 && count++ < 1000){\n\t++ *((int *) i);\n \n }\n}\n\nvoid* Dec(void* i){\n int count = 0;\n while (*((int *) i) < 1000 && count++ < 1000){\n\t-- *((int *) i);\n \n }\n}\n\nint ThreadProp(int in){\n pthread_t tid[2];\n pthread_create(&tid[0], NULL, Inc, (void *) &in); \n pthread_create(&tid[1], NULL, Dec, (void *) &in); \n pthread_join(tid[0], NULL); \n pthread_join(tid[1], NULL); \n return in;\n}\n\n\nint logic_bomb(char* s) {\n int symvar = s[0] - 48;\n int i=ThreadProp(symvar-909);\n \n if(i == -1900)\n return 1;\n return 0;\n}\n", "target": "#include \"assert.h\"\nint main(){\n\tassert(logic_bomb() == 1);\n}"} 45 | {"index": 44, "logic_bombs_task_id": "forkshm_pp_l1", "focal_method_name": "logic_bomb", "focal_method_para": "(char* s)", "focal_method_return_type": "int", "focal_method": "#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\n\nint logic_bomb(char* symvar) {\n char *p_addr, *c_addr;\n int flag = 0;\n key_t shmid = shmget(IPC_PRIVATE, 1024, S_IRUSR|S_IWUSR);\n if (shmid < 0)\n\treturn 0;\n pid_t pid = fork();\n if(pid == 0){\n\tp_addr = shmat(shmid,0,0);\n memset(p_addr,'/0',1024);\n strncpy(p_addr, symvar, 1024);\n\texit(0);\n }\n if (pid > 0){\n\tsleep(1);\n c_addr = shmat(shmid,0,0);\n if(strcmp(c_addr, \"7\") == 0)\n\t flag = 1;\n shmctl(shmid,IPC_RMID,0);\n if(flag == 1)\n\t return 1;\n\treturn 0;\n }\n}\n", "target": "#include \"assert.h\"\nint main(){\n\tassert(logic_bomb() == 1);\n}"} 46 | {"index": 45, "logic_bombs_task_id": "2thread_pp_l1", "focal_method_name": "logic_bomb", "focal_method_para": "(char* s)", "focal_method_return_type": "int", "focal_method": "#include \n#include \n#include \n\nvoid* Inc(void* i){\n\t++ *((int*) i);\n}\n\nvoid* Mult(void* i){\n\t*((int*) i) = *((int*) i) * *((int*) i);\n}\n\nint ThreadProp(int in){\n\tpthread_t tid[2];\n\tint rc1 = pthread_create(&tid[0], NULL, Inc, (void *) &in); \n\tint rc2 = pthread_create(&tid[1], NULL, Mult, (void *) &in); \n\trc1 = pthread_join(tid[0], NULL); \n\trc2 = pthread_join(tid[1], NULL); \n\tint out = in;\n\treturn out;\n}\n\n\nint logic_bomb(char* s) {\n int symvar = s[0] - 48;\n int i=ThreadProp(symvar);\n if(i == 50)\n return 1;\n return 0;\n}\n", "target": "#include \"assert.h\"\nint main(){\n\tassert(logic_bomb() == 1);\n}"} 47 | {"index": 46, "logic_bombs_task_id": "mthread_pp_l2", "focal_method_name": "logic_bomb", "focal_method_para": "(char* s)", "focal_method_return_type": "int", "focal_method": "#include \n#include \n#include \n\nvoid* Inc(void* i){\n int count = 0;\n while (*((int *) i) > -1000 && count++ < 1000){\n\t++ *((int*) i);\n }\n}\n\nvoid* Dec(void* i){\n int count = 0;\n while (*((int *) i) < 1000 && count++ < 1000){\n\t-- *((int*) i);\n }\n}\n\nint ThreadProp(int in){\n pthread_t tid[10];\n pthread_create(&tid[0], NULL, Inc, (void *) &in); \n pthread_create(&tid[1], NULL, Dec, (void *) &in); \n pthread_create(&tid[2], NULL, Inc, (void *) &in); \n pthread_create(&tid[3], NULL, Dec, (void *) &in); \n pthread_create(&tid[4], NULL, Inc, (void *) &in); \n pthread_create(&tid[5], NULL, Dec, (void *) &in); \n pthread_create(&tid[6], NULL, Inc, (void *) &in); \n pthread_create(&tid[7], NULL, Dec, (void *) &in); \n pthread_create(&tid[8], NULL, Inc, (void *) &in); \n pthread_create(&tid[9], NULL, Dec, (void *) &in); \n pthread_join(tid[0], NULL); \n pthread_join(tid[1], NULL); \n pthread_join(tid[2], NULL); \n pthread_join(tid[3], NULL); \n pthread_join(tid[4], NULL); \n pthread_join(tid[5], NULL); \n pthread_join(tid[6], NULL); \n pthread_join(tid[7], NULL); \n pthread_join(tid[8], NULL); \n pthread_join(tid[9], NULL); \n return in;\n}\n\n\nint logic_bomb(char* s) {\n int symvar = s[0] - 48;\n int i=ThreadProp(symvar+990);\n \n if(i == 5999)\n return 1;\n return 0;\n}\n", "target": "#include \"assert.h\"\nint main(){\n\tassert(logic_bomb() == 1);\n}"} 48 | {"index": 47, "logic_bombs_task_id": "forkpipe_pp_l1", "focal_method_name": "logic_bomb", "focal_method_para": "(char* s)", "focal_method_return_type": "int", "focal_method": "#include \n\n\nint logic_bomb(char* s) {\n int pid, fd[2];\n pipe(fd);\n if ((pid = fork()) == -1)\n return 0;\n if (pid == 0) {\n close(fd[0]);\n write(fd[1], s, sizeof(s));\n wait(NULL);\n\texit(0);\n }\n else {\n char content[8];\n close(fd[1]);\n read(fd[0], content, 8);\n if (strcmp(content, \"7\") == 0) {\n return 1;\n }\n return 0; \n }\n}\n", "target": "#include \"assert.h\"\nint main(){\n\tassert(logic_bomb() == 1);\n}"} 49 | {"index": 48, "logic_bombs_task_id": "float4_fp_l2", "focal_method_name": "logic_bomb", "focal_method_para": "(char* s)", "focal_method_return_type": "int", "focal_method": "#include\n#include \n\n\nint logic_bomb(char* symvar) {\n float x = atof(symvar);\n x = x/-10000.0;\n if(1024+x == 1024 && x>0)\n return 1;\n else\n return 0;\n}\n", "target": "#include \"assert.h\"\nint main(){\n\tassert(logic_bomb() == 1);\n}"} 50 | {"index": 49, "logic_bombs_task_id": "float3_fp_l2", "focal_method_name": "logic_bomb", "focal_method_para": "(char* s)", "focal_method_return_type": "int", "focal_method": "#include\n#include \n\n\nint logic_bomb(char* symvar) {\n float x = atof(symvar);\n x = x/10000.0;\n if(1024+x == 1024 && x>0)\n return 1;\n else\n return 0;\n}\n", "target": "#include \"assert.h\"\nint main(){\n\tassert(logic_bomb() == 1);\n}"} 51 | {"index": 50, "logic_bombs_task_id": "float1_fp_l1", "focal_method_name": "logic_bomb", "focal_method_para": "(char* s)", "focal_method_return_type": "int", "focal_method": "#include\n#include \n\n\nint logic_bomb(char* s) {\n int symvar = s[0] - 48;\n float a = symvar/70.0;\n float b = 0.1;\n if(a != 0.1){\n\tif(a - b == 0)\n return 1;\n }\n return 0;\n}\n", "target": "#include \"assert.h\"\nint main(){\n\tassert(logic_bomb() == 1);\n}"} 52 | {"index": 51, "logic_bombs_task_id": "float2_fp_l1", "focal_method_name": "logic_bomb", "focal_method_para": "(char* s)", "focal_method_return_type": "int", "focal_method": "#include\n#include \n\n\nint logic_bomb(char* s) {\n int symvar = s[0] - 48;\n float x = symvar + 0.0000005;\n if(x != 7){\n float x = symvar + 1;\n if (x == 8)\n return 1;\n }\n return 0;\n}\n", "target": "#include \"assert.h\"\nint main(){\n\tassert(logic_bomb() == 1);\n}"} 53 | {"index": 52, "logic_bombs_task_id": "float5_fp_l2", "focal_method_name": "logic_bomb", "focal_method_para": "(char* s)", "focal_method_return_type": "int", "focal_method": "#include\n#include \n\n\nint logic_bomb(char* symvar) {\n float x = atof(symvar);\n x = x/10.0;\n x = x + 0.1;\n x = x * x;\n if (x > 0.1)\n\tx -= x;\n if(x != 0.02){\n x = x + 7.98;\n if(x == 8)\n return 1;\n }\n return 0;\n}\n", "target": "#include \"assert.h\"\nint main(){\n\tassert(logic_bomb() == 1);\n}"} 54 | -------------------------------------------------------------------------------- /data/rw/coreutils/build.sh: -------------------------------------------------------------------------------- 1 | git clone https://github.com/coreutils/coreutils.git 2 | cd coreutils 3 | git reset --hard 84f820228708fbee53e2267e6d2ad805197df8be 4 | make -f cfg.mk 5 | cd .. -------------------------------------------------------------------------------- /data/rw/coreutils/check.sh: -------------------------------------------------------------------------------- 1 | cd coreutils 2 | cp ../p.c src/ 3 | gcc -I. -I./lib -Ilib -I./lib -Isrc -I./src -Werror -Wall -Warith-conversion -Wbad-function-cast -Wcast-align=strict -Wdate-time -Wdisabled-optimization -Wdouble-promotion -Wduplicated-branches -Wduplicated-cond -Wextra -Wformat-signedness -Winit-self -Winvalid-pch -Wlogical-op -Wmissing-declarations -Wmissing-include-dirs -Wmissing-prototypes -Wnull-dereference -Wold-style-definition -Wopenmp-simd -Woverlength-strings -Wpacked -Wpointer-arith -Wshadow -Wstrict-overflow -Wstrict-prototypes -Wsuggest-attribute=cold -Wsuggest-attribute=const -Wsuggest-attribute=format -Wsuggest-attribute=malloc -Wsuggest-attribute=noreturn -Wsuggest-attribute=pure -Wsuggest-final-methods -Wsuggest-final-types -Wsync-nand -Wtrampolines -Wuninitialized -Wunknown-pragmas -Wno-unused-macros -Wvariadic-macros -Wvla -Wwrite-strings -Warray-bounds=2 -Wattribute-alias=2 -Wformat=2 -Wimplicit-fallthrough=5 -Wshift-overflow=2 -Wno-unused-const-variable -Wno-unused-function -Wvla-larger-than=4031 -Wno-sign-compare -Wno-unused-parameter -Wno-format-nonliteral -fdiagnostics-show-option -funit-at-a-time -Wno-return-local-addr -Wno-stringop-overflow -Wno-unused-macros -g -O2 -Wl,--as-needed -o src/p src/p.c src/libver.a lib/libcoreutils.a lib/libcoreutils.a -ldl 4 | 5 | if [ $? -ne 0 ]; then 6 | echo "compilation error" 7 | exit 2 8 | fi 9 | 10 | ./src/p 11 | if [ $? -ne 0 ]; then 12 | echo "assertion error" 13 | exit 1 14 | fi 15 | cd .. 16 | exit 0 17 | -------------------------------------------------------------------------------- /data/rw/coreutils/info.json: -------------------------------------------------------------------------------- 1 | {"index": 0, "project": "data/rw/coreutils", "focal_method_name": "remove_suffix", "focal_method_para": "(char *name, char const *suffix)", "focal_method_return_type": "void", "focal_method": "static void\nremove_suffix (char *name, char const *suffix)\n{\n char *np;\n char const *sp;\n\n np = name + strlen (name);\n sp = suffix + strlen (suffix);\n\n while (np > name && sp > suffix)\n if (*--np != *--sp)\n return;\n if (np > name)\n *np = '\u0000';\n}", "target": "int\nmain (int argc, char **argv)\n{\n char const *suffix = \".def.x\";\n char name[256];\n strncpy(name, , 256);\n remove_suffix(name, suffix);\n if(strncmp(name, \"abc\", 256)==0){\n return EXIT_SUCCESS;\n }\n return EXIT_FAILURE;\n}"} -------------------------------------------------------------------------------- /data/rw/coreutils/p.c: -------------------------------------------------------------------------------- 1 | /* basename -- strip directory and suffix from file names 2 | Copyright (C) 1990-2024 Free Software Foundation, Inc. 3 | 4 | This program is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program. If not, see . */ 16 | 17 | #include 18 | #include 19 | #include 20 | #include 21 | 22 | #include "system.h" 23 | #include "quote.h" 24 | 25 | /* The official name of this program (e.g., no 'g' prefix). */ 26 | #define PROGRAM_NAME "basename" 27 | 28 | #define AUTHORS proper_name ("David MacKenzie") 29 | 30 | static struct option const longopts[] = 31 | { 32 | {"multiple", no_argument, nullptr, 'a'}, 33 | {"suffix", required_argument, nullptr, 's'}, 34 | {"zero", no_argument, nullptr, 'z'}, 35 | {GETOPT_HELP_OPTION_DECL}, 36 | {GETOPT_VERSION_OPTION_DECL}, 37 | {nullptr, 0, nullptr, 0} 38 | }; 39 | 40 | void 41 | usage (int status) 42 | { 43 | if (status != EXIT_SUCCESS) 44 | emit_try_help (); 45 | else 46 | { 47 | printf (_("\ 48 | Usage: %s NAME [SUFFIX]\n\ 49 | or: %s OPTION... NAME...\n\ 50 | "), 51 | program_name, program_name); 52 | fputs (_("\ 53 | Print NAME with any leading directory components removed.\n\ 54 | If specified, also remove a trailing SUFFIX.\n\ 55 | "), stdout); 56 | 57 | emit_mandatory_arg_note (); 58 | 59 | fputs (_("\ 60 | -a, --multiple support multiple arguments and treat each as a NAME\n\ 61 | -s, --suffix=SUFFIX remove a trailing SUFFIX; implies -a\n\ 62 | -z, --zero end each output line with NUL, not newline\n\ 63 | "), stdout); 64 | fputs (HELP_OPTION_DESCRIPTION, stdout); 65 | fputs (VERSION_OPTION_DESCRIPTION, stdout); 66 | printf (_("\ 67 | \n\ 68 | Examples:\n\ 69 | %s /usr/bin/sort -> \"sort\"\n\ 70 | %s include/stdio.h .h -> \"stdio\"\n\ 71 | %s -s .h include/stdio.h -> \"stdio\"\n\ 72 | %s -a any/str1 any/str2 -> \"str1\" followed by \"str2\"\n\ 73 | "), 74 | program_name, program_name, program_name, program_name); 75 | emit_ancillary_info (PROGRAM_NAME); 76 | } 77 | exit (status); 78 | } 79 | 80 | /* Remove SUFFIX from the end of NAME if it is there, unless NAME 81 | consists entirely of SUFFIX. */ 82 | 83 | static void 84 | remove_suffix (char *name, char const *suffix) 85 | { 86 | char *np; 87 | char const *sp; 88 | 89 | np = name + strlen (name); 90 | sp = suffix + strlen (suffix); 91 | 92 | while (np > name && sp > suffix) 93 | if (*--np != *--sp) 94 | return; 95 | if (np > name) 96 | *np = '\0'; 97 | } 98 | 99 | /* Perform the basename operation on STRING. If SUFFIX is non-null, remove 100 | the trailing SUFFIX. Finally, output the result string. */ 101 | 102 | static void 103 | perform_basename (char const *string, char const *suffix, bool use_nuls) 104 | { 105 | char *name = base_name (string); 106 | strip_trailing_slashes (name); 107 | 108 | /* Per POSIX, 'basename // /' must return '//' on platforms with 109 | distinct //. On platforms with drive letters, this generalizes 110 | to making 'basename c: :' return 'c:'. This rule is captured by 111 | skipping suffix stripping if base_name returned an absolute path 112 | or a drive letter (only possible if name is a file-system 113 | root). */ 114 | if (suffix && IS_RELATIVE_FILE_NAME (name) && ! FILE_SYSTEM_PREFIX_LEN (name)) 115 | remove_suffix (name, suffix); 116 | 117 | fputs (name, stdout); 118 | putchar (use_nuls ? '\0' : '\n'); 119 | free (name); 120 | } 121 | 122 | int 123 | main (int argc, char **argv) 124 | { 125 | char const *suffix = ".def.x"; 126 | char name[256]; 127 | strncpy(name, , 256); 128 | remove_suffix(name, suffix); 129 | if(strncmp(name, "abc", 256)==0){ 130 | return EXIT_SUCCESS; 131 | } 132 | return EXIT_FAILURE; 133 | } 134 | -------------------------------------------------------------------------------- /example.py: -------------------------------------------------------------------------------- 1 | from patheval import set_dataset, read_problems, evaluate_one 2 | from pprint import pprint 3 | 4 | def ask(prompt): 5 | # hook, this completion is from gpt-3.5-turbo 6 | return "vector{1.5, 2.5, 3.5, 4.5, 5.5}, 0.5" 7 | 8 | def your_model_or_approach_here(d): 9 | # query with custom prompt 10 | prompt = f"""Please fill the arguments tagged with to make the targeted assertion success.\n```{d['focal_method'] + d['target']}```""" 11 | completion = ask(prompt) 12 | # any post-processing if needed, for example: 13 | completion = completion.strip() 14 | return completion 15 | 16 | 17 | set_dataset("patheval_cpp") # or "patheval_java" or "patheval_py" or "logic_bombs_c" 18 | problems = read_problems() 19 | problem = problems[0] 20 | print(problem['focal_method'] + problem['target']) 21 | print(evaluate_one(problem, your_model_or_approach_here(problem))['pass']) 22 | 23 | -------------------------------------------------------------------------------- /patheval.py: -------------------------------------------------------------------------------- 1 | from utils import load_data, dump_data 2 | from pprint import pprint 3 | import tqdm 4 | import argparse 5 | 6 | dataset = None 7 | 8 | datasets = { 9 | "patheval_cpp":"data/patheval_cpp.jsonl", 10 | "patheval_py":"data/patheval_py.jsonl", 11 | "patheval_java":"data/patheval_java.jsonl", 12 | "logic_bombs_c":"data/logic_bombs_c.jsonl", 13 | } 14 | 15 | def set_dataset(ds): 16 | global dataset 17 | global datasets 18 | if ds not in datasets.keys(): 19 | raise Exception("no dataset.") 20 | dataset = ds 21 | 22 | def read_problems() -> list: 23 | if dataset != None: 24 | return load_data(datasets[dataset]) 25 | raise Exception("set_dataset should be called first.") 26 | 27 | def debug_one(d:dict, completion:str): 28 | if dataset == "patheval_cpp": 29 | import scripts.cpp_check as c 30 | pprint(c.check_one(d, completion)) 31 | elif dataset == "patheval_java": 32 | import scripts.java_check as c 33 | pprint(c.check_one(d, completion)) 34 | elif dataset == "patheval_py": 35 | import scripts.py_check as c 36 | pprint(c.check_one(d, completion)) 37 | elif dataset == "logic_bombs_c": 38 | import scripts.c_check as c 39 | pprint(c.check_one(d, completion)) 40 | 41 | def evaluate_one(d:dict, completion:str) -> True: 42 | if dataset == "patheval_cpp": 43 | import scripts.cpp_check as c 44 | rv, _, _ = c.check_one(d, completion) 45 | elif dataset == "patheval_java": 46 | import scripts.java_check as c 47 | rv, _, _ = c.check_one(d, completion) 48 | elif dataset == "patheval_py": 49 | import scripts.py_check as c 50 | rv, _, _ = c.check_one(d, completion) 51 | elif dataset == "logic_bombs_c": 52 | import scripts.c_check as c 53 | rv, _, _ = c.check_one(d, completion) 54 | d['pass'] = (rv == 0) 55 | d['completion'] = completion 56 | return d 57 | 58 | def evaluate_all(problems, completions): 59 | for idx, problem in enumerate(problems): 60 | problems[idx]['pass'] = False 61 | for completion in tqdm.tqdm(completions): 62 | idx = completion['index'] 63 | comp = completion['completion'] 64 | d = evaluate_one(problems[idx], comp) 65 | if d['pass']: 66 | problems[idx]['pass'] = True 67 | pass_samples = list(filter(lambda x : x['pass'], problems)) 68 | pass_rate = len(pass_samples) / len(problems) 69 | print(f"Pass Rate : {pass_rate} ({len(pass_samples)} / {len(problems)})") 70 | 71 | 72 | def test(): 73 | set_dataset("patheval_py") 74 | data = read_problems() 75 | d = data[0] 76 | assert evaluate_one(d, "[1.1, 2.2, 3.3], 2")['pass'] 77 | 78 | set_dataset("patheval_java") 79 | data = read_problems() 80 | d = data[0] 81 | assert evaluate_one(d, "Arrays.asList(1.1, 2.2, 3.3), 2.0")['pass'] 82 | 83 | set_dataset("patheval_cpp") 84 | data = read_problems() 85 | d = data[0] 86 | assert not evaluate_one(d, "vector({1.1,2.2,3.3}), 2.0")['pass'] 87 | assert evaluate_one(d, "vector{1.5, 2.5, 3.5, 4.5, 5.5}, 0.5")['pass'] 88 | 89 | catch = False 90 | try: 91 | set_dataset("123") 92 | except: 93 | catch = True 94 | assert catch 95 | 96 | 97 | def main(): 98 | parser = argparse.ArgumentParser(description='Process some command line arguments.') 99 | # Add arguments 100 | parser.add_argument('--dataset', type=str, required=True, 101 | help='the selected dataset') 102 | parser.add_argument('-i', '--input', type=str, required=True, 103 | help='the json file of index and completion from LLMs for each problem.') 104 | # Parse arguments 105 | args = parser.parse_args() 106 | 107 | set_dataset(args.dataset) 108 | problems = read_problems() 109 | try: 110 | completions = load_data(args.input) 111 | except Exception as e: 112 | print(e) 113 | exit(-1) 114 | evaluate_all(problems, completions) 115 | 116 | if __name__ == "__main__": 117 | # test() 118 | main() -------------------------------------------------------------------------------- /sample.jsonl: -------------------------------------------------------------------------------- 1 | {"index": 572, "completion": "Arrays.asList(1.0, 2., 3.)"} 2 | {"index": 486, "completion": "\"Mary had a little lamb\", 4"} 3 | {"index": 532, "completion": "\"Hello,Hello,world !\" "} 4 | {"index": 599, "completion": "\"1\", 1"} 5 | {"index": 514, "completion": "Arrays.asList(30, 13, 24, 321)"} 6 | {"index": 378, "completion": "0, 1"} 7 | {"index": 547, "completion": "Arrays.asList(Arrays.asList(8, 14, 9, 2), Arrays.asList(6, 4, 13, 15), Arrays.asList(5, 7, 1, 12), Arrays.asList(3, 10, 11, 16)), 5"} 8 | {"index": 450, "completion": "\"r t g\" "} 9 | {"index": 305, "completion": "\"MadaM\""} 10 | {"index": 711, "completion": "new ArrayList<>(Arrays.asList(\"aaaaaaa\", \"bb\", \"cc\"))"} 11 | {"index": 438, "completion": "Arrays.asList(1, 100, 98, -7, 1, -1)"} 12 | {"index": 447, "completion": "\"r t g\" "} 13 | {"index": 456, "completion": "\"abcdedcba\", \"ab\" "} 14 | {"index": 513, "completion": "Arrays.asList(5, 9)"} 15 | {"index": 669, "completion": "List.of()"} 16 | {"index": 605, "completion": "7"} 17 | {"index": 578, "completion": "List.of(0.)"} 18 | {"index": 465, "completion": "List.of(7)"} 19 | {"index": 522, "completion": "12"} 20 | {"index": 40, "completion": "new ArrayList<>(Arrays.asList(\"x\", \"y\", \"z\"))"} 21 | {"index": 546, "completion": "Arrays.asList(Arrays.asList(1, 2, 3, 4), Arrays.asList(5, 6, 7, 8), Arrays.asList(9, 10, 11, 12), Arrays.asList(13, 14, 15, 16)), 4"} 22 | {"index": 446, "completion": "\"a b c a b\" "} 23 | {"index": 588, "completion": "Arrays.asList(4, 5, 3, 6, 2, 7, -7)"} 24 | {"index": 611, "completion": "\" Exa 1 2 2 mple\" "} 25 | {"index": 335, "completion": "new ArrayList<>(Arrays.asList(15, 42, 87, 32 ,11, 0))"} 26 | {"index": 548, "completion": "Arrays.asList(Arrays.asList(2, 7, 4), Arrays.asList(3, 1, 5), Arrays.asList(6, 8, 9)), 8"} 27 | {"index": 101, "completion": "new ArrayList<>(Arrays.asList(\"x\", \"y\", \"z\", \"w\", \"k\"))"} 28 | {"index": 269, "completion": "new ArrayList<>(List.of(3)), 5"} 29 | {"index": 592, "completion": "Arrays.asList(1, 3, 2, 4, 5, 6, -2)"} 30 | {"index": 588, "completion": "Arrays.asList(-6, -4, -4, -3, -100, 1)"} 31 | {"index": 222, "completion": "\"abcde\""} 32 | {"index": 387, "completion": "\"-15.5\""} 33 | {"index": 691, "completion": "0"} 34 | {"index": 89, "completion": "3 * 19 * 19 * 19"} 35 | {"index": 384, "completion": "\"abcdefg\""} 36 | {"index": 581, "completion": "List.of(-1.)"} 37 | {"index": 100, "completion": "new ArrayList<>(List.of())"} 38 | {"index": 697, "completion": "426"} 39 | {"index": 207, "completion": "\"(()\""} 40 | {"index": 462, "completion": "Arrays.asList(\"3\", \"11111111\" )"} 41 | {"index": 470, "completion": "Arrays.asList(100, -1, -2, -3, 10, -5)"} 42 | {"index": 540, "completion": "Arrays.asList(2, 4,1, 2, -1, -1, 9)"} 43 | {"index": 598, "completion": "\"5,1\", \"6\""} 44 | {"index": 725, "completion": "1, 10, 10"} 45 | {"index": 570, "completion": "\"[[[[]]]]\" "} 46 | {"index": 422, "completion": "new ArrayList<>(Arrays.asList(9, 4, 8))"} 47 | {"index": 505, "completion": "new ArrayList<>(Arrays.asList(4, -4)), 2"} 48 | {"index": 503, "completion": "new ArrayList<>(Arrays.asList(4, -4)), 2"} 49 | {"index": 315, "completion": "147"} 50 | {"index": 508, "completion": "new ArrayList<>(Arrays.asList(1, 0, 5, -7)), 1"} 51 | {"index": 238, "completion": "\"5 apples and 6 oranges\",19"} 52 | {"index": 700, "completion": "532"} 53 | {"index": 724, "completion": "4, 5, 7"} 54 | {"index": 159, "completion": "12"} 55 | {"index": 693, "completion": "-45347"} 56 | {"index": 561, "completion": "6"} 57 | {"index": 445, "completion": "Arrays.asList(100, 200), Arrays.asList(200, 200)"} 58 | {"index": 492, "completion": "\"eAsy\""} 59 | {"index": 276, "completion": "new ArrayList<>(Arrays.asList(\"hi\", \"admin\")), new ArrayList<>(Arrays.asList(\"hi\", \"hi\"))"} 60 | {"index": 696, "completion": "43"} 61 | {"index": 249, "completion": "new ArrayList<>(Arrays.asList(2, 7, 8, 8, 4, 8, 7, 3, 9, 6, 5, 10, 4, 3, 6, 7, 1, 7, 4, 10, 8, 1))"} 62 | {"index": 133, "completion": "7"} 63 | {"index": 414, "completion": "197, 233"} 64 | {"index": 457, "completion": "\"mamma\", \"mia\" "} 65 | {"index": 631, "completion": "\"1/6\", \"2/1\""} 66 | {"index": 374, "completion": "18"} 67 | {"index": 427, "completion": "7"} 68 | {"index": 502, "completion": "new ArrayList<>(Arrays.asList(-123, 20, 0 , 1, 2, -3)), 4"} 69 | {"index": 699, "completion": "994"} 70 | {"index": 582, "completion": "\"eeeee\" "} 71 | {"index": 265, "completion": "2, 2, 10"} 72 | {"index": 597, "completion": "\"1\", 1"} 73 | {"index": 573, "completion": "Arrays.asList(1., 3., 5., 7.)"} 74 | {"index": 621, "completion": "Arrays.asList(-56,-99,1,0,-2)"} 75 | {"index": 590, "completion": "Arrays.asList(-1, -3, -5, -6, 0)"} 76 | {"index": 302, "completion": "new ArrayList<>(List.of(1.2))"} 77 | {"index": 650, "completion": "\"Earth\", \"Earth\""} 78 | {"index": 638, "completion": "new ArrayList<>(Arrays.asList(1234, 423, 463, 145, 2, 423, 423, 53, 6, 37, 3457, 3, 56, 0, 46))"} 79 | {"index": 132, "completion": "7"} 80 | {"index": 562, "completion": "5"} 81 | {"index": 249, "completion": "new ArrayList<>(Arrays.asList(8, 8, 8, 8, 8, 8, 8, 8))"} 82 | {"index": 590, "completion": "List.of(0)"} 83 | {"index": 686, "completion": "\"efef\", \"fee\""} 84 | {"index": 88, "completion": "3 * 19 * 3 * 19"} 85 | {"index": 411, "completion": "5, 5"} 86 | {"index": 503, "completion": "new ArrayList<>(Arrays.asList(-1, 0, 2, 5, 3, -10)), 2"} 87 | {"index": 558, "completion": "4"} 88 | {"index": 439, "completion": "Arrays.asList(12, 23, 34, -45, -56, 0)"} 89 | {"index": 272, "completion": "new ArrayList<>(Arrays.asList(1, 2, 3, 5, 4, 7, 9, 6))"} 90 | {"index": 201, "completion": "27"} 91 | {"index": 374, "completion": "10"} 92 | {"index": 723, "completion": "4, 8, 9"} 93 | {"index": 70, "completion": "new ArrayList<>(Arrays.asList(1.0, 2.0, 3.0, 4.0, 5.0, 2.0))"} 94 | {"index": 291, "completion": "\"112233445566778899AABBCCDDEEFF00\""} 95 | {"index": 201, "completion": "15"} 96 | {"index": 733, "completion": "\"#a@C\""} 97 | {"index": 411, "completion": "7, 13"} 98 | {"index": 126, "completion": "1"} 99 | {"index": 504, "completion": "new ArrayList<>(Arrays.asList(1, 0, 5, -7)), 1"} 100 | {"index": 384, "completion": "\"EEEE\""} 101 | -------------------------------------------------------------------------------- /scripts/c_check.py: -------------------------------------------------------------------------------- 1 | import subprocess 2 | import tempfile 3 | import os 4 | import shutil 5 | 6 | includes = """ 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | """ 23 | 24 | def compile_and_run(code): 25 | original_dir = os.getcwd() 26 | temp_dir = tempfile.TemporaryDirectory() 27 | shutil.copytree("scripts/include", os.path.join(temp_dir.name, "include")) 28 | shutil.copytree("scripts/lib", os.path.join(temp_dir.name, "lib")) 29 | os.chdir(temp_dir.name) 30 | with open("./a.c", "w") as f: 31 | f.write(code) 32 | compile_command = ['gcc', "./a.c","lib/sha1.c", "lib/aes.c", "lib/crypto_utils.c", '-lm','-lcrypto', '-lpthread', '-o', './a.out', "-Iinclude"] 33 | compile_process = subprocess.run(compile_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE) 34 | 35 | if compile_process.returncode == 0: 36 | run_command = ['./a.out'] 37 | try: 38 | run_process = subprocess.run(run_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=10) 39 | output = run_process.stdout.decode('utf-8') 40 | error = run_process.stderr.decode('utf-8') 41 | rv = run_process.returncode 42 | except subprocess.TimeoutExpired: 43 | os.chdir(original_dir) 44 | shutil.rmtree(temp_dir.name) 45 | return -1 , "runtime error", "Timeout" 46 | else: 47 | error = compile_process.stderr.decode('utf-8') 48 | rv = -1 49 | output = "compile error" 50 | os.chdir(original_dir) 51 | shutil.rmtree(temp_dir.name) 52 | return rv, output, error 53 | 54 | def check_one(d, completion): 55 | code = d['focal_method'] 56 | target = d['target'] 57 | target = target.replace("", completion) 58 | rv, output, error = compile_and_run(includes + "\n" + code + "\n" + target) 59 | return rv, output, error 60 | 61 | 62 | def test(): 63 | import json 64 | with open("data/logic_bombs_c.jsonl") as f: 65 | data = [json.loads(s) for s in f.readlines()] 66 | for d in data: 67 | rv, output, error = check_one(d, "\"7\"") 68 | if "compile error" in output: 69 | print(d['focal_method']) 70 | print(error) 71 | raise Exception("Test fail, compilation error.") 72 | 73 | if __name__ == "__main__": 74 | test() 75 | -------------------------------------------------------------------------------- /scripts/cpp_check.py: -------------------------------------------------------------------------------- 1 | import subprocess 2 | import tempfile 3 | from utils import load_data, dump_data 4 | import os 5 | import shutil 6 | 7 | def compile_and_run(code): 8 | original_dir = os.getcwd() 9 | temp_dir = tempfile.TemporaryDirectory() 10 | os.chdir(temp_dir.name) 11 | with open("./a.cpp", "w") as f: 12 | f.write(code) 13 | 14 | compile_command = ['g++', "./a.cpp", '-o', './a.out', "-std=c++11", "-lcrypto", "-lssl"] 15 | compile_process = subprocess.run(compile_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE) 16 | 17 | if compile_process.returncode == 0: 18 | run_command = ['./a.out'] 19 | try: 20 | run_process = subprocess.run(run_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=10) 21 | output = run_process.stdout.decode('utf-8') 22 | error = run_process.stderr.decode('utf-8') 23 | rv = run_process.returncode 24 | except subprocess.TimeoutExpired: 25 | os.chdir(original_dir) 26 | shutil.rmtree(temp_dir.name) 27 | return -1 , "runtime error", "Timeout" 28 | else: 29 | error = compile_process.stderr.decode('utf-8') 30 | rv = -1 31 | output = "compile error" 32 | os.chdir(original_dir) 33 | shutil.rmtree(temp_dir.name) 34 | return rv, output, error 35 | 36 | def check_one(d, completion): 37 | code = d['focal_method'] 38 | target = d['target'] 39 | target = target.replace("", completion) 40 | rv, output, error = compile_and_run(code + "\n" + target) 41 | return rv, output, error 42 | -------------------------------------------------------------------------------- /scripts/include/a_tester.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #define BOMB_ENDING 3 4 | #define NORMAL_ENDING 0 5 | -------------------------------------------------------------------------------- /scripts/include/aes.h: -------------------------------------------------------------------------------- 1 | #ifndef _AES_H_ 2 | #define _AES_H_ 3 | 4 | #include 5 | 6 | 7 | // #define the macros below to 1/0 to enable/disable the mode of operation. 8 | // 9 | // CBC enables AES128 encryption in CBC-mode of operation and handles 0-padding. 10 | // ECB enables the basic ECB 16-byte block algorithm. Both can be enabled simultaneously. 11 | 12 | // The #ifndef-guard allows it to be configured before #include'ing or at compile time. 13 | #ifndef CBC 14 | #define CBC 1 15 | #endif 16 | 17 | #ifndef ECB 18 | #define ECB 1 19 | #endif 20 | 21 | 22 | 23 | #if defined(ECB) && ECB 24 | 25 | void AES128_ECB_encrypt(uint8_t* input, const uint8_t* key, uint8_t *output); 26 | void AES128_ECB_decrypt(uint8_t* input, const uint8_t* key, uint8_t *output); 27 | 28 | #endif // #if defined(ECB) && ECB 29 | 30 | 31 | #if defined(CBC) && CBC 32 | 33 | void AES128_CBC_encrypt_buffer(uint8_t* output, uint8_t* input, uint32_t length, const uint8_t* key, const uint8_t* iv); 34 | void AES128_CBC_decrypt_buffer(uint8_t* output, uint8_t* input, uint32_t length, const uint8_t* key, const uint8_t* iv); 35 | 36 | #endif // #if defined(CBC) && CBC 37 | 38 | 39 | 40 | #endif //_AES_H_ 41 | -------------------------------------------------------------------------------- /scripts/include/sha1.h: -------------------------------------------------------------------------------- 1 | /* 2 | * sha1.h 3 | * 4 | * Copyright (C) 1998, 2009 5 | * Paul E. Jones 6 | * All Rights Reserved 7 | * 8 | ***************************************************************************** 9 | * $Id: sha1.h 12 2009-06-22 19:34:25Z paulej $ 10 | ***************************************************************************** 11 | * 12 | * Description: 13 | * This class implements the Secure Hashing Standard as defined 14 | * in FIPS PUB 180-1 published April 17, 1995. 15 | * 16 | * Many of the variable names in the SHA1Context, especially the 17 | * single character names, were used because those were the names 18 | * used in the publication. 19 | * 20 | * Please read the file sha1.c for more information. 21 | * 22 | */ 23 | 24 | #ifndef _SHA1_H_ 25 | #define _SHA1_H_ 26 | 27 | #ifdef __cplusplus 28 | extern "C" { 29 | #endif 30 | /* 31 | * This structure will hold context information for the hashing 32 | * operation 33 | */ 34 | typedef struct SHA1Context 35 | { 36 | unsigned Message_Digest[5]; /* Message Digest (output) */ 37 | 38 | unsigned Length_Low; /* Message length in bits */ 39 | unsigned Length_High; /* Message length in bits */ 40 | 41 | unsigned char Message_Block[64]; /* 512-bit message blocks */ 42 | int Message_Block_Index; /* Index into message block array */ 43 | 44 | int Computed; /* Is the digest computed? */ 45 | int Corrupted; /* Is the message digest corruped? */ 46 | } SHA1Context; 47 | 48 | /* 49 | * Function Prototypes 50 | */ 51 | void SHA1Reset(SHA1Context *); 52 | int SHA1Result(SHA1Context *); 53 | void SHA1Input( SHA1Context *, 54 | const unsigned char *, 55 | unsigned); 56 | 57 | #ifdef __cplusplus 58 | } 59 | #endif 60 | #endif 61 | -------------------------------------------------------------------------------- /scripts/include/utils.h: -------------------------------------------------------------------------------- 1 | /* 2 | TOY: 3 | */ 4 | #ifndef _BOMB_UTILS_H_ 5 | #define _BOMB_UTILS_H_ 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | 23 | #ifdef __cplusplus 24 | extern "C" { 25 | #endif 26 | 27 | void Bomb(); 28 | 29 | void Foobar(); 30 | 31 | int GetSec(); 32 | 33 | long GetSecSince1970(); 34 | 35 | int SHA1_COMP(int, unsigned[5]); 36 | 37 | int open_socket(char* host,char *port); 38 | 39 | #ifdef __cplusplus 40 | } 41 | #endif 42 | #endif 43 | -------------------------------------------------------------------------------- /scripts/java_check.py: -------------------------------------------------------------------------------- 1 | import os 2 | import subprocess 3 | import json 4 | import tempfile 5 | import tqdm 6 | import sys 7 | import shutil 8 | 9 | def compile_and_run(focal_method, main): 10 | original_dir = os.getcwd() 11 | temp_dir = tempfile.TemporaryDirectory() 12 | os.chdir(temp_dir.name) 13 | with open("./Main.java", "w") as f: 14 | f.write(main) 15 | with open("./FocalMethod.java", "w") as f: 16 | f.write(focal_method) 17 | 18 | class_name = "Main" 19 | compile_command = ['javac', './Main.java', './FocalMethod.java'] 20 | compile_process = subprocess.run(compile_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE) 21 | 22 | if compile_process.returncode == 0: 23 | run_command = ['java', class_name] 24 | try: 25 | run_process = subprocess.run(run_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=10) 26 | output = run_process.stdout.decode('utf-8') 27 | error = run_process.stderr.decode('utf-8') 28 | rv = run_process.returncode 29 | except subprocess.TimeoutExpired: 30 | os.chdir(original_dir) 31 | shutil.rmtree(temp_dir.name) 32 | return -1 , "runtime error", "Timeout" 33 | else: 34 | error = compile_process.stderr.decode('utf-8') 35 | rv = -1 36 | output = "compile error" 37 | os.chdir(original_dir) 38 | shutil.rmtree(temp_dir.name) 39 | return rv, output, error 40 | 41 | def check_one(d, filling): 42 | focal_method = d['focal_method'] 43 | main = d['target'] 44 | new_main = main.replace("", filling) 45 | prefix = "import java.util.*;\nimport java.lang.*;\n" 46 | new_main = prefix + new_main 47 | return compile_and_run(focal_method, new_main) -------------------------------------------------------------------------------- /scripts/lib/aes.c: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | This is an implementation of the AES128 algorithm, specifically ECB and CBC mode. 4 | 5 | The implementation is verified against the test vectors in: 6 | National Institute of Standards and Technology Special Publication 800-38A 2001 ED 7 | 8 | ECB-AES128 9 | ---------- 10 | 11 | plain-text: 12 | 6bc1bee22e409f96e93d7e117393172a 13 | ae2d8a571e03ac9c9eb76fac45af8e51 14 | 30c81c46a35ce411e5fbc1191a0a52ef 15 | f69f2445df4f9b17ad2b417be66c3710 16 | 17 | key: 18 | 2b7e151628aed2a6abf7158809cf4f3c 19 | 20 | resulting cipher 21 | 3ad77bb40d7a3660a89ecaf32466ef97 22 | f5d3d58503b9699de785895a96fdbaaf 23 | 43b1cd7f598ece23881b00e3ed030688 24 | 7b0c785e27e8ad3f8223207104725dd4 25 | 26 | 27 | NOTE: String length must be evenly divisible by 16byte (str_len % 16 == 0) 28 | You should pad the end of the string with zeros if this is not the case. 29 | 30 | */ 31 | 32 | 33 | /*****************************************************************************/ 34 | /* Includes: */ 35 | /*****************************************************************************/ 36 | #include 37 | #include // CBC mode, for memset 38 | #include "aes.h" 39 | 40 | 41 | /*****************************************************************************/ 42 | /* Defines: */ 43 | /*****************************************************************************/ 44 | // The number of columns comprising a state in AES. This is a constant in AES. Value=4 45 | #define Nb 4 46 | // The number of 32 bit words in a key. 47 | #define Nk 4 48 | // Key length in bytes [128 bit] 49 | #define KEYLEN 16 50 | // The number of rounds in AES Cipher. 51 | #define Nr 10 52 | 53 | // jcallan@github points out that declaring Multiply as a function 54 | // reduces code size considerably with the Keil ARM compiler. 55 | // See this link for more information: https://github.com/kokke/tiny-AES128-C/pull/3 56 | #ifndef MULTIPLY_AS_A_FUNCTION 57 | #define MULTIPLY_AS_A_FUNCTION 0 58 | #endif 59 | 60 | 61 | /*****************************************************************************/ 62 | /* Private variables: */ 63 | /*****************************************************************************/ 64 | // state - array holding the intermediate results during decryption. 65 | typedef uint8_t state_t[4][4]; 66 | static state_t* state; 67 | 68 | // The array that stores the round keys. 69 | static uint8_t RoundKey[176]; 70 | 71 | // The Key input to the AES Program 72 | static const uint8_t* Key; 73 | 74 | #if defined(CBC) && CBC 75 | // Initial Vector used only for CBC mode 76 | static uint8_t* Iv; 77 | #endif 78 | 79 | // The lookup-tables are marked const so they can be placed in read-only storage instead of RAM 80 | // The numbers below can be computed dynamically trading ROM for RAM - 81 | // This can be useful in (embedded) bootloader applications, where ROM is often limited. 82 | static const uint8_t sbox[256] = { 83 | //0 1 2 3 4 5 6 7 8 9 A B C D E F 84 | 0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76, 85 | 0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0, 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0, 86 | 0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc, 0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15, 87 | 0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a, 0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75, 88 | 0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0, 0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84, 89 | 0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b, 0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf, 90 | 0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85, 0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8, 91 | 0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5, 0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2, 92 | 0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17, 0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73, 93 | 0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88, 0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb, 94 | 0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c, 0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79, 95 | 0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9, 0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08, 96 | 0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6, 0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a, 97 | 0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e, 0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e, 98 | 0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94, 0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf, 99 | 0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16 }; 100 | 101 | static const uint8_t rsbox[256] = 102 | { 0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38, 0xbf, 0x40, 0xa3, 0x9e, 0x81, 0xf3, 0xd7, 0xfb, 103 | 0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87, 0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb, 104 | 0x54, 0x7b, 0x94, 0x32, 0xa6, 0xc2, 0x23, 0x3d, 0xee, 0x4c, 0x95, 0x0b, 0x42, 0xfa, 0xc3, 0x4e, 105 | 0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2, 0x76, 0x5b, 0xa2, 0x49, 0x6d, 0x8b, 0xd1, 0x25, 106 | 0x72, 0xf8, 0xf6, 0x64, 0x86, 0x68, 0x98, 0x16, 0xd4, 0xa4, 0x5c, 0xcc, 0x5d, 0x65, 0xb6, 0x92, 107 | 0x6c, 0x70, 0x48, 0x50, 0xfd, 0xed, 0xb9, 0xda, 0x5e, 0x15, 0x46, 0x57, 0xa7, 0x8d, 0x9d, 0x84, 108 | 0x90, 0xd8, 0xab, 0x00, 0x8c, 0xbc, 0xd3, 0x0a, 0xf7, 0xe4, 0x58, 0x05, 0xb8, 0xb3, 0x45, 0x06, 109 | 0xd0, 0x2c, 0x1e, 0x8f, 0xca, 0x3f, 0x0f, 0x02, 0xc1, 0xaf, 0xbd, 0x03, 0x01, 0x13, 0x8a, 0x6b, 110 | 0x3a, 0x91, 0x11, 0x41, 0x4f, 0x67, 0xdc, 0xea, 0x97, 0xf2, 0xcf, 0xce, 0xf0, 0xb4, 0xe6, 0x73, 111 | 0x96, 0xac, 0x74, 0x22, 0xe7, 0xad, 0x35, 0x85, 0xe2, 0xf9, 0x37, 0xe8, 0x1c, 0x75, 0xdf, 0x6e, 112 | 0x47, 0xf1, 0x1a, 0x71, 0x1d, 0x29, 0xc5, 0x89, 0x6f, 0xb7, 0x62, 0x0e, 0xaa, 0x18, 0xbe, 0x1b, 113 | 0xfc, 0x56, 0x3e, 0x4b, 0xc6, 0xd2, 0x79, 0x20, 0x9a, 0xdb, 0xc0, 0xfe, 0x78, 0xcd, 0x5a, 0xf4, 114 | 0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31, 0xb1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xec, 0x5f, 115 | 0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d, 0x2d, 0xe5, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef, 116 | 0xa0, 0xe0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 0xb0, 0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61, 117 | 0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26, 0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d }; 118 | 119 | 120 | // The round constant word array, Rcon[i], contains the values given by 121 | // x to th e power (i-1) being powers of x (x is denoted as {02}) in the field GF(2^8) 122 | // Note that i starts at 1, not 0). 123 | static const uint8_t Rcon[255] = { 124 | 0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a, 125 | 0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef, 0xc5, 0x91, 0x39, 126 | 0x72, 0xe4, 0xd3, 0xbd, 0x61, 0xc2, 0x9f, 0x25, 0x4a, 0x94, 0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a, 127 | 0x74, 0xe8, 0xcb, 0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8, 128 | 0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef, 129 | 0xc5, 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd, 0x61, 0xc2, 0x9f, 0x25, 0x4a, 0x94, 0x33, 0x66, 0xcc, 130 | 0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb, 0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 131 | 0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3, 132 | 0x7d, 0xfa, 0xef, 0xc5, 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd, 0x61, 0xc2, 0x9f, 0x25, 0x4a, 0x94, 133 | 0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb, 0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 134 | 0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35, 135 | 0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef, 0xc5, 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd, 0x61, 0xc2, 0x9f, 136 | 0x25, 0x4a, 0x94, 0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb, 0x8d, 0x01, 0x02, 0x04, 137 | 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63, 138 | 0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef, 0xc5, 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd, 139 | 0x61, 0xc2, 0x9f, 0x25, 0x4a, 0x94, 0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb }; 140 | 141 | 142 | /*****************************************************************************/ 143 | /* Private functions: */ 144 | /*****************************************************************************/ 145 | static uint8_t getSBoxValue(uint8_t num) 146 | { 147 | return sbox[num]; 148 | } 149 | 150 | static uint8_t getSBoxInvert(uint8_t num) 151 | { 152 | return rsbox[num]; 153 | } 154 | 155 | // This function produces Nb(Nr+1) round keys. The round keys are used in each round to decrypt the states. 156 | static void KeyExpansion(void) 157 | { 158 | uint32_t i, j, k; 159 | uint8_t tempa[4]; // Used for the column/row operations 160 | 161 | // The first round key is the key itself. 162 | for(i = 0; i < Nk; ++i) 163 | { 164 | RoundKey[(i * 4) + 0] = Key[(i * 4) + 0]; 165 | RoundKey[(i * 4) + 1] = Key[(i * 4) + 1]; 166 | RoundKey[(i * 4) + 2] = Key[(i * 4) + 2]; 167 | RoundKey[(i * 4) + 3] = Key[(i * 4) + 3]; 168 | } 169 | 170 | // All other round keys are found from the previous round keys. 171 | for(; (i < (Nb * (Nr + 1))); ++i) 172 | { 173 | for(j = 0; j < 4; ++j) 174 | { 175 | tempa[j]=RoundKey[(i-1) * 4 + j]; 176 | } 177 | if (i % Nk == 0) 178 | { 179 | // This function rotates the 4 bytes in a word to the left once. 180 | // [a0,a1,a2,a3] becomes [a1,a2,a3,a0] 181 | 182 | // Function RotWord() 183 | { 184 | k = tempa[0]; 185 | tempa[0] = tempa[1]; 186 | tempa[1] = tempa[2]; 187 | tempa[2] = tempa[3]; 188 | tempa[3] = k; 189 | } 190 | 191 | // SubWord() is a function that takes a four-byte input word and 192 | // applies the S-box to each of the four bytes to produce an output word. 193 | 194 | // Function Subword() 195 | { 196 | tempa[0] = getSBoxValue(tempa[0]); 197 | tempa[1] = getSBoxValue(tempa[1]); 198 | tempa[2] = getSBoxValue(tempa[2]); 199 | tempa[3] = getSBoxValue(tempa[3]); 200 | } 201 | 202 | tempa[0] = tempa[0] ^ Rcon[i/Nk]; 203 | } 204 | else if (Nk > 6 && i % Nk == 4) 205 | { 206 | // Function Subword() 207 | { 208 | tempa[0] = getSBoxValue(tempa[0]); 209 | tempa[1] = getSBoxValue(tempa[1]); 210 | tempa[2] = getSBoxValue(tempa[2]); 211 | tempa[3] = getSBoxValue(tempa[3]); 212 | } 213 | } 214 | RoundKey[i * 4 + 0] = RoundKey[(i - Nk) * 4 + 0] ^ tempa[0]; 215 | RoundKey[i * 4 + 1] = RoundKey[(i - Nk) * 4 + 1] ^ tempa[1]; 216 | RoundKey[i * 4 + 2] = RoundKey[(i - Nk) * 4 + 2] ^ tempa[2]; 217 | RoundKey[i * 4 + 3] = RoundKey[(i - Nk) * 4 + 3] ^ tempa[3]; 218 | } 219 | } 220 | 221 | // This function adds the round key to state. 222 | // The round key is added to the state by an XOR function. 223 | static void AddRoundKey(uint8_t round) 224 | { 225 | uint8_t i,j; 226 | for(i=0;i<4;++i) 227 | { 228 | for(j = 0; j < 4; ++j) 229 | { 230 | (*state)[i][j] ^= RoundKey[round * Nb * 4 + i * Nb + j]; 231 | } 232 | } 233 | } 234 | 235 | // The SubBytes Function Substitutes the values in the 236 | // state matrix with values in an S-box. 237 | static void SubBytes(void) 238 | { 239 | uint8_t i, j; 240 | for(i = 0; i < 4; ++i) 241 | { 242 | for(j = 0; j < 4; ++j) 243 | { 244 | (*state)[j][i] = getSBoxValue((*state)[j][i]); 245 | } 246 | } 247 | } 248 | 249 | // The ShiftRows() function shifts the rows in the state to the left. 250 | // Each row is shifted with different offset. 251 | // Offset = Row number. So the first row is not shifted. 252 | static void ShiftRows(void) 253 | { 254 | uint8_t temp; 255 | 256 | // Rotate first row 1 columns to left 257 | temp = (*state)[0][1]; 258 | (*state)[0][1] = (*state)[1][1]; 259 | (*state)[1][1] = (*state)[2][1]; 260 | (*state)[2][1] = (*state)[3][1]; 261 | (*state)[3][1] = temp; 262 | 263 | // Rotate second row 2 columns to left 264 | temp = (*state)[0][2]; 265 | (*state)[0][2] = (*state)[2][2]; 266 | (*state)[2][2] = temp; 267 | 268 | temp = (*state)[1][2]; 269 | (*state)[1][2] = (*state)[3][2]; 270 | (*state)[3][2] = temp; 271 | 272 | // Rotate third row 3 columns to left 273 | temp = (*state)[0][3]; 274 | (*state)[0][3] = (*state)[3][3]; 275 | (*state)[3][3] = (*state)[2][3]; 276 | (*state)[2][3] = (*state)[1][3]; 277 | (*state)[1][3] = temp; 278 | } 279 | 280 | static uint8_t xtime(uint8_t x) 281 | { 282 | return ((x<<1) ^ (((x>>7) & 1) * 0x1b)); 283 | } 284 | 285 | // MixColumns function mixes the columns of the state matrix 286 | static void MixColumns(void) 287 | { 288 | uint8_t i; 289 | uint8_t Tmp,Tm,t; 290 | for(i = 0; i < 4; ++i) 291 | { 292 | t = (*state)[i][0]; 293 | Tmp = (*state)[i][0] ^ (*state)[i][1] ^ (*state)[i][2] ^ (*state)[i][3] ; 294 | Tm = (*state)[i][0] ^ (*state)[i][1] ; Tm = xtime(Tm); (*state)[i][0] ^= Tm ^ Tmp ; 295 | Tm = (*state)[i][1] ^ (*state)[i][2] ; Tm = xtime(Tm); (*state)[i][1] ^= Tm ^ Tmp ; 296 | Tm = (*state)[i][2] ^ (*state)[i][3] ; Tm = xtime(Tm); (*state)[i][2] ^= Tm ^ Tmp ; 297 | Tm = (*state)[i][3] ^ t ; Tm = xtime(Tm); (*state)[i][3] ^= Tm ^ Tmp ; 298 | } 299 | } 300 | 301 | // Multiply is used to multiply numbers in the field GF(2^8) 302 | #if MULTIPLY_AS_A_FUNCTION 303 | static uint8_t Multiply(uint8_t x, uint8_t y) 304 | { 305 | return (((y & 1) * x) ^ 306 | ((y>>1 & 1) * xtime(x)) ^ 307 | ((y>>2 & 1) * xtime(xtime(x))) ^ 308 | ((y>>3 & 1) * xtime(xtime(xtime(x)))) ^ 309 | ((y>>4 & 1) * xtime(xtime(xtime(xtime(x)))))); 310 | } 311 | #else 312 | #define Multiply(x, y) \ 313 | ( ((y & 1) * x) ^ \ 314 | ((y>>1 & 1) * xtime(x)) ^ \ 315 | ((y>>2 & 1) * xtime(xtime(x))) ^ \ 316 | ((y>>3 & 1) * xtime(xtime(xtime(x)))) ^ \ 317 | ((y>>4 & 1) * xtime(xtime(xtime(xtime(x)))))) \ 318 | 319 | #endif 320 | 321 | // MixColumns function mixes the columns of the state matrix. 322 | // The method used to multiply may be difficult to understand for the inexperienced. 323 | // Please use the references to gain more information. 324 | static void InvMixColumns(void) 325 | { 326 | int i; 327 | uint8_t a,b,c,d; 328 | for(i=0;i<4;++i) 329 | { 330 | a = (*state)[i][0]; 331 | b = (*state)[i][1]; 332 | c = (*state)[i][2]; 333 | d = (*state)[i][3]; 334 | 335 | (*state)[i][0] = Multiply(a, 0x0e) ^ Multiply(b, 0x0b) ^ Multiply(c, 0x0d) ^ Multiply(d, 0x09); 336 | (*state)[i][1] = Multiply(a, 0x09) ^ Multiply(b, 0x0e) ^ Multiply(c, 0x0b) ^ Multiply(d, 0x0d); 337 | (*state)[i][2] = Multiply(a, 0x0d) ^ Multiply(b, 0x09) ^ Multiply(c, 0x0e) ^ Multiply(d, 0x0b); 338 | (*state)[i][3] = Multiply(a, 0x0b) ^ Multiply(b, 0x0d) ^ Multiply(c, 0x09) ^ Multiply(d, 0x0e); 339 | } 340 | } 341 | 342 | 343 | // The SubBytes Function Substitutes the values in the 344 | // state matrix with values in an S-box. 345 | static void InvSubBytes(void) 346 | { 347 | uint8_t i,j; 348 | for(i=0;i<4;++i) 349 | { 350 | for(j=0;j<4;++j) 351 | { 352 | (*state)[j][i] = getSBoxInvert((*state)[j][i]); 353 | } 354 | } 355 | } 356 | 357 | static void InvShiftRows(void) 358 | { 359 | uint8_t temp; 360 | 361 | // Rotate first row 1 columns to right 362 | temp=(*state)[3][1]; 363 | (*state)[3][1]=(*state)[2][1]; 364 | (*state)[2][1]=(*state)[1][1]; 365 | (*state)[1][1]=(*state)[0][1]; 366 | (*state)[0][1]=temp; 367 | 368 | // Rotate second row 2 columns to right 369 | temp=(*state)[0][2]; 370 | (*state)[0][2]=(*state)[2][2]; 371 | (*state)[2][2]=temp; 372 | 373 | temp=(*state)[1][2]; 374 | (*state)[1][2]=(*state)[3][2]; 375 | (*state)[3][2]=temp; 376 | 377 | // Rotate third row 3 columns to right 378 | temp=(*state)[0][3]; 379 | (*state)[0][3]=(*state)[1][3]; 380 | (*state)[1][3]=(*state)[2][3]; 381 | (*state)[2][3]=(*state)[3][3]; 382 | (*state)[3][3]=temp; 383 | } 384 | 385 | 386 | // Cipher is the main function that encrypts the PlainText. 387 | static void Cipher(void) 388 | { 389 | uint8_t round = 0; 390 | 391 | // Add the First round key to the state before starting the rounds. 392 | AddRoundKey(0); 393 | 394 | // There will be Nr rounds. 395 | // The first Nr-1 rounds are identical. 396 | // These Nr-1 rounds are executed in the loop below. 397 | for(round = 1; round < Nr; ++round) 398 | { 399 | SubBytes(); 400 | ShiftRows(); 401 | MixColumns(); 402 | AddRoundKey(round); 403 | } 404 | 405 | // The last round is given below. 406 | // The MixColumns function is not here in the last round. 407 | SubBytes(); 408 | ShiftRows(); 409 | AddRoundKey(Nr); 410 | } 411 | 412 | static void InvCipher(void) 413 | { 414 | uint8_t round=0; 415 | 416 | // Add the First round key to the state before starting the rounds. 417 | AddRoundKey(Nr); 418 | 419 | // There will be Nr rounds. 420 | // The first Nr-1 rounds are identical. 421 | // These Nr-1 rounds are executed in the loop below. 422 | for(round=Nr-1;round>0;round--) 423 | { 424 | InvShiftRows(); 425 | InvSubBytes(); 426 | AddRoundKey(round); 427 | InvMixColumns(); 428 | } 429 | 430 | // The last round is given below. 431 | // The MixColumns function is not here in the last round. 432 | InvShiftRows(); 433 | InvSubBytes(); 434 | AddRoundKey(0); 435 | } 436 | 437 | static void BlockCopy(uint8_t* output, uint8_t* input) 438 | { 439 | uint8_t i; 440 | for (i=0;i 6 | * All Rights Reserved 7 | * 8 | ***************************************************************************** 9 | * $Id: sha1.c 12 2009-06-22 19:34:25Z paulej $ 10 | ***************************************************************************** 11 | * 12 | * Description: 13 | * This file implements the Secure Hashing Standard as defined 14 | * in FIPS PUB 180-1 published April 17, 1995. 15 | * 16 | * The Secure Hashing Standard, which uses the Secure Hashing 17 | * Algorithm (SHA), produces a 160-bit message digest for a 18 | * given data stream. In theory, it is highly improbable that 19 | * two messages will produce the same message digest. Therefore, 20 | * this algorithm can serve as a means of providing a "fingerprint" 21 | * for a message. 22 | * 23 | * Portability Issues: 24 | * SHA-1 is defined in terms of 32-bit "words". This code was 25 | * written with the expectation that the processor has at least 26 | * a 32-bit machine word size. If the machine word size is larger, 27 | * the code should still function properly. One caveat to that 28 | * is that the input functions taking characters and character 29 | * arrays assume that only 8 bits of information are stored in each 30 | * character. 31 | * 32 | * Caveats: 33 | * SHA-1 is designed to work with messages less than 2^64 bits 34 | * long. Although SHA-1 allows a message digest to be generated for 35 | * messages of any number of bits less than 2^64, this 36 | * implementation only works with messages with a length that is a 37 | * multiple of the size of an 8-bit character. 38 | * 39 | */ 40 | 41 | #include "sha1.h" 42 | 43 | /* 44 | * Define the circular shift macro 45 | */ 46 | #define SHA1CircularShift(bits,word) \ 47 | ((((word) << (bits)) & 0xFFFFFFFF) | \ 48 | ((word) >> (32-(bits)))) 49 | 50 | /* Function prototypes */ 51 | void SHA1ProcessMessageBlock(SHA1Context *); 52 | void SHA1PadMessage(SHA1Context *); 53 | 54 | /* 55 | * SHA1Reset 56 | * 57 | * Description: 58 | * This function will initialize the SHA1Context in preparation 59 | * for computing a new message digest. 60 | * 61 | * Parameters: 62 | * context: [in/out] 63 | * The context to reset. 64 | * 65 | * Returns: 66 | * Nothing. 67 | * 68 | * Comments: 69 | * 70 | */ 71 | void SHA1Reset(SHA1Context *context) 72 | { 73 | context->Length_Low = 0; 74 | context->Length_High = 0; 75 | context->Message_Block_Index = 0; 76 | 77 | context->Message_Digest[0] = 0x67452301; 78 | context->Message_Digest[1] = 0xEFCDAB89; 79 | context->Message_Digest[2] = 0x98BADCFE; 80 | context->Message_Digest[3] = 0x10325476; 81 | context->Message_Digest[4] = 0xC3D2E1F0; 82 | 83 | context->Computed = 0; 84 | context->Corrupted = 0; 85 | } 86 | 87 | /* 88 | * SHA1Result 89 | * 90 | * Description: 91 | * This function will return the 160-bit message digest into the 92 | * Message_Digest array within the SHA1Context provided 93 | * 94 | * Parameters: 95 | * context: [in/out] 96 | * The context to use to calculate the SHA-1 hash. 97 | * 98 | * Returns: 99 | * 1 if successful, 0 if it failed. 100 | * 101 | * Comments: 102 | * 103 | */ 104 | int SHA1Result(SHA1Context *context) 105 | { 106 | 107 | if (context->Corrupted) 108 | { 109 | return 0; 110 | } 111 | 112 | if (!context->Computed) 113 | { 114 | SHA1PadMessage(context); 115 | context->Computed = 1; 116 | } 117 | 118 | return 1; 119 | } 120 | 121 | /* 122 | * SHA1Input 123 | * 124 | * Description: 125 | * This function accepts an array of octets as the next portion of 126 | * the message. 127 | * 128 | * Parameters: 129 | * context: [in/out] 130 | * The SHA-1 context to update 131 | * message_array: [in] 132 | * An array of characters representing the next portion of the 133 | * message. 134 | * length: [in] 135 | * The length of the message in message_array 136 | * 137 | * Returns: 138 | * Nothing. 139 | * 140 | * Comments: 141 | * 142 | */ 143 | void SHA1Input( SHA1Context *context, 144 | const unsigned char *message_array, 145 | unsigned length) 146 | { 147 | if (!length) 148 | { 149 | return; 150 | } 151 | 152 | if (context->Computed || context->Corrupted) 153 | { 154 | context->Corrupted = 1; 155 | return; 156 | } 157 | 158 | while(length-- && !context->Corrupted) 159 | { 160 | context->Message_Block[context->Message_Block_Index++] = 161 | (*message_array & 0xFF); 162 | 163 | context->Length_Low += 8; 164 | /* Force it to 32 bits */ 165 | context->Length_Low &= 0xFFFFFFFF; 166 | if (context->Length_Low == 0) 167 | { 168 | context->Length_High++; 169 | /* Force it to 32 bits */ 170 | context->Length_High &= 0xFFFFFFFF; 171 | if (context->Length_High == 0) 172 | { 173 | /* Message is too long */ 174 | context->Corrupted = 1; 175 | } 176 | } 177 | 178 | if (context->Message_Block_Index == 64) 179 | { 180 | SHA1ProcessMessageBlock(context); 181 | } 182 | 183 | message_array++; 184 | } 185 | } 186 | 187 | /* 188 | * SHA1ProcessMessageBlock 189 | * 190 | * Description: 191 | * This function will process the next 512 bits of the message 192 | * stored in the Message_Block array. 193 | * 194 | * Parameters: 195 | * None. 196 | * 197 | * Returns: 198 | * Nothing. 199 | * 200 | * Comments: 201 | * Many of the variable names in the SHAContext, especially the 202 | * single character names, were used because those were the names 203 | * used in the publication. 204 | * 205 | * 206 | */ 207 | void SHA1ProcessMessageBlock(SHA1Context *context) 208 | { 209 | const unsigned K[] = /* Constants defined in SHA-1 */ 210 | { 211 | 0x5A827999, 212 | 0x6ED9EBA1, 213 | 0x8F1BBCDC, 214 | 0xCA62C1D6 215 | }; 216 | int t; /* Loop counter */ 217 | unsigned temp; /* Temporary word value */ 218 | unsigned W[80]; /* Word sequence */ 219 | unsigned A, B, C, D, E; /* Word buffers */ 220 | 221 | /* 222 | * Initialize the first 16 words in the array W 223 | */ 224 | for(t = 0; t < 16; t++) 225 | { 226 | W[t] = ((unsigned) context->Message_Block[t * 4]) << 24; 227 | W[t] |= ((unsigned) context->Message_Block[t * 4 + 1]) << 16; 228 | W[t] |= ((unsigned) context->Message_Block[t * 4 + 2]) << 8; 229 | W[t] |= ((unsigned) context->Message_Block[t * 4 + 3]); 230 | } 231 | 232 | for(t = 16; t < 80; t++) 233 | { 234 | W[t] = SHA1CircularShift(1,W[t-3] ^ W[t-8] ^ W[t-14] ^ W[t-16]); 235 | } 236 | 237 | A = context->Message_Digest[0]; 238 | B = context->Message_Digest[1]; 239 | C = context->Message_Digest[2]; 240 | D = context->Message_Digest[3]; 241 | E = context->Message_Digest[4]; 242 | 243 | for(t = 0; t < 20; t++) 244 | { 245 | temp = SHA1CircularShift(5,A) + 246 | ((B & C) | ((~B) & D)) + E + W[t] + K[0]; 247 | temp &= 0xFFFFFFFF; 248 | E = D; 249 | D = C; 250 | C = SHA1CircularShift(30,B); 251 | B = A; 252 | A = temp; 253 | } 254 | 255 | for(t = 20; t < 40; t++) 256 | { 257 | temp = SHA1CircularShift(5,A) + (B ^ C ^ D) + E + W[t] + K[1]; 258 | temp &= 0xFFFFFFFF; 259 | E = D; 260 | D = C; 261 | C = SHA1CircularShift(30,B); 262 | B = A; 263 | A = temp; 264 | } 265 | 266 | for(t = 40; t < 60; t++) 267 | { 268 | temp = SHA1CircularShift(5,A) + 269 | ((B & C) | (B & D) | (C & D)) + E + W[t] + K[2]; 270 | temp &= 0xFFFFFFFF; 271 | E = D; 272 | D = C; 273 | C = SHA1CircularShift(30,B); 274 | B = A; 275 | A = temp; 276 | } 277 | 278 | for(t = 60; t < 80; t++) 279 | { 280 | temp = SHA1CircularShift(5,A) + (B ^ C ^ D) + E + W[t] + K[3]; 281 | temp &= 0xFFFFFFFF; 282 | E = D; 283 | D = C; 284 | C = SHA1CircularShift(30,B); 285 | B = A; 286 | A = temp; 287 | } 288 | 289 | context->Message_Digest[0] = 290 | (context->Message_Digest[0] + A) & 0xFFFFFFFF; 291 | context->Message_Digest[1] = 292 | (context->Message_Digest[1] + B) & 0xFFFFFFFF; 293 | context->Message_Digest[2] = 294 | (context->Message_Digest[2] + C) & 0xFFFFFFFF; 295 | context->Message_Digest[3] = 296 | (context->Message_Digest[3] + D) & 0xFFFFFFFF; 297 | context->Message_Digest[4] = 298 | (context->Message_Digest[4] + E) & 0xFFFFFFFF; 299 | 300 | context->Message_Block_Index = 0; 301 | } 302 | 303 | /* 304 | * SHA1PadMessage 305 | * 306 | * Description: 307 | * According to the standard, the message must be padded to an even 308 | * 512 bits. The first padding bit must be a '1'. The last 64 309 | * bits represent the length of the original message. All bits in 310 | * between should be 0. This function will pad the message 311 | * according to those rules by filling the Message_Block array 312 | * accordingly. It will also call SHA1ProcessMessageBlock() 313 | * appropriately. When it returns, it can be assumed that the 314 | * message digest has been computed. 315 | * 316 | * Parameters: 317 | * context: [in/out] 318 | * The context to pad 319 | * 320 | * Returns: 321 | * Nothing. 322 | * 323 | * Comments: 324 | * 325 | */ 326 | void SHA1PadMessage(SHA1Context *context) 327 | { 328 | /* 329 | * Check to see if the current message block is too small to hold 330 | * the initial padding bits and length. If so, we will pad the 331 | * block, process it, and then continue padding into a second 332 | * block. 333 | */ 334 | if (context->Message_Block_Index > 55) 335 | { 336 | context->Message_Block[context->Message_Block_Index++] = 0x80; 337 | while(context->Message_Block_Index < 64) 338 | { 339 | context->Message_Block[context->Message_Block_Index++] = 0; 340 | } 341 | 342 | SHA1ProcessMessageBlock(context); 343 | 344 | while(context->Message_Block_Index < 56) 345 | { 346 | context->Message_Block[context->Message_Block_Index++] = 0; 347 | } 348 | } 349 | else 350 | { 351 | context->Message_Block[context->Message_Block_Index++] = 0x80; 352 | while(context->Message_Block_Index < 56) 353 | { 354 | context->Message_Block[context->Message_Block_Index++] = 0; 355 | } 356 | } 357 | 358 | /* 359 | * Store the message length as the last 8 octets 360 | */ 361 | context->Message_Block[56] = (context->Length_High >> 24) & 0xFF; 362 | context->Message_Block[57] = (context->Length_High >> 16) & 0xFF; 363 | context->Message_Block[58] = (context->Length_High >> 8) & 0xFF; 364 | context->Message_Block[59] = (context->Length_High) & 0xFF; 365 | context->Message_Block[60] = (context->Length_Low >> 24) & 0xFF; 366 | context->Message_Block[61] = (context->Length_Low >> 16) & 0xFF; 367 | context->Message_Block[62] = (context->Length_Low >> 8) & 0xFF; 368 | context->Message_Block[63] = (context->Length_Low) & 0xFF; 369 | 370 | SHA1ProcessMessageBlock(context); 371 | } 372 | -------------------------------------------------------------------------------- /scripts/lib/utils.c: -------------------------------------------------------------------------------- 1 | /* 2 | TOY: 3 | */ 4 | #include "utils.h" 5 | 6 | void Bomb(){ 7 | printf("bomb triggered\n"); 8 | } 9 | void Foobar(){ 10 | printf("nomal execution\n"); 11 | } 12 | int GetSec(){ 13 | time_t t_t; 14 | struct tm* t_tm; 15 | time(&t_t); 16 | t_tm = gmtime(&t_t); 17 | int sec = (int) t_tm->tm_sec; 18 | printf("Current second: %d\n", sec); 19 | return sec; 20 | } 21 | 22 | long GetSecSince1970(){ 23 | struct timeval tv; 24 | gettimeofday(&tv, NULL); 25 | printf("Seconds sine 1970: %ld\n", tv.tv_sec); 26 | return tv.tv_sec; 27 | } 28 | 29 | int open_socket(char* host,char *port){ 30 | struct addrinfo *res;// 31 | struct addrinfo hints; 32 | memset(&hints,0,sizeof(hints)); 33 | hints.ai_family=PF_UNSPEC; 34 | hints.ai_socktype=SOCK_STREAM; 35 | if(getaddrinfo(host,port,&hints,&res)==-1) 36 | return -1; 37 | int sock=socket(res->ai_family,res->ai_socktype, res->ai_protocol); 38 | if(sock==-1) 39 | return -1; 40 | 41 | int con=connect(sock,res->ai_addr,res->ai_addrlen); 42 | freeaddrinfo(res); 43 | if(con==-1) 44 | return -1; 45 | return sock; 46 | } 47 | -------------------------------------------------------------------------------- /scripts/py_check.py: -------------------------------------------------------------------------------- 1 | import os 2 | import json 3 | import tempfile 4 | import subprocess 5 | import tqdm 6 | 7 | def compile_and_run(code): 8 | temp_dir = tempfile.TemporaryDirectory() 9 | file_path = os.path.join(temp_dir.name, "temp.py") 10 | with open(file_path, "w") as f: 11 | f.write(code) 12 | try: 13 | result = subprocess.run(["python3", file_path], stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=10) 14 | output = result.stdout.decode("utf-8") 15 | stderr = result.stderr.decode("utf-8") 16 | except subprocess.TimeoutExpired: 17 | temp_dir.cleanup() 18 | return -1 , "runtime error", "Timeout" 19 | 20 | temp_dir.cleanup() 21 | return result.returncode, output, stderr 22 | 23 | def check_one(d, filling): 24 | target = d['target'] 25 | focal_method = d['focal_method'] 26 | target = target.replace("", filling) 27 | return compile_and_run(focal_method + "\n" + target) 28 | -------------------------------------------------------------------------------- /scripts/utils.py: -------------------------------------------------------------------------------- 1 | import argparse 2 | import json 3 | import os 4 | 5 | def load_data(file): 6 | with open(file) as f: 7 | return [json.loads(line) for line in f.readlines()] 8 | 9 | def dump_data(data, file): 10 | if os.path.exists(file): 11 | print(f"{file} exists") 12 | exit() 13 | with open(file, "a") as f: 14 | for d in data: 15 | f.write(json.dumps(d) + "\n") 16 | f.flush() -------------------------------------------------------------------------------- /utils.py: -------------------------------------------------------------------------------- 1 | import argparse 2 | import json 3 | import os 4 | 5 | 6 | def parse_iofile(): 7 | parser = argparse.ArgumentParser() 8 | parser.add_argument("-i", "--input_path", required=True, help="Input file path") 9 | parser.add_argument("-o", "--output_path", required=True, help="Output file path") 10 | args = parser.parse_args() 11 | return args 12 | 13 | def load_data(file): 14 | with open(file) as f: 15 | return [json.loads(line) for line in f.readlines()] 16 | 17 | def dump_data(data, file): 18 | if os.path.exists(file): 19 | print(f"{file} exists") 20 | exit() 21 | with open(file, "a") as f: 22 | for d in data: 23 | f.write(json.dumps(d) + "\n") 24 | f.flush() --------------------------------------------------------------------------------