HDFS-15955. Make explicit_bzero cross platform (#2875)
This commit is contained in:
parent
c6539e3289
commit
bf66116407
@ -25,6 +25,7 @@
|
|||||||
#include "fs/filesystem.h"
|
#include "fs/filesystem.h"
|
||||||
#include "fs/filehandle.h"
|
#include "fs/filehandle.h"
|
||||||
#include "x-platform/utils.h"
|
#include "x-platform/utils.h"
|
||||||
|
#include "x-platform/syscall.h"
|
||||||
|
|
||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
#include <string>
|
#include <string>
|
||||||
@ -1395,7 +1396,7 @@ int hdfsGetBlockLocations(hdfsFS fs, const char *path, struct hdfsBlockLocations
|
|||||||
hdfsBlockLocations *locations = new struct hdfsBlockLocations();
|
hdfsBlockLocations *locations = new struct hdfsBlockLocations();
|
||||||
(*locations_out) = locations;
|
(*locations_out) = locations;
|
||||||
|
|
||||||
explicit_bzero(locations, sizeof(*locations));
|
XPlatform::Syscall::ClearBufferSafely(locations, sizeof(*locations));
|
||||||
locations->fileLength = ppLocations->getFileLength();
|
locations->fileLength = ppLocations->getFileLength();
|
||||||
locations->isLastBlockComplete = ppLocations->isLastBlockComplete();
|
locations->isLastBlockComplete = ppLocations->isLastBlockComplete();
|
||||||
locations->isUnderConstruction = ppLocations->isUnderConstruction();
|
locations->isUnderConstruction = ppLocations->isUnderConstruction();
|
||||||
|
@ -59,6 +59,18 @@ class Syscall {
|
|||||||
*/
|
*/
|
||||||
static bool FnMatch(const std::string& pattern, const std::string& str);
|
static bool FnMatch(const std::string& pattern, const std::string& str);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Clears the given {@link buffer} upto {@link sz_bytes} by
|
||||||
|
* filling them with zeros. This method is immune to compiler
|
||||||
|
* optimizations and guarantees that the first {@link sz_bytes} of
|
||||||
|
* {@link buffer} is cleared. The {@link buffer} must be at least
|
||||||
|
* as big as {@link sz_bytes}, the behaviour is undefined otherwise.
|
||||||
|
*
|
||||||
|
* @param buffer the pointer to the buffer to clear.
|
||||||
|
* @param sz_bytes the count of the bytes to clear.
|
||||||
|
*/
|
||||||
|
static void ClearBufferSafely(void* buffer, size_t sz_bytes);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static bool WriteToStdoutImpl(const char* message);
|
static bool WriteToStdoutImpl(const char* message);
|
||||||
};
|
};
|
||||||
|
@ -41,3 +41,10 @@ bool XPlatform::Syscall::WriteToStdoutImpl(const char* message) {
|
|||||||
const auto result = write(1, message, message_len);
|
const auto result = write(1, message, message_len);
|
||||||
return result == static_cast<ssize_t>(message_len);
|
return result == static_cast<ssize_t>(message_len);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void XPlatform::Syscall::ClearBufferSafely(void* buffer,
|
||||||
|
const size_t sz_bytes) {
|
||||||
|
if (buffer != nullptr) {
|
||||||
|
explicit_bzero(buffer, sz_bytes);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -17,6 +17,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include <Shlwapi.h>
|
#include <Shlwapi.h>
|
||||||
|
#include <WinBase.h>
|
||||||
#include <Windows.h>
|
#include <Windows.h>
|
||||||
|
|
||||||
#include "syscall.h"
|
#include "syscall.h"
|
||||||
@ -49,3 +50,10 @@ bool XPlatform::Syscall::WriteToStdoutImpl(const char* message) {
|
|||||||
WriteFile(stdout_handle, message, message_len, &bytes_written, nullptr);
|
WriteFile(stdout_handle, message, message_len, &bytes_written, nullptr);
|
||||||
return result && static_cast<unsigned long>(message_len) == bytes_written;
|
return result && static_cast<unsigned long>(message_len) == bytes_written;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void XPlatform::Syscall::ClearBufferSafely(void* buffer,
|
||||||
|
const size_t sz_bytes) {
|
||||||
|
if (buffer != nullptr) {
|
||||||
|
SecureZeroMemory(buffer, sz_bytes);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -18,6 +18,7 @@
|
|||||||
|
|
||||||
#include "hdfspp_mini_dfs.h"
|
#include "hdfspp_mini_dfs.h"
|
||||||
#include "hdfspp/hdfs_ext.h"
|
#include "hdfspp/hdfs_ext.h"
|
||||||
|
#include "x-platform/syscall.h"
|
||||||
|
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
@ -475,7 +476,7 @@ TEST_F(HdfsExtTest, TestReadStats) {
|
|||||||
hdfsFile file = hdfsOpenFile(fs, path.c_str(), O_WRONLY, 0, 0, 0);
|
hdfsFile file = hdfsOpenFile(fs, path.c_str(), O_WRONLY, 0, 0, 0);
|
||||||
EXPECT_NE(nullptr, file);
|
EXPECT_NE(nullptr, file);
|
||||||
void * buf = malloc(size);
|
void * buf = malloc(size);
|
||||||
explicit_bzero(buf, size);
|
XPlatform::Syscall::ClearBufferSafely(buf, size);
|
||||||
EXPECT_EQ(size, hdfsWrite(fs, file, buf, size));
|
EXPECT_EQ(size, hdfsWrite(fs, file, buf, size));
|
||||||
free(buf);
|
free(buf);
|
||||||
EXPECT_EQ(0, hdfsCloseFile(fs, file));
|
EXPECT_EQ(0, hdfsCloseFile(fs, file));
|
||||||
|
@ -19,6 +19,7 @@
|
|||||||
#include "hdfs/hdfs.h"
|
#include "hdfs/hdfs.h"
|
||||||
#include "hdfspp/hdfspp.h"
|
#include "hdfspp/hdfspp.h"
|
||||||
#include <native_mini_dfs.h>
|
#include <native_mini_dfs.h>
|
||||||
|
#include "x-platform/syscall.h"
|
||||||
|
|
||||||
#include <google/protobuf/io/coded_stream.h>
|
#include <google/protobuf/io/coded_stream.h>
|
||||||
#include <gmock/gmock.h>
|
#include <gmock/gmock.h>
|
||||||
@ -92,7 +93,7 @@ public:
|
|||||||
hdfsFile file = hdfsOpenFile(*this, path.c_str(), O_WRONLY, 0, 0, 0);
|
hdfsFile file = hdfsOpenFile(*this, path.c_str(), O_WRONLY, 0, 0, 0);
|
||||||
EXPECT_NE(nullptr, file);
|
EXPECT_NE(nullptr, file);
|
||||||
void * buf = malloc(size);
|
void * buf = malloc(size);
|
||||||
explicit_bzero(buf, size);
|
XPlatform::Syscall::ClearBufferSafely(buf, size);
|
||||||
EXPECT_EQ(1024, hdfsWrite(*this, file, buf, size));
|
EXPECT_EQ(1024, hdfsWrite(*this, file, buf, size));
|
||||||
EXPECT_EQ(0, hdfsCloseFile(*this, file));
|
EXPECT_EQ(0, hdfsCloseFile(*this, file));
|
||||||
free(buf);
|
free(buf);
|
||||||
|
@ -18,7 +18,9 @@
|
|||||||
|
|
||||||
#include <gtest/gtest.h>
|
#include <gtest/gtest.h>
|
||||||
|
|
||||||
|
#include <numeric>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
#include "x-platform/syscall.h"
|
#include "x-platform/syscall.h"
|
||||||
|
|
||||||
@ -45,3 +47,24 @@ TEST(XPlatformSyscall, FnMatchNegativeQuestionMark) {
|
|||||||
const std::string str("abc.doc");
|
const std::string str("abc.doc");
|
||||||
EXPECT_FALSE(XPlatform::Syscall::FnMatch(pattern, str));
|
EXPECT_FALSE(XPlatform::Syscall::FnMatch(pattern, str));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST(XPlatformSyscall, ClearBufferSafelyChars) {
|
||||||
|
std::vector<char> alphabets(26);
|
||||||
|
std::iota(alphabets.begin(), alphabets.end(), 'a');
|
||||||
|
|
||||||
|
XPlatform::Syscall::ClearBufferSafely(alphabets.data(), alphabets.size());
|
||||||
|
for (const auto alphabet : alphabets) {
|
||||||
|
EXPECT_EQ(alphabet, '\0');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(XPlatformSyscall, ClearBufferSafelyNumbers) {
|
||||||
|
std::vector<int> numbers(200);
|
||||||
|
std::iota(numbers.begin(), numbers.end(), 0);
|
||||||
|
|
||||||
|
XPlatform::Syscall::ClearBufferSafely(numbers.data(),
|
||||||
|
numbers.size() * sizeof(int));
|
||||||
|
for (const auto number : numbers) {
|
||||||
|
EXPECT_EQ(number, 0);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user