| 1 | /* Test deflate() on concurrently modified next_in. |
| 2 | * |
| 3 | * Plain zlib does not document that this is supported, but in practice it tolerates this, and QEMU live migration is |
| 4 | * known to rely on this. Make sure zlib-ng tolerates this as well. |
| 5 | */ |
| 6 | |
| 7 | #include "zbuild.h" |
| 8 | #ifdef ZLIB_COMPAT |
| 9 | #include "zlib.h" |
| 10 | #else |
| 11 | #include "zlib-ng.h" |
| 12 | #endif |
| 13 | |
| 14 | #include <gtest/gtest.h> |
| 15 | |
| 16 | #include <algorithm> |
| 17 | #include <atomic> |
| 18 | #include <cstring> |
| 19 | #include <thread> |
| 20 | |
| 21 | static uint8_t buf[8 * 1024]; |
| 22 | static uint8_t zbuf[4 * 1024]; |
| 23 | static uint8_t tmp[8 * 1024]; |
| 24 | |
| 25 | /* Thread that increments all bytes in buf by 1. */ |
| 26 | class Mutator { |
| 27 | enum class State { |
| 28 | PAUSED, |
| 29 | RUNNING, |
| 30 | STOPPED, |
| 31 | }; |
| 32 | |
| 33 | public: |
| 34 | Mutator() |
| 35 | : m_state(State::PAUSED), m_target_state(State::PAUSED), |
| 36 | m_thread(&Mutator::run, this) {} |
| 37 | ~Mutator() { |
| 38 | transition(State::STOPPED); |
| 39 | m_thread.join(); |
| 40 | } |
| 41 | |
| 42 | void pause() { |
| 43 | transition(State::PAUSED); |
| 44 | } |
| 45 | |
| 46 | void resume() { |
| 47 | transition(State::RUNNING); |
| 48 | } |
| 49 | |
| 50 | private: |
| 51 | void run() { |
| 52 | while (true) { |
| 53 | m_state.store(m_target_state); |
| 54 | if (m_state == State::PAUSED) |
| 55 | continue; |
| 56 | if (m_state == State::STOPPED) |
| 57 | break; |
| 58 | for (uint8_t & i: buf) |
| 59 | i++; |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | void transition(State target_state) { |
| 64 | m_target_state = target_state; |
| 65 | while (m_state != target_state) { |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | std::atomic<State> m_state, m_target_state; |
| 70 | std::thread m_thread; |
| 71 | }; |
| 72 | |
| 73 | TEST(deflate, concurrency) { |
| 74 | #ifdef S390_DFLTCC_DEFLATE |
| 75 | GTEST_SKIP() << "Known to be broken with S390_DFLTCC_DEFLATE"; |
| 76 | #endif |
| 77 | |
| 78 | /* Create reusable mutator and streams. */ |
| 79 | Mutator mutator; |
| 80 | |
| 81 | PREFIX3(stream) dstrm; |
| 82 | memset(&dstrm, 0, sizeof(dstrm)); |
| 83 | int err = PREFIX(deflateInit2)(&dstrm, Z_BEST_SPEED, Z_DEFLATED, -15, 8, Z_DEFAULT_STRATEGY); |
| 84 | ASSERT_EQ(Z_OK, err) << dstrm.msg; |
| 85 | |
| 86 | PREFIX3(stream) istrm; |
| 87 | memset(&istrm, 0, sizeof(istrm)); |
| 88 | err = PREFIX(inflateInit2)(&istrm, -15); |
| 89 | ASSERT_EQ(Z_OK, err) << istrm.msg; |
| 90 | |
| 91 | /* Iterate for a certain amount of time. */ |
| 92 | auto deadline = std::chrono::steady_clock::now() + std::chrono::seconds(1); |
| 93 | while (std::chrono::steady_clock::now() < deadline) { |
| 94 | /* Start each iteration with a fresh stream state. */ |
| 95 | err = PREFIX(deflateReset)(&dstrm); |
| 96 | ASSERT_EQ(Z_OK, err) << dstrm.msg; |
| 97 | |
| 98 | err = PREFIX(inflateReset)(&istrm); |
| 99 | ASSERT_EQ(Z_OK, err) << istrm.msg; |
| 100 | |
| 101 | /* Mutate and compress the first half of buf concurrently. |
| 102 | * Decompress and throw away the results, which are unpredictable. |
| 103 | */ |
| 104 | mutator.resume(); |
| 105 | dstrm.next_in = buf; |
| 106 | dstrm.avail_in = sizeof(buf) / 2; |
| 107 | while (dstrm.avail_in > 0) { |
| 108 | dstrm.next_out = zbuf; |
| 109 | dstrm.avail_out = sizeof(zbuf); |
| 110 | err = PREFIX(deflate)(&dstrm, Z_NO_FLUSH); |
| 111 | ASSERT_EQ(Z_OK, err) << dstrm.msg; |
| 112 | istrm.next_in = zbuf; |
| 113 | istrm.avail_in = sizeof(zbuf) - dstrm.avail_out; |
| 114 | while (istrm.avail_in > 0) { |
| 115 | istrm.next_out = tmp; |
| 116 | istrm.avail_out = sizeof(tmp); |
| 117 | err = PREFIX(inflate)(&istrm, Z_NO_FLUSH); |
| 118 | ASSERT_EQ(Z_OK, err) << istrm.msg; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | /* Stop mutation and compress the second half of buf. |
| 123 | * Decompress and check that the result matches. |
| 124 | */ |
| 125 | mutator.pause(); |
| 126 | dstrm.next_in = buf + sizeof(buf) / 2; |
| 127 | dstrm.avail_in = sizeof(buf) - sizeof(buf) / 2; |
| 128 | while (dstrm.avail_in > 0) { |
| 129 | dstrm.next_out = zbuf; |
| 130 | dstrm.avail_out = sizeof(zbuf); |
| 131 | err = PREFIX(deflate)(&dstrm, Z_FINISH); |
| 132 | if (err == Z_STREAM_END) |
| 133 | ASSERT_EQ(0u, dstrm.avail_in); |
| 134 | else |
| 135 | ASSERT_EQ(Z_OK, err) << dstrm.msg; |
| 136 | istrm.next_in = zbuf; |
| 137 | istrm.avail_in = sizeof(zbuf) - dstrm.avail_out; |
| 138 | while (istrm.avail_in > 0) { |
| 139 | size_t orig_total_out = istrm.total_out; |
| 140 | istrm.next_out = tmp; |
| 141 | istrm.avail_out = sizeof(tmp); |
| 142 | err = PREFIX(inflate)(&istrm, Z_NO_FLUSH); |
| 143 | if (err == Z_STREAM_END) |
| 144 | ASSERT_EQ(0u, istrm.avail_in); |
| 145 | else |
| 146 | ASSERT_EQ(Z_OK, err) << istrm.msg; |
| 147 | size_t concurrent_size = sizeof(buf) - sizeof(buf) / 2; |
| 148 | if (istrm.total_out > concurrent_size) { |
| 149 | size_t tmp_offset, buf_offset, size; |
| 150 | if (orig_total_out >= concurrent_size) { |
| 151 | tmp_offset = 0; |
| 152 | buf_offset = orig_total_out - concurrent_size; |
| 153 | size = istrm.total_out - orig_total_out; |
| 154 | } else { |
| 155 | tmp_offset = concurrent_size - orig_total_out; |
| 156 | buf_offset = 0; |
| 157 | size = istrm.total_out - concurrent_size; |
| 158 | } |
| 159 | ASSERT_EQ(0, memcmp(tmp + tmp_offset, buf + sizeof(buf) / 2 + buf_offset, size)); |
| 160 | } |
| 161 | } |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | err = PREFIX(inflateEnd)(&istrm); |
| 166 | ASSERT_EQ(Z_OK, err) << istrm.msg; |
| 167 | |
| 168 | err = PREFIX(deflateEnd)(&dstrm); |
| 169 | ASSERT_EQ(Z_OK, err) << istrm.msg; |
| 170 | } |