[IRMemoryMap] Use labels in the "malloc" and "free" lldb-test commands

Change the syntax of the malloc and free commands in lldb-test's
ir-memory-map subcommand to:

  <malloc> ::= <label> = malloc <size> <alignment>

  <free> ::= free <label>

This should make it easier to read and extend tests in the future, e.g
to test IRMemoryMap::WriteMemory or double-free behavior.

Differential Revision: https://reviews.llvm.org/D47646

llvm-svn: 333930
This commit is contained in:
Vedant Kumar 2018-06-04 17:11:15 +00:00
parent 675a51750a
commit c418b5cc25
4 changed files with 355 additions and 341 deletions

View File

@ -1,25 +1,25 @@
malloc 0 1
malloc 1 1
L1 = malloc 0 1
L2 = malloc 1 1
malloc 2 1
malloc 2 2
malloc 2 4
L3 = malloc 2 1
L4 = malloc 2 2
L5 = malloc 2 4
malloc 3 1
malloc 3 2
malloc 3 4
L6 = malloc 3 1
L7 = malloc 3 2
L8 = malloc 3 4
malloc 128 1
malloc 128 2
malloc 128 4
malloc 128 128
L9 = malloc 128 1
L10 = malloc 128 2
L11 = malloc 128 4
L12 = malloc 128 128
malloc 2048 1
malloc 2048 2
malloc 2048 4
L13 = malloc 2048 1
L14 = malloc 2048 2
L15 = malloc 2048 4
malloc 3968 1
malloc 3968 2
malloc 3968 4
L16 = malloc 3968 1
L17 = malloc 3968 2
L18 = malloc 3968 4
malloc 0 1
L19 = malloc 0 1

View File

@ -1,275 +1,272 @@
# This file was generated by a slightly extended version of a script
# attached to https://reviews.llvm.org/D47508.
malloc 2 4
malloc 4095 128
malloc 3 16
malloc 8192 2
malloc 1 128
malloc 3 2
free 3
free 3
malloc 32 2
malloc 2 16
free 1
malloc 2048 2
free 2
malloc 2049 4
malloc 4097 2
malloc 2 16
free 1
free 6
malloc 1 16
malloc 33 128
malloc 2 128
malloc 2 16
malloc 2 4
malloc 2 4
free 4
malloc 4 4
malloc 2 16
free 3
malloc 0 128
free 6
free 2
malloc 33 2
malloc 4095 2
free 6
malloc 2 4
malloc 0 2
free 1
free 9
free 5
malloc 32 2
malloc 4096 2
free 2
free 9
free 6
malloc 8192 16
malloc 2 4
malloc 4096 16
free 10
malloc 4 4
free 7
malloc 4 4
malloc 8193 2
malloc 1 2
free 3
free 7
malloc 4096 2
free 0
malloc 4096 4
free 3
malloc 4097 128
malloc 1 2
malloc 4 2
free 10
free 8
malloc 0 16
malloc 2049 16
free 9
malloc 8193 16
free 10
free 1
malloc 8193 2
free 11
malloc 3 16
free 5
malloc 33 128
free 0
malloc 2049 4
malloc 1 128
malloc 32 16
free 1
free 9
malloc 2048 4
free 0
free 2
malloc 5 4
free 11
malloc 2048 4
malloc 4097 16
malloc 8192 4
free 2
free 8
free 11
malloc 8192 4
free 2
malloc 8191 4
malloc 32 128
free 7
malloc 4 16
malloc 4096 128
free 12
malloc 2 4
free 9
malloc 8193 128
malloc 4095 4
malloc 2049 16
malloc 2 4
free 16
malloc 0 128
malloc 5 2
malloc 2047 4
malloc 2 2
free 10
malloc 0 128
free 6
malloc 2047 128
free 11
free 0
free 9
malloc 4 4
malloc 3 2
free 7
malloc 1 128
free 13
malloc 8193 16
malloc 4097 128
free 15
free 3
malloc 1 2
malloc 2049 4
malloc 2048 2
free 7
malloc 31 4
free 5
free 14
free 4
free 10
malloc 4 4
free 6
malloc 3 2
malloc 1 128
free 13
malloc 4 16
free 4
free 8
malloc 3 16
free 13
malloc 0 4
free 8
free 13
malloc 1 2
malloc 8192 128
free 12
malloc 2049 2
malloc 1 16
free 4
free 7
malloc 4 16
malloc 4 128
malloc 4096 16
malloc 2048 16
malloc 32 4
malloc 8193 4
free 2
malloc 3 16
malloc 8192 4
free 1
malloc 8191 2
free 3
malloc 8192 2
malloc 8192 2
free 3
free 7
malloc 31 4
malloc 2049 2
free 4
free 3
free 14
free 1
malloc 2048 4
malloc 1 4
malloc 1 4
malloc 2 4
malloc 4 16
free 12
free 14
free 3
free 0
free 12
free 10
malloc 32 16
free 5
free 9
free 4
free 9
free 3
malloc 4096 16
malloc 4 2
free 12
free 10
free 3
free 7
malloc 4097 4
malloc 4095 16
free 3
malloc 2047 16
free 12
malloc 8193 2
free 2
free 3
malloc 0 4
free 2
free 9
malloc 8192 16
malloc 2 2
malloc 4096 4
malloc 5 128
malloc 4095 4
malloc 4095 4
free 9
malloc 32 4
malloc 31 16
free 15
malloc 4097 16
malloc 2048 128
malloc 2048 2
malloc 4096 128
malloc 3 16
free 2
malloc 1 4
malloc 3 4
free 3
malloc 31 2
malloc 1 128
free 21
malloc 0 128
free 8
malloc 3 128
free 5
malloc 1 16
free 9
malloc 2048 4
free 8
free 4
malloc 31 2
malloc 1 16
free 19
malloc 2 4
malloc 2 2
free 8
malloc 8193 16
free 11
free 8
free 6
free 12
free 4
free 0
free 10
free 1
free 0
free 5
free 5
free 3
free 6
free 1
free 2
free 6
free 5
free 4
free 3
free 1
free 1
free 0
L1 = malloc 32 2
free L1
L2 = malloc 3 4
L3 = malloc 3 128
free L3
free L2
L4 = malloc 2 16
free L4
L5 = malloc 4097 4
free L5
L6 = malloc 2 2
L7 = malloc 3 2
L8 = malloc 32 16
free L6
free L8
free L7
L9 = malloc 8192 2
L10 = malloc 33 4
free L9
L11 = malloc 2047 2
L12 = malloc 5 16
L13 = malloc 4 16
L14 = malloc 2048 2
free L10
L15 = malloc 4 128
free L13
L16 = malloc 1 2
L17 = malloc 4095 128
free L12
free L15
L18 = malloc 0 128
L19 = malloc 2 128
L20 = malloc 4096 2
L21 = malloc 2 2
L22 = malloc 8192 128
L23 = malloc 4 2
L24 = malloc 4097 4
free L19
free L20
L25 = malloc 3 128
free L23
free L21
free L22
free L25
free L16
L26 = malloc 1 128
L27 = malloc 4096 4
free L14
L28 = malloc 31 2
free L24
L29 = malloc 2048 128
free L28
free L11
L30 = malloc 4 2
L31 = malloc 32 16
L32 = malloc 1 4
free L30
L33 = malloc 3 2
L34 = malloc 4096 4
free L17
free L18
free L32
L35 = malloc 4097 4
L36 = malloc 8193 2
L37 = malloc 3 2
free L34
L38 = malloc 8193 16
L39 = malloc 4096 4
L40 = malloc 8192 16
L41 = malloc 32 4
free L26
free L38
free L37
L42 = malloc 0 2
free L31
free L40
free L36
free L35
L43 = malloc 2047 2
free L41
L44 = malloc 4 4
free L43
free L33
free L42
L45 = malloc 4097 16
free L27
L46 = malloc 32 128
L47 = malloc 8191 16
L48 = malloc 1 16
L49 = malloc 32 128
L50 = malloc 3 2
L51 = malloc 4096 128
free L51
L52 = malloc 2048 4
free L29
L53 = malloc 4097 128
free L44
L54 = malloc 1 16
L55 = malloc 4095 16
L56 = malloc 2047 2
L57 = malloc 0 2
L58 = malloc 2048 2
free L48
L59 = malloc 1 4
L60 = malloc 32 16
free L50
L61 = malloc 1 4
L62 = malloc 4096 2
free L60
L63 = malloc 1 16
L64 = malloc 32 128
free L55
L65 = malloc 8192 16
free L57
L66 = malloc 1 128
free L65
free L61
free L45
free L64
free L39
L67 = malloc 2048 2
free L47
L68 = malloc 2049 4
free L63
free L68
free L54
free L59
free L52
L69 = malloc 2 2
L70 = malloc 8192 16
L71 = malloc 2049 16
L72 = malloc 3 16
L73 = malloc 4097 16
L74 = malloc 4096 2
L75 = malloc 4097 4
free L67
free L71
free L72
free L75
free L74
L76 = malloc 3 4
free L70
free L69
L77 = malloc 4 4
free L49
L78 = malloc 4096 16
L79 = malloc 33 2
free L76
L80 = malloc 2 16
free L58
free L80
free L56
L81 = malloc 1 128
free L73
L82 = malloc 1 16
free L53
free L81
free L77
L83 = malloc 2 2
L84 = malloc 3 16
free L62
L85 = malloc 2049 2
free L83
L86 = malloc 3 4
L87 = malloc 4096 128
free L86
L88 = malloc 3 2
free L82
free L66
free L84
L89 = malloc 8192 4
free L88
L90 = malloc 3 4
L91 = malloc 1 4
L92 = malloc 4097 4
L93 = malloc 5 16
L94 = malloc 2 128
L95 = malloc 4096 2
L96 = malloc 32 16
L97 = malloc 8192 16
L98 = malloc 32 128
free L90
free L79
L99 = malloc 8193 4
free L46
L100 = malloc 31 4
L101 = malloc 8192 128
free L99
L102 = malloc 2049 16
L103 = malloc 4 2
L104 = malloc 32 2
free L101
free L98
L105 = malloc 1 16
free L92
L106 = malloc 2 2
L107 = malloc 31 16
L108 = malloc 2 4
free L94
L109 = malloc 4097 4
L110 = malloc 31 4
free L103
L111 = malloc 31 4
free L111
L112 = malloc 2049 2
L113 = malloc 32 128
free L106
L114 = malloc 8191 2
free L105
free L97
free L109
L115 = malloc 2 16
free L78
free L93
free L114
free L115
free L96
free L85
L116 = malloc 2 16
free L89
free L87
L117 = malloc 33 2
L118 = malloc 1 4
L119 = malloc 4096 128
free L107
L120 = malloc 8192 4
L121 = malloc 1 128
L122 = malloc 3 4
L123 = malloc 2047 4
L124 = malloc 2 2
free L121
free L102
L125 = malloc 2 4
L126 = malloc 1 16
L127 = malloc 2048 2
L128 = malloc 2048 16
L129 = malloc 32 128
free L124
L130 = malloc 2048 16
L131 = malloc 32 16
L132 = malloc 32 128
free L100
free L117
free L126
L133 = malloc 8192 4
L134 = malloc 8192 2
L135 = malloc 2 16
free L119
L136 = malloc 31 16
free L118
free L125
free L132
free L122
free L134
free L136
free L130
free L110
free L123
free L104
free L116
free L133
free L91
free L113
free L128
free L129
free L95
free L131
free L127
free L112
free L108
free L135
free L120

View File

@ -1,10 +1,10 @@
malloc 8 16
malloc 16 8
malloc 64 32
malloc 1 8
malloc 64 32
malloc 64 8
malloc 1024 32
malloc 1 16
malloc 8 16
malloc 1024 16
L1 = malloc 8 16
L2 = malloc 16 8
L3 = malloc 64 32
L4 = malloc 1 8
L5 = malloc 64 32
L6 = malloc 64 8
L7 = malloc 1024 32
L8 = malloc 1 16
L9 = malloc 8 16
L10 = malloc 1024 16

View File

@ -167,13 +167,25 @@ static cl::opt<bool> UseHostOnlyAllocationPolicy(
cl::init(false), cl::sub(IRMemoryMapSubcommand));
using AllocationT = std::pair<addr_t, addr_t>;
bool areAllocationsOverlapping(const AllocationT &L, const AllocationT &R);
using AddrIntervalMap =
IntervalMap<addr_t, unsigned, 8, IntervalMapHalfOpenInfo<addr_t>>;
bool evalMalloc(IRMemoryMap &IRMemMap, StringRef Line,
AddrIntervalMap &AllocatedIntervals);
bool evalFree(IRMemoryMap &IRMemMap, StringRef Line,
AddrIntervalMap &AllocatedIntervals);
struct IRMemoryMapTestState {
TargetSP Target;
IRMemoryMap Map;
AddrIntervalMap::Allocator IntervalMapAllocator;
AddrIntervalMap Allocations;
StringMap<addr_t> Label2AddrMap;
IRMemoryMapTestState(TargetSP Target)
: Target(Target), Map(Target), Allocations(IntervalMapAllocator) {}
};
bool areAllocationsOverlapping(const AllocationT &L, const AllocationT &R);
bool evalMalloc(StringRef Line, IRMemoryMapTestState &State);
bool evalFree(StringRef Line, IRMemoryMapTestState &State);
int evaluateMemoryMapCommands(Debugger &Dbg);
} // namespace irmemorymap
@ -514,17 +526,23 @@ bool opts::irmemorymap::areAllocationsOverlapping(const AllocationT &L,
return R.first < L.second && L.first < R.second;
}
bool opts::irmemorymap::evalMalloc(IRMemoryMap &IRMemMap, StringRef Line,
AddrIntervalMap &AllocatedIntervals) {
// ::= malloc <size> <alignment>
bool opts::irmemorymap::evalMalloc(StringRef Line,
IRMemoryMapTestState &State) {
// ::= <label> = malloc <size> <alignment>
StringRef Label;
std::tie(Label, Line) = Line.split('=');
if (Line.empty())
return false;
Label = Label.trim();
Line = Line.trim();
size_t Size;
uint8_t Alignment;
int Matches = sscanf(Line.data(), "malloc %zu %hhu", &Size, &Alignment);
if (Matches != 2)
return false;
outs() << formatv("Command: malloc(size={0}, alignment={1})\n", Size,
Alignment);
outs() << formatv("Command: {0} = malloc(size={1}, alignment={2})\n", Label,
Size, Alignment);
if (!isPowerOf2_32(Alignment)) {
outs() << "Malloc error: alignment is not a power of 2\n";
exit(1);
@ -539,7 +557,7 @@ bool opts::irmemorymap::evalMalloc(IRMemoryMap &IRMemMap, StringRef Line,
const bool ZeroMemory = false;
Status ST;
addr_t Addr =
IRMemMap.Malloc(Size, Alignment, Permissions, AP, ZeroMemory, ST);
State.Map.Malloc(Size, Alignment, Permissions, AP, ZeroMemory, ST);
if (ST.Fail()) {
outs() << formatv("Malloc error: {0}\n", ST);
return true;
@ -557,10 +575,10 @@ bool opts::irmemorymap::evalMalloc(IRMemoryMap &IRMemMap, StringRef Line,
// Check that the allocation does not overlap another allocation. Do so by
// testing each allocation which may cover the interval [Addr, EndOfRegion).
addr_t EndOfRegion = Addr + Size;
auto Probe = AllocatedIntervals.begin();
auto Probe = State.Allocations.begin();
Probe.advanceTo(Addr); //< First interval s.t stop >= Addr.
AllocationT NewAllocation = {Addr, EndOfRegion};
while (Probe != AllocatedIntervals.end() && Probe.start() < EndOfRegion) {
while (Probe != State.Allocations.end() && Probe.start() < EndOfRegion) {
AllocationT ProbeAllocation = {Probe.start(), Probe.stop()};
if (areAllocationsOverlapping(ProbeAllocation, NewAllocation)) {
outs() << "Malloc error: overlapping allocation detected"
@ -575,41 +593,42 @@ bool opts::irmemorymap::evalMalloc(IRMemoryMap &IRMemMap, StringRef Line,
// to inhibit interval coalescing.
static unsigned AllocationID = 0;
if (Size)
AllocatedIntervals.insert(Addr, EndOfRegion, AllocationID++);
State.Allocations.insert(Addr, EndOfRegion, AllocationID++);
// Store the label -> address mapping.
State.Label2AddrMap[Label] = Addr;
return true;
}
bool opts::irmemorymap::evalFree(IRMemoryMap &IRMemMap, StringRef Line,
AddrIntervalMap &AllocatedIntervals) {
// ::= free <allocation-index>
size_t AllocIndex;
int Matches = sscanf(Line.data(), "free %zu", &AllocIndex);
if (Matches != 1)
bool opts::irmemorymap::evalFree(StringRef Line, IRMemoryMapTestState &State) {
// ::= free <label>
if (!Line.consume_front("free"))
return false;
StringRef Label = Line.trim();
outs() << formatv("Command: free(allocation-index={0})\n", AllocIndex);
// Find and free the AllocIndex-th allocation.
auto Probe = AllocatedIntervals.begin();
for (size_t I = 0; I < AllocIndex && Probe != AllocatedIntervals.end(); ++I)
++Probe;
if (Probe == AllocatedIntervals.end()) {
outs() << "Free error: Invalid allocation index\n";
outs() << formatv("Command: free({0})\n", Label);
auto LabelIt = State.Label2AddrMap.find(Label);
if (LabelIt == State.Label2AddrMap.end()) {
outs() << "Free error: Invalid allocation label\n";
exit(1);
}
Status ST;
IRMemMap.Free(Probe.start(), ST);
addr_t Addr = LabelIt->getValue();
State.Map.Free(Addr, ST);
if (ST.Fail()) {
outs() << formatv("Free error: {0}\n", ST);
exit(1);
}
// Erase the allocation from the live interval map.
outs() << formatv("Free: [{0:x}, {1:x})\n", Probe.start(), Probe.stop());
Probe.erase();
auto Interval = State.Allocations.find(Addr);
if (Interval != State.Allocations.end()) {
outs() << formatv("Free: [{0:x}, {1:x})\n", Interval.start(),
Interval.stop());
Interval.erase();
}
return true;
}
@ -638,9 +657,7 @@ int opts::irmemorymap::evaluateMemoryMapCommands(Debugger &Dbg) {
}
// Set up an IRMemoryMap and associated testing state.
IRMemoryMap IRMemMap(Target);
AddrIntervalMap::Allocator AIMapAllocator;
AddrIntervalMap AllocatedIntervals(AIMapAllocator);
IRMemoryMapTestState State(Target);
// Parse and apply commands from the command file.
std::unique_ptr<MemoryBuffer> MB = opts::openFile(irmemorymap::CommandFile);
@ -653,10 +670,10 @@ int opts::irmemorymap::evaluateMemoryMapCommands(Debugger &Dbg) {
if (Line.empty() || Line[0] == '#')
continue;
if (evalMalloc(IRMemMap, Line, AllocatedIntervals))
if (evalMalloc(Line, State))
continue;
if (evalFree(IRMemMap, Line, AllocatedIntervals))
if (evalFree(Line, State))
continue;
errs() << "Could not parse line: " << Line << "\n";