diff --git a/dom/indexedDB/ActorsChild.cpp b/dom/indexedDB/ActorsChild.cpp index 417a31d3663a..c913e111c1e9 100644 --- a/dom/indexedDB/ActorsChild.cpp +++ b/dom/indexedDB/ActorsChild.cpp @@ -3017,6 +3017,39 @@ void BackgroundRequestChild::PreprocessHelper::RunOnOwningThread() { } } +class MemUnmap { + uint32_t mSize = 0; + + public: + MemUnmap() = default; + explicit MemUnmap(uint32_t aSize) : mSize(aSize) {} + + void operator()(uint8_t* aP) { + MOZ_ASSERT(mSize); + PR_MemUnmap(aP, mSize); + } +}; + +using UniqueMapping = UniquePtr; + +static UniqueMapping MapFile(PRFileDesc* aFile, PRFileInfo* aInfo) { + if (PR_GetOpenFileInfo(aFile, aInfo) != PR_SUCCESS) { + return nullptr; + } + + PRFileMap* map = PR_CreateFileMap(aFile, aInfo->size, PR_PROT_READONLY); + if (!map) { + return nullptr; + } + + // PRFileMap objects do not need to be kept alive after the memory has been + // mapped, so unconditionally close the PRFileMap, regardless of whether + // PR_MemMap succeeds. + uint8_t* memory = (uint8_t*)PR_MemMap(map, 0, aInfo->size); + PR_CloseFileMap(map); + return UniqueMapping(memory, MemUnmap(aInfo->size)); +} + void BackgroundRequestChild::PreprocessHelper::ProcessCurrentStream() { MOZ_ASSERT(!IsOnOwningThread()); MOZ_ASSERT(!mStreams.IsEmpty()); @@ -3038,8 +3071,16 @@ void BackgroundRequestChild::PreprocessHelper::ProcessCurrentStream() { MOZ_ASSERT(mCurrentBytecodeFileDesc); + PRFileInfo bytecodeInfo; + UniqueMapping bytecodeMapping = + MapFile(mCurrentBytecodeFileDesc, &bytecodeInfo); + if (NS_WARN_IF(!bytecodeMapping)) { + ContinueWithStatus(NS_ERROR_FAILURE); + return; + } + RefPtr module = - JS::DeserializeWasmModule(mCurrentBytecodeFileDesc, nullptr, 0); + JS::DeserializeWasmModule(bytecodeMapping.get(), bytecodeInfo.size); if (NS_WARN_IF(!module)) { ContinueWithStatus(NS_ERROR_FAILURE); return; diff --git a/js/src/jsapi.cpp b/js/src/jsapi.cpp index f0e56e1e4dce..8b681e08e75a 100644 --- a/js/src/jsapi.cpp +++ b/js/src/jsapi.cpp @@ -5819,8 +5819,8 @@ JS_PUBLIC_API RefPtr JS::GetWasmModule(HandleObject obj) { } JS_PUBLIC_API RefPtr JS::DeserializeWasmModule( - PRFileDesc* bytecode, UniqueChars filename, unsigned line) { - return wasm::DeserializeModule(bytecode, std::move(filename), line); + const uint8_t* bytecode, size_t bytecodeLength) { + return wasm::DeserializeModule(bytecode, bytecodeLength); } JS_PUBLIC_API void JS::SetProcessLargeAllocationFailureCallback( diff --git a/js/src/jsapi.h b/js/src/jsapi.h index 53f1665d859f..45d2d43a1f06 100644 --- a/js/src/jsapi.h +++ b/js/src/jsapi.h @@ -3029,7 +3029,7 @@ extern JS_PUBLIC_API RefPtr GetWasmModule(HandleObject obj); */ extern JS_PUBLIC_API RefPtr DeserializeWasmModule( - PRFileDesc* bytecode, JS::UniqueChars filename, unsigned line); + const uint8_t* bytecode, size_t bytecodeLength); /** * If a large allocation fails when calling pod_{calloc,realloc}CanGC, the JS diff --git a/js/src/vm/PosixNSPR.cpp b/js/src/vm/PosixNSPR.cpp index a70b4ab7981d..7c68bb7c5536 100644 --- a/js/src/vm/PosixNSPR.cpp +++ b/js/src/vm/PosixNSPR.cpp @@ -14,29 +14,4 @@ # include # include -int32_t PR_FileDesc2NativeHandle(PRFileDesc* fd) { - MOZ_CRASH("PR_FileDesc2NativeHandle"); -} - -PRStatus PR_GetOpenFileInfo(PRFileDesc* fd, PRFileInfo* info) { - MOZ_CRASH("PR_GetOpenFileInfo"); -} - -int32_t PR_Seek(PRFileDesc* fd, int32_t offset, PRSeekWhence whence) { - MOZ_CRASH("PR_Seek"); -} - -PRFileMap* PR_CreateFileMap(PRFileDesc* fd, int64_t size, - PRFileMapProtect prot) { - MOZ_CRASH("PR_CreateFileMap"); -} - -void* PR_MemMap(PRFileMap* fmap, int64_t offset, uint32_t len) { - MOZ_CRASH("PR_MemMap"); -} - -PRStatus PR_MemUnmap(void* addr, uint32_t len) { MOZ_CRASH("PR_MemUnmap"); } - -PRStatus PR_CloseFileMap(PRFileMap* fmap) { MOZ_CRASH("PR_CloseFileMap"); } - #endif /* JS_POSIX_NSPR */ diff --git a/js/src/vm/PosixNSPR.h b/js/src/vm/PosixNSPR.h index ef36f62351ba..a75437faa216 100644 --- a/js/src/vm/PosixNSPR.h +++ b/js/src/vm/PosixNSPR.h @@ -11,42 +11,6 @@ # include "jspubtd.h" -int32_t PR_FileDesc2NativeHandle(PRFileDesc* fd); - -enum PRFileType { PR_FILE_FILE = 1, PR_FILE_DIRECTORY = 2, PR_FILE_OTHER = 3 }; - -struct PRFileInfo { - PRFileType type; - int32_t size; - int64_t creationTime; - int64_t modifyTime; -}; - -typedef enum { PR_FAILURE = -1, PR_SUCCESS = 0 } PRStatus; - -PRStatus PR_GetOpenFileInfo(PRFileDesc* fd, PRFileInfo* info); - -enum PRSeekWhence { PR_SEEK_SET = 0, PR_SEEK_CUR = 1, PR_SEEK_END = 2 }; - -int32_t PR_Seek(PRFileDesc* fd, int32_t offset, PRSeekWhence whence); - -enum PRFileMapProtect { - PR_PROT_READONLY, - PR_PROT_READWRITE, - PR_PROT_WRITECOPY -}; - -struct PRFileMap; - -PRFileMap* PR_CreateFileMap(PRFileDesc* fd, int64_t size, - PRFileMapProtect prot); - -void* PR_MemMap(PRFileMap* fmap, int64_t offset, uint32_t len); - -PRStatus PR_MemUnmap(void* addr, uint32_t len); - -PRStatus PR_CloseFileMap(PRFileMap* fmap); - #endif /* JS_POSIX_NSPR */ #endif /* vm_PosixNSPR_h */ diff --git a/js/src/wasm/WasmModule.cpp b/js/src/wasm/WasmModule.cpp index 7d312675dd43..354c480fe913 100644 --- a/js/src/wasm/WasmModule.cpp +++ b/js/src/wasm/WasmModule.cpp @@ -357,39 +357,8 @@ bool wasm::GetOptimizedEncodingBuildId(JS::BuildIdCharVector* buildId) { return true; } -struct MemUnmap { - uint32_t size; - MemUnmap() : size(0) {} - explicit MemUnmap(uint32_t size) : size(size) {} - void operator()(uint8_t* p) { - MOZ_ASSERT(size); - PR_MemUnmap(p, size); - } -}; - -typedef UniquePtr UniqueMapping; - -static UniqueMapping MapFile(PRFileDesc* file, PRFileInfo* info) { - if (PR_GetOpenFileInfo(file, info) != PR_SUCCESS) { - return nullptr; - } - - PRFileMap* map = PR_CreateFileMap(file, info->size, PR_PROT_READONLY); - if (!map) { - return nullptr; - } - - // PRFileMap objects do not need to be kept alive after the memory has been - // mapped, so unconditionally close the PRFileMap, regardless of whether - // PR_MemMap succeeds. - uint8_t* memory = (uint8_t*)PR_MemMap(map, 0, info->size); - PR_CloseFileMap(map); - return UniqueMapping(memory, MemUnmap(info->size)); -} - -RefPtr wasm::DeserializeModule(PRFileDesc* bytecodeFile, - UniqueChars filename, - unsigned line) { +RefPtr wasm::DeserializeModule(const uint8_t* bytecode, + size_t bytecodeLength) { // We have to compile new code here so if we're fundamentally unable to // compile, we have to fail. If you change this code, update the // MutableCompileArgs setting below. @@ -397,23 +366,17 @@ RefPtr wasm::DeserializeModule(PRFileDesc* bytecodeFile, return nullptr; } - PRFileInfo bytecodeInfo; - UniqueMapping bytecodeMapping = MapFile(bytecodeFile, &bytecodeInfo); - if (!bytecodeMapping) { + MutableBytes bytecodeCopy = js_new(); + if (!bytecodeCopy || + !bytecodeCopy->bytes.initLengthUninitialized(bytecodeLength)) { return nullptr; } - MutableBytes bytecode = js_new(); - if (!bytecode || - !bytecode->bytes.initLengthUninitialized(bytecodeInfo.size)) { - return nullptr; - } - - memcpy(bytecode->bytes.begin(), bytecodeMapping.get(), bytecodeInfo.size); + memcpy(bytecodeCopy->bytes.begin(), bytecode, bytecodeLength); ScriptedCaller scriptedCaller; - scriptedCaller.filename = std::move(filename); - scriptedCaller.line = line; + scriptedCaller.filename = nullptr; + scriptedCaller.line = 0; MutableCompileArgs args = js_new(std::move(scriptedCaller)); if (!args) { @@ -438,7 +401,7 @@ RefPtr wasm::DeserializeModule(PRFileDesc* bytecodeFile, UniqueChars error; UniqueCharsVector warnings; - SharedModule module = CompileBuffer(*args, *bytecode, &error, &warnings); + SharedModule module = CompileBuffer(*args, *bytecodeCopy, &error, &warnings); if (!module) { return nullptr; } diff --git a/js/src/wasm/WasmModule.h b/js/src/wasm/WasmModule.h index 75660a75e747..599fad2e620f 100644 --- a/js/src/wasm/WasmModule.h +++ b/js/src/wasm/WasmModule.h @@ -225,8 +225,8 @@ typedef RefPtr SharedModule; MOZ_MUST_USE bool GetOptimizedEncodingBuildId(JS::BuildIdCharVector* buildId); -RefPtr DeserializeModule(PRFileDesc* bytecode, - UniqueChars filename, unsigned line); +RefPtr DeserializeModule(const uint8_t* bytecode, + size_t bytecodeLength); } // namespace wasm } // namespace js