Skip to content

Commit

Permalink
Bug 1847469 - Part 24: Use column number types in JS::DescribeScripte…
Browse files Browse the repository at this point in the history
…dCaller. r=iain

Differential Revision: https://phabricator.services.mozilla.com/D185762
  • Loading branch information
arai-a committed Aug 16, 2023
1 parent 264b992 commit 357b9c5
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 26 deletions.
5 changes: 3 additions & 2 deletions caps/nsScriptSecurityManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
#include "nsContentUtils.h"
#include "nsJSUtils.h"
#include "nsILoadInfo.h"
#include "js/ColumnNumber.h" // JS::ColumnNumberZeroOrigin

// This should be probably defined on some other place... but I couldn't find it
#define WEBAPPS_PERM_NAME "webapps-manage"
Expand Down Expand Up @@ -532,7 +533,7 @@ bool nsScriptSecurityManager::ContentSecurityPolicyPermitsJSAction(
JS::AutoFilename scriptFilename;
nsAutoString fileName;
uint32_t lineNum = 0;
uint32_t columnNum = 0;
JS::ColumnNumberZeroOrigin columnNum;
if (JS::DescribeScriptedCaller(cx, &scriptFilename, &lineNum, &columnNum)) {
if (const char* file = scriptFilename.get()) {
CopyUTF8toUTF16(nsDependentCString(file), fileName);
Expand All @@ -554,7 +555,7 @@ bool nsScriptSecurityManager::ContentSecurityPolicyPermitsJSAction(
csp->LogViolationDetails(violationType,
nullptr, // triggering element
cspEventListener, fileName, scriptSample, lineNum,
columnNum, u""_ns, u""_ns);
columnNum.zeroOriginValue(), u""_ns, u""_ns);
}

return evalOK;
Expand Down
4 changes: 1 addition & 3 deletions dom/base/Document.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4323,9 +4323,7 @@ void Document::NoteScriptTrackingStatus(const nsACString& aURL,

bool Document::IsScriptTracking(JSContext* aCx) const {
JS::AutoFilename filename;
uint32_t line = 0;
uint32_t column = 0;
if (!JS::DescribeScriptedCaller(aCx, &filename, &line, &column)) {
if (!JS::DescribeScriptedCaller(aCx, &filename)) {
return false;
}
return mTrackingScripts.Contains(nsDependentCString(filename.get()));
Expand Down
12 changes: 10 additions & 2 deletions dom/base/nsJSUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,27 @@ using namespace mozilla::dom;
bool nsJSUtils::GetCallingLocation(JSContext* aContext, nsACString& aFilename,
uint32_t* aLineno, uint32_t* aColumn) {
JS::AutoFilename filename;
if (!JS::DescribeScriptedCaller(aContext, &filename, aLineno, aColumn)) {
JS::ColumnNumberZeroOrigin column;
if (!JS::DescribeScriptedCaller(aContext, &filename, aLineno, &column)) {
return false;
}
if (aColumn) {
*aColumn = column.zeroOriginValue();
}

return aFilename.Assign(filename.get(), fallible);
}

bool nsJSUtils::GetCallingLocation(JSContext* aContext, nsAString& aFilename,
uint32_t* aLineno, uint32_t* aColumn) {
JS::AutoFilename filename;
if (!JS::DescribeScriptedCaller(aContext, &filename, aLineno, aColumn)) {
JS::ColumnNumberZeroOrigin column;
if (!JS::DescribeScriptedCaller(aContext, &filename, aLineno, &column)) {
return false;
}
if (aColumn) {
*aColumn = column.zeroOriginValue();
}

return aFilename.Assign(NS_ConvertUTF8toUTF16(filename.get()), fallible);
}
Expand Down
5 changes: 3 additions & 2 deletions dom/events/EventListenerManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
// Microsoft's API Name hackery sucks
#undef CreateEvent

#include "js/ColumnNumber.h" // JS::ColumnNumberZeroOrigin
#include "js/loader/LoadedScript.h"
#include "mozilla/BasicEvents.h"
#include "mozilla/BinarySearch.h"
Expand Down Expand Up @@ -1028,7 +1029,7 @@ nsresult EventListenerManager::SetEventHandler(nsAtom* aName,
// Perform CSP check
nsCOMPtr<nsIContentSecurityPolicy> csp = doc->GetCsp();
uint32_t lineNum = 0;
uint32_t columnNum = 0;
JS::ColumnNumberZeroOrigin columnNum;

JSContext* cx = nsContentUtils::GetCurrentJSContext();
if (cx && !JS::DescribeScriptedCaller(cx, nullptr, &lineNum, &columnNum)) {
Expand All @@ -1044,7 +1045,7 @@ nsresult EventListenerManager::SetEventHandler(nsAtom* aName,
true, // aParserCreated (true because attribute event handler)
aElement,
nullptr, // nsICSPEventListener
aBody, lineNum, columnNum, &allowsInlineScript);
aBody, lineNum, columnNum.zeroOriginValue(), &allowsInlineScript);
NS_ENSURE_SUCCESS(rv, rv);

// return early if CSP wants us to block inline scripts
Expand Down
5 changes: 3 additions & 2 deletions dom/system/IOUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include "ErrorList.h"
#include "js/ArrayBuffer.h"
#include "js/ColumnNumber.h" // JS::ColumnNumberZeroOrigin
#include "js/JSON.h"
#include "js/Utility.h"
#include "js/experimental/TypedData.h"
Expand Down Expand Up @@ -290,15 +291,15 @@ static bool AssertParentProcessWithCallerLocationImpl(GlobalObject& aGlobal,

JS::AutoFilename scriptFilename;
uint32_t lineNo = 0;
uint32_t colNo = 0;
JS::ColumnNumberZeroOrigin colNo;

NS_ENSURE_TRUE(
JS::DescribeScriptedCaller(cx, &scriptFilename, &lineNo, &colNo), false);

NS_ENSURE_TRUE(scriptFilename.get(), false);

reason.AppendPrintf(" Called from %s:%d:%d.", scriptFilename.get(), lineNo,
colNo);
colNo.zeroOriginValue());
return false;
}

Expand Down
12 changes: 8 additions & 4 deletions dom/websocket/WebSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "mozilla/dom/WebSocketBinding.h"
#include "mozilla/net/WebSocketChannel.h"

#include "js/ColumnNumber.h" // JS::ColumnNumberZeroOrigin
#include "jsapi.h"
#include "jsfriendapi.h"
#include "mozilla/Atomics.h"
Expand Down Expand Up @@ -1371,7 +1372,8 @@ already_AddRefed<WebSocket> WebSocket::ConstructorCommon(
WorkerPrivate* workerPrivate = GetCurrentThreadWorkerPrivate();
MOZ_ASSERT(workerPrivate);

uint32_t lineno, column;
uint32_t lineno;
JS::ColumnNumberZeroOrigin column;
JS::AutoFilename file;
if (!JS::DescribeScriptedCaller(aGlobal.Context(), &file, &lineno,
&column)) {
Expand All @@ -1381,7 +1383,8 @@ already_AddRefed<WebSocket> WebSocket::ConstructorCommon(
RefPtr<InitRunnable> runnable = new InitRunnable(
workerPrivate, webSocketImpl,
workerPrivate->GlobalScope()->GetClientInfo(), !!aTransportProvider,
aUrl, protocolArray, nsDependentCString(file.get()), lineno, column);
aUrl, protocolArray, nsDependentCString(file.get()), lineno,
column.zeroOriginValue());
runnable->Dispatch(Canceling, aRv);
if (NS_WARN_IF(aRv.Failed())) {
return nullptr;
Expand Down Expand Up @@ -1608,12 +1611,13 @@ nsresult WebSocketImpl::Init(JSContext* aCx, bool aIsSecure,
} else {
MOZ_ASSERT(aCx);

uint32_t lineno, column;
uint32_t lineno;
JS::ColumnNumberZeroOrigin column;
JS::AutoFilename file;
if (JS::DescribeScriptedCaller(aCx, &file, &lineno, &column)) {
mScriptFile = file.get();
mScriptLine = lineno;
mScriptColumn = column;
mScriptColumn = column.zeroOriginValue();
}
}

Expand Down
6 changes: 4 additions & 2 deletions dom/workers/RuntimeService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <algorithm>
#include "mozilla/ipc/BackgroundChild.h"
#include "GeckoProfiler.h"
#include "js/ColumnNumber.h" // JS::ColumnNumberZeroOrigin
#include "js/experimental/CTypes.h" // JS::CTypesActivityType, JS::SetCTypesActivityCallback
#include "jsfriendapi.h"
#include "js/friend/ErrorMessages.h" // js::GetErrorMessage, JSMSG_*
Expand Down Expand Up @@ -518,7 +519,7 @@ bool ContentSecurityPolicyAllows(JSContext* aCx, JS::RuntimeCode aKind,
if (reportViolation) {
nsString fileName;
uint32_t lineNum = 0;
uint32_t columnNum = 0;
JS::ColumnNumberZeroOrigin columnNum;

JS::AutoFilename file;
if (JS::DescribeScriptedCaller(aCx, &file, &lineNum, &columnNum) &&
Expand All @@ -530,7 +531,8 @@ bool ContentSecurityPolicyAllows(JSContext* aCx, JS::RuntimeCode aKind,

RefPtr<LogViolationDetailsRunnable> runnable =
new LogViolationDetailsRunnable(worker, violationType, fileName,
lineNum, columnNum, scriptSample);
lineNum, columnNum.zeroOriginValue(),
scriptSample);

ErrorResult rv;
runnable->Dispatch(Killing, rv);
Expand Down
11 changes: 6 additions & 5 deletions js/src/jsapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
#include "jit/JitSpewer.h"
#include "js/CallAndConstruct.h" // JS::IsCallable
#include "js/CharacterEncoding.h"
#include "js/ColumnNumber.h" // JS::TaggedColumnNumberZeroOrigin, JS::ColumnNumberOneOrigin
#include "js/ColumnNumber.h" // JS::TaggedColumnNumberZeroOrigin, JS::ColumnNumberZeroOrigin, JS::ColumnNumberOneOrigin
#include "js/CompileOptions.h"
#include "js/ContextOptions.h" // JS::ContextOptions{,Ref}
#include "js/Conversions.h"
Expand Down Expand Up @@ -4524,15 +4524,16 @@ const char* AutoFilename::get() const {
}

JS_PUBLIC_API bool DescribeScriptedCaller(JSContext* cx, AutoFilename* filename,
uint32_t* lineno, uint32_t* column) {
uint32_t* lineno,
JS::ColumnNumberZeroOrigin* column) {
if (filename) {
filename->reset();
}
if (lineno) {
*lineno = 0;
}
if (column) {
*column = 0;
*column = JS::ColumnNumberZeroOrigin::zero();
}

if (!cx->compartment()) {
Expand Down Expand Up @@ -4569,12 +4570,12 @@ JS_PUBLIC_API bool DescribeScriptedCaller(JSContext* cx, AutoFilename* filename,
JS::TaggedColumnNumberZeroOrigin columnNumber;
*lineno = i.computeLine(&columnNumber);
if (column) {
*column = columnNumber.zeroOriginValue();
*column = JS::ColumnNumberZeroOrigin(columnNumber.zeroOriginValue());
}
} else if (column) {
JS::TaggedColumnNumberZeroOrigin columnNumber;
i.computeLine(&columnNumber);
*column = columnNumber.zeroOriginValue();
*column = JS::ColumnNumberZeroOrigin(columnNumber.zeroOriginValue());
}

return true;
Expand Down
2 changes: 1 addition & 1 deletion js/src/jsapi.h
Original file line number Diff line number Diff line change
Expand Up @@ -934,7 +934,7 @@ class MOZ_RAII JS_PUBLIC_API AutoFilename {
*/
extern JS_PUBLIC_API bool DescribeScriptedCaller(
JSContext* cx, AutoFilename* filename = nullptr, uint32_t* lineno = nullptr,
uint32_t* column = nullptr);
JS::ColumnNumberZeroOrigin* column = nullptr);

extern JS_PUBLIC_API JSObject* GetScriptedCallerGlobal(JSContext* cx);

Expand Down
9 changes: 6 additions & 3 deletions js/xpconnect/wrappers/XrayWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include "jsapi.h"
#include "js/CallAndConstruct.h" // JS::Call, JS::Construct, JS::IsCallable
#include "js/ColumnNumber.h" // JS::ColumnNumberZeroOrigin
#include "js/experimental/TypedData.h" // JS_GetTypedArrayLength
#include "js/friend/WindowProxy.h" // js::IsWindowProxy
#include "js/friend/XrayJitInfo.h" // JS::XrayJitInfo
Expand Down Expand Up @@ -217,14 +218,15 @@ bool ReportWrapperDenial(JSContext* cx, HandleId id, WrapperDenialType type,
return false;
}
AutoFilename filename;
uint32_t line = 0, column = 0;
uint32_t line = 0;
JS::ColumnNumberZeroOrigin column;
DescribeScriptedCaller(cx, &filename, &line, &column);

// Warn to the terminal for the logs.
NS_WARNING(
nsPrintfCString("Silently denied access to property %s: %s (@%s:%u:%u)",
NS_LossyConvertUTF16toASCII(propertyName).get(), reason,
filename.get(), line, column)
filename.get(), line, column.zeroOriginValue())
.get());

// If this isn't the first warning on this topic for this global, we've
Expand Down Expand Up @@ -271,7 +273,8 @@ bool ReportWrapperDenial(JSContext* cx, HandleId id, WrapperDenialType type,
nsString filenameStr(NS_ConvertASCIItoUTF16(filename.get()));
nsresult rv = errorObject->InitWithWindowID(
NS_ConvertASCIItoUTF16(errorMessage.ref()), filenameStr, u""_ns, line,
column, nsIScriptError::warningFlag, "XPConnect", windowId);
column.zeroOriginValue(), nsIScriptError::warningFlag, "XPConnect",
windowId);
NS_ENSURE_SUCCESS(rv, true);
rv = consoleService->LogMessage(errorObject);
NS_ENSURE_SUCCESS(rv, true);
Expand Down

0 comments on commit 357b9c5

Please sign in to comment.