Skip to content

Commit fd053ba

Browse files
fixup! Add test
1 parent d098df3 commit fd053ba

File tree

4 files changed

+95
-0
lines changed

4 files changed

+95
-0
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
C_SOURCES := main.c
2+
#CFLAGS_EXTRAS := --target=arm64e-apple-macosx13.0
3+
include Makefile.rules
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import lldb
2+
import json
3+
import tempfile
4+
import os
5+
from lldbsuite.test.decorators import *
6+
from lldbsuite.test.lldbtest import *
7+
from lldbsuite.test import lldbutil
8+
9+
10+
@skipIf(archs=no_match(["arm64", "arm64e"]))
11+
class TestArmPointerMetadataStripping(TestBase):
12+
# Use extra_symbols.json as a template to add a new symbol whose address
13+
# contains non-zero high order bits set.
14+
def create_symbols_file(self):
15+
template_path = os.path.join(self.getSourceDir(), "extra_symbols.json")
16+
with open(template_path, "r") as f:
17+
symbols_data = json.load(f)
18+
19+
target = self.dbg.GetSelectedTarget()
20+
symbols_data["triple"] = target.GetTriple()
21+
22+
module = target.GetModuleAtIndex(0)
23+
symbols_data["uuid"] = module.GetUUIDString()
24+
25+
json_filename = self.getBuildArtifact("extra_symbols.json")
26+
with open(json_filename, "w") as file:
27+
json.dump(symbols_data, file, indent=4)
28+
29+
return json_filename
30+
31+
def test(self):
32+
self.build()
33+
src = lldb.SBFileSpec("main.c")
34+
target, process, thread, bkpt = lldbutil.run_to_source_breakpoint(
35+
self, "break here", src
36+
)
37+
38+
self.expect_expr("get_high_bits(&x)", result_value="0")
39+
self.expect_expr("get_high_bits(&myglobal)", result_value="0")
40+
41+
symbols_file = self.create_symbols_file()
42+
43+
# The high order bits should be stripped.
44+
self.runCmd(f"target module add {symbols_file}")
45+
self.expect_expr("get_high_bits(&myglobal_json)", result_value="0")
46+
47+
# Mark all bits as used for addresses and ensure bits are no longer stripped.
48+
self.runCmd("settings set target.process.virtual-addressable-bits 64")
49+
self.expect_expr(
50+
"get_high_bits(&myglobal_json)", result_value=str(0x1200000000000000)
51+
)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"triple": "replace me",
3+
"uuid": "replace me",
4+
"type": "executable",
5+
"sections": [
6+
{
7+
"name": "__DATA",
8+
"type": "data",
9+
"address": 1297224342667202580,
10+
"size": 16
11+
}
12+
],
13+
"symbols": [
14+
{
15+
"name": "myglobal_json",
16+
"size": 8,
17+
"type": "data",
18+
"address": 1297224342667202580
19+
}
20+
]
21+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include <assert.h>
2+
#include <stdint.h>
3+
#include <stdio.h>
4+
5+
6+
int myglobal = 41;
7+
8+
uint64_t get_high_bits(void *ptr) {
9+
uint64_t mask = ~((1ULL << 48) - 1);
10+
uint64_t ptrtoint = (uint64_t)ptr;
11+
uint64_t high_bits = ptrtoint & mask;
12+
printf("Higher bits are = %llx\n", high_bits);
13+
return high_bits;
14+
}
15+
16+
int main (){
17+
int x = 42;
18+
assert(0 == get_high_bits(&x));
19+
return 0; //break here
20+
}

0 commit comments

Comments
 (0)