WARNING! Access to this system is limited to authorised users only.
Unauthorised users may be subject to prosecution.
Unauthorised access to this system is a criminal offence under Australian law (Federal Crimes Act 1914 Part VIA)
It is a criminal offence to:
(1) Obtain access to data without authority. -Penalty 2 years imprisonment.
(2) Damage, delete, alter or insert data without authority. -Penalty 10 years imprisonment.
User activity is monitored and recorded. Anyone using this system expressly consents to such monitoring and recording.

Commit 23932cb1 authored by Kunshan Wang's avatar Kunshan Wang
Browse files

WIP: Tested id_of and name_of.

parent 325749b4
...@@ -101,15 +101,21 @@ object ClientAccessibleClassExposer { ...@@ -101,15 +101,21 @@ object ClientAccessibleClassExposer {
val ptr = NativeClientSupport.stringPool.getOrElseUpdate(str, { val ptr = NativeClientSupport.stringPool.getOrElseUpdate(str, {
val bytes = str.getBytes(StandardCharsets.US_ASCII) val bytes = str.getBytes(StandardCharsets.US_ASCII)
val newPtr = jnrMemoryManager.allocateDirect(bytes.size + 1) val newPtr = jnrMemoryManager.allocateDirect(bytes.size + 1)
newPtr.put(0L, bytes, 0, 0) newPtr.put(0L, bytes, 0, bytes.length)
newPtr.putByte(bytes.length, 0)
newPtr newPtr
}) })
ptr.address() ptr.address()
} }
private def getObj[T](buffer: Buffer, index: Int, orm: ObjectReferenceManager[T]): T = { private def getObj[T](buffer: Buffer, index: Int, orm: ObjectReferenceManager[T]): T = {
val addr = buffer.getLong(index) val arg0 = buffer.getLong(index)
orm.get(jnrMemoryManager.newPointer(addr)) val objAddr = theMemory.getAddress(arg0)
val obj = orm.get(jnrMemoryManager.newPointer(objAddr))
if (obj == null) {
throw new UvmRefImplException("Exposed object not found. Address: %d 0x%x".format(objAddr, objAddr))
}
obj
} }
// Reflection utils. // Reflection utils.
......
...@@ -13,31 +13,37 @@ class NativeClientSupportTest extends UvmBundleTesterBase { ...@@ -13,31 +13,37 @@ class NativeClientSupportTest extends UvmBundleTesterBase {
"uvm.refimpl.nat" -> DEBUG) "uvm.refimpl.nat" -> DEBUG)
preloadBundles("tests/uvm-refimpl-test/primitives.uir") preloadBundles("tests/uvm-refimpl-test/primitives.uir")
val fileName = "./tests/c-snippets/ncs_tests.so" val fileName = "./tests/c-snippets/ncs_tests.so"
if (!new java.io.File(fileName).isFile()) { if (!new java.io.File(fileName).isFile()) {
throw new RuntimeException("Need to compile the ncs_tests.so library. cd into tests/c-snippets and invoke 'make'.") throw new RuntimeException("Need to compile the ncs_tests.so library. cd into tests/c-snippets and invoke 'make'.")
} }
trait NcsTestsLib { trait NcsTestsLib {
def test_basic(mvm: Word): Unit def test_basic(mvm: Word, theID: Int, theName: String): Int
} }
val ncs_tests = LibraryLoader.create(classOf[NcsTestsLib]).load(fileName) val ncs_tests = LibraryLoader.create(classOf[NcsTestsLib]).load(fileName)
"The ClientAccessibleClassExposer" should "enumerate declared methods" in { "The ClientAccessibleClassExposer" should "enumerate declared methods" in {
val cace = new ClientAccessibleClassExposer(NativeMuVM) val cace = new ClientAccessibleClassExposer(NativeMuVM)
val microVMAddr = NativeClientSupport.exposeMicroVM(microVM) val microVMAddr = NativeClientSupport.exposeMicroVM(microVM)
val funcTable = cace.makeFuncTable(microVMAddr) val funcTable = cace.makeFuncTable(microVMAddr)
val funcTablePtr = NativeClientSupport.funcTableToPointer(funcTable) val funcTablePtr = NativeClientSupport.funcTableToPointer(funcTable)
val header = funcTablePtr.getAddress(0) val header = funcTablePtr.getAddress(0)
val mvm =NativeClientSupport.microVMs.get(jnrMemoryManager.newPointer(header)) val mvm = NativeClientSupport.microVMs.get(jnrMemoryManager.newPointer(header))
mvm shouldBe microVM mvm shouldBe microVM
ncs_tests.test_basic(funcTable) val theName = "@i64"
val theID = microVM.idOf(theName)
val result = ncs_tests.test_basic(funcTable, theID, theName)
if (result == 0) {
fail("Failed in the native program.")
}
} }
} }
\ No newline at end of file
#include <stdio.h> #include <stdio.h>
#include <string.h>
#include <muapi.h> #include <muapi.h>
void test_basic(MuVM *mvm) { int test_basic(MuVM *mvm, int theID, char *theName) {
printf("[C] header: %p\n", mvm->header); printf("[C] header: %p\n", mvm->header);
printf("[C] new_context: %p\n", mvm->new_context); printf("[C] new_context: %p\n", mvm->new_context);
printf("[C] id_of: %p\n", mvm->id_of); printf("[C] id_of: %p\n", mvm->id_of);
printf("[C] name_of: %p\n", mvm->name_of); printf("[C] name_of: %p\n", mvm->name_of);
printf("[C] set_trap_handler: %p\n", mvm->set_trap_handler); printf("[C] set_trap_handler: %p\n", mvm->set_trap_handler);
int id = mvm->id_of(mvm, theName);
printf("[C] id = %d\n", id);
if (id != theID) {
printf("[C] ID %d is not equal to %d\n", id, theID);
return 0;
}
char *name = mvm->name_of(mvm, theID);
printf("[C] name = %s\n", name);
if (strcmp(name, theName) != 0) {
printf("[C] name %s is not equal to %s\n", name, theName);
return 0;
}
return 1;
} }
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment