Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

src: use struct as arguments to node::Assert #25869

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 6 additions & 12 deletions src/node_errors.cc
Original file line number Diff line number Diff line change
Expand Up @@ -166,23 +166,17 @@ void AppendExceptionLine(Environment* env,
ABORT_NO_BACKTRACE();
}

[[noreturn]] void Assert(const char* const (*args)[4]) {
auto filename = (*args)[0];
auto linenum = (*args)[1];
auto message = (*args)[2];
auto function = (*args)[3];

[[noreturn]] void Assert(const AssertionInfo& info) {
char name[1024];
GetHumanReadableProcessName(&name);

fprintf(stderr,
"%s: %s:%s:%s%s Assertion `%s' failed.\n",
"%s: %s:%s%s Assertion `%s' failed.\n",
name,
filename,
linenum,
function,
*function ? ":" : "",
message);
info.file_line,
info.function,
*info.function ? ":" : "",
info.message);
fflush(stderr);

Abort();
Expand Down
20 changes: 14 additions & 6 deletions src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,15 @@ extern bool v8_initialized;
// whether V8 is initialized.
void LowMemoryNotification();

// The slightly odd function signature for Assert() is to ease
// instruction cache pressure in calls from CHECK.
// The reason that Assert() takes a struct argument instead of individual
// const char*s is to ease instruction cache pressure in calls from CHECK.
struct AssertionInfo {
const char* file_line; // filename:line
const char* message;
const char* function;
};
[[noreturn]] void Assert(const AssertionInfo& info);
[[noreturn]] void Abort();
[[noreturn]] void Assert(const char* const (*args)[4]);
void DumpBacktrace(FILE* fp);

#define DISALLOW_COPY_AND_ASSIGN(TypeName) \
Expand Down Expand Up @@ -120,9 +125,12 @@ void DumpBacktrace(FILE* fp);
#define CHECK(expr) \
do { \
Copy link
Contributor

@refack refack Feb 1, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you're here, would you consider de-crufting this? i.e.:

    if (!(expr)) {                                                             \
      node::Assert({ __FILE__, __LINE__, #expr, PRETTY_FUNCTION_NAME });       \
    }                                                                          \

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@refack At least locally (gcc-7 on Ubuntu 18.04), that would undo the purpose of this (what the comment is referring to): It would put the object construction in-line into the code instead of ending up as a simple pointer load from the read-only data section.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So how about:

    if (!(expr)) {                                                             \
      static constexpr args{ __FILE__, __LINE__, #expr, PRETTY_FUNCTION_NAME } \
      node::Assert(args);                                                      \
    }

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@refack You are talking about constconstexpr, right? That has the same result for me, and I don’t see it as being clearer about the intention here (having an explicit, hardcoded object in the read-only data section).

I’ll make the comment more explicit about what the idea here is.

if (UNLIKELY(!(expr))) { \
static const char* const args[] = { __FILE__, STRINGIFY(__LINE__), \
#expr, PRETTY_FUNCTION_NAME }; \
node::Assert(&args); \
/* Make sure that this struct does not end up in inline code, but */ \
/* rather in a read-only data section when modifying this code. */ \
static const node::AssertionInfo args = { \
__FILE__ ":" STRINGIFY(__LINE__), #expr, PRETTY_FUNCTION_NAME \
}; \
node::Assert(args); \
} \
} while (0)

Expand Down