Skip to content
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions lldb/source/Expression/IRMemoryMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,15 @@ void IRMemoryMap::WritePointerToMemory(lldb::addr_t process_address,
lldb::addr_t address, Status &error) {
error.Clear();

/// Only ask the Process to fix the address if this address belongs to the
/// process. An address belongs to the process if the Allocation policy is not
/// eAllocationPolicyHostOnly.
auto it = FindAllocation(address, 1);
if (it == m_allocations.end() ||
it->second.m_policy != AllocationPolicy::eAllocationPolicyHostOnly)
if (auto process_sp = GetProcessWP().lock())
address = process_sp->FixAnyAddress(address);

Scalar scalar(address);

WriteScalarToMemory(process_address, scalar, GetAddressByteSize(), error);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
C_SOURCES := main.c
include Makefile.rules
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import lldb
import json
import os
from lldbsuite.test.decorators import *
from lldbsuite.test.lldbtest import *
from lldbsuite.test import lldbutil


@skipIf(archs=no_match(["arm64", "arm64e"]))
class TestArmPointerMetadataStripping(TestBase):
# Use extra_symbols.json as a template to add a new symbol whose address
# contains non-zero high order bits set.
def create_symbols_file(self):
template_path = os.path.join(self.getSourceDir(), "extra_symbols.json")
with open(template_path, "r") as f:
symbols_data = json.load(f)

target = self.dbg.GetSelectedTarget()
symbols_data["triple"] = target.GetTriple()

module = target.GetModuleAtIndex(0)
symbols_data["uuid"] = module.GetUUIDString()

json_filename = self.getBuildArtifact("extra_symbols.json")
with open(json_filename, "w") as file:
json.dump(symbols_data, file, indent=4)

return json_filename

def test(self):
self.build()
src = lldb.SBFileSpec("main.c")
target, process, thread, bkpt = lldbutil.run_to_source_breakpoint(
self, "break here", src
)

self.expect_expr("get_high_bits(&x)", result_value="0")
self.expect_expr("get_high_bits(&myglobal)", result_value="0")

symbols_file = self.create_symbols_file()

# The high order bits should be stripped.
self.runCmd(f"target module add {symbols_file}")
self.expect_expr("get_high_bits(&myglobal_json)", result_value="0")

# Mark all bits as used for addresses and ensure bits are no longer stripped.
self.runCmd("settings set target.process.virtual-addressable-bits 64")
self.expect_expr(
"get_high_bits(&myglobal_json)", result_value=str(0x1200000000000000)
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"triple": "replace me",
"uuid": "replace me",
"type": "executable",
"sections": [
{
"name": "__DATA",
"type": "data",
"address": 1297224342667202580,
"size": 16
}
],
"symbols": [
{
"name": "myglobal_json",
"size": 8,
"type": "data",
"address": 1297224342667202580
}
]
}
20 changes: 20 additions & 0 deletions lldb/test/API/macosx/arm-pointer-metadata-stripping/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include <assert.h>
#include <stdint.h>
#include <stdio.h>


int myglobal = 41;

uint64_t get_high_bits(void *ptr) {
uint64_t mask = ~((1ULL << 48) - 1);
uint64_t ptrtoint = (uint64_t)ptr;
uint64_t high_bits = ptrtoint & mask;
printf("Higher bits are = %llx\n", high_bits);
return high_bits;
}

int main (){
int x = 42;
assert(0 == get_high_bits(&x));
return 0; //break here
}
Loading