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

x86: Truncate RAX when matching smaller return values #847

Merged
merged 6 commits into from
Oct 6, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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
10 changes: 10 additions & 0 deletions intTests/test_llvm_x86_05/test.S
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
section .bss
section .text
global returntest
returntest:
mov rax, 42
ret
global _start
_start:
mov rax, 60
syscall
8 changes: 8 additions & 0 deletions intTests/test_llvm_x86_05/test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include <stdint.h>
#include <stdio.h>

extern uint32_t returntest();

uint32_t test() {
return returntest();
};
10 changes: 10 additions & 0 deletions intTests/test_llvm_x86_05/test.saw
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
enable_experimental;

m <- llvm_load_module "test.bc";

let returntest_setup = do {
crucible_execute_func [];

crucible_return (crucible_term {{ 42 : [32] }});
};
crucible_llvm_verify_x86 m "./test" "returntest" [] false returntest_setup w4;
6 changes: 6 additions & 0 deletions intTests/test_llvm_x86_05/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
set -e

yasm -felf64 test.S
ld test.o -o test
clang -c -emit-llvm test.c
$SAW test.saw
17 changes: 16 additions & 1 deletion src/SAWScript/Crucible/LLVM/X86.hs
Original file line number Diff line number Diff line change
Expand Up @@ -722,7 +722,22 @@ assertPost globals env premem preregs = do
returnMatches <- case (ms ^. MS.csRetValue, ms ^. MS.csRet) of
(Just expectedRet, Just retTy) -> do
postRAX <- C.LLVM.ptrToPtrVal <$> getReg Macaw.RAX postregs
pure [LO.matchArg opts sc cc ms MS.PostState postRAX retTy expectedRet]
case (postRAX, C.LLVM.memTypeBitwidth retTy) of
(C.LLVM.LLVMValInt base off, Just retTyBits) -> do
let
truncateRAX :: forall r. NatRepr r -> X86Sim (C.LLVM.LLVMVal Sym)
truncateRAX rsz =
case (testLeq (knownNat @1) rsz, testLeq rsz (W4.bvWidth off)) of
(Just LeqProof, Just LeqProof) ->
case testStrictLeq rsz (W4.bvWidth off) of
Left LeqProof -> do
offTrunc <- liftIO $ W4.bvTrunc sym rsz off
pure $ C.LLVM.LLVMValInt base offTrunc
_ -> pure $ C.LLVM.LLVMValInt base off
_ -> throwX86 "Width of return type is zero bits"
postRAXTrunc <- viewSome truncateRAX (mkNatRepr retTyBits)
pure [LO.matchArg opts sc cc ms MS.PostState postRAXTrunc retTy expectedRet]
_ -> throwX86 "Invalid return type"
chameco marked this conversation as resolved.
Show resolved Hide resolved
_ -> pure []

pointsToMatches <- forM (ms ^. MS.csPostState . MS.csPointsTos)
Expand Down