Commit 04dccc92 authored by aniket-engg's avatar aniket-engg Committed by Aniket

method updates

parent e9ac30cb
......@@ -3,7 +3,7 @@ import { getStateVariableDeclarationsFormContractNode,
getFunctionOrModifierDefinitionParameterPart, getType, getDeclaredVariableName,
getFunctionDefinitionReturnParameterPart } from './staticAnalysisCommon'
import { AstWalker } from 'remix-astwalker'
import { CommonAstNode, FunctionDefinitionAstNode, ParameterListAstNode } from 'types'
import { CommonAstNode, FunctionDefinitionAstNode, ParameterListAstNode, ModifierDefinitionAstNode } from 'types'
export default class abstractAstView {
contracts = []
......@@ -159,11 +159,11 @@ export default class abstractAstView {
return that.getCurrentContract(that).modifiers[that.currentModifierIndex]
}
private getLocalParameters (funcNode) {
private getLocalParameters (funcNode: FunctionDefinitionAstNode | ModifierDefinitionAstNode) {
return getFunctionOrModifierDefinitionParameterPart(funcNode).parameters.map(getType)
}
private getReturnParameters (funcNode) {
private getReturnParameters (funcNode: FunctionDefinitionAstNode) {
return this.getLocalVariables(getFunctionDefinitionReturnParameterPart(funcNode)).map((n) => {
return {
type: getType(n),
......
......@@ -17,7 +17,7 @@ export default class constantFunctions implements AnalyzerModule {
abstractAst: AbstractAst = new AbstractAst()
visit = this.abstractAst.build_visit(
(node: CommonAstNode) => isLowLevelCall(node) ||
(node: any) => isLowLevelCall(node) ||
isTransfer(node) ||
isExternalDirectCall(node) ||
isEffect(node) ||
......
import { default as category } from './categories'
import { isDeleteOfDynamicArray } from './staticAnalysisCommon'
import { default as algorithm } from './algorithmCategories'
import { AnalyzerModule, ModuleAlgorithm, ModuleCategory, ReportObj, AstNodeLegacy, CompilationResult} from './../../types'
import { AnalyzerModule, ModuleAlgorithm, ModuleCategory, ReportObj, CompilationResult, UnaryOperationAstNode} from './../../types'
export default class deleteDynamicArrays implements AnalyzerModule {
rel: AstNodeLegacy[] = []
rel: UnaryOperationAstNode[] = []
name: string = 'Delete on dynamic Array: '
description: string = 'Use require and appropriately'
category: ModuleCategory = category.GAS
algorithm: ModuleAlgorithm = algorithm.EXACT
visit (node: AstNodeLegacy): void {
visit (node: UnaryOperationAstNode): void {
if (isDeleteOfDynamicArray(node)) this.rel.push(node)
}
......
import { default as category } from './categories'
import { default as algorithm } from './algorithmCategories'
import { isForLoop, isDynamicArrayLengthAccess, isBinaryOperation } from './staticAnalysisCommon'
import { AnalyzerModule, ModuleAlgorithm, ModuleCategory, ReportObj, AstNodeLegacy, CompilationResult, CommonAstNode} from './../../types'
import { isDynamicArrayLengthAccess } from './staticAnalysisCommon'
import { AnalyzerModule, ModuleAlgorithm, ModuleCategory, ReportObj, CompilationResult, ForStatementAstNode} from './../../types'
export default class forLoopIteratesOverDynamicArray implements AnalyzerModule {
relevantNodes: CommonAstNode[] = []
relevantNodes: ForStatementAstNode[] = []
name: string = 'For loop iterates over dynamic array: '
description: string = 'The number of \'for\' loop iterations depends on dynamic array\'s size'
category: ModuleCategory = category.GAS
algorithm: ModuleAlgorithm = algorithm.EXACT
visit (node: CommonAstNode): void {
if (node.nodeType === "Forstatement" && node.children) {
let conditionChildrenNode: AstNodeLegacy | null = null
// Access 'condition' node of 'for' loop statement
const forLoopConditionNode: AstNodeLegacy = node.children[1]
// Access right side of condition as its children
if(forLoopConditionNode && forLoopConditionNode.children){
conditionChildrenNode = forLoopConditionNode.children[1]
}
// Check if it is a binary operation. if yes, check if its children node access length of dynamic array
if (conditionChildrenNode && conditionChildrenNode.children && isBinaryOperation(conditionChildrenNode) && isDynamicArrayLengthAccess(conditionChildrenNode.children[0])) {
this.relevantNodes.push(node)
} else if (isDynamicArrayLengthAccess(conditionChildrenNode)) { // else check if condition node itself access length of dynamic array
visit (node: ForStatementAstNode): void {
const { condition } = node
// Check if condition is `i < array.length - 1`
if ((condition.nodeType === "BinaryOperation" && condition.rightExpression.nodeType === "BinaryOperation" && isDynamicArrayLengthAccess(condition.rightExpression.leftExpression)) ||
// or condition is `i < array.length`
(condition.nodeType === "BinaryOperation" && isDynamicArrayLengthAccess(condition.rightExpression))) {
this.relevantNodes.push(node)
}
}
}
report (compilationResults: CompilationResult): ReportObj[] {
......
import { default as category } from './categories'
import { isIntDivision } from './staticAnalysisCommon'
import { default as algorithm } from './algorithmCategories'
import { AnalyzerModule, ModuleAlgorithm, ModuleCategory, ReportObj, AstNodeLegacy, CompilationResult} from './../../types'
import { AnalyzerModule, ModuleAlgorithm, ModuleCategory, ReportObj, CompilationResult, BinaryOperationAstNode} from './../../types'
export default class intDivisionTruncate implements AnalyzerModule {
warningNodes: AstNodeLegacy[] = []
warningNodes: BinaryOperationAstNode[] = []
name: string = 'Data Truncated: '
description: string = 'Division on int/uint values truncates the result.'
category: ModuleCategory = category.MISC
algorithm: ModuleAlgorithm = algorithm.EXACT
visit (node: AstNodeLegacy): void {
visit (node: BinaryOperationAstNode): void {
if (isIntDivision(node)) this.warningNodes.push(node)
}
......
......@@ -2,10 +2,10 @@ import { default as category } from './categories'
import { isLowLevelCallInst, isLowLevelCallInst050, isLowLevelCallcodeInst, isLowLevelDelegatecallInst,
isLowLevelSendInst, isLowLevelSendInst050, isLLDelegatecallInst050, lowLevelCallTypes } from './staticAnalysisCommon'
import { default as algorithm } from './algorithmCategories'
import { AnalyzerModule, ModuleAlgorithm, ModuleCategory, ReportObj, AstNodeLegacy, CompilationResult} from './../../types'
import { AnalyzerModule, ModuleAlgorithm, ModuleCategory, ReportObj, CompilationResult, MemberAccessAstNode} from './../../types'
interface llcNode {
node: AstNodeLegacy
node: MemberAccessAstNode
type: {
ident: string,
type: string
......@@ -19,7 +19,7 @@ export default class lowLevelCalls implements AnalyzerModule {
category: ModuleCategory = category.SECURITY
algorithm: ModuleAlgorithm = algorithm.EXACT
visit (node : AstNodeLegacy): void {
visit (node : MemberAccessAstNode): void {
if (isLowLevelCallInst(node)) {
this.llcNodes.push({node: node, type: lowLevelCallTypes.CALL})
} else if (isLowLevelCallInst050(node)) {
......
import { default as category } from './categories'
import { isThisLocalCall } from './staticAnalysisCommon'
import { default as algorithm } from './algorithmCategories'
import { AnalyzerModule, ModuleAlgorithm, ModuleCategory, ReportObj, AstNodeLegacy, CompilationResult} from './../../types'
import { AnalyzerModule, ModuleAlgorithm, ModuleCategory, ReportObj, AstNodeLegacy, CompilationResult, MemberAccessAstNode} from './../../types'
export default class thisLocal implements AnalyzerModule {
warningNodes: AstNodeLegacy[] = []
warningNodes: MemberAccessAstNode[] = []
name: string = 'This on local calls: '
description: string = 'Invocation of local functions via this'
category: ModuleCategory = category.GAS
algorithm: ModuleAlgorithm = algorithm.EXACT
visit (node: AstNodeLegacy): void {
visit (node: MemberAccessAstNode): void {
if (isThisLocalCall(node)) this.warningNodes.push(node)
}
......
......@@ -337,7 +337,7 @@ export interface WhileStatementAstNode {
id: number
nodeType: 'WhileStatement' | 'DoWhileStatement'
src: string
condition: object
condition: any
body: BlockAstNode
}
......@@ -346,7 +346,7 @@ export interface ForStatementAstNode {
nodeType: 'ForStatement'
src: string
initializationExpression: VariableDeclarationStatementAstNode
condition: object
condition: any
loopExpression: ExpressionStatementAstNode
body: BlockAstNode
}
......@@ -397,7 +397,7 @@ export interface ExpressionStatementAstNode {
id: number
nodeType: 'ExpressionStatement'
src: string
expression: object
expression: any
}
interface ExpressionAttributes {
......@@ -441,7 +441,7 @@ export interface UnaryOperationAstNode extends ExpressionAttributes {
src: string
prefix: boolean
operator: string
subExpression: object
subExpression: any
}
export interface BinaryOperationAstNode extends ExpressionAttributes {
......@@ -458,7 +458,7 @@ export interface FunctionCallAstNode extends ExpressionAttributes {
id: number
nodeType: 'FunctionCall'
src: string
expression: object
expression: any
names: Array<any>
arguments: object
tryCall: boolean
......@@ -481,10 +481,8 @@ export interface NewExpressionAstNode extends ExpressionAttributes {
typeName: UserDefinedTypeNameAstNode | ElementaryTypeNameAstNode
}
export interface MemberAccessAstNode extends ExpressionAttributes {
id: number
export interface MemberAccessAstNode extends CommonAstNode, ExpressionAttributes {
nodeType: 'MemberAccess'
src: string
memberName: string
expression: object
referencedDeclaration: number | null
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment