Skip to content

Instantly share code, notes, and snippets.

@tony-o

tony-o/help.c Secret

Created November 22, 2022 08:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tony-o/5c8b950657869d5672dfc47a043b6a08 to your computer and use it in GitHub Desktop.
Save tony-o/5c8b950657869d5672dfc47a043b6a08 to your computer and use it in GitHub Desktop.
#include <stdlib.h>
struct TODD {
int x;
};
struct TODD * parray(int n) {
int sz = sizeof(size_t);
struct TODD *ts = malloc(n*sz);
for(int i=0; i<n; i++){
ts[i].x = i;
}
return ts;
}
# gcc -shared -ohelp.so help.c
use NativeCall;
class TODD is repr('CStruct') {
has int32 $.x;
}
sub parray(int32) returns Pointer is native('./help.so') {*}
my $head = parray(10);
(0..^10).map(-> $i {
say nativecast(
TODD,
Pointer.new($head + ($i * nativesizeof(TODD))),
).x;
});
# must be run in same dir as help.c or you must correct the path on line 7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment