1999-09-01 01:40:21 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
2000-05-03 02:38:04 +04:00
|
|
|
* This Original Code has been modified by IBM Corporation.
|
|
|
|
* Modifications made by IBM described herein are
|
|
|
|
* Copyright (c) International Business Machines
|
|
|
|
* Corporation, 2000
|
|
|
|
*
|
|
|
|
* Modifications to Mozilla code or documentation
|
|
|
|
* identified per MPL Section 3.3
|
|
|
|
*
|
|
|
|
* Date Modified by Description of modification
|
|
|
|
* 04/20/2000 IBM Corp. Added PR_CALLBACK for Optlink use in OS2
|
1999-09-01 01:40:21 +04:00
|
|
|
*/
|
|
|
|
|
2006-04-13 21:03:18 +04:00
|
|
|
/* Allow logging in the release build */
|
|
|
|
#ifdef MOZ_LOGGING
|
2005-12-13 20:55:43 +03:00
|
|
|
#define FORCE_PR_LOG
|
2006-04-13 21:03:18 +04:00
|
|
|
#endif
|
2005-12-03 18:27:17 +03:00
|
|
|
|
2005-12-13 20:55:43 +03:00
|
|
|
#include "nsNativeComponentLoader.h"
|
2000-01-25 00:28:28 +03:00
|
|
|
|
2000-10-29 02:17:53 +04:00
|
|
|
#include "prlog.h"
|
2005-12-13 20:55:43 +03:00
|
|
|
#include "prinit.h"
|
2006-01-18 18:55:10 +03:00
|
|
|
#include "prerror.h"
|
1999-09-01 01:40:21 +04:00
|
|
|
|
2005-12-13 20:55:43 +03:00
|
|
|
#include "nsComponentManager.h"
|
2011-06-24 22:08:13 +04:00
|
|
|
#include "ManifestParser.h" // for LogMessage
|
2005-12-13 20:55:43 +03:00
|
|
|
#include "nsCRTGlue.h"
|
2008-11-26 22:39:36 +03:00
|
|
|
#include "nsThreadUtils.h"
|
2005-12-13 20:55:43 +03:00
|
|
|
#include "nsTraceRefcntImpl.h"
|
2005-12-03 18:27:17 +03:00
|
|
|
|
2012-06-06 06:08:30 +04:00
|
|
|
#include "nsIFile.h"
|
2013-11-12 17:31:32 +04:00
|
|
|
#include "mozilla/WindowsDllBlocklist.h"
|
2000-10-29 02:17:53 +04:00
|
|
|
|
2005-12-13 20:55:43 +03:00
|
|
|
#ifdef XP_WIN
|
|
|
|
#include <windows.h>
|
2003-04-09 00:29:43 +04:00
|
|
|
#endif
|
1999-09-26 22:06:41 +04:00
|
|
|
|
2005-12-13 20:55:43 +03:00
|
|
|
#ifdef XP_MACOSX
|
|
|
|
#include <signal.h>
|
|
|
|
#endif
|
2003-04-09 00:29:43 +04:00
|
|
|
|
2005-12-13 20:55:43 +03:00
|
|
|
#ifdef VMS
|
|
|
|
#include <lib$routines.h>
|
|
|
|
#include <ssdef.h>
|
|
|
|
#endif
|
2003-04-09 00:29:43 +04:00
|
|
|
|
Bug 627277 - Remove (broken) BeOS support. r=biesi,dwitte,gavin,joe,jorendorff,josh,khuey,mfinkle,neil,Pike,roc,shaver,smontagu,taras
2011-02-19 22:10:24 +03:00
|
|
|
#ifdef DEBUG
|
2005-12-13 20:55:43 +03:00
|
|
|
#define IMPLEMENT_BREAK_AFTER_LOAD
|
|
|
|
#endif
|
2003-04-09 00:29:43 +04:00
|
|
|
|
2011-11-08 21:10:51 +04:00
|
|
|
using namespace mozilla;
|
|
|
|
|
2012-11-08 01:20:25 +04:00
|
|
|
static PRLogModuleInfo *
|
|
|
|
GetNativeModuleLoaderLog()
|
|
|
|
{
|
|
|
|
static PRLogModuleInfo *sLog;
|
|
|
|
if (!sLog)
|
|
|
|
sLog = PR_NewLogModule("nsNativeModuleLoader");
|
|
|
|
return sLog;
|
|
|
|
}
|
2003-04-09 00:29:43 +04:00
|
|
|
|
2012-11-08 01:20:25 +04:00
|
|
|
#define LOG(level, args) PR_LOG(GetNativeModuleLoaderLog(), level, args)
|
2005-12-03 18:27:17 +03:00
|
|
|
|
2012-11-08 01:20:25 +04:00
|
|
|
NS_IMPL_QUERY_INTERFACE1(nsNativeModuleLoader,
|
2010-06-10 22:11:11 +04:00
|
|
|
mozilla::ModuleLoader)
|
2008-02-21 23:39:20 +03:00
|
|
|
|
2005-12-13 20:55:43 +03:00
|
|
|
NS_IMPL_ADDREF_USING_AGGREGATOR(nsNativeModuleLoader,
|
|
|
|
nsComponentManagerImpl::gComponentManager)
|
|
|
|
NS_IMPL_RELEASE_USING_AGGREGATOR(nsNativeModuleLoader,
|
|
|
|
nsComponentManagerImpl::gComponentManager)
|
2005-12-03 18:27:17 +03:00
|
|
|
|
2005-12-13 20:55:43 +03:00
|
|
|
nsresult
|
|
|
|
nsNativeModuleLoader::Init()
|
1999-09-14 17:40:22 +04:00
|
|
|
{
|
2013-02-08 09:54:20 +04:00
|
|
|
MOZ_ASSERT(NS_IsMainThread(), "Startup not on main thread?");
|
2005-12-13 20:55:43 +03:00
|
|
|
LOG(PR_LOG_DEBUG, ("nsNativeModuleLoader::Init()"));
|
2012-05-18 21:30:49 +04:00
|
|
|
return NS_OK;
|
1999-09-14 17:40:22 +04:00
|
|
|
}
|
|
|
|
|
2010-06-10 22:11:11 +04:00
|
|
|
class LoadModuleMainThreadRunnable : public nsRunnable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
LoadModuleMainThreadRunnable(nsNativeModuleLoader* loader,
|
2011-11-08 21:10:51 +04:00
|
|
|
FileLocation &file)
|
2010-06-10 22:11:11 +04:00
|
|
|
: mLoader(loader)
|
|
|
|
, mFile(file)
|
2013-10-11 00:41:00 +04:00
|
|
|
, mResult(nullptr)
|
2010-06-10 22:11:11 +04:00
|
|
|
{ }
|
|
|
|
|
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
|
|
|
mResult = mLoader->LoadModule(mFile);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<nsNativeModuleLoader> mLoader;
|
2011-11-08 21:10:51 +04:00
|
|
|
FileLocation mFile;
|
2010-06-10 22:11:11 +04:00
|
|
|
const mozilla::Module* mResult;
|
|
|
|
};
|
|
|
|
|
|
|
|
const mozilla::Module*
|
2011-11-08 21:10:51 +04:00
|
|
|
nsNativeModuleLoader::LoadModule(FileLocation &aFile)
|
2005-12-03 18:27:17 +03:00
|
|
|
{
|
2011-11-08 21:10:51 +04:00
|
|
|
if (aFile.IsZip()) {
|
|
|
|
NS_ERROR("Binary components cannot be loaded from JARs");
|
2013-10-11 00:41:00 +04:00
|
|
|
return nullptr;
|
2011-11-08 21:10:51 +04:00
|
|
|
}
|
2012-06-06 06:08:30 +04:00
|
|
|
nsCOMPtr<nsIFile> file = aFile.GetBaseFile();
|
2005-12-13 20:55:43 +03:00
|
|
|
nsresult rv;
|
2005-12-03 18:27:17 +03:00
|
|
|
|
2008-11-26 22:39:36 +03:00
|
|
|
if (!NS_IsMainThread()) {
|
|
|
|
// If this call is off the main thread, synchronously proxy it
|
|
|
|
// to the main thread.
|
2010-06-10 22:11:11 +04:00
|
|
|
nsRefPtr<LoadModuleMainThreadRunnable> r = new LoadModuleMainThreadRunnable(this, aFile);
|
|
|
|
NS_DispatchToMainThread(r, NS_DISPATCH_SYNC);
|
|
|
|
return r->mResult;
|
2008-11-26 22:39:36 +03:00
|
|
|
}
|
|
|
|
|
2011-11-08 21:10:51 +04:00
|
|
|
nsCOMPtr<nsIHashable> hashedFile(do_QueryInterface(file));
|
2005-12-13 20:55:43 +03:00
|
|
|
if (!hashedFile) {
|
|
|
|
NS_ERROR("nsIFile is not nsIHashable");
|
2013-10-11 00:41:00 +04:00
|
|
|
return nullptr;
|
1999-10-22 08:47:14 +04:00
|
|
|
}
|
2005-12-03 18:27:17 +03:00
|
|
|
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString filePath;
|
2011-11-08 21:10:51 +04:00
|
|
|
file->GetNativePath(filePath);
|
2010-06-23 00:22:17 +04:00
|
|
|
|
2005-12-13 20:55:43 +03:00
|
|
|
NativeLoadData data;
|
2005-12-03 18:27:17 +03:00
|
|
|
|
2005-12-13 20:55:43 +03:00
|
|
|
if (mLibraries.Get(hashedFile, &data)) {
|
|
|
|
NS_ASSERTION(data.module, "Corrupt mLibraries hash");
|
|
|
|
LOG(PR_LOG_DEBUG,
|
|
|
|
("nsNativeModuleLoader::LoadModule(\"%s\") - found in cache",
|
|
|
|
filePath.get()));
|
2010-06-10 22:11:11 +04:00
|
|
|
return data.module;
|
2005-12-03 18:27:17 +03:00
|
|
|
}
|
2002-11-21 05:36:50 +03:00
|
|
|
|
2005-12-13 20:55:43 +03:00
|
|
|
// We haven't loaded this module before
|
2013-11-12 17:31:32 +04:00
|
|
|
{
|
|
|
|
#ifdef HAS_DLL_BLOCKLIST
|
|
|
|
AutoSetXPCOMLoadOnMainThread guard;
|
|
|
|
#endif
|
|
|
|
rv = file->Load(&data.library);
|
|
|
|
}
|
2006-02-14 17:45:32 +03:00
|
|
|
|
2005-12-13 20:55:43 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
2006-01-18 18:55:10 +03:00
|
|
|
char errorMsg[1024] = "<unknown; can't get error from NSPR>";
|
|
|
|
|
|
|
|
if (PR_GetErrorTextLength() < (int) sizeof(errorMsg))
|
|
|
|
PR_GetErrorText(errorMsg);
|
|
|
|
|
2011-06-24 22:08:13 +04:00
|
|
|
LogMessage("Failed to load native module at path '%s': (%lx) %s",
|
|
|
|
filePath.get(), rv, errorMsg);
|
2006-01-18 18:55:10 +03:00
|
|
|
|
2013-10-11 00:41:00 +04:00
|
|
|
return nullptr;
|
2005-12-02 19:34:42 +03:00
|
|
|
}
|
2002-11-21 05:36:50 +03:00
|
|
|
|
2005-12-13 20:55:43 +03:00
|
|
|
#ifdef IMPLEMENT_BREAK_AFTER_LOAD
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString leafName;
|
2011-11-08 21:10:51 +04:00
|
|
|
file->GetNativeLeafName(leafName);
|
2005-12-13 20:55:43 +03:00
|
|
|
|
|
|
|
char *env = getenv("XPCOM_BREAK_ON_LOAD");
|
|
|
|
char *blist;
|
|
|
|
if (env && *env && (blist = strdup(env))) {
|
|
|
|
char *nextTok = blist;
|
|
|
|
while (char *token = NS_strtok(":", &nextTok)) {
|
2011-10-17 18:59:28 +04:00
|
|
|
if (leafName.Find(token, true) != kNotFound) {
|
2005-12-13 20:55:43 +03:00
|
|
|
NS_BREAK();
|
|
|
|
}
|
1999-09-01 01:40:21 +04:00
|
|
|
}
|
|
|
|
|
2005-12-13 20:55:43 +03:00
|
|
|
free(blist);
|
2005-12-02 19:34:42 +03:00
|
|
|
}
|
2003-04-09 00:29:43 +04:00
|
|
|
#endif
|
2000-05-15 23:28:12 +04:00
|
|
|
|
2010-07-15 16:55:05 +04:00
|
|
|
void *module = PR_FindSymbol(data.library, "NSModule");
|
2011-06-24 22:08:13 +04:00
|
|
|
if (!module) {
|
|
|
|
LogMessage("Native module at path '%s' doesn't export symbol `NSModule`.",
|
|
|
|
filePath.get());
|
|
|
|
PR_UnloadLibrary(data.library);
|
2013-10-11 00:41:00 +04:00
|
|
|
return nullptr;
|
1999-10-22 08:47:14 +04:00
|
|
|
}
|
1999-11-10 03:28:34 +03:00
|
|
|
|
2011-06-24 22:08:13 +04:00
|
|
|
data.module = *(mozilla::Module const *const *) module;
|
|
|
|
if (mozilla::Module::kVersion != data.module->mVersion) {
|
|
|
|
LogMessage("Native module at path '%s' is incompatible with this version of Firefox, has version %i, expected %i.",
|
|
|
|
filePath.get(), data.module->mVersion,
|
|
|
|
mozilla::Module::kVersion);
|
|
|
|
PR_UnloadLibrary(data.library);
|
2013-10-11 00:41:00 +04:00
|
|
|
return nullptr;
|
2011-06-24 22:08:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
mLibraries.Put(hashedFile, data); // infallible
|
|
|
|
return data.module;
|
1999-09-01 01:40:21 +04:00
|
|
|
}
|
|
|
|
|
2005-12-13 20:55:43 +03:00
|
|
|
PLDHashOperator
|
|
|
|
nsNativeModuleLoader::ReleaserFunc(nsIHashable* aHashedFile,
|
|
|
|
NativeLoadData& aLoadData, void*)
|
1999-09-01 01:40:21 +04:00
|
|
|
{
|
2012-07-30 18:20:58 +04:00
|
|
|
aLoadData.module = nullptr;
|
2005-12-13 20:55:43 +03:00
|
|
|
return PL_DHASH_NEXT;
|
1999-09-01 01:40:21 +04:00
|
|
|
}
|
|
|
|
|
2005-12-13 20:55:43 +03:00
|
|
|
PLDHashOperator
|
|
|
|
nsNativeModuleLoader::UnloaderFunc(nsIHashable* aHashedFile,
|
|
|
|
NativeLoadData& aLoadData, void*)
|
1999-09-01 01:40:21 +04:00
|
|
|
{
|
2012-11-08 01:20:25 +04:00
|
|
|
if (PR_LOG_TEST(GetNativeModuleLoaderLog(), PR_LOG_DEBUG)) {
|
2005-12-13 20:55:43 +03:00
|
|
|
nsCOMPtr<nsIFile> file(do_QueryInterface(aHashedFile));
|
1999-09-26 22:06:41 +04:00
|
|
|
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString filePath;
|
2005-12-13 20:55:43 +03:00
|
|
|
file->GetNativePath(filePath);
|
1999-09-01 01:40:21 +04:00
|
|
|
|
2005-12-13 20:55:43 +03:00
|
|
|
LOG(PR_LOG_DEBUG,
|
|
|
|
("nsNativeModuleLoader::UnloaderFunc(\"%s\")", filePath.get()));
|
1999-10-22 08:47:14 +04:00
|
|
|
}
|
1999-09-01 01:40:21 +04:00
|
|
|
|
2005-12-13 20:55:43 +03:00
|
|
|
#ifdef NS_BUILD_REFCNT_LOGGING
|
2011-10-17 18:59:28 +04:00
|
|
|
nsTraceRefcntImpl::SetActivityIsLegal(false);
|
2005-12-02 23:27:52 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if 0
|
2005-12-13 20:55:43 +03:00
|
|
|
// XXXbsmedberg: do this as soon as the static-destructor crash(es)
|
|
|
|
// are fixed
|
|
|
|
PRStatus ret = PR_UnloadLibrary(aLoadData.library);
|
|
|
|
NS_ASSERTION(ret == PR_SUCCESS, "Failed to unload library");
|
2005-12-02 23:27:52 +03:00
|
|
|
#endif
|
1999-09-01 01:40:21 +04:00
|
|
|
|
2005-12-13 20:55:43 +03:00
|
|
|
#ifdef NS_BUILD_REFCNT_LOGGING
|
2011-10-17 18:59:28 +04:00
|
|
|
nsTraceRefcntImpl::SetActivityIsLegal(true);
|
2005-12-02 19:34:42 +03:00
|
|
|
#endif
|
1999-09-02 11:00:29 +04:00
|
|
|
|
2005-12-13 20:55:43 +03:00
|
|
|
return PL_DHASH_REMOVE;
|
1999-09-01 01:40:21 +04:00
|
|
|
}
|
|
|
|
|
2005-12-13 20:55:43 +03:00
|
|
|
void
|
|
|
|
nsNativeModuleLoader::UnloadLibraries()
|
1999-09-01 01:40:21 +04:00
|
|
|
{
|
2013-02-08 09:54:20 +04:00
|
|
|
MOZ_ASSERT(NS_IsMainThread(), "Shutdown not on main thread?");
|
2012-07-30 18:20:58 +04:00
|
|
|
mLibraries.Enumerate(ReleaserFunc, nullptr);
|
|
|
|
mLibraries.Enumerate(UnloaderFunc, nullptr);
|
1999-09-01 01:40:21 +04:00
|
|
|
}
|