Skip to content

Commit 10ac48a

Browse files
committed
[scudo] Add __scudo_get_info symbol to export stats to a buffer.
Also make possible to get the fragmentation stats from the primary allocator. Currenty, `__scudo_print_stats` symbol writes the report to the provided printer, which is not convenient for processing the result.
1 parent f7daa9d commit 10ac48a

File tree

8 files changed

+117
-12
lines changed

8 files changed

+117
-12
lines changed

compiler-rt/lib/scudo/standalone/combined.h

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -633,14 +633,8 @@ class Allocator {
633633
// sizing purposes.
634634
uptr getStats(char *Buffer, uptr Size) {
635635
ScopedString Str;
636-
const uptr Length = getStats(&Str) + 1;
637-
if (Length < Size)
638-
Size = Length;
639-
if (Buffer && Size) {
640-
memcpy(Buffer, Str.data(), Size);
641-
Buffer[Size - 1] = '\0';
642-
}
643-
return Length;
636+
getStats(&Str);
637+
return Str.copyToBuffer(Buffer, Size);
644638
}
645639

646640
void printStats() {
@@ -649,6 +643,12 @@ class Allocator {
649643
Str.output();
650644
}
651645

646+
uptr getFragmentationInfo(char *Buffer, uptr Size) {
647+
ScopedString Str;
648+
Primary.getFragmentationInfo(&Str);
649+
return Str.copyToBuffer(Buffer, Size);
650+
}
651+
652652
void printFragmentationInfo() {
653653
ScopedString Str;
654654
Primary.getFragmentationInfo(&Str);
@@ -1632,12 +1632,11 @@ class Allocator {
16321632
}
16331633
}
16341634

1635-
uptr getStats(ScopedString *Str) {
1635+
void getStats(ScopedString *Str) {
16361636
Primary.getStats(Str);
16371637
Secondary.getStats(Str);
16381638
Quarantine.getStats(Str);
16391639
TSDRegistry.getStats(Str);
1640-
return Str->length();
16411640
}
16421641

16431642
static typename AllocationRingBuffer::Entry *

compiler-rt/lib/scudo/standalone/include/scudo/interface.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,23 @@ __attribute__((weak)) void __scudo_realloc_deallocate_hook(void *old_ptr);
3535

3636
void __scudo_print_stats(void);
3737

38+
// Reports all allocators configuration and general statistics as a null
39+
// terminated text string.
40+
#ifndef M_INFO_TOPIC_STATS
41+
#define M_INFO_TOPIC_STATS 1
42+
#endif
43+
44+
// Reports fragmentation statistics of the primary allocation as a null
45+
// terminated text string.
46+
#ifndef M_INFO_TOPIC_FRAGMENTATION
47+
#define M_INFO_TOPIC_FRAGMENTATION 2
48+
#endif
49+
50+
// Writes allocator statistics to the buffer, truncating to the specified size
51+
// if necessary. Returns the full report size (before truncation) for buffer
52+
// sizing purpose, or zero if the topic is not supported.
53+
size_t __scudo_get_info(uint32_t topic, void *buffer, size_t size);
54+
3855
typedef void (*iterate_callback)(uintptr_t base, size_t size, void *arg);
3956

4057
// Determine the likely cause of a tag check fault or other memory protection

compiler-rt/lib/scudo/standalone/string_utils.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,15 @@ void ScopedString::append(const char *Format, ...) {
229229
va_end(Args);
230230
}
231231

232+
size_t ScopedString::copyToBuffer(char *OutputBase, size_t OutputLength) {
233+
if (OutputBase && OutputLength) {
234+
const size_t Written = Min(length(), OutputLength - 1);
235+
memcpy(OutputBase, data(), Written);
236+
OutputBase[Written] = '\0';
237+
}
238+
return length() + 1;
239+
}
240+
232241
void Printf(const char *Format, ...) {
233242
va_list Args;
234243
va_start(Args, Format);

compiler-rt/lib/scudo/standalone/string_utils.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ namespace scudo {
1919
class ScopedString {
2020
public:
2121
explicit ScopedString() { String.push_back('\0'); }
22-
uptr length() { return String.size() - 1; }
23-
const char *data() { return String.data(); }
22+
uptr length() const { return String.size() - 1; }
23+
const char *data() const { return String.data(); }
2424
void clear() {
2525
String.clear();
2626
String.push_back('\0');
@@ -31,6 +31,11 @@ class ScopedString {
3131
void reserve(size_t Size) { String.reserve(Size + 1); }
3232
uptr capacity() { return String.capacity() - 1; }
3333

34+
// Copies the string to the buffer, truncating if necessary.
35+
// Null-terminates the output if output_length is greater than zero.
36+
// Returns the original string's size (including null).
37+
size_t copyToBuffer(char *OutputBase, size_t OutputLength);
38+
3439
private:
3540
void appendNumber(u64 AbsoluteValue, u8 Base, u8 MinNumberLength,
3641
bool PadWithZero, bool Negative, bool Upper);

compiler-rt/lib/scudo/standalone/tests/combined_test.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,12 @@ void ScudoCombinedTest<Config>::BasicTest(scudo::uptr SizeLog) {
291291

292292
Allocator->printStats();
293293
Allocator->printFragmentationInfo();
294+
295+
{
296+
char buffer[256] = {0};
297+
EXPECT_LE(0, Allocator->getStats(buffer, sizeof(buffer)));
298+
EXPECT_LE(0, Allocator->getFragmentationInfo(buffer, sizeof(buffer)));
299+
}
294300
}
295301

296302
#define SCUDO_MAKE_BASIC_TEST(SizeLog) \

compiler-rt/lib/scudo/standalone/tests/strings_test.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,52 @@ TEST(ScudoStringsTest, Padding) {
128128
testAgainstLibc<int>("%03d - %03d", -12, -1234);
129129
}
130130

131+
TEST(ScudoStringsTest, CopyIntoNullBuffer) {
132+
scudo::ScopedString Str;
133+
Str.append("abc");
134+
EXPECT_EQ(4U, Str.copyToBuffer(nullptr, 0));
135+
}
136+
137+
TEST(ScudoStringsTest, CopyFromAnEmptyString) {
138+
scudo::ScopedString Str;
139+
char buf[256] = {'0', '1'};
140+
EXPECT_EQ(1U, Str.copyToBuffer(buf, sizeof(buf)));
141+
EXPECT_STREQ("", buf);
142+
EXPECT_EQ(0, buf[0]); // Rest of the buffer remains unchanged.
143+
}
144+
145+
TEST(ScudoStringsTest, CopyFromAnEmptyStringIntoZeroSizeBuffer) {
146+
scudo::ScopedString Str;
147+
char buf[256] = {'0', '1'};
148+
EXPECT_EQ(1U, Str.copyToBuffer(buf, 0));
149+
EXPECT_EQ('0', buf[0]); // Nothing changed because provided size is 0.
150+
}
151+
152+
TEST(ScudoStringsTest, CopyIntoLargeEnoughBuffer) {
153+
scudo::ScopedString Str;
154+
Str.append("abc");
155+
char buf[256] = {'0', '1', '2', '3', '4', '5'};
156+
// Size includes terminal null.
157+
EXPECT_EQ(4U, Str.copyToBuffer(buf, sizeof(buf)));
158+
EXPECT_STREQ("abc", buf);
159+
}
160+
161+
TEST(ScudoStringsTest, CopyWithTextOverflow) {
162+
scudo::ScopedString Str;
163+
Str.append("abc");
164+
char buf[256] = {'0', '1', '2', '3', '4', '5'};
165+
EXPECT_EQ(4U, Str.copyToBuffer(buf, 3));
166+
EXPECT_STREQ("ab", buf);
167+
}
168+
169+
TEST(ScudoStringsTest, CopyIntoExactFit) {
170+
scudo::ScopedString Str;
171+
Str.append("abc");
172+
char buf[256] = {'0', '1', '2', '3', '4', '5'};
173+
EXPECT_EQ(4U, Str.copyToBuffer(buf, 4));
174+
EXPECT_STREQ("abc", buf);
175+
}
176+
131177
#if defined(__linux__)
132178

133179
#include <sys/resource.h>

compiler-rt/lib/scudo/standalone/wrappers_c.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,16 @@ scudo::Allocator<scudo::Config, SCUDO_PREFIX(malloc_postinit)> SCUDO_ALLOCATOR;
3737

3838
extern "C" INTERFACE void __scudo_print_stats(void) { Allocator.printStats(); }
3939

40+
extern "C" INTERFACE size_t __scudo_get_info(uint32_t topic, void *buffer,
41+
size_t size) {
42+
switch (topic) {
43+
case M_INFO_TOPIC_STATS:
44+
return Allocator.getStats(reinterpret_cast<char *>(buffer), size);
45+
case M_INFO_TOPIC_FRAGMENTATION:
46+
return Allocator.getFragmentationInfo(reinterpret_cast<char *>(buffer),
47+
size);
48+
}
49+
return 0;
50+
}
51+
4052
#endif // !SCUDO_ANDROID || !_BIONIC

compiler-rt/lib/scudo/standalone/wrappers_c_bionic.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,17 @@ static scudo::Allocator<scudo::Config, SCUDO_PREFIX(malloc_postinit)>
3838
// TODO(kostyak): support both allocators.
3939
INTERFACE void __scudo_print_stats(void) { Allocator.printStats(); }
4040

41+
INTERFACE size_t __scudo_get_info(uint32_t topic, void *buffer, size_t size) {
42+
switch (topic) {
43+
case M_INFO_TOPIC_STATS:
44+
return Allocator.getStats(reinterpret_cast<char *>(buffer), size);
45+
case M_INFO_TOPIC_FRAGMENTATION:
46+
return Allocator.getFragmentationInfo(reinterpret_cast<char *>(buffer),
47+
size);
48+
}
49+
return 0;
50+
}
51+
4152
INTERFACE void __scudo_get_error_info(
4253
struct scudo_error_info *error_info, uintptr_t fault_addr,
4354
const char *stack_depot, size_t stack_depot_size, const char *region_info,

0 commit comments

Comments
 (0)