Skip to content

Commit 3fb8250

Browse files
authored
This gets rid of the silly ALLOW_SAME_PAGE_BUFFER_OVERRUN (simdjson#268)
1 parent 7be2998 commit 3fb8250

File tree

7 files changed

+13
-79
lines changed

7 files changed

+13
-79
lines changed

include/simdjson/common_defs.h

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -43,38 +43,9 @@
4343
#define unlikely(x) x
4444
#endif
4545

46-
// For Visual Studio compilers, same-page buffer overrun is not fine.
47-
#define ALLOW_SAME_PAGE_BUFFER_OVERRUN false
4846

4947
#else
5048

51-
// For non-Visual Studio compilers, we may assume that same-page buffer overrun
52-
// is fine. However, it will make it difficult to be "valgrind clean".
53-
//#ifndef ALLOW_SAME_PAGE_BUFFER_OVERRUN
54-
//#define ALLOW_SAME_PAGE_BUFFER_OVERRUN true
55-
//#else
56-
#define ALLOW_SAME_PAGE_BUFFER_OVERRUN false
57-
//#endif
58-
59-
// The following is likely unnecessarily complex.
60-
#ifdef __SANITIZE_ADDRESS__
61-
// we have GCC, stuck with https://gcc.gnu.org/bugzilla/show_bug.cgi?id=67368
62-
#define ALLOW_SAME_PAGE_BUFFER_OVERRUN false
63-
#elif defined(__has_feature)
64-
// we have CLANG?
65-
// todo: if we're setting ALLOW_SAME_PAGE_BUFFER_OVERRUN to false, why do we
66-
// have a non-empty qualifier?
67-
#if (__has_feature(address_sanitizer))
68-
#define ALLOW_SAME_PAGE_BUFFER_OVERRUN_QUALIFIER \
69-
__attribute__((no_sanitize("address")))
70-
#endif
71-
#endif
72-
73-
#if defined(__has_feature)
74-
#if (__has_feature(memory_sanitizer))
75-
#define LENIENT_MEM_SANITIZER __attribute__((no_sanitize("memory")))
76-
#endif
77-
#endif
7849

7950
#define really_inline inline __attribute__((always_inline, unused))
8051
#define never_inline inline __attribute__((noinline, unused))
@@ -91,12 +62,4 @@
9162

9263
#endif // MSC_VER
9364

94-
// if it does not apply, make it an empty macro
95-
#ifndef ALLOW_SAME_PAGE_BUFFER_OVERRUN_QUALIFIER
96-
#define ALLOW_SAME_PAGE_BUFFER_OVERRUN_QUALIFIER
97-
#endif
98-
#ifndef LENIENT_MEM_SANITIZER
99-
#define LENIENT_MEM_SANITIZER
100-
#endif
101-
10265
#endif // SIMDJSON_COMMON_DEFS_H

include/simdjson/jsonparser.h

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -29,38 +29,12 @@ int json_parse_implementation(const uint8_t *buf, size_t len, ParsedJson &pj,
2929
}
3030
bool reallocated = false;
3131
if (realloc_if_needed) {
32-
#if ALLOW_SAME_PAGE_BUFFER_OVERRUN
33-
// realloc is needed if the end of the memory crosses a page
34-
#ifdef _MSC_VER
35-
SYSTEM_INFO sysInfo;
36-
GetSystemInfo(&sysInfo);
37-
long page_size = sysInfo.dwPageSize;
38-
#else
39-
long page_size = sysconf(_SC_PAGESIZE);
40-
#endif
41-
//////////////
42-
// We want to check that buf + len - 1 and buf + len - 1 + SIMDJSON_PADDING
43-
// are in the same page.
44-
// That is, we want to check that
45-
// (buf + len - 1) / page_size == (buf + len - 1 + SIMDJSON_PADDING) /
46-
// page_size That's true if (buf + len - 1) % page_size + SIMDJSON_PADDING <
47-
// page_size.
48-
///////////
49-
if ((reinterpret_cast<uintptr_t>(buf + len - 1) % page_size) +
50-
SIMDJSON_PADDING <
51-
static_cast<uintptr_t>(page_size)) {
52-
#else // SIMDJSON_SAFE_SAME_PAGE_READ_OVERRUN
53-
if (true) { // if not SIMDJSON_SAFE_SAME_PAGE_READ_OVERRUN, we always
54-
// reallocate
55-
#endif
5632
const uint8_t *tmp_buf = buf;
5733
buf = (uint8_t *)allocate_padded_buffer(len);
5834
if (buf == NULL)
5935
return simdjson::MEMALLOC;
6036
memcpy((void *)buf, tmp_buf, len);
6137
reallocated = true;
62-
} // if (true) OR if ( (reinterpret_cast<uintptr_t>(buf + len - 1) %
63-
// page_size ) + SIMDJSON_PADDING < static_cast<uintptr_t>(page_size) ) {
6438
} // if(realloc_if_needed) {
6539
int stage1_is_ok = simdjson::find_structural_bits<T>(buf, len, pj);
6640
if (stage1_is_ok != simdjson::SUCCESS) {

include/simdjson/stage2_build_tape.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ really_inline bool is_valid_null_atom(const uint8_t *loc) {
7272
}
7373

7474
template <Architecture T = Architecture::NATIVE>
75-
WARN_UNUSED ALLOW_SAME_PAGE_BUFFER_OVERRUN_QUALIFIER LENIENT_MEM_SANITIZER int
75+
WARN_UNUSED int
7676
unified_machine(const uint8_t *buf, size_t len, ParsedJson &pj);
7777

7878
template <Architecture T = Architecture::NATIVE>

include/simdjson/stage2_build_tape_common.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,9 @@ namespace simdjson {
4242
* for documentation.
4343
***********/
4444
template <>
45-
WARN_UNUSED ALLOW_SAME_PAGE_BUFFER_OVERRUN_QUALIFIER LENIENT_MEM_SANITIZER int
45+
WARN_UNUSED int
4646
unified_machine<TARGETED_ARCHITECTURE>(const uint8_t *buf, size_t len,
4747
ParsedJson &pj) {
48-
if (ALLOW_SAME_PAGE_BUFFER_OVERRUN) {
49-
memset((uint8_t *)buf + len, 0, SIMDJSON_PADDING); /* to please valgrind */
50-
}
5148
uint32_t i = 0; /* index of the structural character (0,1,2,3...) */
5249
uint32_t idx; /* location of the structural character in the input (buf) */
5350
uint8_t c; /* used to track the (structural) character we are looking at,

include/simdjson/stringparsing.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ parse_string_helper find_bs_bits_and_quote_bits(const uint8_t *src,
9090
uint8_t *dst);
9191

9292
template <Architecture T>
93-
WARN_UNUSED ALLOW_SAME_PAGE_BUFFER_OVERRUN_QUALIFIER LENIENT_MEM_SANITIZER
93+
WARN_UNUSED
9494
really_inline bool
9595
parse_string(UNUSED const uint8_t *buf, UNUSED size_t len, ParsedJson &pj,
9696
UNUSED const uint32_t depth, UNUSED uint32_t offset);

include/simdjson/stringparsing_common.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ TARGETED_REGION
1010
namespace simdjson {
1111

1212
template <>
13-
WARN_UNUSED ALLOW_SAME_PAGE_BUFFER_OVERRUN_QUALIFIER LENIENT_MEM_SANITIZER
13+
WARN_UNUSED
1414
really_inline bool
1515
parse_string<TARGETED_ARCHITECTURE>(UNUSED const uint8_t *buf,
1616
UNUSED size_t len, ParsedJson &pj,

src/jsonparser.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,21 @@ namespace simdjson {
1212

1313
// function pointer type for json_parse
1414
using json_parse_functype = int(const uint8_t *buf, size_t len, ParsedJson &pj,
15-
bool realloc_if_needed);
15+
bool realloc);
1616

1717
// Pointer that holds the json_parse implementation corresponding to the
1818
// available SIMD instruction set
1919
extern std::atomic<json_parse_functype *> json_parse_ptr;
2020

2121
int json_parse(const uint8_t *buf, size_t len, ParsedJson &pj,
22-
bool realloc_if_needed) {
23-
return json_parse_ptr.load(std::memory_order_relaxed)(buf, len, pj, realloc_if_needed);
22+
bool realloc) {
23+
return json_parse_ptr.load(std::memory_order_relaxed)(buf, len, pj, realloc);
2424
}
2525

2626
int json_parse(const char *buf, size_t len, ParsedJson &pj,
27-
bool realloc_if_needed) {
27+
bool realloc) {
2828
return json_parse_ptr.load(std::memory_order_relaxed)(reinterpret_cast<const uint8_t *>(buf), len, pj,
29-
realloc_if_needed);
29+
realloc);
3030
}
3131

3232
Architecture find_best_supported_implementation() {
@@ -50,7 +50,7 @@ Architecture find_best_supported_implementation() {
5050

5151
// Responsible to select the best json_parse implementation
5252
int json_parse_dispatch(const uint8_t *buf, size_t len, ParsedJson &pj,
53-
bool realloc_if_needed) {
53+
bool realloc) {
5454
Architecture best_implementation = find_best_supported_implementation();
5555
// Selecting the best implementation
5656
switch (best_implementation) {
@@ -72,18 +72,18 @@ int json_parse_dispatch(const uint8_t *buf, size_t len, ParsedJson &pj,
7272
return simdjson::UNEXPECTED_ERROR;
7373
}
7474

75-
return json_parse_ptr.load(std::memory_order_relaxed)(buf, len, pj, realloc_if_needed);
75+
return json_parse_ptr.load(std::memory_order_relaxed)(buf, len, pj, realloc);
7676
}
7777

7878
std::atomic<json_parse_functype *> json_parse_ptr = &json_parse_dispatch;
7979

8080
WARN_UNUSED
8181
ParsedJson build_parsed_json(const uint8_t *buf, size_t len,
82-
bool realloc_if_needed) {
82+
bool realloc) {
8383
ParsedJson pj;
8484
bool ok = pj.allocate_capacity(len);
8585
if (ok) {
86-
json_parse(buf, len, pj, realloc_if_needed);
86+
json_parse(buf, len, pj, realloc);
8787
} else {
8888
std::cerr << "failure during memory allocation " << std::endl;
8989
}

0 commit comments

Comments
 (0)