├── .gitignore ├── .idea ├── compiler.xml ├── encodings.xml ├── misc.xml └── vcs.xml ├── LICENSE ├── README.md ├── build.gradle ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── settings.gradle └── src └── main └── java └── cn └── edu └── thu └── tsmart ├── core ├── cfa │ ├── llvm │ │ ├── APFloat.java │ │ ├── APInt.java │ │ ├── AddrSpaceCastInst.java │ │ ├── AllocaInst.java │ │ ├── Argument.java │ │ ├── ArrayType.java │ │ ├── AtomicCmpXchgInst.java │ │ ├── AtomicRMWInst.java │ │ ├── Attribute.java │ │ ├── AttributeList.java │ │ ├── BasicBlock.java │ │ ├── BinaryConstantExpr.java │ │ ├── BinaryOperator.java │ │ ├── BitCastInst.java │ │ ├── BranchInst.java │ │ ├── CallInst.java │ │ ├── CastInst.java │ │ ├── CatchPadInst.java │ │ ├── CatchReturnInst.java │ │ ├── CatchSwitchInst.java │ │ ├── CleanupPadInst.java │ │ ├── CleanupReturnInst.java │ │ ├── CmpInst.java │ │ ├── CompareConstantExpr.java │ │ ├── CompositeType.java │ │ ├── Constant.java │ │ ├── ConstantAggregate.java │ │ ├── ConstantAggregateZero.java │ │ ├── ConstantArray.java │ │ ├── ConstantData.java │ │ ├── ConstantDataArray.java │ │ ├── ConstantDataSequential.java │ │ ├── ConstantDataVector.java │ │ ├── ConstantExpr.java │ │ ├── ConstantFP.java │ │ ├── ConstantInt.java │ │ ├── ConstantPointerNull.java │ │ ├── ConstantStruct.java │ │ ├── Context.java │ │ ├── Converter.java │ │ ├── DataLayout.java │ │ ├── ExtractElementConstantExpr.java │ │ ├── ExtractElementInst.java │ │ ├── ExtractValueConstantExpr.java │ │ ├── ExtractValueInst.java │ │ ├── FCmpInst.java │ │ ├── FPExtInst.java │ │ ├── FPToSIInst.java │ │ ├── FPToUIInst.java │ │ ├── FPTruncInst.java │ │ ├── FenceInst.java │ │ ├── FuncletPadInst.java │ │ ├── FunctionType.java │ │ ├── GetElementPtrConstantExpr.java │ │ ├── GetElementPtrInst.java │ │ ├── GlobalObject.java │ │ ├── GlobalValue.java │ │ ├── GlobalVariable.java │ │ ├── ICmpInst.java │ │ ├── IInstruction.java │ │ ├── IRReader.java │ │ ├── IndirectBrInst.java │ │ ├── InsertElementConstantExpr.java │ │ ├── InsertElementInst.java │ │ ├── InsertValueConstantExpr.java │ │ ├── InsertValueInst.java │ │ ├── Instruction.java │ │ ├── InstructionProperties.java │ │ ├── IntToPtrInst.java │ │ ├── IntegerType.java │ │ ├── InvokeInst.java │ │ ├── LandingPadInst.java │ │ ├── LlvmFunction.java │ │ ├── LlvmModule.java │ │ ├── LoadInst.java │ │ ├── Metadata.java │ │ ├── PhiNode.java │ │ ├── PointerType.java │ │ ├── PtrToIntInst.java │ │ ├── ResumeInst.java │ │ ├── ReturnInst.java │ │ ├── SExtInst.java │ │ ├── SIToFPInst.java │ │ ├── SelectConstantExpr.java │ │ ├── SelectInst.java │ │ ├── SequentialType.java │ │ ├── ShuffleVectorConstantExpr.java │ │ ├── ShuffleVectorInst.java │ │ ├── StoreInst.java │ │ ├── StructType.java │ │ ├── SwitchInst.java │ │ ├── TerminatorInst.java │ │ ├── TruncInst.java │ │ ├── Type.java │ │ ├── UIToFPInst.java │ │ ├── UnaryConstantExpr.java │ │ ├── UnaryInstruction.java │ │ ├── UndefValue.java │ │ ├── UnreachableInst.java │ │ ├── Use.java │ │ ├── User.java │ │ ├── VAArgInst.java │ │ ├── Value.java │ │ ├── VectorType.java │ │ └── ZExtInst.java │ └── util │ │ ├── Casting.java │ │ ├── Formatter.java │ │ └── visitor │ │ └── InstructionVisitor.java └── exceptions │ └── CPAException.java └── util ├── Trouble.java └── lang └── LlvmUtil.java /.gitignore: -------------------------------------------------------------------------------- 1 | # Created by .ignore support plugin (hsz.mobi) 2 | ### Java template 3 | # Compiled class file 4 | *.class 5 | 6 | # Log file 7 | *.log 8 | 9 | # BlueJ files 10 | *.ctxt 11 | 12 | # Mobile Tools for Java (J2ME) 13 | .mtj.tmp/ 14 | 15 | # Package Files # 16 | *.jar 17 | *.war 18 | *.ear 19 | *.zip 20 | *.tar.gz 21 | *.rar 22 | 23 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 24 | hs_err_pid* 25 | ### Gradle template 26 | .gradle 27 | /build/ 28 | 29 | # Ignore Gradle GUI config 30 | gradle-app.setting 31 | 32 | # Avoid ignoring Gradle wrapper jar file (.jar files are usually ignored) 33 | !gradle-wrapper.jar 34 | 35 | # Cache of project 36 | .gradletasknamecache 37 | 38 | # # Work around https://youtrack.jetbrains.com/issue/IDEA-116898 39 | # gradle/wrapper/gradle-wrapper.properties 40 | ### JetBrains template 41 | # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm 42 | # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 43 | 44 | # User-specific stuff: 45 | .idea/**/workspace.xml 46 | .idea/**/tasks.xml 47 | .idea/dictionaries 48 | 49 | # Sensitive or high-churn files: 50 | .idea/**/dataSources/ 51 | .idea/**/dataSources.ids 52 | .idea/**/dataSources.local.xml 53 | .idea/**/sqlDataSources.xml 54 | .idea/**/dynamic.xml 55 | .idea/**/uiDesigner.xml 56 | 57 | # Gradle: 58 | .idea/**/gradle.xml 59 | .idea/**/libraries 60 | 61 | # CMake 62 | cmake-build-debug/ 63 | cmake-build-release/ 64 | 65 | # Mongo Explorer plugin: 66 | .idea/**/mongoSettings.xml 67 | 68 | ## File-based project format: 69 | *.iws 70 | 71 | ## Plugin-specific files: 72 | 73 | # IntelliJ 74 | out/ 75 | 76 | # mpeltonen/sbt-idea plugin 77 | .idea_modules/ 78 | 79 | # JIRA plugin 80 | atlassian-ide-plugin.xml 81 | 82 | # Cursive Clojure plugin 83 | .idea/replstate.xml 84 | 85 | # Crashlytics plugin (for Android Studio and IntelliJ) 86 | com_crashlytics_export_strings.xml 87 | crashlytics.properties 88 | crashlytics-build.properties 89 | fabric.properties 90 | 91 | -------------------------------------------------------------------------------- /.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Institute on Software System and Engineering, School of Software, Tsinghua University 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 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # llvm-java-binding 2 | 3 | There are already java bindings for LLVM C API, like [javacpp-presets](https://github.com/bytedeco/javacpp-presets/tree/master/llvm). 4 | However, using the C API in java is painful and most of the LLVM examples/documents/stackoverflows are about using LLVM in C++ way. 5 | To ease the usage of LLVM in java project, we built this library for writting our own software in java upon analysis LLVM IR. 6 | 7 | The goal of this project is finally mocking the full LLVM C++ Class in java. 8 | Currently not all llvm staffs are mapped, but most of common use should be usable. (We used this library in our real world project) 9 | The LLVM and Clang version we use on master branch is **3.9.0** 10 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | /* 2 | * This build file was generated by the Gradle 'init' task. 3 | * 4 | * This generated file contains a sample Java Library project to get you started. 5 | * For more details take a look at the Java Libraries chapter in the Gradle 6 | * user guide available at https://docs.gradle.org/4.2.1/userguide/java_library_plugin.html 7 | */ 8 | 9 | // Apply the java-library plugin to add support for Java Library 10 | apply plugin: 'java-library' 11 | 12 | // In this section you declare where to find the dependencies of your project 13 | repositories { 14 | // Use jcenter for resolving your dependencies. 15 | // You can declare any Maven/Ivy/file repository here. 16 | jcenter() 17 | } 18 | 19 | dependencies { 20 | // This dependency is exported to consumers, that is to say found on their compile classpath. 21 | //api 'org.apache.commons:commons-math3:3.6.1' 22 | 23 | implementation 'org.bytedeco.javacpp-presets:llvm-platform:3.9.0-1.3' 24 | implementation 'org.javolution:javolution-core-java:6.0.0' 25 | implementation 'com.google.guava:guava:21.0' 26 | implementation 'com.google.code.findbugs:jsr305:3.0.0' 27 | implementation files('libs/mod-commons-3.0.jar') 28 | 29 | // Use JUnit test framework 30 | testImplementation 'junit:junit:4.12' 31 | } 32 | 33 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tsmart-thu/llvm-java-binding/9ed6f2be4d5a1964d11f2f8fcb7f5750dad9e0a4/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Thu Jul 19 14:46:15 CST 2018 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-4.2.1-all.zip 7 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | set DIRNAME=%~dp0 12 | if "%DIRNAME%" == "" set DIRNAME=. 13 | set APP_BASE_NAME=%~n0 14 | set APP_HOME=%DIRNAME% 15 | 16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 17 | set DEFAULT_JVM_OPTS= 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windows variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | 53 | :win9xME_args 54 | @rem Slurp the command line arguments. 55 | set CMD_LINE_ARGS= 56 | set _SKIP=2 57 | 58 | :win9xME_args_slurp 59 | if "x%~1" == "x" goto execute 60 | 61 | set CMD_LINE_ARGS=%* 62 | 63 | :execute 64 | @rem Setup the command line 65 | 66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 67 | 68 | @rem Execute Gradle 69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 70 | 71 | :end 72 | @rem End local scope for the variables with windows NT shell 73 | if "%ERRORLEVEL%"=="0" goto mainEnd 74 | 75 | :fail 76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 77 | rem the _cmd.exe /c_ return code! 78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 79 | exit /b 1 80 | 81 | :mainEnd 82 | if "%OS%"=="Windows_NT" endlocal 83 | 84 | :omega 85 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | /* 2 | * This settings file was generated by the Gradle 'init' task. 3 | * 4 | * The settings file is used to specify which projects to include in your build. 5 | * In a single project build this file can be empty or even removed. 6 | * 7 | * Detailed information about configuring a multi-project build in Gradle can be found 8 | * in the user guide at https://docs.gradle.org/4.2.1/userguide/multi_project_builds.html 9 | */ 10 | 11 | /* 12 | // To declare projects as part of a multi-project build use the 'include' method 13 | include 'shared' 14 | include 'api' 15 | include 'services:webservice' 16 | */ 17 | 18 | rootProject.name = 'llvm-java-binding' 19 | -------------------------------------------------------------------------------- /src/main/java/cn/edu/thu/tsmart/core/cfa/llvm/AddrSpaceCastInst.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Institute on Software System and Engineering, School of Software, Tsinghua University 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package cn.edu.thu.tsmart.core.cfa.llvm; 25 | 26 | import static cn.edu.thu.tsmart.core.cfa.llvm.InstructionProperties.*; 27 | 28 | /** 29 | * @author guangchen on 27/02/2017. 30 | */ 31 | public class AddrSpaceCastInst extends CastInst { 32 | 33 | public AddrSpaceCastInst(String name, Type type) { 34 | super(name, type); 35 | super.opCode = OpCode.ADDRSPACECAST; 36 | } 37 | 38 | public Value getPointerOperand() { 39 | return getOperand(0); 40 | } 41 | 42 | // NOTICE return type uses int to store unsigned 43 | public int getSrcPointerAddressSpace() { 44 | return getPointerOperand().getType().getPointerAddressSpace(); 45 | } 46 | 47 | // NOTICE return type uses int to store unsigned 48 | public int getDestPointerAddressSpace() { 49 | return getType().getPointerAddressSpace(); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/main/java/cn/edu/thu/tsmart/core/cfa/llvm/Argument.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Institute on Software System and Engineering, School of Software, Tsinghua University 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package cn.edu.thu.tsmart.core.cfa.llvm; 25 | 26 | /** 27 | * @author guangchen on 08/06/2017. 28 | */ 29 | public class Argument extends Value { 30 | 31 | private LlvmFunction function; 32 | 33 | public Argument(String name, Type type) { 34 | super(name, type); 35 | } 36 | 37 | public void setFunction(LlvmFunction function) { 38 | this.function = function; 39 | } 40 | 41 | public LlvmFunction getFunction() { 42 | return function; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/cn/edu/thu/tsmart/core/cfa/llvm/ArrayType.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Institute on Software System and Engineering, School of Software, Tsinghua University 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package cn.edu.thu.tsmart.core.cfa.llvm; 25 | 26 | /** @author guangchen on 01/03/2017. */ 27 | public class ArrayType extends SequentialType { 28 | private Type elementType; 29 | private long numElements; 30 | 31 | protected ArrayType(Context context, Type elementType, long numElements) { 32 | super(context, TypeID.ArrayTyID); 33 | this.elementType = elementType; 34 | this.numElements = numElements; 35 | } 36 | 37 | public long getNumElements() { 38 | return numElements; 39 | } 40 | 41 | @Override 42 | public Type getElementType() { 43 | return elementType; 44 | } 45 | 46 | public static ArrayType get(Type elementType, long numElements) { 47 | return new ArrayType(elementType.getContext(), elementType, numElements); 48 | } 49 | 50 | @Override 51 | public String toString() { 52 | return "[" + getNumElements() +" x " + getElementType().toString() + "]"; 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/main/java/cn/edu/thu/tsmart/core/cfa/llvm/AtomicCmpXchgInst.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Institute on Software System and Engineering, School of Software, Tsinghua University 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package cn.edu.thu.tsmart.core.cfa.llvm; 25 | 26 | import static cn.edu.thu.tsmart.core.cfa.llvm.InstructionProperties.AtomicOrdering; 27 | import static cn.edu.thu.tsmart.core.cfa.llvm.InstructionProperties.OpCode; 28 | import static cn.edu.thu.tsmart.core.cfa.llvm.InstructionProperties.SynchronizationScope; 29 | 30 | import cn.edu.thu.tsmart.core.cfa.util.visitor.InstructionVisitor; 31 | import cn.edu.thu.tsmart.core.exceptions.CPAException; 32 | 33 | /** 34 | * @author guangchen on 27/02/2017. 35 | */ 36 | public class AtomicCmpXchgInst extends Instruction { 37 | 38 | // TODO initialize in Converter 39 | private boolean isVolatile = false; 40 | private boolean isWeak = false; 41 | private AtomicOrdering successOrdering = null; 42 | private AtomicOrdering failureOrdering = null; 43 | private SynchronizationScope synchScope = null; 44 | 45 | public AtomicCmpXchgInst(String name, Type type) { 46 | super(name, type); 47 | super.opCode = OpCode.CMPXCHG; 48 | } 49 | 50 | // only for Converter 51 | public void setVolatile(boolean aVolatile) { 52 | isVolatile = aVolatile; 53 | } 54 | 55 | // only for Converter 56 | public void setWeak(boolean weak) { 57 | isWeak = weak; 58 | } 59 | 60 | // only for Converter 61 | public void setSuccessOrdering(AtomicOrdering successOrdering) { 62 | this.successOrdering = successOrdering; 63 | } 64 | 65 | // only for Converter 66 | public void setFailureOrdering(AtomicOrdering failureOrdering) { 67 | this.failureOrdering = failureOrdering; 68 | } 69 | 70 | // only for Converter 71 | public void setSynchScope(SynchronizationScope synchScope) { 72 | this.synchScope = synchScope; 73 | } 74 | 75 | public boolean isVolatile() { 76 | return isVolatile; 77 | } 78 | 79 | public boolean isWeak() { 80 | return isWeak; 81 | } 82 | 83 | public AtomicOrdering getSuccessOrdering() { 84 | return successOrdering; 85 | } 86 | 87 | public AtomicOrdering getFailureOrdering() { 88 | return failureOrdering; 89 | } 90 | 91 | public SynchronizationScope getSynchScope() { 92 | return synchScope; 93 | } 94 | 95 | public Value getPointerOperand() { 96 | return getOperand(0); 97 | } 98 | 99 | public Value getCompareOperand() { 100 | return getOperand(1); 101 | } 102 | 103 | public Value getNewValOperand() { 104 | return getOperand(2); 105 | } 106 | 107 | // NOTICE return type uses int to store unsigned 108 | public int getPointerAddressSpace() { 109 | return getPointerOperand().getType().getPointerAddressSpace(); 110 | } 111 | 112 | @Override 113 | public R accept(InstructionVisitor visitor) throws E { 114 | return visitor.visit(this); 115 | } 116 | } 117 | -------------------------------------------------------------------------------- /src/main/java/cn/edu/thu/tsmart/core/cfa/llvm/AtomicRMWInst.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Institute on Software System and Engineering, School of Software, Tsinghua University 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package cn.edu.thu.tsmart.core.cfa.llvm; 25 | 26 | import static cn.edu.thu.tsmart.core.cfa.llvm.InstructionProperties.AtomicOrdering; 27 | import static cn.edu.thu.tsmart.core.cfa.llvm.InstructionProperties.BinOp; 28 | import static cn.edu.thu.tsmart.core.cfa.llvm.InstructionProperties.OpCode; 29 | import static cn.edu.thu.tsmart.core.cfa.llvm.InstructionProperties.SynchronizationScope; 30 | 31 | import cn.edu.thu.tsmart.core.cfa.util.visitor.InstructionVisitor; 32 | import cn.edu.thu.tsmart.core.exceptions.CPAException; 33 | 34 | /** 35 | * @author guangchen on 27/02/2017. 36 | */ 37 | public class AtomicRMWInst extends Instruction { 38 | 39 | // TODO initialize in Converter 40 | private BinOp operation = null; 41 | private boolean isVolatile = false; 42 | private AtomicOrdering ordering = null; 43 | private SynchronizationScope synchScope = null; 44 | 45 | public AtomicRMWInst(String name, Type type) { 46 | super(name, type); 47 | super.opCode = OpCode.ATOMICRMW; 48 | } 49 | 50 | // only for Converter 51 | public void setOperation(BinOp operation) { 52 | this.operation = operation; 53 | } 54 | 55 | // only for Converter 56 | public void setVolatile(boolean aVolatile) { 57 | isVolatile = aVolatile; 58 | } 59 | 60 | // only for Converter 61 | public void setOrdering(AtomicOrdering ordering) { 62 | this.ordering = ordering; 63 | } 64 | 65 | // only for Converter 66 | public void setSynchScope(SynchronizationScope synchScope) { 67 | this.synchScope = synchScope; 68 | } 69 | 70 | public BinOp getOperation() { 71 | return operation; 72 | } 73 | 74 | public boolean isVolatile() { 75 | return isVolatile; 76 | } 77 | 78 | public AtomicOrdering getOrdering() { 79 | return ordering; 80 | } 81 | 82 | public SynchronizationScope getSynchScope() { 83 | return synchScope; 84 | } 85 | 86 | public Value getPointerOperand() { 87 | return getOperand(0); 88 | } 89 | 90 | public Value getValOperand() { 91 | return getOperand(1); 92 | } 93 | 94 | // NOTICE return type uses int to store unsigned 95 | public int getPointerAddressSpace() { 96 | return getPointerOperand().getType().getPointerAddressSpace(); 97 | } 98 | 99 | @Override 100 | public R accept(InstructionVisitor visitor) throws E { 101 | return visitor.visit(this); 102 | } 103 | } 104 | -------------------------------------------------------------------------------- /src/main/java/cn/edu/thu/tsmart/core/cfa/llvm/BasicBlock.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Institute on Software System and Engineering, School of Software, Tsinghua University 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package cn.edu.thu.tsmart.core.cfa.llvm; 25 | 26 | import java.util.ArrayList; 27 | import java.util.List; 28 | 29 | /** 30 | * @author guangchen on 26/02/2017. 31 | */ 32 | public class BasicBlock extends Value { 33 | private List instList = new ArrayList<>(); 34 | private LlvmFunction parent; 35 | 36 | public BasicBlock() {} 37 | 38 | public BasicBlock(String name, Type type, LlvmFunction parent, List instList) { 39 | super(name, type); 40 | this.parent = parent; 41 | this.instList = instList; 42 | } 43 | 44 | public BasicBlock(String name, Type type, LlvmFunction parent) { 45 | super(name, type); 46 | this.parent = parent; 47 | } 48 | 49 | public List getInstList() { 50 | return instList; 51 | } 52 | 53 | public void setInstList(List instList) { 54 | this.instList = instList; 55 | } 56 | 57 | public LlvmFunction getParent() { 58 | return parent; 59 | } 60 | 61 | public void setParent(LlvmFunction parent) { 62 | this.parent = parent; 63 | } 64 | 65 | @Override 66 | public int hashCode() { 67 | return instList.hashCode(); 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /src/main/java/cn/edu/thu/tsmart/core/cfa/llvm/BinaryConstantExpr.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Institute on Software System and Engineering, School of Software, Tsinghua University 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package cn.edu.thu.tsmart.core.cfa.llvm; 25 | 26 | import cn.edu.thu.sse.common.util.Pair; 27 | import cn.edu.thu.tsmart.core.cfa.llvm.InstructionProperties.OpCode; 28 | import cn.edu.thu.tsmart.core.cfa.llvm.InstructionProperties.OperatorFlags; 29 | import java.util.HashMap; 30 | 31 | /** 32 | * Created by zhch on 2017/5/15. 33 | */ 34 | public class BinaryConstantExpr extends ConstantExpr { 35 | 36 | private OperatorFlags flags; 37 | private static HashMap, Pair>, BinaryConstantExpr> instances = new HashMap<>(); 38 | 39 | private BinaryConstantExpr(String name, Type type, OpCode opCode, OperatorFlags flags) { 40 | super(name, type, opCode); 41 | this.flags = flags; 42 | } 43 | 44 | public static BinaryConstantExpr getInstance(String name, Type type, OpCode opCode, 45 | OperatorFlags flags, Constant op1, Constant op2) { 46 | Pair, Pair> key = Pair 47 | .of(Pair.of(opCode, flags), Pair.of(op1, op2)); 48 | if (instances.containsKey(key)) { 49 | return instances.get(key); 50 | } else { 51 | BinaryConstantExpr instance = new BinaryConstantExpr(name, type, opCode, flags); 52 | instances.put(key, instance); 53 | return instance; 54 | } 55 | } 56 | 57 | @Override 58 | public String toString() { 59 | String res = getOpcode().toString() + " "; 60 | if(flags.hasAnyFlag()) 61 | res += flags.toString() + " "; 62 | res += "("; 63 | for(int i = 0; i < getNumOperands(); i++) { 64 | res += getOperand(i).getType().toString() + " "; 65 | res += getOperand(i).toString(); 66 | if(i != getNumOperands() - 1) 67 | res += ", "; 68 | } 69 | res += ")"; 70 | return res; 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /src/main/java/cn/edu/thu/tsmart/core/cfa/llvm/BinaryOperator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Institute on Software System and Engineering, School of Software, Tsinghua University 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package cn.edu.thu.tsmart.core.cfa.llvm; 25 | 26 | import cn.edu.thu.tsmart.core.cfa.util.Formatter; 27 | import cn.edu.thu.tsmart.core.cfa.util.visitor.InstructionVisitor; 28 | import cn.edu.thu.tsmart.core.exceptions.CPAException; 29 | import java.util.Arrays; 30 | import java.util.List; 31 | 32 | import static cn.edu.thu.tsmart.core.cfa.llvm.InstructionProperties.OpCode; 33 | import static cn.edu.thu.tsmart.core.cfa.llvm.InstructionProperties.OperatorFlags; 34 | 35 | /** 36 | * @author guangchen on 27/02/2017. 37 | */ 38 | public class BinaryOperator extends Instruction { 39 | 40 | public BinaryOperator(String name, Type type, OpCode opcode) { 41 | super(name, type); 42 | super.opCode = opcode; 43 | } 44 | 45 | @Override 46 | public R accept(InstructionVisitor visitor) throws E { 47 | return visitor.visit(this); 48 | } 49 | 50 | private String operatorToString() { 51 | switch (opCode) { 52 | case ADD: 53 | return "add"; 54 | case FADD: 55 | return "fadd"; 56 | case SUB: 57 | return "sub"; 58 | case FSUB: 59 | return "fsub"; 60 | case MUL: 61 | return "mul"; 62 | case FMUL: 63 | return "fmul"; 64 | case SDIV: 65 | return "sdiv"; 66 | case UDIV: 67 | return "udiv"; 68 | case UREM: 69 | return "urem"; 70 | case FREM: 71 | return "frem"; 72 | case SHL: 73 | return "shl"; 74 | case ASHR: 75 | return "ashr"; 76 | case LSHR: 77 | return "lshr"; 78 | case AND: 79 | return "and"; 80 | case OR: 81 | return "or"; 82 | case XOR: 83 | return "xor"; 84 | case SREM: 85 | return "srem"; 86 | case FDIV: 87 | return "fdiv"; 88 | } 89 | return ""; 90 | } 91 | 92 | public static Instruction create(OpCode opCode, Value operand1, Value operand2) { 93 | List list = Arrays.asList(operand1, operand2); 94 | BinaryOperator instruction = new BinaryOperator("", operand1.getType(), opCode); 95 | instruction.setOperands(list); 96 | return instruction; 97 | } 98 | 99 | @Override 100 | public String toString() { 101 | String res = "%" + getName() + " = " + operatorToString() + " "; 102 | OperatorFlags operatorFlags = getOperatorFlags(); 103 | if (operatorFlags != null && !operatorFlags.toString().equals("")) { 104 | res += operatorFlags.toString() + " "; 105 | } 106 | res += 107 | getType().toString() + " " + Formatter.asOperand(getOperand(0)) + ", " + Formatter.asOperand(getOperand(1)); 108 | return res; 109 | } 110 | } 111 | -------------------------------------------------------------------------------- /src/main/java/cn/edu/thu/tsmart/core/cfa/llvm/BitCastInst.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Institute on Software System and Engineering, School of Software, Tsinghua University 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package cn.edu.thu.tsmart.core.cfa.llvm; 25 | 26 | import static cn.edu.thu.tsmart.core.cfa.llvm.InstructionProperties.OpCode; 27 | 28 | /** @author guangchen on 27/02/2017. */ 29 | public class BitCastInst extends CastInst { 30 | 31 | public BitCastInst(String name, Type type) { 32 | super(name, type); 33 | super.opCode = OpCode.BITCAST; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/main/java/cn/edu/thu/tsmart/core/cfa/llvm/BranchInst.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Institute on Software System and Engineering, School of Software, Tsinghua University 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package cn.edu.thu.tsmart.core.cfa.llvm; 25 | 26 | import static cn.edu.thu.tsmart.core.cfa.llvm.InstructionProperties.OpCode; 27 | import static cn.edu.thu.tsmart.core.cfa.util.Casting.castOrNull; 28 | 29 | import cn.edu.thu.tsmart.core.cfa.util.Formatter; 30 | import cn.edu.thu.tsmart.core.cfa.util.visitor.InstructionVisitor; 31 | import cn.edu.thu.tsmart.core.exceptions.CPAException; 32 | import javax.annotation.Nullable; 33 | 34 | /** 35 | * @author guangchen on 27/02/2017. 36 | */ 37 | public class BranchInst extends TerminatorInst { 38 | 39 | public BranchInst(String name, Type type) { 40 | super(name, type); 41 | super.opCode = OpCode.BR; 42 | } 43 | 44 | public boolean isUnconditional() { 45 | return getNumOperands() == 1; 46 | } 47 | 48 | public boolean isConditional() { 49 | return getNumOperands() == 3; 50 | } 51 | 52 | public Value getCondition() { 53 | assert isConditional() : "Cannot get condition of an uncond branch!"; 54 | return getOperand(0); 55 | } 56 | 57 | @Override 58 | public int getNumSuccessors() { 59 | if (isConditional()) { 60 | return 2; 61 | } else { 62 | return 1; 63 | } 64 | } 65 | 66 | @Override 67 | @Nullable 68 | public BasicBlock getSuccessor(int i) { 69 | assert i >= 0 && i < getNumSuccessors() : "Successor # out of range for branch!"; 70 | if (isConditional()) { 71 | // note: successor order is reverse from operand order 72 | return castOrNull(getOperand(getNumSuccessors() - i), BasicBlock.class); 73 | } else { 74 | return castOrNull(getOperand(i), BasicBlock.class); 75 | } 76 | } 77 | 78 | @Override 79 | public R accept(InstructionVisitor visitor) throws E { 80 | return visitor.visit(this); 81 | } 82 | 83 | @Override 84 | public String toString() { 85 | String res = "br "; 86 | if (isConditional()) { 87 | res += getCondition().getType().toString() + " " + Formatter.asOperand(getCondition()) + ", "; 88 | } 89 | res += "label %" + getSuccessor(0).getName(); 90 | if (isConditional()) { 91 | res += ", label %" + getSuccessor(1).getName(); 92 | } 93 | return res; 94 | } 95 | } 96 | -------------------------------------------------------------------------------- /src/main/java/cn/edu/thu/tsmart/core/cfa/llvm/CatchPadInst.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Institute on Software System and Engineering, School of Software, Tsinghua University 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package cn.edu.thu.tsmart.core.cfa.llvm; 25 | 26 | import static cn.edu.thu.tsmart.core.cfa.llvm.InstructionProperties.*; 27 | 28 | import cn.edu.thu.tsmart.core.cfa.util.visitor.InstructionVisitor; 29 | import cn.edu.thu.tsmart.core.exceptions.CPAException; 30 | 31 | /** 32 | * @author guangchen on 27/02/2017. 33 | */ 34 | public class CatchPadInst extends FuncletPadInst { 35 | 36 | public CatchPadInst(String name, Type type) { 37 | super(name, type); 38 | super.opCode = OpCode.CATCHPAD; 39 | } 40 | 41 | @Override 42 | public R accept(InstructionVisitor visitor) throws E { 43 | return visitor.visit(this); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/main/java/cn/edu/thu/tsmart/core/cfa/llvm/CatchReturnInst.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Institute on Software System and Engineering, School of Software, Tsinghua University 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package cn.edu.thu.tsmart.core.cfa.llvm; 25 | 26 | import static cn.edu.thu.tsmart.core.cfa.llvm.InstructionProperties.OpCode; 27 | import static cn.edu.thu.tsmart.core.cfa.util.Casting.cast; 28 | 29 | import cn.edu.thu.tsmart.core.cfa.util.visitor.InstructionVisitor; 30 | import cn.edu.thu.tsmart.core.exceptions.CPAException; 31 | 32 | /** 33 | * @author guangchen on 27/02/2017. 34 | */ 35 | public class CatchReturnInst extends TerminatorInst { 36 | 37 | public CatchReturnInst(String name, Type type) { 38 | super(name, type); 39 | super.opCode = OpCode.CATCHRET; 40 | // this instruction should not be used when analysing C 41 | assert false : "Unhandled instruction: catchret"; 42 | } 43 | 44 | @Override 45 | public int getNumSuccessors() { 46 | return 1; 47 | } 48 | 49 | @Override 50 | public BasicBlock getSuccessor(int i) { 51 | assert i == 0 : "Successor # out of range for catchret!"; 52 | return cast(getOperand(1), BasicBlock.class); 53 | } 54 | 55 | @Override 56 | public R accept(InstructionVisitor visitor) throws E { 57 | return visitor.visit(this); 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/main/java/cn/edu/thu/tsmart/core/cfa/llvm/CatchSwitchInst.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Institute on Software System and Engineering, School of Software, Tsinghua University 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package cn.edu.thu.tsmart.core.cfa.llvm; 25 | 26 | import static cn.edu.thu.tsmart.core.cfa.llvm.InstructionProperties.OpCode; 27 | import static cn.edu.thu.tsmart.core.cfa.util.Casting.cast; 28 | 29 | import cn.edu.thu.tsmart.core.cfa.util.visitor.InstructionVisitor; 30 | import cn.edu.thu.tsmart.core.exceptions.CPAException; 31 | 32 | /** 33 | * @author guangchen on 27/02/2017. 34 | */ 35 | public class CatchSwitchInst extends TerminatorInst { 36 | 37 | public CatchSwitchInst(String name, Type type) { 38 | super(name, type); 39 | super.opCode = OpCode.CATCHSWITCH; 40 | // this instruction should not be used when analysing C 41 | assert false : "Unhandled instruction: catchswitch"; 42 | } 43 | 44 | @Override 45 | public int getNumSuccessors() { 46 | return getNumOperands() - 1; 47 | } 48 | 49 | @Override 50 | public BasicBlock getSuccessor(int i) { 51 | assert i >= 0 && i < getNumSuccessors() : "Successor # out of range for catchswitch!"; 52 | return cast(getOperand(i + 1), BasicBlock.class); 53 | } 54 | 55 | @Override 56 | public R accept(InstructionVisitor visitor) throws E { 57 | return visitor.visit(this); 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/main/java/cn/edu/thu/tsmart/core/cfa/llvm/CleanupPadInst.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Institute on Software System and Engineering, School of Software, Tsinghua University 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package cn.edu.thu.tsmart.core.cfa.llvm; 25 | 26 | import static cn.edu.thu.tsmart.core.cfa.llvm.InstructionProperties.*; 27 | 28 | import cn.edu.thu.tsmart.core.cfa.util.visitor.InstructionVisitor; 29 | import cn.edu.thu.tsmart.core.exceptions.CPAException; 30 | 31 | /** 32 | * @author guangchen on 27/02/2017. 33 | */ 34 | public class CleanupPadInst extends FuncletPadInst { 35 | 36 | public CleanupPadInst(String name, Type type) { 37 | super(name, type); 38 | super.opCode = OpCode.CLEANUPPAD; 39 | } 40 | 41 | @Override 42 | public R accept(InstructionVisitor visitor) throws E { 43 | return visitor.visit(this); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/main/java/cn/edu/thu/tsmart/core/cfa/llvm/CleanupReturnInst.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Institute on Software System and Engineering, School of Software, Tsinghua University 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package cn.edu.thu.tsmart.core.cfa.llvm; 25 | 26 | import static cn.edu.thu.tsmart.core.cfa.llvm.InstructionProperties.OpCode; 27 | 28 | import cn.edu.thu.tsmart.core.cfa.util.visitor.InstructionVisitor; 29 | import cn.edu.thu.tsmart.core.exceptions.CPAException; 30 | 31 | /** 32 | * @author guangchen on 27/02/2017. 33 | */ 34 | public class CleanupReturnInst extends TerminatorInst { 35 | 36 | public CleanupReturnInst(String name, Type type) { 37 | super(name, type); 38 | super.opCode = OpCode.CLEANUPRET; 39 | // this instruction should not be used when analysing C 40 | assert false : "Unhandled instruction: cleanupret"; 41 | } 42 | 43 | @Override 44 | public int getNumSuccessors() { 45 | // return hasUnwindDest() ? 1 : 0; 46 | return 0; 47 | } 48 | 49 | @Override 50 | public BasicBlock getSuccessor(int i) { 51 | // assert i == 0; return getUnwindDest(); 52 | return null; 53 | } 54 | 55 | @Override 56 | public R accept(InstructionVisitor visitor) throws E { 57 | return visitor.visit(this); 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/main/java/cn/edu/thu/tsmart/core/cfa/llvm/CompareConstantExpr.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Institute on Software System and Engineering, School of Software, Tsinghua University 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package cn.edu.thu.tsmart.core.cfa.llvm; 25 | 26 | import cn.edu.thu.sse.common.util.Pair; 27 | import cn.edu.thu.tsmart.core.cfa.llvm.InstructionProperties.OpCode; 28 | import cn.edu.thu.tsmart.core.cfa.llvm.InstructionProperties.Predicate; 29 | import java.util.HashMap; 30 | import javax.annotation.Nullable; 31 | import org.bytedeco.javacpp.annotation.Virtual; 32 | 33 | /** 34 | * Created by zhch on 2017/5/15. 35 | */ 36 | public class CompareConstantExpr extends ConstantExpr { 37 | 38 | private Predicate pred; 39 | private static HashMap, Pair>, CompareConstantExpr> instances = new HashMap<>(); 40 | 41 | private CompareConstantExpr(String name, Type type, OpCode opCode, Predicate pred) { 42 | super(name, type, opCode); 43 | this.pred = pred; 44 | } 45 | 46 | public static CompareConstantExpr getInstance(String name, Type type, OpCode opCode, 47 | Predicate pred, Constant op1, Constant op2) { 48 | Pair, Pair> key = Pair 49 | .of(Pair.of(opCode, pred), Pair.of(op1, op2)); 50 | if (instances.containsKey(key)) { 51 | return instances.get(key); 52 | } else { 53 | CompareConstantExpr instance = new CompareConstantExpr(name, type, opCode, pred); 54 | instances.put(key, instance); 55 | return instance; 56 | } 57 | } 58 | 59 | @Override 60 | public Predicate getPredicate() { 61 | return pred; 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /src/main/java/cn/edu/thu/tsmart/core/cfa/llvm/CompositeType.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Institute on Software System and Engineering, School of Software, Tsinghua University 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package cn.edu.thu.tsmart.core.cfa.llvm; 25 | 26 | /** 27 | * @author guangchen on 01/03/2017. 28 | */ 29 | public class CompositeType extends Type { 30 | protected CompositeType(Context context, TypeID id) { 31 | super(context, id); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/cn/edu/thu/tsmart/core/cfa/llvm/Constant.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Institute on Software System and Engineering, School of Software, Tsinghua University 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package cn.edu.thu.tsmart.core.cfa.llvm; 25 | 26 | import cn.edu.thu.tsmart.core.cfa.util.Casting; 27 | import sun.reflect.generics.reflectiveObjects.NotImplementedException; 28 | 29 | /** 30 | * @author guangchen on 26/02/2017. 31 | */ 32 | public abstract class Constant extends User { 33 | 34 | public Constant() { 35 | } 36 | 37 | protected Constant(String name, Type type) { 38 | super(name, type); 39 | } 40 | 41 | public Constant getAggregateElement(int index) { 42 | if (this instanceof ConstantAggregate) { 43 | ConstantAggregate that = Casting.cast(this, ConstantAggregate.class); 44 | return index < that.getNumOperands() ? (Constant) that.getOperand(index) : null; 45 | } 46 | if (this instanceof ConstantAggregateZero) { 47 | ConstantAggregateZero that = Casting.cast(this, ConstantAggregateZero.class); 48 | return index < that.getNumElements() ? that.getElementValue(index) : null; 49 | } 50 | if (this instanceof ConstantDataSequential) { 51 | ConstantDataSequential that = Casting.cast(this, ConstantDataSequential.class); 52 | return index < that.getNumElements() ? that.getElementAsConstant(index) : null; 53 | } 54 | assert false : "not implemented aggregate type"; 55 | return null; 56 | } 57 | 58 | public static Constant getNullValue(Type type) { 59 | switch (type.getTypeID()) { 60 | case IntegerTyID: 61 | return ConstantInt 62 | .get((IntegerType) type, new APInt(((IntegerType) type).getBitWidth(), "0")); 63 | case HalfTyID: 64 | case FloatTyID: 65 | case DoubleTyID: 66 | case X86_FP80TyID: 67 | case FP128TyID: 68 | case PPC_FP128TyID: 69 | throw new NotImplementedException(); 70 | case PointerTyID: 71 | return ConstantPointerNull.get((PointerType) type); 72 | case StructTyID: 73 | case ArrayTyID: 74 | case VectorTyID: 75 | return ConstantAggregateZero.get(type); 76 | case TokenTyID: 77 | throw new NotImplementedException(); 78 | default: 79 | assert false : "Cannot create a null constant of that type!"; 80 | } 81 | return null; 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /src/main/java/cn/edu/thu/tsmart/core/cfa/llvm/ConstantAggregate.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Institute on Software System and Engineering, School of Software, Tsinghua University 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package cn.edu.thu.tsmart.core.cfa.llvm; 25 | 26 | /** 27 | * @author guangchen on 07/07/2017. 28 | */ 29 | public class ConstantAggregate extends Constant { 30 | protected ConstantAggregate() {} 31 | protected ConstantAggregate(String name, Type type) { 32 | super(name, type); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/main/java/cn/edu/thu/tsmart/core/cfa/llvm/ConstantAggregateZero.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Institute on Software System and Engineering, School of Software, Tsinghua University 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package cn.edu.thu.tsmart.core.cfa.llvm; 25 | 26 | import cn.edu.thu.tsmart.core.cfa.util.Casting; 27 | import com.google.common.base.Preconditions; 28 | import sun.reflect.generics.reflectiveObjects.NotImplementedException; 29 | 30 | /** @author guangchen on 08/06/2017. */ 31 | public class ConstantAggregateZero extends Constant { 32 | public ConstantAggregateZero(Type type) { 33 | super("", type); 34 | } 35 | 36 | long getNumElements() { 37 | Type type = getType(); 38 | VectorType vectorType = Casting.dyncast(type, VectorType.class); 39 | if (vectorType != null) { 40 | throw new NotImplementedException(); 41 | } 42 | ArrayType arrayType = Casting.dyncast(type, ArrayType.class); 43 | if (arrayType != null) { 44 | return arrayType.getNumElements(); 45 | } 46 | return type.getStructNumElements(); 47 | } 48 | 49 | public Constant getElementValue(int index) { 50 | if (getType() instanceof SequentialType) { 51 | return getSequentialElement(); 52 | } 53 | return getStructElement(index); 54 | } 55 | 56 | public Constant getStructElement(int index) { 57 | return Constant.getNullValue(getType().getStructElementType(index)); 58 | } 59 | 60 | public Constant getSequentialElement() { 61 | return Constant.getNullValue(getType().getSequentialElementType()); 62 | } 63 | 64 | public static Constant get(Type type) { 65 | Preconditions.checkArgument(type.isStructTy() || type.isArrayTy() || type.isVectorTy(), "Cannot create an aggregate zero of non-aggregate type!"); 66 | ConstantAggregateZero entry = type.getContext().getCAZConstants(type); 67 | if (entry != null) { 68 | return entry; 69 | } 70 | entry = new ConstantAggregateZero(type); 71 | type.getContext().putCAZConstants(type, entry); 72 | return entry; 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /src/main/java/cn/edu/thu/tsmart/core/cfa/llvm/ConstantArray.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Institute on Software System and Engineering, School of Software, Tsinghua University 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package cn.edu.thu.tsmart.core.cfa.llvm; 25 | 26 | import cn.edu.thu.tsmart.core.cfa.util.Casting; 27 | 28 | /** 29 | * @author guangchen on 14/06/2017. 30 | */ 31 | public class ConstantArray extends ConstantAggregate { 32 | public ConstantArray(String name, Type type) { super(name, type); } 33 | 34 | public ArrayType getType () { 35 | return Casting.cast(super.getType(), ArrayType.class); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/cn/edu/thu/tsmart/core/cfa/llvm/ConstantData.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Institute on Software System and Engineering, School of Software, Tsinghua University 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package cn.edu.thu.tsmart.core.cfa.llvm; 25 | 26 | /** 27 | * Created by zhch on 2017/4/8. 28 | */ 29 | public class ConstantData extends Constant { 30 | 31 | public ConstantData(String name, Type type) { 32 | super(name, type); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/main/java/cn/edu/thu/tsmart/core/cfa/llvm/ConstantDataArray.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Institute on Software System and Engineering, School of Software, Tsinghua University 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package cn.edu.thu.tsmart.core.cfa.llvm; 25 | 26 | import cn.edu.thu.tsmart.core.cfa.util.Casting; 27 | import java.util.List; 28 | 29 | /** 30 | * @author guangchen on 12/06/2017. 31 | */ 32 | public class ConstantDataArray extends ConstantDataSequential { 33 | 34 | public ConstantDataArray(String name, Type type) { 35 | super(name, type); 36 | } 37 | 38 | public ArrayType getType() { 39 | return Casting.cast(super.getType(), ArrayType.class); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/main/java/cn/edu/thu/tsmart/core/cfa/llvm/ConstantDataVector.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Institute on Software System and Engineering, School of Software, Tsinghua University 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package cn.edu.thu.tsmart.core.cfa.llvm; 25 | 26 | import cn.edu.thu.tsmart.core.cfa.util.Casting; 27 | 28 | /** 29 | * Created by fzp on 18-3-4. 30 | */ 31 | public class ConstantDataVector extends ConstantDataSequential { 32 | 33 | public ConstantDataVector(String name, Type type) { 34 | super(name, type); 35 | } 36 | 37 | public VectorType getType() { 38 | return Casting.cast(super.getType(), VectorType.class); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/main/java/cn/edu/thu/tsmart/core/cfa/llvm/ConstantFP.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Institute on Software System and Engineering, School of Software, Tsinghua University 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package cn.edu.thu.tsmart.core.cfa.llvm; 25 | 26 | /** 27 | * @author guangchen on 10/08/2017. 28 | */ 29 | public class ConstantFP extends ConstantData { 30 | private final APFloat apFloat; 31 | 32 | public ConstantFP(String name, Type type, double value, boolean isHex) { 33 | super(name, type); 34 | apFloat = new APFloat(value, isHex); 35 | } 36 | 37 | public APFloat getValue() { 38 | return apFloat; 39 | } 40 | 41 | @Override 42 | public String toString() { 43 | return apFloat.toString(); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/main/java/cn/edu/thu/tsmart/core/cfa/llvm/ConstantInt.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Institute on Software System and Engineering, School of Software, Tsinghua University 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package cn.edu.thu.tsmart.core.cfa.llvm; 25 | 26 | import java.util.HashMap; 27 | 28 | /** 29 | * Created by zhch on 2017/4/8. 30 | */ 31 | public class ConstantInt extends ConstantData { 32 | 33 | private APInt val; 34 | private static HashMap instances = new HashMap<>(); 35 | 36 | private ConstantInt(String name, IntegerType type, APInt val) { 37 | super(name, type); 38 | this.val = val; 39 | } 40 | 41 | public static ConstantInt get(IntegerType type, APInt val) { 42 | if (instances.containsKey(val)) { 43 | return instances.get(val); 44 | } else { 45 | ConstantInt ci = new ConstantInt("", type, val); 46 | instances.put(val, ci); 47 | return ci; 48 | } 49 | } 50 | 51 | public final APInt getValue() { 52 | return val; 53 | } 54 | 55 | public int getBitWidth() { 56 | return val.getBitWidth(); 57 | } 58 | 59 | // NOTICE return type uses long to store u_int64 60 | public long getZExtValue() { 61 | return val.getZExtValue(); 62 | } 63 | 64 | public long getSExtValue() { 65 | return val.getSExtValue(); 66 | } 67 | 68 | public boolean isNegative() { 69 | return val.isNegative(); 70 | } 71 | 72 | public boolean isZero() { 73 | return val.isZero(); 74 | } 75 | 76 | public boolean isOne() { 77 | return val.countLeadingZeros() == val.getBitWidth() - 1; 78 | } 79 | 80 | public boolean isMinusOne() { 81 | return val.isAllOnesValue(); 82 | } 83 | 84 | public boolean isMaxValue(boolean isSigned) { 85 | if (isSigned) { 86 | return val.isMaxSignedValue(); 87 | } else { 88 | return val.isMaxValue(); 89 | } 90 | } 91 | 92 | public boolean isMinValue(boolean isSigned) { 93 | if (isSigned) { 94 | return val.isMinSignedValue(); 95 | } else { 96 | return val.isMinValue(); 97 | } 98 | } 99 | 100 | @Override 101 | public String getName() { 102 | return "CONSTANT_INT"; 103 | } 104 | 105 | @Override 106 | public String toString() { 107 | if(val.getBitWidth() == 1) 108 | return String.valueOf(val.getBoolValue()); 109 | else { 110 | return String.valueOf(val.getSExtValue()); 111 | //return String.valueOf(this.val.getLimitedValue(Long.MAX_VALUE)); 112 | } 113 | } 114 | } 115 | -------------------------------------------------------------------------------- /src/main/java/cn/edu/thu/tsmart/core/cfa/llvm/ConstantPointerNull.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Institute on Software System and Engineering, School of Software, Tsinghua University 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package cn.edu.thu.tsmart.core.cfa.llvm; 25 | 26 | /** 27 | * @author guangchen on 04/06/2017. 28 | */ 29 | public class ConstantPointerNull extends Constant { 30 | public ConstantPointerNull(Type type) { 31 | super("", type); 32 | } 33 | 34 | @Override 35 | public String toString() { 36 | return "null"; 37 | } 38 | 39 | public static Constant get(PointerType type) { 40 | return new ConstantPointerNull(type); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/main/java/cn/edu/thu/tsmart/core/cfa/llvm/ConstantStruct.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Institute on Software System and Engineering, School of Software, Tsinghua University 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package cn.edu.thu.tsmart.core.cfa.llvm; 25 | 26 | import java.util.List; 27 | 28 | /** 29 | * Created by wendy on 17-6-27. 30 | */ 31 | public class ConstantStruct extends ConstantAggregate { 32 | public List data; 33 | public ConstantStruct(String name, Type type, List data) { 34 | super(name, type); 35 | this.data = data; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/cn/edu/thu/tsmart/core/cfa/llvm/ExtractElementConstantExpr.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Institute on Software System and Engineering, School of Software, Tsinghua University 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package cn.edu.thu.tsmart.core.cfa.llvm; 25 | 26 | import cn.edu.thu.tsmart.core.cfa.llvm.InstructionProperties.OpCode; 27 | 28 | /** 29 | * Created by zhch on 2017/5/15. 30 | */ 31 | public class ExtractElementConstantExpr extends ConstantExpr { 32 | 33 | private ExtractElementConstantExpr(String name, Type type) { 34 | super(name, type, OpCode.EXTRACTELEMENT); 35 | } 36 | 37 | public static ExtractElementConstantExpr getInstance(String name, Type type, Constant op1, 38 | Constant op2) { 39 | // this expr should not be used when analysing C 40 | assert false : "Unhandled constant expression: extractelement"; 41 | return null; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/main/java/cn/edu/thu/tsmart/core/cfa/llvm/ExtractElementInst.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Institute on Software System and Engineering, School of Software, Tsinghua University 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package cn.edu.thu.tsmart.core.cfa.llvm; 25 | 26 | import static cn.edu.thu.tsmart.core.cfa.llvm.InstructionProperties.OpCode; 27 | 28 | import cn.edu.thu.tsmart.core.cfa.util.visitor.InstructionVisitor; 29 | import cn.edu.thu.tsmart.core.exceptions.CPAException; 30 | import java.util.Arrays; 31 | import java.util.List; 32 | import sun.reflect.generics.reflectiveObjects.NotImplementedException; 33 | 34 | /** 35 | * @author guangchen on 27/02/2017. 36 | */ 37 | public class ExtractElementInst extends Instruction { 38 | 39 | public ExtractElementInst(String name, Type type) { 40 | super(name, type); 41 | super.opCode = OpCode.EXTRACTELEMENT; 42 | // this instruction should not be used when analysing C 43 | assert false : "Unhandled instruction: extractelement"; 44 | } 45 | 46 | @Override 47 | public R accept(InstructionVisitor visitor) throws E { 48 | return visitor.visit(this); 49 | } 50 | 51 | public static Instruction create(Value operand, Value operand1) { 52 | List list = Arrays.asList(operand, operand1); 53 | ExtractElementInst instruction = new ExtractElementInst("", null); 54 | instruction.setOperands(list); 55 | return instruction; 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/main/java/cn/edu/thu/tsmart/core/cfa/llvm/ExtractValueConstantExpr.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Institute on Software System and Engineering, School of Software, Tsinghua University 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package cn.edu.thu.tsmart.core.cfa.llvm; 25 | 26 | import cn.edu.thu.sse.common.util.Pair; 27 | import cn.edu.thu.tsmart.core.cfa.llvm.InstructionProperties.OpCode; 28 | import com.google.common.collect.ImmutableList; 29 | import java.util.HashMap; 30 | 31 | /** 32 | * Created by zhch on 2017/5/15. 33 | */ 34 | public class ExtractValueConstantExpr extends ConstantExpr { 35 | 36 | private ImmutableList idxList; 37 | private static HashMap, ExtractValueConstantExpr> instances; 38 | 39 | private ExtractValueConstantExpr(String name, Type type, ImmutableList idxList) { 40 | super(name, type, OpCode.EXTRACTVALUE); 41 | this.idxList = idxList; 42 | } 43 | 44 | public static ExtractValueConstantExpr getInstance(String name, Type type, 45 | ImmutableList idxList, Constant op) { 46 | Pair key = Pair.of(op, new IndexList(idxList)); 47 | if (instances.containsKey(key)) { 48 | return instances.get(key); 49 | } else { 50 | ExtractValueConstantExpr instance = new ExtractValueConstantExpr(name, type, idxList); 51 | instances.put(key, instance); 52 | return instance; 53 | } 54 | } 55 | 56 | @Override 57 | public ImmutableList getIndices() { 58 | return idxList; 59 | } 60 | 61 | private static class IndexList { 62 | 63 | private final ImmutableList idxList; 64 | 65 | public IndexList(ImmutableList idxList) { 66 | this.idxList = idxList; 67 | } 68 | 69 | @Override 70 | public boolean equals(Object obj) { 71 | if (obj == null || !obj.getClass().isInstance(IndexList.class)) { 72 | return false; 73 | } 74 | IndexList other = (IndexList) obj; 75 | if (this.idxList.size() != other.idxList.size()) { 76 | return false; 77 | } 78 | return this.idxList.containsAll(other.idxList); 79 | } 80 | 81 | @Override 82 | public int hashCode() { 83 | return idxList.hashCode(); 84 | } 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /src/main/java/cn/edu/thu/tsmart/core/cfa/llvm/ExtractValueInst.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Institute on Software System and Engineering, School of Software, Tsinghua University 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package cn.edu.thu.tsmart.core.cfa.llvm; 25 | 26 | import static cn.edu.thu.tsmart.core.cfa.llvm.InstructionProperties.OpCode; 27 | 28 | import cn.edu.thu.tsmart.core.cfa.util.Casting; 29 | import cn.edu.thu.tsmart.core.cfa.util.visitor.InstructionVisitor; 30 | import cn.edu.thu.tsmart.core.exceptions.CPAException; 31 | import com.google.common.collect.ImmutableList; 32 | import java.util.Arrays; 33 | import java.util.List; 34 | 35 | /** 36 | * @author guangchen on 27/02/2017. 37 | */ 38 | public class ExtractValueInst extends UnaryInstruction { 39 | 40 | // TODO initialize in Converter 41 | private ImmutableList indices; 42 | 43 | public ExtractValueInst(String name, Type type) { 44 | super(name, type); 45 | super.opCode = OpCode.EXTRACTVALUE; 46 | } 47 | 48 | // only for Converter 49 | public void setIndices(ImmutableList indices) { 50 | this.indices = indices; 51 | } 52 | 53 | public Value getAggregateOperand() { 54 | return getOperand(0); 55 | } 56 | 57 | public boolean hasIndices() { 58 | return true; 59 | } 60 | 61 | public int getNumIndices() { 62 | return indices.size(); 63 | } 64 | 65 | public ImmutableList getIndices() { 66 | return indices; 67 | } 68 | 69 | @Override 70 | public R accept(InstructionVisitor visitor) throws E { 71 | return visitor.visit(this); 72 | } 73 | 74 | public static Instruction create(Value operand, ImmutableList indices) { 75 | List list = Arrays.asList(operand); 76 | ExtractValueInst instruction = new ExtractValueInst("", null); 77 | instruction.setOperands(list); 78 | instruction.setIndices(indices); 79 | return instruction; 80 | } 81 | 82 | @Override 83 | public String toString() { 84 | CallInst callInst = Casting.dyncast(getOperand(0), CallInst.class); 85 | if (callInst == null) { 86 | return ""; 87 | } 88 | String res = "%" + getName().toString() + " = " + getOpcode().toString() + " " + callInst.getType().toString(); 89 | res += " %" + callInst.getName().toString() + ", "; 90 | for(int i = 0; i <= callInst.getNumArgOperands(); i++) { 91 | if(callInst.getArgOperandUse(i).getUser() == this) { 92 | res += callInst.getNumArgOperands() - i; 93 | break; 94 | } 95 | } 96 | return res; 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /src/main/java/cn/edu/thu/tsmart/core/cfa/llvm/FCmpInst.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Institute on Software System and Engineering, School of Software, Tsinghua University 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package cn.edu.thu.tsmart.core.cfa.llvm; 25 | 26 | import static cn.edu.thu.tsmart.core.cfa.llvm.InstructionProperties.OpCode; 27 | import static cn.edu.thu.tsmart.core.cfa.llvm.InstructionProperties.Predicate; 28 | 29 | import cn.edu.thu.tsmart.core.cfa.util.visitor.InstructionVisitor; 30 | import cn.edu.thu.tsmart.core.exceptions.CPAException; 31 | 32 | /** 33 | * @author guangchen on 27/02/2017. 34 | */ 35 | public class FCmpInst extends CmpInst { 36 | 37 | public FCmpInst(String name, Type type, Predicate predicate) { 38 | super(name, type); 39 | super.opCode = OpCode.FCMP; 40 | super.predicate = predicate; 41 | } 42 | 43 | @Override 44 | public boolean isCommutative() { 45 | return isEquality() || getPredicate() == Predicate.FCMP_FALSE 46 | || getPredicate() == Predicate.FCMP_TRUE || getPredicate() == Predicate.FCMP_ORD 47 | || getPredicate() == Predicate.FCMP_UNO; 48 | } 49 | 50 | @Override 51 | public boolean isEquality() { 52 | return isEquality(getPredicate()); 53 | } 54 | 55 | public static boolean isEquality(Predicate predicate) { 56 | return predicate == Predicate.FCMP_OEQ || predicate == Predicate.FCMP_ONE 57 | || predicate == Predicate.FCMP_UEQ || predicate == Predicate.FCMP_UNE; 58 | } 59 | 60 | public boolean isRelational() { 61 | return !isEquality(); 62 | } 63 | 64 | @Override 65 | public R accept(InstructionVisitor visitor) throws E { 66 | return visitor.visit(this); 67 | } 68 | 69 | @Override 70 | public String toString() { 71 | String res = "%" + getName() + " = " + getOpcode().toString(); 72 | res += " " + getPredicate().toString(); 73 | res += " " + getOperand(0).getType().toString() + " "; 74 | for(int i = 0; i < getNumOperands(); i++) { 75 | Value operand = getOperand(i); 76 | if (operand instanceof Constant) { 77 | res += operand.toString(); 78 | } else { 79 | res += "%" + operand.getName(); 80 | } 81 | if(i != getNumOperands() - 1) 82 | res += ", "; 83 | } 84 | return res; 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /src/main/java/cn/edu/thu/tsmart/core/cfa/llvm/FPExtInst.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Institute on Software System and Engineering, School of Software, Tsinghua University 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package cn.edu.thu.tsmart.core.cfa.llvm; 25 | 26 | import static cn.edu.thu.tsmart.core.cfa.llvm.InstructionProperties.*; 27 | 28 | /** 29 | * @author guangchen on 27/02/2017. 30 | */ 31 | public class FPExtInst extends CastInst { 32 | 33 | public FPExtInst(String name, Type type) { 34 | super(name, type); 35 | super.opCode = OpCode.FPEXT; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/cn/edu/thu/tsmart/core/cfa/llvm/FPToSIInst.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Institute on Software System and Engineering, School of Software, Tsinghua University 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package cn.edu.thu.tsmart.core.cfa.llvm; 25 | 26 | import static cn.edu.thu.tsmart.core.cfa.llvm.InstructionProperties.*; 27 | 28 | /** 29 | * @author guangchen on 27/02/2017. 30 | */ 31 | public class FPToSIInst extends CastInst { 32 | 33 | public FPToSIInst(String name, Type type) { 34 | super(name, type); 35 | super.opCode = OpCode.FPTOSI; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/cn/edu/thu/tsmart/core/cfa/llvm/FPToUIInst.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Institute on Software System and Engineering, School of Software, Tsinghua University 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package cn.edu.thu.tsmart.core.cfa.llvm; 25 | 26 | import static cn.edu.thu.tsmart.core.cfa.llvm.InstructionProperties.*; 27 | 28 | /** 29 | * @author guangchen on 27/02/2017. 30 | */ 31 | public class FPToUIInst extends CastInst { 32 | 33 | public FPToUIInst(String name, Type type) { 34 | super(name, type); 35 | super.opCode = OpCode.FPTOUI; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/cn/edu/thu/tsmart/core/cfa/llvm/FPTruncInst.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Institute on Software System and Engineering, School of Software, Tsinghua University 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package cn.edu.thu.tsmart.core.cfa.llvm; 25 | 26 | import static cn.edu.thu.tsmart.core.cfa.llvm.InstructionProperties.*; 27 | 28 | /** 29 | * @author guangchen on 27/02/2017. 30 | */ 31 | public class FPTruncInst extends CastInst { 32 | 33 | public FPTruncInst(String name, Type type) { 34 | super(name, type); 35 | super.opCode = OpCode.FPTRUNC; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/cn/edu/thu/tsmart/core/cfa/llvm/FenceInst.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Institute on Software System and Engineering, School of Software, Tsinghua University 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package cn.edu.thu.tsmart.core.cfa.llvm; 25 | 26 | import static cn.edu.thu.tsmart.core.cfa.llvm.InstructionProperties.AtomicOrdering; 27 | import static cn.edu.thu.tsmart.core.cfa.llvm.InstructionProperties.OpCode; 28 | import static cn.edu.thu.tsmart.core.cfa.llvm.InstructionProperties.SynchronizationScope; 29 | 30 | import cn.edu.thu.tsmart.core.cfa.util.visitor.InstructionVisitor; 31 | import cn.edu.thu.tsmart.core.exceptions.CPAException; 32 | 33 | /** 34 | * @author guangchen on 27/02/2017. 35 | */ 36 | public class FenceInst extends Instruction { 37 | 38 | // TODO initialize in Converter 39 | private AtomicOrdering ordering = null; 40 | private SynchronizationScope synchScope = null; 41 | 42 | public FenceInst(String name, Type type) { 43 | super(name, type); 44 | super.opCode = OpCode.FENCE; 45 | } 46 | 47 | // only for Converter 48 | public void setOrdering(AtomicOrdering ordering) { 49 | this.ordering = ordering; 50 | } 51 | 52 | // only for Converter 53 | public void setSynchScope(SynchronizationScope synchScope) { 54 | this.synchScope = synchScope; 55 | } 56 | 57 | public AtomicOrdering getOrdering() { 58 | return ordering; 59 | } 60 | 61 | public SynchronizationScope getSynchScope() { 62 | return synchScope; 63 | } 64 | 65 | @Override 66 | public R accept(InstructionVisitor visitor) throws E { 67 | return visitor.visit(this); 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /src/main/java/cn/edu/thu/tsmart/core/cfa/llvm/FuncletPadInst.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Institute on Software System and Engineering, School of Software, Tsinghua University 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package cn.edu.thu.tsmart.core.cfa.llvm; 25 | 26 | import cn.edu.thu.tsmart.core.cfa.util.visitor.InstructionVisitor; 27 | import cn.edu.thu.tsmart.core.exceptions.CPAException; 28 | 29 | /** 30 | * @author guangchen on 27/02/2017. 31 | */ 32 | public abstract class FuncletPadInst extends Instruction { 33 | 34 | protected FuncletPadInst(String name, Type type) { 35 | super(name, type); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/cn/edu/thu/tsmart/core/cfa/llvm/FunctionType.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Institute on Software System and Engineering, School of Software, Tsinghua University 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package cn.edu.thu.tsmart.core.cfa.llvm; 25 | 26 | import sun.reflect.generics.reflectiveObjects.NotImplementedException; 27 | 28 | /** @author guangchen on 01/03/2017. */ 29 | public class FunctionType extends Type { 30 | protected FunctionType(Type result, Type[] params, boolean isVarArg) { 31 | super(result.getContext(), TypeID.FunctionTyID); 32 | this.returnType = result; 33 | this.params = params; 34 | this.isVarArg = isVarArg; 35 | } 36 | 37 | private boolean isVarArg; 38 | private Type returnType; 39 | private Type[] params; 40 | 41 | public boolean isVarArg() { 42 | return isVarArg; 43 | } 44 | 45 | public Type getReturnType() { 46 | return returnType; 47 | } 48 | 49 | public Type[] getParams() { 50 | return params; 51 | } 52 | 53 | public int getNumParams() { 54 | return params.length; 55 | } 56 | 57 | @Override 58 | public boolean isFunctionVarArg() { 59 | return isVarArg(); 60 | } 61 | 62 | @Override 63 | public Type[] getFunctionParamType() { 64 | return getParams(); 65 | } 66 | 67 | @Override 68 | public int getFunctionNumParams() { 69 | return getNumParams(); 70 | } 71 | 72 | public static FunctionType get(Type result, Type[] params, boolean isVarArg) { 73 | return new FunctionType(result, params, isVarArg); 74 | } 75 | 76 | public static FunctionType get(Type result, boolean isVarArg) { 77 | return get(result, new Type[] {}, isVarArg); 78 | } 79 | 80 | public static boolean isValidReturnType(Type retTy) { 81 | throw new NotImplementedException(); 82 | } 83 | 84 | public static boolean isValidArgumentType(Type argTy) { 85 | throw new NotImplementedException(); 86 | } 87 | 88 | @Override 89 | public String toString() { 90 | String res = returnType + " ("; 91 | for(int i = 0; i < params.length; i++) { 92 | res += params[i].toString(); 93 | if(i < params.length - 1 || isVarArg()) 94 | res += ", "; 95 | } 96 | if(isVarArg()) 97 | res += "...)"; 98 | else 99 | res += ")"; 100 | return res; 101 | } 102 | } 103 | -------------------------------------------------------------------------------- /src/main/java/cn/edu/thu/tsmart/core/cfa/llvm/GetElementPtrConstantExpr.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Institute on Software System and Engineering, School of Software, Tsinghua University 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package cn.edu.thu.tsmart.core.cfa.llvm; 25 | 26 | import cn.edu.thu.sse.common.util.Pair; 27 | import cn.edu.thu.tsmart.core.cfa.llvm.InstructionProperties.OpCode; 28 | import com.google.common.collect.ImmutableList; 29 | import java.util.HashMap; 30 | 31 | /** 32 | * Created by zhch on 2017/5/15. 33 | */ 34 | public class GetElementPtrConstantExpr extends ConstantExpr { 35 | 36 | private ImmutableList idxList; 37 | private boolean isInBounds = false; 38 | private static HashMap, Boolean>, GetElementPtrConstantExpr> instances = new HashMap<>(); 39 | 40 | private GetElementPtrConstantExpr(String name, Type type, ImmutableList idxList, boolean isInBounds) { 41 | super(name, type, OpCode.GETELEMENTPTR); 42 | this.idxList = idxList; 43 | this.isInBounds = isInBounds; 44 | } 45 | 46 | public static GetElementPtrConstantExpr getInstance(String name, Type type, 47 | ImmutableList idxList, Constant op, boolean isInBounds) { 48 | Pair, Boolean> key = Pair.of(Pair.of(op, new IndexList(idxList)), isInBounds); 49 | if (instances.containsKey(key)) { 50 | return instances.get(key); 51 | } else { 52 | GetElementPtrConstantExpr instance = new GetElementPtrConstantExpr(name, type, idxList, isInBounds); 53 | instances.put(key, instance); 54 | return instance; 55 | } 56 | } 57 | 58 | public boolean isInBounds() { 59 | return isInBounds; 60 | } 61 | 62 | @Override 63 | public String toString() { 64 | String res = ""; 65 | res += opCode.toString(); 66 | res += " inbounds ("; 67 | res += getOperand(0).getType().toString(); 68 | res = res.substring(0, res.length()-1); 69 | for(int i = 0; i < getNumOperands(); i++) { 70 | res += ", "; 71 | res += getOperand(i).getType().toString(); 72 | res += " "; 73 | res += getOperand(i).toString(); 74 | } 75 | res += ")"; 76 | return res; 77 | } 78 | 79 | private static class IndexList { 80 | 81 | private final ImmutableList idxList; 82 | 83 | public IndexList(ImmutableList idxList) { 84 | this.idxList = idxList; 85 | } 86 | 87 | @Override 88 | public boolean equals(Object obj) { 89 | if (obj == null || !obj.getClass().isInstance(IndexList.class)) { 90 | return false; 91 | } 92 | IndexList other = (IndexList) obj; 93 | if (this.idxList.size() != other.idxList.size()) { 94 | return false; 95 | } 96 | return this.idxList.containsAll(other.idxList); 97 | } 98 | 99 | @Override 100 | public int hashCode() { 101 | return idxList.hashCode(); 102 | } 103 | } 104 | } 105 | -------------------------------------------------------------------------------- /src/main/java/cn/edu/thu/tsmart/core/cfa/llvm/GlobalObject.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Institute on Software System and Engineering, School of Software, Tsinghua University 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package cn.edu.thu.tsmart.core.cfa.llvm; 25 | 26 | /** 27 | * @author guangchen on 26/02/2017. 28 | */ 29 | public class GlobalObject extends GlobalValue { 30 | public GlobalObject() { 31 | } 32 | 33 | public GlobalObject(String name, Type type) { 34 | super(name, type); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/main/java/cn/edu/thu/tsmart/core/cfa/llvm/GlobalValue.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Institute on Software System and Engineering, School of Software, Tsinghua University 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package cn.edu.thu.tsmart.core.cfa.llvm; 25 | 26 | /** 27 | * @author guangchen on 26/02/2017. 28 | */ 29 | public class GlobalValue extends Constant { 30 | public GlobalValue() { 31 | } 32 | 33 | public GlobalValue(String name, Type type) { 34 | super(name, type); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/main/java/cn/edu/thu/tsmart/core/cfa/llvm/GlobalVariable.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Institute on Software System and Engineering, School of Software, Tsinghua University 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package cn.edu.thu.tsmart.core.cfa.llvm; 25 | 26 | /** 27 | * @author guangchen on 27/05/2017. 28 | */ 29 | public class GlobalVariable extends GlobalObject { 30 | private Constant initializer; 31 | private Metadata metadata; 32 | 33 | public GlobalVariable(String name, Type type, Constant init, Metadata pMetadata) { 34 | super(name, type); 35 | this.initializer = init; 36 | this.metadata = pMetadata; 37 | } 38 | 39 | public Constant getInitializer() { 40 | return initializer; 41 | } 42 | 43 | public Metadata getMetadata() { return metadata; } 44 | 45 | @Override 46 | public String toString() { 47 | String res = "@"; 48 | res += getName().toString(); 49 | return res; 50 | } 51 | 52 | @Override 53 | public int hashCode() { 54 | int result = toString().hashCode(); 55 | return result; 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/main/java/cn/edu/thu/tsmart/core/cfa/llvm/IInstruction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Institute on Software System and Engineering, School of Software, Tsinghua University 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package cn.edu.thu.tsmart.core.cfa.llvm; 25 | 26 | import cn.edu.thu.tsmart.core.cfa.util.visitor.InstructionVisitor; 27 | import cn.edu.thu.tsmart.core.exceptions.CPAException; 28 | 29 | /** 30 | * Created by tomgu on 4/21/17. 31 | */ 32 | public interface IInstruction { 33 | 34 | public R accept(InstructionVisitor visitor) 35 | throws E; 36 | } 37 | -------------------------------------------------------------------------------- /src/main/java/cn/edu/thu/tsmart/core/cfa/llvm/IRReader.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Institute on Software System and Engineering, School of Software, Tsinghua University 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | package cn.edu.thu.tsmart.core.cfa.llvm; 26 | 27 | import java.io.BufferedReader; 28 | import java.io.File; 29 | import java.io.FileReader; 30 | import java.io.IOException; 31 | import org.bytedeco.javacpp.BytePointer; 32 | 33 | import static org.bytedeco.javacpp.LLVM.*; 34 | 35 | /** @author guangchen on 24/02/2017. */ 36 | public class IRReader { 37 | public LlvmModule parseIRFile(String fileName, Context context) { 38 | LLVMMemoryBufferRef bufferRef = new LLVMMemoryBufferRef(); 39 | BytePointer outMessage = new BytePointer(new BytePointer()); 40 | LLVMCreateMemoryBufferWithContentsOfFile(new BytePointer(fileName), bufferRef, outMessage); 41 | LLVMModuleRef moduleRef = new LLVMModuleRef(); 42 | LLVMParseIRInContext(context.getContextRef(), bufferRef, moduleRef, outMessage); 43 | 44 | Converter converter = new Converter(context); 45 | return converter.convert(moduleRef, fileName); 46 | } 47 | 48 | public LlvmModule parseBytecodeFile(String fileName, Context context) { 49 | LLVMMemoryBufferRef bufferRef = new LLVMMemoryBufferRef(); 50 | BytePointer outMessage = new BytePointer(new BytePointer()); 51 | LLVMCreateMemoryBufferWithContentsOfFile(new BytePointer(fileName), bufferRef, outMessage); 52 | LLVMModuleRef moduleRef = new LLVMModuleRef(); 53 | LLVMParseBitcodeInContext2(context.getContextRef(), bufferRef, moduleRef); 54 | Converter converter = new Converter(context); 55 | return converter.convert(moduleRef, fileName); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/main/java/cn/edu/thu/tsmart/core/cfa/llvm/IndirectBrInst.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Institute on Software System and Engineering, School of Software, Tsinghua University 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package cn.edu.thu.tsmart.core.cfa.llvm; 25 | 26 | import static cn.edu.thu.tsmart.core.cfa.llvm.InstructionProperties.OpCode; 27 | import static cn.edu.thu.tsmart.core.cfa.util.Casting.cast; 28 | 29 | import cn.edu.thu.tsmart.core.cfa.util.visitor.InstructionVisitor; 30 | import cn.edu.thu.tsmart.core.exceptions.CPAException; 31 | 32 | /** 33 | * @author guangchen on 27/02/2017. 34 | */ 35 | public class IndirectBrInst extends TerminatorInst { 36 | 37 | public IndirectBrInst(String name, Type type) { 38 | super(name, type); 39 | super.opCode = OpCode.INDIRECTBR; 40 | } 41 | 42 | @Override 43 | public int getNumSuccessors() { 44 | return getNumOperands() - 1; 45 | } 46 | 47 | @Override 48 | public BasicBlock getSuccessor(int i) { 49 | return cast(getOperand(i + 1), BasicBlock.class); 50 | } 51 | 52 | public Value getAddress() { 53 | return getOperand(0); 54 | } 55 | 56 | public int getNumDestinations() { 57 | return getNumOperands() - 1; 58 | } 59 | 60 | public BasicBlock getDestination(int i) { 61 | return getSuccessor(i); 62 | } 63 | 64 | @Override 65 | public R accept(InstructionVisitor visitor) throws E { 66 | return visitor.visit(this); 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /src/main/java/cn/edu/thu/tsmart/core/cfa/llvm/InsertElementConstantExpr.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Institute on Software System and Engineering, School of Software, Tsinghua University 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package cn.edu.thu.tsmart.core.cfa.llvm; 25 | 26 | import cn.edu.thu.tsmart.core.cfa.llvm.InstructionProperties.OpCode; 27 | 28 | /** 29 | * Created by zhch on 2017/5/15. 30 | */ 31 | public class InsertElementConstantExpr extends ConstantExpr { 32 | 33 | private InsertElementConstantExpr(String name, Type type) { 34 | super(name, type, OpCode.INSERTELEMENT); 35 | } 36 | 37 | public static InsertElementConstantExpr getInstance(String name, Type type, Constant op1, 38 | Constant op2, Constant op3) { 39 | // this expr should not be used when analysing C 40 | assert false : "Unhandled constant expression: insertelement"; 41 | return null; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/main/java/cn/edu/thu/tsmart/core/cfa/llvm/InsertElementInst.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Institute on Software System and Engineering, School of Software, Tsinghua University 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package cn.edu.thu.tsmart.core.cfa.llvm; 25 | 26 | import static cn.edu.thu.tsmart.core.cfa.llvm.InstructionProperties.OpCode; 27 | 28 | import cn.edu.thu.tsmart.core.cfa.util.visitor.InstructionVisitor; 29 | import cn.edu.thu.tsmart.core.exceptions.CPAException; 30 | import java.util.Arrays; 31 | import java.util.List; 32 | import sun.reflect.generics.reflectiveObjects.NotImplementedException; 33 | 34 | /** 35 | * @author guangchen on 27/02/2017. 36 | */ 37 | public class InsertElementInst extends Instruction { 38 | 39 | public InsertElementInst(String name, Type type) { 40 | super(name, type); 41 | super.opCode = OpCode.INSERTELEMENT; 42 | // this instruction should not be used when analysing C 43 | assert false : "Unhandled instruction: invoke"; 44 | } 45 | 46 | @Override 47 | public R accept(InstructionVisitor visitor) throws E { 48 | return visitor.visit(this); 49 | } 50 | 51 | public static Instruction create(Value operand, Value operand1, Value operand2) { 52 | List list = Arrays.asList(operand, operand1, operand2); 53 | InsertElementInst instruction = new InsertElementInst("", null); 54 | instruction.setOperands(list); 55 | return instruction; 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/main/java/cn/edu/thu/tsmart/core/cfa/llvm/InsertValueConstantExpr.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Institute on Software System and Engineering, School of Software, Tsinghua University 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package cn.edu.thu.tsmart.core.cfa.llvm; 25 | 26 | import cn.edu.thu.sse.common.util.Pair; 27 | import cn.edu.thu.tsmart.core.cfa.llvm.InstructionProperties.OpCode; 28 | import com.google.common.collect.ImmutableList; 29 | import java.util.HashMap; 30 | 31 | /** 32 | * Created by zhch on 2017/5/15. 33 | */ 34 | public class InsertValueConstantExpr extends ConstantExpr { 35 | 36 | private ImmutableList idxList; 37 | private static HashMap, IndexList>, InsertValueConstantExpr> instances; 38 | 39 | private InsertValueConstantExpr(String name, Type type, ImmutableList idxList) { 40 | super(name, type, OpCode.INSERTVALUE); 41 | this.idxList = idxList; 42 | } 43 | 44 | public static InsertValueConstantExpr getInstance(String name, Type type, 45 | ImmutableList idxList, Constant opAgg, Constant opVal) { 46 | Pair, IndexList> key = Pair 47 | .of(Pair.of(opAgg, opVal), new IndexList(idxList)); 48 | if (instances.containsKey(key)) { 49 | return instances.get(key); 50 | } else { 51 | InsertValueConstantExpr instance = new InsertValueConstantExpr(name, type, idxList); 52 | instances.put(key, instance); 53 | return instance; 54 | } 55 | } 56 | 57 | @Override 58 | public ImmutableList getIndices() { 59 | return idxList; 60 | } 61 | 62 | private static class IndexList { 63 | 64 | private final ImmutableList idxList; 65 | 66 | public IndexList(ImmutableList idxList) { 67 | this.idxList = idxList; 68 | } 69 | 70 | @Override 71 | public boolean equals(Object obj) { 72 | if (obj == null || !obj.getClass().isInstance(IndexList.class)) { 73 | return false; 74 | } 75 | IndexList other = (IndexList) obj; 76 | if (this.idxList.size() != other.idxList.size()) { 77 | return false; 78 | } 79 | return this.idxList.containsAll(other.idxList); 80 | } 81 | 82 | @Override 83 | public int hashCode() { 84 | return idxList.hashCode(); 85 | } 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /src/main/java/cn/edu/thu/tsmart/core/cfa/llvm/InsertValueInst.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Institute on Software System and Engineering, School of Software, Tsinghua University 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package cn.edu.thu.tsmart.core.cfa.llvm; 25 | 26 | import static cn.edu.thu.tsmart.core.cfa.llvm.InstructionProperties.OpCode; 27 | 28 | import cn.edu.thu.tsmart.core.cfa.util.visitor.InstructionVisitor; 29 | import cn.edu.thu.tsmart.core.exceptions.CPAException; 30 | import com.google.common.collect.ImmutableList; 31 | import java.util.Arrays; 32 | import java.util.List; 33 | 34 | /** 35 | * @author guangchen on 27/02/2017. 36 | */ 37 | public class InsertValueInst extends Instruction { 38 | 39 | // TODO initialize in Converter 40 | private ImmutableList indices; 41 | 42 | public InsertValueInst(String name, Type type) { 43 | super(name, type); 44 | super.opCode = OpCode.INSERTVALUE; 45 | } 46 | 47 | // only for Converter 48 | public void setIndices(ImmutableList indices) { 49 | this.indices = indices; 50 | } 51 | 52 | public Value getAggregateOperand() { 53 | return getOperand(0); 54 | } 55 | 56 | public Value getInsertedValueOperand() { 57 | return getOperand(1); 58 | } 59 | 60 | public boolean hasIndices() { 61 | return true; 62 | } 63 | 64 | public int getNumIndices() { 65 | return indices.size(); 66 | } 67 | 68 | public ImmutableList getIndices() { 69 | return indices; 70 | } 71 | 72 | @Override 73 | public R accept(InstructionVisitor visitor) throws E { 74 | return visitor.visit(this); 75 | } 76 | 77 | public static Instruction create(Value operand1, Value operand2, ImmutableList indices) { 78 | List list = Arrays.asList(operand1, operand2); 79 | ExtractValueInst instruction = new ExtractValueInst("", null); 80 | instruction.setOperands(list); 81 | instruction.setIndices(indices); 82 | return instruction; 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /src/main/java/cn/edu/thu/tsmart/core/cfa/llvm/IntToPtrInst.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Institute on Software System and Engineering, School of Software, Tsinghua University 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package cn.edu.thu.tsmart.core.cfa.llvm; 25 | 26 | import static cn.edu.thu.tsmart.core.cfa.llvm.InstructionProperties.*; 27 | 28 | /** 29 | * @author guangchen on 27/02/2017. 30 | */ 31 | public class IntToPtrInst extends CastInst { 32 | 33 | public IntToPtrInst(String name, Type type) { 34 | super(name, type); 35 | super.opCode = OpCode.INTTOPTR; 36 | } 37 | 38 | // NOTICE return type uses int to store unsigned 39 | public int getPointerAddressSpace() { 40 | return getType().getPointerAddressSpace(); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/main/java/cn/edu/thu/tsmart/core/cfa/llvm/IntegerType.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Institute on Software System and Engineering, School of Software, Tsinghua University 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package cn.edu.thu.tsmart.core.cfa.llvm; 25 | 26 | // import cn.edu.thu.tsmart.core.util.math.MathExtras; 27 | 28 | import java.util.Objects; 29 | 30 | /** 31 | * @author guangchen on 01/03/2017. 32 | */ 33 | public class IntegerType extends Type { 34 | 35 | private final int bitWidth; 36 | 37 | public IntegerType(Context context, int n) { 38 | super(context, TypeID.IntegerTyID); 39 | assert n >= 1 : "n >= 1"; 40 | this.bitWidth = n; 41 | } 42 | 43 | public int getBitWidth() { 44 | return this.bitWidth; 45 | } 46 | 47 | public long getBitMask() { 48 | return ~0L >> (64 - getBitWidth()); 49 | } 50 | 51 | public long getSignBit() { 52 | return 1L << (getBitWidth() - 1); 53 | } 54 | 55 | public boolean isPowerOf2ByteWidth() { 56 | int bitWidth = getBitWidth(); 57 | // return (bitWidth > 7) && MathExtras.isPowerOf2_32(bitWidth); 58 | return false; 59 | } 60 | 61 | @Override 62 | public String toString() { 63 | return "i" + bitWidth; 64 | } 65 | 66 | @Override 67 | public boolean equals(Object o) { 68 | if (this == o) { 69 | return true; 70 | } 71 | if (o == null || getClass() != o.getClass()) { 72 | return false; 73 | } 74 | IntegerType that = (IntegerType) o; 75 | return bitWidth == that.bitWidth; 76 | } 77 | 78 | @Override 79 | public int hashCode() { 80 | return bitWidth; 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /src/main/java/cn/edu/thu/tsmart/core/cfa/llvm/InvokeInst.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Institute on Software System and Engineering, School of Software, Tsinghua University 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package cn.edu.thu.tsmart.core.cfa.llvm; 25 | 26 | import static cn.edu.thu.tsmart.core.cfa.llvm.InstructionProperties.OpCode; 27 | 28 | import cn.edu.thu.tsmart.core.cfa.util.visitor.InstructionVisitor; 29 | import cn.edu.thu.tsmart.core.exceptions.CPAException; 30 | 31 | /** 32 | * @author guangchen on 27/02/2017. 33 | */ 34 | public class InvokeInst extends TerminatorInst { 35 | 36 | public InvokeInst(String name, Type type) { 37 | super(name, type); 38 | super.opCode = OpCode.INVOKE; 39 | // this instruction should not be used when analysing C 40 | assert false : "Unhandled instruction: invoke"; 41 | } 42 | 43 | @Override 44 | public int getNumSuccessors() { 45 | return 2; 46 | } 47 | 48 | @Override 49 | public BasicBlock getSuccessor(int i) { 50 | assert i >= 0 && i < 2 : "Successor # out of range for invoke!"; 51 | return null; 52 | } 53 | 54 | @Override 55 | public R accept(InstructionVisitor visitor) throws E { 56 | return visitor.visit(this); 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /src/main/java/cn/edu/thu/tsmart/core/cfa/llvm/LandingPadInst.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Institute on Software System and Engineering, School of Software, Tsinghua University 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package cn.edu.thu.tsmart.core.cfa.llvm; 25 | 26 | import static cn.edu.thu.tsmart.core.cfa.llvm.InstructionProperties.OpCode; 27 | 28 | import cn.edu.thu.tsmart.core.cfa.util.visitor.InstructionVisitor; 29 | import cn.edu.thu.tsmart.core.exceptions.CPAException; 30 | 31 | /** 32 | * @author guangchen on 27/02/2017. 33 | */ 34 | public class LandingPadInst extends Instruction { 35 | 36 | public LandingPadInst(String name, Type type) { 37 | super(name, type); 38 | super.opCode = OpCode.LANDINGPAD; 39 | } 40 | 41 | @Override 42 | public R accept(InstructionVisitor visitor) throws E { 43 | return visitor.visit(this); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/main/java/cn/edu/thu/tsmart/core/cfa/llvm/LlvmFunction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Institute on Software System and Engineering, School of Software, Tsinghua University 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package cn.edu.thu.tsmart.core.cfa.llvm; 25 | 26 | import cn.edu.thu.tsmart.core.cfa.llvm.Attribute.AttributeKind; 27 | import com.google.common.collect.ImmutableSet; 28 | import java.util.ArrayList; 29 | import java.util.List; 30 | import java.util.Objects; 31 | import javax.annotation.Nullable; 32 | 33 | /** 34 | * @author guangchen on 26/02/2017. 35 | */ 36 | public class LlvmFunction extends GlobalObject { 37 | 38 | private List basicBlockList = new ArrayList<>(); 39 | 40 | // TODO initialize in Converter 41 | private AttributeList attrs; 42 | private List argumentList; 43 | private Metadata metadata; 44 | 45 | public LlvmFunction() {} 46 | 47 | public LlvmFunction(String name, Type type, List basicBlockList) { 48 | super(name, type); 49 | this.basicBlockList = basicBlockList; 50 | for (BasicBlock basicBlock : this.basicBlockList) { 51 | basicBlock.setParent(this); 52 | } 53 | } 54 | 55 | // only for Converter 56 | public void setAttrs(AttributeList attrs) { 57 | this.attrs = attrs; 58 | } 59 | 60 | public List getBasicBlockList() { 61 | return basicBlockList; 62 | } 63 | 64 | public void setBasicBlockList(List basicBlockList) { 65 | this.basicBlockList = basicBlockList; 66 | } 67 | 68 | public AttributeList getAttributes() { 69 | return attrs; 70 | } 71 | 72 | public ImmutableSet getFnAttributes() { 73 | return attrs.getFnAttributes(); 74 | } 75 | 76 | public boolean hasFnAttribute(AttributeKind attrKind) { 77 | return attrs.hasFnAttribute(attrKind); 78 | } 79 | 80 | @Nullable 81 | public Attribute getFnAttribute(AttributeKind attrKind) { 82 | return getFnAttribute(attrKind); 83 | } 84 | 85 | public List getArgumentList() { 86 | return argumentList; 87 | } 88 | 89 | public void setArgumentList(List argumentList) { 90 | this.argumentList = argumentList; 91 | } 92 | 93 | @Override 94 | public String toString() { 95 | String res = "@"; 96 | res += getName(); 97 | return res; 98 | } 99 | 100 | @Override 101 | public int hashCode() { 102 | int result = basicBlockList.hashCode(); 103 | if (getName() != null) { 104 | result += 31 * result + getName().hashCode(); 105 | } 106 | return result; 107 | } 108 | 109 | public void setMetadata(Metadata metadata) { 110 | this.metadata = metadata; 111 | } 112 | 113 | public Metadata getMetadata() { 114 | return metadata; 115 | } 116 | } 117 | -------------------------------------------------------------------------------- /src/main/java/cn/edu/thu/tsmart/core/cfa/llvm/LlvmModule.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Institute on Software System and Engineering, School of Software, Tsinghua University 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | package cn.edu.thu.tsmart.core.cfa.llvm; 26 | 27 | import com.google.common.collect.Sets; 28 | import java.util.List; 29 | import java.util.Map; 30 | import java.util.Map.Entry; 31 | 32 | /** 33 | * @author guangchen on 24/02/2017. 34 | */ 35 | public class LlvmModule { 36 | private final Context context; 37 | private final String moduleIdentifier; 38 | private final Map functionMap; 39 | private final List globalList; 40 | 41 | public LlvmModule(Context context, String moduleIdentifier, Map functionMap, List globalList) { 42 | this.context = context; 43 | this.moduleIdentifier = moduleIdentifier; 44 | this.functionMap = functionMap; 45 | this.globalList = globalList; 46 | } 47 | 48 | public String getModuleIdentifier() { 49 | return this.moduleIdentifier; 50 | } 51 | 52 | public LlvmFunction getFunction(String name) { 53 | return functionMap.get(name); 54 | } 55 | 56 | public Iterable> functionEntries() { 57 | return functionMap.entrySet(); 58 | } 59 | 60 | public Iterable functions() { 61 | return functionMap.values(); 62 | } 63 | 64 | public List getGlobalList() { 65 | return globalList; 66 | } 67 | 68 | public Context getContext() { 69 | return context; 70 | } 71 | 72 | @Override 73 | public int hashCode() { 74 | int result = moduleIdentifier.hashCode(); 75 | result = 31 * result + functionMap.hashCode(); 76 | result = 31 * result + globalList.hashCode(); 77 | return result; 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /src/main/java/cn/edu/thu/tsmart/core/cfa/llvm/LoadInst.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Institute on Software System and Engineering, School of Software, Tsinghua University 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package cn.edu.thu.tsmart.core.cfa.llvm; 25 | 26 | import static cn.edu.thu.tsmart.core.cfa.llvm.InstructionProperties.AtomicOrdering; 27 | import static cn.edu.thu.tsmart.core.cfa.llvm.InstructionProperties.OpCode; 28 | import static cn.edu.thu.tsmart.core.cfa.llvm.InstructionProperties.SynchronizationScope; 29 | 30 | import cn.edu.thu.tsmart.core.cfa.util.Formatter; 31 | import cn.edu.thu.tsmart.core.cfa.util.visitor.InstructionVisitor; 32 | import cn.edu.thu.tsmart.core.exceptions.CPAException; 33 | 34 | /** 35 | * @author guangchen on 27/02/2017. 36 | */ 37 | public class LoadInst extends UnaryInstruction { 38 | 39 | // TODO initialize in Converter 40 | private boolean isVolatile = false; 41 | private int alignment = 0; 42 | private AtomicOrdering ordering = null; 43 | private SynchronizationScope synchScope = null; 44 | 45 | public LoadInst(String name, Type type, int alignment) { 46 | super(name, type); 47 | super.opCode = OpCode.LOAD; 48 | this.alignment = alignment; 49 | } 50 | 51 | // only for Converter 52 | public void setVolatile(boolean isVolatile) { 53 | this.isVolatile = isVolatile; 54 | } 55 | 56 | // only for Converter 57 | // NOTICE parameter type uses int to store unsigned 58 | public void setAlignment(int alignment) { 59 | this.alignment = alignment; 60 | } 61 | 62 | // only for Converter 63 | public void setOrdering(AtomicOrdering ordering) { 64 | this.ordering = ordering; 65 | } 66 | 67 | // only for Converter 68 | public void setSynchScope(SynchronizationScope synchScope) { 69 | this.synchScope = synchScope; 70 | } 71 | 72 | public boolean isVolatile() { 73 | return isVolatile; 74 | } 75 | 76 | // NOTICE return type uses int to store unsigned 77 | public int getAlignment() { 78 | return alignment; 79 | } 80 | 81 | public AtomicOrdering getOrdering() { 82 | return ordering; 83 | } 84 | 85 | public SynchronizationScope getSynchScope() { 86 | return synchScope; 87 | } 88 | 89 | public boolean isSimple() { 90 | return !isAtomic() && !isVolatile(); 91 | } 92 | 93 | public boolean isUnordered() { 94 | return (getOrdering() == AtomicOrdering.NOT_ATOMIC || getOrdering() == AtomicOrdering.UNORDERED) 95 | && !isVolatile(); 96 | } 97 | 98 | public Value getPointerOperand() { 99 | return getOperand(0); 100 | } 101 | 102 | // NOTICE return type uses int to store unsigned 103 | public int getPointerAddressSpace() { 104 | return getPointerOperand().getType().getPointerAddressSpace(); 105 | } 106 | 107 | @Override 108 | public R accept(InstructionVisitor visitor) throws E { 109 | return visitor.visit(this); 110 | } 111 | 112 | @Override 113 | public String toString() { 114 | String res = "%" + getName() + " = load "; 115 | if(isVolatile()) 116 | res += "volatile "; 117 | res += getType().toString(); 118 | Value operand = getOperand(0); 119 | res += ", " + operand.getType().toString(); 120 | res += " " + Formatter.asOperand(operand); 121 | res += ", align " + getAlignment(); 122 | return res; 123 | } 124 | } 125 | -------------------------------------------------------------------------------- /src/main/java/cn/edu/thu/tsmart/core/cfa/llvm/Metadata.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Institute on Software System and Engineering, School of Software, Tsinghua University 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package cn.edu.thu.tsmart.core.cfa.llvm; 25 | 26 | /** 27 | * @author guangchen on 16/04/2017. 28 | */ 29 | public class Metadata extends Value { 30 | private String file; 31 | private int line; 32 | private int column; 33 | public String getFile() { 34 | return file; 35 | } 36 | public int getLine() { 37 | return line; 38 | } 39 | public int getColumn() { 40 | return column; 41 | } 42 | public void setFile(String f) { 43 | file = f; 44 | } 45 | public void setLine(int l) { 46 | line = l; 47 | } 48 | public void setColumn(int c) { 49 | column = c; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/main/java/cn/edu/thu/tsmart/core/cfa/llvm/PointerType.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Institute on Software System and Engineering, School of Software, Tsinghua University 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package cn.edu.thu.tsmart.core.cfa.llvm; 25 | 26 | /** @author guangchen on 01/03/2017. */ 27 | public class PointerType extends SequentialType { 28 | private int addressSpace; 29 | private Type elementType; 30 | 31 | protected PointerType(Context context, Type elementType, int addressSpace) { 32 | super(context, TypeID.PointerTyID); 33 | this.addressSpace = addressSpace; 34 | this.elementType = elementType; 35 | } 36 | 37 | public static PointerType get(Type elementType, int addressSpace) { 38 | return elementType.getPointerTo(addressSpace); 39 | } 40 | 41 | public static PointerType getUnqual(Type elementType) { 42 | return get(elementType, 0); 43 | } 44 | 45 | public int getAddressSpace() { 46 | return addressSpace; 47 | } 48 | 49 | @Override 50 | public int getPointerAddressSpace() { 51 | return getAddressSpace(); 52 | } 53 | 54 | @Override 55 | public Type getPointerElementType() { 56 | return this.elementType; 57 | } 58 | 59 | @Override 60 | public Type getElementType() { 61 | return this.elementType; 62 | } 63 | 64 | @Override 65 | public String toString() { 66 | return elementType.toString() + "*"; 67 | } 68 | 69 | @Override 70 | public long sizeInBytes() { 71 | return getContext().getPointerSize(); 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /src/main/java/cn/edu/thu/tsmart/core/cfa/llvm/PtrToIntInst.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Institute on Software System and Engineering, School of Software, Tsinghua University 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package cn.edu.thu.tsmart.core.cfa.llvm; 25 | 26 | import static cn.edu.thu.tsmart.core.cfa.llvm.InstructionProperties.*; 27 | 28 | /** 29 | * @author guangchen on 27/02/2017. 30 | */ 31 | public class PtrToIntInst extends CastInst { 32 | 33 | public PtrToIntInst(String name, Type type) { 34 | super(name, type); 35 | super.opCode = OpCode.PTRTOINT; 36 | } 37 | 38 | public Value getPointerOperand() { 39 | return getOperand(0); 40 | } 41 | 42 | public int getPointerAddressSpace() { 43 | return getPointerOperand().getType().getPointerAddressSpace(); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/main/java/cn/edu/thu/tsmart/core/cfa/llvm/ResumeInst.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Institute on Software System and Engineering, School of Software, Tsinghua University 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package cn.edu.thu.tsmart.core.cfa.llvm; 25 | 26 | import static cn.edu.thu.tsmart.core.cfa.llvm.InstructionProperties.OpCode; 27 | 28 | import cn.edu.thu.tsmart.core.cfa.util.visitor.InstructionVisitor; 29 | import cn.edu.thu.tsmart.core.exceptions.CPAException; 30 | import javax.annotation.Nullable; 31 | 32 | /** 33 | * @author guangchen on 27/02/2017. 34 | */ 35 | public class ResumeInst extends TerminatorInst { 36 | 37 | public ResumeInst(String name, Type type) { 38 | super(name, type); 39 | super.opCode = OpCode.RESUME; 40 | // this instruction should not be used when analysing C 41 | assert false : "Unhandled instruction: resume"; 42 | } 43 | 44 | @Override 45 | public int getNumSuccessors() { 46 | return 0; 47 | } 48 | 49 | @Override 50 | @Nullable 51 | public BasicBlock getSuccessor(int i) { 52 | return null; 53 | } 54 | 55 | @Override 56 | public R accept(InstructionVisitor visitor) throws E { 57 | return visitor.visit(this); 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/main/java/cn/edu/thu/tsmart/core/cfa/llvm/ReturnInst.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Institute on Software System and Engineering, School of Software, Tsinghua University 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package cn.edu.thu.tsmart.core.cfa.llvm; 25 | 26 | import static cn.edu.thu.tsmart.core.cfa.llvm.InstructionProperties.OpCode; 27 | 28 | import cn.edu.thu.tsmart.core.cfa.util.Formatter; 29 | import cn.edu.thu.tsmart.core.cfa.util.visitor.InstructionVisitor; 30 | import cn.edu.thu.tsmart.core.exceptions.CPAException; 31 | import javax.annotation.Nullable; 32 | 33 | /** 34 | * @author guangchen on 27/02/2017. 35 | */ 36 | public class ReturnInst extends TerminatorInst { 37 | 38 | public ReturnInst(String name, Type type) { 39 | super(name, type); 40 | super.opCode = OpCode.RET; 41 | } 42 | 43 | @Override 44 | public int getNumSuccessors() { 45 | return 0; 46 | } 47 | 48 | @Override 49 | @Nullable 50 | public BasicBlock getSuccessor(int i) { 51 | return null; 52 | } 53 | 54 | @Nullable 55 | public Value getReturnValue() { 56 | if (getNumOperands() == 0) { 57 | return null; 58 | } else { 59 | return getOperand(0); 60 | } 61 | } 62 | 63 | @Override 64 | public R accept(InstructionVisitor visitor) throws E { 65 | return visitor.visit(this); 66 | } 67 | 68 | @Override 69 | public String toString() { 70 | String res = "ret"; 71 | if (getReturnValue() != null) { 72 | res += " " + getReturnValue().getType().toString() + " " + Formatter.asOperand(getReturnValue()); 73 | } else { 74 | res += " void"; 75 | } 76 | return res; 77 | } 78 | } -------------------------------------------------------------------------------- /src/main/java/cn/edu/thu/tsmart/core/cfa/llvm/SExtInst.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Institute on Software System and Engineering, School of Software, Tsinghua University 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package cn.edu.thu.tsmart.core.cfa.llvm; 25 | 26 | import static cn.edu.thu.tsmart.core.cfa.llvm.InstructionProperties.*; 27 | 28 | /** 29 | * @author guangchen on 27/02/2017. 30 | */ 31 | public class SExtInst extends CastInst { 32 | 33 | public SExtInst(String name, Type type) { 34 | super(name, type); 35 | super.opCode = OpCode.SEXT; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/cn/edu/thu/tsmart/core/cfa/llvm/SIToFPInst.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Institute on Software System and Engineering, School of Software, Tsinghua University 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package cn.edu.thu.tsmart.core.cfa.llvm; 25 | 26 | import static cn.edu.thu.tsmart.core.cfa.llvm.InstructionProperties.*; 27 | 28 | /** 29 | * @author guangchen on 27/02/2017. 30 | */ 31 | public class SIToFPInst extends CastInst { 32 | 33 | public SIToFPInst(String name, Type type) { 34 | super(name, type); 35 | super.opCode = OpCode.SITOFP; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/cn/edu/thu/tsmart/core/cfa/llvm/SelectConstantExpr.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Institute on Software System and Engineering, School of Software, Tsinghua University 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package cn.edu.thu.tsmart.core.cfa.llvm; 25 | 26 | import cn.edu.thu.sse.common.util.Pair; 27 | import cn.edu.thu.tsmart.core.cfa.llvm.InstructionProperties.OpCode; 28 | import java.util.HashMap; 29 | 30 | /** 31 | * Created by zhch on 2017/5/15. 32 | */ 33 | public class SelectConstantExpr extends ConstantExpr { 34 | 35 | private static HashMap, Constant>, SelectConstantExpr> instances; 36 | 37 | private SelectConstantExpr(String name, Type type) { 38 | super(name, type, OpCode.SELECT); 39 | } 40 | 41 | public static SelectConstantExpr getInstance(String name, Type type, Constant opCond, 42 | Constant op1, Constant op2) { 43 | Pair, Constant> key = Pair.of(Pair.of(op1, op2), opCond); 44 | if (instances.containsKey(key)) { 45 | return instances.get(key); 46 | } else { 47 | SelectConstantExpr instance = new SelectConstantExpr(name, type); 48 | instances.put(key, instance); 49 | return instance; 50 | } 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/main/java/cn/edu/thu/tsmart/core/cfa/llvm/SelectInst.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Institute on Software System and Engineering, School of Software, Tsinghua University 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package cn.edu.thu.tsmart.core.cfa.llvm; 25 | 26 | import static cn.edu.thu.tsmart.core.cfa.llvm.InstructionProperties.OpCode; 27 | 28 | import cn.edu.thu.tsmart.core.cfa.util.visitor.InstructionVisitor; 29 | import cn.edu.thu.tsmart.core.exceptions.CPAException; 30 | import java.util.Arrays; 31 | import java.util.List; 32 | import sun.reflect.generics.reflectiveObjects.NotImplementedException; 33 | 34 | /** 35 | * @author guangchen on 27/02/2017. 36 | */ 37 | public class SelectInst extends Instruction { 38 | 39 | public SelectInst(String name, Type type) { 40 | super(name, type); 41 | super.opCode = OpCode.SELECT; 42 | } 43 | 44 | public Value getCondition() { 45 | return getOperand(0); 46 | } 47 | 48 | public Value getTrueValue() { 49 | return getOperand(1); 50 | } 51 | 52 | public Value getFalseValue() { 53 | return getOperand(2); 54 | } 55 | 56 | @Override 57 | public R accept(InstructionVisitor visitor) throws E { 58 | return visitor.visit(this); 59 | } 60 | 61 | public static Instruction create(Value operand, Value operand1, Value operand2) { 62 | List list = Arrays.asList(operand, operand1, operand2); 63 | SelectInst instruction = new SelectInst("", null); 64 | instruction.setOperands(list); 65 | return instruction; 66 | } 67 | 68 | @Override 69 | public String toString() { 70 | String res = "%" + getName().toString() + " = "; 71 | res += getOpcode() + " "; 72 | for(int i = 0; i < getNumOperands(); i++) { 73 | res += getOperand(i).getType().toString() + " "; 74 | if(getOperand(i).getType().isPointerTy()) { 75 | res += "@" + getOperand(i).getName().toString(); 76 | } else if(getOperand(i).getName().equals("CONSTANT_INT")) { 77 | res += getOperand(i).toString(); 78 | } else { 79 | res += "%" + getOperand(i).getName().toString(); 80 | } 81 | if(i != getNumOperands() - 1) { 82 | res += ", "; 83 | } 84 | } 85 | return res; 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /src/main/java/cn/edu/thu/tsmart/core/cfa/llvm/SequentialType.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Institute on Software System and Engineering, School of Software, Tsinghua University 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package cn.edu.thu.tsmart.core.cfa.llvm; 25 | 26 | import sun.reflect.generics.reflectiveObjects.NotImplementedException; 27 | 28 | /** @author guangchen on 01/03/2017. */ 29 | public class SequentialType extends CompositeType { 30 | protected SequentialType(Context context, TypeID id) { 31 | super(context, id); 32 | } 33 | 34 | public Type getElementType() { 35 | throw new NotImplementedException(); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/cn/edu/thu/tsmart/core/cfa/llvm/ShuffleVectorConstantExpr.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Institute on Software System and Engineering, School of Software, Tsinghua University 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package cn.edu.thu.tsmart.core.cfa.llvm; 25 | 26 | import cn.edu.thu.tsmart.core.cfa.llvm.InstructionProperties.OpCode; 27 | 28 | /** 29 | * Created by zhch on 2017/5/15. 30 | */ 31 | public class ShuffleVectorConstantExpr extends ConstantExpr { 32 | 33 | private ShuffleVectorConstantExpr(String name, Type type) { 34 | super(name, type, OpCode.SHUFFLEVECTOR); 35 | } 36 | 37 | public static ShuffleVectorConstantExpr getInstance(String name, Type type, Constant op1, 38 | Constant op2, Constant op3) { 39 | // this expr should not be used when analysing C 40 | assert false : "Unhandled constant expression: shufflevector"; 41 | return null; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/main/java/cn/edu/thu/tsmart/core/cfa/llvm/ShuffleVectorInst.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Institute on Software System and Engineering, School of Software, Tsinghua University 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package cn.edu.thu.tsmart.core.cfa.llvm; 25 | 26 | import static cn.edu.thu.tsmart.core.cfa.llvm.InstructionProperties.OpCode; 27 | 28 | import cn.edu.thu.tsmart.core.cfa.util.visitor.InstructionVisitor; 29 | import cn.edu.thu.tsmart.core.exceptions.CPAException; 30 | 31 | /** 32 | * @author guangchen on 27/02/2017. 33 | */ 34 | public class ShuffleVectorInst extends Instruction { 35 | 36 | public ShuffleVectorInst(String name, Type type) { 37 | super(name, type); 38 | super.opCode = OpCode.SHUFFLEVECTOR; 39 | // this instruction should not be used when analysing C 40 | assert false : "Unhandled instruction: invoke"; 41 | } 42 | 43 | @Override 44 | public R accept(InstructionVisitor visitor) throws E { 45 | return visitor.visit(this); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/main/java/cn/edu/thu/tsmart/core/cfa/llvm/TerminatorInst.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Institute on Software System and Engineering, School of Software, Tsinghua University 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package cn.edu.thu.tsmart.core.cfa.llvm; 25 | 26 | import javax.annotation.Nullable; 27 | 28 | /** 29 | * @author guangchen on 27/02/2017. 30 | */ 31 | public abstract class TerminatorInst extends Instruction { 32 | 33 | protected TerminatorInst(String name, Type type) { 34 | super(name, type); 35 | } 36 | 37 | public abstract int getNumSuccessors(); 38 | 39 | @Nullable 40 | public abstract BasicBlock getSuccessor(int i); 41 | 42 | public boolean isExceptional() { 43 | switch (getOpcode()) { 44 | case CATCHSWITCH: 45 | case CATCHRET: 46 | case CLEANUPRET: 47 | case INVOKE: 48 | case RESUME: 49 | return true; 50 | default: 51 | return false; 52 | } 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/main/java/cn/edu/thu/tsmart/core/cfa/llvm/TruncInst.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Institute on Software System and Engineering, School of Software, Tsinghua University 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package cn.edu.thu.tsmart.core.cfa.llvm; 25 | 26 | import static cn.edu.thu.tsmart.core.cfa.llvm.InstructionProperties.*; 27 | 28 | /** 29 | * @author guangchen on 27/02/2017. 30 | */ 31 | public class TruncInst extends CastInst { 32 | 33 | public TruncInst(String name, Type type) { 34 | super(name, type); 35 | super.opCode = OpCode.TRUNC; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/cn/edu/thu/tsmart/core/cfa/llvm/UIToFPInst.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Institute on Software System and Engineering, School of Software, Tsinghua University 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package cn.edu.thu.tsmart.core.cfa.llvm; 25 | 26 | import static cn.edu.thu.tsmart.core.cfa.llvm.InstructionProperties.*; 27 | 28 | /** 29 | * @author guangchen on 27/02/2017. 30 | */ 31 | public class UIToFPInst extends CastInst { 32 | 33 | public UIToFPInst(String name, Type type) { 34 | super(name, type); 35 | super.opCode = OpCode.UITOFP; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/cn/edu/thu/tsmart/core/cfa/llvm/UnaryConstantExpr.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Institute on Software System and Engineering, School of Software, Tsinghua University 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package cn.edu.thu.tsmart.core.cfa.llvm; 25 | 26 | import cn.edu.thu.sse.common.util.Pair; 27 | import cn.edu.thu.tsmart.core.cfa.llvm.InstructionProperties.OpCode; 28 | import java.util.HashMap; 29 | 30 | /** 31 | * Created by zhch on 2017/5/15. 32 | */ 33 | public class UnaryConstantExpr extends ConstantExpr { 34 | 35 | private Type destTy; 36 | private static HashMap>, UnaryConstantExpr> instances = new HashMap<>(); 37 | 38 | private UnaryConstantExpr(String name, Type type, OpCode opCode, Type destTy) { 39 | super(name, type, opCode); 40 | this.destTy = destTy; 41 | } 42 | 43 | public static UnaryConstantExpr getInstance(String name, Type type, OpCode opCode, Constant op, 44 | Type destTy) { 45 | Pair> key = Pair.of(opCode, Pair.of(op, destTy)); 46 | if (instances.containsKey(key)) { 47 | return instances.get(key); 48 | } else { 49 | UnaryConstantExpr instance = new UnaryConstantExpr(name, type, opCode, destTy); 50 | instances.put(key, instance); 51 | return instance; 52 | } 53 | } 54 | 55 | @Override 56 | public String toString() { 57 | String res = opCode.toString(); 58 | res += " ("; 59 | res += getOperand(0).getType().toString(); 60 | res += " "; 61 | res += getOperand(0).toString(); 62 | res += " to "; 63 | res += destTy.toString(); 64 | res += ")"; 65 | return res; 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /src/main/java/cn/edu/thu/tsmart/core/cfa/llvm/UnaryInstruction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Institute on Software System and Engineering, School of Software, Tsinghua University 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package cn.edu.thu.tsmart.core.cfa.llvm; 25 | 26 | /** 27 | * @author guangchen on 27/02/2017. 28 | */ 29 | public abstract class UnaryInstruction extends Instruction { 30 | 31 | protected UnaryInstruction(String name, Type type) { 32 | super(name, type); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/main/java/cn/edu/thu/tsmart/core/cfa/llvm/UndefValue.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Institute on Software System and Engineering, School of Software, Tsinghua University 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package cn.edu.thu.tsmart.core.cfa.llvm; 25 | 26 | /** 27 | * @author guangchen on 14/12/2017. 28 | */ 29 | public class UndefValue extends ConstantData { 30 | 31 | public UndefValue(String name, Type type) { 32 | super(name, type); 33 | } 34 | 35 | @Override 36 | public String toString() { 37 | return "undef"; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/main/java/cn/edu/thu/tsmart/core/cfa/llvm/UnreachableInst.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Institute on Software System and Engineering, School of Software, Tsinghua University 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package cn.edu.thu.tsmart.core.cfa.llvm; 25 | 26 | import static cn.edu.thu.tsmart.core.cfa.llvm.InstructionProperties.OpCode; 27 | 28 | import cn.edu.thu.tsmart.core.cfa.util.visitor.InstructionVisitor; 29 | import cn.edu.thu.tsmart.core.exceptions.CPAException; 30 | 31 | /** 32 | * @author guangchen on 27/02/2017. 33 | */ 34 | public class UnreachableInst extends TerminatorInst { 35 | 36 | public UnreachableInst(String name, Type type) { 37 | super(name, type); 38 | super.opCode = OpCode.UNREACHABLE; 39 | } 40 | 41 | @Override 42 | public int getNumSuccessors() { 43 | return 0; 44 | } 45 | 46 | @Override 47 | public BasicBlock getSuccessor(int i) { 48 | assert false : "UnreachableInst has no successors!"; 49 | return null; 50 | } 51 | 52 | @Override 53 | public R accept(InstructionVisitor visitor) throws E { 54 | return visitor.visit(this); 55 | } 56 | 57 | @Override 58 | public String toString() { 59 | return "unreachable"; 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /src/main/java/cn/edu/thu/tsmart/core/cfa/llvm/Use.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Institute on Software System and Engineering, School of Software, Tsinghua University 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package cn.edu.thu.tsmart.core.cfa.llvm; 25 | 26 | import java.util.Iterator; 27 | 28 | /** 29 | * @author guangchen on 26/02/2017. 30 | */ 31 | public class Use { 32 | 33 | private final Value value; 34 | private final User user; 35 | private final int index; 36 | private Use next = null; 37 | 38 | public Use(Value value, User user, int index) { 39 | this.value = value; 40 | this.user = user; 41 | this.index = index; 42 | } 43 | 44 | public Value get() { 45 | return value; 46 | } 47 | 48 | public Use getNext() { 49 | return next; 50 | } 51 | 52 | public User getUser() { 53 | return user; 54 | } 55 | 56 | public int getOperandNo() { 57 | return index; 58 | } 59 | 60 | public void setNext(Use next) { 61 | this.next = next; 62 | } 63 | 64 | public static class LazyIterable implements Iterable { 65 | private Iterator iter; 66 | 67 | public LazyIterable(Use use) { 68 | iter = new LazyIterator(use); 69 | } 70 | 71 | @Override 72 | public Iterator iterator() { 73 | return iter; 74 | } 75 | } 76 | 77 | public static class LazyIterator implements Iterator { 78 | private Use use; 79 | 80 | public LazyIterator(Use use) { 81 | this.use = use; 82 | } 83 | 84 | @Override 85 | public boolean hasNext() { 86 | return use != null && use.getNext() != null; 87 | } 88 | 89 | @Override 90 | public Use next() { 91 | return use = use.getNext(); 92 | } 93 | 94 | @Override 95 | public void remove() { 96 | throw new UnsupportedOperationException("LazyIterator.remove not supported"); 97 | } 98 | } 99 | 100 | public static Iterable makeIterable(Use use) { 101 | return new LazyIterable(use); 102 | } 103 | } 104 | -------------------------------------------------------------------------------- /src/main/java/cn/edu/thu/tsmart/core/cfa/llvm/User.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Institute on Software System and Engineering, School of Software, Tsinghua University 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package cn.edu.thu.tsmart.core.cfa.llvm; 25 | 26 | import sun.reflect.generics.reflectiveObjects.NotImplementedException; 27 | 28 | import java.util.List; 29 | 30 | /** 31 | * @author guangchen on 26/02/2017. 32 | */ 33 | public class User extends Value { 34 | // 35 | // User(LLVMValueRef valueRef) { 36 | // super(valueRef); 37 | // for (int i = 0, end = LLVMGetNumOperands(valueRef); i < end; i++) { 38 | // Value value = new Value(LLVMGetOperand(valueRef, i)); 39 | // use.add(new Use(value, this, i)); 40 | // } 41 | // for (int i = 0; i < use.size() - 1; i ++) { 42 | // use.get(i).setNext(use.get(i + 1)); 43 | // } 44 | // } 45 | 46 | private List operands; 47 | 48 | public User() {} 49 | 50 | public User(String name, Type type) { 51 | super(name, type); 52 | } 53 | 54 | public Value getOperand(int i) { 55 | return operands.get(i); 56 | } 57 | 58 | public void setOperands(List operands) { 59 | this.operands = operands; 60 | } 61 | 62 | public List getOperands() { 63 | return this.operands; 64 | } 65 | 66 | public List getOperandList() { 67 | throw new NotImplementedException(); 68 | } 69 | 70 | public Use getOperandUse(int i) { 71 | return uses().get(i); 72 | } 73 | 74 | public int getNumOperands() { 75 | return operands.size(); 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /src/main/java/cn/edu/thu/tsmart/core/cfa/llvm/VAArgInst.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Institute on Software System and Engineering, School of Software, Tsinghua University 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package cn.edu.thu.tsmart.core.cfa.llvm; 25 | 26 | import static cn.edu.thu.tsmart.core.cfa.llvm.InstructionProperties.OpCode; 27 | 28 | import cn.edu.thu.tsmart.core.cfa.util.visitor.InstructionVisitor; 29 | import cn.edu.thu.tsmart.core.exceptions.CPAException; 30 | 31 | /** 32 | * @author guangchen on 27/02/2017. 33 | */ 34 | public class VAArgInst extends UnaryInstruction { 35 | 36 | public VAArgInst(String name, Type type) { 37 | super(name, type); 38 | super.opCode = OpCode.VA_ARG; 39 | } 40 | 41 | public Value getPointerOperand() { 42 | return getOperand(0); 43 | } 44 | 45 | @Override 46 | public R accept(InstructionVisitor visitor) throws E { 47 | return visitor.visit(this); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/main/java/cn/edu/thu/tsmart/core/cfa/llvm/Value.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Institute on Software System and Engineering, School of Software, Tsinghua University 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package cn.edu.thu.tsmart.core.cfa.llvm; 25 | 26 | import com.google.common.base.Function; 27 | import com.google.common.base.Optional; 28 | import com.google.common.collect.Lists; 29 | 30 | import java.util.List; 31 | 32 | /** 33 | * @author guangchen on 26/02/2017. 34 | */ 35 | public class Value { 36 | private String name; 37 | private Type type; 38 | private List uses; 39 | 40 | public Value() {} 41 | 42 | public Value(String name, Type type) { 43 | this.name = name; 44 | this.type = type; 45 | } 46 | 47 | public String getName() { 48 | return name; 49 | } 50 | 51 | public void setName(String name) { 52 | this.name = name; 53 | } 54 | 55 | public Type getType() { 56 | return type; 57 | } 58 | 59 | public void setType(Type type) { 60 | this.type = type; 61 | } 62 | 63 | public boolean hasName() { 64 | return name != null && !name.equals(""); 65 | } 66 | 67 | public List uses() { 68 | return this.uses; 69 | } 70 | 71 | public void setUses(final List uses) { 72 | this.uses = uses; 73 | } 74 | 75 | public List users() { 76 | return Lists.transform(this.uses, new Function() { 77 | @Override 78 | public User apply(Use input) { 79 | return Optional.fromNullable(input).transform(new Function() { 80 | @Override 81 | public User apply(Use input) { 82 | return input.getUser(); 83 | } 84 | }).orNull(); 85 | } 86 | }); 87 | } 88 | 89 | } 90 | -------------------------------------------------------------------------------- /src/main/java/cn/edu/thu/tsmart/core/cfa/llvm/VectorType.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Institute on Software System and Engineering, School of Software, Tsinghua University 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package cn.edu.thu.tsmart.core.cfa.llvm; 25 | 26 | /** 27 | * @author guangchen on 01/03/2017. 28 | */ 29 | public class VectorType extends SequentialType { 30 | private Type elementType; 31 | private int numElements; 32 | 33 | protected VectorType(Context context, Type elementType, int numElements) { 34 | super(context, TypeID.VectorTyID); 35 | this.elementType = elementType; 36 | this.numElements = numElements; 37 | } 38 | 39 | public Type getElementType() { return this.elementType; } 40 | 41 | public int getNumElements() { return this.numElements; } 42 | 43 | public static VectorType get(Type elementType, int numElements) { 44 | return new VectorType(elementType.getContext(), elementType, numElements); 45 | } 46 | 47 | public int getBitWidth() { return numElements * elementType.getPrimitiveSizeInBits(); } 48 | } 49 | -------------------------------------------------------------------------------- /src/main/java/cn/edu/thu/tsmart/core/cfa/llvm/ZExtInst.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Institute on Software System and Engineering, School of Software, Tsinghua University 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package cn.edu.thu.tsmart.core.cfa.llvm; 25 | 26 | import static cn.edu.thu.tsmart.core.cfa.llvm.InstructionProperties.*; 27 | 28 | /** 29 | * @author guangchen on 27/02/2017. 30 | */ 31 | public class ZExtInst extends CastInst { 32 | 33 | public ZExtInst(String name, Type type) { 34 | super(name, type); 35 | super.opCode = OpCode.ZEXT; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/cn/edu/thu/tsmart/core/cfa/util/Casting.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Institute on Software System and Engineering, School of Software, Tsinghua University 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package cn.edu.thu.tsmart.core.cfa.util; 25 | 26 | import javax.annotation.Nullable; 27 | 28 | /** 29 | * @author guangchen on 01/03/2017. 30 | */ 31 | public class Casting { 32 | 33 | public static T2 cast(T1 obj, Class t2Class) { 34 | assert t2Class.isInstance(obj) : "cast() argument of incompatible type!"; 35 | return t2Class.cast(obj); 36 | } 37 | 38 | @Nullable 39 | public static T2 castOrNull(T1 obj, @Nullable Class t2Class) { 40 | if (t2Class == null || obj == null) { 41 | return null; 42 | } 43 | assert t2Class.isInstance(obj) : "cast_or_null() argument of incompatible type!"; 44 | return t2Class.cast(obj); 45 | } 46 | 47 | @Nullable 48 | public static T2 dyncast(T1 obj, Class t2Class) { 49 | if (t2Class.isInstance(obj)) { 50 | return t2Class.cast(obj); 51 | } 52 | return null; 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/main/java/cn/edu/thu/tsmart/core/cfa/util/Formatter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Institute on Software System and Engineering, School of Software, Tsinghua University 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package cn.edu.thu.tsmart.core.cfa.util; 25 | 26 | import cn.edu.thu.tsmart.core.cfa.llvm.*; 27 | 28 | /** @author guangchen on 27/05/2017. */ 29 | public class Formatter { 30 | public static String asOperand(Value value) { 31 | String res = ""; 32 | if (value instanceof Instruction) { 33 | res = "%" + value.getName(); 34 | } else if (value instanceof GlobalValue) { 35 | res = "@" + value.getName(); 36 | } else if (value instanceof Constant) { 37 | res = value.toString(); 38 | } else if (value instanceof Argument) { 39 | return "%" + value.getName(); 40 | } else { 41 | assert false : "not handled"; 42 | } 43 | return res; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/main/java/cn/edu/thu/tsmart/core/exceptions/CPAException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Institute on Software System and Engineering, School of Software, Tsinghua University 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package cn.edu.thu.tsmart.core.exceptions; 25 | 26 | /** 27 | * Super class for all exceptions thrown by CPA operators. 28 | * 29 | * TODO This exception should probably be abstract, and specialized sub-classes 30 | * should be used for specific reasons. 31 | */ 32 | public abstract class CPAException extends Exception { 33 | 34 | private static final long serialVersionUID = 6846683924964869559L; 35 | 36 | public CPAException(String msg) { 37 | super(msg); 38 | } 39 | 40 | public CPAException(String msg, Throwable cause) { 41 | super(msg, cause); 42 | } 43 | 44 | public CPAException(Throwable cause) { 45 | super(cause); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/main/java/cn/edu/thu/tsmart/util/Trouble.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Institute on Software System and Engineering, School of Software, Tsinghua University 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package cn.edu.thu.tsmart.util; 25 | 26 | /** 27 | * Dummy handler for various troubles 28 | * 29 | * Always use this class to temporarily eliminate troubles, because: 30 | * 1. it has generic result which can be used as return value for any function 31 | * 2. it terminates the program by throwing the function 32 | * 3. it throws runtime exception which does not affect the function signature 33 | * 4. we can search reference of this class and fix them later 34 | * 35 | * Created by zhoumin on 6/26/17. 36 | */ 37 | public class Trouble { 38 | 39 | private static T error(String msg) { 40 | try { 41 | throw new RuntimeException(msg); 42 | } catch (RuntimeException e) { 43 | e.printStackTrace(); 44 | throw e; 45 | } 46 | } 47 | 48 | public static T futureWorkSymbolicPointer() { 49 | return error("reading or writing of symbolic pointer is not supported."); 50 | } 51 | 52 | public static T futureWorkBitPrecise() { 53 | return error("bit-precise operation analysis is not supported now"); 54 | } 55 | 56 | public static T futureWorkMultipleValues() { 57 | return error("multiple values not handled here."); 58 | } 59 | 60 | public static T futureWorkPathCondition() { 61 | return error("multiple values not handled here."); 62 | } 63 | 64 | public static T futureWorkReport(String errorType) { 65 | return error("should report " + errorType + " here."); 66 | } 67 | 68 | public static T futureWork(String msg) { 69 | return error(msg); 70 | } 71 | 72 | public static T unexpected(String msg) { 73 | return error(msg); 74 | } 75 | 76 | public static T unexpected() { 77 | return error("something is wrong. it is not possible to reach this line."); 78 | } 79 | 80 | public static T notImplementedFor(Object witness) { 81 | return error("not implemented for " + witness.getClass().getCanonicalName()); 82 | } 83 | 84 | public static T notImplemented() { 85 | return error("not implemented!"); 86 | } 87 | } 88 | --------------------------------------------------------------------------------