Compare commits

...

6 Commits

Author SHA1 Message Date
Ian Gulliver
109a9be193 Fix typo 2019-05-18 23:45:02 -07:00
Ian Gulliver
858b291f46 Google style guide 2019-05-18 12:11:20 -07:00
flamingcow
2102147a2f Fix asan, add tsan and ubsan 2019-05-09 21:37:04 -07:00
flamingcow
944483d13f Shared library, hardening 2019-05-09 21:29:53 -07:00
flamingcow
4d9486e281 Error on full buffer 2019-05-09 19:19:15 -07:00
flamingcow
49766e29f4 Linking order matters 2019-05-07 23:43:45 -07:00
7 changed files with 271 additions and 119 deletions

151
.clang-format Normal file
View File

@@ -0,0 +1,151 @@
---
Language: Cpp
# BasedOnStyle: Google
AccessModifierOffset: -1
AlignAfterOpenBracket: Align
AlignConsecutiveAssignments: false
AlignConsecutiveDeclarations: false
AlignEscapedNewlines: Left
AlignOperands: true
AlignTrailingComments: true
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortBlocksOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: All
AllowShortIfStatementsOnASingleLine: true
AllowShortLoopsOnASingleLine: true
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: true
AlwaysBreakTemplateDeclarations: Yes
BinPackArguments: true
BinPackParameters: true
BraceWrapping:
AfterClass: false
AfterControlStatement: false
AfterEnum: false
AfterFunction: false
AfterNamespace: false
AfterObjCDeclaration: false
AfterStruct: false
AfterUnion: false
AfterExternBlock: false
BeforeCatch: false
BeforeElse: false
IndentBraces: false
SplitEmptyFunction: true
SplitEmptyRecord: true
SplitEmptyNamespace: true
BreakBeforeBinaryOperators: None
BreakBeforeBraces: Attach
BreakBeforeInheritanceComma: false
BreakInheritanceList: BeforeColon
BreakBeforeTernaryOperators: true
BreakConstructorInitializersBeforeComma: false
BreakConstructorInitializers: BeforeColon
BreakAfterJavaFieldAnnotations: false
BreakStringLiterals: true
ColumnLimit: 80
CommentPragmas: '^ IWYU pragma:'
CompactNamespaces: false
ConstructorInitializerAllOnOneLineOrOnePerLine: true
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4
Cpp11BracedListStyle: true
DerivePointerAlignment: true
DisableFormat: false
ExperimentalAutoDetectBinPacking: false
FixNamespaceComments: true
ForEachMacros:
- foreach
- Q_FOREACH
- BOOST_FOREACH
IncludeBlocks: Preserve
IncludeCategories:
- Regex: '^<ext/.*\.h>'
Priority: 2
- Regex: '^<.*\.h>'
Priority: 1
- Regex: '^<.*'
Priority: 2
- Regex: '.*'
Priority: 3
IncludeIsMainRegex: '([-_](test|unittest))?$'
IndentCaseLabels: true
IndentPPDirectives: None
IndentWidth: 2
IndentWrappedFunctionNames: false
JavaScriptQuotes: Leave
JavaScriptWrapImports: true
KeepEmptyLinesAtTheStartOfBlocks: false
MacroBlockBegin: ''
MacroBlockEnd: ''
MaxEmptyLinesToKeep: 1
NamespaceIndentation: None
ObjCBinPackProtocolList: Never
ObjCBlockIndentWidth: 2
ObjCSpaceAfterProperty: false
ObjCSpaceBeforeProtocolList: true
PenaltyBreakAssignment: 2
PenaltyBreakBeforeFirstCallParameter: 1
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 120
PenaltyBreakString: 1000
PenaltyBreakTemplateDeclaration: 10
PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 200
PointerAlignment: Left
RawStringFormats:
- Language: Cpp
Delimiters:
- cc
- CC
- cpp
- Cpp
- CPP
- 'c++'
- 'C++'
CanonicalDelimiter: ''
BasedOnStyle: google
- Language: TextProto
Delimiters:
- pb
- PB
- proto
- PROTO
EnclosingFunctions:
- EqualsProto
- EquivToProto
- PARSE_PARTIAL_TEXT_PROTO
- PARSE_TEST_PROTO
- PARSE_TEXT_PROTO
- ParseTextOrDie
- ParseTextProtoOrDie
CanonicalDelimiter: ''
BasedOnStyle: google
ReflowComments: true
SortIncludes: true
SortUsingDeclarations: true
SpaceAfterCStyleCast: false
SpaceAfterTemplateKeyword: true
SpaceBeforeAssignmentOperators: true
SpaceBeforeCpp11BracedList: false
SpaceBeforeCtorInitializerColon: true
SpaceBeforeInheritanceColon: true
SpaceBeforeParens: ControlStatements
SpaceBeforeRangeBasedForLoopColon: true
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 2
SpacesInAngles: false
SpacesInContainerLiterals: true
SpacesInCStyleCastParentheses: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
Standard: Auto
StatementMacros:
- Q_UNUSED
- QT_REQUIRE_VERSION
TabWidth: 8
UseTab: Never
...

1
.gitignore vendored
View File

@@ -1,2 +1,3 @@
*.a
*.o
*.so

View File

@@ -1,7 +1,9 @@
FIRE_CXX ?= clang++
FIRE_CXXFLAGS ?= -O3 -std=gnu++2a -Wall -Werror
FIRE_CXXFLAGS ?= -O3 -std=gnu++2a -Wall -Werror -Wextra -fPIE -fPIC -fstack-protector-strong -fsanitize=safe-stack -fsanitize=safe-stack
FIRE_LDFLAGS ?= -fuse-ld=gold -flto -Wl,-z,relro -Wl,-z,now
FIRE_LDLIBS ?= -lglog
all: firebuf.a firebuf.o
all: firebuf.a firebuf.o firebuf.so
objects = stream_buffer.o buffer.o
@@ -9,14 +11,25 @@ firebuf.a: $(objects)
ar rcs $@ $^
firebuf.o: $(objects)
ld --relocatable --output=$@ $^
gold -z relro -z now -r --output=$@ $+
firebuf.so: $(objects)
$(FIRE_CXX) $(FIRE_CXXFLAGS) $(FIRE_LDFLAGS) -shared -o $@ $+ $(FIRE_LDLIBS)
%.o: %.cc *.h Makefile
$(FIRE_CXX) $(FIRE_CXXFLAGS) -c -o $@ $<
clean:
rm --force *.o *.a
rm --force *.so *.o *.a
asan:
$(MAKE) clean
FIRE_CXXFLAGS="-O1 -g -fsanitize=address -fno-omit-frame-pointer -std=gnu++2a -Wall -Werror" $(MAKE) all
FIRE_CXXFLAGS="-O1 -g -fsanitize=address -fno-omit-frame-pointer -std=gnu++2a -fPIE -fPIC" $(MAKE) all
tsan:
$(MAKE) clean
FIRE_CXXFLAGS="-O1 -g -fsanitize=thread -std=gnu++2a -fPIE -fPIC" $(MAKE) all
ubsan:
$(MAKE) clean
FIRE_CXXFLAGS="-O1 -g -fsanitize=undefined -std=gnu++2a -fPIE -fPIC" $(MAKE) all

View File

@@ -3,90 +3,73 @@
namespace firebuf {
ConstBuffer::ConstBuffer(const char *buf, size_t len)
: const_buf_(buf),
len_(len) {}
: const_buf_(buf), len_(len) {}
size_t ConstBuffer::ReadMaxLen() const {
return len_ - start_;
}
size_t ConstBuffer::ReadMaxLen() const { return len_ - start_; }
const char *ConstBuffer::Read(size_t len) {
if (ReadMaxLen() < len) {
return nullptr;
}
const auto *ret = &const_buf_[start_];
start_ += len;
return ret;
if (ReadMaxLen() < len) {
return nullptr;
}
const auto *ret = &const_buf_[start_];
start_ += len;
return ret;
}
bool ConstBuffer::Discard(size_t len) {
if (len > ReadMaxLen()) {
return false;
}
static_cast<void>(Read(len));
return true;
if (len > ReadMaxLen()) {
return false;
}
static_cast<void>(Read(len));
return true;
}
void ConstBuffer::ResetRead() {
start_ = commit_;
}
void ConstBuffer::ResetRead() { start_ = commit_; }
void ConstBuffer::Commit() {
commit_ = start_;
}
void ConstBuffer::Reset() {
commit_ = start_ = 0;
}
void ConstBuffer::Commit() { commit_ = start_; }
void ConstBuffer::Reset() { commit_ = start_ = 0; }
Buffer::Buffer(char *buf, size_t size, size_t len)
: ConstBuffer(buf, size),
buf_(buf),
size_(size) {
len_ = len;
: ConstBuffer(buf, size), buf_(buf), size_(size) {
len_ = len;
}
Buffer::Buffer(size_t size)
: Buffer(new char[size], size, 0) {
own_buf_.reset(buf_);
Buffer::Buffer(size_t size) : Buffer(new char[size], size, 0) {
own_buf_.reset(buf_);
}
char *Buffer::WritePtr() {
return &buf_[len_];
}
char *Buffer::WritePtr() { return &buf_[len_]; }
size_t Buffer::WriteMaxLen() const {
return size_ - len_;
}
size_t Buffer::WriteMaxLen() const { return size_ - len_; }
bool Buffer::Write(const std::string_view& str) {
if (WriteMaxLen() < str.size()) {
return false;
}
memcpy(WritePtr(), str.data(), str.size());
Wrote(str.size());
return true;
bool Buffer::Write(const std::string_view &str) {
if (WriteMaxLen() < str.size()) {
return false;
}
memcpy(WritePtr(), str.data(), str.size());
Wrote(str.size());
return true;
}
void Buffer::Wrote(ssize_t len) {
CHECK_LE(len, WriteMaxLen());
len_ += len;
CHECK_LE(len, WriteMaxLen());
len_ += len;
}
void Buffer::Consume() {
if (commit_ == 0) {
return;
}
memmove(buf_, &buf_[commit_], len_ - commit_);
len_ -= commit_;
start_ -= commit_;
commit_ = 0;
if (commit_ == 0) {
return;
}
memmove(buf_, &buf_[commit_], len_ - commit_);
len_ -= commit_;
start_ -= commit_;
commit_ = 0;
}
void Buffer::Reset() {
ConstBuffer::Reset();
len_ = 0;
ConstBuffer::Reset();
len_ = 0;
}
} // namespace firebuf
} // namespace firebuf

View File

@@ -7,51 +7,53 @@
namespace firebuf {
class ConstBuffer {
public:
ConstBuffer(const char *buf, size_t len);
public:
ConstBuffer(const char *buf, size_t len);
[[nodiscard]] size_t ReadMaxLen() const;
[[nodiscard]] virtual const char *Read(size_t len);
template<class T> [[nodiscard]] const T *ReadObj();
[[nodiscard]] size_t ReadMaxLen() const;
[[nodiscard]] virtual const char *Read(size_t len);
template <class T>
[[nodiscard]] const T *ReadObj();
bool Discard(size_t len); // like Read() but don't use the result
void ResetRead(); // next read from last commit
void Commit(); // commit read position
bool Discard(size_t len); // like Read() but don't use the result
void ResetRead(); // next read from last commit
void Commit(); // commit read position
virtual void Reset(); // full reset
virtual void Reset(); // full reset
protected:
const char *const_buf_;
size_t len_;
size_t start_ = 0;
size_t commit_ = 0;
protected:
const char *const_buf_;
size_t len_;
size_t start_ = 0;
size_t commit_ = 0;
};
class Buffer : public ConstBuffer {
public:
Buffer(char *buf, size_t size, size_t len);
Buffer(size_t size);
public:
Buffer(char *buf, size_t size, size_t len);
Buffer(size_t size);
[[nodiscard]] char *WritePtr();
[[nodiscard]] size_t WriteMaxLen() const;
bool Write(const std::string_view& str);
void Wrote(ssize_t len);
[[nodiscard]] char *WritePtr();
[[nodiscard]] size_t WriteMaxLen() const;
bool Write(const std::string_view &str);
void Wrote(ssize_t len);
void Consume(); // discard up to last commit
void Consume(); // discard up to last commit
void Reset() override; // full reset
void Reset() override; // full reset
protected:
std::unique_ptr<char[]> own_buf_;
char *buf_;
const size_t size_;
protected:
std::unique_ptr<char[]> own_buf_;
char *buf_;
const size_t size_;
};
template<class T> const T *ConstBuffer::ReadObj() {
if (ReadMaxLen() < sizeof(T)) {
return nullptr;
}
return reinterpret_cast<const T*>(Read(sizeof(T)));
template <class T>
const T *ConstBuffer::ReadObj() {
if (ReadMaxLen() < sizeof(T)) {
return nullptr;
}
return reinterpret_cast<const T *>(Read(sizeof(T)));
}
} // namespace firebuf
} // namespace firebuf

View File

@@ -2,24 +2,26 @@
namespace firebuf {
StreamBuffer::StreamBuffer(int sock, size_t size)
: Buffer(size),
sock_(sock) {}
StreamBuffer::StreamBuffer(int sock, size_t size) : Buffer(size), sock_(sock) {}
bool StreamBuffer::Refill() {
auto read_len = read(sock_, WritePtr(), WriteMaxLen());
if (read_len == -1) {
if (errno == EINTR) {
return true;
}
PLOG(ERROR) << "read()";
return false;
}
if (read_len == 0) {
return false;
}
Wrote(read_len);
return true;
if (WriteMaxLen() == 0) {
LOG(ERROR) << "buffer full";
return false;
}
auto read_len = read(sock_, WritePtr(), WriteMaxLen());
if (read_len == -1) {
if (errno == EINTR) {
return true;
}
PLOG(ERROR) << "read()";
return false;
}
if (read_len == 0) {
return false;
}
Wrote(read_len);
return true;
}
} // namespace firebuf
} // namespace firebuf

View File

@@ -5,13 +5,13 @@
namespace firebuf {
class StreamBuffer : public Buffer {
public:
StreamBuffer(int sock, size_t size);
public:
StreamBuffer(int sock, size_t size);
[[nodiscard]] bool Refill();
[[nodiscard]] bool Refill();
private:
int sock_;
private:
int sock_;
};
} // namespace firebuf
} // namespace firebuf