@@ -3,6 +3,7 @@ import semmle.code.cpp.models.interfaces.Taint
33import semmle.code.cpp.models.interfaces.Alias
44import semmle.code.cpp.models.interfaces.SideEffect
55
6+ /** Pure string functions. */
67class PureStrFunction extends AliasFunction , ArrayFunction , TaintFunction , SideEffectFunction {
78 PureStrFunction ( ) {
89 hasGlobalOrStdName ( [
@@ -58,6 +59,7 @@ class PureStrFunction extends AliasFunction, ArrayFunction, TaintFunction, SideE
5859 }
5960}
6061
62+ /** String standard `strlen` function, and related functions for computing string lengths. */
6163class StrLenFunction extends AliasFunction , ArrayFunction , SideEffectFunction {
6264 StrLenFunction ( ) {
6365 hasGlobalOrStdName ( [ "strlen" , "strnlen" , "wcslen" ] )
@@ -91,6 +93,7 @@ class StrLenFunction extends AliasFunction, ArrayFunction, SideEffectFunction {
9193 }
9294}
9395
96+ /** Pure functions. */
9497class PureFunction extends TaintFunction , SideEffectFunction {
9598 PureFunction ( ) { hasGlobalOrStdName ( [ "abs" , "labs" ] ) }
9699
@@ -106,3 +109,49 @@ class PureFunction extends TaintFunction, SideEffectFunction {
106109
107110 override predicate hasOnlySpecificWriteSideEffects ( ) { any ( ) }
108111}
112+
113+ /** Pure raw-memory functions. */
114+ class PureMemFunction extends AliasFunction , ArrayFunction , TaintFunction , SideEffectFunction {
115+ PureMemFunction ( ) { hasGlobalOrStdName ( [ "memchr" , "memrchr" , "rawmemchr" , "memcmp" , "memmem" ] ) }
116+
117+ override predicate hasArrayInput ( int bufParam ) {
118+ getParameter ( bufParam ) .getUnspecifiedType ( ) instanceof PointerType
119+ }
120+
121+ override predicate hasTaintFlow ( FunctionInput input , FunctionOutput output ) {
122+ exists ( ParameterIndex i |
123+ input .isParameter ( i ) and
124+ exists ( getParameter ( i ) )
125+ or
126+ input .isParameterDeref ( i ) and
127+ getParameter ( i ) .getUnspecifiedType ( ) instanceof PointerType
128+ ) and
129+ (
130+ output .isReturnValueDeref ( ) and
131+ getUnspecifiedType ( ) instanceof PointerType
132+ or
133+ output .isReturnValue ( )
134+ )
135+ }
136+
137+ override predicate parameterNeverEscapes ( int i ) {
138+ getParameter ( i ) .getUnspecifiedType ( ) instanceof PointerType and
139+ not parameterEscapesOnlyViaReturn ( i )
140+ }
141+
142+ override predicate parameterEscapesOnlyViaReturn ( int i ) {
143+ i = 0 and
144+ getUnspecifiedType ( ) instanceof PointerType
145+ }
146+
147+ override predicate parameterIsAlwaysReturned ( int i ) { none ( ) }
148+
149+ override predicate hasOnlySpecificReadSideEffects ( ) { any ( ) }
150+
151+ override predicate hasOnlySpecificWriteSideEffects ( ) { any ( ) }
152+
153+ override predicate hasSpecificReadSideEffect ( ParameterIndex i , boolean buffer ) {
154+ getParameter ( i ) .getUnspecifiedType ( ) instanceof PointerType and
155+ buffer = true
156+ }
157+ }
0 commit comments