Skip to content

Commit

Permalink
Merge pull request #541 from Tencent/dev
Browse files Browse the repository at this point in the history
for v1.2.3
  • Loading branch information
lingol authored Oct 16, 2020
2 parents 66b3f41 + ba73f62 commit 28e867c
Show file tree
Hide file tree
Showing 29 changed files with 196 additions and 84 deletions.
2 changes: 1 addition & 1 deletion Android/MMKV/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:4.0.1'
classpath 'com.android.tools.build:gradle:4.1.0'
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.8.1'
classpath 'digital.wup:android-maven-publish:3.6.2'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
Expand Down
2 changes: 1 addition & 1 deletion Android/MMKV/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ org.gradle.jvmargs=-Xmx1536m
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true

VERSION_NAME_PREFIX=1.2.2
VERSION_NAME_PREFIX=1.2.3
#VERSION_NAME_SUFFIX=-SNAPSHOT
VERSION_NAME_SUFFIX=
4 changes: 2 additions & 2 deletions Android/MMKV/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Tue Jun 02 16:48:00 CST 2020
#Fri Oct 16 21:16:39 CST 2020
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.1.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-6.5-bin.zip
4 changes: 2 additions & 2 deletions Android/MMKV/mmkv/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,6 @@ dependencies {
implementation 'androidx.annotation:annotation:1.1.0'
javadocDeps 'androidx.annotation:annotation:1.1.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
androidTestImplementation 'androidx.test:runner:1.3.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}
12 changes: 6 additions & 6 deletions Android/MMKV/mmkvdemo/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,13 @@ repositories {
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation project(':mmkv')
// implementation 'com.tencent:mmkv:1.2.2'
// implementation 'com.tencent:mmkv-static:1.2.2'
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
// implementation 'com.tencent:mmkv:1.2.3'
// implementation 'com.tencent:mmkv-static:1.2.3'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
androidTestImplementation 'androidx.test:runner:1.3.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'com.getkeepsafe.relinker:relinker:1.3.1'
}
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
# MMKV Change Log

## v1.2.3 / 2020-10-16
### Changes for All platforms
* Fix a potential crash on 32-bit devices due to pointer alignment issue.
* Fix a decode error of encrypted MMKV on some 32-bit devices.

### iOS / macOS
* Fix a potential `crc32()` crash on some kind of arm64 devices.
* Fix a potential crash after calling `+[MMKV onAppTerminate]`.

### Android
* Fix a rare lock conflict on `checkProcessMode()`.

### POSIX
Add MMKV support for **Python** on POSIX platforms. Most things actually work!
Check out the [wiki](https://github.com/Tencent/MMKV/wiki/python_setup) for more info.

## v1.2.2 / 2020-07-30

### iOS / macOS
Expand Down
1 change: 1 addition & 0 deletions Core/CodedInputDataCrypt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*/

#include "CodedInputDataCrypt.h"
#include "MMKVLog.h"
#include "PBUtility.h"
#include <cassert>
#include <cerrno>
Expand Down
19 changes: 12 additions & 7 deletions Core/InterProcessLock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ bool FileLock::lock(LockType lockType) {
return doLock(lockType, true);
}

bool FileLock::try_lock(LockType lockType) {
return doLock(lockType, false);
bool FileLock::try_lock(LockType lockType, bool *tryAgain) {
return doLock(lockType, false, tryAgain);
}

bool FileLock::doLock(LockType lockType, bool wait) {
bool FileLock::doLock(LockType lockType, bool wait, bool *tryAgain) {
if (!isFileLockValid()) {
return false;
}
Expand All @@ -59,7 +59,7 @@ bool FileLock::doLock(LockType lockType, bool wait) {
}
}

auto ret = platformLock(lockType, wait, unLockFirstIfNeeded);
auto ret = platformLock(lockType, wait, unLockFirstIfNeeded, tryAgain);
if (ret) {
if (lockType == SharedLockType) {
m_sharedLockCount++;
Expand All @@ -82,10 +82,10 @@ static int32_t LockType2FlockType(LockType lockType) {
return LOCK_EX;
}

bool FileLock::platformLock(LockType lockType, bool wait, bool unLockFirstIfNeeded) {
bool FileLock::platformLock(LockType lockType, bool wait, bool unLockFirstIfNeeded, bool *tryAgain) {
# ifdef MMKV_ANDROID
if (m_isAshmem) {
return ashmemLock(lockType, wait, unLockFirstIfNeeded);
return ashmemLock(lockType, wait, unLockFirstIfNeeded, tryAgain);
}
# endif
auto realLockType = LockType2FlockType(lockType);
Expand All @@ -105,7 +105,12 @@ bool FileLock::platformLock(LockType lockType, bool wait, bool unLockFirstIfNeed

auto ret = flock(m_fd, cmd);
if (ret != 0) {
MMKVError("fail to lock fd=%d, ret=%d, error:%s", m_fd, ret, strerror(errno));
if (tryAgain) {
*tryAgain = (errno == EWOULDBLOCK);
}
if (wait) {
MMKVError("fail to lock fd=%d, ret=%d, error:%s", m_fd, ret, strerror(errno));
}
// try recover my shared-lock
if (unLockFirstIfNeeded) {
ret = flock(m_fd, LockType2FlockType(SharedLockType));
Expand Down
42 changes: 21 additions & 21 deletions Core/InterProcessLock.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
#define MMKV_INTERPROCESSLOCK_H
#ifdef __cplusplus

#include "MMKVPredef.h"
# include "MMKVPredef.h"

#include <fcntl.h>
# include <fcntl.h>

namespace mmkv {

Expand All @@ -40,39 +40,39 @@ class FileLock {
size_t m_sharedLockCount;
size_t m_exclusiveLockCount;

bool doLock(LockType lockType, bool wait);
bool platformLock(LockType lockType, bool wait, bool unLockFirstIfNeeded);
bool doLock(LockType lockType, bool wait, bool *tryAgain = nullptr);
bool platformLock(LockType lockType, bool wait, bool unLockFirstIfNeeded, bool *tryAgain);
bool platformUnLock(bool unLockFirstIfNeeded);

#ifndef MMKV_WIN32
# ifndef MMKV_WIN32
bool isFileLockValid() { return m_fd >= 0; }
# ifdef MMKV_ANDROID
# ifdef MMKV_ANDROID
const bool m_isAshmem;
struct flock m_lockInfo;
bool ashmemLock(LockType lockType, bool wait, bool unLockFirstIfNeeded);
bool ashmemLock(LockType lockType, bool wait, bool unLockFirstIfNeeded, bool *tryAgain);
bool ashmemUnLock(bool unLockFirstIfNeeded);
# endif
# endif

#else // defined(MMKV_WIN32)
# else // defined(MMKV_WIN32)
OVERLAPPED m_overLapped;

bool isFileLockValid() { return m_fd != INVALID_HANDLE_VALUE; }
#endif // MMKV_WIN32
# endif // MMKV_WIN32

public:
#ifndef MMKV_WIN32
# ifndef MMKV_ANDROID
# ifndef MMKV_WIN32
# ifndef MMKV_ANDROID
explicit FileLock(MMKVFileHandle_t fd) : m_fd(fd), m_sharedLockCount(0), m_exclusiveLockCount(0) {}
# else
# else
explicit FileLock(MMKVFileHandle_t fd, bool isAshmem = false);
# endif // MMKV_ANDROID
#else // defined(MMKV_WIN32)
# endif // MMKV_ANDROID
# else // defined(MMKV_WIN32)
explicit FileLock(MMKVFileHandle_t fd) : m_fd(fd), m_overLapped{}, m_sharedLockCount(0), m_exclusiveLockCount(0) {}
#endif // MMKV_WIN32
# endif // MMKV_WIN32

bool lock(LockType lockType);

bool try_lock(LockType lockType);
bool try_lock(LockType lockType, bool *tryAgain);

bool unlock(LockType lockType);

Expand All @@ -99,9 +99,9 @@ class InterProcessLock {
}
}

bool try_lock() {
bool try_lock(bool *tryAgain = nullptr) {
if (m_enable) {
return m_fileLock->try_lock(m_lockType);
return m_fileLock->try_lock(m_lockType, tryAgain);
}
return false;
}
Expand All @@ -115,5 +115,5 @@ class InterProcessLock {

} // namespace mmkv

#endif
#endif //MMKV_INTERPROCESSLOCK_H
#endif // __cplusplus
#endif // MMKV_INTERPROCESSLOCK_H
5 changes: 4 additions & 1 deletion Core/InterProcessLock_Android.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ static short LockType2FlockType(LockType lockType) {
}
}

bool FileLock::ashmemLock(LockType lockType, bool wait, bool unLockFirstIfNeeded) {
bool FileLock::ashmemLock(LockType lockType, bool wait, bool unLockFirstIfNeeded, bool *tryAgain) {
m_lockInfo.l_type = LockType2FlockType(lockType);
if (unLockFirstIfNeeded) {
// try lock
Expand All @@ -66,6 +66,9 @@ bool FileLock::ashmemLock(LockType lockType, bool wait, bool unLockFirstIfNeeded
int cmd = wait ? F_SETLKW : F_SETLK;
auto ret = fcntl(m_fd, cmd, &m_lockInfo);
if (ret != 0) {
if (tryAgain) {
*tryAgain = (errno == EAGAIN);
}
if (wait) {
MMKVError("fail to lock fd=%d, ret=%d, error:%s", m_fd, ret, strerror(errno));
}
Expand Down
9 changes: 7 additions & 2 deletions Core/InterProcessLock_Win32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ static DWORD LockType2Flag(LockType lockType) {
return flag;
}

bool FileLock::platformLock(LockType lockType, bool wait, bool unLockFirstIfNeeded) {
bool FileLock::platformLock(LockType lockType, bool wait, bool unLockFirstIfNeeded, bool *tryAgain) {
auto realLockType = LockType2Flag(lockType);
auto flag = wait ? realLockType : (realLockType | LOCKFILE_FAIL_IMMEDIATELY);
if (unLockFirstIfNeeded) {
Expand All @@ -56,7 +56,12 @@ bool FileLock::platformLock(LockType lockType, bool wait, bool unLockFirstIfNeed

auto ret = LockFileEx(m_fd, flag, 0, 1, 0, &m_overLapped);
if (!ret) {
MMKVError("fail to lock fd=%p, error:%d", m_fd, GetLastError());
if (tryAgain) {
*tryAgain = (GetLastError() == ERROR_LOCK_VIOLATION);
}
if (wait) {
MMKVError("fail to lock fd=%p, error:%d", m_fd, GetLastError());
}
// try recover my shared-lock
if (unLockFirstIfNeeded) {
ret = LockFileEx(m_fd, LockType2Flag(SharedLockType), 0, 1, 0, &m_overLapped);
Expand Down
14 changes: 13 additions & 1 deletion Core/KeyValueHolder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,21 @@ KeyValueHolderCrypt::~KeyValueHolderCrypt() {
}
}

uint32_t KeyValueHolderCrypt::realValueSize() const {
switch (type) {
case KeyValueHolderType_Direct:
return paddedSize;
case KeyValueHolderType_Offset:
return valueSize;
case KeyValueHolderType_Memory:
return memSize;
}
return 0;
}

// get decrypt data with [position, -1)
static MMBuffer decryptBuffer(AESCrypt &crypter, const MMBuffer &inputBuffer, size_t position) {
static uint8_t smallBuffer[16];
static size_t smallBuffer[16 / sizeof(size_t)];
auto basePtr = (uint8_t *) inputBuffer.getPtr();
auto ptr = basePtr;
for (size_t index = sizeof(smallBuffer); index < position; index += sizeof(smallBuffer)) {
Expand Down
2 changes: 2 additions & 0 deletions Core/KeyValueHolder.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ struct KeyValueHolderCrypt {

~KeyValueHolderCrypt();

uint32_t realValueSize() const;

MMBuffer toMMBuffer(const void *basePtr, const AESCrypt *crypter) const;

std::tuple<uint32_t, uint32_t, AESCryptStatus *> toTuple() {
Expand Down
6 changes: 6 additions & 0 deletions Core/MMKV.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ MMKV::~MMKV() {
delete m_sharedProcessModeLock;
delete m_exclusiveProcessModeLock;
#endif

MMKVInfo("destruct [%s]", m_mmapID.c_str());
}

MMKV *MMKV::defaultMMKV(MMKVMode mode, string *cryptKey) {
Expand Down Expand Up @@ -163,12 +165,16 @@ void initialize() {
AES_encrypt = openssl_aes_armv8_encrypt;
AES_decrypt = openssl_aes_armv8_decrypt;
MMKVInfo("armv8 AES instructions is supported");
} else {
MMKVInfo("armv8 AES instructions is not supported");
}
# endif // MMKV_DISABLE_CRYPT
# ifdef MMKV_USE_ARMV8_CRC32
if (hwcaps & HWCAP_CRC32) {
CRC32 = mmkv::armv8_crc32;
MMKVInfo("armv8 CRC32 instructions is supported");
} else {
MMKVInfo("armv8 CRC32 instructions is not supported");
}
# endif // MMKV_USE_ARMV8_CRC32
#endif // __aarch64__ && defined(__linux__)
Expand Down
Loading

0 comments on commit 28e867c

Please sign in to comment.