Skip to content

Reject invalid JALR and SYSTEM encodings - #12

Merged
jserv merged 1 commit into
sysprog21:mainfrom
thc1006:rvopt-reject-invalid-encodings
Aug 3, 2026
Merged

Reject invalid JALR and SYSTEM encodings#12
jserv merged 1 commit into
sysprog21:mainfrom
thc1006:rvopt-reject-invalid-encodings

Conversation

@thc1006

@thc1006 thc1006 commented Aug 2, 2026

Copy link
Copy Markdown
Contributor

decode_word() accept every opcode 0x67 word as JALR and every opcode 0x73 word as SYSTEM. This allows 0x00009067, whose JALR funct3 is reserved and to be lowered as a direct jump. It also allow 0x00001073, a CSR instruction, to become SYS_EXIT when a7 is 93.

JALR requires funct3 == 0, while ECALL and EBREAK use exact instruction words. Reject the other encodings, check the exact ECALL word in analyze_syscalls(), and add hand-encoded regression cases.

make check-all passes.


Summary by cubic

Reject invalid JALR and SYSTEM encodings to stop reserved forms from being lowered as jumps or syscalls. JALR now requires funct3==000, and SYSTEM matches only exact ECALL/EBREAK words.

  • Bug Fixes
    • Decoder: JALR only with funct3 000; SYSTEM only when word is 0x00000073 (ECALL) or 0x00100073 (EBREAK); otherwise mark illegal.
    • Syscall analysis: Match the exact ECALL word; treat EBREAK and non-ECALL as bad syscalls.
    • Tests: Add hand-encoded cases in the Makefile to reject bad encodings and accept valid ones, using python3 to pack words.
    • Docs: Note decode-time checks so reserved forms are rejected as unsupported ops.

Written for commit 4c45bf0. Summary will update on new commits.

Review in cubic

cubic-dev-ai[bot]

This comment was marked as resolved.

Comment thread Makefile
reserved-ret-funct3-1:00c000ef,05d00893,00000073,00009067 \
CSR-funct3-1:05d00893,00001073 \
CSR-funct3-5:05d00893,00005073 \
ebreak:05d00893,00100073 \

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

ebreak is grouped with the decode-gate rejections, but unlike the other RVOPT_BAD_ENC cases it decodes successfully as K_SYSTEM and is rejected later as SYS_BAD in analyze_syscalls() — not by the JALR/SYSTEM decode gate. The test still passes (rc=1 + "unsupported op"), so this is only a grouping nuance: it exercises the SYS_BAD emission path rather than the decode gate the neighboring cases test. Might be worth a one-line comment saying so.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thx prof's advice. ebreak pass decode as K_SYSTEM and is reject later as SYS_BAD. Add a comment to distinguish it from the decode-gate rejection cases.

@thc1006
thc1006 force-pushed the rvopt-reject-invalid-encodings branch from c53a379 to 5d7b85f Compare August 3, 2026 04:19
decode_word() accepts every opcode 0x67 word as JALR and every opcode
0x73 word as SYSTEM. This allows 0x00009067, whose JALR funct3 is
reserved, to be lowered as a direct jump. It also allows 0x00001073,
a CSR instruction, to become SYS_EXIT when a7 is 93.

JALR requires funct3 == 0, while ECALL and EBREAK use exact instruction
words. Reject the other encodings, check the exact ECALL word in
analyze_syscalls(), and add hand-encoded regression cases.

make check-all passes.

Signed-off-by: thc1006 <hctsai1006@cs.nctu.edu.tw>
@thc1006
thc1006 force-pushed the rvopt-reject-invalid-encodings branch from 5d7b85f to 4c45bf0 Compare August 3, 2026 04:25
@jserv
jserv merged commit ae69cf1 into sysprog21:main Aug 3, 2026
6 checks passed
@jserv

jserv commented Aug 3, 2026

Copy link
Copy Markdown
Collaborator

Thank @thc1006 for contributing!

@thc1006
thc1006 deleted the rvopt-reject-invalid-encodings branch August 3, 2026 04:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants