- Notifications
You must be signed in to change notification settings - Fork70
ImplementEssentialTypes#188
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
30 commits Select commitHold shift + click to select a range
a53e650 EssentialTypes: create a new package
lcartey60dc594 EssentialTypes: Refine set of rules and commit package files.
lcartey8423344 EssentialTypes: Add a utility module for MISRA definitions
lcartey79e44e3 EssentialTypes: Add essential types library
lcartey7c5fea9 EssentialTypes: Implement Rule 10.1
lcartey2147282 EssentialTypes: Implement Rule 10.2
lcarteyfe01ab8 EssentialTypes: Implement Rule 10.3
lcartey810a775 EssentialTypes: Implement Rule 10.4
lcartey931aa12 EssentialTypes: Implement Rule 10.5
lcartey87e7013 EssentialTypes: Implement Rule 10.6
lcartey70147d5 EssentialTypes: Implement Rule 10.6.
lcartey46be7be EssentialTypes: Implement Rule 10.8
lcartey2d70f7b EssentialTypes: Add basic test case for library.
lcarteyebc07fe EssentialTypes: Handle typedefs
lcarteyebae3e6 EssentialTypes: Implement Rule 14.1
lcartey2c106bc EssentialType: Handle type specifiers
lcarteyc330ed6 EssentialTypes: Add test cases for library
lcarteycf12521 EssentialTypes: Implement Rule 21.16
lcarteyc6ce829 EssentialTypes: Update metadata
lcarteybeff5c1 Merge branch 'main' into lcartey/essential-types
lcartey326d7d3 EssentialTypes: add . to descriptions
lcartey3eca5d6 EssentialTypes: Ensure compatibility with C++
lcarteyb38552c EssentialTypes: Implement Rule 21.14
lcarteye84f98f Merge branch 'main' into lcartey/essential-types
lcartey2750f69 EssentialTypes: Fix metadata consistency issue
lcartey3e3e052 Merge branch 'main' into lcartey/essential-types
lcartey3a5d033 Merge branch 'main' into lcartey/essential-types
lcarteybf1ee22 EssentialTypes: Handle boolean type defs
lcartey4e477e9 EssentialTypes: Make switch cases valid
lcartey0b984b2 Merge branch 'main' into lcartey/essential-types
lcarteyFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
376 changes: 376 additions & 0 deletionsc/misra/src/codingstandards/c/misra/EssentialTypes.qll
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,376 @@ | ||
| /** | ||
| * A module for identifying essential types as defined by MISRA C 2012. | ||
| */ | ||
| import codingstandards.c.misra | ||
| import semmle.code.cpp.rangeanalysis.RangeAnalysisUtils | ||
| import MisraExpressions | ||
| newtype TEssentialTypeCategory = | ||
| EssentiallyBooleanType() or | ||
| EssentiallyCharacterType() or | ||
| EssentiallyEnumType() or | ||
| EssentiallySignedType() or | ||
| EssentiallyUnsignedType() or | ||
| EssentiallyFloatingType() | ||
| /** An essential type category, as specified by Appendix D.1. */ | ||
| class EssentialTypeCategory extends TEssentialTypeCategory { | ||
| string toString() { | ||
| this = EssentiallyBooleanType() and result = "essentially Boolean type" | ||
| or | ||
| this = EssentiallyCharacterType() and result = "essentially Character type" | ||
| or | ||
| this = EssentiallyEnumType() and result = "essentially Enum Type" | ||
| or | ||
| this = EssentiallySignedType() and result = "essentially Signed type" | ||
| or | ||
| this = EssentiallyUnsignedType() and result = "essentially Unsigned type" | ||
| or | ||
| this = EssentiallyFloatingType() and result = "essentially Floating type" | ||
| } | ||
| } | ||
| /** | ||
| * Gets the unsigned type of lowest rank that can represent the value of the given expression, | ||
| * assuming that the expression is essentially unsigned. | ||
| */ | ||
| private IntegralType utlr(Expr const) { | ||
| getEssentialTypeCategory(const.getType()) = EssentiallyUnsignedType() and | ||
| getEssentialTypeCategory(result) = EssentiallyUnsignedType() and | ||
| exists(float c | c = const.getValue().toFloat() | | ||
| // As with range analysis, we assume two's complement representation | ||
| typeLowerBound(result) <= c and | ||
| typeUpperBound(result) >= c and | ||
| forall(IntegralType it | | ||
| getEssentialTypeCategory(it) = EssentiallyUnsignedType() and | ||
| typeLowerBound(it) <= c and | ||
| typeUpperBound(it) >= c | ||
| | | ||
| result.getSize() <= it.getSize() | ||
| ) | ||
| ) | ||
| } | ||
| /** | ||
| * Gets the signed type of lowest rank that can represent the value of the given expression, | ||
| * assuming that the expression is essentially signed. | ||
| */ | ||
| private IntegralType stlr(Expr const) { | ||
| getEssentialTypeCategory(const.getType()) = EssentiallySignedType() and | ||
| getEssentialTypeCategory(result) = EssentiallySignedType() and | ||
| exists(float c | c = const.getValue().toFloat() | | ||
| // As with range analysis, we assume two's complement representation | ||
| typeLowerBound(result) <= c and | ||
| typeUpperBound(result) >= c and | ||
| forall(IntegralType it | | ||
| getEssentialTypeCategory(it) = EssentiallySignedType() and | ||
| typeLowerBound(it) <= c and | ||
| typeUpperBound(it) >= c | ||
| | | ||
| result.getSize() <= it.getSize() | ||
| ) | ||
| ) | ||
| } | ||
| /** | ||
| * Define the essential type category for an essentialType or a typedef of an essentialType. | ||
| */ | ||
| EssentialTypeCategory getEssentialTypeCategory(Type type) { | ||
| exists(Type essentialType | | ||
| if type instanceof MisraBoolType | ||
| then essentialType = type | ||
| else | ||
| // If not a bool type, resolve the typedefs to determine the actual type | ||
| essentialType = type.getUnspecifiedType() | ||
| | | ||
| result = EssentiallyBooleanType() and essentialType instanceof MisraBoolType | ||
| or | ||
| result = EssentiallyCharacterType() and essentialType instanceof PlainCharType | ||
| or | ||
| result = EssentiallySignedType() and | ||
| essentialType.(IntegralType).isSigned() and | ||
| not essentialType instanceof PlainCharType | ||
| or | ||
| result = EssentiallyUnsignedType() and | ||
| essentialType.(IntegralType).isUnsigned() and | ||
| not essentialType instanceof PlainCharType | ||
| or | ||
| result = EssentiallyEnumType() and | ||
| essentialType instanceof Enum and | ||
| not essentialType instanceof MisraBoolType | ||
| or | ||
| result = EssentiallyFloatingType() and | ||
| essentialType instanceof FloatingPointType | ||
| ) | ||
| } | ||
| /** | ||
| * Gets the essential type of the given expression `e`, considering any explicit conversions. | ||
| */ | ||
| Type getEssentialType(Expr e) { | ||
| if e.hasExplicitConversion() | ||
| then | ||
| if e.getConversion() instanceof ParenthesisExpr | ||
| then | ||
| if e.getConversion().(ParenthesisExpr).hasExplicitConversion() | ||
| then result = e.getConversion().(ParenthesisExpr).getConversion().getType() | ||
| else result = e.getConversion().(ParenthesisExpr).getExpr().(EssentialExpr).getEssentialType() | ||
| else result = e.getConversion().getType() | ||
| else result = e.(EssentialExpr).getEssentialType() | ||
| } | ||
| Type getEssentialTypeBeforeConversions(Expr e) { result = e.(EssentialExpr).getEssentialType() } | ||
| class EssentialExpr extends Expr { | ||
| Type getEssentialType() { result = this.getType() } | ||
| Type getStandardType() { result = this.getType() } | ||
| } | ||
| class EssentialCommaExpr extends EssentialExpr, CommaExpr { | ||
| override Type getEssentialType() { result = getEssentialType(getRightOperand()) } | ||
| } | ||
| class EssentialRelationalOperationExpr extends EssentialExpr, RelationalOperation { | ||
| override Type getEssentialType() { result instanceof BoolType } | ||
| } | ||
| class EssentialBinaryLogicalOperationExpr extends EssentialExpr, BinaryLogicalOperation { | ||
| override Type getEssentialType() { result instanceof BoolType } | ||
| } | ||
| class EssentialEqualityOperationExpr extends EssentialExpr, EqualityOperation { | ||
| override Type getEssentialType() { result instanceof BoolType } | ||
| } | ||
| class EssentialBinaryBitwiseOperationExpr extends EssentialExpr, BinaryBitwiseOperation { | ||
| EssentialBinaryBitwiseOperationExpr() { | ||
| this instanceof LShiftExpr or | ||
| this instanceof RShiftExpr | ||
| } | ||
| override Type getEssentialType() { | ||
| exists(Type operandEssentialType, EssentialTypeCategory operandEssentialTypeCategory | | ||
| operandEssentialType = getEssentialType(getLeftOperand()) and | ||
| operandEssentialTypeCategory = getEssentialTypeCategory(operandEssentialType) | ||
| | | ||
| if operandEssentialTypeCategory instanceof EssentiallyUnsignedType | ||
| then | ||
| if exists(this.getValue()) | ||
| then result = utlr(this) // If constant and essentially unsigned us the utlr | ||
| else result = operandEssentialType | ||
| else result = this.getStandardType() | ||
| ) | ||
| } | ||
| } | ||
| class EssentialBitwiseComplementExpr extends EssentialExpr, ComplementExpr { | ||
| override Type getEssentialType() { | ||
| exists(Type operandEssentialType, EssentialTypeCategory operandEssentialTypeCategory | | ||
| operandEssentialType = getEssentialType(getOperand()) and | ||
| operandEssentialTypeCategory = getEssentialTypeCategory(operandEssentialType) | ||
| | | ||
| if operandEssentialTypeCategory instanceof EssentiallyUnsignedType | ||
| then | ||
| if exists(this.getValue()) | ||
| then result = utlr(this) // If constant and essentially unsigned us the utlr | ||
| else result = operandEssentialType | ||
| else result = this.getStandardType() | ||
| ) | ||
| } | ||
| } | ||
| class EssentialUnaryPlusExpr extends EssentialExpr, UnaryPlusExpr { | ||
| override Type getEssentialType() { | ||
| exists(Type operandEssentialType, EssentialTypeCategory operandEssentialTypeCategory | | ||
| operandEssentialType = getEssentialType(getOperand()) and | ||
| operandEssentialTypeCategory = getEssentialTypeCategory(operandEssentialType) | ||
| | | ||
| if | ||
| operandEssentialTypeCategory = | ||
| [EssentiallyUnsignedType().(TEssentialTypeCategory), EssentiallySignedType()] | ||
| then result = operandEssentialType | ||
| else result = getStandardType() | ||
| ) | ||
| } | ||
| } | ||
| class EssentialUnaryMinusExpr extends EssentialExpr, UnaryMinusExpr { | ||
| override Type getEssentialType() { | ||
| exists(Type operandEssentialType, EssentialTypeCategory operandEssentialTypeCategory | | ||
| operandEssentialType = getEssentialType(getOperand()) and | ||
| operandEssentialTypeCategory = getEssentialTypeCategory(operandEssentialType) | ||
| | | ||
| if operandEssentialTypeCategory = EssentiallySignedType() | ||
| then if exists(this.getValue()) then result = stlr(this) else result = operandEssentialType | ||
| else result = getStandardType() | ||
| ) | ||
| } | ||
| } | ||
| class EssentialConditionalExpr extends EssentialExpr, ConditionalExpr { | ||
| override Type getEssentialType() { | ||
| exists(Type thenEssentialType, Type elseEssentialType | | ||
| thenEssentialType = getEssentialType(getThen()) and | ||
| elseEssentialType = getEssentialType(getElse()) | ||
| | | ||
| if thenEssentialType = elseEssentialType | ||
| then result = thenEssentialType | ||
| else | ||
| if | ||
| getEssentialTypeCategory(thenEssentialType) = EssentiallySignedType() and | ||
| getEssentialTypeCategory(elseEssentialType) = EssentiallySignedType() | ||
| then | ||
| if thenEssentialType.getSize() > elseEssentialType.getSize() | ||
| then result = thenEssentialType | ||
| else result = elseEssentialType | ||
| else | ||
| if | ||
| getEssentialTypeCategory(thenEssentialType) = EssentiallyUnsignedType() and | ||
| getEssentialTypeCategory(elseEssentialType) = EssentiallyUnsignedType() | ||
| then | ||
| if thenEssentialType.getSize() > elseEssentialType.getSize() | ||
| then result = thenEssentialType | ||
| else result = elseEssentialType | ||
| else result = this.getStandardType() | ||
| ) | ||
| } | ||
| } | ||
| class EssentialBinaryArithmeticExpr extends EssentialExpr, BinaryArithmeticOperation { | ||
| EssentialBinaryArithmeticExpr() { | ||
| // GNU C extension has min/max which we can ignore | ||
| not this instanceof MinExpr and | ||
| not this instanceof MaxExpr | ||
| } | ||
| override Type getEssentialType() { | ||
| exists( | ||
| Type leftEssentialType, Type rightEssentialType, | ||
| EssentialTypeCategory leftEssentialTypeCategory, | ||
| EssentialTypeCategory rightEssentialTypeCategory | ||
| | | ||
| leftEssentialType = getEssentialType(getLeftOperand()) and | ||
| rightEssentialType = getEssentialType(getRightOperand()) and | ||
| leftEssentialTypeCategory = getEssentialTypeCategory(leftEssentialType) and | ||
| rightEssentialTypeCategory = getEssentialTypeCategory(rightEssentialType) | ||
| | | ||
| if | ||
| leftEssentialTypeCategory = EssentiallySignedType() and | ||
| rightEssentialTypeCategory = EssentiallySignedType() | ||
| then | ||
| if exists(getValue()) | ||
| then result = stlr(this) | ||
| else ( | ||
| if leftEssentialType.getSize() > rightEssentialType.getSize() | ||
| then result = leftEssentialType | ||
| else result = rightEssentialType | ||
| ) | ||
| else | ||
| if | ||
| leftEssentialTypeCategory = EssentiallyUnsignedType() and | ||
| rightEssentialTypeCategory = EssentiallyUnsignedType() | ||
| then | ||
| if exists(getValue()) | ||
| then result = utlr(this) | ||
| else ( | ||
| if leftEssentialType.getSize() > rightEssentialType.getSize() | ||
| then result = leftEssentialType | ||
| else result = rightEssentialType | ||
| ) | ||
| else | ||
| if | ||
| this instanceof AddExpr and | ||
| ( | ||
| leftEssentialTypeCategory = EssentiallyCharacterType() | ||
| or | ||
| rightEssentialTypeCategory = EssentiallyCharacterType() | ||
| ) and | ||
| ( | ||
| leftEssentialTypeCategory = | ||
| [EssentiallySignedType(), EssentiallyUnsignedType().(TEssentialTypeCategory)] | ||
| or | ||
| rightEssentialTypeCategory = | ||
| [EssentiallySignedType(), EssentiallyUnsignedType().(TEssentialTypeCategory)] | ||
| ) | ||
| or | ||
| this instanceof SubExpr and | ||
| leftEssentialTypeCategory = EssentiallyCharacterType() and | ||
| rightEssentialTypeCategory = | ||
| [EssentiallySignedType(), EssentiallyUnsignedType().(TEssentialTypeCategory)] | ||
| then result instanceof PlainCharType | ||
| else result = this.getStandardType() | ||
| ) | ||
| } | ||
| } | ||
| class EssentialEnumConstantAccess extends EssentialExpr, EnumConstantAccess { | ||
| override Type getEssentialType() { result = getTarget().getDeclaringEnum() } | ||
| } | ||
| class EssentialLiteral extends EssentialExpr, Literal { | ||
| override Type getEssentialType() { | ||
| if this instanceof BooleanLiteral | ||
| then result instanceof MisraBoolType | ||
| else ( | ||
| if this.(CharLiteral).getCharacter().length() = 1 | ||
| then result instanceof PlainCharType | ||
| else ( | ||
| getStandardType().(IntegralType).isSigned() and | ||
| result = stlr(this) | ||
| or | ||
| not getStandardType().(IntegralType).isSigned() and | ||
| result = utlr(this) | ||
| ) | ||
| ) | ||
| } | ||
| } | ||
| /** | ||
| * Holds if `rValue` is assigned to an object of type `lValueEssentialType`. | ||
| * | ||
| * Assignment is according to "Assignment" in Appendix J of MISRA C 2012, with the inclusion of a | ||
| * special case for switch statements as specified for Rule 10.3 and Rule 10.6. | ||
| */ | ||
| predicate isAssignmentToEssentialType(Type lValueEssentialType, Expr rValue) { | ||
| // Special case for Rule 10.3/ Rule 10.6. | ||
| exists(SwitchCase sc | | ||
| lValueEssentialType = sc.getSwitchStmt().getControllingExpr().getType() and | ||
| rValue = sc.getExpr() | ||
| ) | ||
| or | ||
| exists(Assignment a | | ||
| lValueEssentialType = a.getLValue().getType() and | ||
| rValue = a.getRValue() | ||
| ) | ||
| or | ||
| exists(FunctionCall fc, int i | | ||
| lValueEssentialType = fc.getTarget().getParameter(i).getType() and | ||
| rValue = fc.getArgument(i) | ||
| ) | ||
| or | ||
| exists(Function f, ReturnStmt rs | | ||
| lValueEssentialType = f.getType() and | ||
| rs.getEnclosingFunction() = f and | ||
| rValue = rs.getExpr() | ||
| ) | ||
| or | ||
| // Initializing a non-aggregate type | ||
| exists(Initializer i | | ||
| lValueEssentialType = i.getDeclaration().(Variable).getType() and | ||
| rValue = i.getExpr() | ||
| ) | ||
| or | ||
| // Initializing an array | ||
| exists(ArrayAggregateLiteral aal | | ||
| lValueEssentialType = aal.getElementType() and | ||
| rValue = aal.getElementExpr(_) | ||
| ) | ||
| or | ||
| // Initializing a struct or union | ||
| exists(ClassAggregateLiteral cal, Field field | | ||
| lValueEssentialType = field.getType() and | ||
| rValue = cal.getFieldExpr(field) | ||
| ) | ||
| } |
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.