Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fixup! src: use Blob{Des|S}erializer for SEA blobs
  • Loading branch information
joyeecheung committed May 12, 2023
commit 7f904591fb75f5dc0683baab41bd399de08f7241
35 changes: 10 additions & 25 deletions src/blob_serializer_deserializer-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@

#include "debug_utils-inl.h"

// This is related to the blob that is used in snapshots and has nothing to do
// with `node_blob.h`.
// This is related to the blob that is used in snapshots and single executable
// applications and has nothing to do with `node_blob.h`.

namespace node {

Expand Down Expand Up @@ -131,25 +131,17 @@ std::vector<T> BlobDeserializer<Impl>::ReadVector() {
template <typename Impl>
std::string BlobDeserializer<Impl>::ReadString() {
std::string_view view = ReadStringView();
std::string result(view.data(),
view.size()); // This creates a copy of view.data.
return result;
Debug("ReadString(), length=%zu: \"%s\"\n", view.size(), view.data());
return std::string(view);
}

template <typename Impl>
std::string_view BlobDeserializer<Impl>::ReadStringView() {
size_t length = ReadArithmetic<size_t>();
Debug("ReadStringView(), length=%zu: ", length);

if (is_debug) {
Debug("ReadStringView(), length=%d: ", length);
}

CHECK_GT(length, 0); // There should be no empty strings.
std::string_view result(sink.data() + read_total, length);

if (is_debug) {
Debug("\"%s\", read %zu bytes\n", result.data(), result.size());
}
Debug("%p, read %zu bytes\n", result.data(), result.size());

read_total += length;
return result;
Expand Down Expand Up @@ -268,29 +260,22 @@ size_t BlobSerializer<Impl>::WriteVector(const std::vector<T>& data) {
// [ 4/8 bytes ] length
// [ |length| bytes ] contents
template <typename Impl>
size_t BlobSerializer<Impl>::WriteStringView(const std::string_view& data) {
CHECK_GT(data.size(), 0); // No empty strings should be written.
size_t BlobSerializer<Impl>::WriteStringView(std::string_view data) {
size_t written_total = WriteArithmetic<size_t>(data.size());
if (is_debug) {
Debug("WriteStringView(), length=%zu: %p\n", data.size(), data.data());
}
Debug("WriteStringView(), length=%zu: %p\n", data.size(), data.data());

size_t length = data.size();
sink.insert(sink.end(), data.data(), data.data() + length);
written_total += length;

if (is_debug) {
Debug("WriteString() wrote %zu bytes\n", written_total);
}
Debug("WriteStringView() wrote %zu bytes\n", written_total);

return written_total;
}

template <typename Impl>
size_t BlobSerializer<Impl>::WriteString(const std::string& data) {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
size_t BlobSerializer<Impl>::WriteString(const std::string& data) {
size_t BlobSerializer<Impl>::WriteString(const std::string_view data) {

Copy link
Member Author

Choose a reason for hiding this comment

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

This is the method that writes strings (which logs the string directly in debug mode). The one that doesn't is WriteStringView (which obviously doesn't log that data as string).

if (is_debug) {
Debug("WriteString(), length=%zu: \"%s\"\n", data.size(), data.data());
}
Debug("WriteString(), length=%zu: \"%s\"\n", data.size(), data.data());
return WriteStringView(data);
}

Expand Down
6 changes: 3 additions & 3 deletions src/blob_serializer_deserializer.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS

// This is related to the blob that is used in snapshots and has nothing to do
// with `node_blob.h`.
// This is related to the blob that is used in snapshots and single executable
// applications and has nothing to do with `node_blob.h`.

namespace node {

Expand Down Expand Up @@ -99,7 +99,7 @@ class BlobSerializer : public BlobSerializerDeserializer {
// The layout of a written string:
// [ 4/8 bytes ] length
// [ |length| bytes ] contents
size_t WriteStringView(const std::string_view& data);
size_t WriteStringView(std::string_view data);
size_t WriteString(const std::string& data);

// Helper for writing an array of numeric types.
Expand Down
19 changes: 8 additions & 11 deletions src/node_sea.cc
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ SeaFlags operator|=(/* NOLINT (runtime/references) */ SeaFlags& x, SeaFlags y) {

class SeaSerializer : public BlobSerializer<SeaSerializer> {
public:
SeaSerializer() : BlobSerializer<SeaSerializer>(false) {}
SeaSerializer()
: BlobSerializer<SeaSerializer>(
per_process::enabled_debug_list.enabled(DebugCategory::SEA)) {}

template <typename T,
std::enable_if_t<!std::is_same<T, std::string>::value>* = nullptr,
Expand All @@ -75,7 +77,8 @@ size_t SeaSerializer::Write(const SeaResource& sea) {
class SeaDeserializer : public BlobDeserializer<SeaDeserializer> {
public:
explicit SeaDeserializer(std::string_view v)
: BlobDeserializer<SeaDeserializer>(false, v) {}
: BlobDeserializer<SeaDeserializer>(
per_process::enabled_debug_list.enabled(DebugCategory::SEA), v) {}

template <typename T,
std::enable_if_t<!std::is_same<T, std::string>::value>* = nullptr,
Expand All @@ -88,16 +91,11 @@ SeaResource SeaDeserializer::Read() {
uint32_t magic = ReadArithmetic<uint32_t>();
CHECK_EQ(magic, kMagic);
SeaFlags flags(static_cast<SeaFlags>(ReadArithmetic<uint32_t>()));
per_process::Debug(DebugCategory::SEA,
"Read SEA flag %" PRIu32 "\n",
static_cast<uint32_t>(flags));
Debug("Read SEA flag %d", static_cast<uint32_t>(flags));
CHECK_EQ(read_total, SeaResource::kHeaderSize);

std::string_view code = ReadStringView();
per_process::Debug(DebugCategory::SEA,
"Read SEA resource code %p %zu\n",
code.data(),
code.size());
Debug("Read SEA resource code %p %zu\n", code.data(), code.size());
return {flags, code};
}

Expand Down Expand Up @@ -241,8 +239,7 @@ ExitCode GenerateSingleExecutableBlob(const SeaConfig& config) {
return ExitCode::kGenericUserError;
}

SeaResource sea{config.flags,
std::string_view{main_script.data(), main_script.size()}};
SeaResource sea{config.flags, main_script};

SeaSerializer serializer;
serializer.Write(sea);
Expand Down
43 changes: 43 additions & 0 deletions test/sequential/test-single-executable-application-empty.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
'use strict';

require('../common');

const {
injectAndCodeSign,
skipIfSingleExecutableIsNotSupported,
} = require('../common/sea');

skipIfSingleExecutableIsNotSupported();

// This tests the creation of a single executable application.

const tmpdir = require('../common/tmpdir');
const { copyFileSync, writeFileSync, existsSync } = require('fs');
const { execFileSync } = require('child_process');
const { join } = require('path');
const assert = require('assert');

const configFile = join(tmpdir.path, 'sea-config.json');
const seaPrepBlob = join(tmpdir.path, 'sea-prep.blob');
const outputFile = join(tmpdir.path, process.platform === 'win32' ? 'sea.exe' : 'sea');

tmpdir.refresh();

writeFileSync(join(tmpdir.path, 'empty.js'), '', 'utf-8');
writeFileSync(configFile, `
{
"main": "empty.js",
"output": "sea-prep.blob"
}
`);

execFileSync(process.execPath, ['--experimental-sea-config', 'sea-config.json'], {
cwd: tmpdir.path
});

assert(existsSync(seaPrepBlob));

copyFileSync(process.execPath, outputFile);
injectAndCodeSign(outputFile, seaPrepBlob);

execFileSync(outputFile);