@@ -1891,6 +1891,92 @@ static void test_x86_ro_segfault(void)
1891
1891
OK (uc_close (uc ));
1892
1892
}
1893
1893
1894
+ static bool test_x86_hook_insn_rdtsc_cb (uc_engine * uc , void * user_data )
1895
+ {
1896
+ uint64_t h = 0x00000000FEDCBA98 ;
1897
+ OK (uc_reg_write (uc , UC_X86_REG_RDX , & h ));
1898
+
1899
+ uint64_t l = 0x0000000076543210 ;
1900
+ OK (uc_reg_write (uc , UC_X86_REG_RAX , & l ));
1901
+
1902
+ return true;
1903
+ }
1904
+
1905
+ static void test_x86_hook_insn_rdtsc (void )
1906
+ {
1907
+ char code [] = "\x0F\x31" ; // RDTSC
1908
+
1909
+ uc_engine * uc ;
1910
+ uc_common_setup (& uc , UC_ARCH_X86 , UC_MODE_64 , code , sizeof code - 1 );
1911
+
1912
+ uc_hook hook ;
1913
+ OK (uc_hook_add (uc , & hook , UC_HOOK_INSN , test_x86_hook_insn_rdtsc_cb , NULL ,
1914
+ 1 , 0 , UC_X86_INS_RDTSC ));
1915
+
1916
+ OK (uc_emu_start (uc , code_start , code_start + sizeof code - 1 , 0 , 0 ));
1917
+
1918
+ OK (uc_hook_del (uc , hook ));
1919
+
1920
+ uint64_t h = 0 ;
1921
+ OK (uc_reg_read (uc , UC_X86_REG_RDX , & h ));
1922
+ TEST_CHECK (h == 0x00000000FEDCBA98 );
1923
+
1924
+ uint64_t l = 0 ;
1925
+ OK (uc_reg_read (uc , UC_X86_REG_RAX , & l ));
1926
+ TEST_CHECK (l == 0x0000000076543210 );
1927
+
1928
+ OK (uc_close (uc ));
1929
+ }
1930
+
1931
+ static bool test_x86_hook_insn_rdtscp_cb (uc_engine * uc , void * user_data )
1932
+ {
1933
+ uint64_t h = 0x0000000001234567 ;
1934
+ OK (uc_reg_write (uc , UC_X86_REG_RDX , & h ));
1935
+
1936
+ uint64_t l = 0x0000000089ABCDEF ;
1937
+ OK (uc_reg_write (uc , UC_X86_REG_RAX , & l ));
1938
+
1939
+ uint64_t i = 0x00000000DEADBEEF ;
1940
+ OK (uc_reg_write (uc , UC_X86_REG_RCX , & i ));
1941
+
1942
+ return true;
1943
+ }
1944
+
1945
+ static void test_x86_hook_insn_rdtscp (void )
1946
+ {
1947
+ uc_engine * uc ;
1948
+ OK (uc_open (UC_ARCH_X86 , UC_MODE_64 , & uc ));
1949
+
1950
+ OK (uc_ctl_set_cpu_model (uc , UC_CPU_X86_HASWELL ));
1951
+
1952
+ OK (uc_mem_map (uc , code_start , code_len , UC_PROT_ALL ));
1953
+
1954
+ char code [] = "\x0F\x01\xF9" ; // RDTSCP
1955
+ OK (uc_mem_write (uc , code_start , code , sizeof code - 1 ));
1956
+
1957
+ uc_hook hook ;
1958
+ OK (uc_hook_add (uc , & hook , UC_HOOK_INSN , test_x86_hook_insn_rdtscp_cb , NULL ,
1959
+ 1 , 0 , UC_X86_INS_RDTSCP ));
1960
+
1961
+ OK (uc_emu_start (uc , code_start , code_start + sizeof code - 1 , 0 , 0 ));
1962
+
1963
+ OK (uc_hook_del (uc , hook ));
1964
+
1965
+ uint64_t h = 0 ;
1966
+ OK (uc_reg_read (uc , UC_X86_REG_RDX , & h ));
1967
+ TEST_CHECK (h == 0x0000000001234567 );
1968
+
1969
+ uint64_t l = 0 ;
1970
+ OK (uc_reg_read (uc , UC_X86_REG_RAX , & l ));
1971
+ TEST_CHECK (l == 0x0000000089ABCDEF );
1972
+
1973
+ uint64_t i = 0 ;
1974
+ OK (uc_reg_read (uc , UC_X86_REG_RCX , & i ));
1975
+ TEST_CHECK (i == 0x00000000DEADBEEF );
1976
+
1977
+ OK (uc_close (uc ));
1978
+ }
1979
+
1894
1980
TEST_LIST = {
1895
1981
{"test_x86_in" , test_x86_in },
1896
1982
{"test_x86_out" , test_x86_out },
@@ -1947,4 +2033,6 @@ TEST_LIST = {
1947
2033
{"test_bswap_x64" , test_bswap_ax },
1948
2034
{"test_rex_x64" , test_rex_x64 },
1949
2035
{"test_x86_ro_segfault" , test_x86_ro_segfault },
2036
+ {"test_x86_hook_insn_rdtsc" , test_x86_hook_insn_rdtsc },
2037
+ {"test_x86_hook_insn_rdtscp" , test_x86_hook_insn_rdtscp },
1950
2038
{NULL , NULL }};
0 commit comments