Add pop tests.

This commit is contained in:
Joe Hendrix 2019-04-04 09:42:11 -07:00
parent 092cbc2e26
commit ef6951458a
No known key found for this signature in database
GPG Key ID: 8DFA5FF784098C4F
10 changed files with 77 additions and 15 deletions

View File

@ -1,3 +1 @@
/test_btx
/test_ret
/test_ucomis
/*.exe

View File

@ -1,18 +1,15 @@
run : test_btx test_ucomis test_ret
./test_btx
./test_ucomis
./test_ret
tests = btx_test.exe ucomis_test.exe pop_test.exe ret_test.exe
test_btx : test_btx.c btx.s utils.h
clang -o test_btx test_btx.c btx.s
run : $(tests)
set -e; for v in $(tests); do echo "Running $$v"; ./$$v; done
test_ucomis : test_ucomis.c ucomis.s utils.h
clang -o test_ucomis test_ucomis.c ucomis.s
%_test.exe : %_test.c %_run.s utils.h
clang -o $@ $*_test.c $*_run.s
test_ret : test_ret.c ret.s utils.h
clang -o test_ret test_ret.c ret.s
test_%.exe : test_%.c %.s utils.h
clang -o $@ test_$*.c $*.s
clean :
rm -rf test_btx test_ucomis test_ret
rm -rf $(tests)
.PHONY : run clean

View File

@ -70,6 +70,5 @@ int main(int argc, char** argv) {
test_addr_imm("bts64_addr_imm04", &bts64_addr_imm4, 4, &bts_expected);
test_addr_imm("bts64_addr_imm68", &bts64_addr_imm68, 68, &bts_expected);
return 0;
}

42
x86/x86_tests/pop_run.s Normal file
View File

@ -0,0 +1,42 @@
.intel_syntax noprefix
.section __TEXT,__text
// This function tests what happens when we pop into rsp
.global _pop_rsp
_pop_rsp:
push rbp
mov rbp, rsp
sub rsp, 0x100
mov QWORD PTR [rsp], rdi
// Pop into rsp. This should assign rsp the value currently stored in rsp.
pop rsp
mov [rsi], rsp
mov rsp, rbp
pop rbp
ret
// pop_sp(v, &rsp0, &rsp1) tests what happens when poping into sp at an address
// that should make the increment visible.
.global _pop_sp
_pop_sp:
// Save stack pointer in rbp
push rbp
mov rbp, rsp
// Give some space to the stack
sub rsp, 0x100
// Decrease rsp and make sure the low 16-bits are 0xffff
and rsp, 0xffffffffffff0000
sub rsp, 1
// Store rsp in [rsi] (2nd argument)
mov [rsi], rsp
// Move rdi (first argument) into address pointed by rsp
// This is used to have a known value poped from rsp.
mov QWORD PTR [rsp], rdi
// Pop into sp. This should increment rsp, and store the value currently stored in [rsp] in sp.
pop sp
// Store new stack pointer in address pointed to by rdx (3rd argument)
mov [rdx], rsp
// Restore rsp,rbp, and return
mov rsp, rbp
pop rbp
ret

26
x86/x86_tests/pop_test.c Normal file
View File

@ -0,0 +1,26 @@
/*
This file contains some functionality tests to assess properties of ret.
*/
#include<stdbool.h>
#include<stdint.h>
#include<stdio.h>
#include<stdlib.h>
#include<stdarg.h>
#include "utils.h"
void pop_sp(uint64_t val, uint64_t* rsp0, uint64_t* rsp1);
void pop_rsp(uint64_t val, uint64_t* rsp0);
int main(int argc, char** argv) {
uint64_t rsp0;
uint64_t rsp1;
pop_rsp(0xdeadbeefdeadbeef, &rsp1);
my_assert(rsp1 == 0xdeadbeefdeadbeef, "Expected pop_rsp to return first value.\n");
pop_sp(0xdead, &rsp0, &rsp1);
// pop_sp should ensure the upper 48 bits have been incremented.
my_assert((rsp0 >> 16) + 1 == (rsp1 >> 16), "Expected pop_rsp to return first value.\n");
my_assert((rsp1 & 0xffff) == 0xdead, "Expected pop_rsp to return first value.\n");
return 0;
}