Skip to content

Commit

Permalink
sea: use a uint32_t bit field instead of bool disable_experimental_se…
Browse files Browse the repository at this point in the history
…a_warning

Refs: nodejs#47588 (comment)
Signed-off-by: Darshan Sen <raisinten@gmail.com>
  • Loading branch information
RaisinTen committed Apr 25, 2023
1 parent 9fc4544 commit d597344
Showing 1 changed file with 20 additions and 17 deletions.
37 changes: 20 additions & 17 deletions src/node_sea.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,11 @@ const uint32_t kMagic = 0x143da20;

struct SeaResource {
std::string_view code;
bool disable_experimental_sea_warning;
// Layout of `bit_field`:
// * The LSB is set only if the user wants to disable the experimental SEA
// warning.
// * The other bits are unused.
uint32_t bit_field;
};

SeaResource FindSingleExecutableResource() {
Expand All @@ -61,13 +65,12 @@ SeaResource FindSingleExecutableResource() {
#endif
uint32_t first_word = reinterpret_cast<const uint32_t*>(code)[0];
CHECK_EQ(first_word, kMagic);
bool disable_experimental_sea_warning =
reinterpret_cast<const bool*>(code + sizeof(first_word))[0];
uint32_t bit_field =
reinterpret_cast<const uint32_t*>(code + sizeof(first_word))[0];
// TODO(joyeecheung): do more checks here e.g. matching the versions.
return {
{code + sizeof(first_word) + sizeof(disable_experimental_sea_warning),
size - sizeof(first_word) - sizeof(disable_experimental_sea_warning)},
disable_experimental_sea_warning};
return {{code + sizeof(first_word) + sizeof(bit_field),
size - sizeof(first_word) - sizeof(bit_field)},
bit_field};
}();
return sea_resource;
}
Expand All @@ -89,7 +92,9 @@ void IsSingleExecutable(const FunctionCallbackInfo<Value>& args) {

void IsExperimentalSeaWarningDisabled(const FunctionCallbackInfo<Value>& args) {
SeaResource sea_resource = FindSingleExecutableResource();
args.GetReturnValue().Set(sea_resource.disable_experimental_sea_warning);
// The LSB of `bit_field` is set only if the user wants to disable the
// experimental SEA warning.
args.GetReturnValue().Set(static_cast<bool>(sea_resource.bit_field & 1));
}

std::tuple<int, char**> FixupArgsForSEA(int argc, char** argv) {
Expand Down Expand Up @@ -180,17 +185,15 @@ bool GenerateSingleExecutableBlob(const SeaConfig& config) {

std::vector<char> sink;
// TODO(joyeecheung): reuse the SnapshotSerializerDeserializer for this.
sink.reserve(sizeof(kMagic) +
sizeof(config.disable_experimental_sea_warning) +
main_script.size());
sink.reserve(sizeof(kMagic) + sizeof(uint32_t) + main_script.size());
const char* pos = reinterpret_cast<const char*>(&kMagic);
sink.insert(sink.end(), pos, pos + sizeof(kMagic));
const char* disable_experimental_sea_warning =
reinterpret_cast<const char*>(&config.disable_experimental_sea_warning);
sink.insert(sink.end(),
disable_experimental_sea_warning,
disable_experimental_sea_warning +
sizeof(config.disable_experimental_sea_warning));
uint32_t bit_field = 0;
// The LSB of `bit_field` is set only if the user wants to disable the
// experimental SEA warning.
bit_field |= config.disable_experimental_sea_warning;
const char* bit_field_str = reinterpret_cast<const char*>(&bit_field);
sink.insert(sink.end(), bit_field_str, bit_field_str + sizeof(bit_field));
sink.insert(
sink.end(), main_script.data(), main_script.data() + main_script.size());

Expand Down

0 comments on commit d597344

Please sign in to comment.