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

size is not correct on struct_kinfo_proc from sys/sysctl.h #115

Closed
inv2004 opened this issue Aug 1, 2024 · 8 comments
Closed

size is not correct on struct_kinfo_proc from sys/sysctl.h #115

inv2004 opened this issue Aug 1, 2024 · 8 comments

Comments

@inv2004
Copy link

inv2004 commented Aug 1, 2024

nim-c-code:

  {.emit: """
#include <stdio.h>
#include <sys/sysctl.h>
""".}

  proc f() =
    {.emit: """
    printf("sz=%d\n", sizeof(struct kinfo_proc));
""".}
  f()
# sz=648

nim-code:

import futhark

importc:
  path: "/Library/Developer/CommandLineTools/SDKs/MacOSX14.4.sdk/usr/include"
  "sys/sysctl.h"

echo "sz = ", sizeof(struct_kinfo_proc)
# sz = 656

Do you know what could be the reason? Thank you

@inv2004
Copy link
Author

inv2004 commented Aug 1, 2024

additional info: 8 bytes diff is on extern_proc from sys/proc.h

@PMunch
Copy link
Owner

PMunch commented Aug 1, 2024

Hmm, sysctl is deprecated and removed from glibc. Unfortunately this means I don't have the header on my machine so I can't really debug this. Wild guess though is that those two builds end up linking against a slightly different version of sys/sysctl.h, or that it's some GCC/Clang difference.

@inv2004
Copy link
Author

inv2004 commented Aug 1, 2024

I was trying to use it on my mac. Let me try to extract minimal headers/structs

@inv2004
Copy link
Author

inv2004 commented Aug 2, 2024

#define MAXCOMLEN       16

typedef int                     __int32_t;

typedef int             boolean_t;
typedef long                    __darwin_time_t;
typedef __int32_t       __darwin_suseconds_t;
typedef char *          caddr_t;
typedef unsigned int    u_int;

typedef unsigned int            u_int32_t;
typedef u_int32_t               fixpt_t;

typedef unsigned long long      u_int64_t;
typedef u_int64_t               u_quad_t;

typedef unsigned int            __uint32_t;
typedef __uint32_t      __darwin_sigset_t; 
typedef __darwin_sigset_t               sigset_t;

typedef unsigned char   u_char;

typedef __int32_t       __darwin_pid_t;
typedef __darwin_pid_t        pid_t;

struct timeval
{
	__darwin_time_t         tv_sec;         /* seconds */
	__darwin_suseconds_t    tv_usec;        /* and microseconds */
};

struct  itimerval {
	struct  timeval it_interval;    /* timer interval */
	struct  timeval it_value;       /* current value */
};

/* Exported fields for kern sysctls */
struct extern_proc4 {
	union {
		struct {
			struct  proc *__p_forw; /* Doubly-linked run/sleep queue. */
			struct  proc *__p_back;
		} p_st1;
		struct timeval __p_starttime;   /* process start time */
	} p_un;
	struct  vmspace *p_vmspace;     /* Address space. */
	struct  sigacts *p_sigacts;     /* Signal actions, state (PROC ONLY). */
	int     p_flag;                 /* P_* flags. */
	char    p_stat;                 /* S* process status. */
	pid_t   p_pid;                  /* Process identifier. */
	pid_t   p_oppid;         /* Save parent pid during ptrace. XXX */
	int     p_dupfd;         /* Sideways return value from fdopen. XXX */
	/* Mach related  */
	caddr_t user_stack;     /* where user stack was allocated */
	void    *exit_thread;   /* XXX Which thread is exiting? */
	int             p_debugger;             /* allow to debug */
	boolean_t       sigwait;        /* indication to suspend */
	/* scheduling */
	u_int   p_estcpu;        /* Time averaged value of p_cpticks. */
	int     p_cpticks;       /* Ticks of cpu time. */
	fixpt_t p_pctcpu;        /* %cpu for this process during p_swtime */
	void    *p_wchan;        /* Sleep address. */
	char    *p_wmesg;        /* Reason for sleep. */
	u_int   p_swtime;        /* Time swapped in or out. */
	u_int   p_slptime;       /* Time since last blocked. */
};

@inv2004
Copy link
Author

inv2004 commented Aug 2, 2024

struct extern_proc4 {
	u_int   p_swtime;        /* Time swapped in or out. */
	u_int   p_slptime;       /* Time since last blocked. */
};
sz = 8
sz = 16

--added--
I am not sure, but my assumption that the u_int from .h file classified like nim's uint

--added--
rename "u_int", "u_intt" helps. Not sure if futhark should handle it or not

PMunch added a commit that referenced this issue Aug 2, 2024
The type override system exists to allow people to manually wrap parts
of code that Futhark might struggle with. This system also means that if
Nim has an existing type that has the same name as a C defined type then
the nim defined type will take precedence. In #115 it was discovered
that defining `u_int` as `unsigned int` meant that Nims `uint` which is
64 bits long took precedence over the 32 bit long `u_int`. This caused
structures to have incorrect sizes and invalid alignment. To help
against this, and to warn users trying to implement their own types
incorrectly, this warning has been added.
PMunch added a commit that referenced this issue Aug 2, 2024
This adds the list of types defined in `system.nim` to the list of
identifiers Futhark will consider as collisions. This means that the
code from #115 will not redefine `u_int` to mean `uint` and break sizes
and alignment, and hopefully avoid similar mistakes in the future.
@PMunch
Copy link
Owner

PMunch commented Aug 2, 2024

Aha! Wow that's an inconvenient bug, and hard to catch.

In 0.13.3 I've added a warning for when overridden types don't match in size, that should make these errors easier to catch in the future. I've also added a collision check for all the types in system.nim, so u_int now gets renamed so it doesn't collide with uint.

This last fix should mean that your code now works out of the box. Please try the code again and check.

@PMunch
Copy link
Owner

PMunch commented Aug 17, 2024

Did you ever get this tested?

@inv2004
Copy link
Author

inv2004 commented Aug 17, 2024

I changed my work macbook to lenovo. But if I remember correct it worked with simple example.

Thank you

@inv2004 inv2004 closed this as completed Aug 17, 2024
This issue was closed.
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

2 participants