Skip to content

Commit a18fdee

Browse files
itzmeanjanoreparaz
andauthored
Add mfence before issuing rdtsc (#30)
* fix: issue `mfence` instruction before issuing `rdtsc` This should make all memory load/ store operations happening before RDTSC (in program order) globally visible. Read https://stackoverflow.com/a/12634857. Signed-off-by: Anjan Roy <[email protected]> * udt: update example showing constant-timeness of `memcmp` varies based on different optimization settings Signed-off-by: Anjan Roy <[email protected]> * chg: get rid of redundant assertion Comparison of unsigned expression in `>= 0` is always **true**. Signed-off-by: Anjan Roy <[email protected]> * ci: fix broken apt install (#31) * readme: add credits for PR 30 (Anjan Roy) * add link to github issue --------- Signed-off-by: Anjan Roy <[email protected]> Co-authored-by: Oscar Reparaz <[email protected]>
1 parent 4d7fcc3 commit a18fdee

File tree

4 files changed

+18
-12
lines changed

4 files changed

+18
-12
lines changed

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ dut_donnabad: $(OBJS_DONNABAD) examples/donnabad/dut_donnabad.c
3434

3535
dut_simple: examples/simple/example.c
3636
# higher compiler optimization levels can make this constant time
37-
$(CC) $(LDFLAGS) -O0 $(INCS) -o dudect_simple_O0 examples/simple/example.c $(LIBS)
37+
$(CC) $(LDFLAGS) -O0 $(INCS) -DMEASUREMENTS_PER_CHUNK=100000 -o dudect_simple_O0 examples/simple/example.c $(LIBS)
3838
$(CC) $(LDFLAGS) -O2 $(INCS) -DMEASUREMENTS_PER_CHUNK=100000 -o dudect_simple_O2 examples/simple/example.c $(LIBS)
3939

4040
.c.o:

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ The following people have contributed to `dudect` through code, bug reports, iss
195195
* RashidAlsuwaidi
196196
* paul90317
197197
* Fabian Albert (https://github.com/FAlbertDev)
198+
* Anjan Roy (https://github.com/itzmeanjan)
198199

199200
The approach is described in this paper
200201
> Oscar Reparaz, Josep Balasch and Ingrid Verbauwhede

examples/simple/example.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ int check_tag(uint8_t *x, uint8_t *y, size_t len) {
1010
return memcmp(x, y, len);
1111
}
1212

13-
#define SECRET_LEN_BYTES (16)
13+
#define SECRET_LEN_BYTES (512)
1414

1515
uint8_t secret[SECRET_LEN_BYTES] = {0, 1, 2, 3, 4, 5, 6, 42};
1616

src/dudect.h

+15-10
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ extern "C" {
7171

7272
#include <stddef.h>
7373
#include <stdint.h>
74+
#include <emmintrin.h>
75+
#include <x86intrin.h>
7476

7577
#ifdef DUDECT_VISIBLITY_STATIC
7678
#define DUDECT_VISIBILITY static
@@ -202,7 +204,6 @@ static int cmp(const int64_t *a, const int64_t *b) { return (int)(*a - *b); }
202204

203205
static int64_t percentile(int64_t *a_sorted, double which, size_t size) {
204206
size_t array_position = (size_t)((double)size * (double)which);
205-
assert(array_position >= 0);
206207
assert(array_position < size);
207208
return a_sorted[array_position];
208209
}
@@ -262,16 +263,20 @@ uint8_t randombit(void) {
262263
}
263264

264265
/*
265-
Intel actually recommends calling CPUID to serialize the execution flow
266-
and reduce variance in measurement due to out-of-order execution.
267-
We don't do that here yet.
268-
see §3.2.1 http://www.intel.com/content/www/us/en/embedded/training/ia-32-ia-64-benchmark-code-execution-paper.html
269-
*/
270-
static int64_t cpucycles(void) {
271-
unsigned int hi, lo;
266+
Returns current CPU tick count from *T*ime *S*tamp *C*ounter.
267+
268+
To enforce CPU to issue RDTSC instruction where we want it to, we put a `mfence` instruction before
269+
issuing `rdtsc`, which should make all memory load/ store operations, prior to RDTSC, globally visible.
272270
273-
__asm__ volatile("rdtsc\n\t" : "=a"(lo), "=d"(hi));
274-
return ((int64_t)lo) | (((int64_t)hi) << 32);
271+
See https://github.com/oreparaz/dudect/issues/32
272+
See RDTSC documentation @ https://www.intel.com/content/www/us/en/docs/intrinsics-guide/index.htm#text=rdtsc&ig_expand=4395,5273
273+
See MFENCE documentation @ https://www.intel.com/content/www/us/en/docs/intrinsics-guide/index.htm#text=mfence&ig_expand=4395,5273,4395
274+
275+
Also see https://stackoverflow.com/a/12634857
276+
*/
277+
static inline int64_t cpucycles(void) {
278+
_mm_mfence();
279+
return (int64_t)__rdtsc();
275280
}
276281

277282
// threshold values for Welch's t-test

0 commit comments

Comments
 (0)