Skip to content

Commit e96470a

Browse files
fixup! Add test
1 parent d098df3 commit e96470a

File tree

4 files changed

+93
-0
lines changed

4 files changed

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