Skip to content

Ruby/Python: Clear call contexts after jump steps in type tracking #8317

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ private module Cached {
CallStep() or
ReturnStep() or
StoreStep(ContentName content) or
LoadStep(ContentName content)
LoadStep(ContentName content) or
JumpStep()

/** Gets the summary resulting from appending `step` to type-tracking summary `tt`. */
cached
Expand All @@ -49,6 +50,9 @@ private module Cached {
step = LoadStep(content) and result = MkTypeTracker(hasCall, "")
or
exists(string p | step = StoreStep(p) and content = "" and result = MkTypeTracker(hasCall, p))
or
step = JumpStep() and
result = MkTypeTracker(false, content)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at the JavaScript implementation of TypeTracker.append I see that none of the steps ever clear the hasCall property and vice-versa for hasReturn in TypeBackTracker.prepend. @asgerf , what do you think?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

None of our type-tracking steps are jump-steps. No fundamental reason not to have them, we just haven't added them.

Note we handle captured variables differently; they are not modelled as jump steps (which is too imprecise for JS code). Also, when writing a type-tracking predicate, you may choose to manually reset the calling context using t.start() (I'm working on a doc describing the importance of this in more detail). Perhaps this is why it never seemed important for JS.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@asgerf Thanks for the explanation. Just wanted to be sure that there wasn't a fundamental reason why resetting the hasCall/hasReturn flags would be bad.

Copy link
Contributor Author

@hvitved hvitved Mar 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, when writing a type-tracking predicate, you may choose to manually reset the calling context using t.start()

Doesn't that also clear whether or not data is stored in a property?

)
}

Expand All @@ -67,6 +71,9 @@ private module Cached {
)
or
step = StoreStep(content) and result = MkTypeBackTracker(hasReturn, "")
or
step = JumpStep() and
result = MkTypeBackTracker(false, content)
)
}

Expand Down Expand Up @@ -110,12 +117,17 @@ class StepSummary extends TStepSummary {
exists(string content | this = StoreStep(content) | result = "store " + content)
or
exists(string content | this = LoadStep(content) | result = "load " + content)
or
this instanceof JumpStep and result = "jump"
}
}

pragma[noinline]
private predicate smallstepNoCall(Node nodeFrom, TypeTrackingNode nodeTo, StepSummary summary) {
jumpStep(nodeFrom, nodeTo) and
summary = JumpStep()
or
levelStep(nodeFrom, nodeTo) and
summary = LevelStep()
or
exists(string content |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ predicate simpleLocalFlowStep = DataFlowPrivate::simpleLocalFlowStep/2;

predicate jumpStep = DataFlowPrivate::jumpStep/2;

/** Holds if there is a level step from `pred` to `succ`. */
predicate levelStep(Node pred, Node succ) { none() }

/**
* Gets the name of a possible piece of content. For Python, this is currently only attribute names,
* using the name of the attribute for the corresponding content.
Expand Down
14 changes: 13 additions & 1 deletion ruby/ql/lib/codeql/ruby/typetracking/TypeTracker.qll
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ private module Cached {
CallStep() or
ReturnStep() or
StoreStep(ContentName content) or
LoadStep(ContentName content)
LoadStep(ContentName content) or
JumpStep()

/** Gets the summary resulting from appending `step` to type-tracking summary `tt`. */
cached
Expand All @@ -49,6 +50,9 @@ private module Cached {
step = LoadStep(content) and result = MkTypeTracker(hasCall, "")
or
exists(string p | step = StoreStep(p) and content = "" and result = MkTypeTracker(hasCall, p))
or
step = JumpStep() and
result = MkTypeTracker(false, content)
)
}

Expand All @@ -67,6 +71,9 @@ private module Cached {
)
or
step = StoreStep(content) and result = MkTypeBackTracker(hasReturn, "")
or
step = JumpStep() and
result = MkTypeBackTracker(false, content)
)
}

Expand Down Expand Up @@ -110,12 +117,17 @@ class StepSummary extends TStepSummary {
exists(string content | this = StoreStep(content) | result = "store " + content)
or
exists(string content | this = LoadStep(content) | result = "load " + content)
or
this instanceof JumpStep and result = "jump"
}
}

pragma[noinline]
private predicate smallstepNoCall(Node nodeFrom, TypeTrackingNode nodeTo, StepSummary summary) {
jumpStep(nodeFrom, nodeTo) and
summary = JumpStep()
or
levelStep(nodeFrom, nodeTo) and
summary = LevelStep()
or
exists(string content |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ predicate simpleLocalFlowStep = DataFlowPrivate::localFlowStepTypeTracker/2;

predicate jumpStep = DataFlowPrivate::jumpStep/2;

/** Holds if there is a level step from `pred` to `succ`. */
predicate levelStep(Node pred, Node succ) { none() }

/**
* Gets the name of a possible piece of content. This will usually include things like
*
Expand Down