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

Arch-specific options for integer type mapping #115

Open
XuehaiPan opened this issue Dec 1, 2021 · 1 comment
Open

Arch-specific options for integer type mapping #115

XuehaiPan opened this issue Dec 1, 2021 · 1 comment

Comments

@XuehaiPan
Copy link

Firstly, thanks for creating and maintaining such a useful utility.

I found commit ddcfe26 changes the default type mapping for long and int type. Indeed, unless the size is explicitly specified (e.g. uint64_t), the sizes of types int / unsigned int / long / unsigned long are arch-specific and implementation-specific. The change in commit ddcfe26 seems to be correct.

However, such changes will break the signature of existing Go bindings between versions (NVIDIA/go-nvml#31 (comment)). If the user wants to generate bindings for a specific platform (e.g. x86_64_linux), type unsigned long should be translated to uint or uint64 type.

Should we make this mapping to be arch-specific (wiki/Parser-config-section#arch)?

var arches = map[string]TargetArch{
"386": Arch32,
"arm": ArchArm32,
"aarch64": ArchArm64,
"armv7a": ArchArm32,
"armv8a": ArchArm64,
"armeabi-v7a": ArchArm32,
"armeabi-v8a": ArchArm64,
"armbe": ArchArm32,
"mips": Arch32,
"mipsle": Arch32,
"sparc": Arch32,
"amd64": Arch64,
"amd64p32": ArchArm32,
"arm64": ArchArm64,
"arm64be": ArchArm64,
"ppc64": Arch64,
"ppc64le": Arch64,
"mips64": Arch64,
"mips64le": Arch64,
"mips64p32": Arch48,
"mips64p32le": Arch48,
"sparc64": Arch64,
}

Or is there a way to add some option in the configuration to customize it?

Related issue NVIDIA/go-nvml#31.

@XuehaiPan
Copy link
Author

XuehaiPan commented Dec 1, 2021

C snippet:

#include <limits.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>

// User defined function in C
void printf_ulong(unsigned long *x)
{
	*x = (unsigned long)UINT32_MAX + 1;
	printf("Value in     function: %lu\n", *x);
}

// Binding in Golang
void go_binding()
{
	uint64_t value64 = 0;
	printf("Value before function: %lu\n", value64);
	printf_ulong((unsigned long *)&value64);
	printf("Value after  function: %lu\n", value64);
}

int main()
{
	go_binding();
	return 0;
}

On x86_64 Linux machine (Ubuntu 20.04 LTS), compile and run with g++ 9.3.0:

$ uname -io 
x86_64 GNU/Linux

$ gcc --version
gcc (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$ gcc -std=c99 "test.c" -o "test" && { ./"test"; rm "test" }
Value before function: 0
Value in     function: 4294967296
Value after  function: 4294967296

C snippet (change types to uint32_t but keep the function input type as unsigned long):

#include <limits.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>

// User defined function in C
void printf_ulong(unsigned long *x)
{
	*x = (unsigned long)UINT32_MAX + 1;
	printf("Value in     function: %lu\n", *x);
}

// Binding in Golang
void go_binding()
{
	uint32_t value32 = 0;
	printf("Value before function: %u\n", value32);
	printf_ulong((unsigned long *)&value32);
	printf("Value after  function: %u\n", value32);
}

int main()
{
	go_binding();
	return 0;
}

Got errors:

$ gcc -std=c99 "test.c" -o "test" && { ./"test"; rm "test" }
Value before function: 0
Value in     function: 4294967296
Value after  function: 0
*** stack smashing detected ***: terminated

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