hack.cpp
3.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
* Copyright 2017 Realm Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "hack.hpp"
#include <string.h>
#include <android/log.h>
#include <realm/util/assert.hpp>
#ifndef REALM_WRAP_MEMMOVE
#error "REALM_WRAP_MEMMOVE is not defined!"
#endif
#if REALM_WRAP_MEMMOVE
extern "C" {
void* __wrap_memmove(void *dest, const void *src, size_t n);
void* __real_memmove(void *dest, const void *src, size_t n);
void* __wrap_memcpy(void *dest, const void *src, size_t n);
void* __real_memcpy(void *dest, const void *src, size_t n);
}
using namespace realm::jni_util;
typedef void* (*MemMoveFunc)(void *dest, const void *src, size_t n);
static MemMoveFunc s_wrap_memmove_ptr = &__real_memmove;
static MemMoveFunc s_wrap_memcpy_ptr = &__real_memcpy;
static void* hacked_memmove(void* s1, const void* s2, size_t n)
{
// adapted from https://github.com/dryc/libc11/blob/master/src/string/memmove.c
char* dest = (char*)s1;
const char* src = (const char*)s2;
if (dest <= src) {
while (n--) {
*dest++ = *src++;
}
}
else {
src += n;
dest += n;
while (n--) {
*--dest = *--src;
}
}
return static_cast<void*>(s1);
}
static void* hacked_memcpy(void* s1, const void* s2, size_t n)
{
// adapted from https://github.com/dryc/libc11/blob/master/src/string/memcpy.c
char* dest = (char*)s1;
const char* src = (const char*)s2;
while (n--) {
*dest++ = *src++;
}
return static_cast<void*>(s1);
}
void* __wrap_memmove(void *dest, const void *src, size_t n)
{
return (*s_wrap_memmove_ptr)(dest, src, n);
}
void* __wrap_memcpy(void *dest, const void *src, size_t n)
{
return (*s_wrap_memcpy_ptr)(dest, src, n);
}
// See https://github.com/realm/realm-java/issues/3651#issuecomment-290290228
// There is a bug in memmove for some Samsung devices which will return "dest-n" instead of dest.
// The bug was originally found by QT, see https://bugreports.qt.io/browse/QTBUG-34984 .
// To work around it, we use linker's wrap feature to use a pure C implementation of memmove if the device has the
// problem.
static void check_memmove()
{
char* array = strdup("Foobar");
size_t len = strlen(array);
void* ptr = __real_memmove(array + 1, array, len - 1);
if (ptr != array + 1 || strncmp(array, "FFooba", len) != 0) {
__android_log_print(ANDROID_LOG_DEBUG, "JSRealm", "memmove is broken on this device. Switching to the builtin implementation.");
s_wrap_memmove_ptr = &hacked_memmove;
s_wrap_memcpy_ptr = &hacked_memcpy;
}
free(array);
}
#endif
namespace realm {
namespace jni_util {
void hack_init()
{
#if REALM_WRAP_MEMMOVE
check_memmove();
#endif
}
}
}