2014-07-10 03:40:08 +08:00
|
|
|
//===- SpecialCaseListTest.cpp - Unit tests for SpecialCaseList -----------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2015-02-05 01:39:48 +08:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
2014-07-10 03:40:08 +08:00
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
|
|
|
#include "llvm/Support/SpecialCaseList.h"
|
|
|
|
#include "gtest/gtest.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class SpecialCaseListTest : public ::testing::Test {
|
|
|
|
protected:
|
2014-09-03 02:13:54 +08:00
|
|
|
std::unique_ptr<SpecialCaseList> makeSpecialCaseList(StringRef List,
|
|
|
|
std::string &Error) {
|
2014-08-28 04:14:18 +08:00
|
|
|
std::unique_ptr<MemoryBuffer> MB = MemoryBuffer::getMemBuffer(List);
|
2014-07-10 03:40:08 +08:00
|
|
|
return SpecialCaseList::create(MB.get(), Error);
|
|
|
|
}
|
|
|
|
|
2014-09-03 02:13:54 +08:00
|
|
|
std::unique_ptr<SpecialCaseList> makeSpecialCaseList(StringRef List) {
|
2014-07-10 03:40:08 +08:00
|
|
|
std::string Error;
|
2014-09-03 02:13:54 +08:00
|
|
|
auto SCL = makeSpecialCaseList(List, Error);
|
2014-07-10 03:40:08 +08:00
|
|
|
assert(SCL);
|
|
|
|
assert(Error == "");
|
|
|
|
return SCL;
|
|
|
|
}
|
2015-02-05 01:39:48 +08:00
|
|
|
|
|
|
|
std::string makeSpecialCaseListFile(StringRef Contents) {
|
|
|
|
int FD;
|
|
|
|
SmallString<64> Path;
|
|
|
|
sys::fs::createTemporaryFile("SpecialCaseListTest", "temp", FD, Path);
|
|
|
|
raw_fd_ostream OF(FD, true, true);
|
|
|
|
OF << Contents;
|
|
|
|
OF.close();
|
|
|
|
return Path.str();
|
|
|
|
}
|
2014-07-10 03:40:08 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
TEST_F(SpecialCaseListTest, Basic) {
|
2014-09-03 02:13:54 +08:00
|
|
|
std::unique_ptr<SpecialCaseList> SCL =
|
2014-07-10 03:40:08 +08:00
|
|
|
makeSpecialCaseList("# This is a comment.\n"
|
|
|
|
"\n"
|
|
|
|
"src:hello\n"
|
|
|
|
"src:bye\n"
|
|
|
|
"src:hi=category\n"
|
2014-09-03 02:13:54 +08:00
|
|
|
"src:z*=category\n");
|
2014-07-10 03:40:08 +08:00
|
|
|
EXPECT_TRUE(SCL->inSection("src", "hello"));
|
|
|
|
EXPECT_TRUE(SCL->inSection("src", "bye"));
|
|
|
|
EXPECT_TRUE(SCL->inSection("src", "hi", "category"));
|
|
|
|
EXPECT_TRUE(SCL->inSection("src", "zzzz", "category"));
|
|
|
|
EXPECT_FALSE(SCL->inSection("src", "hi"));
|
|
|
|
EXPECT_FALSE(SCL->inSection("fun", "hello"));
|
|
|
|
EXPECT_FALSE(SCL->inSection("src", "hello", "category"));
|
|
|
|
}
|
|
|
|
|
2014-11-20 09:27:19 +08:00
|
|
|
TEST_F(SpecialCaseListTest, GlobalInit) {
|
2014-09-03 02:13:54 +08:00
|
|
|
std::unique_ptr<SpecialCaseList> SCL =
|
|
|
|
makeSpecialCaseList("global:foo=init\n");
|
2014-07-10 03:40:08 +08:00
|
|
|
EXPECT_FALSE(SCL->inSection("global", "foo"));
|
|
|
|
EXPECT_FALSE(SCL->inSection("global", "bar"));
|
|
|
|
EXPECT_TRUE(SCL->inSection("global", "foo", "init"));
|
|
|
|
EXPECT_FALSE(SCL->inSection("global", "bar", "init"));
|
|
|
|
|
2014-09-03 02:13:54 +08:00
|
|
|
SCL = makeSpecialCaseList("type:t2=init\n");
|
2014-07-10 03:40:08 +08:00
|
|
|
EXPECT_FALSE(SCL->inSection("type", "t1"));
|
|
|
|
EXPECT_FALSE(SCL->inSection("type", "t2"));
|
|
|
|
EXPECT_FALSE(SCL->inSection("type", "t1", "init"));
|
|
|
|
EXPECT_TRUE(SCL->inSection("type", "t2", "init"));
|
|
|
|
|
2014-09-03 02:13:54 +08:00
|
|
|
SCL = makeSpecialCaseList("src:hello=init\n");
|
2014-07-10 03:40:08 +08:00
|
|
|
EXPECT_FALSE(SCL->inSection("src", "hello"));
|
|
|
|
EXPECT_FALSE(SCL->inSection("src", "bye"));
|
|
|
|
EXPECT_TRUE(SCL->inSection("src", "hello", "init"));
|
|
|
|
EXPECT_FALSE(SCL->inSection("src", "bye", "init"));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(SpecialCaseListTest, Substring) {
|
2014-09-03 02:13:54 +08:00
|
|
|
std::unique_ptr<SpecialCaseList> SCL = makeSpecialCaseList("src:hello\n"
|
|
|
|
"fun:foo\n"
|
|
|
|
"global:bar\n");
|
2014-07-10 03:40:08 +08:00
|
|
|
EXPECT_FALSE(SCL->inSection("src", "othello"));
|
|
|
|
EXPECT_FALSE(SCL->inSection("fun", "tomfoolery"));
|
|
|
|
EXPECT_FALSE(SCL->inSection("global", "bartender"));
|
|
|
|
|
2014-09-03 02:13:54 +08:00
|
|
|
SCL = makeSpecialCaseList("fun:*foo*\n");
|
2014-07-10 03:40:08 +08:00
|
|
|
EXPECT_TRUE(SCL->inSection("fun", "tomfoolery"));
|
|
|
|
EXPECT_TRUE(SCL->inSection("fun", "foobar"));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(SpecialCaseListTest, InvalidSpecialCaseList) {
|
|
|
|
std::string Error;
|
|
|
|
EXPECT_EQ(nullptr, makeSpecialCaseList("badline", Error));
|
2015-02-05 01:39:48 +08:00
|
|
|
EXPECT_EQ("malformed line 1: 'badline'", Error);
|
2014-07-10 03:40:08 +08:00
|
|
|
EXPECT_EQ(nullptr, makeSpecialCaseList("src:bad[a-", Error));
|
2015-02-05 01:39:48 +08:00
|
|
|
EXPECT_EQ("malformed regex in line 1: 'bad[a-': invalid character range",
|
2014-07-10 03:40:08 +08:00
|
|
|
Error);
|
|
|
|
EXPECT_EQ(nullptr, makeSpecialCaseList("src:a.c\n"
|
|
|
|
"fun:fun(a\n",
|
|
|
|
Error));
|
2015-02-05 01:39:48 +08:00
|
|
|
EXPECT_EQ("malformed regex in line 2: 'fun(a': parentheses not balanced",
|
2014-07-10 03:40:08 +08:00
|
|
|
Error);
|
2015-02-05 01:39:48 +08:00
|
|
|
std::vector<std::string> Files(1, "unexisting");
|
|
|
|
EXPECT_EQ(nullptr, SpecialCaseList::create(Files, Error));
|
|
|
|
EXPECT_EQ(0U, Error.find("can't open file 'unexisting':"));
|
2014-07-10 03:40:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(SpecialCaseListTest, EmptySpecialCaseList) {
|
2014-09-03 02:13:54 +08:00
|
|
|
std::unique_ptr<SpecialCaseList> SCL = makeSpecialCaseList("");
|
2014-07-10 03:40:08 +08:00
|
|
|
EXPECT_FALSE(SCL->inSection("foo", "bar"));
|
|
|
|
}
|
|
|
|
|
2015-02-05 01:39:48 +08:00
|
|
|
TEST_F(SpecialCaseListTest, MultipleBlacklists) {
|
|
|
|
std::vector<std::string> Files;
|
|
|
|
Files.push_back(makeSpecialCaseListFile("src:bar\n"
|
|
|
|
"src:*foo*\n"
|
|
|
|
"src:ban=init\n"));
|
|
|
|
Files.push_back(makeSpecialCaseListFile("src:baz\n"
|
|
|
|
"src:*fog*\n"));
|
|
|
|
auto SCL = SpecialCaseList::createOrDie(Files);
|
|
|
|
EXPECT_TRUE(SCL->inSection("src", "bar"));
|
|
|
|
EXPECT_TRUE(SCL->inSection("src", "baz"));
|
|
|
|
EXPECT_FALSE(SCL->inSection("src", "ban"));
|
|
|
|
EXPECT_TRUE(SCL->inSection("src", "ban", "init"));
|
|
|
|
EXPECT_TRUE(SCL->inSection("src", "tomfoolery"));
|
|
|
|
EXPECT_TRUE(SCL->inSection("src", "tomfoglery"));
|
2016-09-02 08:51:34 +08:00
|
|
|
for (auto &Path : Files)
|
|
|
|
sys::fs::remove(Path);
|
2014-07-10 03:40:08 +08:00
|
|
|
}
|
|
|
|
|
Use trigrams to speed up SpecialCaseList.
Summary:
it's often the case when the rules in the SpecialCaseList
are of the form hel.o*bar. That gives us a chance to build
trigram index to quickly discard 99% of inputs without
running a full regex. A similar idea was used in Google Code Search
as described in the blog post:
https://swtch.com/~rsc/regexp/regexp4.html
The check is defeated, if there's at least one regex
more complicated than that. In this case, all inputs
will go through the regex. That said, the real-world
rules are often simple or can be simplied. That considerably
speeds up compiling Chromium with CFI and UBSan.
As measured on Chromium's content_message_generator.cc:
before, CFI: 44 s
after, CFI: 23 s
after, CFI, no blacklist: 23 s (~1% slower, but 3 runs were unable to show the difference)
after, regular compilation to bitcode: 23 s
Reviewers: pcc
Subscribers: mgorny, llvm-commits
Differential Revision: https://reviews.llvm.org/D27188
llvm-svn: 288303
2016-12-01 10:54:54 +08:00
|
|
|
TEST_F(SpecialCaseListTest, NoTrigramsInRules) {
|
|
|
|
std::unique_ptr<SpecialCaseList> SCL = makeSpecialCaseList("fun:b.r\n"
|
|
|
|
"fun:za*az\n");
|
|
|
|
EXPECT_TRUE(SCL->inSection("fun", "bar"));
|
|
|
|
EXPECT_FALSE(SCL->inSection("fun", "baz"));
|
|
|
|
EXPECT_TRUE(SCL->inSection("fun", "zakaz"));
|
|
|
|
EXPECT_FALSE(SCL->inSection("fun", "zaraza"));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(SpecialCaseListTest, NoTrigramsInARule) {
|
|
|
|
std::unique_ptr<SpecialCaseList> SCL = makeSpecialCaseList("fun:*bar*\n"
|
|
|
|
"fun:za*az\n");
|
|
|
|
EXPECT_TRUE(SCL->inSection("fun", "abara"));
|
|
|
|
EXPECT_FALSE(SCL->inSection("fun", "bor"));
|
|
|
|
EXPECT_TRUE(SCL->inSection("fun", "zakaz"));
|
|
|
|
EXPECT_FALSE(SCL->inSection("fun", "zaraza"));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(SpecialCaseListTest, RepetitiveRule) {
|
|
|
|
std::unique_ptr<SpecialCaseList> SCL = makeSpecialCaseList("fun:*bar*bar*bar*bar*\n"
|
|
|
|
"fun:bar*\n");
|
|
|
|
EXPECT_TRUE(SCL->inSection("fun", "bara"));
|
|
|
|
EXPECT_FALSE(SCL->inSection("fun", "abara"));
|
|
|
|
EXPECT_TRUE(SCL->inSection("fun", "barbarbarbar"));
|
|
|
|
EXPECT_TRUE(SCL->inSection("fun", "abarbarbarbar"));
|
|
|
|
EXPECT_FALSE(SCL->inSection("fun", "abarbarbar"));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(SpecialCaseListTest, SpecialSymbolRule) {
|
|
|
|
std::unique_ptr<SpecialCaseList> SCL = makeSpecialCaseList("src:*c\\+\\+abi*\n");
|
|
|
|
EXPECT_TRUE(SCL->inSection("src", "c++abi"));
|
|
|
|
EXPECT_FALSE(SCL->inSection("src", "c\\+\\+abi"));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(SpecialCaseListTest, PopularTrigram) {
|
|
|
|
std::unique_ptr<SpecialCaseList> SCL = makeSpecialCaseList("fun:*aaaaaa*\n"
|
|
|
|
"fun:*aaaaa*\n"
|
|
|
|
"fun:*aaaa*\n"
|
|
|
|
"fun:*aaa*\n");
|
|
|
|
EXPECT_TRUE(SCL->inSection("fun", "aaa"));
|
|
|
|
EXPECT_TRUE(SCL->inSection("fun", "aaaa"));
|
|
|
|
EXPECT_TRUE(SCL->inSection("fun", "aaaabbbaaa"));
|
|
|
|
}
|
|
|
|
|
2016-12-03 07:30:16 +08:00
|
|
|
TEST_F(SpecialCaseListTest, EscapedSymbols) {
|
|
|
|
std::unique_ptr<SpecialCaseList> SCL = makeSpecialCaseList("src:*c\\+\\+abi*\n"
|
|
|
|
"src:*hello\\\\world*\n");
|
|
|
|
EXPECT_TRUE(SCL->inSection("src", "dir/c++abi"));
|
|
|
|
EXPECT_FALSE(SCL->inSection("src", "dir/c\\+\\+abi"));
|
|
|
|
EXPECT_FALSE(SCL->inSection("src", "c\\+\\+abi"));
|
|
|
|
EXPECT_TRUE(SCL->inSection("src", "C:\\hello\\world"));
|
|
|
|
EXPECT_TRUE(SCL->inSection("src", "hello\\world"));
|
|
|
|
EXPECT_FALSE(SCL->inSection("src", "hello\\\\world"));
|
|
|
|
}
|
|
|
|
|
2015-02-05 01:39:48 +08:00
|
|
|
}
|