Commit 5ae81138 authored by Michael Lee's avatar Michael Lee Committed by Facebook Github Bot 7

Remove portability/Stdlib.{h,cpp}

Summary: This was wrong and is like better solved, not with a shim layer, but by not using such a platform-specific function in the first place.

Reviewed By: yfeldblum

Differential Revision: D3001253

fb-gh-sync-id: 460cc46b1c9adc3d229a07cb290270f7afbbb1e0
shipit-source-id: 460cc46b1c9adc3d229a07cb290270f7afbbb1e0
parent b1eb6819
......@@ -271,7 +271,7 @@ nobase_follyinclude_HEADERS = \
portability/Constexpr.h \
portability/Environment.h \
portability/GFlags.h \
portability/Stdlib.h \
portability/Memory.h \
portability/Strings.h \
portability/Syscall.h \
portability/SysStat.h \
......@@ -406,7 +406,6 @@ libfolly_la_SOURCES = \
MacAddress.cpp \
MemoryMapping.cpp \
portability/Environment.cpp \
portability/Stdlib.cpp \
portability/Strings.cpp \
portability/SysStat.cpp \
portability/SysTime.cpp \
......
......@@ -18,6 +18,7 @@
#define FOLLY_MEMORY_H_
#include <folly/Traits.h>
#include <folly/portability/Memory.h>
#include <cstddef>
#include <cstdlib>
......
......@@ -14,29 +14,42 @@
* limitations under the License.
*/
#include <folly/portability/Stdlib.h>
#include <folly/portability/Memory.h>
#include <errno.h>
#if defined(__ANDROID__)
#include <cerrno>
#include <cstdlib>
#ifdef __ANDROID__
#include <android/api-level.h>
#endif
namespace folly {
namespace detail {
#ifdef _WIN32
void* aligned_malloc(size_t size, size_t align) { return nullptr; }
void aligned_free(void* aligned_ptr) {}
#elif defined(__ANDROID__) && (__ANDROID_API__ <= 15)
#if (__ANDROID_API__ <= 15)
void* aligned_malloc(size_t size, size_t align) { return memalign(align, size) }
int posix_memalign(void** memptr, size_t alignment, size_t size) {
int rc = 0;
int saved_errno = errno;
void aligned_free(void* aligned_ptr) { free(aligned_ptr); }
#else
// Use poxis_memalign, but mimic the behavior of memalign
void* aligned_malloc(size_t size, size_t align) {
void* ptr = nullptr;
ptr = memalign(alignment, size);
if (nullptr == ptr) {
rc = errno;
} else {
*memptr = ptr;
int rc = posix_memalign(&ptr, align, size);
if (rc == 0) {
return ptr;
}
errno = saved_errno;
return rc;
errno = rc;
return nullptr;
}
void aligned_free(void* aligned_ptr) { free(aligned_ptr); }
#endif
#endif
}
}
......@@ -18,4 +18,11 @@
#include <cstdlib>
extern int posix_memalign(void** memptr, size_t alignment, size_t size);
namespace folly {
namespace detail {
void* aligned_malloc(size_t size, size_t align);
void aligned_free(void* aligned_ptr);
}
}
......@@ -19,7 +19,7 @@
#include <folly/Range.h>
#include <folly/portability/Stdlib.h>
#include <folly/portability/Memory.h>
#include <sys/mman.h>
#include <array>
......@@ -34,6 +34,7 @@
#include <gtest/gtest.h>
using namespace folly;
using namespace folly::detail;
using namespace std;
static_assert(std::is_literal_type<StringPiece>::value, "");
......@@ -977,18 +978,19 @@ const size_t kPageSize = 4096;
void createProtectedBuf(StringPiece& contents, char** buf) {
ASSERT_LE(contents.size(), kPageSize);
const size_t kSuccess = 0;
if (kSuccess != posix_memalign((void**)buf, kPageSize, 4 * kPageSize)) {
ASSERT_FALSE(true);
}
mprotect(*buf + kPageSize, kPageSize, PROT_NONE);
char* pageAlignedBuf = (char*)aligned_malloc(2 * kPageSize, kPageSize);
// Protect the page after the first full page-aligned region of the
// malloc'ed buffer
mprotect(pageAlignedBuf + kPageSize, kPageSize, PROT_NONE);
size_t newBegin = kPageSize - contents.size();
memcpy(*buf + newBegin, contents.data(), contents.size());
contents.reset(*buf + newBegin, contents.size());
memcpy(pageAlignedBuf + newBegin, contents.data(), contents.size());
contents.reset(pageAlignedBuf + newBegin, contents.size());
*buf = pageAlignedBuf;
}
void freeProtectedBuf(char* buf) {
mprotect(buf + kPageSize, kPageSize, PROT_READ | PROT_WRITE);
free(buf);
aligned_free(buf);
}
TYPED_TEST(NeedleFinderTest, NoSegFault) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment