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

AVM: Avoid panics while type checking bad immediates #5271

Merged
merged 1 commit into from
Apr 10, 2023

Conversation

jannotti
Copy link
Contributor

@jannotti jannotti commented Apr 7, 2023

Added tests for immediate bounds

Fixes #5217

@jannotti jannotti changed the title Avoid panics while type checking bad immediates AVM: Avoid panics while type checking bad immediates Apr 7, 2023
Copy link
Contributor

@ahangsu ahangsu left a comment

Choose a reason for hiding this comment

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

lgtm, I had some question about how assembler works, but the PR itself looks right

@@ -1242,7 +1252,7 @@ func typeBury(pgm *ProgramKnowledge, args []string) (StackTypes, StackTypes, err
}

func typeFrameDig(pgm *ProgramKnowledge, args []string) (StackTypes, StackTypes, error) {
n, ok := getImm(args, 0)
n, ok := getImm(args, 0, true)
Copy link
Contributor

Choose a reason for hiding this comment

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

yes

testProg(t, "frame_dig 128;", AssemblerMaxVersion,
Expect{1, "frame_dig unable to parse..."})
testProg(t, "int 1; frame_bury -129;", AssemblerMaxVersion,
Expect{1, "frame_bury unable to parse..."})
Copy link
Contributor

Choose a reason for hiding this comment

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

The ... usage in testMatch ~ testProg ~ assembler_test.go is mindblowing...

@@ -1428,16 +1438,16 @@ func typeStores(pgm *ProgramKnowledge, args []string) (StackTypes, StackTypes, e
}

func typeLoad(pgm *ProgramKnowledge, args []string) (StackTypes, StackTypes, error) {
scratchIndex, ok := getImm(args, 0)
scratchIndex, ok := getImm(args, 0, false)
if !ok {
return nil, nil, nil
}
return nil, StackTypes{pgm.scratchSpace[scratchIndex]}, nil
Copy link
Contributor

Choose a reason for hiding this comment

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

so it seems we previously allowed people to pass in -1 from getImm, and passed in scratchSpace, thus panic?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I often forget how fragile the type* methods are. A lot of checking happens later, so I forget that these need good bounds checking on their own. Someday, I'd like to refactor so they are a bit safer/easier to write.

testProg(t, "load -100;", AssemblerMaxVersion,
Expect{1, "load unable to parse..."})
testProg(t, "int 1; store 256;", AssemblerMaxVersion,
Expect{1, "store i beyond 255: 256"})
Copy link
Contributor

Choose a reason for hiding this comment

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

I had a gap in thinking through the error message: so failing to get type info w.r.t stack won't stop you from assembling, but what really stops you is in asmDefault where you call byteImm or int8Imm.

Does that sounds right about code path?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Right, because the type* functions bailout and usually return nil, nil, nil when they are confused by the immediates or the stack size. They know that the real assembly routine will be angry, so they don't need to also complain.

Copy link
Member

@jasonpaulos jasonpaulos left a comment

Choose a reason for hiding this comment

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

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

load/store arg between -257 and 0 causes unhandled error upon compile
3 participants