Commit 35e0a28a authored by Nadim Kobeissi's avatar Nadim Kobeissi 💾
Browse files

Cleanup

parent d93536ce
Pipeline #811 passed with stages
in 1 minute and 20 seconds
...@@ -49,7 +49,7 @@ func injectConstantRules(c *Constant, arg int, p *Primitive) bool { ...@@ -49,7 +49,7 @@ func injectConstantRules(c *Constant, arg int, p *Primitive) bool {
switch { switch {
case p.Arguments[arg].Kind != typesEnumConstant: case p.Arguments[arg].Kind != typesEnumConstant:
return false return false
case c.ID == valueG.Data.(*Constant).ID: case valueEquivalentConstants(c, valueG.Data.(*Constant)):
return false return false
} }
return true return true
......
...@@ -19,7 +19,6 @@ func mutationMapInit( ...@@ -19,7 +19,6 @@ func mutationMapInit(
Mutations: [][]*Value{}, Mutations: [][]*Value{},
Combination: []*Value{}, Combination: []*Value{},
DepthIndex: []int{}, DepthIndex: []int{},
Worthwhile: false,
OutOfMutations: false, OutOfMutations: false,
} }
InfoMessage(fmt.Sprintf( InfoMessage(fmt.Sprintf(
...@@ -206,15 +205,11 @@ func mutationMapReplaceEquation( ...@@ -206,15 +205,11 @@ func mutationMapReplaceEquation(
} }
func mutationMapNext(valMutationMap MutationMap) MutationMap { func mutationMapNext(valMutationMap MutationMap) MutationMap {
valMutationMap.Worthwhile = false
if len(valMutationMap.Combination) == 0 { if len(valMutationMap.Combination) == 0 {
valMutationMap.OutOfMutations = true valMutationMap.OutOfMutations = true
return valMutationMap return valMutationMap
} }
for i := 0; i < len(valMutationMap.Combination); i++ { for i := 0; i < len(valMutationMap.Combination); i++ {
if i >= valMutationMap.LastIncrement {
valMutationMap.Worthwhile = true
}
valMutationMap.Combination[i] = valMutationMap.Mutations[i][valMutationMap.DepthIndex[i]] valMutationMap.Combination[i] = valMutationMap.Mutations[i][valMutationMap.DepthIndex[i]]
if i != len(valMutationMap.Combination)-1 { if i != len(valMutationMap.Combination)-1 {
continue continue
......
...@@ -15,8 +15,8 @@ func possibleToDecomposePrimitive( ...@@ -15,8 +15,8 @@ func possibleToDecomposePrimitive(
if !prim.Decompose.HasRule { if !prim.Decompose.HasRule {
return false, &Value{}, has return false, &Value{}, has
} }
for i, g := range prim.Decompose.Given { for i := 0; i < len(prim.Decompose.Given); i++ {
a := p.Arguments[g] a := p.Arguments[prim.Decompose.Given[i]]
a, valid := prim.Decompose.Filter(p, a, i) a, valid := prim.Decompose.Filter(p, a, i)
if !valid { if !valid {
continue continue
......
...@@ -60,7 +60,7 @@ func sanityAssignmentConstants( ...@@ -60,7 +60,7 @@ func sanityAssignmentConstants(
case typesEnumConstant: case typesEnumConstant:
unique := true unique := true
for _, c := range constants { for _, c := range constants {
if right.Data.(*Constant).ID == c.ID { if valueEquivalentConstants(right.Data.(*Constant), c) {
unique = false unique = false
break break
} }
...@@ -106,7 +106,7 @@ func sanityAssignmentConstantsFromPrimitive( ...@@ -106,7 +106,7 @@ func sanityAssignmentConstantsFromPrimitive(
case typesEnumConstant: case typesEnumConstant:
unique := true unique := true
for _, c := range constants { for _, c := range constants {
if a.Data.(*Constant).ID == c.ID { if valueEquivalentConstants(a.Data.(*Constant), c) {
unique = false unique = false
break break
} }
...@@ -133,7 +133,7 @@ func sanityAssignmentConstantsFromEquation(right *Value, constants []*Constant) ...@@ -133,7 +133,7 @@ func sanityAssignmentConstantsFromEquation(right *Value, constants []*Constant)
for _, v := range right.Data.(*Equation).Values { for _, v := range right.Data.(*Equation).Values {
unique := true unique := true
for _, c := range constants { for _, c := range constants {
if v.Data.(*Constant).ID == c.ID { if valueEquivalentConstants(v.Data.(*Constant), c) {
unique = false unique = false
break break
} }
...@@ -431,7 +431,7 @@ func sanityCheckEquationRootGenerator(e *Equation) error { ...@@ -431,7 +431,7 @@ func sanityCheckEquationRootGenerator(e *Equation) error {
} }
} }
if i > 0 { if i > 0 {
if c.Data.(*Constant).ID == valueG.Data.(*Constant).ID { if valueEquivalentConstants(c.Data.(*Constant), valueG.Data.(*Constant)) {
return fmt.Errorf( return fmt.Errorf(
"equation (%s) uses 'g' not as a generator", "equation (%s) uses 'g' not as a generator",
prettyEquation(e), prettyEquation(e),
......
...@@ -325,7 +325,6 @@ type MutationMap struct { ...@@ -325,7 +325,6 @@ type MutationMap struct {
Constants []*Constant Constants []*Constant
Mutations [][]*Value Mutations [][]*Value
Combination []*Value Combination []*Value
Worthwhile bool
DepthIndex []int DepthIndex []int
} }
......
...@@ -75,7 +75,7 @@ func valueIsGOrNil(c *Constant) bool { ...@@ -75,7 +75,7 @@ func valueIsGOrNil(c *Constant) bool {
func valueGetKnowledgeMapIndexFromConstant(valKnowledgeMap *KnowledgeMap, c *Constant) int { func valueGetKnowledgeMapIndexFromConstant(valKnowledgeMap *KnowledgeMap, c *Constant) int {
for i := range valKnowledgeMap.Constants { for i := range valKnowledgeMap.Constants {
if valKnowledgeMap.Constants[i].ID == c.ID { if valueEquivalentConstants(valKnowledgeMap.Constants[i], c) {
return i return i
} }
} }
...@@ -84,7 +84,7 @@ func valueGetKnowledgeMapIndexFromConstant(valKnowledgeMap *KnowledgeMap, c *Con ...@@ -84,7 +84,7 @@ func valueGetKnowledgeMapIndexFromConstant(valKnowledgeMap *KnowledgeMap, c *Con
func valueGetPrincipalStateIndexFromConstant(valPrincipalState *PrincipalState, c *Constant) int { func valueGetPrincipalStateIndexFromConstant(valPrincipalState *PrincipalState, c *Constant) int {
for i := range valPrincipalState.Constants { for i := range valPrincipalState.Constants {
if valPrincipalState.Constants[i].ID == c.ID { if valueEquivalentConstants(valPrincipalState.Constants[i], c) {
return i return i
} }
} }
...@@ -140,7 +140,7 @@ func valueEquivalentValues(a1 *Value, a2 *Value, considerOutput bool) bool { ...@@ -140,7 +140,7 @@ func valueEquivalentValues(a1 *Value, a2 *Value, considerOutput bool) bool {
} }
switch a1.Kind { switch a1.Kind {
case typesEnumConstant: case typesEnumConstant:
return a1.Data.(*Constant).ID == a2.Data.(*Constant).ID return valueEquivalentConstants(a1.Data.(*Constant), a2.Data.(*Constant))
case typesEnumPrimitive: case typesEnumPrimitive:
equivPrim, _, _ := valueEquivalentPrimitives( equivPrim, _, _ := valueEquivalentPrimitives(
a1.Data.(*Primitive), a2.Data.(*Primitive), considerOutput, a1.Data.(*Primitive), a2.Data.(*Primitive), considerOutput,
...@@ -154,6 +154,10 @@ func valueEquivalentValues(a1 *Value, a2 *Value, considerOutput bool) bool { ...@@ -154,6 +154,10 @@ func valueEquivalentValues(a1 *Value, a2 *Value, considerOutput bool) bool {
return false return false
} }
func valueEquivalentConstants(c1 *Constant, c2 *Constant) bool {
return c1.ID == c2.ID
}
func valueEquivalentPrimitives( func valueEquivalentPrimitives(
p1 *Primitive, p2 *Primitive, considerOutput bool, p1 *Primitive, p2 *Primitive, considerOutput bool,
) (bool, int, int) { ) (bool, int, int) {
...@@ -217,6 +221,15 @@ func valueEquivalentValueInValues(v *Value, a []*Value) int { ...@@ -217,6 +221,15 @@ func valueEquivalentValueInValues(v *Value, a []*Value) int {
return -1 return -1
} }
func valueEquivalentConstantInConstants(c *Constant, a []*Constant) int {
for i := 0; i < len(a); i++ {
if valueEquivalentConstants(c, a[i]) {
return i
}
}
return -1
}
func valuePerformPrimitiveRewrite( func valuePerformPrimitiveRewrite(
p *Primitive, pi int, valPrincipalState *PrincipalState, p *Primitive, pi int, valPrincipalState *PrincipalState,
) ([]*Primitive, bool, *Value) { ) ([]*Primitive, bool, *Value) {
......
...@@ -90,10 +90,10 @@ func verifyActiveScan( ...@@ -90,10 +90,10 @@ func verifyActiveScan(
scanGroup.Done() scanGroup.Done()
return err return err
} }
if valMutationMap.Worthwhile { valPrincipalStateMutated, isWorthwhileMutation := verifyActiveMutatePrincipalState(
valPrincipalStateMutated := verifyActiveMutatePrincipalState( constructPrincipalStateClone(valPrincipalState, true), valAttackerState, valMutationMap,
constructPrincipalStateClone(valPrincipalState, true), valAttackerState, valMutationMap, )
) if isWorthwhileMutation {
scanGroup.Add(1) scanGroup.Add(1)
go func() { go func() {
err = verifyAnalysis( err = verifyAnalysis(
...@@ -123,7 +123,8 @@ func verifyActiveScan( ...@@ -123,7 +123,8 @@ func verifyActiveScan(
func verifyActiveMutatePrincipalState( func verifyActiveMutatePrincipalState(
valPrincipalState *PrincipalState, valAttackerState AttackerState, valMutationMap MutationMap, valPrincipalState *PrincipalState, valAttackerState AttackerState, valMutationMap MutationMap,
) *PrincipalState { ) (*PrincipalState, bool) {
isWorthwhileMutation := false
MutationLoop: MutationLoop:
for i := 0; i < len(valMutationMap.Constants); i++ { for i := 0; i < len(valMutationMap.Constants); i++ {
ai, ii := valueResolveConstant(valMutationMap.Constants[i], valPrincipalState) ai, ii := valueResolveConstant(valMutationMap.Constants[i], valPrincipalState)
...@@ -131,31 +132,17 @@ MutationLoop: ...@@ -131,31 +132,17 @@ MutationLoop:
ar, _ := valueResolveValueInternalValuesFromPrincipalState( ar, _ := valueResolveValueInternalValuesFromPrincipalState(
ai, ai, ii, valPrincipalState, valAttackerState, true, ai, ai, ii, valPrincipalState, valAttackerState, true,
) )
switch ar.Kind {
case typesEnumPrimitive:
_, aar := possibleToRewrite(ar.Data.(*Primitive), valPrincipalState)
switch aar[0].Kind {
case typesEnumPrimitive:
ar.Data = aar[0].Data.(*Primitive)
}
}
switch ac.Kind { switch ac.Kind {
case typesEnumPrimitive: case typesEnumPrimitive:
_, aac := possibleToRewrite(ac.Data.(*Primitive), valPrincipalState)
switch aac[0].Kind {
case typesEnumPrimitive:
ac.Data = aac[0].Data.(*Primitive)
}
switch ar.Kind { switch ar.Kind {
case typesEnumPrimitive: case typesEnumPrimitive:
ac.Data.(*Primitive).Output = ar.Data.(*Primitive).Output ac.Data.(*Primitive).Output = ar.Data.(*Primitive).Output
ac.Data.(*Primitive).Check = ar.Data.(*Primitive).Check ac.Data.(*Primitive).Check = ar.Data.(*Primitive).Check
} }
acc := valueGetConstantsFromValue(ac) if valueEquivalentConstantInConstants(
for acci := 0; acci < len(acc); acci++ { valMutationMap.Constants[i], valueGetConstantsFromValue(ac),
if acc[acci].ID == valMutationMap.Constants[i].ID { ) >= 0 {
continue MutationLoop continue MutationLoop
}
} }
} }
switch { switch {
...@@ -167,6 +154,12 @@ MutationLoop: ...@@ -167,6 +154,12 @@ MutationLoop:
valPrincipalState.Mutated[ii] = true valPrincipalState.Mutated[ii] = true
valPrincipalState.Assigned[ii] = ac valPrincipalState.Assigned[ii] = ac
valPrincipalState.BeforeRewrite[ii] = ac valPrincipalState.BeforeRewrite[ii] = ac
if i >= valMutationMap.LastIncrement {
isWorthwhileMutation = true
}
}
if !isWorthwhileMutation {
return valPrincipalState, isWorthwhileMutation
} }
valPrincipalState, _ = valueResolveAllPrincipalStateValues(valPrincipalState, valAttackerState) valPrincipalState, _ = valueResolveAllPrincipalStateValues(valPrincipalState, valAttackerState)
failedRewrites, failedRewriteIndices, valPrincipalState := valuePerformAllRewrites(valPrincipalState) failedRewrites, failedRewriteIndices, valPrincipalState := valuePerformAllRewrites(valPrincipalState)
...@@ -194,7 +187,7 @@ FailedRewritesLoop: ...@@ -194,7 +187,7 @@ FailedRewritesLoop:
} }
} }
} }
return valPrincipalState return valPrincipalState, isWorthwhileMutation
} }
func verifyActiveDropPrincipalStateAfterIndex(valPrincipalState *PrincipalState, f int) *PrincipalState { func verifyActiveDropPrincipalStateAfterIndex(valPrincipalState *PrincipalState, f int) *PrincipalState {
......
...@@ -20,4 +20,4 @@ principal Bob[ ...@@ -20,4 +20,4 @@ principal Bob[
queries[ queries[
confidentiality? m confidentiality? m
] ]
\ No newline at end of file
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