Skip to content

Commit 6c2902b

Browse files
vvfedorenkofacebook-github-bot
authored andcommitted
avoid reading of uninit data
Summary: the tests showed that the reader thread can start before the writer did the first write. That means the load function will return uninit data. Let's use a sequence value of 0 as special indicator of uninit structure add a bit more print info for the failed test Reviewed By: t3lurid3 Differential Revision: D75869421 fbshipit-source-id: c12c6689578b1ec705a0dad224b3b21c5d50578f
1 parent 6928057 commit 6c2902b

2 files changed

Lines changed: 8 additions & 0 deletions

File tree

fbclock/cpp_test/test.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ int reader_thread_v2(fbclock_shmdata_v2* shmp, int tries) {
179179
for (int i = 0; i < tries; i++) {
180180
err = fbclock_clockdata_load_data_v2(shmp, &data);
181181
if (err != 0) {
182+
printf("load v2 data failed: %d\n", err);
182183
return err;
183184
}
184185
if (data.ingress_time_ns * 2 != data.error_bound_ns) {

fbclock/fbclock.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ int fbclock_clockdata_store_data_v2(uint32_t fd, fbclock_clockdata_v2* data) {
104104
memcpy(&shmp->data, data, FBCLOCK_CLOCKDATA_V2_SIZE);
105105
__sync_synchronize();
106106
seq++;
107+
if (!seq)
108+
seq += 2; // avoid 0 value on wraparound
107109
atomic_store(&shmp->seq, seq);
108110
munmap(shmp, FBCLOCK_SHMDATA_V2_SIZE);
109111
return FBCLOCK_E_NO_ERROR;
@@ -133,6 +135,11 @@ int fbclock_clockdata_load_data_v2(
133135
fbclock_clockdata_v2* data) {
134136
for (int i = 0; i < FBCLOCK_MAX_READ_TRIES; i++) {
135137
uint64_t seq = atomic_load(&shmp->seq);
138+
if (!seq) { // 0 value means uninitialized
139+
usleep(10);
140+
__sync_synchronize();
141+
continue;
142+
}
136143
if (seq & 1) {
137144
__sync_synchronize();
138145
continue;

0 commit comments

Comments
 (0)