Commit 16e464fb authored by Camillo Bruni's avatar Camillo Bruni Committed by V8 LUCI CQ

[d8] Harden Shell::ReadFile

- Always return a MaybeLocal<String>
- Don't crash on long filenames

Bug: chromium:1311923
Change-Id: I96e10337ceb32aeafafe0b73c78651a1ac38fb9f
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3576122Reviewed-by: 's avatarMaya Lekova <mslekova@chromium.org>
Commit-Queue: Camillo Bruni <cbruni@chromium.org>
Cr-Commit-Position: refs/heads/main@{#79859}
parent ec778f9a
...@@ -1022,7 +1022,7 @@ MaybeLocal<Module> Shell::FetchModuleTree(Local<Module> referrer, ...@@ -1022,7 +1022,7 @@ MaybeLocal<Module> Shell::FetchModuleTree(Local<Module> referrer,
ModuleType module_type) { ModuleType module_type) {
DCHECK(IsAbsolutePath(file_name)); DCHECK(IsAbsolutePath(file_name));
Isolate* isolate = context->GetIsolate(); Isolate* isolate = context->GetIsolate();
Local<String> source_text = ReadFile(isolate, file_name.c_str(), false); MaybeLocal<String> source_text = ReadFile(isolate, file_name.c_str(), false);
if (source_text.IsEmpty() && options.fuzzy_module_file_extensions) { if (source_text.IsEmpty() && options.fuzzy_module_file_extensions) {
std::string fallback_file_name = file_name + ".js"; std::string fallback_file_name = file_name + ".js";
source_text = ReadFile(isolate, fallback_file_name.c_str(), false); source_text = ReadFile(isolate, fallback_file_name.c_str(), false);
...@@ -1053,14 +1053,16 @@ MaybeLocal<Module> Shell::FetchModuleTree(Local<Module> referrer, ...@@ -1053,14 +1053,16 @@ MaybeLocal<Module> Shell::FetchModuleTree(Local<Module> referrer,
Local<Module> module; Local<Module> module;
if (module_type == ModuleType::kJavaScript) { if (module_type == ModuleType::kJavaScript) {
ScriptCompiler::Source source(source_text, origin); ScriptCompiler::Source source(source_text.ToLocalChecked(), origin);
if (!CompileString<Module>(isolate, context, source_text, origin) if (!CompileString<Module>(isolate, context, source_text.ToLocalChecked(),
origin)
.ToLocal(&module)) { .ToLocal(&module)) {
return MaybeLocal<Module>(); return MaybeLocal<Module>();
} }
} else if (module_type == ModuleType::kJSON) { } else if (module_type == ModuleType::kJSON) {
Local<Value> parsed_json; Local<Value> parsed_json;
if (!v8::JSON::Parse(context, source_text).ToLocal(&parsed_json)) { if (!v8::JSON::Parse(context, source_text.ToLocalChecked())
.ToLocal(&parsed_json)) {
return MaybeLocal<Module>(); return MaybeLocal<Module>();
} }
...@@ -2302,8 +2304,8 @@ void Shell::ReadFile(const v8::FunctionCallbackInfo<v8::Value>& args) { ...@@ -2302,8 +2304,8 @@ void Shell::ReadFile(const v8::FunctionCallbackInfo<v8::Value>& args) {
return; return;
} }
} }
Local<String> source = ReadFile(args.GetIsolate(), *file_name); Local<String> source;
if (source.IsEmpty()) return; if (!ReadFile(args.GetIsolate(), *file_name).ToLocal(&source)) return;
args.GetReturnValue().Set(source); args.GetReturnValue().Set(source);
} }
...@@ -2356,8 +2358,8 @@ void Shell::ExecuteFile(const v8::FunctionCallbackInfo<v8::Value>& args) { ...@@ -2356,8 +2358,8 @@ void Shell::ExecuteFile(const v8::FunctionCallbackInfo<v8::Value>& args) {
String::NewFromUtf8(isolate, oss.str().c_str()).ToLocalChecked()); String::NewFromUtf8(isolate, oss.str().c_str()).ToLocalChecked());
return; return;
} }
Local<String> source = ReadFile(isolate, *file_name); Local<String> source;
if (source.IsEmpty()) return; if (!ReadFile(isolate, *file_name).ToLocal(&source)) return;
if (!ExecuteString( if (!ExecuteString(
args.GetIsolate(), source, args.GetIsolate(), source,
String::NewFromUtf8(isolate, *file_name).ToLocalChecked(), String::NewFromUtf8(isolate, *file_name).ToLocalChecked(),
...@@ -2497,8 +2499,9 @@ MaybeLocal<String> Shell::ReadSource( ...@@ -2497,8 +2499,9 @@ MaybeLocal<String> Shell::ReadSource(
return MaybeLocal<String>(); return MaybeLocal<String>();
} }
String::Utf8Value filename(isolate, args[index]); String::Utf8Value filename(isolate, args[index]);
source = Shell::ReadFile(isolate, *filename); if (!Shell::ReadFile(isolate, *filename).ToLocal(&source)) {
if (source.IsEmpty()) return MaybeLocal<String>(); return MaybeLocal<String>();
};
break; break;
} }
case CodeType::kString: case CodeType::kString:
...@@ -3270,7 +3273,7 @@ void Shell::Initialize(Isolate* isolate, D8Console* console, ...@@ -3270,7 +3273,7 @@ void Shell::Initialize(Isolate* isolate, D8Console* console,
Local<String> Shell::WasmLoadSourceMapCallback(Isolate* isolate, Local<String> Shell::WasmLoadSourceMapCallback(Isolate* isolate,
const char* path) { const char* path) {
return Shell::ReadFile(isolate, path, false); return Shell::ReadFile(isolate, path, false).ToLocalChecked();
} }
Local<Context> Shell::CreateEvaluationContext(Isolate* isolate) { Local<Context> Shell::CreateEvaluationContext(Isolate* isolate) {
...@@ -3650,33 +3653,31 @@ void Shell::ReadBuffer(const v8::FunctionCallbackInfo<v8::Value>& args) { ...@@ -3650,33 +3653,31 @@ void Shell::ReadBuffer(const v8::FunctionCallbackInfo<v8::Value>& args) {
} }
// Reads a file into a v8 string. // Reads a file into a v8 string.
Local<String> Shell::ReadFile(Isolate* isolate, const char* name, MaybeLocal<String> Shell::ReadFile(Isolate* isolate, const char* name,
bool should_throw) { bool should_throw) {
std::unique_ptr<base::OS::MemoryMappedFile> file( std::unique_ptr<base::OS::MemoryMappedFile> file(
base::OS::MemoryMappedFile::open( base::OS::MemoryMappedFile::open(
name, base::OS::MemoryMappedFile::FileMode::kReadOnly)); name, base::OS::MemoryMappedFile::FileMode::kReadOnly));
if (!file) { if (!file) {
if (should_throw) { if (should_throw) {
std::ostringstream oss; std::ostringstream oss;
oss << "Error loading file: \"" << name << '"'; oss << "Error loading file: " << name;
isolate->ThrowError( isolate->ThrowError(
v8::String::NewFromUtf8(isolate, oss.str().c_str()).ToLocalChecked()); v8::String::NewFromUtf8(
isolate, oss.str().substr(0, String::kMaxLength).c_str())
.ToLocalChecked());
} }
return Local<String>(); return MaybeLocal<String>();
} }
int size = static_cast<int>(file->size()); int size = static_cast<int>(file->size());
char* chars = static_cast<char*>(file->memory()); char* chars = static_cast<char*>(file->memory());
Local<String> result;
if (i::FLAG_use_external_strings && i::String::IsAscii(chars, size)) { if (i::FLAG_use_external_strings && i::String::IsAscii(chars, size)) {
String::ExternalOneByteStringResource* resource = String::ExternalOneByteStringResource* resource =
new ExternalOwningOneByteStringResource(std::move(file)); new ExternalOwningOneByteStringResource(std::move(file));
result = String::NewExternalOneByte(isolate, resource).ToLocalChecked(); return String::NewExternalOneByte(isolate, resource);
} else {
result = String::NewFromUtf8(isolate, chars, NewStringType::kNormal, size)
.ToLocalChecked();
} }
return result; return String::NewFromUtf8(isolate, chars, NewStringType::kNormal, size);
} }
void Shell::WriteChars(const char* name, uint8_t* buffer, size_t buffer_size) { void Shell::WriteChars(const char* name, uint8_t* buffer, size_t buffer_size) {
...@@ -3970,8 +3971,8 @@ bool SourceGroup::Execute(Isolate* isolate) { ...@@ -3970,8 +3971,8 @@ bool SourceGroup::Execute(Isolate* isolate) {
HandleScope handle_scope(isolate); HandleScope handle_scope(isolate);
Local<String> file_name = Local<String> file_name =
String::NewFromUtf8(isolate, arg).ToLocalChecked(); String::NewFromUtf8(isolate, arg).ToLocalChecked();
Local<String> source = Shell::ReadFile(isolate, arg); Local<String> source;
if (source.IsEmpty()) { if (!Shell::ReadFile(isolate, arg).ToLocal(&source)) {
printf("Error reading '%s'\n", arg); printf("Error reading '%s'\n", arg);
base::OS::ExitProcess(1); base::OS::ExitProcess(1);
} }
......
...@@ -500,8 +500,8 @@ class Shell : public i::AllStatic { ...@@ -500,8 +500,8 @@ class Shell : public i::AllStatic {
static void ReportException(Isolate* isolate, Local<Message> message, static void ReportException(Isolate* isolate, Local<Message> message,
Local<Value> exception); Local<Value> exception);
static void ReportException(Isolate* isolate, TryCatch* try_catch); static void ReportException(Isolate* isolate, TryCatch* try_catch);
static Local<String> ReadFile(Isolate* isolate, const char* name, static MaybeLocal<String> ReadFile(Isolate* isolate, const char* name,
bool should_throw = true); bool should_throw = true);
static Local<String> WasmLoadSourceMapCallback(Isolate* isolate, static Local<String> WasmLoadSourceMapCallback(Isolate* isolate,
const char* name); const char* name);
static Local<Context> CreateEvaluationContext(Isolate* isolate); static Local<Context> CreateEvaluationContext(Isolate* isolate);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment