Skip to content

Commit

Permalink
Merge branch 'main' into fix/signDisplay
Browse files Browse the repository at this point in the history
  • Loading branch information
shubhamguptadream11 authored Sep 1, 2024
2 parents 9ce83eb + 1dd060f commit 5d9cc93
Show file tree
Hide file tree
Showing 11 changed files with 88 additions and 25 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: facebook/hermes/build
on:
push
push:
pull_request:
branches:
- main
Expand Down Expand Up @@ -43,7 +43,7 @@ jobs:
name: android-hermes
path: output
linux:
runs-on: ubuntu-20.04
runs-on: ubuntu-22.04
steps:
- name: Install dependencies
run: |-
Expand Down
55 changes: 44 additions & 11 deletions API/hermes/hermes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1197,15 +1197,25 @@ void HermesRuntime::setFatalHandler(void (*handler)(const std::string &)) {
}

namespace {
// A class which adapts a jsi buffer to a Hermes buffer.

/// A class which adapts a jsi buffer to a Hermes buffer.
/// It also provides the ability to create a partial "view" into the buffer.
class BufferAdapter final : public ::hermes::Buffer {
public:
BufferAdapter(std::shared_ptr<const jsi::Buffer> buf) : buf_(std::move(buf)) {
data_ = buf_->data();
size_ = buf_->size();
explicit BufferAdapter(
const std::shared_ptr<const jsi::Buffer> &buf,
const uint8_t *data,
size_t size)
: buf_(buf) {
data_ = data;
size_ = size;
}

explicit BufferAdapter(const std::shared_ptr<const jsi::Buffer> &buf)
: BufferAdapter(buf, buf->data(), buf->size()) {}

private:
/// The buffer we are "adapting".
std::shared_ptr<const jsi::Buffer> buf_;
};
} // namespace
Expand Down Expand Up @@ -1420,6 +1430,28 @@ class HermesPreparedJavaScript final : public jsi::PreparedJavaScript {
}
};

#ifndef HERMESVM_LEAN

/// If the buffer contains an embedded terminating zero, shrink it, so it is
/// one past the size, as per the LLVM MemoryBuffer convention. Otherwise, copy
/// it into a new zero-terminated buffer.
std::unique_ptr<BufferAdapter> ensureZeroTerminated(
const std::shared_ptr<const jsi::Buffer> &buf) {
size_t size = buf->size();
const uint8_t *data = buf->data();

// Check for zero termination
if (size != 0 && data[size - 1] == 0) {
return std::make_unique<BufferAdapter>(buf, data, size - 1);
} else {
// Copy into a zero-terminated instance.
return std::make_unique<BufferAdapter>(std::make_shared<jsi::StringBuffer>(
std::string((const char *)data, size)));
}
}

#endif

} // namespace

std::shared_ptr<const jsi::PreparedJavaScript>
Expand All @@ -1428,11 +1460,10 @@ HermesRuntimeImpl::prepareJavaScriptWithSourceMap(
const std::shared_ptr<const jsi::Buffer> &sourceMapBuf,
std::string sourceURL) {
std::pair<std::unique_ptr<hbc::BCProvider>, std::string> bcErr{};
auto buffer = std::make_unique<BufferAdapter>(jsiBuffer);
vm::RuntimeModuleFlags runtimeFlags{};
runtimeFlags.persistent = true;

bool isBytecode = isHermesBytecode(buffer->data(), buffer->size());
bool isBytecode = isHermesBytecode(jsiBuffer->data(), jsiBuffer->size());
#ifdef HERMESVM_PLATFORM_LOGGING
hermesLog(
"HermesVM", "Prepare JS on %s.", isBytecode ? "bytecode" : "source");
Expand All @@ -1444,18 +1475,17 @@ HermesRuntimeImpl::prepareJavaScriptWithSourceMap(
throw std::logic_error("Source map cannot be specified with bytecode");
}
bcErr = hbc::BCProviderFromBuffer::createBCProviderFromBuffer(
std::move(buffer));
std::make_unique<BufferAdapter>(jsiBuffer));
} else {
#if defined(HERMESVM_LEAN)
bcErr.second = "prepareJavaScript source compilation not supported";
#else
std::unique_ptr<::hermes::SourceMap> sourceMap{};
if (sourceMapBuf) {
auto buf0 = ensureZeroTerminated(sourceMapBuf);
// Convert the buffer into a form the parser needs.
llvh::MemoryBufferRef mbref(
llvh::StringRef(
(const char *)sourceMapBuf->data(), sourceMapBuf->size()),
"");
llvh::StringRef((const char *)buf0->data(), buf0->size()), "");
::hermes::SimpleDiagHandler diag;
::hermes::SourceErrorManager sm;
diag.installInto(sm);
Expand All @@ -1467,7 +1497,10 @@ HermesRuntimeImpl::prepareJavaScriptWithSourceMap(
}
}
bcErr = hbc::BCProviderFromSrc::createBCProviderFromSrc(
std::move(buffer), sourceURL, std::move(sourceMap), compileFlags_);
ensureZeroTerminated(jsiBuffer),
sourceURL,
std::move(sourceMap),
compileFlags_);
#endif
}
if (!bcErr.first) {
Expand Down
2 changes: 1 addition & 1 deletion lib/VM/JSArray.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ void ArrayImpl::_snapshotAddEdgesImpl(
for (uint32_t i = beginIndex; i < endIndex; i++) {
const auto &elem = indexedStorage->at(gc.getPointerBase(), i - beginIndex);
const llvh::Optional<HeapSnapshot::NodeID> elemID =
gc.getSnapshotID(elem.unboxToHV(gc.getPointerBase()));
gc.getSnapshotID(elem.toHV(gc.getPointerBase()));
if (!elemID) {
continue;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/VM/JSObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2471,7 +2471,7 @@ void JSObject::_snapshotAddEdgesImpl(GCCell *cell, GC &gc, HeapSnapshot &snap) {
// Else, it's a user-visible property.
HermesValue prop =
getNamedSlotValueUnsafe(self, gc.getPointerBase(), desc.slot)
.unboxToHV(gc.getPointerBase());
.toHV(gc.getPointerBase());
const llvh::Optional<HeapSnapshot::NodeID> idForProp =
gc.getSnapshotID(prop);
if (!idForProp) {
Expand Down
2 changes: 1 addition & 1 deletion tools/hermes-parser/js/.flowconfig
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ component_syntax=true
enums=true

[version]
^0.244.0
^0.245.0

[lints]
untyped-type-import=error
Expand Down
2 changes: 1 addition & 1 deletion tools/hermes-parser/js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"eslint-plugin-flowtype": "^8.0.3",
"eslint-plugin-jest": "^25.2.4",
"eslint-plugin-prettier": "^4.2.1",
"flow-bin": "^0.244.0",
"flow-bin": "^0.245.0",
"glob": "^8.0.3",
"jest": "^29.2.2",
"jest-specific-snapshot": "^5.0.0",
Expand Down
8 changes: 4 additions & 4 deletions tools/hermes-parser/js/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3003,10 +3003,10 @@ flatted@^3.1.0:
resolved "https://registry.yarnpkg.com/flatted/-/flatted-3.2.2.tgz#64bfed5cb68fe3ca78b3eb214ad97b63bedce561"
integrity sha512-JaTY/wtrcSyvXJl4IMFHPKyFur1sE9AUqc0QnhOaJ0CxHtAoIV8pYDzeEfAaNEtGkOfq4gr3LBFmdXW5mOQFnA==

flow-bin@^0.244.0:
version "0.244.0"
resolved "https://registry.yarnpkg.com/flow-bin/-/flow-bin-0.244.0.tgz#ed1c2123d4fd0a49b0bb62ce5c732a2e19927601"
integrity sha512-v9LyZCV7ubZZUOttMy1W4ih9cYd2XalxoH7/g+EvPrX5P/I4brA0JINgjW4A3B3P0macrgg466TbvcZrB9o7KQ==
flow-bin@^0.245.0:
version "0.245.0"
resolved "https://registry.yarnpkg.com/flow-bin/-/flow-bin-0.245.0.tgz#19497db89b7b1c6dd33301d5d7b537efc354e429"
integrity sha512-okpuzgdKWq7XQgzpl1Of4lcHtUq70ZtDniAyKxZ7YxgoWxjCm4LPMHPdcydw701rPG//waRPBvlIdPl7W6wPcg==

flow-enums-runtime@^0.0.6:
version "0.0.6"
Expand Down
20 changes: 20 additions & 0 deletions unittests/VMRuntime/HeapSnapshotTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -988,6 +988,9 @@ TEST_F(HeapSnapshotRuntimeTest, PropertyUpdatesTest) {
FIRST_NAMED_PROPERTY_EDGE + 2));
EXPECT_EQ(nodesAndEdges.second.size(), FIRST_NAMED_PROPERTY_EDGE + 2);

// When Handle-SAN is enabled, we put all numbers on the heap, which changes
// what the snapshot ID is.
#ifndef HERMESVM_SANITIZE_HANDLES
EXPECT_EQ(
nodesAndEdges.second[FIRST_NAMED_PROPERTY_EDGE],
Edge(
Expand All @@ -1001,6 +1004,7 @@ TEST_F(HeapSnapshotRuntimeTest, PropertyUpdatesTest) {
HeapSnapshot::EdgeType::Property,
"bar",
runtime.getHeap().getIDTracker().getNumberID(200)));
#endif
}

TEST_F(HeapSnapshotRuntimeTest, ArrayElementsCaptureNumeric) {
Expand Down Expand Up @@ -1047,24 +1051,32 @@ TEST_F(HeapSnapshotRuntimeTest, ArrayElementsCaptureNumeric) {
arrayID,
array->getAllocatedSize(),
FIRST_NAMED_PROPERTY_EDGE + 6));
// When Handle-SAN is enabled, we put all numbers on the heap, which changes
// what the snapshot ID is.
#ifndef HERMESVM_SANITIZE_HANDLES
EXPECT_EQ(
nodeAndEdges.second[FIRST_NAMED_PROPERTY_EDGE + 2],
Edge(
HeapSnapshot::EdgeType::Element,
(1 << 20) + 1000,
runtime.getHeap().getIDTracker().getNumberID(333)));
#endif
EXPECT_EQ(
nodeAndEdges.second[FIRST_NAMED_PROPERTY_EDGE + 4],
Edge(
HeapSnapshot::EdgeType::Element,
10,
runtime.getHeap().getObjectID(firstElement.get())));
// When Handle-SAN is enabled, we put all numbers on the heap, which changes
// what the snapshot ID is.
#ifndef HERMESVM_SANITIZE_HANDLES
EXPECT_EQ(
nodeAndEdges.second[FIRST_NAMED_PROPERTY_EDGE + 5],
Edge(
HeapSnapshot::EdgeType::Element,
15,
runtime.getHeap().getIDTracker().getNumberID(222)));
#endif

// Verify there are numeric nodes
bool hasNumeric = false;
Expand Down Expand Up @@ -1131,26 +1143,34 @@ TEST_F(HeapSnapshotRuntimeTest, ArrayElementsNoNumeric) {
array->getAllocatedSize(),
FIRST_NAMED_PROPERTY_EDGE + 6));

// When Handle-SAN is enabled, we put all numbers on the heap, which changes
// what the snapshot ID is.
#ifndef HERMESVM_SANITIZE_HANDLES
EXPECT_EQ(
nodeAndEdges.second[FIRST_NAMED_PROPERTY_EDGE + 2],
Edge(
HeapSnapshot::EdgeType::Element,
(1 << 20) + 1000,
GCBase::IDTracker::reserved(
GCBase::IDTracker::ReservedObjectID::Number)));
#endif
EXPECT_EQ(
nodeAndEdges.second[FIRST_NAMED_PROPERTY_EDGE + 4],
Edge(
HeapSnapshot::EdgeType::Element,
10,
runtime.getHeap().getObjectID(firstElement.get())));
// When Handle-SAN is enabled, we put all numbers on the heap, which changes
// what the snapshot ID is.
#ifndef HERMESVM_SANITIZE_HANDLES
EXPECT_EQ(
nodeAndEdges.second[FIRST_NAMED_PROPERTY_EDGE + 5],
Edge(
HeapSnapshot::EdgeType::Element,
15,
GCBase::IDTracker::reserved(
GCBase::IDTracker::ReservedObjectID::Number)));
#endif

// Verify there are no numeric nodes
auto nodesIt = nodes.begin();
Expand Down
1 change: 0 additions & 1 deletion unittests/VMRuntime/Instrumentation/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

set(ISources
ApproxIntegralTest.cpp
ProcessStatsTest.cpp
)

add_hermes_unittest(HermesInstrumentationTests
Expand Down
2 changes: 1 addition & 1 deletion unsupported/juno/crates/juno_support/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ license = "MIT"

[dependencies]
libcplusplus = { path = "../libcplusplus" }
base64 = "0.13"
base64 = "0.21.7"
anyhow = "1.0"
thiserror = "1.0"
url = "2.2.2"
Expand Down
15 changes: 13 additions & 2 deletions unsupported/juno/crates/juno_support/src/fetchurl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,21 @@ use std::fs::File;
use std::io::Read;

use anyhow;
use base64;
use base64::alphabet::URL_SAFE;
use base64::engine::general_purpose::GeneralPurpose;
use base64::engine::general_purpose::GeneralPurposeConfig;
use base64::engine::general_purpose::PAD;
use base64::engine::DecodePaddingMode;
use base64::Engine;
use thiserror;
use url::Url;

// Bring back the pre 0.20 bevahiour and allow either padded or un-padded base64 strings at decode time.
const URL_SAFE_INDIFFERENT: GeneralPurpose = GeneralPurpose::new(
&URL_SAFE,
PAD.with_decode_padding_mode(DecodePaddingMode::Indifferent),
);

#[derive(thiserror::Error, Debug)]
pub enum FetchError {
#[error("URL parse error")]
Expand Down Expand Up @@ -125,7 +136,7 @@ fn fetch_data(url: &Url) -> Result<Data, FetchError> {
return Err(FetchError::InvalidURL("data URL unsupported encoding"));
}

let buf = base64::decode_config(data, base64::URL_SAFE).map_err(|e| {
let buf: Vec<u8> = URL_SAFE_INDIFFERENT.decode(data).map_err(|e| {
FetchError::DecodeError(anyhow::anyhow!(e).context("error decoding data URL"))
})?;

Expand Down

0 comments on commit 5d9cc93

Please sign in to comment.