@@ -352,7 +352,6 @@ bool AvarBoundsInference::getReachableBoundKeys(const ProgramVarScope *DstScope,
352
352
353
353
void AvarBoundsInference::getRelevantBounds (BoundsKey BK,
354
354
BndsKindMap &ResBounds) {
355
- // Try to get the bounds of all RBKeys.
356
355
if (CurrIterInferBounds.find (BK) != CurrIterInferBounds.end ()) {
357
356
// get the bounds inferred from the current iteration
358
357
ResBounds = CurrIterInferBounds[BK];
@@ -812,8 +811,11 @@ BoundsKey AVarBoundsInfo::getVariable(clang::VarDecl *VD) {
812
811
auto *PVar =
813
812
ProgramVar::createNewProgramVar (NK, VD->getNameAsString (), PVS);
814
813
insertProgramVar (NK, PVar);
815
- if (isPtrOrArrayType (VD->getType ()))
814
+ if (isPtrOrArrayType (VD->getType ())) {
816
815
PointerBoundsKey.insert (NK);
816
+ if (!VD->isLocalVarDeclOrParm ())
817
+ IneligibleForRangeBounds.insert (NK);
818
+ }
817
819
}
818
820
return getVarKey (PSL);
819
821
}
@@ -876,8 +878,10 @@ BoundsKey AVarBoundsInfo::getVariable(clang::FieldDecl *FD) {
876
878
const StructScope *SS = StructScope::getStructScope (StName);
877
879
auto *PVar = ProgramVar::createNewProgramVar (NK, FD->getNameAsString (), SS);
878
880
insertProgramVar (NK, PVar);
879
- if (isPtrOrArrayType (FD->getType ()))
881
+ if (isPtrOrArrayType (FD->getType ())) {
880
882
PointerBoundsKey.insert (NK);
883
+ IneligibleForRangeBounds.insert (NK);
884
+ }
881
885
}
882
886
return getVarKey (PSL);
883
887
}
@@ -938,7 +942,8 @@ void AVarBoundsInfo::addAssignment(BoundsKey L, BoundsKey R) {
938
942
// pointer. For future work, all bounds "down stream" of pointer arithmetic
939
943
// could also use range bounds using the same base pointer.
940
944
auto AddEdgeUnlessPointerArithmetic = [this ](BoundsKey From, BoundsKey To) {
941
- if (!hasPointerArithmetic (From))
945
+ if (!hasPointerArithmetic (From) &&
946
+ (!hasPointerArithmetic (To) || canInferRangeBounds (To)))
942
947
ProgVarGraph.addUniqueEdge (From, To);
943
948
};
944
949
@@ -987,10 +992,17 @@ bool AVarBoundsInfo::hasPointerArithmetic(BoundsKey BK) {
987
992
return ArrPointersWithArithmetic.find (BK) != ArrPointersWithArithmetic.end ();
988
993
}
989
994
990
- // A pointer needs range bounds if it is computed by pointer arithmetic and
991
- // would otherwise need bounds.
995
+ bool AVarBoundsInfo::canInferRangeBounds (BoundsKey BK) {
996
+ return IneligibleForRangeBounds.find (BK) == IneligibleForRangeBounds.end ();
997
+ }
998
+
992
999
bool AVarBoundsInfo::needsRangeBound (BoundsKey BK) {
993
- return hasPointerArithmetic (BK) && getBounds (BK) != nullptr ;
1000
+ // A pointer should get range bounds if it is computed by pointer arithmetic
1001
+ // and would otherwise need bounds. Some pointers (global variables and struct
1002
+ // fields) can't be rewritten to use range bounds (by 3C; Checked C does
1003
+ // permit it), so we return false on these.
1004
+ return hasPointerArithmetic (BK) && canInferRangeBounds (BK) &&
1005
+ getBounds (BK) != nullptr ;
994
1006
}
995
1007
996
1008
ProgramVar *AVarBoundsInfo::getProgramVar (BoundsKey VK) {
0 commit comments