Skip to content
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

[Bugfix] Add check to avoid calling back() on an empty container #8930

Merged
merged 5 commits into from
Sep 7, 2021

Conversation

syang-ng
Copy link
Contributor

@syang-ng syang-ng commented Sep 5, 2021

This is related to an issue here.

In a word, function PrimExpr VisitExpr_(const LoadNode* op) forgets to check the size of the vector scope_[op->buffer_var.get()], while calling std::vector::back on an empty container will cause undefined behavior.

This PR fixed this problem by adding the vector size check before calling back().

To reproduce the bug:

from tvm import tir
import tvm

zero = tir.const(0)
nop = tir.Evaluate(zero)
v = tir.Var('i1', 'int32')
for_stmt = tir.For(v, zero, zero, tir.ForKind.SERIAL, nop)
load = tir.Evaluate(tir.Load('int32', v, zero))
seq = tir.SeqStmt([for_stmt, for_stmt, load])
func = tir.PrimFunc([], seq)

tvm.build(func)

@tqchen
Copy link
Member

tqchen commented Sep 5, 2021

Thanks @syang-ng , can you add a regression unit test case to cover the problem(ideally not calling build but directly call the pass that invokes this function)?

@tqchen tqchen added the status: need test case need test cases to cover the change label Sep 5, 2021
@tqchen
Copy link
Member

tqchen commented Sep 5, 2021

cc @ZihengJiang @vinx13 please help to manage the PR

@syang-ng
Copy link
Contributor Author

syang-ng commented Sep 6, 2021

I add a test case to cover the problem in the new commit. Since there is no way to invoke this function directly, I call the pass tir.transform.InjectVirtualThread to trigger this problem. Could you help review this PR :-D @vinx13

@vinx13
Copy link
Member

vinx13 commented Sep 6, 2021

please fix ci error

@syang-ng
Copy link
Contributor Author

syang-ng commented Sep 7, 2021

I updated the test case and it has passed all checks. Could you help to review it? Thanks :-P @vinx13

@vinx13 vinx13 added status: accepted and removed status: need test case need test cases to cover the change labels Sep 7, 2021
@vinx13 vinx13 merged commit d594934 into apache:main Sep 7, 2021
ylc pushed a commit to ylc/tvm that referenced this pull request Sep 29, 2021
…che#8930)

* fix bug of calling back() on an empty container scope_[op->buffer_var.get()]

* add test case for ConvertSSA

* update the style to fix ci error

* add annotation for function test_convert_ssa

* update the style of test_convert_ssa to fix ci error
@syang-ng syang-ng deleted the fix-scope-vector-size-check branch November 22, 2021 13:40
ylc pushed a commit to ylc/tvm that referenced this pull request Jan 13, 2022
…che#8930)

* fix bug of calling back() on an empty container scope_[op->buffer_var.get()]

* add test case for ConvertSSA

* update the style to fix ci error

* add annotation for function test_convert_ssa

* update the style of test_convert_ssa to fix ci error
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants