Software Random Number Generation Based On Race Conditions
Software Random Number Generation Based On Race Conditions
net/publication/224578073
CITATIONS READS
10 2,465
3 authors:
Sebastian Banescu
Technische Universität München
38 PUBLICATIONS 204 CITATIONS
SEE PROFILE
Some of the authors of this publication are also working on these related projects:
All content following this page was uploaded by Sebastian Banescu on 30 March 2016.
which is scheduled first. Figure 1 illustrates the determinis- — it can contribute to the randomness of our application’s
tic scheduling of the two threads. behavior.
Although the behavior of the scheduler is deterministic, We identified three hardware factors that contribute to
we observed during multiple execution of our application the random behavior of our application:
that the final value of the shared variable is different and
unpredictable. So, the question that must be answered is: 1. Cache misses force the CPU to bring the needed in-
“What makes it possible to get different random values of struction or data from another memory level and con-
the shared variable in different runs under the same condi- sequently to consume some extra CPU clock cycles on
tions, even if the system is deterministic?” behalf of the currently running thread [13]. That means
In order to answer this question we followed two direc- that during the same time interval, i.e. the allocated
tions: finding software and hardware factors of the random- time quantum, a different number of thread’s instruc-
ness. On the other hand, we also took into consideration tions can be executed in different situations. That re-
two different perspectives on any factor identified: theoret- sults in one thread being preempted at different places
ical and practical possibility that factor to contribute to the during its execution. Related to our application, that
generation of random values of the shared variable. means that the two different execution scenarios de-
Related to the software factors, one that contributes to scribed in Section 3 can be met. If that happens ran-
the randomness behavior of our application is its execution domly during the execution of our application or dur-
environment. ing different runs of it, having the effect of generating
The scheduler being deterministic, it behaves identically random values of the shared variable, depends on the
each time in the same conditions, generating the same se- way the cache misses occur or not. This is very ex-
quence of scheduled threads. From this point of view, gen- ecution environment dependent. In a special test-bed
erating an execution environment identical with a previous prepared system, like the one we used to test our ap-
one means to execute the same applications and start them plication, where the environment was very stable, the
all at the same corresponding moments and in the system cache misses do not occur eventually at all, in the case
state as in the previous environment. This is theoretically all the threads of the testing application fit in the cache
possible, but practically very difficult if not impossible to (which we think is the case of our application) or oc-
get. cur accordingly to a regular periodic pattern, in the
We tried to test of our application in the same execu- case where there is not enough room for all the running
tion environment: (1) we ran it in Linux single mode with threads in the cache. The latter case happens because
no other user processes running, but the ones the operating the scheduling sequence is a fixed one and also follows
system automatically starts in order to function properly and a regular periodic pattern as we already mentioned. In
(2) started it when the system was in an identical stabilized a real practical system, anyway, the environment being
state, observed using a monitoring tool. Even so, the val- not stable, the cache miss sequence will also not be a
ues of the shared variable we obtained were random. So, it regular and deterministic one, thus we can take it into
is very possible that the software execution environment is account for the randomness of our application.
not the only factor responsible for the randomness observed.
Nevertheless, in a practical situation, on a system in use, the 2. Another hardware factor that can make our applica-
execution environment being not identical at different runs tion behave differently in similar situations is related
of our application and also not even during one execution to the way the pipeline instruction flow is affected by
— because other applications running evolve dynamically the occurrence of the timer interrupt, which leads to a
context switch. In such a situation, the CPU instruc-
tion execution pipeline contains instructions of the pre-
empted thread. Now, due to the context switch the
following instructions being introduced in the pipeline
belong to the newly scheduled thread, the remaining
instructions of the preempted thread being executed Figure 3. Undeterministic scheduling of
when it resumes later its execution. Anyway, after Thread 1 and Thread 2 on a real sys-
the context switch, the pipeline contains instructions of tem. All the time quanta assigned to the
both threads. We do no insist now on the way the CPU two threads last theoretically τ milliseconds.
protects one thread from another in such a situation, Practically, however, they contain a different
because this is not so important for us now (see [13] number νk of CPU clock cycles. So, it cannot
for details). Let us suppose that the first instruction be calculated at one moment which thread
that will be fetched by the CPU when the preempted will be the executed one.
thread will resume its execution depends in some way
on some other previous instruction, which was in the
pipeline at the moment of the context switch. In an un-
of CPU clock cycles and, consequently different num-
interrupted execution, when the context switch would
bers of instructions, being executed in different time
have not occurred, this instruction has had to wait some
quanta of the same (theoretical) length. Relative to our
time, i.e. some number of CPU clock cycles, for the
application, that means that its execution can generate
instruction it depends on to be retired (finished). In
randomly any of the two scenarios described before.
the case of the context switch, however, the instruc-
tion is not fetched by the CPU. Nevertheless, in the
The second and the third hardware factors we mentioned
same time the previous instruction it depends on, be-
above do not depend of the execution environment. They
ing in the CPU pipeline, continues its execution, but
are tightly coupled, though the second one, the hazard in
in the context of the next thread executed. That is, it
the instruction pipeline, seems to be a consequence of the
“consumes” (in fact, shares) time from the quantum al-
third one, the variation of clock quatua. So, they always
located to the next thread. When the preempted thread
contribute to the randomness of our application.
resumes its execution and its next instruction is fetched
The hardware clock’s drift rate can vary in the interval
in the CPU, the instruction it depends on is already re-
[−ρ, ρ], where ρ is in the range [10−4 , 10−6 ] µs s , i.e. every
tired and consequently it can be launched immediately,
second the clock deviates with a 10−4 or 10−6 µs from the
so saving CPU clock cycles from its quantum, com-
real time. Thus a time quantum of theoretical τ millisec-
pared with the uninterrupted execution. Similar to the
onds lasts in reality something between [τ − ρτ, τ + ρτ ].
case of cache misses, that results in a thread executing
Figure 2 illustrates this. So, the variation in time of the time
different number of instructions during different iden-
quantum is 2ρτ . Considering the default value of a time
tical time quanta and consequently meeting the two ex-
quantum in Linux of 100ms that means that the variation
ecution scenarios.
interval I of a time quantum is
I = 2ρτ = 2×10−4 µs s ×100ms = 2×10
−4 µs
s ×100×
3. The timer interrupt, the scheduler uses to preempt
10 s = 2 × (10 × 10 × 10 )µs = 2 × 10−5 µs =
−3 −4 2 −3
currently running thread and switch the CPU to an-
2 × 10−2 ns = 0.02ns
other thread, is not generated perfectly periodically.
For a processor of 2.6GHz, which generates a clock tick
This happens because the hardware clock, which de-
every 0.385ns, a CPU clock cycle can be missed or saved
termines the timer interrupt is not perfect. It is known
at each 20 consecutive time quantum, i.e. each 2 seconds.
[1] that a hardware clock has a small bounded drift
All the above mentioned factors contribute in a real sys-
rate, i.e. the difference between the clock and real-
tem to the randomness behavior of our application, due to
time can change every second at most by some known
the fact that different time quanta allocated to its threads,
constant ρ 1. Consequently, different time quanta
even equal as mathematical values, contain different num-
allocated by the scheduler to the running threads are
ber of CPU clock cycles. That means that the theoretical
not of the same length, even if, mathematically speak-
scheduling described in Figure 1 does not hold in practice.
ing, they have the same value.
The scheduling situation, we suppose to happen in reality, is
In case the clock used to generate timer interrupts is illustrated in Figure 3. The different lengths of time quan-
not the same or not synchronized with the one used to tum we illustrated in that figure are different in terms of
generate clock cycles to the CPU, the variation in real- CPU clock cycles, a characteristic specific to all the factors
time length of time quanta results in different numbers we described above.
5. The Application