Skip to content

Instantly share code, notes, and snippets.

@jeremy-hiatt
Created March 12, 2021 02:17
Show Gist options
  • Save jeremy-hiatt/424b2aad09b68f2f7fa589c0a3c86ac4 to your computer and use it in GitHub Desktop.
Save jeremy-hiatt/424b2aad09b68f2f7fa589c0a3c86ac4 to your computer and use it in GitHub Desktop.
Trigger NEON alignment fault
#include <glib.h>
#include <glib-object.h>
#include <graphene.h>
#include <stdio.h>
G_BEGIN_DECLS
#define GST_TYPE_GRAPHENE_ALIGN (graphene_align_get_type())
#define GRAPHENE_ALIGN(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_GRAPHENE_ALIGN,GrapheneAlign))
#define GST_IS_GRAPHENE_ALIGN(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_GRAPHENE_ALIGN))
#define GRAPHENE_ALIGN_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass) ,GST_TYPE_GRAPHENE_ALIGN,GrapheneAlignClass))
#define GST_IS_GRAPHENE_ALIGN_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass) ,GST_TYPE_GRAPHENE_ALIGN))
#define GRAPHENE_ALIGN_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj) ,GST_TYPE_GRAPHENE_ALIGN,GrapheneAlignClass))
typedef struct _GrapheneAlign GrapheneAlign;
typedef struct _GrapheneAlignClass GrapheneAlignClass;
struct _GrapheneAlign
{
GObject parent;
// Simulate a bunch of other fields
char padding[64];
graphene_vec3_t aligned_vector;
};
struct _GrapheneAlignClass
{
GObjectClass filter_class;
};
GType graphene_align_get_type (void);
G_END_DECLS
#define graphene_align_parent_class parent_class
G_DEFINE_TYPE (GrapheneAlign, graphene_align,
G_TYPE_OBJECT);
static void
graphene_align_class_init (GrapheneAlignClass * klass) {
}
static void
graphene_align_init (GrapheneAlign * align)
{
printf("Pointer to instance is %p, alignment is %#0x\n", align, G_ALIGNOF(GrapheneAlign));
graphene_vec3_init(&align->aligned_vector, 0., 0., 0.);
}
int main(int argc, char **argv) {
unsigned pad = 0;
GrapheneAlign *align;
printf("Offset of graphene vector: %#0x\n", offsetof(GrapheneAlign, aligned_vector));
if (argc > 1) {
pad = atoi(argv[1]);
}
if (pad > 0) {
printf("Allocating %u padding bytes\n", pad);
g_slice_alloc(pad);
} else {
printf("No padding bytes allocated\n");
}
align = g_object_new(graphene_align_get_type(), NULL);
g_object_unref(align);
return 0;
}
@jeremy-hiatt
Copy link
Author

Sample Makefile:

CFLAGS = -Wall

PKGCONFIG_DEPS = graphene-1.0 gobject-2.0

CFLAGS += $(shell pkg-config --cflags $(PKGCONFIG_DEPS))
LDFLAGS += $(shell pkg-config --libs-only-L $(PKGCONFIG_DEPS))
LDLIBS += $(shell pkg-config --libs-only-l $(PKGCONFIG_DEPS))

graphene-align: graphene-align.o

Sample invocation:

./graphene-align 0 # Doesn't crash
./graphene-align 1024 # Doesn't crash
./graphene-align 16 # Crashes reliably on Cortex-A9

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment