Skip to content

Commit 7ef143f

Browse files
Tom CherryGerrit Code Review
Tom Cherry
authored and
Gerrit Code Review
committed
Merge "liblog: support extended logger_entry headers"
2 parents 83c7ad2 + d3ecc66 commit 7ef143f

File tree

5 files changed

+79
-17
lines changed

5 files changed

+79
-17
lines changed

liblog/include/log/log_read.h

-4
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,6 @@ extern "C" {
4848
* access to raw information, or parsing is an issue.
4949
*/
5050

51-
#pragma clang diagnostic push
52-
#pragma clang diagnostic ignored "-Wzero-length-array"
5351
struct logger_entry {
5452
uint16_t len; /* length of the payload */
5553
uint16_t hdr_size; /* sizeof(struct logger_entry) */
@@ -59,9 +57,7 @@ struct logger_entry {
5957
uint32_t nsec; /* nanoseconds */
6058
uint32_t lid; /* log id of the payload, bottom 4 bits currently */
6159
uint32_t uid; /* generating process's uid */
62-
char msg[0]; /* the entry's payload */
6360
};
64-
#pragma clang diagnostic pop
6561

6662
/*
6763
* The maximum size of the log entry payload that can be

liblog/logprint.cpp

+11-11
Original file line numberDiff line numberDiff line change
@@ -509,12 +509,12 @@ int android_log_processLogBuffer(struct logger_entry* buf, AndroidLogEntry* entr
509509
* format: <priority:1><tag:N>\0<message:N>\0
510510
*
511511
* tag str
512-
* starts at buf->msg+1
512+
* starts at buf + buf->hdr_size + 1
513513
* msg
514-
* starts at buf->msg+1+len(tag)+1
514+
* starts at buf + buf->hdr_size + 1 + len(tag) + 1
515515
*
516-
* The message may have been truncated by the kernel log driver.
517-
* When that happens, we must null-terminate the message ourselves.
516+
* The message may have been truncated. When that happens, we must null-terminate the message
517+
* ourselves.
518518
*/
519519
if (buf->len < 3) {
520520
/*
@@ -529,11 +529,11 @@ int android_log_processLogBuffer(struct logger_entry* buf, AndroidLogEntry* entr
529529
int msgEnd = -1;
530530

531531
int i;
532-
char* msg = buf->msg;
533-
if (buf->hdr_size != sizeof(struct logger_entry)) {
534-
fprintf(stderr, "+++ LOG: entry illegal hdr_size\n");
532+
if (buf->hdr_size < sizeof(logger_entry)) {
533+
fprintf(stderr, "+++ LOG: hdr_size must be at least as big as struct logger_entry\n");
535534
return -1;
536535
}
536+
char* msg = reinterpret_cast<char*>(buf) + buf->hdr_size;
537537
entry->uid = buf->uid;
538538

539539
for (i = 1; i < buf->len; i++) {
@@ -985,11 +985,11 @@ int android_log_processBinaryLogBuffer(
985985
entry->pid = buf->pid;
986986
entry->tid = buf->tid;
987987

988-
eventData = (const unsigned char*)buf->msg;
989-
if (buf->hdr_size != sizeof(struct logger_entry)) {
990-
fprintf(stderr, "+++ LOG: entry illegal hdr_size\n");
988+
if (buf->hdr_size < sizeof(logger_entry)) {
989+
fprintf(stderr, "+++ LOG: hdr_size must be at least as big as struct logger_entry\n");
991990
return -1;
992991
}
992+
eventData = reinterpret_cast<unsigned char*>(buf) + buf->hdr_size;
993993
if (buf->lid == LOG_ID_SECURITY) {
994994
entry->priority = ANDROID_LOG_WARN;
995995
}
@@ -1048,7 +1048,7 @@ int android_log_processBinaryLogBuffer(
10481048
}
10491049
if ((result == 1) && fmtStr) {
10501050
/* We overflowed :-(, let's repaint the line w/o format dressings */
1051-
eventData = (const unsigned char*)buf->msg;
1051+
eventData = reinterpret_cast<unsigned char*>(buf) + buf->hdr_size;
10521052
eventData += 4;
10531053
outBuf = messageBuf;
10541054
outRemaining = messageBufLen - 1;

liblog/pmsg_reader.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ int PmsgRead(struct logger_list* logger_list, struct log_msg* log_msg) {
9696
((logger_list->start.tv_sec != buf.l.realtime.tv_sec) ||
9797
(logger_list->start.tv_nsec <= buf.l.realtime.tv_nsec)))) &&
9898
(!logger_list->pid || (logger_list->pid == buf.p.pid))) {
99-
char* msg = log_msg->entry.msg;
99+
char* msg = reinterpret_cast<char*>(&log_msg->entry) + log_msg->entry.hdr_size;
100100
*msg = buf.prio;
101101
fd = atomic_load(&logger_list->fd);
102102
if (fd <= 0) {

liblog/tests/logprint_test.cpp

+66
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,14 @@
1414
* limitations under the License.
1515
*/
1616

17+
#include <log/logprint.h>
18+
19+
#include <string>
20+
1721
#include <gtest/gtest.h>
1822

23+
#include <log/log_read.h>
24+
1925
size_t convertPrintable(char* p, const char* message, size_t messageLen);
2026

2127
TEST(liblog, convertPrintable_ascii) {
@@ -85,3 +91,63 @@ TEST(liblog, convertPrintable_mixed) {
8591
EXPECT_EQ(output_size, strlen(expected_output));
8692
EXPECT_STREQ(expected_output, output);
8793
}
94+
95+
TEST(liblog, log_print_different_header_size) {
96+
constexpr int32_t kPid = 123;
97+
constexpr uint32_t kTid = 456;
98+
constexpr uint32_t kSec = 1000;
99+
constexpr uint32_t kNsec = 999;
100+
constexpr uint32_t kLid = LOG_ID_MAIN;
101+
constexpr uint32_t kUid = 987;
102+
constexpr char kPriority = ANDROID_LOG_ERROR;
103+
104+
auto create_buf = [](char* buf, size_t len, uint16_t hdr_size) {
105+
memset(buf, 0, len);
106+
logger_entry* header = reinterpret_cast<logger_entry*>(buf);
107+
header->hdr_size = hdr_size;
108+
header->pid = kPid;
109+
header->tid = kTid;
110+
header->sec = kSec;
111+
header->nsec = kNsec;
112+
header->lid = kLid;
113+
header->uid = kUid;
114+
char* message = buf + header->hdr_size;
115+
uint16_t message_len = 0;
116+
message[message_len++] = kPriority;
117+
message[message_len++] = 'T';
118+
message[message_len++] = 'a';
119+
message[message_len++] = 'g';
120+
message[message_len++] = '\0';
121+
message[message_len++] = 'm';
122+
message[message_len++] = 's';
123+
message[message_len++] = 'g';
124+
message[message_len++] = '!';
125+
message[message_len++] = '\0';
126+
header->len = message_len;
127+
};
128+
129+
auto check_entry = [&](const AndroidLogEntry& entry) {
130+
EXPECT_EQ(kSec, static_cast<uint32_t>(entry.tv_sec));
131+
EXPECT_EQ(kNsec, static_cast<uint32_t>(entry.tv_nsec));
132+
EXPECT_EQ(kPriority, entry.priority);
133+
EXPECT_EQ(kUid, static_cast<uint32_t>(entry.uid));
134+
EXPECT_EQ(kPid, entry.pid);
135+
EXPECT_EQ(kTid, static_cast<uint32_t>(entry.tid));
136+
EXPECT_STREQ("Tag", entry.tag);
137+
EXPECT_EQ(4U, entry.tagLen); // Apparently taglen includes the nullptr?
138+
EXPECT_EQ(4U, entry.messageLen);
139+
EXPECT_STREQ("msg!", entry.message);
140+
};
141+
alignas(logger_entry) char buf[LOGGER_ENTRY_MAX_LEN];
142+
create_buf(buf, sizeof(buf), sizeof(logger_entry));
143+
144+
AndroidLogEntry entry_normal_size;
145+
ASSERT_EQ(0,
146+
android_log_processLogBuffer(reinterpret_cast<logger_entry*>(buf), &entry_normal_size));
147+
check_entry(entry_normal_size);
148+
149+
create_buf(buf, sizeof(buf), sizeof(logger_entry) + 3);
150+
AndroidLogEntry entry_odd_size;
151+
ASSERT_EQ(0, android_log_processLogBuffer(reinterpret_cast<logger_entry*>(buf), &entry_odd_size));
152+
check_entry(entry_odd_size);
153+
}

logd/tests/logd_test.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ static void dump_log_msg(const char* prefix, log_msg* msg, int lid) {
246246
std::cerr << std::flush;
247247
fflush(stdout);
248248
fflush(stderr);
249-
EXPECT_EQ(sizeof(logger_entry), msg->entry.hdr_size);
249+
EXPECT_GE(msg->entry.hdr_size, sizeof(logger_entry));
250250

251251
fprintf(stderr, "%s: [%u] ", prefix, msg->len());
252252
fprintf(stderr, "hdr_size=%u ", msg->entry.hdr_size);

0 commit comments

Comments
 (0)