Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ReadPointer function can return nullptr #81

Open
Hikai opened this issue May 5, 2023 · 0 comments
Open

ReadPointer function can return nullptr #81

Hikai opened this issue May 5, 2023 · 0 comments

Comments

@Hikai
Copy link

Hikai commented May 5, 2023

let i32 = {
    size: 4,
    indirection: 1,
    get: function get(buf, offset) {
        return buf['readInt32' + "LE"](offset || 0);
    },
    set: function set(buf, offset, val) {
        return buf['writeInt32' + "LE"](val, offset || 0);
    }
};
let PtrI32 = refType(i32);
var test_alloc = alloc(PtrI32);
var test_deref = test_alloc.deref()

// A segment fault occurs on the line below.
test_deref[0]; // or console.log(test_deref);

I edited ReadPointer function like as follows.

// binding.cc, Line 322
Value ReadPointer(const CallbackInfo& args) {
  Env env = args.Env();
  char* ptr = AddressForArgs(args);

  if (ptr == nullptr) {
    throw Error::New(env, "readPointer: Cannot read from nullptr pointer");
  }

  int64_t size = args[2].ToNumber();

  printf("ptr: %p\n", ptr);
  char* val = *reinterpret_cast<char**>(ptr);
  printf("val: %p\n", val);
  return WrapPointer(env, val, size);
}

Then, executed the node and got the result as follows.

ptr: 0000024B2112E8D0
val: 0000000000000002

I did not believe these results.
So, some tests have been performed.

#include <iostream>

int main(void)
{
	char ptr[1024] = "";
	printf("ptr: %p\n", ptr);

	char* val = *reinterpret_cast<char**>(ptr);
	printf("val: %p\n", val);

}

Result:

ptr: 00CFF8FC
val: 00000000

I think need to patch a part of the ReadPointer function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant